FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
samples/SpiTestTool/main.cpp at develop · ERRLSTAR/samples · GitHub
ERRLSTAR
/
samples
Public
forked from
ms-iot/samples
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
samples
/
SpiTestTool
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
418 lines (364 loc) · 13 KB
Breadcrumbs
samples
/
SpiTestTool
/
main.cpp
Copy path
File metadata and controls
418 lines (364 loc) · 13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
Copyright (c) Microsoft. All rights reserved.
//
//
spitesttool
//
//
Utility to read and write SPI devices from the command line.
//
#
include
<
ppltasks.h
>
#
include
<
collection.h
>
#
include
<
string
>
#
include
<
vector
>
#
include
<
sstream
>
#
include
<
iostream
>
#
include
<
cwctype
>
using
namespace
Platform
;
using
namespace
Windows
::Foundation
;
using
namespace
Windows
::Devices::Spi
;
class
wexception
{
public:
explicit
wexception
(
const
std::wstring &msg) : msg_(msg) { }
virtual
~wexception
() {
/*
empty
*/
}
virtual
const
wchar_t
*
wwhat
()
const
{
return
msg_.
c_str
();
}
private:
std::wstring msg_;
};
void
ListSpiControllers
()
{
using
namespace
Windows
::Devices::Enumeration
;
using
namespace
Platform
::Collections
;
String^ friendlyNameProperty =
L"
System.DeviceInterface.Spb.ControllerFriendlyName
"
;
auto
properties = ref
new
Vector<String^>();
properties->
Append
(friendlyNameProperty);
auto
dis =
concurrency::create_task
(
DeviceInformation::FindAllAsync
(
SpiDevice::GetDeviceSelector
(),
properties)).
get
();
if
(dis->
Size
<
1
) {
std::wcout <<
L"
There are no SPI controllers on this system.
\n
"
;
return
;
}
wprintf
(
L"
FriendlyName DeviceId
\n
"
);
for
(
const
auto
& di : dis) {
wprintf
(
L"
%12s %s
\n
"
,
((String^)di->
Properties
->
Lookup
(friendlyNameProperty))->
Data
(),
di->
Id
->
Data
());
}
}
SpiDevice^ MakeDevice (
String^ friendlyName,
int
chipSelectLine,
SpiMode mode,
int
dataBitLength,
int
clockFrequency
)
{
using
namespace
Windows
::Devices::Enumeration
;
String^ aqs;
if
(friendlyName)
aqs =
SpiDevice::GetDeviceSelector
(friendlyName);
else
aqs =
SpiDevice::GetDeviceSelector
();
auto
dis =
concurrency::create_task
(
DeviceInformation::FindAllAsync
(aqs)).
get
();
if
(dis->
Size
<
1
)
throw
wexception
(
L"
SPI bus not found.
"
);
String^ id = dis->
GetAt
(
0
)->
Id
;
auto
settings = ref
new
SpiConnectionSettings
(chipSelectLine);
if
(
int
(mode) != -
1
) {
settings->
Mode
= mode;
}
if
(dataBitLength !=
0
) {
settings->
DataBitLength
= dataBitLength;
}
if
(clockFrequency !=
0
) {
settings->
ClockFrequency
= clockFrequency;
}
auto
device =
concurrency::create_task
(
SpiDevice::FromIdAsync
(id, settings)).
get
();
if
(!device) {
std::wostringstream msg;
msg <<
L"
Chip select line
"
<< std::dec << chipSelectLine <<
L"
on bus
"
<< id->
Data
() <<
L"
is in use. Please ensure that no other applications are using SPI.
"
;
throw
wexception
(msg.
str
());
}
return
device;
}
std::wistream&
expect
(std::wistream& is,
wchar_t
delim)
{
wchar_t
ch;
while
(is.
get
(ch)) {
if
(ch == delim)
return
is;
if
(!
isspace
(ch)) {
is.
clear
(is.
failbit
);
break
;
}
}
return
is;
}
std::wistream&
operator
>> (std::wistream& is, std::vector<
BYTE
>& bytes)
{
bytes.
clear
();
if
(!
expect
(is, L
'
{
'
)) {
std::wcout <<
L"
Syntax error: expecting '{'
\n
"
;
return
is;
}
//
get a sequence of bytes, e.g.
//
write { 0 1 2 3 4 aa bb cc dd }
unsigned
int
byte;
while
(is >> std::hex >> byte) {
if
(byte >
0xff
) {
std::wcout <<
L"
Out of range [0, 0xff]:
"
<< std::hex << byte <<
L"
\n
"
;
is.
clear
(is.
failbit
);
return
is;
}
bytes.
push_back
(
static_cast
<
BYTE
>(byte));
}
if
(bytes.
empty
()) {
std::wcout <<
L"
Zero-length buffers are not allowed
\n
"
;
is.
clear
(is.
failbit
);
return
is;
}
is.
clear
();
if
(!
expect
(is, L
'
}
'
)) {
std::wcout <<
L"
Syntax error: expecting '}'
\n
"
;
return
is;
}
return
is;
}
std::wostream&
operator
<< (std::wostream& os, Array<
BYTE
>^& bytes)
{
for
(
auto
byte : bytes)
os <<
L"
"
<< std::hex << byte;
return
os;
}
PCWSTR
Help =
L"
Commands:
\n
"
L"
> write { 00 11 22 .. FF } Write bytes to device
\n
"
L"
> read N Read N bytes
\n
"
L"
> writeread { 00 11 .. FF } N Write bytes then read N bytes
\n
"
L"
> fullduplex { 00 11 .. FF } Perform full duplex transfer
\n
"
L"
> info Display device information
\n
"
L"
> help Display this help message
\n
"
L"
> quit Quit
\n\n
"
;
void
ShowPrompt
(SpiDevice^ device)
{
while
(std::wcin) {
std::wcout <<
L"
>
"
;
std::wstring line;
if
(!
std::getline
(std::wcin, line)) {
return
;
}
std::wistringstream
linestream
(line);
std::wstring command;
linestream >> command;
if
((command ==
L"
q
"
) || (command ==
L"
quit
"
)) {
return
;
}
else
if
((command ==
L"
h
"
) || (command ==
L"
help
"
)) {
std::wcout << Help;
}
else
if
(command ==
L"
write
"
) {
std::vector<
BYTE
> writeBuf;
if
(!(linestream >> writeBuf)) {
std::wcout <<
L"
Usage: write { 55 a0 ... ff }
\n
"
;
continue
;
}
device->
Write
(ArrayReference<
BYTE
>(
writeBuf.
data
(),
static_cast
<
unsigned
int
>(writeBuf.
size
())));
}
else
if
(command ==
L"
read
"
) {
//
expecting a single int, number of bytes to read
unsigned
int
bytesToRead;
if
(!(linestream >> std::dec >> bytesToRead)) {
std::wcout <<
L"
Expecting integer. e.g: read 4
\n
"
;
continue
;
}
auto
readBuf = ref
new
Array<
BYTE
>(bytesToRead);
device->
Read
(readBuf);
std::wcout << readBuf <<
L"
\n
"
;
}
else
if
(command ==
L"
writeread
"
) {
//
get a sequence of bytes, e.g.
//
write 0 1 2 3 4 aa bb cc dd
std::vector<
BYTE
> writeBuf;
if
(!(linestream >> writeBuf)) {
std::wcout <<
L"
Usage: writeread { 55 a0 ... ff } 4
\n
"
;
continue
;
}
unsigned
int
bytesToRead;
if
(!(linestream >> std::dec >> bytesToRead)) {
std::wcout <<
L"
Syntax error: expecting integer
\n
"
;
std::wcout <<
L"
Usage: writeread { 55 a0 ... ff } 4
\n
"
;
continue
;
}
auto
readBuf = ref
new
Array<
BYTE
>(bytesToRead);
device->
TransferSequential
(
ArrayReference<
BYTE
>(
writeBuf.
data
(),
static_cast
<
unsigned
int
>(writeBuf.
size
())),
readBuf);
std::wcout << readBuf <<
L"
\n
"
;
}
else
if
(command ==
L"
fullduplex
"
) {
std::vector<
BYTE
> writeBuf;
if
(!(linestream >> writeBuf)) {
std::wcout <<
L"
Usage: fullduplex { 55 a0 ... ff }
\n
"
;
continue
;
}
auto
readBuf = ref
new
Array<
BYTE
>(
static_cast
<
unsigned
int
>(writeBuf.
size
()));
device->
TransferFullDuplex
(
ArrayReference<
BYTE
>(
writeBuf.
data
(),
static_cast
<
unsigned
int
>(writeBuf.
size
())),
readBuf);
std::wcout << readBuf <<
L"
\n
"
;
}
else
if
(command ==
L"
info
"
) {
auto
settings = device->
ConnectionSettings
;
std::wcout <<
L"
DeviceId:
"
<< device->
DeviceId
->
Data
() <<
"
\n
"
;
std::wcout <<
L"
ChipSelectLine:
"
<< std::dec << settings->
ChipSelectLine
<<
L"
\n
"
;
std::wcout <<
L"
Mode:
"
<< std::dec <<
int
(settings->
Mode
) <<
L"
\n
"
;
std::wcout <<
L"
DataBitLength:
"
<< std::dec << settings->
DataBitLength
<<
L"
\n
"
;
std::wcout <<
L"
ClockFrequency:
"
<< std::dec << settings->
ClockFrequency
<<
L"
Hz
\n
"
;
}
else
if
(command.
empty
()) {
//
ignore
}
else
{
std::wcout <<
L"
Unrecognized command:
"
<< command <<
L"
. Type 'help' for command usage.
\n
"
;
}
}
}
void
PrintUsage
(
PCWSTR
name)
{
wprintf
(
L"
SpiTestTool: Command line SPI testing utility
\n
"
L"
Usage: %s [-list] [-n FriendlyName] [-c ChipSelectLine] [-m Mode]
"
L"
[-d DataBitLength] [-f ClockFrequency]
\n
"
L"
\n
"
L"
-list List available SPI controllers and exit.
\n
"
L"
FriendlyName The friendly name of the SPI controller over which
\n
"
L"
you wish to communicate. This parameter is
\n
"
L"
optional and defaults to the first enumerated SPI
\n
"
L"
controller.
\n
"
L"
ChipSelectLine The chip select line to use. This parameter is
\n
"
L"
optional and defaults to 0.
\n
"
L"
Mode The SPI mode to use (0-3). This parameter is
\n
"
L"
optional and defaults to Mode 0.
\n
"
L"
DataBitLength The data bit length to use. This parameter is optional
\n
"
L"
and defaults to 8.
\n
"
L"
ClockFrequency The SPI clock frequency to use. This parameter is
\n
"
L"
optional and defaults to 4Mhz.
\n
"
L"
\n
"
L"
Examples:
\n
"
L"
Connect to the first SPI controller found with default settings
\n
"
L"
(ChipSelectLine=0, Mode=0, DataBitLength=8, Frequency=4Mhz):
\n
"
L"
%s
\n
"
L"
\n
"
L"
List available SPI controllers and exit:
\n
"
L"
%s -list
\n
"
L"
\n
"
L"
Connect to SPI1 in mode 2, with default speed (4Mhz) and chip
\n
"
L"
select line (0):
\n
"
L"
%s -n SPI1 -m 2
\n
"
L"
\n
"
L"
Connect to chip select 1 on SPI1 in mode 2 at 1Mhz:
\n
"
L"
%s -c 1 -n SPI1 -m 2 -f 1000000
\n
"
,
name,
name,
name,
name,
name);
}
int
main
(Platform::Array<Platform::String^>^ args)
{
String^ friendlyName;
int
chipSelectLine =
0
;
SpiMode mode =
SpiMode
(-
1
);
int
dataBitLength =
0
;
int
clockFrequency =
0
;
for
(
unsigned
int
optind =
1
; optind < args->
Length
; optind +=
2
) {
PCWSTR
arg1 = args->
get
(optind)->
Data
();
if
(!
_wcsicmp
(arg1,
L"
-h
"
) || !
_wcsicmp
(arg1,
L"
/h
"
) ||
!
_wcsicmp
(arg1,
L"
-?
"
) || !
_wcsicmp
(arg1,
L"
/?
"
)) {
PrintUsage
(args->
get
(
0
)->
Data
());
return
0
;
}
if
(!
_wcsicmp
(arg1,
L"
-l
"
) || !
_wcsicmp
(arg1,
L"
-list
"
)) {
ListSpiControllers
();
return
0
;
}
if
(arg1[
0
] != L
'
-
'
) {
std::wcerr <<
L"
Unexpected positional parameter:
"
<< arg1 <<
L"
\n
"
;
return
1
;
}
if
(args->
get
(optind)->
Length
() !=
2
) {
std::wcerr <<
L"
Invalid option format:
"
<< arg1 <<
L"
\n
"
;
return
1
;
}
if
((optind +
1
) >= args->
Length
) {
std::wcerr <<
L"
Missing required parameter for option:
"
<< arg1 <<
L"
\n
"
;
return
1
;
}
PCWSTR
arg2 = args->
get
(optind +
1
)->
Data
();
wchar_t
*endptr;
switch
(
towlower
(arg1[
1
])) {
case
L
'
n
'
:
friendlyName = args->
get
(optind +
1
);
break
;
case
L
'
c
'
:
chipSelectLine =
static_cast
<
int
>(
wcstoul
(arg2, &endptr,
0
));
break
;
case
L
'
m
'
:
{
unsigned
long
modeUlong =
wcstoul
(arg2, &endptr,
0
);
if
((modeUlong <
0
) || (modeUlong >
3
)) {
std::wcerr <<
L"
Invalid mode value:
"
<< modeUlong <<
L"
. Mode must be in the range [0, 3]
\n
"
;
return
1
;
}
mode =
static_cast
<SpiMode>(modeUlong);
static_assert
(
int
(SpiMode::Mode0) ==
0
,
"
Verifying SpiMode::Mode0 is 0
"
);
static_assert
(
int
(SpiMode::Mode1) ==
1
,
"
Verifying SpiMode::Mode1 is 1
"
);
static_assert
(
int
(SpiMode::Mode2) ==
2
,
"
Verifying SpiMode::Mode2 is 2
"
);
static_assert
(
int
(SpiMode::Mode3) ==
3
,
"
Verifying SpiMode::Mode3 is 3
"
);
break
;
}
case
L
'
d
'
:
dataBitLength =
static_cast
<
int
>(
wcstoul
(arg2, &endptr,
0
));
if
(dataBitLength ==
0
) {
std::wcerr <<
L"
Invalid data bit length
\n
"
;
return
1
;
}
break
;
case
L
'
f
'
:
clockFrequency =
static_cast
<
int
>(
wcstoul
(arg2, &endptr,
0
));
if
(clockFrequency ==
0
) {
std::wcerr <<
L"
Invalid clock frequency
\n
"
;
return
1
;
}
break
;
default
:
std::wcerr <<
L"
Unrecognized option:
"
<< arg1 <<
L"
\n
"
;
return
1
;
}
}
try
{
auto
device =
MakeDevice
(
friendlyName,
chipSelectLine,
mode,
dataBitLength,
clockFrequency);
std::wcout <<
L"
Type 'help' for a list of commands
\n
"
;
ShowPrompt
(device);
}
catch
(
const
wexception& ex) {
std::wcerr <<
L"
Error:
"
<< ex.
wwhat
() <<
L"
\n
"
;
return
1
;
}
catch
(Platform::Exception^ ex) {
std::wcerr <<
L"
Error:
"
<< ex->
Message
->
Data
() <<
L"
\n
"
;
return
1
;
}
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL