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
21 #include <sys/ioctl.h>
22 #include <linux/ioctl.h>
24 #include <linux/types.h>
25 #include <linux/spi/spidev.h>
27 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
29 static void pabort(const char *s
)
35 static const char *device
= "/dev/spidev1.1";
37 static uint8_t bits
= 8;
38 static char *input_file
;
39 static char *output_file
;
40 static uint32_t speed
= 500000;
41 static uint16_t delay
;
44 uint8_t default_tx
[] = {
45 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
46 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
47 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
48 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
49 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
53 uint8_t default_rx
[ARRAY_SIZE(default_tx
)] = {0, };
56 static void hex_dump(const void *src
, size_t length
, size_t line_size
,
60 const unsigned char *address
= src
;
61 const unsigned char *line
= address
;
64 printf("%s | ", prefix
);
65 while (length
-- > 0) {
66 printf("%02X ", *address
++);
67 if (!(++i
% line_size
) || (length
== 0 && i
% line_size
)) {
69 while (i
++ % line_size
)
72 printf(" | "); /* right close */
73 while (line
< address
) {
75 printf("%c", (c
< 33 || c
== 255) ? 0x2E : c
);
79 printf("%s | ", prefix
);
85 * Unescape - process hexadecimal escape character
86 * converts shell input "\x23" -> 0x23
88 static int unescape(char *_dst
, char *_src
, size_t len
)
97 if (*src
== '\\' && *(src
+1) == 'x') {
98 match
= sscanf(src
+ 2, "%2x", &ch
);
100 pabort("malformed input string");
103 *dst
++ = (unsigned char)ch
;
112 static void transfer(int fd
, uint8_t const *tx
, uint8_t const *rx
, size_t len
)
116 struct spi_ioc_transfer tr
= {
117 .tx_buf
= (unsigned long)tx
,
118 .rx_buf
= (unsigned long)rx
,
120 .delay_usecs
= delay
,
122 .bits_per_word
= bits
,
125 if (mode
& SPI_TX_QUAD
)
127 else if (mode
& SPI_TX_DUAL
)
129 if (mode
& SPI_RX_QUAD
)
131 else if (mode
& SPI_RX_DUAL
)
133 if (!(mode
& SPI_LOOP
)) {
134 if (mode
& (SPI_TX_QUAD
| SPI_TX_DUAL
))
136 else if (mode
& (SPI_RX_QUAD
| SPI_RX_DUAL
))
140 ret
= ioctl(fd
, SPI_IOC_MESSAGE(1), &tr
);
142 pabort("can't send spi message");
145 hex_dump(tx
, len
, 32, "TX");
148 out_fd
= open(output_file
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
150 pabort("could not open output file");
152 ret
= write(out_fd
, rx
, len
);
154 pabort("not all bytes written to output file");
159 if (verbose
|| !output_file
)
160 hex_dump(rx
, len
, 32, "RX");
163 static void print_usage(const char *prog
)
165 printf("Usage: %s [-DsbdlHOLC3]\n", prog
);
166 puts(" -D --device device to use (default /dev/spidev1.1)\n"
167 " -s --speed max speed (Hz)\n"
168 " -d --delay delay (usec)\n"
169 " -b --bpw bits per word\n"
170 " -i --input input data from a file (e.g. \"test.bin\")\n"
171 " -o --output output data to a file (e.g. \"results.bin\")\n"
172 " -l --loop loopback\n"
173 " -H --cpha clock phase\n"
174 " -O --cpol clock polarity\n"
175 " -L --lsb least significant bit first\n"
176 " -C --cs-high chip select active high\n"
177 " -3 --3wire SI/SO signals shared\n"
178 " -v --verbose Verbose (show tx buffer)\n"
179 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
180 " -N --no-cs no chip select\n"
181 " -R --ready slave pulls low to pause\n"
182 " -2 --dual dual transfer\n"
183 " -4 --quad quad transfer\n");
187 static void parse_opts(int argc
, char *argv
[])
190 static const struct option lopts
[] = {
191 { "device", 1, 0, 'D' },
192 { "speed", 1, 0, 's' },
193 { "delay", 1, 0, 'd' },
194 { "bpw", 1, 0, 'b' },
195 { "input", 1, 0, 'i' },
196 { "output", 1, 0, 'o' },
197 { "loop", 0, 0, 'l' },
198 { "cpha", 0, 0, 'H' },
199 { "cpol", 0, 0, 'O' },
200 { "lsb", 0, 0, 'L' },
201 { "cs-high", 0, 0, 'C' },
202 { "3wire", 0, 0, '3' },
203 { "no-cs", 0, 0, 'N' },
204 { "ready", 0, 0, 'R' },
205 { "dual", 0, 0, '2' },
206 { "verbose", 0, 0, 'v' },
207 { "quad", 0, 0, '4' },
212 c
= getopt_long(argc
, argv
, "D:s:d:b:i:o:lHOLC3NR24p:v",
223 speed
= atoi(optarg
);
226 delay
= atoi(optarg
);
235 output_file
= optarg
;
247 mode
|= SPI_LSB_FIRST
;
274 print_usage(argv
[0]);
278 if (mode
& SPI_LOOP
) {
279 if (mode
& SPI_TX_DUAL
)
281 if (mode
& SPI_TX_QUAD
)
286 static void transfer_escaped_string(int fd
, char *str
)
288 size_t size
= strlen(str
);
294 pabort("can't allocate tx buffer");
298 pabort("can't allocate rx buffer");
300 size
= unescape((char *)tx
, str
, size
);
301 transfer(fd
, tx
, rx
, size
);
306 static void transfer_file(int fd
, char *filename
)
314 if (stat(filename
, &sb
) == -1)
315 pabort("can't stat input file");
317 tx_fd
= open(filename
, O_RDONLY
);
319 pabort("can't open input file");
321 tx
= malloc(sb
.st_size
);
323 pabort("can't allocate tx buffer");
325 rx
= malloc(sb
.st_size
);
327 pabort("can't allocate rx buffer");
329 bytes
= read(tx_fd
, tx
, sb
.st_size
);
330 if (bytes
!= sb
.st_size
)
331 pabort("failed to read input file");
333 transfer(fd
, tx
, rx
, sb
.st_size
);
339 int main(int argc
, char *argv
[])
344 parse_opts(argc
, argv
);
346 fd
= open(device
, O_RDWR
);
348 pabort("can't open device");
353 ret
= ioctl(fd
, SPI_IOC_WR_MODE32
, &mode
);
355 pabort("can't set spi mode");
357 ret
= ioctl(fd
, SPI_IOC_RD_MODE32
, &mode
);
359 pabort("can't get spi mode");
364 ret
= ioctl(fd
, SPI_IOC_WR_BITS_PER_WORD
, &bits
);
366 pabort("can't set bits per word");
368 ret
= ioctl(fd
, SPI_IOC_RD_BITS_PER_WORD
, &bits
);
370 pabort("can't get bits per word");
375 ret
= ioctl(fd
, SPI_IOC_WR_MAX_SPEED_HZ
, &speed
);
377 pabort("can't set max speed hz");
379 ret
= ioctl(fd
, SPI_IOC_RD_MAX_SPEED_HZ
, &speed
);
381 pabort("can't get max speed hz");
383 printf("spi mode: 0x%x\n", mode
);
384 printf("bits per word: %d\n", bits
);
385 printf("max speed: %d Hz (%d KHz)\n", speed
, speed
/1000);
387 if (input_tx
&& input_file
)
388 pabort("only one of -p and --input may be selected");
391 transfer_escaped_string(fd
, input_tx
);
393 transfer_file(fd
, input_file
);
395 transfer(fd
, default_tx
, default_rx
, sizeof(default_tx
));