1 // SPDX-License-Identifier: GPL-2.0-only
3 * SPI testing utility (using spidev driver)
5 * Copyright (c) 2007 MontaVista Software, Inc.
6 * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
8 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
20 #include <sys/ioctl.h>
21 #include <linux/ioctl.h>
23 #include <linux/types.h>
24 #include <linux/spi/spidev.h>
26 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
28 static void pabort(const char *s)
38 static const char *device = "/dev/spidev1.1";
40 static uint8_t bits = 8;
41 static char *input_file;
42 static char *output_file;
43 static uint32_t speed = 500000;
44 static uint16_t delay;
46 static int transfer_size;
47 static int iterations;
48 static int interval = 5; /* interval in seconds for showing transfer rate */
50 static uint8_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,
59 static uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
60 static char *input_tx;
62 static void hex_dump(const void *src, size_t length, size_t line_size,
66 const unsigned char *address = src;
67 const unsigned char *line = address;
70 printf("%s | ", prefix);
71 while (length-- > 0) {
72 printf("%02X ", *address++);
73 if (!(++i % line_size) || (length == 0 && i % line_size)) {
75 while (i++ % line_size)
79 while (line < address) {
81 printf("%c", (c < 32 || c > 126) ? '.' : c);
85 printf("%s | ", prefix);
91 * Unescape - process hexadecimal escape character
92 * converts shell input "\x23" -> 0x23
94 static int unescape(char *_dst, char *_src, size_t len)
103 if (*src == '\\' && *(src+1) == 'x') {
104 match = sscanf(src + 2, "%2x", &ch);
106 pabort("malformed input string");
109 *dst++ = (unsigned char)ch;
118 static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
122 struct spi_ioc_transfer tr = {
123 .tx_buf = (unsigned long)tx,
124 .rx_buf = (unsigned long)rx,
126 .delay_usecs = delay,
128 .bits_per_word = bits,
131 if (mode & SPI_TX_OCTAL)
133 else if (mode & SPI_TX_QUAD)
135 else if (mode & SPI_TX_DUAL)
137 if (mode & SPI_RX_OCTAL)
139 else if (mode & SPI_RX_QUAD)
141 else if (mode & SPI_RX_DUAL)
143 if (!(mode & SPI_LOOP)) {
144 if (mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL))
146 else if (mode & (SPI_RX_OCTAL | SPI_RX_QUAD | SPI_RX_DUAL))
150 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
152 pabort("can't send spi message");
155 hex_dump(tx, len, 32, "TX");
158 out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
160 pabort("could not open output file");
162 ret = write(out_fd, rx, len);
164 pabort("not all bytes written to output file");
170 hex_dump(rx, len, 32, "RX");
173 static void print_usage(const char *prog)
175 printf("Usage: %s [-2348CDFHILMNORSZbdilopsv]\n", prog);
176 puts("general device settings:\n"
177 " -D --device device to use (default /dev/spidev1.1)\n"
178 " -s --speed max speed (Hz)\n"
179 " -d --delay delay (usec)\n"
180 " -l --loop loopback\n"
182 " -H --cpha clock phase\n"
183 " -O --cpol clock polarity\n"
184 " -F --rx-cpha-flip flip CPHA on Rx only xfer\n"
185 "number of wires for transmission:\n"
186 " -2 --dual dual transfer\n"
187 " -4 --quad quad transfer\n"
188 " -8 --octal octal transfer\n"
189 " -3 --3wire SI/SO signals shared\n"
190 " -Z --3wire-hiz high impedance turnaround\n"
192 " -i --input input data from a file (e.g. \"test.bin\")\n"
193 " -o --output output data to a file (e.g. \"results.bin\")\n"
194 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
195 " -S --size transfer size\n"
196 " -I --iter iterations\n"
197 "additional parameters:\n"
198 " -b --bpw bits per word\n"
199 " -L --lsb least significant bit first\n"
200 " -C --cs-high chip select active high\n"
201 " -N --no-cs no chip select\n"
202 " -R --ready slave pulls low to pause\n"
203 " -M --mosi-idle-low leave mosi line low when idle\n"
205 " -v --verbose Verbose (show tx buffer)\n");
209 static void parse_opts(int argc, char *argv[])
212 static const struct option lopts[] = {
213 { "device", 1, 0, 'D' },
214 { "speed", 1, 0, 's' },
215 { "delay", 1, 0, 'd' },
216 { "loop", 0, 0, 'l' },
217 { "cpha", 0, 0, 'H' },
218 { "cpol", 0, 0, 'O' },
219 { "rx-cpha-flip", 0, 0, 'F' },
220 { "dual", 0, 0, '2' },
221 { "quad", 0, 0, '4' },
222 { "octal", 0, 0, '8' },
223 { "3wire", 0, 0, '3' },
224 { "3wire-hiz", 0, 0, 'Z' },
225 { "input", 1, 0, 'i' },
226 { "output", 1, 0, 'o' },
227 { "size", 1, 0, 'S' },
228 { "iter", 1, 0, 'I' },
229 { "bpw", 1, 0, 'b' },
230 { "lsb", 0, 0, 'L' },
231 { "cs-high", 0, 0, 'C' },
232 { "no-cs", 0, 0, 'N' },
233 { "ready", 0, 0, 'R' },
234 { "mosi-idle-low", 0, 0, 'M' },
235 { "verbose", 0, 0, 'v' },
240 c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3ZFMNR248p:vS:I:",
251 speed = atoi(optarg);
254 delay = atoi(optarg);
263 output_file = optarg;
275 mode |= SPI_LSB_FIRST;
284 mode |= SPI_3WIRE_HIZ;
287 mode |= SPI_RX_CPHA_FLIP;
290 mode |= SPI_MOSI_IDLE_LOW;
311 mode |= SPI_TX_OCTAL;
314 transfer_size = atoi(optarg);
317 iterations = atoi(optarg);
320 print_usage(argv[0]);
323 if (mode & SPI_LOOP) {
324 if (mode & SPI_TX_DUAL)
326 if (mode & SPI_TX_QUAD)
328 if (mode & SPI_TX_OCTAL)
329 mode |= SPI_RX_OCTAL;
333 static void transfer_escaped_string(int fd, char *str)
335 size_t size = strlen(str);
341 pabort("can't allocate tx buffer");
345 pabort("can't allocate rx buffer");
347 size = unescape((char *)tx, str, size);
348 transfer(fd, tx, rx, size);
353 static void transfer_file(int fd, char *filename)
361 if (stat(filename, &sb) == -1)
362 pabort("can't stat input file");
364 tx_fd = open(filename, O_RDONLY);
366 pabort("can't open input file");
368 tx = malloc(sb.st_size);
370 pabort("can't allocate tx buffer");
372 rx = malloc(sb.st_size);
374 pabort("can't allocate rx buffer");
376 bytes = read(tx_fd, tx, sb.st_size);
377 if (bytes != sb.st_size)
378 pabort("failed to read input file");
380 transfer(fd, tx, rx, sb.st_size);
386 static uint64_t _read_count;
387 static uint64_t _write_count;
389 static void show_transfer_rate(void)
391 static uint64_t prev_read_count, prev_write_count;
392 double rx_rate, tx_rate;
394 rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0);
395 tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0);
397 printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate);
399 prev_read_count = _read_count;
400 prev_write_count = _write_count;
403 static void transfer_buf(int fd, int len)
411 pabort("can't allocate tx buffer");
412 for (i = 0; i < len; i++)
417 pabort("can't allocate rx buffer");
419 transfer(fd, tx, rx, len);
424 if (mode & SPI_LOOP) {
425 if (memcmp(tx, rx, len)) {
426 fprintf(stderr, "transfer error !\n");
427 hex_dump(tx, len, 32, "TX");
428 hex_dump(rx, len, 32, "RX");
437 int main(int argc, char *argv[])
443 parse_opts(argc, argv);
445 if (input_tx && input_file)
446 pabort("only one of -p and --input may be selected");
448 fd = open(device, O_RDWR);
450 pabort("can't open device");
455 /* WR is make a request to assign 'mode' */
457 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
459 pabort("can't set spi mode");
461 /* RD is read what mode the device actually is in */
462 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
464 pabort("can't get spi mode");
465 /* Drivers can reject some mode bits without returning an error.
466 * Read the current value to identify what mode it is in, and if it
467 * differs from the requested mode, warn the user.
470 printf("WARNING device does not support requested mode 0x%x\n",
476 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
478 pabort("can't set bits per word");
480 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
482 pabort("can't get bits per word");
487 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
489 pabort("can't set max speed hz");
491 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
493 pabort("can't get max speed hz");
495 printf("spi mode: 0x%x\n", mode);
496 printf("bits per word: %u\n", bits);
497 printf("max speed: %u Hz (%u kHz)\n", speed, speed/1000);
500 transfer_escaped_string(fd, input_tx);
502 transfer_file(fd, input_file);
503 else if (transfer_size) {
504 struct timespec last_stat;
506 clock_gettime(CLOCK_MONOTONIC, &last_stat);
508 while (iterations-- > 0) {
509 struct timespec current;
511 transfer_buf(fd, transfer_size);
513 clock_gettime(CLOCK_MONOTONIC, ¤t);
514 if (current.tv_sec - last_stat.tv_sec > interval) {
515 show_transfer_rate();
519 printf("total: tx %.1fKB, rx %.1fKB\n",
520 _write_count/1024.0, _read_count/1024.0);
522 transfer(fd, default_tx, default_rx, sizeof(default_tx));