]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - Documentation/spi/spidev_test.c
Merge remote-tracking branch 'kgdb/kgdb-next'
[karo-tx-linux.git] / Documentation / spi / spidev_test.c
1 /*
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>
18 #include <string.h>
19 #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
27 static void pabort(const char *s)
28 {
29         perror(s);
30         abort();
31 }
32
33 static const char *device = "/dev/spidev1.1";
34 static uint32_t mode;
35 static uint8_t bits = 8;
36 static uint32_t speed = 500000;
37 static uint16_t delay;
38 static int verbose;
39
40 uint8_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
49 uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
50 char *input_tx;
51
52 static 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
79 /*
80  *  Unescape - process hexadecimal escape character
81  *      converts shell input "\x23" -> 0x23
82  */
83 static int unescape(char *_dst, char *_src, size_t len)
84 {
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
103 static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
104 {
105         int ret;
106
107         struct spi_ioc_transfer tr = {
108                 .tx_buf = (unsigned long)tx,
109                 .rx_buf = (unsigned long)rx,
110                 .len = len,
111                 .delay_usecs = delay,
112                 .speed_hz = speed,
113                 .bits_per_word = bits,
114         };
115
116         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
131         ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
132         if (ret < 1)
133                 pabort("can't send spi message");
134
135         if (verbose)
136                 hex_dump(tx, len, 32, "TX");
137         hex_dump(rx, len, 32, "RX");
138 }
139
140 static void print_usage(const char *prog)
141 {
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"
152              "  -3 --3wire    SI/SO signals shared\n"
153              "  -v --verbose  Verbose (show tx buffer)\n"
154              "  -p            Send data (e.g. \"1234\\xde\\xad\")\n"
155              "  -N --no-cs    no chip select\n"
156              "  -R --ready    slave pulls low to pause\n"
157              "  -2 --dual     dual transfer\n"
158              "  -4 --quad     quad transfer\n");
159         exit(1);
160 }
161
162 static void parse_opts(int argc, char *argv[])
163 {
164         while (1) {
165                 static const struct option lopts[] = {
166                         { "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' },
176                         { "no-cs",   0, 0, 'N' },
177                         { "ready",   0, 0, 'R' },
178                         { "dual",    0, 0, '2' },
179                         { "verbose", 0, 0, 'v' },
180                         { "quad",    0, 0, '4' },
181                         { NULL, 0, 0, 0 },
182                 };
183                 int c;
184
185                 c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24p:v", lopts, NULL);
186
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;
221                 case 'N':
222                         mode |= SPI_NO_CS;
223                         break;
224                 case 'v':
225                         verbose = 1;
226                         break;
227                 case 'R':
228                         mode |= SPI_READY;
229                         break;
230                 case 'p':
231                         input_tx = optarg;
232                         break;
233                 case '2':
234                         mode |= SPI_TX_DUAL;
235                         break;
236                 case '4':
237                         mode |= SPI_TX_QUAD;
238                         break;
239                 default:
240                         print_usage(argv[0]);
241                         break;
242                 }
243         }
244         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         }
250 }
251
252 int main(int argc, char *argv[])
253 {
254         int ret = 0;
255         int fd;
256         uint8_t *tx;
257         uint8_t *rx;
258         int size;
259
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          */
269         ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
270         if (ret == -1)
271                 pabort("can't set spi mode");
272
273         ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
274         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
299         printf("spi mode: 0x%x\n", mode);
300         printf("bits per word: %d\n", bits);
301         printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
302
303         if (input_tx) {
304                 size = strlen(input_tx+1);
305                 tx = malloc(size);
306                 rx = malloc(size);
307                 size = unescape((char *)tx, input_tx, size);
308                 transfer(fd, tx, rx, size);
309                 free(rx);
310                 free(tx);
311         } else {
312                 transfer(fd, default_tx, default_rx, sizeof(default_tx));
313         }
314
315         close(fd);
316
317         return ret;
318 }