9cb09184a3d6f86a646e2de7a28dd0c23da5b809
[firefly-linux-kernel-4.4.55.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
39 static void hex_dump(const void *src, size_t length, size_t line_size, char *prefix)
40 {
41         int i = 0;
42         const unsigned char *address = src;
43         const unsigned char *line = address;
44         unsigned char c;
45
46         printf("%s | ", prefix);
47         while (length-- > 0) {
48                 printf("%02X ", *address++);
49                 if (!(++i % line_size) || (length == 0 && i % line_size)) {
50                         if (length == 0) {
51                                 while (i++ % line_size)
52                                         printf("__ ");
53                         }
54                         printf(" | ");  /* right close */
55                         while (line < address) {
56                                 c = *line++;
57                                 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
58                         }
59                         printf("\n");
60                         if (length > 0)
61                                 printf("%s | ", prefix);
62                 }
63         }
64 }
65
66 static void transfer(int fd)
67 {
68         int ret;
69         uint8_t tx[] = {
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,
76                 0xF0, 0x0D,
77         };
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),
83                 .delay_usecs = delay,
84                 .speed_hz = speed,
85                 .bits_per_word = bits,
86         };
87
88         if (mode & SPI_TX_QUAD)
89                 tr.tx_nbits = 4;
90         else if (mode & SPI_TX_DUAL)
91                 tr.tx_nbits = 2;
92         if (mode & SPI_RX_QUAD)
93                 tr.rx_nbits = 4;
94         else if (mode & SPI_RX_DUAL)
95                 tr.rx_nbits = 2;
96         if (!(mode & SPI_LOOP)) {
97                 if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
98                         tr.rx_buf = 0;
99                 else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
100                         tr.tx_buf = 0;
101         }
102
103         ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
104         if (ret < 1)
105                 pabort("can't send spi message");
106
107         hex_dump(rx, ARRAY_SIZE(rx), 32, "RX");
108 }
109
110 static void print_usage(const char *prog)
111 {
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");
127         exit(1);
128 }
129
130 static void parse_opts(int argc, char *argv[])
131 {
132         while (1) {
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' },
148                         { NULL, 0, 0, 0 },
149                 };
150                 int c;
151
152                 c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24", lopts, NULL);
153
154                 if (c == -1)
155                         break;
156
157                 switch (c) {
158                 case 'D':
159                         device = optarg;
160                         break;
161                 case 's':
162                         speed = atoi(optarg);
163                         break;
164                 case 'd':
165                         delay = atoi(optarg);
166                         break;
167                 case 'b':
168                         bits = atoi(optarg);
169                         break;
170                 case 'l':
171                         mode |= SPI_LOOP;
172                         break;
173                 case 'H':
174                         mode |= SPI_CPHA;
175                         break;
176                 case 'O':
177                         mode |= SPI_CPOL;
178                         break;
179                 case 'L':
180                         mode |= SPI_LSB_FIRST;
181                         break;
182                 case 'C':
183                         mode |= SPI_CS_HIGH;
184                         break;
185                 case '3':
186                         mode |= SPI_3WIRE;
187                         break;
188                 case 'N':
189                         mode |= SPI_NO_CS;
190                         break;
191                 case 'R':
192                         mode |= SPI_READY;
193                         break;
194                 case '2':
195                         mode |= SPI_TX_DUAL;
196                         break;
197                 case '4':
198                         mode |= SPI_TX_QUAD;
199                         break;
200                 default:
201                         print_usage(argv[0]);
202                         break;
203                 }
204         }
205         if (mode & SPI_LOOP) {
206                 if (mode & SPI_TX_DUAL)
207                         mode |= SPI_RX_DUAL;
208                 if (mode & SPI_TX_QUAD)
209                         mode |= SPI_RX_QUAD;
210         }
211 }
212
213 int main(int argc, char *argv[])
214 {
215         int ret = 0;
216         int fd;
217
218         parse_opts(argc, argv);
219
220         fd = open(device, O_RDWR);
221         if (fd < 0)
222                 pabort("can't open device");
223
224         /*
225          * spi mode
226          */
227         ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
228         if (ret == -1)
229                 pabort("can't set spi mode");
230
231         ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
232         if (ret == -1)
233                 pabort("can't get spi mode");
234
235         /*
236          * bits per word
237          */
238         ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
239         if (ret == -1)
240                 pabort("can't set bits per word");
241
242         ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
243         if (ret == -1)
244                 pabort("can't get bits per word");
245
246         /*
247          * max speed hz
248          */
249         ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
250         if (ret == -1)
251                 pabort("can't set max speed hz");
252
253         ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
254         if (ret == -1)
255                 pabort("can't get max speed hz");
256
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);
260
261         transfer(fd);
262
263         close(fd);
264
265         return ret;
266 }