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/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 uint32_t speed
= 500000;
37 static uint16_t delay
;
40 uint8_t default_tx
[] = {
41 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
42 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
43 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
44 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
45 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
49 uint8_t default_rx
[ARRAY_SIZE(default_tx
)] = {0, };
52 static void hex_dump(const void *src
, size_t length
, size_t line_size
, char *prefix
)
55 const unsigned char *address
= src
;
56 const unsigned char *line
= address
;
59 printf("%s | ", prefix
);
60 while (length
-- > 0) {
61 printf("%02X ", *address
++);
62 if (!(++i
% line_size
) || (length
== 0 && i
% line_size
)) {
64 while (i
++ % line_size
)
67 printf(" | "); /* right close */
68 while (line
< address
) {
70 printf("%c", (c
< 33 || c
== 255) ? 0x2E : c
);
74 printf("%s | ", prefix
);
80 * Unescape - process hexadecimal escape character
81 * converts shell input "\x23" -> 0x23
83 static int unescape(char *_dst
, char *_src
, size_t len
)
91 if (*src
== '\\' && *(src
+1) == 'x') {
92 sscanf(src
+ 2, "%2x", &ch
);
94 *dst
++ = (unsigned char)ch
;
103 static void transfer(int fd
, uint8_t const *tx
, uint8_t const *rx
, size_t len
)
107 struct spi_ioc_transfer tr
= {
108 .tx_buf
= (unsigned long)tx
,
109 .rx_buf
= (unsigned long)rx
,
111 .delay_usecs
= delay
,
113 .bits_per_word
= bits
,
116 if (mode
& SPI_TX_QUAD
)
118 else if (mode
& SPI_TX_DUAL
)
120 if (mode
& SPI_RX_QUAD
)
122 else if (mode
& SPI_RX_DUAL
)
124 if (!(mode
& SPI_LOOP
)) {
125 if (mode
& (SPI_TX_QUAD
| SPI_TX_DUAL
))
127 else if (mode
& (SPI_RX_QUAD
| SPI_RX_DUAL
))
131 ret
= ioctl(fd
, SPI_IOC_MESSAGE(1), &tr
);
133 pabort("can't send spi message");
136 hex_dump(tx
, len
, 32, "TX");
137 hex_dump(rx
, len
, 32, "RX");
140 static void print_usage(const char *prog
)
142 printf("Usage: %s [-DsbdlHOLC3]\n", prog
);
143 puts(" -D --device device to use (default /dev/spidev1.1)\n"
144 " -s --speed max speed (Hz)\n"
145 " -d --delay delay (usec)\n"
146 " -b --bpw bits per word \n"
147 " -l --loop loopback\n"
148 " -H --cpha clock phase\n"
149 " -O --cpol clock polarity\n"
150 " -L --lsb least significant bit first\n"
151 " -C --cs-high chip select active high\n"
152 " -3 --3wire SI/SO signals shared\n"
153 " -v --verbose Verbose (show tx buffer)\n"
154 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
155 " -N --no-cs no chip select\n"
156 " -R --ready slave pulls low to pause\n"
157 " -2 --dual dual transfer\n"
158 " -4 --quad quad transfer\n");
162 static void parse_opts(int argc
, char *argv
[])
165 static const struct option lopts
[] = {
166 { "device", 1, 0, 'D' },
167 { "speed", 1, 0, 's' },
168 { "delay", 1, 0, 'd' },
169 { "bpw", 1, 0, 'b' },
170 { "loop", 0, 0, 'l' },
171 { "cpha", 0, 0, 'H' },
172 { "cpol", 0, 0, 'O' },
173 { "lsb", 0, 0, 'L' },
174 { "cs-high", 0, 0, 'C' },
175 { "3wire", 0, 0, '3' },
176 { "no-cs", 0, 0, 'N' },
177 { "ready", 0, 0, 'R' },
178 { "dual", 0, 0, '2' },
179 { "verbose", 0, 0, 'v' },
180 { "quad", 0, 0, '4' },
185 c
= getopt_long(argc
, argv
, "D:s:d:b:lHOLC3NR24p:v", lopts
, NULL
);
195 speed
= atoi(optarg
);
198 delay
= atoi(optarg
);
213 mode
|= SPI_LSB_FIRST
;
240 print_usage(argv
[0]);
244 if (mode
& SPI_LOOP
) {
245 if (mode
& SPI_TX_DUAL
)
247 if (mode
& SPI_TX_QUAD
)
252 int main(int argc
, char *argv
[])
260 parse_opts(argc
, argv
);
262 fd
= open(device
, O_RDWR
);
264 pabort("can't open device");
269 ret
= ioctl(fd
, SPI_IOC_WR_MODE32
, &mode
);
271 pabort("can't set spi mode");
273 ret
= ioctl(fd
, SPI_IOC_RD_MODE32
, &mode
);
275 pabort("can't get spi mode");
280 ret
= ioctl(fd
, SPI_IOC_WR_BITS_PER_WORD
, &bits
);
282 pabort("can't set bits per word");
284 ret
= ioctl(fd
, SPI_IOC_RD_BITS_PER_WORD
, &bits
);
286 pabort("can't get bits per word");
291 ret
= ioctl(fd
, SPI_IOC_WR_MAX_SPEED_HZ
, &speed
);
293 pabort("can't set max speed hz");
295 ret
= ioctl(fd
, SPI_IOC_RD_MAX_SPEED_HZ
, &speed
);
297 pabort("can't get max speed hz");
299 printf("spi mode: 0x%x\n", mode
);
300 printf("bits per word: %d\n", bits
);
301 printf("max speed: %d Hz (%d KHz)\n", speed
, speed
/1000);
304 size
= strlen(input_tx
+1);
307 size
= unescape((char *)tx
, input_tx
, size
);
308 transfer(fd
, tx
, rx
, size
);
312 transfer(fd
, default_tx
, default_rx
, sizeof(default_tx
));