blob: 322370dbc7702f333d2fb1e2e87af08add5a5228 [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>
Joshua Clayton7af475a2015-11-18 14:30:39 -080022#include <sys/stat.h>
Anton Vorontsov22b238b2007-07-31 00:38:44 -070023#include <linux/types.h>
24#include <linux/spi/spidev.h>
25
26#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
27
28static void pabort(const char *s)
29{
30 perror(s);
31 abort();
32}
33
WANG Congcd583102007-10-16 01:27:47 -070034static const char *device = "/dev/spidev1.1";
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +010035static uint32_t mode;
Anton Vorontsov22b238b2007-07-31 00:38:44 -070036static uint8_t bits = 8;
Joshua Clayton7af475a2015-11-18 14:30:39 -080037static char *input_file;
Joshua Clayton983b278862015-11-18 14:30:40 -080038static char *output_file;
Anton Vorontsov22b238b2007-07-31 00:38:44 -070039static uint32_t speed = 500000;
40static uint16_t delay;
Adrian Remonda31a5c5a2015-03-10 16:12:31 -040041static int verbose;
Anton Vorontsov22b238b2007-07-31 00:38:44 -070042
Adrian Remonda30061912015-03-10 16:12:32 -040043uint8_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,
49 0xF0, 0x0D,
50};
51
52uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
53char *input_tx;
54
Adrian Remondab78ce7e2015-03-10 16:12:30 -040055static void hex_dump(const void *src, size_t length, size_t line_size, char *prefix)
56{
57 int i = 0;
58 const unsigned char *address = src;
59 const unsigned char *line = address;
60 unsigned char c;
61
62 printf("%s | ", prefix);
63 while (length-- > 0) {
64 printf("%02X ", *address++);
65 if (!(++i % line_size) || (length == 0 && i % line_size)) {
66 if (length == 0) {
67 while (i++ % line_size)
68 printf("__ ");
69 }
70 printf(" | "); /* right close */
71 while (line < address) {
72 c = *line++;
73 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
74 }
75 printf("\n");
76 if (length > 0)
77 printf("%s | ", prefix);
78 }
79 }
80}
81
Adrian Remonda30061912015-03-10 16:12:32 -040082/*
83 * Unescape - process hexadecimal escape character
84 * converts shell input "\x23" -> 0x23
85 */
Andrew Morton07eec622015-04-16 12:49:29 -070086static int unescape(char *_dst, char *_src, size_t len)
Adrian Remonda30061912015-03-10 16:12:32 -040087{
88 int ret = 0;
Joshua Claytona20874f2015-11-18 14:30:41 -080089 int match;
Adrian Remonda30061912015-03-10 16:12:32 -040090 char *src = _src;
91 char *dst = _dst;
92 unsigned int ch;
93
94 while (*src) {
95 if (*src == '\\' && *(src+1) == 'x') {
Joshua Claytona20874f2015-11-18 14:30:41 -080096 match = sscanf(src + 2, "%2x", &ch);
97 if (!match)
98 pabort("malformed input string");
99
Adrian Remonda30061912015-03-10 16:12:32 -0400100 src += 4;
101 *dst++ = (unsigned char)ch;
102 } else {
103 *dst++ = *src++;
104 }
105 ret++;
106 }
107 return ret;
108}
109
110static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700111{
112 int ret;
Joshua Clayton983b278862015-11-18 14:30:40 -0800113 int out_fd;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700114 struct spi_ioc_transfer tr = {
115 .tx_buf = (unsigned long)tx,
116 .rx_buf = (unsigned long)rx,
Adrian Remonda30061912015-03-10 16:12:32 -0400117 .len = len,
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700118 .delay_usecs = delay,
119 .speed_hz = speed,
120 .bits_per_word = bits,
121 };
122
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100123 if (mode & SPI_TX_QUAD)
124 tr.tx_nbits = 4;
125 else if (mode & SPI_TX_DUAL)
126 tr.tx_nbits = 2;
127 if (mode & SPI_RX_QUAD)
128 tr.rx_nbits = 4;
129 else if (mode & SPI_RX_DUAL)
130 tr.rx_nbits = 2;
131 if (!(mode & SPI_LOOP)) {
132 if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
133 tr.rx_buf = 0;
134 else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
135 tr.tx_buf = 0;
136 }
137
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700138 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
Hector Palacios95b1ed22010-04-29 15:02:28 -0700139 if (ret < 1)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700140 pabort("can't send spi message");
141
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400142 if (verbose)
Adrian Remonda30061912015-03-10 16:12:32 -0400143 hex_dump(tx, len, 32, "TX");
Joshua Clayton983b278862015-11-18 14:30:40 -0800144
145 if (output_file) {
146 out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
147 if (out_fd < 0)
148 pabort("could not open output file");
149
150 ret = write(out_fd, rx, len);
151 if (ret != len)
152 pabort("not all bytes written to utput file");
153
154 close(out_fd);
155 }
156
157 if (verbose || !output_file)
158 hex_dump(rx, len, 32, "RX");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700159}
160
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700161static void print_usage(const char *prog)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700162{
163 printf("Usage: %s [-DsbdlHOLC3]\n", prog);
164 puts(" -D --device device to use (default /dev/spidev1.1)\n"
165 " -s --speed max speed (Hz)\n"
166 " -d --delay delay (usec)\n"
167 " -b --bpw bits per word \n"
Joshua Clayton7af475a2015-11-18 14:30:39 -0800168 " -i --input input data from a file (e.g. \"test.bin\")\n"
Joshua Clayton983b278862015-11-18 14:30:40 -0800169 " -o --output output data to a file (e.g. \"results.bin\")\n"
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700170 " -l --loop loopback\n"
171 " -H --cpha clock phase\n"
172 " -O --cpol clock polarity\n"
173 " -L --lsb least significant bit first\n"
174 " -C --cs-high chip select active high\n"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100175 " -3 --3wire SI/SO signals shared\n"
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400176 " -v --verbose Verbose (show tx buffer)\n"
Adrian Remonda30061912015-03-10 16:12:32 -0400177 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100178 " -N --no-cs no chip select\n"
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100179 " -R --ready slave pulls low to pause\n"
180 " -2 --dual dual transfer\n"
181 " -4 --quad quad transfer\n");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700182 exit(1);
183}
184
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700185static void parse_opts(int argc, char *argv[])
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700186{
187 while (1) {
WANG Congcd583102007-10-16 01:27:47 -0700188 static const struct option lopts[] = {
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700189 { "device", 1, 0, 'D' },
190 { "speed", 1, 0, 's' },
191 { "delay", 1, 0, 'd' },
192 { "bpw", 1, 0, 'b' },
Joshua Clayton7af475a2015-11-18 14:30:39 -0800193 { "input", 1, 0, 'i' },
Joshua Clayton983b278862015-11-18 14:30:40 -0800194 { "output", 1, 0, 'o' },
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700195 { "loop", 0, 0, 'l' },
196 { "cpha", 0, 0, 'H' },
197 { "cpol", 0, 0, 'O' },
198 { "lsb", 0, 0, 'L' },
199 { "cs-high", 0, 0, 'C' },
200 { "3wire", 0, 0, '3' },
David Brownellb55f6272009-06-30 11:41:26 -0700201 { "no-cs", 0, 0, 'N' },
202 { "ready", 0, 0, 'R' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100203 { "dual", 0, 0, '2' },
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400204 { "verbose", 0, 0, 'v' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100205 { "quad", 0, 0, '4' },
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700206 { NULL, 0, 0, 0 },
207 };
208 int c;
209
Joshua Clayton983b278862015-11-18 14:30:40 -0800210 c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:v",
Joshua Clayton7af475a2015-11-18 14:30:39 -0800211 lopts, NULL);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700212
213 if (c == -1)
214 break;
215
216 switch (c) {
217 case 'D':
218 device = optarg;
219 break;
220 case 's':
221 speed = atoi(optarg);
222 break;
223 case 'd':
224 delay = atoi(optarg);
225 break;
226 case 'b':
227 bits = atoi(optarg);
228 break;
Joshua Clayton7af475a2015-11-18 14:30:39 -0800229 case 'i':
230 input_file = optarg;
231 break;
Joshua Clayton983b278862015-11-18 14:30:40 -0800232 case 'o':
233 output_file = optarg;
234 break;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700235 case 'l':
236 mode |= SPI_LOOP;
237 break;
238 case 'H':
239 mode |= SPI_CPHA;
240 break;
241 case 'O':
242 mode |= SPI_CPOL;
243 break;
244 case 'L':
245 mode |= SPI_LSB_FIRST;
246 break;
247 case 'C':
248 mode |= SPI_CS_HIGH;
249 break;
250 case '3':
251 mode |= SPI_3WIRE;
252 break;
David Brownellb55f6272009-06-30 11:41:26 -0700253 case 'N':
254 mode |= SPI_NO_CS;
255 break;
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400256 case 'v':
257 verbose = 1;
258 break;
David Brownellb55f6272009-06-30 11:41:26 -0700259 case 'R':
260 mode |= SPI_READY;
261 break;
Adrian Remonda30061912015-03-10 16:12:32 -0400262 case 'p':
263 input_tx = optarg;
264 break;
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100265 case '2':
266 mode |= SPI_TX_DUAL;
267 break;
268 case '4':
269 mode |= SPI_TX_QUAD;
270 break;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700271 default:
272 print_usage(argv[0]);
273 break;
274 }
275 }
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100276 if (mode & SPI_LOOP) {
277 if (mode & SPI_TX_DUAL)
278 mode |= SPI_RX_DUAL;
279 if (mode & SPI_TX_QUAD)
280 mode |= SPI_RX_QUAD;
281 }
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700282}
283
Joshua Clayton5c437a42015-11-18 14:30:38 -0800284static void transfer_escaped_string(int fd, char *str)
285{
286 size_t size = strlen(str + 1);
287 uint8_t *tx;
288 uint8_t *rx;
289
290 tx = malloc(size);
291 if (!tx)
292 pabort("can't allocate tx buffer");
293
294 rx = malloc(size);
295 if (!rx)
296 pabort("can't allocate rx buffer");
297
298 size = unescape((char *)tx, str, size);
299 transfer(fd, tx, rx, size);
300 free(rx);
301 free(tx);
302}
303
Joshua Clayton7af475a2015-11-18 14:30:39 -0800304static void transfer_file(int fd, char *filename)
305{
306 ssize_t bytes;
307 struct stat sb;
308 int tx_fd;
309 uint8_t *tx;
310 uint8_t *rx;
311
312 if (stat(filename, &sb) == -1)
313 pabort("can't stat input file");
314
315 tx_fd = open(filename, O_RDONLY);
316 if (fd < 0)
317 pabort("can't open input file");
318
319 tx = malloc(sb.st_size);
320 if (!tx)
321 pabort("can't allocate tx buffer");
322
323 rx = malloc(sb.st_size);
324 if (!rx)
325 pabort("can't allocate rx buffer");
326
327 bytes = read(tx_fd, tx, sb.st_size);
328 if (bytes != sb.st_size)
329 pabort("failed to read input file");
330
331 transfer(fd, tx, rx, sb.st_size);
332 free(rx);
333 free(tx);
334 close(tx_fd);
335}
336
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700337int main(int argc, char *argv[])
338{
339 int ret = 0;
340 int fd;
341
342 parse_opts(argc, argv);
343
344 fd = open(device, O_RDWR);
345 if (fd < 0)
346 pabort("can't open device");
347
348 /*
349 * spi mode
350 */
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100351 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700352 if (ret == -1)
353 pabort("can't set spi mode");
354
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100355 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700356 if (ret == -1)
357 pabort("can't get spi mode");
358
359 /*
360 * bits per word
361 */
362 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
363 if (ret == -1)
364 pabort("can't set bits per word");
365
366 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
367 if (ret == -1)
368 pabort("can't get bits per word");
369
370 /*
371 * max speed hz
372 */
373 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
374 if (ret == -1)
375 pabort("can't set max speed hz");
376
377 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
378 if (ret == -1)
379 pabort("can't get max speed hz");
380
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100381 printf("spi mode: 0x%x\n", mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700382 printf("bits per word: %d\n", bits);
383 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
384
Joshua Clayton7af475a2015-11-18 14:30:39 -0800385 if (input_tx && input_file)
386 pabort("only one of -p and --input may be selected");
387
Joshua Clayton5c437a42015-11-18 14:30:38 -0800388 if (input_tx)
389 transfer_escaped_string(fd, input_tx);
Joshua Clayton7af475a2015-11-18 14:30:39 -0800390 else if (input_file)
391 transfer_file(fd, input_file);
Joshua Clayton5c437a42015-11-18 14:30:38 -0800392 else
Adrian Remonda30061912015-03-10 16:12:32 -0400393 transfer(fd, default_tx, default_rx, sizeof(default_tx));
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700394
395 close(fd);
396
397 return ret;
398}