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;
39 static void hex_dump(const void *src, size_t length, size_t line_size, char *prefix)
42 const unsigned char *address = src;
43 const unsigned char *line = address;
46 printf("%s | ", prefix);
47 while (length-- > 0) {
48 printf("%02X ", *address++);
49 if (!(++i % line_size) || (length == 0 && i % line_size)) {
51 while (i++ % line_size)
54 printf(" | "); /* right close */
55 while (line < address) {
57 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
61 printf("%s | ", prefix);
66 static void transfer(int fd)
70 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
71 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
72 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
73 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
74 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
75 0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
78 uint8_t rx[ARRAY_SIZE(tx)] = {0, };
79 struct spi_ioc_transfer tr = {
80 .tx_buf = (unsigned long)tx,
81 .rx_buf = (unsigned long)rx,
82 .len = ARRAY_SIZE(tx),
85 .bits_per_word = bits,
88 if (mode & SPI_TX_QUAD)
90 else if (mode & SPI_TX_DUAL)
92 if (mode & SPI_RX_QUAD)
94 else if (mode & SPI_RX_DUAL)
96 if (!(mode & SPI_LOOP)) {
97 if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
99 else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
103 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
105 pabort("can't send spi message");
107 hex_dump(rx, ARRAY_SIZE(rx), 32, "RX");
110 static void print_usage(const char *prog)
112 printf("Usage: %s [-DsbdlHOLC3]\n", prog);
113 puts(" -D --device device to use (default /dev/spidev1.1)\n"
114 " -s --speed max speed (Hz)\n"
115 " -d --delay delay (usec)\n"
116 " -b --bpw bits per word \n"
117 " -l --loop loopback\n"
118 " -H --cpha clock phase\n"
119 " -O --cpol clock polarity\n"
120 " -L --lsb least significant bit first\n"
121 " -C --cs-high chip select active high\n"
122 " -3 --3wire SI/SO signals shared\n"
123 " -N --no-cs no chip select\n"
124 " -R --ready slave pulls low to pause\n"
125 " -2 --dual dual transfer\n"
126 " -4 --quad quad transfer\n");
130 static void parse_opts(int argc, char *argv[])
133 static const struct option lopts[] = {
134 { "device", 1, 0, 'D' },
135 { "speed", 1, 0, 's' },
136 { "delay", 1, 0, 'd' },
137 { "bpw", 1, 0, 'b' },
138 { "loop", 0, 0, 'l' },
139 { "cpha", 0, 0, 'H' },
140 { "cpol", 0, 0, 'O' },
141 { "lsb", 0, 0, 'L' },
142 { "cs-high", 0, 0, 'C' },
143 { "3wire", 0, 0, '3' },
144 { "no-cs", 0, 0, 'N' },
145 { "ready", 0, 0, 'R' },
146 { "dual", 0, 0, '2' },
147 { "quad", 0, 0, '4' },
152 c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24", lopts, NULL);
162 speed = atoi(optarg);
165 delay = atoi(optarg);
180 mode |= SPI_LSB_FIRST;
201 print_usage(argv[0]);
205 if (mode & SPI_LOOP) {
206 if (mode & SPI_TX_DUAL)
208 if (mode & SPI_TX_QUAD)
213 int main(int argc, char *argv[])
218 parse_opts(argc, argv);
220 fd = open(device, O_RDWR);
222 pabort("can't open device");
227 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
229 pabort("can't set spi mode");
231 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
233 pabort("can't get spi mode");
238 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
240 pabort("can't set bits per word");
242 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
244 pabort("can't get bits per word");
249 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
251 pabort("can't set max speed hz");
253 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
255 pabort("can't get max speed hz");
257 printf("spi mode: 0x%x\n", mode);
258 printf("bits per word: %d\n", bits);
259 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);