spi: spidev_test: Check input_tx and input_file first after parse options
[linux-2.6-block.git] / tools / spi / spidev_test.c
CommitLineData
84a14ae8 1// SPDX-License-Identifier: GPL-2.0-only
22b238bd
AV
2/*
3 * SPI testing utility (using spidev driver)
4 *
5 * Copyright (c) 2007 MontaVista Software, Inc.
6 * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
7 *
22b238bd
AV
8 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
9 */
10
11#include <stdint.h>
12#include <unistd.h>
13#include <stdio.h>
14#include <stdlib.h>
b78ce7ed 15#include <string.h>
22b238bd
AV
16#include <getopt.h>
17#include <fcntl.h>
9006a7b3 18#include <time.h>
22b238bd 19#include <sys/ioctl.h>
8736f802 20#include <linux/ioctl.h>
7af475a5 21#include <sys/stat.h>
22b238bd
AV
22#include <linux/types.h>
23#include <linux/spi/spidev.h>
24
25#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
26
27static void pabort(const char *s)
28{
29 perror(s);
30 abort();
31}
32
cd58310d 33static const char *device = "/dev/spidev1.1";
c2e78c34 34static uint32_t mode;
22b238bd 35static uint8_t bits = 8;
7af475a5 36static char *input_file;
983b2788 37static char *output_file;
22b238bd
AV
38static uint32_t speed = 500000;
39static uint16_t delay;
31a5c5a7 40static int verbose;
9006a7b3
FI
41static int transfer_size;
42static int iterations;
43static int interval = 5; /* interval in seconds for showing transfer rate */
22b238bd 44
30061915
AR
45uint8_t default_tx[] = {
46 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
47 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
48 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
49 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
50 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
51 0xF0, 0x0D,
52};
53
54uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
55char *input_tx;
56
b2cfc904
JC
57static void hex_dump(const void *src, size_t length, size_t line_size,
58 char *prefix)
b78ce7ed
AR
59{
60 int i = 0;
61 const unsigned char *address = src;
62 const unsigned char *line = address;
63 unsigned char c;
64
65 printf("%s | ", prefix);
66 while (length-- > 0) {
67 printf("%02X ", *address++);
68 if (!(++i % line_size) || (length == 0 && i % line_size)) {
69 if (length == 0) {
70 while (i++ % line_size)
71 printf("__ ");
72 }
35386dfd 73 printf(" |");
b78ce7ed
AR
74 while (line < address) {
75 c = *line++;
35386dfd 76 printf("%c", (c < 32 || c > 126) ? '.' : c);
b78ce7ed 77 }
35386dfd 78 printf("|\n");
b78ce7ed
AR
79 if (length > 0)
80 printf("%s | ", prefix);
81 }
82 }
83}
84
30061915
AR
85/*
86 * Unescape - process hexadecimal escape character
87 * converts shell input "\x23" -> 0x23
88 */
07eec628 89static int unescape(char *_dst, char *_src, size_t len)
30061915
AR
90{
91 int ret = 0;
a20874f7 92 int match;
30061915
AR
93 char *src = _src;
94 char *dst = _dst;
95 unsigned int ch;
96
97 while (*src) {
98 if (*src == '\\' && *(src+1) == 'x') {
a20874f7
JC
99 match = sscanf(src + 2, "%2x", &ch);
100 if (!match)
101 pabort("malformed input string");
102
30061915
AR
103 src += 4;
104 *dst++ = (unsigned char)ch;
105 } else {
106 *dst++ = *src++;
107 }
108 ret++;
109 }
110 return ret;
111}
112
113static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
22b238bd
AV
114{
115 int ret;
983b2788 116 int out_fd;
22b238bd
AV
117 struct spi_ioc_transfer tr = {
118 .tx_buf = (unsigned long)tx,
119 .rx_buf = (unsigned long)rx,
30061915 120 .len = len,
22b238bd
AV
121 .delay_usecs = delay,
122 .speed_hz = speed,
123 .bits_per_word = bits,
124 };
125
c2e78c34
GU
126 if (mode & SPI_TX_QUAD)
127 tr.tx_nbits = 4;
128 else if (mode & SPI_TX_DUAL)
129 tr.tx_nbits = 2;
130 if (mode & SPI_RX_QUAD)
131 tr.rx_nbits = 4;
132 else if (mode & SPI_RX_DUAL)
133 tr.rx_nbits = 2;
134 if (!(mode & SPI_LOOP)) {
135 if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
136 tr.rx_buf = 0;
137 else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
138 tr.tx_buf = 0;
139 }
140
22b238bd 141 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
95b1ed2a 142 if (ret < 1)
22b238bd
AV
143 pabort("can't send spi message");
144
31a5c5a7 145 if (verbose)
30061915 146 hex_dump(tx, len, 32, "TX");
983b2788
JC
147
148 if (output_file) {
149 out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
150 if (out_fd < 0)
151 pabort("could not open output file");
152
153 ret = write(out_fd, rx, len);
154 if (ret != len)
edd3899c 155 pabort("not all bytes written to output file");
983b2788
JC
156
157 close(out_fd);
158 }
159
9006a7b3 160 if (verbose)
983b2788 161 hex_dump(rx, len, 32, "RX");
22b238bd
AV
162}
163
b7ed698c 164static void print_usage(const char *prog)
22b238bd 165{
9006a7b3 166 printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog);
22b238bd
AV
167 puts(" -D --device device to use (default /dev/spidev1.1)\n"
168 " -s --speed max speed (Hz)\n"
169 " -d --delay delay (usec)\n"
b2cfc904 170 " -b --bpw bits per word\n"
7af475a5 171 " -i --input input data from a file (e.g. \"test.bin\")\n"
983b2788 172 " -o --output output data to a file (e.g. \"results.bin\")\n"
22b238bd
AV
173 " -l --loop loopback\n"
174 " -H --cpha clock phase\n"
175 " -O --cpol clock polarity\n"
176 " -L --lsb least significant bit first\n"
177 " -C --cs-high chip select active high\n"
925d16a2 178 " -3 --3wire SI/SO signals shared\n"
31a5c5a7 179 " -v --verbose Verbose (show tx buffer)\n"
30061915 180 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
925d16a2 181 " -N --no-cs no chip select\n"
c2e78c34
GU
182 " -R --ready slave pulls low to pause\n"
183 " -2 --dual dual transfer\n"
9006a7b3
FI
184 " -4 --quad quad transfer\n"
185 " -S --size transfer size\n"
186 " -I --iter iterations\n");
22b238bd
AV
187 exit(1);
188}
189
b7ed698c 190static void parse_opts(int argc, char *argv[])
22b238bd
AV
191{
192 while (1) {
cd58310d 193 static const struct option lopts[] = {
22b238bd
AV
194 { "device", 1, 0, 'D' },
195 { "speed", 1, 0, 's' },
196 { "delay", 1, 0, 'd' },
197 { "bpw", 1, 0, 'b' },
7af475a5 198 { "input", 1, 0, 'i' },
983b2788 199 { "output", 1, 0, 'o' },
22b238bd
AV
200 { "loop", 0, 0, 'l' },
201 { "cpha", 0, 0, 'H' },
202 { "cpol", 0, 0, 'O' },
203 { "lsb", 0, 0, 'L' },
204 { "cs-high", 0, 0, 'C' },
205 { "3wire", 0, 0, '3' },
b55f627f
DB
206 { "no-cs", 0, 0, 'N' },
207 { "ready", 0, 0, 'R' },
c2e78c34 208 { "dual", 0, 0, '2' },
31a5c5a7 209 { "verbose", 0, 0, 'v' },
c2e78c34 210 { "quad", 0, 0, '4' },
9006a7b3
FI
211 { "size", 1, 0, 'S' },
212 { "iter", 1, 0, 'I' },
22b238bd
AV
213 { NULL, 0, 0, 0 },
214 };
215 int c;
216
9006a7b3 217 c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:vS:I:",
7af475a5 218 lopts, NULL);
22b238bd
AV
219
220 if (c == -1)
221 break;
222
223 switch (c) {
224 case 'D':
225 device = optarg;
226 break;
227 case 's':
228 speed = atoi(optarg);
229 break;
230 case 'd':
231 delay = atoi(optarg);
232 break;
233 case 'b':
234 bits = atoi(optarg);
235 break;
7af475a5
JC
236 case 'i':
237 input_file = optarg;
238 break;
983b2788
JC
239 case 'o':
240 output_file = optarg;
241 break;
22b238bd
AV
242 case 'l':
243 mode |= SPI_LOOP;
244 break;
245 case 'H':
246 mode |= SPI_CPHA;
247 break;
248 case 'O':
249 mode |= SPI_CPOL;
250 break;
251 case 'L':
252 mode |= SPI_LSB_FIRST;
253 break;
254 case 'C':
255 mode |= SPI_CS_HIGH;
256 break;
257 case '3':
258 mode |= SPI_3WIRE;
259 break;
b55f627f
DB
260 case 'N':
261 mode |= SPI_NO_CS;
262 break;
31a5c5a7
AR
263 case 'v':
264 verbose = 1;
265 break;
b55f627f
DB
266 case 'R':
267 mode |= SPI_READY;
268 break;
30061915
AR
269 case 'p':
270 input_tx = optarg;
271 break;
c2e78c34
GU
272 case '2':
273 mode |= SPI_TX_DUAL;
274 break;
275 case '4':
276 mode |= SPI_TX_QUAD;
277 break;
9006a7b3
FI
278 case 'S':
279 transfer_size = atoi(optarg);
280 break;
281 case 'I':
282 iterations = atoi(optarg);
283 break;
22b238bd
AV
284 default:
285 print_usage(argv[0]);
22b238bd
AV
286 }
287 }
c2e78c34
GU
288 if (mode & SPI_LOOP) {
289 if (mode & SPI_TX_DUAL)
290 mode |= SPI_RX_DUAL;
291 if (mode & SPI_TX_QUAD)
292 mode |= SPI_RX_QUAD;
293 }
22b238bd
AV
294}
295
5c437a40
JC
296static void transfer_escaped_string(int fd, char *str)
297{
0278b34b 298 size_t size = strlen(str);
5c437a40
JC
299 uint8_t *tx;
300 uint8_t *rx;
301
302 tx = malloc(size);
303 if (!tx)
304 pabort("can't allocate tx buffer");
305
306 rx = malloc(size);
307 if (!rx)
308 pabort("can't allocate rx buffer");
309
310 size = unescape((char *)tx, str, size);
311 transfer(fd, tx, rx, size);
312 free(rx);
313 free(tx);
314}
315
7af475a5
JC
316static void transfer_file(int fd, char *filename)
317{
318 ssize_t bytes;
319 struct stat sb;
320 int tx_fd;
321 uint8_t *tx;
322 uint8_t *rx;
323
324 if (stat(filename, &sb) == -1)
325 pabort("can't stat input file");
326
327 tx_fd = open(filename, O_RDONLY);
e634b76c 328 if (tx_fd < 0)
7af475a5
JC
329 pabort("can't open input file");
330
331 tx = malloc(sb.st_size);
332 if (!tx)
333 pabort("can't allocate tx buffer");
334
335 rx = malloc(sb.st_size);
336 if (!rx)
337 pabort("can't allocate rx buffer");
338
339 bytes = read(tx_fd, tx, sb.st_size);
340 if (bytes != sb.st_size)
341 pabort("failed to read input file");
342
343 transfer(fd, tx, rx, sb.st_size);
344 free(rx);
345 free(tx);
346 close(tx_fd);
347}
348
9006a7b3
FI
349static uint64_t _read_count;
350static uint64_t _write_count;
351
352static void show_transfer_rate(void)
353{
354 static uint64_t prev_read_count, prev_write_count;
355 double rx_rate, tx_rate;
356
357 rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0);
358 tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0);
359
360 printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate);
361
362 prev_read_count = _read_count;
363 prev_write_count = _write_count;
364}
365
366static void transfer_buf(int fd, int len)
367{
368 uint8_t *tx;
369 uint8_t *rx;
370 int i;
371
372 tx = malloc(len);
373 if (!tx)
374 pabort("can't allocate tx buffer");
375 for (i = 0; i < len; i++)
376 tx[i] = random();
377
378 rx = malloc(len);
379 if (!rx)
380 pabort("can't allocate rx buffer");
381
382 transfer(fd, tx, rx, len);
383
384 _write_count += len;
385 _read_count += len;
386
387 if (mode & SPI_LOOP) {
388 if (memcmp(tx, rx, len)) {
389 fprintf(stderr, "transfer error !\n");
390 hex_dump(tx, len, 32, "TX");
391 hex_dump(rx, len, 32, "RX");
392 exit(1);
393 }
394 }
395
396 free(rx);
397 free(tx);
398}
399
22b238bd
AV
400int main(int argc, char *argv[])
401{
402 int ret = 0;
403 int fd;
404
405 parse_opts(argc, argv);
406
1f3c3632
TY
407 if (input_tx && input_file)
408 pabort("only one of -p and --input may be selected");
409
22b238bd
AV
410 fd = open(device, O_RDWR);
411 if (fd < 0)
412 pabort("can't open device");
413
414 /*
415 * spi mode
416 */
c2e78c34 417 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
22b238bd
AV
418 if (ret == -1)
419 pabort("can't set spi mode");
420
c2e78c34 421 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
22b238bd
AV
422 if (ret == -1)
423 pabort("can't get spi mode");
424
425 /*
426 * bits per word
427 */
428 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
429 if (ret == -1)
430 pabort("can't set bits per word");
431
432 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
433 if (ret == -1)
434 pabort("can't get bits per word");
435
436 /*
437 * max speed hz
438 */
439 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
440 if (ret == -1)
441 pabort("can't set max speed hz");
442
443 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
444 if (ret == -1)
445 pabort("can't get max speed hz");
446
c2e78c34 447 printf("spi mode: 0x%x\n", mode);
22b238bd
AV
448 printf("bits per word: %d\n", bits);
449 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
450
5c437a40
JC
451 if (input_tx)
452 transfer_escaped_string(fd, input_tx);
7af475a5
JC
453 else if (input_file)
454 transfer_file(fd, input_file);
9006a7b3
FI
455 else if (transfer_size) {
456 struct timespec last_stat;
457
458 clock_gettime(CLOCK_MONOTONIC, &last_stat);
459
460 while (iterations-- > 0) {
461 struct timespec current;
462
463 transfer_buf(fd, transfer_size);
464
465 clock_gettime(CLOCK_MONOTONIC, &current);
466 if (current.tv_sec - last_stat.tv_sec > interval) {
467 show_transfer_rate();
468 last_stat = current;
469 }
470 }
471 printf("total: tx %.1fKB, rx %.1fKB\n",
472 _write_count/1024.0, _read_count/1024.0);
473 } else
30061915 474 transfer(fd, default_tx, default_rx, sizeof(default_tx));
22b238bd
AV
475
476 close(fd);
477
478 return ret;
479}