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> |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 21 | #include <time.h> |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 22 | #include <sys/ioctl.h> |
Baruch Siach | 8736f80 | 2016-08-12 16:04:33 +0300 | [diff] [blame] | 23 | #include <linux/ioctl.h> |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 25 | #include <linux/types.h> |
| 26 | #include <linux/spi/spidev.h> |
| 27 | |
| 28 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 29 | |
| 30 | static void pabort(const char *s) |
| 31 | { |
| 32 | perror(s); |
| 33 | abort(); |
| 34 | } |
| 35 | |
WANG Cong | cd58310 | 2007-10-16 01:27:47 -0700 | [diff] [blame] | 36 | static const char *device = "/dev/spidev1.1"; |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 37 | static uint32_t mode; |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 38 | static uint8_t bits = 8; |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 39 | static char *input_file; |
Joshua Clayton | 983b27886 | 2015-11-18 14:30:40 -0800 | [diff] [blame] | 40 | static char *output_file; |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 41 | static uint32_t speed = 500000; |
| 42 | static uint16_t delay; |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 43 | static int verbose; |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 44 | static int transfer_size; |
| 45 | static int iterations; |
| 46 | static int interval = 5; /* interval in seconds for showing transfer rate */ |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 47 | |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 48 | uint8_t default_tx[] = { |
| 49 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 50 | 0x40, 0x00, 0x00, 0x00, 0x00, 0x95, |
| 51 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 52 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 53 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 54 | 0xF0, 0x0D, |
| 55 | }; |
| 56 | |
| 57 | uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, }; |
| 58 | char *input_tx; |
| 59 | |
Joshua Clayton | b2cfc90 | 2015-11-18 14:30:42 -0800 | [diff] [blame] | 60 | static void hex_dump(const void *src, size_t length, size_t line_size, |
| 61 | char *prefix) |
Adrian Remonda | b78ce7e | 2015-03-10 16:12:30 -0400 | [diff] [blame] | 62 | { |
| 63 | int i = 0; |
| 64 | const unsigned char *address = src; |
| 65 | const unsigned char *line = address; |
| 66 | unsigned char c; |
| 67 | |
| 68 | printf("%s | ", prefix); |
| 69 | while (length-- > 0) { |
| 70 | printf("%02X ", *address++); |
| 71 | if (!(++i % line_size) || (length == 0 && i % line_size)) { |
| 72 | if (length == 0) { |
| 73 | while (i++ % line_size) |
| 74 | printf("__ "); |
| 75 | } |
| 76 | printf(" | "); /* right close */ |
| 77 | while (line < address) { |
| 78 | c = *line++; |
| 79 | printf("%c", (c < 33 || c == 255) ? 0x2E : c); |
| 80 | } |
| 81 | printf("\n"); |
| 82 | if (length > 0) |
| 83 | printf("%s | ", prefix); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 88 | /* |
| 89 | * Unescape - process hexadecimal escape character |
| 90 | * converts shell input "\x23" -> 0x23 |
| 91 | */ |
Andrew Morton | 07eec62 | 2015-04-16 12:49:29 -0700 | [diff] [blame] | 92 | static int unescape(char *_dst, char *_src, size_t len) |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 93 | { |
| 94 | int ret = 0; |
Joshua Clayton | a20874f | 2015-11-18 14:30:41 -0800 | [diff] [blame] | 95 | int match; |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 96 | char *src = _src; |
| 97 | char *dst = _dst; |
| 98 | unsigned int ch; |
| 99 | |
| 100 | while (*src) { |
| 101 | if (*src == '\\' && *(src+1) == 'x') { |
Joshua Clayton | a20874f | 2015-11-18 14:30:41 -0800 | [diff] [blame] | 102 | match = sscanf(src + 2, "%2x", &ch); |
| 103 | if (!match) |
| 104 | pabort("malformed input string"); |
| 105 | |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 106 | src += 4; |
| 107 | *dst++ = (unsigned char)ch; |
| 108 | } else { |
| 109 | *dst++ = *src++; |
| 110 | } |
| 111 | ret++; |
| 112 | } |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | 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] | 117 | { |
| 118 | int ret; |
Joshua Clayton | 983b27886 | 2015-11-18 14:30:40 -0800 | [diff] [blame] | 119 | int out_fd; |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 120 | struct spi_ioc_transfer tr = { |
| 121 | .tx_buf = (unsigned long)tx, |
| 122 | .rx_buf = (unsigned long)rx, |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 123 | .len = len, |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 124 | .delay_usecs = delay, |
| 125 | .speed_hz = speed, |
| 126 | .bits_per_word = bits, |
| 127 | }; |
| 128 | |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 129 | if (mode & SPI_TX_QUAD) |
| 130 | tr.tx_nbits = 4; |
| 131 | else if (mode & SPI_TX_DUAL) |
| 132 | tr.tx_nbits = 2; |
| 133 | if (mode & SPI_RX_QUAD) |
| 134 | tr.rx_nbits = 4; |
| 135 | else if (mode & SPI_RX_DUAL) |
| 136 | tr.rx_nbits = 2; |
| 137 | if (!(mode & SPI_LOOP)) { |
| 138 | if (mode & (SPI_TX_QUAD | SPI_TX_DUAL)) |
| 139 | tr.rx_buf = 0; |
| 140 | else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL)) |
| 141 | tr.tx_buf = 0; |
| 142 | } |
| 143 | |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 144 | ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); |
Hector Palacios | 95b1ed2 | 2010-04-29 15:02:28 -0700 | [diff] [blame] | 145 | if (ret < 1) |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 146 | pabort("can't send spi message"); |
| 147 | |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 148 | if (verbose) |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 149 | hex_dump(tx, len, 32, "TX"); |
Joshua Clayton | 983b27886 | 2015-11-18 14:30:40 -0800 | [diff] [blame] | 150 | |
| 151 | if (output_file) { |
| 152 | out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 153 | if (out_fd < 0) |
| 154 | pabort("could not open output file"); |
| 155 | |
| 156 | ret = write(out_fd, rx, len); |
| 157 | if (ret != len) |
Fabio Estevam | edd3899 | 2015-12-04 10:59:14 -0200 | [diff] [blame] | 158 | pabort("not all bytes written to output file"); |
Joshua Clayton | 983b27886 | 2015-11-18 14:30:40 -0800 | [diff] [blame] | 159 | |
| 160 | close(out_fd); |
| 161 | } |
| 162 | |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 163 | if (verbose) |
Joshua Clayton | 983b27886 | 2015-11-18 14:30:40 -0800 | [diff] [blame] | 164 | hex_dump(rx, len, 32, "RX"); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 167 | static void print_usage(const char *prog) |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 168 | { |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 169 | printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 170 | puts(" -D --device device to use (default /dev/spidev1.1)\n" |
| 171 | " -s --speed max speed (Hz)\n" |
| 172 | " -d --delay delay (usec)\n" |
Joshua Clayton | b2cfc90 | 2015-11-18 14:30:42 -0800 | [diff] [blame] | 173 | " -b --bpw bits per word\n" |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 174 | " -i --input input data from a file (e.g. \"test.bin\")\n" |
Joshua Clayton | 983b27886 | 2015-11-18 14:30:40 -0800 | [diff] [blame] | 175 | " -o --output output data to a file (e.g. \"results.bin\")\n" |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 176 | " -l --loop loopback\n" |
| 177 | " -H --cpha clock phase\n" |
| 178 | " -O --cpol clock polarity\n" |
| 179 | " -L --lsb least significant bit first\n" |
| 180 | " -C --cs-high chip select active high\n" |
Geert Uytterhoeven | 925d16a | 2014-02-20 16:01:43 +0100 | [diff] [blame] | 181 | " -3 --3wire SI/SO signals shared\n" |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 182 | " -v --verbose Verbose (show tx buffer)\n" |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 183 | " -p Send data (e.g. \"1234\\xde\\xad\")\n" |
Geert Uytterhoeven | 925d16a | 2014-02-20 16:01:43 +0100 | [diff] [blame] | 184 | " -N --no-cs no chip select\n" |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 185 | " -R --ready slave pulls low to pause\n" |
| 186 | " -2 --dual dual transfer\n" |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 187 | " -4 --quad quad transfer\n" |
| 188 | " -S --size transfer size\n" |
| 189 | " -I --iter iterations\n"); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 190 | exit(1); |
| 191 | } |
| 192 | |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 193 | static void parse_opts(int argc, char *argv[]) |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 194 | { |
| 195 | while (1) { |
WANG Cong | cd58310 | 2007-10-16 01:27:47 -0700 | [diff] [blame] | 196 | static const struct option lopts[] = { |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 197 | { "device", 1, 0, 'D' }, |
| 198 | { "speed", 1, 0, 's' }, |
| 199 | { "delay", 1, 0, 'd' }, |
| 200 | { "bpw", 1, 0, 'b' }, |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 201 | { "input", 1, 0, 'i' }, |
Joshua Clayton | 983b27886 | 2015-11-18 14:30:40 -0800 | [diff] [blame] | 202 | { "output", 1, 0, 'o' }, |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 203 | { "loop", 0, 0, 'l' }, |
| 204 | { "cpha", 0, 0, 'H' }, |
| 205 | { "cpol", 0, 0, 'O' }, |
| 206 | { "lsb", 0, 0, 'L' }, |
| 207 | { "cs-high", 0, 0, 'C' }, |
| 208 | { "3wire", 0, 0, '3' }, |
David Brownell | b55f627 | 2009-06-30 11:41:26 -0700 | [diff] [blame] | 209 | { "no-cs", 0, 0, 'N' }, |
| 210 | { "ready", 0, 0, 'R' }, |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 211 | { "dual", 0, 0, '2' }, |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 212 | { "verbose", 0, 0, 'v' }, |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 213 | { "quad", 0, 0, '4' }, |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 214 | { "size", 1, 0, 'S' }, |
| 215 | { "iter", 1, 0, 'I' }, |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 216 | { NULL, 0, 0, 0 }, |
| 217 | }; |
| 218 | int c; |
| 219 | |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 220 | c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:vS:I:", |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 221 | lopts, NULL); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 222 | |
| 223 | if (c == -1) |
| 224 | break; |
| 225 | |
| 226 | switch (c) { |
| 227 | case 'D': |
| 228 | device = optarg; |
| 229 | break; |
| 230 | case 's': |
| 231 | speed = atoi(optarg); |
| 232 | break; |
| 233 | case 'd': |
| 234 | delay = atoi(optarg); |
| 235 | break; |
| 236 | case 'b': |
| 237 | bits = atoi(optarg); |
| 238 | break; |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 239 | case 'i': |
| 240 | input_file = optarg; |
| 241 | break; |
Joshua Clayton | 983b27886 | 2015-11-18 14:30:40 -0800 | [diff] [blame] | 242 | case 'o': |
| 243 | output_file = optarg; |
| 244 | break; |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 245 | case 'l': |
| 246 | mode |= SPI_LOOP; |
| 247 | break; |
| 248 | case 'H': |
| 249 | mode |= SPI_CPHA; |
| 250 | break; |
| 251 | case 'O': |
| 252 | mode |= SPI_CPOL; |
| 253 | break; |
| 254 | case 'L': |
| 255 | mode |= SPI_LSB_FIRST; |
| 256 | break; |
| 257 | case 'C': |
| 258 | mode |= SPI_CS_HIGH; |
| 259 | break; |
| 260 | case '3': |
| 261 | mode |= SPI_3WIRE; |
| 262 | break; |
David Brownell | b55f627 | 2009-06-30 11:41:26 -0700 | [diff] [blame] | 263 | case 'N': |
| 264 | mode |= SPI_NO_CS; |
| 265 | break; |
Adrian Remonda | 31a5c5a | 2015-03-10 16:12:31 -0400 | [diff] [blame] | 266 | case 'v': |
| 267 | verbose = 1; |
| 268 | break; |
David Brownell | b55f627 | 2009-06-30 11:41:26 -0700 | [diff] [blame] | 269 | case 'R': |
| 270 | mode |= SPI_READY; |
| 271 | break; |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 272 | case 'p': |
| 273 | input_tx = optarg; |
| 274 | break; |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 275 | case '2': |
| 276 | mode |= SPI_TX_DUAL; |
| 277 | break; |
| 278 | case '4': |
| 279 | mode |= SPI_TX_QUAD; |
| 280 | break; |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 281 | case 'S': |
| 282 | transfer_size = atoi(optarg); |
| 283 | break; |
| 284 | case 'I': |
| 285 | iterations = atoi(optarg); |
| 286 | break; |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 287 | default: |
| 288 | print_usage(argv[0]); |
| 289 | break; |
| 290 | } |
| 291 | } |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 292 | if (mode & SPI_LOOP) { |
| 293 | if (mode & SPI_TX_DUAL) |
| 294 | mode |= SPI_RX_DUAL; |
| 295 | if (mode & SPI_TX_QUAD) |
| 296 | mode |= SPI_RX_QUAD; |
| 297 | } |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Joshua Clayton | 5c437a4 | 2015-11-18 14:30:38 -0800 | [diff] [blame] | 300 | static void transfer_escaped_string(int fd, char *str) |
| 301 | { |
Geert Uytterhoeven | 0278b34 | 2016-09-09 09:02:51 +0200 | [diff] [blame] | 302 | size_t size = strlen(str); |
Joshua Clayton | 5c437a4 | 2015-11-18 14:30:38 -0800 | [diff] [blame] | 303 | uint8_t *tx; |
| 304 | uint8_t *rx; |
| 305 | |
| 306 | tx = malloc(size); |
| 307 | if (!tx) |
| 308 | pabort("can't allocate tx buffer"); |
| 309 | |
| 310 | rx = malloc(size); |
| 311 | if (!rx) |
| 312 | pabort("can't allocate rx buffer"); |
| 313 | |
| 314 | size = unescape((char *)tx, str, size); |
| 315 | transfer(fd, tx, rx, size); |
| 316 | free(rx); |
| 317 | free(tx); |
| 318 | } |
| 319 | |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 320 | static void transfer_file(int fd, char *filename) |
| 321 | { |
| 322 | ssize_t bytes; |
| 323 | struct stat sb; |
| 324 | int tx_fd; |
| 325 | uint8_t *tx; |
| 326 | uint8_t *rx; |
| 327 | |
| 328 | if (stat(filename, &sb) == -1) |
| 329 | pabort("can't stat input file"); |
| 330 | |
| 331 | tx_fd = open(filename, O_RDONLY); |
Michal Vokáč | e634b76 | 2016-11-04 11:30:03 +0100 | [diff] [blame] | 332 | if (tx_fd < 0) |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 333 | pabort("can't open input file"); |
| 334 | |
| 335 | tx = malloc(sb.st_size); |
| 336 | if (!tx) |
| 337 | pabort("can't allocate tx buffer"); |
| 338 | |
| 339 | rx = malloc(sb.st_size); |
| 340 | if (!rx) |
| 341 | pabort("can't allocate rx buffer"); |
| 342 | |
| 343 | bytes = read(tx_fd, tx, sb.st_size); |
| 344 | if (bytes != sb.st_size) |
| 345 | pabort("failed to read input file"); |
| 346 | |
| 347 | transfer(fd, tx, rx, sb.st_size); |
| 348 | free(rx); |
| 349 | free(tx); |
| 350 | close(tx_fd); |
| 351 | } |
| 352 | |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 353 | static uint64_t _read_count; |
| 354 | static uint64_t _write_count; |
| 355 | |
| 356 | static void show_transfer_rate(void) |
| 357 | { |
| 358 | static uint64_t prev_read_count, prev_write_count; |
| 359 | double rx_rate, tx_rate; |
| 360 | |
| 361 | rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0); |
| 362 | tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0); |
| 363 | |
| 364 | printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate); |
| 365 | |
| 366 | prev_read_count = _read_count; |
| 367 | prev_write_count = _write_count; |
| 368 | } |
| 369 | |
| 370 | static void transfer_buf(int fd, int len) |
| 371 | { |
| 372 | uint8_t *tx; |
| 373 | uint8_t *rx; |
| 374 | int i; |
| 375 | |
| 376 | tx = malloc(len); |
| 377 | if (!tx) |
| 378 | pabort("can't allocate tx buffer"); |
| 379 | for (i = 0; i < len; i++) |
| 380 | tx[i] = random(); |
| 381 | |
| 382 | rx = malloc(len); |
| 383 | if (!rx) |
| 384 | pabort("can't allocate rx buffer"); |
| 385 | |
| 386 | transfer(fd, tx, rx, len); |
| 387 | |
| 388 | _write_count += len; |
| 389 | _read_count += len; |
| 390 | |
| 391 | if (mode & SPI_LOOP) { |
| 392 | if (memcmp(tx, rx, len)) { |
| 393 | fprintf(stderr, "transfer error !\n"); |
| 394 | hex_dump(tx, len, 32, "TX"); |
| 395 | hex_dump(rx, len, 32, "RX"); |
| 396 | exit(1); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | free(rx); |
| 401 | free(tx); |
| 402 | } |
| 403 | |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 404 | int main(int argc, char *argv[]) |
| 405 | { |
| 406 | int ret = 0; |
| 407 | int fd; |
| 408 | |
| 409 | parse_opts(argc, argv); |
| 410 | |
| 411 | fd = open(device, O_RDWR); |
| 412 | if (fd < 0) |
| 413 | pabort("can't open device"); |
| 414 | |
| 415 | /* |
| 416 | * spi mode |
| 417 | */ |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 418 | ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 419 | if (ret == -1) |
| 420 | pabort("can't set spi mode"); |
| 421 | |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 422 | ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 423 | if (ret == -1) |
| 424 | pabort("can't get spi mode"); |
| 425 | |
| 426 | /* |
| 427 | * bits per word |
| 428 | */ |
| 429 | ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); |
| 430 | if (ret == -1) |
| 431 | pabort("can't set bits per word"); |
| 432 | |
| 433 | ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); |
| 434 | if (ret == -1) |
| 435 | pabort("can't get bits per word"); |
| 436 | |
| 437 | /* |
| 438 | * max speed hz |
| 439 | */ |
| 440 | ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); |
| 441 | if (ret == -1) |
| 442 | pabort("can't set max speed hz"); |
| 443 | |
| 444 | ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); |
| 445 | if (ret == -1) |
| 446 | pabort("can't get max speed hz"); |
| 447 | |
Geert Uytterhoeven | c2e78c3 | 2014-02-25 11:40:18 +0100 | [diff] [blame] | 448 | printf("spi mode: 0x%x\n", mode); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 449 | printf("bits per word: %d\n", bits); |
| 450 | printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); |
| 451 | |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 452 | if (input_tx && input_file) |
| 453 | pabort("only one of -p and --input may be selected"); |
| 454 | |
Joshua Clayton | 5c437a4 | 2015-11-18 14:30:38 -0800 | [diff] [blame] | 455 | if (input_tx) |
| 456 | transfer_escaped_string(fd, input_tx); |
Joshua Clayton | 7af475a | 2015-11-18 14:30:39 -0800 | [diff] [blame] | 457 | else if (input_file) |
| 458 | transfer_file(fd, input_file); |
Frode Isaksen | 9006a7b | 2017-03-21 16:48:46 +0100 | [diff] [blame] | 459 | else if (transfer_size) { |
| 460 | struct timespec last_stat; |
| 461 | |
| 462 | clock_gettime(CLOCK_MONOTONIC, &last_stat); |
| 463 | |
| 464 | while (iterations-- > 0) { |
| 465 | struct timespec current; |
| 466 | |
| 467 | transfer_buf(fd, transfer_size); |
| 468 | |
| 469 | clock_gettime(CLOCK_MONOTONIC, ¤t); |
| 470 | if (current.tv_sec - last_stat.tv_sec > interval) { |
| 471 | show_transfer_rate(); |
| 472 | last_stat = current; |
| 473 | } |
| 474 | } |
| 475 | printf("total: tx %.1fKB, rx %.1fKB\n", |
| 476 | _write_count/1024.0, _read_count/1024.0); |
| 477 | } else |
Adrian Remonda | 3006191 | 2015-03-10 16:12:32 -0400 | [diff] [blame] | 478 | transfer(fd, default_tx, default_rx, sizeof(default_tx)); |
Anton Vorontsov | 22b238b | 2007-07-31 00:38:44 -0700 | [diff] [blame] | 479 | |
| 480 | close(fd); |
| 481 | |
| 482 | return ret; |
| 483 | } |