blob: 27967dd90f8f3f772f7c9a83d2fd8c823fc86f69 [file] [log] [blame]
Thomas Gleixner84a14ae2019-05-28 09:57:07 -07001// SPDX-License-Identifier: GPL-2.0-only
Anton Vorontsov22b238b2007-07-31 00:38:44 -07002/*
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 *
Anton Vorontsov22b238b2007-07-31 00:38:44 -07008 * 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>
Adrian Remondab78ce7e2015-03-10 16:12:30 -040015#include <string.h>
Tiezhu Yang470a0722020-02-13 12:16:07 +080016#include <errno.h>
Anton Vorontsov22b238b2007-07-31 00:38:44 -070017#include <getopt.h>
18#include <fcntl.h>
Frode Isaksen9006a7b2017-03-21 16:48:46 +010019#include <time.h>
Anton Vorontsov22b238b2007-07-31 00:38:44 -070020#include <sys/ioctl.h>
Baruch Siach8736f802016-08-12 16:04:33 +030021#include <linux/ioctl.h>
Joshua Clayton7af475a2015-11-18 14:30:39 -080022#include <sys/stat.h>
Anton Vorontsov22b238b2007-07-31 00:38:44 -070023#include <linux/types.h>
24#include <linux/spi/spidev.h>
25
26#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
27
28static void pabort(const char *s)
29{
Tiezhu Yang470a0722020-02-13 12:16:07 +080030 if (errno != 0)
31 perror(s);
32 else
33 printf("%s\n", s);
34
Anton Vorontsov22b238b2007-07-31 00:38:44 -070035 abort();
36}
37
WANG Congcd583102007-10-16 01:27:47 -070038static const char *device = "/dev/spidev1.1";
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +010039static uint32_t mode;
Anton Vorontsov22b238b2007-07-31 00:38:44 -070040static uint8_t bits = 8;
Joshua Clayton7af475a2015-11-18 14:30:39 -080041static char *input_file;
Joshua Clayton983b278862015-11-18 14:30:40 -080042static char *output_file;
Anton Vorontsov22b238b2007-07-31 00:38:44 -070043static uint32_t speed = 500000;
44static uint16_t delay;
Adrian Remonda31a5c5a2015-03-10 16:12:31 -040045static int verbose;
Frode Isaksen9006a7b2017-03-21 16:48:46 +010046static int transfer_size;
47static int iterations;
48static int interval = 5; /* interval in seconds for showing transfer rate */
Anton Vorontsov22b238b2007-07-31 00:38:44 -070049
Adrian Remonda30061912015-03-10 16:12:32 -040050uint8_t default_tx[] = {
51 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
52 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
53 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
54 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
55 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
56 0xF0, 0x0D,
57};
58
59uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
60char *input_tx;
61
Joshua Claytonb2cfc902015-11-18 14:30:42 -080062static void hex_dump(const void *src, size_t length, size_t line_size,
63 char *prefix)
Adrian Remondab78ce7e2015-03-10 16:12:30 -040064{
65 int i = 0;
66 const unsigned char *address = src;
67 const unsigned char *line = address;
68 unsigned char c;
69
70 printf("%s | ", prefix);
71 while (length-- > 0) {
72 printf("%02X ", *address++);
73 if (!(++i % line_size) || (length == 0 && i % line_size)) {
74 if (length == 0) {
75 while (i++ % line_size)
76 printf("__ ");
77 }
Geert Uytterhoeven35386df2018-09-03 19:33:23 +020078 printf(" |");
Adrian Remondab78ce7e2015-03-10 16:12:30 -040079 while (line < address) {
80 c = *line++;
Geert Uytterhoeven35386df2018-09-03 19:33:23 +020081 printf("%c", (c < 32 || c > 126) ? '.' : c);
Adrian Remondab78ce7e2015-03-10 16:12:30 -040082 }
Geert Uytterhoeven35386df2018-09-03 19:33:23 +020083 printf("|\n");
Adrian Remondab78ce7e2015-03-10 16:12:30 -040084 if (length > 0)
85 printf("%s | ", prefix);
86 }
87 }
88}
89
Adrian Remonda30061912015-03-10 16:12:32 -040090/*
91 * Unescape - process hexadecimal escape character
92 * converts shell input "\x23" -> 0x23
93 */
Andrew Morton07eec622015-04-16 12:49:29 -070094static int unescape(char *_dst, char *_src, size_t len)
Adrian Remonda30061912015-03-10 16:12:32 -040095{
96 int ret = 0;
Joshua Claytona20874f2015-11-18 14:30:41 -080097 int match;
Adrian Remonda30061912015-03-10 16:12:32 -040098 char *src = _src;
99 char *dst = _dst;
100 unsigned int ch;
101
102 while (*src) {
103 if (*src == '\\' && *(src+1) == 'x') {
Joshua Claytona20874f2015-11-18 14:30:41 -0800104 match = sscanf(src + 2, "%2x", &ch);
105 if (!match)
106 pabort("malformed input string");
107
Adrian Remonda30061912015-03-10 16:12:32 -0400108 src += 4;
109 *dst++ = (unsigned char)ch;
110 } else {
111 *dst++ = *src++;
112 }
113 ret++;
114 }
115 return ret;
116}
117
118static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700119{
120 int ret;
Joshua Clayton983b278862015-11-18 14:30:40 -0800121 int out_fd;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700122 struct spi_ioc_transfer tr = {
123 .tx_buf = (unsigned long)tx,
124 .rx_buf = (unsigned long)rx,
Adrian Remonda30061912015-03-10 16:12:32 -0400125 .len = len,
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700126 .delay_usecs = delay,
127 .speed_hz = speed,
128 .bits_per_word = bits,
129 };
130
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100131 if (mode & SPI_TX_QUAD)
132 tr.tx_nbits = 4;
133 else if (mode & SPI_TX_DUAL)
134 tr.tx_nbits = 2;
135 if (mode & SPI_RX_QUAD)
136 tr.rx_nbits = 4;
137 else if (mode & SPI_RX_DUAL)
138 tr.rx_nbits = 2;
139 if (!(mode & SPI_LOOP)) {
140 if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
141 tr.rx_buf = 0;
142 else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
143 tr.tx_buf = 0;
144 }
145
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700146 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
Hector Palacios95b1ed22010-04-29 15:02:28 -0700147 if (ret < 1)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700148 pabort("can't send spi message");
149
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400150 if (verbose)
Adrian Remonda30061912015-03-10 16:12:32 -0400151 hex_dump(tx, len, 32, "TX");
Joshua Clayton983b278862015-11-18 14:30:40 -0800152
153 if (output_file) {
154 out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
155 if (out_fd < 0)
156 pabort("could not open output file");
157
158 ret = write(out_fd, rx, len);
159 if (ret != len)
Fabio Estevamedd38992015-12-04 10:59:14 -0200160 pabort("not all bytes written to output file");
Joshua Clayton983b278862015-11-18 14:30:40 -0800161
162 close(out_fd);
163 }
164
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100165 if (verbose)
Joshua Clayton983b278862015-11-18 14:30:40 -0800166 hex_dump(rx, len, 32, "RX");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700167}
168
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700169static void print_usage(const char *prog)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700170{
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100171 printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700172 puts(" -D --device device to use (default /dev/spidev1.1)\n"
173 " -s --speed max speed (Hz)\n"
174 " -d --delay delay (usec)\n"
Joshua Claytonb2cfc902015-11-18 14:30:42 -0800175 " -b --bpw bits per word\n"
Joshua Clayton7af475a2015-11-18 14:30:39 -0800176 " -i --input input data from a file (e.g. \"test.bin\")\n"
Joshua Clayton983b278862015-11-18 14:30:40 -0800177 " -o --output output data to a file (e.g. \"results.bin\")\n"
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700178 " -l --loop loopback\n"
179 " -H --cpha clock phase\n"
180 " -O --cpol clock polarity\n"
181 " -L --lsb least significant bit first\n"
182 " -C --cs-high chip select active high\n"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100183 " -3 --3wire SI/SO signals shared\n"
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400184 " -v --verbose Verbose (show tx buffer)\n"
Adrian Remonda30061912015-03-10 16:12:32 -0400185 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100186 " -N --no-cs no chip select\n"
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100187 " -R --ready slave pulls low to pause\n"
188 " -2 --dual dual transfer\n"
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100189 " -4 --quad quad transfer\n"
190 " -S --size transfer size\n"
191 " -I --iter iterations\n");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700192 exit(1);
193}
194
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700195static void parse_opts(int argc, char *argv[])
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700196{
197 while (1) {
WANG Congcd583102007-10-16 01:27:47 -0700198 static const struct option lopts[] = {
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700199 { "device", 1, 0, 'D' },
200 { "speed", 1, 0, 's' },
201 { "delay", 1, 0, 'd' },
202 { "bpw", 1, 0, 'b' },
Joshua Clayton7af475a2015-11-18 14:30:39 -0800203 { "input", 1, 0, 'i' },
Joshua Clayton983b278862015-11-18 14:30:40 -0800204 { "output", 1, 0, 'o' },
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700205 { "loop", 0, 0, 'l' },
206 { "cpha", 0, 0, 'H' },
207 { "cpol", 0, 0, 'O' },
208 { "lsb", 0, 0, 'L' },
209 { "cs-high", 0, 0, 'C' },
210 { "3wire", 0, 0, '3' },
David Brownellb55f6272009-06-30 11:41:26 -0700211 { "no-cs", 0, 0, 'N' },
212 { "ready", 0, 0, 'R' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100213 { "dual", 0, 0, '2' },
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400214 { "verbose", 0, 0, 'v' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100215 { "quad", 0, 0, '4' },
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100216 { "size", 1, 0, 'S' },
217 { "iter", 1, 0, 'I' },
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700218 { NULL, 0, 0, 0 },
219 };
220 int c;
221
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100222 c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:vS:I:",
Joshua Clayton7af475a2015-11-18 14:30:39 -0800223 lopts, NULL);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700224
225 if (c == -1)
226 break;
227
228 switch (c) {
229 case 'D':
230 device = optarg;
231 break;
232 case 's':
233 speed = atoi(optarg);
234 break;
235 case 'd':
236 delay = atoi(optarg);
237 break;
238 case 'b':
239 bits = atoi(optarg);
240 break;
Joshua Clayton7af475a2015-11-18 14:30:39 -0800241 case 'i':
242 input_file = optarg;
243 break;
Joshua Clayton983b278862015-11-18 14:30:40 -0800244 case 'o':
245 output_file = optarg;
246 break;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700247 case 'l':
248 mode |= SPI_LOOP;
249 break;
250 case 'H':
251 mode |= SPI_CPHA;
252 break;
253 case 'O':
254 mode |= SPI_CPOL;
255 break;
256 case 'L':
257 mode |= SPI_LSB_FIRST;
258 break;
259 case 'C':
260 mode |= SPI_CS_HIGH;
261 break;
262 case '3':
263 mode |= SPI_3WIRE;
264 break;
David Brownellb55f6272009-06-30 11:41:26 -0700265 case 'N':
266 mode |= SPI_NO_CS;
267 break;
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400268 case 'v':
269 verbose = 1;
270 break;
David Brownellb55f6272009-06-30 11:41:26 -0700271 case 'R':
272 mode |= SPI_READY;
273 break;
Adrian Remonda30061912015-03-10 16:12:32 -0400274 case 'p':
275 input_tx = optarg;
276 break;
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100277 case '2':
278 mode |= SPI_TX_DUAL;
279 break;
280 case '4':
281 mode |= SPI_TX_QUAD;
282 break;
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100283 case 'S':
284 transfer_size = atoi(optarg);
285 break;
286 case 'I':
287 iterations = atoi(optarg);
288 break;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700289 default:
290 print_usage(argv[0]);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700291 }
292 }
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100293 if (mode & SPI_LOOP) {
294 if (mode & SPI_TX_DUAL)
295 mode |= SPI_RX_DUAL;
296 if (mode & SPI_TX_QUAD)
297 mode |= SPI_RX_QUAD;
298 }
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700299}
300
Joshua Clayton5c437a42015-11-18 14:30:38 -0800301static void transfer_escaped_string(int fd, char *str)
302{
Geert Uytterhoeven0278b342016-09-09 09:02:51 +0200303 size_t size = strlen(str);
Joshua Clayton5c437a42015-11-18 14:30:38 -0800304 uint8_t *tx;
305 uint8_t *rx;
306
307 tx = malloc(size);
308 if (!tx)
309 pabort("can't allocate tx buffer");
310
311 rx = malloc(size);
312 if (!rx)
313 pabort("can't allocate rx buffer");
314
315 size = unescape((char *)tx, str, size);
316 transfer(fd, tx, rx, size);
317 free(rx);
318 free(tx);
319}
320
Joshua Clayton7af475a2015-11-18 14:30:39 -0800321static void transfer_file(int fd, char *filename)
322{
323 ssize_t bytes;
324 struct stat sb;
325 int tx_fd;
326 uint8_t *tx;
327 uint8_t *rx;
328
329 if (stat(filename, &sb) == -1)
330 pabort("can't stat input file");
331
332 tx_fd = open(filename, O_RDONLY);
Michal Vokáče634b762016-11-04 11:30:03 +0100333 if (tx_fd < 0)
Joshua Clayton7af475a2015-11-18 14:30:39 -0800334 pabort("can't open input file");
335
336 tx = malloc(sb.st_size);
337 if (!tx)
338 pabort("can't allocate tx buffer");
339
340 rx = malloc(sb.st_size);
341 if (!rx)
342 pabort("can't allocate rx buffer");
343
344 bytes = read(tx_fd, tx, sb.st_size);
345 if (bytes != sb.st_size)
346 pabort("failed to read input file");
347
348 transfer(fd, tx, rx, sb.st_size);
349 free(rx);
350 free(tx);
351 close(tx_fd);
352}
353
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100354static uint64_t _read_count;
355static uint64_t _write_count;
356
357static void show_transfer_rate(void)
358{
359 static uint64_t prev_read_count, prev_write_count;
360 double rx_rate, tx_rate;
361
362 rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0);
363 tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0);
364
365 printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate);
366
367 prev_read_count = _read_count;
368 prev_write_count = _write_count;
369}
370
371static void transfer_buf(int fd, int len)
372{
373 uint8_t *tx;
374 uint8_t *rx;
375 int i;
376
377 tx = malloc(len);
378 if (!tx)
379 pabort("can't allocate tx buffer");
380 for (i = 0; i < len; i++)
381 tx[i] = random();
382
383 rx = malloc(len);
384 if (!rx)
385 pabort("can't allocate rx buffer");
386
387 transfer(fd, tx, rx, len);
388
389 _write_count += len;
390 _read_count += len;
391
392 if (mode & SPI_LOOP) {
393 if (memcmp(tx, rx, len)) {
394 fprintf(stderr, "transfer error !\n");
395 hex_dump(tx, len, 32, "TX");
396 hex_dump(rx, len, 32, "RX");
397 exit(1);
398 }
399 }
400
401 free(rx);
402 free(tx);
403}
404
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700405int main(int argc, char *argv[])
406{
407 int ret = 0;
408 int fd;
409
410 parse_opts(argc, argv);
411
Tiezhu Yang1f3c3632020-02-13 12:16:06 +0800412 if (input_tx && input_file)
413 pabort("only one of -p and --input may be selected");
414
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700415 fd = open(device, O_RDWR);
416 if (fd < 0)
417 pabort("can't open device");
418
419 /*
420 * spi mode
421 */
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100422 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700423 if (ret == -1)
424 pabort("can't set spi mode");
425
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100426 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700427 if (ret == -1)
428 pabort("can't get spi mode");
429
430 /*
431 * bits per word
432 */
433 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
434 if (ret == -1)
435 pabort("can't set bits per word");
436
437 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
438 if (ret == -1)
439 pabort("can't get bits per word");
440
441 /*
442 * max speed hz
443 */
444 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
445 if (ret == -1)
446 pabort("can't set max speed hz");
447
448 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
449 if (ret == -1)
450 pabort("can't get max speed hz");
451
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100452 printf("spi mode: 0x%x\n", mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700453 printf("bits per word: %d\n", bits);
454 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
455
Joshua Clayton5c437a42015-11-18 14:30:38 -0800456 if (input_tx)
457 transfer_escaped_string(fd, input_tx);
Joshua Clayton7af475a2015-11-18 14:30:39 -0800458 else if (input_file)
459 transfer_file(fd, input_file);
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100460 else if (transfer_size) {
461 struct timespec last_stat;
462
463 clock_gettime(CLOCK_MONOTONIC, &last_stat);
464
465 while (iterations-- > 0) {
466 struct timespec current;
467
468 transfer_buf(fd, transfer_size);
469
470 clock_gettime(CLOCK_MONOTONIC, &current);
471 if (current.tv_sec - last_stat.tv_sec > interval) {
472 show_transfer_rate();
473 last_stat = current;
474 }
475 }
476 printf("total: tx %.1fKB, rx %.1fKB\n",
477 _write_count/1024.0, _read_count/1024.0);
478 } else
Adrian Remonda30061912015-03-10 16:12:32 -0400479 transfer(fd, default_tx, default_rx, sizeof(default_tx));
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700480
481 close(fd);
482
483 return ret;
484}