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
19 #include <sys/ioctl.h>
20 #include <linux/ioctl.h>
22 #include <linux/types.h>
23 #include <linux/spi/spidev.h>
25 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
27 static void pabort(const char *s)
33 static const char *device = "/dev/spidev1.1";
35 static uint8_t bits = 8;
36 static char *input_file;
37 static char *output_file;
38 static uint32_t speed = 500000;
39 static uint16_t delay;
41 static int transfer_size;
42 static int iterations;
43 static int interval = 5; /* interval in seconds for showing transfer rate */
45 uint8_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,
54 uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
57 static void hex_dump(const void *src, size_t length, size_t line_size,
61 const unsigned char *address = src;
62 const unsigned char *line = address;
65 printf("%s | ", prefix);
66 while (length-- > 0) {
67 printf("%02X ", *address++);
68 if (!(++i % line_size) || (length == 0 && i % line_size)) {
70 while (i++ % line_size)
74 while (line < address) {
76 printf("%c", (c < 32 || c > 126) ? '.' : c);
80 printf("%s | ", prefix);
86 * Unescape - process hexadecimal escape character
87 * converts shell input "\x23" -> 0x23
89 static int unescape(char *_dst, char *_src, size_t len)
98 if (*src == '\\' && *(src+1) == 'x') {
99 match = sscanf(src + 2, "%2x", &ch);
101 pabort("malformed input string");
104 *dst++ = (unsigned char)ch;
113 static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
117 struct spi_ioc_transfer tr = {
118 .tx_buf = (unsigned long)tx,
119 .rx_buf = (unsigned long)rx,
121 .delay_usecs = delay,
123 .bits_per_word = bits,
126 if (mode & SPI_TX_QUAD)
128 else if (mode & SPI_TX_DUAL)
130 if (mode & SPI_RX_QUAD)
132 else if (mode & SPI_RX_DUAL)
134 if (!(mode & SPI_LOOP)) {
135 if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
137 else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
141 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
143 pabort("can't send spi message");
146 hex_dump(tx, len, 32, "TX");
149 out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
151 pabort("could not open output file");
153 ret = write(out_fd, rx, len);
155 pabort("not all bytes written to output file");
161 hex_dump(rx, len, 32, "RX");
164 static void print_usage(const char *prog)
166 printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog);
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"
170 " -b --bpw bits per word\n"
171 " -i --input input data from a file (e.g. \"test.bin\")\n"
172 " -o --output output data to a file (e.g. \"results.bin\")\n"
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"
178 " -3 --3wire SI/SO signals shared\n"
179 " -v --verbose Verbose (show tx buffer)\n"
180 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
181 " -N --no-cs no chip select\n"
182 " -R --ready slave pulls low to pause\n"
183 " -2 --dual dual transfer\n"
184 " -4 --quad quad transfer\n"
185 " -S --size transfer size\n"
186 " -I --iter iterations\n");
190 static void parse_opts(int argc, char *argv[])
193 static const struct option lopts[] = {
194 { "device", 1, 0, 'D' },
195 { "speed", 1, 0, 's' },
196 { "delay", 1, 0, 'd' },
197 { "bpw", 1, 0, 'b' },
198 { "input", 1, 0, 'i' },
199 { "output", 1, 0, 'o' },
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' },
206 { "no-cs", 0, 0, 'N' },
207 { "ready", 0, 0, 'R' },
208 { "dual", 0, 0, '2' },
209 { "verbose", 0, 0, 'v' },
210 { "quad", 0, 0, '4' },
211 { "size", 1, 0, 'S' },
212 { "iter", 1, 0, 'I' },
217 c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:vS:I:",
228 speed = atoi(optarg);
231 delay = atoi(optarg);
240 output_file = optarg;
252 mode |= SPI_LSB_FIRST;
279 transfer_size = atoi(optarg);
282 iterations = atoi(optarg);
285 print_usage(argv[0]);
289 if (mode & SPI_LOOP) {
290 if (mode & SPI_TX_DUAL)
292 if (mode & SPI_TX_QUAD)
297 static void transfer_escaped_string(int fd, char *str)
299 size_t size = strlen(str);
305 pabort("can't allocate tx buffer");
309 pabort("can't allocate rx buffer");
311 size = unescape((char *)tx, str, size);
312 transfer(fd, tx, rx, size);
317 static void transfer_file(int fd, char *filename)
325 if (stat(filename, &sb) == -1)
326 pabort("can't stat input file");
328 tx_fd = open(filename, O_RDONLY);
330 pabort("can't open input file");
332 tx = malloc(sb.st_size);
334 pabort("can't allocate tx buffer");
336 rx = malloc(sb.st_size);
338 pabort("can't allocate rx buffer");
340 bytes = read(tx_fd, tx, sb.st_size);
341 if (bytes != sb.st_size)
342 pabort("failed to read input file");
344 transfer(fd, tx, rx, sb.st_size);
350 static uint64_t _read_count;
351 static uint64_t _write_count;
353 static void show_transfer_rate(void)
355 static uint64_t prev_read_count, prev_write_count;
356 double rx_rate, tx_rate;
358 rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0);
359 tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0);
361 printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate);
363 prev_read_count = _read_count;
364 prev_write_count = _write_count;
367 static void transfer_buf(int fd, int len)
375 pabort("can't allocate tx buffer");
376 for (i = 0; i < len; i++)
381 pabort("can't allocate rx buffer");
383 transfer(fd, tx, rx, len);
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");
401 int main(int argc, char *argv[])
406 parse_opts(argc, argv);
408 fd = open(device, O_RDWR);
410 pabort("can't open device");
415 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
417 pabort("can't set spi mode");
419 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
421 pabort("can't get spi mode");
426 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
428 pabort("can't set bits per word");
430 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
432 pabort("can't get bits per word");
437 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
439 pabort("can't set max speed hz");
441 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
443 pabort("can't get max speed hz");
445 printf("spi mode: 0x%x\n", mode);
446 printf("bits per word: %d\n", bits);
447 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
449 if (input_tx && input_file)
450 pabort("only one of -p and --input may be selected");
453 transfer_escaped_string(fd, input_tx);
455 transfer_file(fd, input_file);
456 else if (transfer_size) {
457 struct timespec last_stat;
459 clock_gettime(CLOCK_MONOTONIC, &last_stat);
461 while (iterations-- > 0) {
462 struct timespec current;
464 transfer_buf(fd, transfer_size);
466 clock_gettime(CLOCK_MONOTONIC, ¤t);
467 if (current.tv_sec - last_stat.tv_sec > interval) {
468 show_transfer_rate();
472 printf("total: tx %.1fKB, rx %.1fKB\n",
473 _write_count/1024.0, _read_count/1024.0);
475 transfer(fd, default_tx, default_rx, sizeof(default_tx));