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>
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)
34 static const char *device = "/dev/spidev1.1";
36 static uint8_t bits = 8;
37 static char *input_file;
38 static char *output_file;
39 static uint32_t speed = 500000;
40 static uint16_t delay;
43 uint8_t default_tx[] = {
44 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
45 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
46 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
47 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
48 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
52 uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
55 static void hex_dump(const void *src, size_t length, size_t line_size,
59 const unsigned char *address = src;
60 const unsigned char *line = address;
63 printf("%s | ", prefix);
64 while (length-- > 0) {
65 printf("%02X ", *address++);
66 if (!(++i % line_size) || (length == 0 && i % line_size)) {
68 while (i++ % line_size)
71 printf(" | "); /* right close */
72 while (line < address) {
74 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
78 printf("%s | ", prefix);
84 * Unescape - process hexadecimal escape character
85 * converts shell input "\x23" -> 0x23
87 static int unescape(char *_dst, char *_src, size_t len)
96 if (*src == '\\' && *(src+1) == 'x') {
97 match = sscanf(src + 2, "%2x", &ch);
99 pabort("malformed input string");
102 *dst++ = (unsigned char)ch;
111 static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
115 struct spi_ioc_transfer tr = {
116 .tx_buf = (unsigned long)tx,
117 .rx_buf = (unsigned long)rx,
119 .delay_usecs = delay,
121 .bits_per_word = bits,
124 if (mode & SPI_TX_QUAD)
126 else if (mode & SPI_TX_DUAL)
128 if (mode & SPI_RX_QUAD)
130 else if (mode & SPI_RX_DUAL)
132 if (!(mode & SPI_LOOP)) {
133 if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
135 else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
139 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
141 pabort("can't send spi message");
144 hex_dump(tx, len, 32, "TX");
147 out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
149 pabort("could not open output file");
151 ret = write(out_fd, rx, len);
153 pabort("not all bytes written to output file");
158 if (verbose || !output_file)
159 hex_dump(rx, len, 32, "RX");
162 static void print_usage(const char *prog)
164 printf("Usage: %s [-DsbdlHOLC3]\n", prog);
165 puts(" -D --device device to use (default /dev/spidev1.1)\n"
166 " -s --speed max speed (Hz)\n"
167 " -d --delay delay (usec)\n"
168 " -b --bpw bits per word\n"
169 " -i --input input data from a file (e.g. \"test.bin\")\n"
170 " -o --output output data to a file (e.g. \"results.bin\")\n"
171 " -l --loop loopback\n"
172 " -H --cpha clock phase\n"
173 " -O --cpol clock polarity\n"
174 " -L --lsb least significant bit first\n"
175 " -C --cs-high chip select active high\n"
176 " -3 --3wire SI/SO signals shared\n"
177 " -v --verbose Verbose (show tx buffer)\n"
178 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
179 " -N --no-cs no chip select\n"
180 " -R --ready slave pulls low to pause\n"
181 " -2 --dual dual transfer\n"
182 " -4 --quad quad transfer\n");
186 static void parse_opts(int argc, char *argv[])
189 static const struct option lopts[] = {
190 { "device", 1, 0, 'D' },
191 { "speed", 1, 0, 's' },
192 { "delay", 1, 0, 'd' },
193 { "bpw", 1, 0, 'b' },
194 { "input", 1, 0, 'i' },
195 { "output", 1, 0, 'o' },
196 { "loop", 0, 0, 'l' },
197 { "cpha", 0, 0, 'H' },
198 { "cpol", 0, 0, 'O' },
199 { "lsb", 0, 0, 'L' },
200 { "cs-high", 0, 0, 'C' },
201 { "3wire", 0, 0, '3' },
202 { "no-cs", 0, 0, 'N' },
203 { "ready", 0, 0, 'R' },
204 { "dual", 0, 0, '2' },
205 { "verbose", 0, 0, 'v' },
206 { "quad", 0, 0, '4' },
211 c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:v",
222 speed = atoi(optarg);
225 delay = atoi(optarg);
234 output_file = optarg;
246 mode |= SPI_LSB_FIRST;
273 print_usage(argv[0]);
277 if (mode & SPI_LOOP) {
278 if (mode & SPI_TX_DUAL)
280 if (mode & SPI_TX_QUAD)
285 static void transfer_escaped_string(int fd, char *str)
287 size_t size = strlen(str + 1);
293 pabort("can't allocate tx buffer");
297 pabort("can't allocate rx buffer");
299 size = unescape((char *)tx, str, size);
300 transfer(fd, tx, rx, size);
305 static void transfer_file(int fd, char *filename)
313 if (stat(filename, &sb) == -1)
314 pabort("can't stat input file");
316 tx_fd = open(filename, O_RDONLY);
318 pabort("can't open input file");
320 tx = malloc(sb.st_size);
322 pabort("can't allocate tx buffer");
324 rx = malloc(sb.st_size);
326 pabort("can't allocate rx buffer");
328 bytes = read(tx_fd, tx, sb.st_size);
329 if (bytes != sb.st_size)
330 pabort("failed to read input file");
332 transfer(fd, tx, rx, sb.st_size);
338 int main(int argc, char *argv[])
343 parse_opts(argc, argv);
345 fd = open(device, O_RDWR);
347 pabort("can't open device");
352 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
354 pabort("can't set spi mode");
356 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
358 pabort("can't get spi mode");
363 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
365 pabort("can't set bits per word");
367 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
369 pabort("can't get bits per word");
374 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
376 pabort("can't set max speed hz");
378 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
380 pabort("can't get max speed hz");
382 printf("spi mode: 0x%x\n", mode);
383 printf("bits per word: %d\n", bits);
384 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
386 if (input_tx && input_file)
387 pabort("only one of -p and --input may be selected");
390 transfer_escaped_string(fd, input_tx);
392 transfer_file(fd, input_file);
394 transfer(fd, default_tx, default_rx, sizeof(default_tx));