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
20 #include <sys/ioctl.h>
21 #include <linux/types.h>
22 #include <linux/spi/spidev.h>
24 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
26 static void pabort(const char *s
)
32 static const char *device
= "/dev/spidev1.1";
34 static uint8_t bits
= 8;
35 static uint32_t speed
= 500000;
36 static uint16_t delay
;
38 static void transfer(int fd
)
42 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
43 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
44 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
45 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
46 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
47 0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
50 uint8_t rx
[ARRAY_SIZE(tx
)] = {0, };
51 struct spi_ioc_transfer tr
= {
52 .tx_buf
= (unsigned long)tx
,
53 .rx_buf
= (unsigned long)rx
,
54 .len
= ARRAY_SIZE(tx
),
57 .bits_per_word
= bits
,
60 if (mode
& SPI_TX_QUAD
)
62 else if (mode
& SPI_TX_DUAL
)
64 if (mode
& SPI_RX_QUAD
)
66 else if (mode
& SPI_RX_DUAL
)
68 if (!(mode
& SPI_LOOP
)) {
69 if (mode
& (SPI_TX_QUAD
| SPI_TX_DUAL
))
71 else if (mode
& (SPI_RX_QUAD
| SPI_RX_DUAL
))
75 ret
= ioctl(fd
, SPI_IOC_MESSAGE(1), &tr
);
77 pabort("can't send spi message");
79 for (ret
= 0; ret
< ARRAY_SIZE(tx
); ret
++) {
82 printf("%.2X ", rx
[ret
]);
87 static void print_usage(const char *prog
)
89 printf("Usage: %s [-DsbdlHOLC3]\n", prog
);
90 puts(" -D --device device to use (default /dev/spidev1.1)\n"
91 " -s --speed max speed (Hz)\n"
92 " -d --delay delay (usec)\n"
93 " -b --bpw bits per word \n"
94 " -l --loop loopback\n"
95 " -H --cpha clock phase\n"
96 " -O --cpol clock polarity\n"
97 " -L --lsb least significant bit first\n"
98 " -C --cs-high chip select active high\n"
99 " -3 --3wire SI/SO signals shared\n"
100 " -N --no-cs no chip select\n"
101 " -R --ready slave pulls low to pause\n"
102 " -2 --dual dual transfer\n"
103 " -4 --quad quad transfer\n");
107 static void parse_opts(int argc
, char *argv
[])
110 static const struct option lopts
[] = {
111 { "device", 1, 0, 'D' },
112 { "speed", 1, 0, 's' },
113 { "delay", 1, 0, 'd' },
114 { "bpw", 1, 0, 'b' },
115 { "loop", 0, 0, 'l' },
116 { "cpha", 0, 0, 'H' },
117 { "cpol", 0, 0, 'O' },
118 { "lsb", 0, 0, 'L' },
119 { "cs-high", 0, 0, 'C' },
120 { "3wire", 0, 0, '3' },
121 { "no-cs", 0, 0, 'N' },
122 { "ready", 0, 0, 'R' },
123 { "dual", 0, 0, '2' },
124 { "quad", 0, 0, '4' },
129 c
= getopt_long(argc
, argv
, "D:s:d:b:lHOLC3NR24", lopts
, NULL
);
139 speed
= atoi(optarg
);
142 delay
= atoi(optarg
);
157 mode
|= SPI_LSB_FIRST
;
178 print_usage(argv
[0]);
182 if (mode
& SPI_LOOP
) {
183 if (mode
& SPI_TX_DUAL
)
185 if (mode
& SPI_TX_QUAD
)
190 int main(int argc
, char *argv
[])
195 parse_opts(argc
, argv
);
197 fd
= open(device
, O_RDWR
);
199 pabort("can't open device");
204 ret
= ioctl(fd
, SPI_IOC_WR_MODE32
, &mode
);
206 pabort("can't set spi mode");
208 ret
= ioctl(fd
, SPI_IOC_RD_MODE32
, &mode
);
210 pabort("can't get spi mode");
215 ret
= ioctl(fd
, SPI_IOC_WR_BITS_PER_WORD
, &bits
);
217 pabort("can't set bits per word");
219 ret
= ioctl(fd
, SPI_IOC_RD_BITS_PER_WORD
, &bits
);
221 pabort("can't get bits per word");
226 ret
= ioctl(fd
, SPI_IOC_WR_MAX_SPEED_HZ
, &speed
);
228 pabort("can't set max speed hz");
230 ret
= ioctl(fd
, SPI_IOC_RD_MAX_SPEED_HZ
, &speed
);
232 pabort("can't get max speed hz");
234 printf("spi mode: 0x%x\n", mode
);
235 printf("bits per word: %d\n", bits
);
236 printf("max speed: %d Hz (%d KHz)\n", speed
, speed
/1000);