forked from parallella/parallella-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspitest.cpp
More file actions
261 lines (191 loc) · 6.45 KB
/
spitest.cpp
File metadata and controls
261 lines (191 loc) · 6.45 KB
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
/*
Copyright (c) 2014, Adapteva, Inc.
Contributed by Fred Huettig <Fred@Adapteva.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
spitest.cpp
Test of the Parallella bit-bang'in SPI library. Requires a connection
to an external SPI device, for example through the Porcupine board.
Allows any combination of read & write to one device
Build:
gcc -o spitest spitest.cpp para_spi.cpp para_gpio.cpp para_gpio.c -lstdc++ -Wall
Notes:
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "para_spi.h"
void Usage() {
printf("Usage: spitest -h (show this help)\n");
printf(" spitest [-m MNO] [PP QQ RR SS]\n\n");
printf(" options:\n");
printf(" -m MNO : Set SPI mode:\n");
printf(" M = active enable level (0/1)\n");
printf(" N = resting clock polarity (0/1)\n");
printf(" O = phase:\n");
printf(" 0=sample on 1st clock edge\n");
printf(" 1=sample on 2nd clock edge\n\n");
printf(" PP : Clock signal GPIO ID (default 65)\n");
printf(" QQ : MOSI signal GPIO ID (default 66)\n");
printf(" RR : MISO signal GPIO ID (default 68)\n");
printf(" SS : Select signal GPIO ID (default 64)\n\n");
printf(" spitest will prompt for a # of bits to write and a hex value.\n");
printf(" The number of bits must be no more than 32, the hex number\n");
printf(" should not have any leading '0x' etc.\n\n");
printf(" #bits hexval > 16 ABCD\n\n");
printf(" Or, enter a quote-delimited string to send to the device one\n");
printf(" byte at a time. Non-ASCII characters may be sent using \\xNN\n");
printf(" (two hex digits), or a literal '\\' or '\"' may be sent with\n");
printf(" '\\\\' or '\\\"' respectively.\n");
printf(" Any return data is discarded.\n\n");
printf(" Enter an empty line to repeat the last operation or 'q' to quit.\n\n");
printf("Note: This application needs (probably root) access to /sys/class/gpio\n");
printf("\n");
}
int hextoi(char c) {
int x;
if(c >= 'a')
x = c - 'a' + 10;
else if(c >= 'A')
x = c - 'A' + 10;
else
x = c - '0';
if(x < 0 || x > 15)
x = -1;
return x;
}
int main(int argc, char *argv[]) {
int nCLK=65, nMOSI=66, nMISO=68, nSS=64, n, c;
int nCPOL=0, nCPHA=0, nEPOL=0, res, sendstr=0, done=0;
char str[256], strLast[256];
unsigned nbits=0, wval=0, rval=0;
CParaSpi spi;
printf("SPITEST - Basic test of Parallella SPI Module\n\n");
while ((c = getopt(argc, argv, "hm:")) != -1) {
switch (c) {
case 'h':
Usage();
exit(0);
case 'm':
n = atoi(optarg);
if(n < 0 || n > 111) {
fprintf(stderr, "Mode setting must be 000 - 111, exiting");
exit(1);
}
nEPOL = n / 100;
nCPOL = (n % 100) / 10;
nCPHA = n % 10;
break;
case '?':
if (optopt == 'w')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
exit(1);
default:
fprintf(stderr, "Unexpected result from getopt?? (%d:%c)\n", c, c);
exit(1);
}
}
if(optind < argc) {
if(optind != argc - 4) {
fprintf(stderr, "If entering pin numbers, please enter all 4");
exit(1);
}
nCLK = atoi(argv[argc-4]);
nMOSI = atoi(argv[argc-3]);
nMISO = atoi(argv[argc-2]);
nSS = atoi(argv[argc-1]);
}
printf("Initializing object...\n");
spi.SetMode(nCPOL, nCPHA, nEPOL);
res = spi.AssignPins(nCLK, nMOSI, nMISO, nSS);
if(res) {
fprintf(stderr, "spi.AssignPins returned %d", res);
exit(1);
}
if(!spi.IsOK()) {
fprintf(stderr, "SPI Object creation failed, exiting\n");
exit(1);
}
printf("Success\n");
strLast[0] = 0; // Just in case someone gets sneaky
printf("Enter 'q' to quit.\n\n");
while(!done) {
printf("#bits hexval > ");
if(fgets(str, 256, stdin) == NULL)
break;
if(str[0] == 'q' || str[0] == 'Q')
break;
if(str[0] != '\n') {
if(str[0] == '"') {
strcpy(strLast, str);
sendstr = 1;
} else if(sscanf(str, "%d %x", &nbits, &wval) == 2) {
sendstr = 0;
} else {
printf(" ???\n");
continue;
}
}
if(sendstr) {
for(n=1; strLast[n] && strLast[n] != '"'; n++) {
c = -1;
if(strLast[n] == '\\') {
n++;
if(str[n] == 'x') {
c = hextoi(strLast[++n]) << 4;
c += hextoi(strLast[++n]);
}
}
if(c >= 0)
res = spi.Xfer(8, c);
else
res = spi.Xfer(8, strLast[n]);
if(res) {
fprintf(stderr, "Xfer() returned %d, exiting\n", res);
done = 1;
}
}
printf("String sent.\n\n");
} else {
res = spi.Xfer(nbits, &wval, &rval);
if(res) {
fprintf(stderr, "Xfer() returned %d, exiting\n", res);
done = 1;
}
printf("Sent 0x%08X, Rcvd 0x%08X\n\n", wval, rval);
}
}
// done:
printf("Closing\n");
spi.Close();
return 0;
}