diff options
author | Joshua Clayton <stillcompiling@gmail.com> | 2015-11-18 17:30:37 -0500 |
---|---|---|
committer | Mark Brown <broonie@kernel.org> | 2015-11-23 09:54:01 -0500 |
commit | 5eca4d843f9f0c3140a8657ba2f8217ee6c08c11 (patch) | |
tree | 207aaf175ef415b4569f4637f26630af1aac0c19 /Documentation/spi | |
parent | 8005c49d9aea74d382f474ce11afbbc7d7130bec (diff) |
spi: Move spi code from Documentation to tools
Jon Corbet requested this code moved with the last changeset,
https://lkml.org/lkml/2015/3/1/144,
but the patch was not applied because it missed the Makefile.
Moved spidev_test, spidev_fdx and their Makefile infrastructure.
Signed-off-by: Joshua Clayton <stillcompiling@gmail.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
Diffstat (limited to 'Documentation/spi')
-rw-r--r-- | Documentation/spi/Makefile | 8 | ||||
-rw-r--r-- | Documentation/spi/spidev_fdx.c | 158 | ||||
-rw-r--r-- | Documentation/spi/spidev_test.c | 318 |
3 files changed, 0 insertions, 484 deletions
diff --git a/Documentation/spi/Makefile b/Documentation/spi/Makefile deleted file mode 100644 index efa255813e9d..000000000000 --- a/Documentation/spi/Makefile +++ /dev/null | |||
@@ -1,8 +0,0 @@ | |||
1 | # List of programs to build | ||
2 | hostprogs-y := spidev_test spidev_fdx | ||
3 | |||
4 | # Tell kbuild to always build the programs | ||
5 | always := $(hostprogs-y) | ||
6 | |||
7 | HOSTCFLAGS_spidev_test.o += -I$(objtree)/usr/include | ||
8 | HOSTCFLAGS_spidev_fdx.o += -I$(objtree)/usr/include | ||
diff --git a/Documentation/spi/spidev_fdx.c b/Documentation/spi/spidev_fdx.c deleted file mode 100644 index 0ea3e51292fc..000000000000 --- a/Documentation/spi/spidev_fdx.c +++ /dev/null | |||
@@ -1,158 +0,0 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <unistd.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <fcntl.h> | ||
5 | #include <string.h> | ||
6 | |||
7 | #include <sys/ioctl.h> | ||
8 | #include <sys/types.h> | ||
9 | #include <sys/stat.h> | ||
10 | |||
11 | #include <linux/types.h> | ||
12 | #include <linux/spi/spidev.h> | ||
13 | |||
14 | |||
15 | static int verbose; | ||
16 | |||
17 | static void do_read(int fd, int len) | ||
18 | { | ||
19 | unsigned char buf[32], *bp; | ||
20 | int status; | ||
21 | |||
22 | /* read at least 2 bytes, no more than 32 */ | ||
23 | if (len < 2) | ||
24 | len = 2; | ||
25 | else if (len > sizeof(buf)) | ||
26 | len = sizeof(buf); | ||
27 | memset(buf, 0, sizeof buf); | ||
28 | |||
29 | status = read(fd, buf, len); | ||
30 | if (status < 0) { | ||
31 | perror("read"); | ||
32 | return; | ||
33 | } | ||
34 | if (status != len) { | ||
35 | fprintf(stderr, "short read\n"); | ||
36 | return; | ||
37 | } | ||
38 | |||
39 | printf("read(%2d, %2d): %02x %02x,", len, status, | ||
40 | buf[0], buf[1]); | ||
41 | status -= 2; | ||
42 | bp = buf + 2; | ||
43 | while (status-- > 0) | ||
44 | printf(" %02x", *bp++); | ||
45 | printf("\n"); | ||
46 | } | ||
47 | |||
48 | static void do_msg(int fd, int len) | ||
49 | { | ||
50 | struct spi_ioc_transfer xfer[2]; | ||
51 | unsigned char buf[32], *bp; | ||
52 | int status; | ||
53 | |||
54 | memset(xfer, 0, sizeof xfer); | ||
55 | memset(buf, 0, sizeof buf); | ||
56 | |||
57 | if (len > sizeof buf) | ||
58 | len = sizeof buf; | ||
59 | |||
60 | buf[0] = 0xaa; | ||
61 | xfer[0].tx_buf = (unsigned long)buf; | ||
62 | xfer[0].len = 1; | ||
63 | |||
64 | xfer[1].rx_buf = (unsigned long) buf; | ||
65 | xfer[1].len = len; | ||
66 | |||
67 | status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer); | ||
68 | if (status < 0) { | ||
69 | perror("SPI_IOC_MESSAGE"); | ||
70 | return; | ||
71 | } | ||
72 | |||
73 | printf("response(%2d, %2d): ", len, status); | ||
74 | for (bp = buf; len; len--) | ||
75 | printf(" %02x", *bp++); | ||
76 | printf("\n"); | ||
77 | } | ||
78 | |||
79 | static void dumpstat(const char *name, int fd) | ||
80 | { | ||
81 | __u8 lsb, bits; | ||
82 | __u32 mode, speed; | ||
83 | |||
84 | if (ioctl(fd, SPI_IOC_RD_MODE32, &mode) < 0) { | ||
85 | perror("SPI rd_mode"); | ||
86 | return; | ||
87 | } | ||
88 | if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0) { | ||
89 | perror("SPI rd_lsb_fist"); | ||
90 | return; | ||
91 | } | ||
92 | if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) { | ||
93 | perror("SPI bits_per_word"); | ||
94 | return; | ||
95 | } | ||
96 | if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) { | ||
97 | perror("SPI max_speed_hz"); | ||
98 | return; | ||
99 | } | ||
100 | |||
101 | printf("%s: spi mode 0x%x, %d bits %sper word, %d Hz max\n", | ||
102 | name, mode, bits, lsb ? "(lsb first) " : "", speed); | ||
103 | } | ||
104 | |||
105 | int main(int argc, char **argv) | ||
106 | { | ||
107 | int c; | ||
108 | int readcount = 0; | ||
109 | int msglen = 0; | ||
110 | int fd; | ||
111 | const char *name; | ||
112 | |||
113 | while ((c = getopt(argc, argv, "hm:r:v")) != EOF) { | ||
114 | switch (c) { | ||
115 | case 'm': | ||
116 | msglen = atoi(optarg); | ||
117 | if (msglen < 0) | ||
118 | goto usage; | ||
119 | continue; | ||
120 | case 'r': | ||
121 | readcount = atoi(optarg); | ||
122 | if (readcount < 0) | ||
123 | goto usage; | ||
124 | continue; | ||
125 | case 'v': | ||
126 | verbose++; | ||
127 | continue; | ||
128 | case 'h': | ||
129 | case '?': | ||
130 | usage: | ||
131 | fprintf(stderr, | ||
132 | "usage: %s [-h] [-m N] [-r N] /dev/spidevB.D\n", | ||
133 | argv[0]); | ||
134 | return 1; | ||
135 | } | ||
136 | } | ||
137 | |||
138 | if ((optind + 1) != argc) | ||
139 | goto usage; | ||
140 | name = argv[optind]; | ||
141 | |||
142 | fd = open(name, O_RDWR); | ||
143 | if (fd < 0) { | ||
144 | perror("open"); | ||
145 | return 1; | ||
146 | } | ||
147 | |||
148 | dumpstat(name, fd); | ||
149 | |||
150 | if (msglen) | ||
151 | do_msg(fd, msglen); | ||
152 | |||
153 | if (readcount) | ||
154 | do_read(fd, readcount); | ||
155 | |||
156 | close(fd); | ||
157 | return 0; | ||
158 | } | ||
diff --git a/Documentation/spi/spidev_test.c b/Documentation/spi/spidev_test.c deleted file mode 100644 index 135b3f592b83..000000000000 --- a/Documentation/spi/spidev_test.c +++ /dev/null | |||
@@ -1,318 +0,0 @@ | |||
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 | } | ||