blob: 135b3f592b83db0d00a940b435b8086b409fa3ef [file] [log] [blame]
Anton Vorontsov22b238b2007-07-31 00:38:44 -07001/*
2 * SPI testing utility (using spidev driver)
3 *
4 * Copyright (c) 2007 MontaVista Software, Inc.
5 * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
6 *
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.
10 *
11 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
12 */
13
14#include <stdint.h>
15#include <unistd.h>
16#include <stdio.h>
17#include <stdlib.h>
Adrian Remondab78ce7e2015-03-10 16:12:30 -040018#include <string.h>
Anton Vorontsov22b238b2007-07-31 00:38:44 -070019#include <getopt.h>
20#include <fcntl.h>
21#include <sys/ioctl.h>
22#include <linux/types.h>
23#include <linux/spi/spidev.h>
24
25#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
26
27static void pabort(const char *s)
28{
29 perror(s);
30 abort();
31}
32
WANG Congcd583102007-10-16 01:27:47 -070033static const char *device = "/dev/spidev1.1";
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +010034static uint32_t mode;
Anton Vorontsov22b238b2007-07-31 00:38:44 -070035static uint8_t bits = 8;
36static uint32_t speed = 500000;
37static uint16_t delay;
Adrian Remonda31a5c5a2015-03-10 16:12:31 -040038static int verbose;
Anton Vorontsov22b238b2007-07-31 00:38:44 -070039
Adrian Remonda30061912015-03-10 16:12:32 -040040uint8_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,
46 0xF0, 0x0D,
47};
48
49uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
50char *input_tx;
51
Adrian Remondab78ce7e2015-03-10 16:12:30 -040052static void hex_dump(const void *src, size_t length, size_t line_size, char *prefix)
53{
54 int i = 0;
55 const unsigned char *address = src;
56 const unsigned char *line = address;
57 unsigned char c;
58
59 printf("%s | ", prefix);
60 while (length-- > 0) {
61 printf("%02X ", *address++);
62 if (!(++i % line_size) || (length == 0 && i % line_size)) {
63 if (length == 0) {
64 while (i++ % line_size)
65 printf("__ ");
66 }
67 printf(" | "); /* right close */
68 while (line < address) {
69 c = *line++;
70 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
71 }
72 printf("\n");
73 if (length > 0)
74 printf("%s | ", prefix);
75 }
76 }
77}
78
Adrian Remonda30061912015-03-10 16:12:32 -040079/*
80 * Unescape - process hexadecimal escape character
81 * converts shell input "\x23" -> 0x23
82 */
Andrew Morton07eec622015-04-16 12:49:29 -070083static int unescape(char *_dst, char *_src, size_t len)
Adrian Remonda30061912015-03-10 16:12:32 -040084{
85 int ret = 0;
86 char *src = _src;
87 char *dst = _dst;
88 unsigned int ch;
89
90 while (*src) {
91 if (*src == '\\' && *(src+1) == 'x') {
92 sscanf(src + 2, "%2x", &ch);
93 src += 4;
94 *dst++ = (unsigned char)ch;
95 } else {
96 *dst++ = *src++;
97 }
98 ret++;
99 }
100 return ret;
101}
102
103static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700104{
105 int ret;
Adrian Remonda30061912015-03-10 16:12:32 -0400106
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700107 struct spi_ioc_transfer tr = {
108 .tx_buf = (unsigned long)tx,
109 .rx_buf = (unsigned long)rx,
Adrian Remonda30061912015-03-10 16:12:32 -0400110 .len = len,
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700111 .delay_usecs = delay,
112 .speed_hz = speed,
113 .bits_per_word = bits,
114 };
115
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100116 if (mode & SPI_TX_QUAD)
117 tr.tx_nbits = 4;
118 else if (mode & SPI_TX_DUAL)
119 tr.tx_nbits = 2;
120 if (mode & SPI_RX_QUAD)
121 tr.rx_nbits = 4;
122 else if (mode & SPI_RX_DUAL)
123 tr.rx_nbits = 2;
124 if (!(mode & SPI_LOOP)) {
125 if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
126 tr.rx_buf = 0;
127 else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
128 tr.tx_buf = 0;
129 }
130
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700131 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
Hector Palacios95b1ed22010-04-29 15:02:28 -0700132 if (ret < 1)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700133 pabort("can't send spi message");
134
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400135 if (verbose)
Adrian Remonda30061912015-03-10 16:12:32 -0400136 hex_dump(tx, len, 32, "TX");
137 hex_dump(rx, len, 32, "RX");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700138}
139
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700140static void print_usage(const char *prog)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700141{
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"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100152 " -3 --3wire SI/SO signals shared\n"
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400153 " -v --verbose Verbose (show tx buffer)\n"
Adrian Remonda30061912015-03-10 16:12:32 -0400154 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100155 " -N --no-cs no chip select\n"
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100156 " -R --ready slave pulls low to pause\n"
157 " -2 --dual dual transfer\n"
158 " -4 --quad quad transfer\n");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700159 exit(1);
160}
161
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700162static void parse_opts(int argc, char *argv[])
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700163{
164 while (1) {
WANG Congcd583102007-10-16 01:27:47 -0700165 static const struct option lopts[] = {
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700166 { "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' },
David Brownellb55f6272009-06-30 11:41:26 -0700176 { "no-cs", 0, 0, 'N' },
177 { "ready", 0, 0, 'R' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100178 { "dual", 0, 0, '2' },
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400179 { "verbose", 0, 0, 'v' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100180 { "quad", 0, 0, '4' },
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700181 { NULL, 0, 0, 0 },
182 };
183 int c;
184
Adrian Remonda30061912015-03-10 16:12:32 -0400185 c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24p:v", lopts, NULL);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700186
187 if (c == -1)
188 break;
189
190 switch (c) {
191 case 'D':
192 device = optarg;
193 break;
194 case 's':
195 speed = atoi(optarg);
196 break;
197 case 'd':
198 delay = atoi(optarg);
199 break;
200 case 'b':
201 bits = atoi(optarg);
202 break;
203 case 'l':
204 mode |= SPI_LOOP;
205 break;
206 case 'H':
207 mode |= SPI_CPHA;
208 break;
209 case 'O':
210 mode |= SPI_CPOL;
211 break;
212 case 'L':
213 mode |= SPI_LSB_FIRST;
214 break;
215 case 'C':
216 mode |= SPI_CS_HIGH;
217 break;
218 case '3':
219 mode |= SPI_3WIRE;
220 break;
David Brownellb55f6272009-06-30 11:41:26 -0700221 case 'N':
222 mode |= SPI_NO_CS;
223 break;
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400224 case 'v':
225 verbose = 1;
226 break;
David Brownellb55f6272009-06-30 11:41:26 -0700227 case 'R':
228 mode |= SPI_READY;
229 break;
Adrian Remonda30061912015-03-10 16:12:32 -0400230 case 'p':
231 input_tx = optarg;
232 break;
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100233 case '2':
234 mode |= SPI_TX_DUAL;
235 break;
236 case '4':
237 mode |= SPI_TX_QUAD;
238 break;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700239 default:
240 print_usage(argv[0]);
241 break;
242 }
243 }
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100244 if (mode & SPI_LOOP) {
245 if (mode & SPI_TX_DUAL)
246 mode |= SPI_RX_DUAL;
247 if (mode & SPI_TX_QUAD)
248 mode |= SPI_RX_QUAD;
249 }
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700250}
251
252int main(int argc, char *argv[])
253{
254 int ret = 0;
255 int fd;
Adrian Remonda30061912015-03-10 16:12:32 -0400256 uint8_t *tx;
257 uint8_t *rx;
258 int size;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700259
260 parse_opts(argc, argv);
261
262 fd = open(device, O_RDWR);
263 if (fd < 0)
264 pabort("can't open device");
265
266 /*
267 * spi mode
268 */
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100269 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700270 if (ret == -1)
271 pabort("can't set spi mode");
272
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100273 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700274 if (ret == -1)
275 pabort("can't get spi mode");
276
277 /*
278 * bits per word
279 */
280 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
281 if (ret == -1)
282 pabort("can't set bits per word");
283
284 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
285 if (ret == -1)
286 pabort("can't get bits per word");
287
288 /*
289 * max speed hz
290 */
291 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
292 if (ret == -1)
293 pabort("can't set max speed hz");
294
295 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
296 if (ret == -1)
297 pabort("can't get max speed hz");
298
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100299 printf("spi mode: 0x%x\n", mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700300 printf("bits per word: %d\n", bits);
301 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
302
Adrian Remonda30061912015-03-10 16:12:32 -0400303 if (input_tx) {
304 size = strlen(input_tx+1);
305 tx = malloc(size);
306 rx = malloc(size);
Andrew Morton07eec622015-04-16 12:49:29 -0700307 size = unescape((char *)tx, input_tx, size);
Adrian Remonda30061912015-03-10 16:12:32 -0400308 transfer(fd, tx, rx, size);
309 free(rx);
310 free(tx);
311 } else {
312 transfer(fd, default_tx, default_rx, sizeof(default_tx));
313 }
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700314
315 close(fd);
316
317 return ret;
318}