2 * SPI testing utility (using spidev driver)
4 * Copyright (c) 2007 MontaVista Software, Inc.
5 * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License.
11 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
22 #include <sys/ioctl.h>
23 #include <linux/ioctl.h>
25 #include <linux/types.h>
26 #include <linux/spi/spidev.h>
28 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
30 static void pabort(const char *s
)
36 static const char *device
= "/dev/spidev1.1";
38 static uint8_t bits
= 8;
39 static char *input_file
;
40 static char *output_file
;
41 static uint32_t speed
= 500000;
42 static uint16_t delay
;
44 static int transfer_size
;
45 static int iterations
;
46 static int interval
= 5; /* interval in seconds for showing transfer rate */
48 uint8_t default_tx
[] = {
49 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
50 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
51 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
52 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
53 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
57 uint8_t default_rx
[ARRAY_SIZE(default_tx
)] = {0, };
60 static void hex_dump(const void *src
, size_t length
, size_t line_size
,
64 const unsigned char *address
= src
;
65 const unsigned char *line
= address
;
68 printf("%s | ", prefix
);
69 while (length
-- > 0) {
70 printf("%02X ", *address
++);
71 if (!(++i
% line_size
) || (length
== 0 && i
% line_size
)) {
73 while (i
++ % line_size
)
76 printf(" | "); /* right close */
77 while (line
< address
) {
79 printf("%c", (c
< 33 || c
== 255) ? 0x2E : c
);
83 printf("%s | ", prefix
);
89 * Unescape - process hexadecimal escape character
90 * converts shell input "\x23" -> 0x23
92 static int unescape(char *_dst
, char *_src
, size_t len
)
101 if (*src
== '\\' && *(src
+1) == 'x') {
102 match
= sscanf(src
+ 2, "%2x", &ch
);
104 pabort("malformed input string");
107 *dst
++ = (unsigned char)ch
;
116 static void transfer(int fd
, uint8_t const *tx
, uint8_t const *rx
, size_t len
)
120 struct spi_ioc_transfer tr
= {
121 .tx_buf
= (unsigned long)tx
,
122 .rx_buf
= (unsigned long)rx
,
124 .delay_usecs
= delay
,
126 .bits_per_word
= bits
,
129 if (mode
& SPI_TX_QUAD
)
131 else if (mode
& SPI_TX_DUAL
)
133 if (mode
& SPI_RX_QUAD
)
135 else if (mode
& SPI_RX_DUAL
)
137 if (!(mode
& SPI_LOOP
)) {
138 if (mode
& (SPI_TX_QUAD
| SPI_TX_DUAL
))
140 else if (mode
& (SPI_RX_QUAD
| SPI_RX_DUAL
))
144 ret
= ioctl(fd
, SPI_IOC_MESSAGE(1), &tr
);
146 pabort("can't send spi message");
149 hex_dump(tx
, len
, 32, "TX");
152 out_fd
= open(output_file
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
154 pabort("could not open output file");
156 ret
= write(out_fd
, rx
, len
);
158 pabort("not all bytes written to output file");
164 hex_dump(rx
, len
, 32, "RX");
167 static void print_usage(const char *prog
)
169 printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog
);
170 puts(" -D --device device to use (default /dev/spidev1.1)\n"
171 " -s --speed max speed (Hz)\n"
172 " -d --delay delay (usec)\n"
173 " -b --bpw bits per word\n"
174 " -i --input input data from a file (e.g. \"test.bin\")\n"
175 " -o --output output data to a file (e.g. \"results.bin\")\n"
176 " -l --loop loopback\n"
177 " -H --cpha clock phase\n"
178 " -O --cpol clock polarity\n"
179 " -L --lsb least significant bit first\n"
180 " -C --cs-high chip select active high\n"
181 " -3 --3wire SI/SO signals shared\n"
182 " -v --verbose Verbose (show tx buffer)\n"
183 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
184 " -N --no-cs no chip select\n"
185 " -R --ready slave pulls low to pause\n"
186 " -2 --dual dual transfer\n"
187 " -4 --quad quad transfer\n"
188 " -S --size transfer size\n"
189 " -I --iter iterations\n");
193 static void parse_opts(int argc
, char *argv
[])
196 static const struct option lopts
[] = {
197 { "device", 1, 0, 'D' },
198 { "speed", 1, 0, 's' },
199 { "delay", 1, 0, 'd' },
200 { "bpw", 1, 0, 'b' },
201 { "input", 1, 0, 'i' },
202 { "output", 1, 0, 'o' },
203 { "loop", 0, 0, 'l' },
204 { "cpha", 0, 0, 'H' },
205 { "cpol", 0, 0, 'O' },
206 { "lsb", 0, 0, 'L' },
207 { "cs-high", 0, 0, 'C' },
208 { "3wire", 0, 0, '3' },
209 { "no-cs", 0, 0, 'N' },
210 { "ready", 0, 0, 'R' },
211 { "dual", 0, 0, '2' },
212 { "verbose", 0, 0, 'v' },
213 { "quad", 0, 0, '4' },
214 { "size", 1, 0, 'S' },
215 { "iter", 1, 0, 'I' },
220 c
= getopt_long(argc
, argv
, "D:s:d:b:i:o:lHOLC3NR24p:vS:I:",
231 speed
= atoi(optarg
);
234 delay
= atoi(optarg
);
243 output_file
= optarg
;
255 mode
|= SPI_LSB_FIRST
;
282 transfer_size
= atoi(optarg
);
285 iterations
= atoi(optarg
);
288 print_usage(argv
[0]);
292 if (mode
& SPI_LOOP
) {
293 if (mode
& SPI_TX_DUAL
)
295 if (mode
& SPI_TX_QUAD
)
300 static void transfer_escaped_string(int fd
, char *str
)
302 size_t size
= strlen(str
);
308 pabort("can't allocate tx buffer");
312 pabort("can't allocate rx buffer");
314 size
= unescape((char *)tx
, str
, size
);
315 transfer(fd
, tx
, rx
, size
);
320 static void transfer_file(int fd
, char *filename
)
328 if (stat(filename
, &sb
) == -1)
329 pabort("can't stat input file");
331 tx_fd
= open(filename
, O_RDONLY
);
333 pabort("can't open input file");
335 tx
= malloc(sb
.st_size
);
337 pabort("can't allocate tx buffer");
339 rx
= malloc(sb
.st_size
);
341 pabort("can't allocate rx buffer");
343 bytes
= read(tx_fd
, tx
, sb
.st_size
);
344 if (bytes
!= sb
.st_size
)
345 pabort("failed to read input file");
347 transfer(fd
, tx
, rx
, sb
.st_size
);
353 static uint64_t _read_count
;
354 static uint64_t _write_count
;
356 static void show_transfer_rate(void)
358 static uint64_t prev_read_count
, prev_write_count
;
359 double rx_rate
, tx_rate
;
361 rx_rate
= ((_read_count
- prev_read_count
) * 8) / (interval
*1000.0);
362 tx_rate
= ((_write_count
- prev_write_count
) * 8) / (interval
*1000.0);
364 printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate
, tx_rate
);
366 prev_read_count
= _read_count
;
367 prev_write_count
= _write_count
;
370 static void transfer_buf(int fd
, int len
)
378 pabort("can't allocate tx buffer");
379 for (i
= 0; i
< len
; i
++)
384 pabort("can't allocate rx buffer");
386 transfer(fd
, tx
, rx
, len
);
391 if (mode
& SPI_LOOP
) {
392 if (memcmp(tx
, rx
, len
)) {
393 fprintf(stderr
, "transfer error !\n");
394 hex_dump(tx
, len
, 32, "TX");
395 hex_dump(rx
, len
, 32, "RX");
404 int main(int argc
, char *argv
[])
409 parse_opts(argc
, argv
);
411 fd
= open(device
, O_RDWR
);
413 pabort("can't open device");
418 ret
= ioctl(fd
, SPI_IOC_WR_MODE32
, &mode
);
420 pabort("can't set spi mode");
422 ret
= ioctl(fd
, SPI_IOC_RD_MODE32
, &mode
);
424 pabort("can't get spi mode");
429 ret
= ioctl(fd
, SPI_IOC_WR_BITS_PER_WORD
, &bits
);
431 pabort("can't set bits per word");
433 ret
= ioctl(fd
, SPI_IOC_RD_BITS_PER_WORD
, &bits
);
435 pabort("can't get bits per word");
440 ret
= ioctl(fd
, SPI_IOC_WR_MAX_SPEED_HZ
, &speed
);
442 pabort("can't set max speed hz");
444 ret
= ioctl(fd
, SPI_IOC_RD_MAX_SPEED_HZ
, &speed
);
446 pabort("can't get max speed hz");
448 printf("spi mode: 0x%x\n", mode
);
449 printf("bits per word: %d\n", bits
);
450 printf("max speed: %d Hz (%d KHz)\n", speed
, speed
/1000);
452 if (input_tx
&& input_file
)
453 pabort("only one of -p and --input may be selected");
456 transfer_escaped_string(fd
, input_tx
);
458 transfer_file(fd
, input_file
);
459 else if (transfer_size
) {
460 struct timespec last_stat
;
462 clock_gettime(CLOCK_MONOTONIC
, &last_stat
);
464 while (iterations
-- > 0) {
465 struct timespec current
;
467 transfer_buf(fd
, transfer_size
);
469 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
470 if (current
.tv_sec
- last_stat
.tv_sec
> interval
) {
471 show_transfer_rate();
475 printf("total: tx %.1fKB, rx %.1fKB\n",
476 _write_count
/1024.0, _read_count
/1024.0);
478 transfer(fd
, default_tx
, default_rx
, sizeof(default_tx
));