blob: 3559e7646256144d5fb0fc6a8b2c7708a9cbc103 [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>
Anton Vorontsov22b238b2007-07-31 00:38:44 -070016#include <getopt.h>
17#include <fcntl.h>
Frode Isaksen9006a7b2017-03-21 16:48:46 +010018#include <time.h>
Anton Vorontsov22b238b2007-07-31 00:38:44 -070019#include <sys/ioctl.h>
Baruch Siach8736f802016-08-12 16:04:33 +030020#include <linux/ioctl.h>
Joshua Clayton7af475a2015-11-18 14:30:39 -080021#include <sys/stat.h>
Anton Vorontsov22b238b2007-07-31 00:38:44 -070022#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
WANG Congcd583102007-10-16 01:27:47 -070033static const char *device = "/dev/spidev1.1";
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +010034static uint32_t mode;
Anton Vorontsov22b238b2007-07-31 00:38:44 -070035static uint8_t bits = 8;
Joshua Clayton7af475a2015-11-18 14:30:39 -080036static char *input_file;
Joshua Clayton983b278862015-11-18 14:30:40 -080037static char *output_file;
Anton Vorontsov22b238b2007-07-31 00:38:44 -070038static uint32_t speed = 500000;
39static uint16_t delay;
Adrian Remonda31a5c5a2015-03-10 16:12:31 -040040static int verbose;
Frode Isaksen9006a7b2017-03-21 16:48:46 +010041static int transfer_size;
42static int iterations;
43static int interval = 5; /* interval in seconds for showing transfer rate */
Anton Vorontsov22b238b2007-07-31 00:38:44 -070044
Adrian Remonda30061912015-03-10 16:12:32 -040045uint8_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
Joshua Claytonb2cfc902015-11-18 14:30:42 -080057static void hex_dump(const void *src, size_t length, size_t line_size,
58 char *prefix)
Adrian Remondab78ce7e2015-03-10 16:12:30 -040059{
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 }
Geert Uytterhoeven35386df2018-09-03 19:33:23 +020073 printf(" |");
Adrian Remondab78ce7e2015-03-10 16:12:30 -040074 while (line < address) {
75 c = *line++;
Geert Uytterhoeven35386df2018-09-03 19:33:23 +020076 printf("%c", (c < 32 || c > 126) ? '.' : c);
Adrian Remondab78ce7e2015-03-10 16:12:30 -040077 }
Geert Uytterhoeven35386df2018-09-03 19:33:23 +020078 printf("|\n");
Adrian Remondab78ce7e2015-03-10 16:12:30 -040079 if (length > 0)
80 printf("%s | ", prefix);
81 }
82 }
83}
84
Adrian Remonda30061912015-03-10 16:12:32 -040085/*
86 * Unescape - process hexadecimal escape character
87 * converts shell input "\x23" -> 0x23
88 */
Andrew Morton07eec622015-04-16 12:49:29 -070089static int unescape(char *_dst, char *_src, size_t len)
Adrian Remonda30061912015-03-10 16:12:32 -040090{
91 int ret = 0;
Joshua Claytona20874f2015-11-18 14:30:41 -080092 int match;
Adrian Remonda30061912015-03-10 16:12:32 -040093 char *src = _src;
94 char *dst = _dst;
95 unsigned int ch;
96
97 while (*src) {
98 if (*src == '\\' && *(src+1) == 'x') {
Joshua Claytona20874f2015-11-18 14:30:41 -080099 match = sscanf(src + 2, "%2x", &ch);
100 if (!match)
101 pabort("malformed input string");
102
Adrian Remonda30061912015-03-10 16:12:32 -0400103 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)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700114{
115 int ret;
Joshua Clayton983b278862015-11-18 14:30:40 -0800116 int out_fd;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700117 struct spi_ioc_transfer tr = {
118 .tx_buf = (unsigned long)tx,
119 .rx_buf = (unsigned long)rx,
Adrian Remonda30061912015-03-10 16:12:32 -0400120 .len = len,
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700121 .delay_usecs = delay,
122 .speed_hz = speed,
123 .bits_per_word = bits,
124 };
125
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100126 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
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700141 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
Hector Palacios95b1ed22010-04-29 15:02:28 -0700142 if (ret < 1)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700143 pabort("can't send spi message");
144
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400145 if (verbose)
Adrian Remonda30061912015-03-10 16:12:32 -0400146 hex_dump(tx, len, 32, "TX");
Joshua Clayton983b278862015-11-18 14:30:40 -0800147
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)
Fabio Estevamedd38992015-12-04 10:59:14 -0200155 pabort("not all bytes written to output file");
Joshua Clayton983b278862015-11-18 14:30:40 -0800156
157 close(out_fd);
158 }
159
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100160 if (verbose)
Joshua Clayton983b278862015-11-18 14:30:40 -0800161 hex_dump(rx, len, 32, "RX");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700162}
163
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700164static void print_usage(const char *prog)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700165{
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100166 printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700167 puts(" -D --device device to use (default /dev/spidev1.1)\n"
168 " -s --speed max speed (Hz)\n"
169 " -d --delay delay (usec)\n"
Joshua Claytonb2cfc902015-11-18 14:30:42 -0800170 " -b --bpw bits per word\n"
Joshua Clayton7af475a2015-11-18 14:30:39 -0800171 " -i --input input data from a file (e.g. \"test.bin\")\n"
Joshua Clayton983b278862015-11-18 14:30:40 -0800172 " -o --output output data to a file (e.g. \"results.bin\")\n"
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700173 " -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"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100178 " -3 --3wire SI/SO signals shared\n"
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400179 " -v --verbose Verbose (show tx buffer)\n"
Adrian Remonda30061912015-03-10 16:12:32 -0400180 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100181 " -N --no-cs no chip select\n"
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100182 " -R --ready slave pulls low to pause\n"
183 " -2 --dual dual transfer\n"
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100184 " -4 --quad quad transfer\n"
185 " -S --size transfer size\n"
186 " -I --iter iterations\n");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700187 exit(1);
188}
189
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700190static void parse_opts(int argc, char *argv[])
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700191{
192 while (1) {
WANG Congcd583102007-10-16 01:27:47 -0700193 static const struct option lopts[] = {
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700194 { "device", 1, 0, 'D' },
195 { "speed", 1, 0, 's' },
196 { "delay", 1, 0, 'd' },
197 { "bpw", 1, 0, 'b' },
Joshua Clayton7af475a2015-11-18 14:30:39 -0800198 { "input", 1, 0, 'i' },
Joshua Clayton983b278862015-11-18 14:30:40 -0800199 { "output", 1, 0, 'o' },
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700200 { "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' },
David Brownellb55f6272009-06-30 11:41:26 -0700206 { "no-cs", 0, 0, 'N' },
207 { "ready", 0, 0, 'R' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100208 { "dual", 0, 0, '2' },
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400209 { "verbose", 0, 0, 'v' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100210 { "quad", 0, 0, '4' },
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100211 { "size", 1, 0, 'S' },
212 { "iter", 1, 0, 'I' },
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700213 { NULL, 0, 0, 0 },
214 };
215 int c;
216
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100217 c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:vS:I:",
Joshua Clayton7af475a2015-11-18 14:30:39 -0800218 lopts, NULL);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700219
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;
Joshua Clayton7af475a2015-11-18 14:30:39 -0800236 case 'i':
237 input_file = optarg;
238 break;
Joshua Clayton983b278862015-11-18 14:30:40 -0800239 case 'o':
240 output_file = optarg;
241 break;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700242 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;
David Brownellb55f6272009-06-30 11:41:26 -0700260 case 'N':
261 mode |= SPI_NO_CS;
262 break;
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400263 case 'v':
264 verbose = 1;
265 break;
David Brownellb55f6272009-06-30 11:41:26 -0700266 case 'R':
267 mode |= SPI_READY;
268 break;
Adrian Remonda30061912015-03-10 16:12:32 -0400269 case 'p':
270 input_tx = optarg;
271 break;
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100272 case '2':
273 mode |= SPI_TX_DUAL;
274 break;
275 case '4':
276 mode |= SPI_TX_QUAD;
277 break;
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100278 case 'S':
279 transfer_size = atoi(optarg);
280 break;
281 case 'I':
282 iterations = atoi(optarg);
283 break;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700284 default:
285 print_usage(argv[0]);
286 break;
287 }
288 }
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100289 if (mode & SPI_LOOP) {
290 if (mode & SPI_TX_DUAL)
291 mode |= SPI_RX_DUAL;
292 if (mode & SPI_TX_QUAD)
293 mode |= SPI_RX_QUAD;
294 }
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700295}
296
Joshua Clayton5c437a42015-11-18 14:30:38 -0800297static void transfer_escaped_string(int fd, char *str)
298{
Geert Uytterhoeven0278b342016-09-09 09:02:51 +0200299 size_t size = strlen(str);
Joshua Clayton5c437a42015-11-18 14:30:38 -0800300 uint8_t *tx;
301 uint8_t *rx;
302
303 tx = malloc(size);
304 if (!tx)
305 pabort("can't allocate tx buffer");
306
307 rx = malloc(size);
308 if (!rx)
309 pabort("can't allocate rx buffer");
310
311 size = unescape((char *)tx, str, size);
312 transfer(fd, tx, rx, size);
313 free(rx);
314 free(tx);
315}
316
Joshua Clayton7af475a2015-11-18 14:30:39 -0800317static void transfer_file(int fd, char *filename)
318{
319 ssize_t bytes;
320 struct stat sb;
321 int tx_fd;
322 uint8_t *tx;
323 uint8_t *rx;
324
325 if (stat(filename, &sb) == -1)
326 pabort("can't stat input file");
327
328 tx_fd = open(filename, O_RDONLY);
Michal Vokáče634b762016-11-04 11:30:03 +0100329 if (tx_fd < 0)
Joshua Clayton7af475a2015-11-18 14:30:39 -0800330 pabort("can't open input file");
331
332 tx = malloc(sb.st_size);
333 if (!tx)
334 pabort("can't allocate tx buffer");
335
336 rx = malloc(sb.st_size);
337 if (!rx)
338 pabort("can't allocate rx buffer");
339
340 bytes = read(tx_fd, tx, sb.st_size);
341 if (bytes != sb.st_size)
342 pabort("failed to read input file");
343
344 transfer(fd, tx, rx, sb.st_size);
345 free(rx);
346 free(tx);
347 close(tx_fd);
348}
349
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100350static uint64_t _read_count;
351static uint64_t _write_count;
352
353static void show_transfer_rate(void)
354{
355 static uint64_t prev_read_count, prev_write_count;
356 double rx_rate, tx_rate;
357
358 rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0);
359 tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0);
360
361 printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate);
362
363 prev_read_count = _read_count;
364 prev_write_count = _write_count;
365}
366
367static void transfer_buf(int fd, int len)
368{
369 uint8_t *tx;
370 uint8_t *rx;
371 int i;
372
373 tx = malloc(len);
374 if (!tx)
375 pabort("can't allocate tx buffer");
376 for (i = 0; i < len; i++)
377 tx[i] = random();
378
379 rx = malloc(len);
380 if (!rx)
381 pabort("can't allocate rx buffer");
382
383 transfer(fd, tx, rx, len);
384
385 _write_count += len;
386 _read_count += len;
387
388 if (mode & SPI_LOOP) {
389 if (memcmp(tx, rx, len)) {
390 fprintf(stderr, "transfer error !\n");
391 hex_dump(tx, len, 32, "TX");
392 hex_dump(rx, len, 32, "RX");
393 exit(1);
394 }
395 }
396
397 free(rx);
398 free(tx);
399}
400
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700401int main(int argc, char *argv[])
402{
403 int ret = 0;
404 int fd;
405
406 parse_opts(argc, argv);
407
408 fd = open(device, O_RDWR);
409 if (fd < 0)
410 pabort("can't open device");
411
412 /*
413 * spi mode
414 */
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100415 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700416 if (ret == -1)
417 pabort("can't set spi mode");
418
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100419 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700420 if (ret == -1)
421 pabort("can't get spi mode");
422
423 /*
424 * bits per word
425 */
426 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
427 if (ret == -1)
428 pabort("can't set bits per word");
429
430 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
431 if (ret == -1)
432 pabort("can't get bits per word");
433
434 /*
435 * max speed hz
436 */
437 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
438 if (ret == -1)
439 pabort("can't set max speed hz");
440
441 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
442 if (ret == -1)
443 pabort("can't get max speed hz");
444
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100445 printf("spi mode: 0x%x\n", mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700446 printf("bits per word: %d\n", bits);
447 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
448
Joshua Clayton7af475a2015-11-18 14:30:39 -0800449 if (input_tx && input_file)
450 pabort("only one of -p and --input may be selected");
451
Joshua Clayton5c437a42015-11-18 14:30:38 -0800452 if (input_tx)
453 transfer_escaped_string(fd, input_tx);
Joshua Clayton7af475a2015-11-18 14:30:39 -0800454 else if (input_file)
455 transfer_file(fd, input_file);
Frode Isaksen9006a7b2017-03-21 16:48:46 +0100456 else if (transfer_size) {
457 struct timespec last_stat;
458
459 clock_gettime(CLOCK_MONOTONIC, &last_stat);
460
461 while (iterations-- > 0) {
462 struct timespec current;
463
464 transfer_buf(fd, transfer_size);
465
466 clock_gettime(CLOCK_MONOTONIC, &current);
467 if (current.tv_sec - last_stat.tv_sec > interval) {
468 show_transfer_rate();
469 last_stat = current;
470 }
471 }
472 printf("total: tx %.1fKB, rx %.1fKB\n",
473 _write_count/1024.0, _read_count/1024.0);
474 } else
Adrian Remonda30061912015-03-10 16:12:32 -0400475 transfer(fd, default_tx, default_rx, sizeof(default_tx));
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700476
477 close(fd);
478
479 return ret;
480}