blob: 9cb09184a3d6f86a646e2de7a28dd0c23da5b809 [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;
38
Adrian Remondab78ce7e2015-03-10 16:12:30 -040039static 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
Anton Vorontsov22b238b2007-07-31 00:38:44 -070066static 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
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +010088 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
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700103 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
Hector Palacios95b1ed22010-04-29 15:02:28 -0700104 if (ret < 1)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700105 pabort("can't send spi message");
106
Adrian Remondab78ce7e2015-03-10 16:12:30 -0400107 hex_dump(rx, ARRAY_SIZE(rx), 32, "RX");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700108}
109
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700110static void print_usage(const char *prog)
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700111{
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"
Geert Uytterhoeven925d16a2014-02-20 16:01:43 +0100122 " -3 --3wire SI/SO signals shared\n"
123 " -N --no-cs no chip select\n"
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100124 " -R --ready slave pulls low to pause\n"
125 " -2 --dual dual transfer\n"
126 " -4 --quad quad transfer\n");
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700127 exit(1);
128}
129
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700130static void parse_opts(int argc, char *argv[])
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700131{
132 while (1) {
WANG Congcd583102007-10-16 01:27:47 -0700133 static const struct option lopts[] = {
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700134 { "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' },
David Brownellb55f6272009-06-30 11:41:26 -0700144 { "no-cs", 0, 0, 'N' },
145 { "ready", 0, 0, 'R' },
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100146 { "dual", 0, 0, '2' },
147 { "quad", 0, 0, '4' },
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700148 { NULL, 0, 0, 0 },
149 };
150 int c;
151
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100152 c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24", lopts, NULL);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700153
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;
David Brownellb55f6272009-06-30 11:41:26 -0700188 case 'N':
189 mode |= SPI_NO_CS;
190 break;
191 case 'R':
192 mode |= SPI_READY;
193 break;
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100194 case '2':
195 mode |= SPI_TX_DUAL;
196 break;
197 case '4':
198 mode |= SPI_TX_QUAD;
199 break;
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700200 default:
201 print_usage(argv[0]);
202 break;
203 }
204 }
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100205 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 }
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700211}
212
213int 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 */
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100227 ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700228 if (ret == -1)
229 pabort("can't set spi mode");
230
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100231 ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700232 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
Geert Uytterhoevenc2e78c32014-02-25 11:40:18 +0100257 printf("spi mode: 0x%x\n", mode);
Anton Vorontsov22b238b2007-07-31 00:38:44 -0700258 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}