blob: 4b6a3803638b2b887c333836d797129ce3f5485c [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 Remondab78ce7e2015-03-10 16:12:30 -040040static void hex_dump(const void *src, size_t length, size_t line_size, char *prefix)
41{
42 int i = 0;
43 const unsigned char *address = src;
44 const unsigned char *line = address;
45 unsigned char c;
46
47 printf("%s | ", prefix);
48 while (length-- > 0) {
49 printf("%02X ", *address++);
50 if (!(++i % line_size) || (length == 0 && i % line_size)) {
51 if (length == 0) {
52 while (i++ % line_size)
53 printf("__ ");
54 }
55 printf(" | "); /* right close */
56 while (line < address) {
57 c = *line++;
58 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
59 }
60 printf("\n");
61 if (length > 0)
62 printf("%s | ", prefix);
63 }
64 }
65}
66
Anton Vorontsov22b238b2007-07-31 00:38:44 -070067static void transfer(int fd)
68{
69 int ret;
70 uint8_t tx[] = {
71 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
72 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
73 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
74 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
75 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
76 0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
77 0xF0, 0x0D,
78 };
79 uint8_t rx[ARRAY_SIZE(tx)] = {0, };
80 struct spi_ioc_transfer tr = {
81 .tx_buf = (unsigned long)tx,
82 .rx_buf = (unsigned long)rx,
83 .len = ARRAY_SIZE(tx),
84 .delay_usecs = delay,
85 .speed_hz = speed,
86 .bits_per_word = bits,
87 };
88
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +010089 if (mode & SPI_TX_QUAD)
90 tr.tx_nbits = 4;
91 else if (mode & SPI_TX_DUAL)
92 tr.tx_nbits = 2;
93 if (mode & SPI_RX_QUAD)
94 tr.rx_nbits = 4;
95 else if (mode & SPI_RX_DUAL)
96 tr.rx_nbits = 2;
97 if (!(mode & SPI_LOOP)) {
98 if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
99 tr.rx_buf = 0;
100 else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
101 tr.tx_buf = 0;
102 }
103
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700104 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
Hector Palacios95b1ed22010-04-29 15:02:28 -0700105 if (ret < 1)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700106 pabort("can't send spi message");
107
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400108 if (verbose)
109 hex_dump(tx, ARRAY_SIZE(tx), 32, "TX");
Adrian Remondab78ce7e2015-03-10 16:12:30 -0400110 hex_dump(rx, ARRAY_SIZE(rx), 32, "RX");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700111}
112
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700113static void print_usage(const char *prog)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700114{
115 printf("Usage: %s [-DsbdlHOLC3]\n", prog);
116 puts(" -D --device device to use (default /dev/spidev1.1)\n"
117 " -s --speed max speed (Hz)\n"
118 " -d --delay delay (usec)\n"
119 " -b --bpw bits per word \n"
120 " -l --loop loopback\n"
121 " -H --cpha clock phase\n"
122 " -O --cpol clock polarity\n"
123 " -L --lsb least significant bit first\n"
124 " -C --cs-high chip select active high\n"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100125 " -3 --3wire SI/SO signals shared\n"
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400126 " -v --verbose Verbose (show tx buffer)\n"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100127 " -N --no-cs no chip select\n"
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100128 " -R --ready slave pulls low to pause\n"
129 " -2 --dual dual transfer\n"
130 " -4 --quad quad transfer\n");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700131 exit(1);
132}
133
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700134static void parse_opts(int argc, char *argv[])
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700135{
136 while (1) {
WANG Congcd583102007-10-16 01:27:47 -0700137 static const struct option lopts[] = {
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700138 { "device", 1, 0, 'D' },
139 { "speed", 1, 0, 's' },
140 { "delay", 1, 0, 'd' },
141 { "bpw", 1, 0, 'b' },
142 { "loop", 0, 0, 'l' },
143 { "cpha", 0, 0, 'H' },
144 { "cpol", 0, 0, 'O' },
145 { "lsb", 0, 0, 'L' },
146 { "cs-high", 0, 0, 'C' },
147 { "3wire", 0, 0, '3' },
David Brownellb55f6272009-06-30 11:41:26 -0700148 { "no-cs", 0, 0, 'N' },
149 { "ready", 0, 0, 'R' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100150 { "dual", 0, 0, '2' },
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400151 { "verbose", 0, 0, 'v' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100152 { "quad", 0, 0, '4' },
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700153 { NULL, 0, 0, 0 },
154 };
155 int c;
156
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400157 c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24:v", lopts, NULL);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700158
159 if (c == -1)
160 break;
161
162 switch (c) {
163 case 'D':
164 device = optarg;
165 break;
166 case 's':
167 speed = atoi(optarg);
168 break;
169 case 'd':
170 delay = atoi(optarg);
171 break;
172 case 'b':
173 bits = atoi(optarg);
174 break;
175 case 'l':
176 mode |= SPI_LOOP;
177 break;
178 case 'H':
179 mode |= SPI_CPHA;
180 break;
181 case 'O':
182 mode |= SPI_CPOL;
183 break;
184 case 'L':
185 mode |= SPI_LSB_FIRST;
186 break;
187 case 'C':
188 mode |= SPI_CS_HIGH;
189 break;
190 case '3':
191 mode |= SPI_3WIRE;
192 break;
David Brownellb55f6272009-06-30 11:41:26 -0700193 case 'N':
194 mode |= SPI_NO_CS;
195 break;
Adrian Remonda31a5c5a2015-03-10 16:12:31 -0400196 case 'v':
197 verbose = 1;
198 break;
David Brownellb55f6272009-06-30 11:41:26 -0700199 case 'R':
200 mode |= SPI_READY;
201 break;
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100202 case '2':
203 mode |= SPI_TX_DUAL;
204 break;
205 case '4':
206 mode |= SPI_TX_QUAD;
207 break;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700208 default:
209 print_usage(argv[0]);
210 break;
211 }
212 }
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100213 if (mode & SPI_LOOP) {
214 if (mode & SPI_TX_DUAL)
215 mode |= SPI_RX_DUAL;
216 if (mode & SPI_TX_QUAD)
217 mode |= SPI_RX_QUAD;
218 }
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700219}
220
221int main(int argc, char *argv[])
222{
223 int ret = 0;
224 int fd;
225
226 parse_opts(argc, argv);
227
228 fd = open(device, O_RDWR);
229 if (fd < 0)
230 pabort("can't open device");
231
232 /*
233 * spi mode
234 */
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100235 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700236 if (ret == -1)
237 pabort("can't set spi mode");
238
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100239 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700240 if (ret == -1)
241 pabort("can't get spi mode");
242
243 /*
244 * bits per word
245 */
246 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
247 if (ret == -1)
248 pabort("can't set bits per word");
249
250 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
251 if (ret == -1)
252 pabort("can't get bits per word");
253
254 /*
255 * max speed hz
256 */
257 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
258 if (ret == -1)
259 pabort("can't set max speed hz");
260
261 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
262 if (ret == -1)
263 pabort("can't get max speed hz");
264
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100265 printf("spi mode: 0x%x\n", mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700266 printf("bits per word: %d\n", bits);
267 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
268
269 transfer(fd);
270
271 close(fd);
272
273 return ret;
274}