Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 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> |
Adrian Remonda | b78ce7e | 2015-03-10 16:12:30 -0400 | [diff] [blame] | 18 | #include <string.h> |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 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 | |
WANG Cong | cd58310 | 2007-10-16 01:27:47 -0700 | [diff] [blame] | 33 | static const char *device = "/dev/spidev1.1"; |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 34 | static uint32_t mode; |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 35 | static uint8_t bits = 8; |
| 36 | static uint32_t speed = 500000; |
| 37 | static uint16_t delay; |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 38 | static int verbose; |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 39 | |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 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 | |
Adrian Remonda | b78ce7e | 2015-03-10 16:12:30 -0400 | [diff] [blame] | 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 | |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 79 | /* |
| 80 | * Unescape - process hexadecimal escape character |
| 81 | * converts shell input "\x23" -> 0x23 |
| 82 | */ |
Andrew Morton | 07eec62 | 2015-04-16 12:49:29 -0700 | [diff] [blame] | 83 | static int unescape(char *_dst, char *_src, size_t len) |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 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) |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 104 | { |
| 105 | int ret; |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 106 | |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 107 | struct spi_ioc_transfer tr = { |
| 108 | .tx_buf = (unsigned long)tx, |
| 109 | .rx_buf = (unsigned long)rx, |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 110 | .len = len, |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 111 | .delay_usecs = delay, |
| 112 | .speed_hz = speed, |
| 113 | .bits_per_word = bits, |
| 114 | }; |
| 115 | |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 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 | |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 131 | ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); |
Hector Palacios | 95b1ed2 | 2010-04-29 15:02:28 -0700 | [diff] [blame] | 132 | if (ret < 1) |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 133 | pabort("can't send spi message"); |
| 134 | |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 135 | if (verbose) |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 136 | hex_dump(tx, len, 32, "TX"); |
| 137 | hex_dump(rx, len, 32, "RX"); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 140 | static void print_usage(const char *prog) |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 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" |
Geert Uytterhoeven | 925d16a | 2014-02-20 16:01:43 +0100 | [diff] [blame] | 152 | " -3 --3wire SI/SO signals shared\n" |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 153 | " -v --verbose Verbose (show tx buffer)\n" |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 154 | " -p Send data (e.g. \"1234\\xde\\xad\")\n" |
Geert Uytterhoeven | 925d16a | 2014-02-20 16:01:43 +0100 | [diff] [blame] | 155 | " -N --no-cs no chip select\n" |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 156 | " -R --ready slave pulls low to pause\n" |
| 157 | " -2 --dual dual transfer\n" |
| 158 | " -4 --quad quad transfer\n"); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 159 | exit(1); |
| 160 | } |
| 161 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 162 | static void parse_opts(int argc, char *argv[]) |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 163 | { |
| 164 | while (1) { |
WANG Cong | cd58310 | 2007-10-16 01:27:47 -0700 | [diff] [blame] | 165 | static const struct option lopts[] = { |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 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' }, |
David Brownell | b55f627 | 2009-06-30 11:41:26 -0700 | [diff] [blame] | 176 | { "no-cs", 0, 0, 'N' }, |
| 177 | { "ready", 0, 0, 'R' }, |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 178 | { "dual", 0, 0, '2' }, |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 179 | { "verbose", 0, 0, 'v' }, |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 180 | { "quad", 0, 0, '4' }, |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 181 | { NULL, 0, 0, 0 }, |
| 182 | }; |
| 183 | int c; |
| 184 | |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 185 | c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24p:v", lopts, NULL); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 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; |
David Brownell | b55f627 | 2009-06-30 11:41:26 -0700 | [diff] [blame] | 221 | case 'N': |
| 222 | mode |= SPI_NO_CS; |
| 223 | break; |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 224 | case 'v': |
| 225 | verbose = 1; |
| 226 | break; |
David Brownell | b55f627 | 2009-06-30 11:41:26 -0700 | [diff] [blame] | 227 | case 'R': |
| 228 | mode |= SPI_READY; |
| 229 | break; |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 230 | case 'p': |
| 231 | input_tx = optarg; |
| 232 | break; |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 233 | case '2': |
| 234 | mode |= SPI_TX_DUAL; |
| 235 | break; |
| 236 | case '4': |
| 237 | mode |= SPI_TX_QUAD; |
| 238 | break; |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 239 | default: |
| 240 | print_usage(argv[0]); |
| 241 | break; |
| 242 | } |
| 243 | } |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 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 | } |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | int main(int argc, char *argv[]) |
| 253 | { |
| 254 | int ret = 0; |
| 255 | int fd; |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 256 | uint8_t *tx; |
| 257 | uint8_t *rx; |
| 258 | int size; |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 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 | */ |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 269 | ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 270 | if (ret == -1) |
| 271 | pabort("can't set spi mode"); |
| 272 | |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 273 | ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 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 | |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 299 | printf("spi mode: 0x%x\n", mode); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 300 | printf("bits per word: %d\n", bits); |
| 301 | printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); |
| 302 | |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 303 | if (input_tx) { |
| 304 | size = strlen(input_tx+1); |
| 305 | tx = malloc(size); |
| 306 | rx = malloc(size); |
Andrew Morton | 07eec62 | 2015-04-16 12:49:29 -0700 | [diff] [blame] | 307 | size = unescape((char *)tx, input_tx, size); |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 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 | } |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 314 | |
| 315 | close(fd); |
| 316 | |
| 317 | return ret; |
| 318 | } |