spidev_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SPI testing utility (using spidev driver)
  4. *
  5. * Copyright (c) 2007 MontaVista Software, Inc.
  6. * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
  9. */
  10. #include <stdint.h>
  11. #include <unistd.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <getopt.h>
  17. #include <fcntl.h>
  18. #include <time.h>
  19. #include <sys/ioctl.h>
  20. #include <linux/ioctl.h>
  21. #include <sys/stat.h>
  22. #include <linux/types.h>
  23. #include <linux/spi/spidev.h>
  24. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  25. static void pabort(const char *s)
  26. {
  27. if (errno != 0)
  28. perror(s);
  29. else
  30. printf("%s\n", s);
  31. abort();
  32. }
  33. static const char *device = "/dev/spidev1.1";
  34. static uint32_t mode;
  35. static uint8_t bits = 8;
  36. static char *input_file;
  37. static char *output_file;
  38. static uint32_t speed = 500000;
  39. static uint16_t delay;
  40. static int verbose;
  41. static int transfer_size;
  42. static int iterations;
  43. static int interval = 5; /* interval in seconds for showing transfer rate */
  44. static uint8_t default_tx[] = {
  45. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  46. 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
  47. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  48. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  49. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  50. 0xF0, 0x0D,
  51. };
  52. static uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
  53. static char *input_tx;
  54. static void hex_dump(const void *src, size_t length, size_t line_size,
  55. char *prefix)
  56. {
  57. int i = 0;
  58. const unsigned char *address = src;
  59. const unsigned char *line = address;
  60. unsigned char c;
  61. printf("%s | ", prefix);
  62. while (length-- > 0) {
  63. printf("%02X ", *address++);
  64. if (!(++i % line_size) || (length == 0 && i % line_size)) {
  65. if (length == 0) {
  66. while (i++ % line_size)
  67. printf("__ ");
  68. }
  69. printf(" |");
  70. while (line < address) {
  71. c = *line++;
  72. printf("%c", (c < 32 || c > 126) ? '.' : c);
  73. }
  74. printf("|\n");
  75. if (length > 0)
  76. printf("%s | ", prefix);
  77. }
  78. }
  79. }
  80. /*
  81. * Unescape - process hexadecimal escape character
  82. * converts shell input "\x23" -> 0x23
  83. */
  84. static int unescape(char *_dst, char *_src, size_t len)
  85. {
  86. int ret = 0;
  87. int match;
  88. char *src = _src;
  89. char *dst = _dst;
  90. unsigned int ch;
  91. while (*src) {
  92. if (*src == '\\' && *(src+1) == 'x') {
  93. match = sscanf(src + 2, "%2x", &ch);
  94. if (!match)
  95. pabort("malformed input string");
  96. src += 4;
  97. *dst++ = (unsigned char)ch;
  98. } else {
  99. *dst++ = *src++;
  100. }
  101. ret++;
  102. }
  103. return ret;
  104. }
  105. static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
  106. {
  107. int ret;
  108. int out_fd;
  109. struct spi_ioc_transfer tr = {
  110. .tx_buf = (unsigned long)tx,
  111. .rx_buf = (unsigned long)rx,
  112. .len = len,
  113. .delay_usecs = delay,
  114. .speed_hz = speed,
  115. .bits_per_word = bits,
  116. };
  117. if (mode & SPI_TX_OCTAL)
  118. tr.tx_nbits = 8;
  119. else if (mode & SPI_TX_QUAD)
  120. tr.tx_nbits = 4;
  121. else if (mode & SPI_TX_DUAL)
  122. tr.tx_nbits = 2;
  123. if (mode & SPI_RX_OCTAL)
  124. tr.rx_nbits = 8;
  125. else if (mode & SPI_RX_QUAD)
  126. tr.rx_nbits = 4;
  127. else if (mode & SPI_RX_DUAL)
  128. tr.rx_nbits = 2;
  129. if (!(mode & SPI_LOOP)) {
  130. if (mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL))
  131. tr.rx_buf = 0;
  132. else if (mode & (SPI_RX_OCTAL | SPI_RX_QUAD | SPI_RX_DUAL))
  133. tr.tx_buf = 0;
  134. }
  135. ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  136. if (ret < 1)
  137. pabort("can't send spi message");
  138. if (verbose)
  139. hex_dump(tx, len, 32, "TX");
  140. if (output_file) {
  141. out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  142. if (out_fd < 0)
  143. pabort("could not open output file");
  144. ret = write(out_fd, rx, len);
  145. if (ret != len)
  146. pabort("not all bytes written to output file");
  147. close(out_fd);
  148. }
  149. if (verbose)
  150. hex_dump(rx, len, 32, "RX");
  151. }
  152. static void print_usage(const char *prog)
  153. {
  154. printf("Usage: %s [-2348CDFHILMNORSZbdilopsv]\n", prog);
  155. puts("general device settings:\n"
  156. " -D --device device to use (default /dev/spidev1.1)\n"
  157. " -s --speed max speed (Hz)\n"
  158. " -d --delay delay (usec)\n"
  159. " -l --loop loopback\n"
  160. "spi mode:\n"
  161. " -H --cpha clock phase\n"
  162. " -O --cpol clock polarity\n"
  163. " -F --rx-cpha-flip flip CPHA on Rx only xfer\n"
  164. "number of wires for transmission:\n"
  165. " -2 --dual dual transfer\n"
  166. " -4 --quad quad transfer\n"
  167. " -8 --octal octal transfer\n"
  168. " -3 --3wire SI/SO signals shared\n"
  169. " -Z --3wire-hiz high impedance turnaround\n"
  170. "data:\n"
  171. " -i --input input data from a file (e.g. \"test.bin\")\n"
  172. " -o --output output data to a file (e.g. \"results.bin\")\n"
  173. " -p Send data (e.g. \"1234\\xde\\xad\")\n"
  174. " -S --size transfer size\n"
  175. " -I --iter iterations\n"
  176. "additional parameters:\n"
  177. " -b --bpw bits per word\n"
  178. " -L --lsb least significant bit first\n"
  179. " -C --cs-high chip select active high\n"
  180. " -N --no-cs no chip select\n"
  181. " -R --ready slave pulls low to pause\n"
  182. " -M --mosi-idle-low leave mosi line low when idle\n"
  183. "misc:\n"
  184. " -v --verbose Verbose (show tx buffer)\n");
  185. exit(1);
  186. }
  187. static void parse_opts(int argc, char *argv[])
  188. {
  189. while (1) {
  190. static const struct option lopts[] = {
  191. { "device", 1, 0, 'D' },
  192. { "speed", 1, 0, 's' },
  193. { "delay", 1, 0, 'd' },
  194. { "loop", 0, 0, 'l' },
  195. { "cpha", 0, 0, 'H' },
  196. { "cpol", 0, 0, 'O' },
  197. { "rx-cpha-flip", 0, 0, 'F' },
  198. { "dual", 0, 0, '2' },
  199. { "quad", 0, 0, '4' },
  200. { "octal", 0, 0, '8' },
  201. { "3wire", 0, 0, '3' },
  202. { "3wire-hiz", 0, 0, 'Z' },
  203. { "input", 1, 0, 'i' },
  204. { "output", 1, 0, 'o' },
  205. { "size", 1, 0, 'S' },
  206. { "iter", 1, 0, 'I' },
  207. { "bpw", 1, 0, 'b' },
  208. { "lsb", 0, 0, 'L' },
  209. { "cs-high", 0, 0, 'C' },
  210. { "no-cs", 0, 0, 'N' },
  211. { "ready", 0, 0, 'R' },
  212. { "mosi-idle-low", 0, 0, 'M' },
  213. { "verbose", 0, 0, 'v' },
  214. { NULL, 0, 0, 0 },
  215. };
  216. int c;
  217. c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3ZFMNR248p:vS:I:",
  218. lopts, NULL);
  219. if (c == -1)
  220. break;
  221. switch (c) {
  222. case 'D':
  223. device = optarg;
  224. break;
  225. case 's':
  226. speed = atoi(optarg);
  227. break;
  228. case 'd':
  229. delay = atoi(optarg);
  230. break;
  231. case 'b':
  232. bits = atoi(optarg);
  233. break;
  234. case 'i':
  235. input_file = optarg;
  236. break;
  237. case 'o':
  238. output_file = optarg;
  239. break;
  240. case 'l':
  241. mode |= SPI_LOOP;
  242. break;
  243. case 'H':
  244. mode |= SPI_CPHA;
  245. break;
  246. case 'O':
  247. mode |= SPI_CPOL;
  248. break;
  249. case 'L':
  250. mode |= SPI_LSB_FIRST;
  251. break;
  252. case 'C':
  253. mode |= SPI_CS_HIGH;
  254. break;
  255. case '3':
  256. mode |= SPI_3WIRE;
  257. break;
  258. case 'Z':
  259. mode |= SPI_3WIRE_HIZ;
  260. break;
  261. case 'F':
  262. mode |= SPI_RX_CPHA_FLIP;
  263. break;
  264. case 'M':
  265. mode |= SPI_MOSI_IDLE_LOW;
  266. break;
  267. case 'N':
  268. mode |= SPI_NO_CS;
  269. break;
  270. case 'v':
  271. verbose = 1;
  272. break;
  273. case 'R':
  274. mode |= SPI_READY;
  275. break;
  276. case 'p':
  277. input_tx = optarg;
  278. break;
  279. case '2':
  280. mode |= SPI_TX_DUAL;
  281. break;
  282. case '4':
  283. mode |= SPI_TX_QUAD;
  284. break;
  285. case '8':
  286. mode |= SPI_TX_OCTAL;
  287. break;
  288. case 'S':
  289. transfer_size = atoi(optarg);
  290. break;
  291. case 'I':
  292. iterations = atoi(optarg);
  293. break;
  294. default:
  295. print_usage(argv[0]);
  296. }
  297. }
  298. if (mode & SPI_LOOP) {
  299. if (mode & SPI_TX_DUAL)
  300. mode |= SPI_RX_DUAL;
  301. if (mode & SPI_TX_QUAD)
  302. mode |= SPI_RX_QUAD;
  303. if (mode & SPI_TX_OCTAL)
  304. mode |= SPI_RX_OCTAL;
  305. }
  306. }
  307. static void transfer_escaped_string(int fd, char *str)
  308. {
  309. size_t size = strlen(str);
  310. uint8_t *tx;
  311. uint8_t *rx;
  312. tx = malloc(size);
  313. if (!tx)
  314. pabort("can't allocate tx buffer");
  315. rx = malloc(size);
  316. if (!rx)
  317. pabort("can't allocate rx buffer");
  318. size = unescape((char *)tx, str, size);
  319. transfer(fd, tx, rx, size);
  320. free(rx);
  321. free(tx);
  322. }
  323. static void transfer_file(int fd, char *filename)
  324. {
  325. ssize_t bytes;
  326. struct stat sb;
  327. int tx_fd;
  328. uint8_t *tx;
  329. uint8_t *rx;
  330. if (stat(filename, &sb) == -1)
  331. pabort("can't stat input file");
  332. tx_fd = open(filename, O_RDONLY);
  333. if (tx_fd < 0)
  334. pabort("can't open input file");
  335. tx = malloc(sb.st_size);
  336. if (!tx)
  337. pabort("can't allocate tx buffer");
  338. rx = malloc(sb.st_size);
  339. if (!rx)
  340. pabort("can't allocate rx buffer");
  341. bytes = read(tx_fd, tx, sb.st_size);
  342. if (bytes != sb.st_size)
  343. pabort("failed to read input file");
  344. transfer(fd, tx, rx, sb.st_size);
  345. free(rx);
  346. free(tx);
  347. close(tx_fd);
  348. }
  349. static uint64_t _read_count;
  350. static uint64_t _write_count;
  351. static void show_transfer_rate(void)
  352. {
  353. static uint64_t prev_read_count, prev_write_count;
  354. double rx_rate, tx_rate;
  355. rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0);
  356. tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0);
  357. printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate);
  358. prev_read_count = _read_count;
  359. prev_write_count = _write_count;
  360. }
  361. static void transfer_buf(int fd, int len)
  362. {
  363. uint8_t *tx;
  364. uint8_t *rx;
  365. int i;
  366. tx = malloc(len);
  367. if (!tx)
  368. pabort("can't allocate tx buffer");
  369. for (i = 0; i < len; i++)
  370. tx[i] = random();
  371. rx = malloc(len);
  372. if (!rx)
  373. pabort("can't allocate rx buffer");
  374. transfer(fd, tx, rx, len);
  375. _write_count += len;
  376. _read_count += len;
  377. if (mode & SPI_LOOP) {
  378. if (memcmp(tx, rx, len)) {
  379. fprintf(stderr, "transfer error !\n");
  380. hex_dump(tx, len, 32, "TX");
  381. hex_dump(rx, len, 32, "RX");
  382. exit(1);
  383. }
  384. }
  385. free(rx);
  386. free(tx);
  387. }
  388. int main(int argc, char *argv[])
  389. {
  390. int ret = 0;
  391. int fd;
  392. uint32_t request;
  393. parse_opts(argc, argv);
  394. if (input_tx && input_file)
  395. pabort("only one of -p and --input may be selected");
  396. fd = open(device, O_RDWR);
  397. if (fd < 0)
  398. pabort("can't open device");
  399. /*
  400. * spi mode
  401. */
  402. /* WR is make a request to assign 'mode' */
  403. request = mode;
  404. ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
  405. if (ret == -1)
  406. pabort("can't set spi mode");
  407. /* RD is read what mode the device actually is in */
  408. ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
  409. if (ret == -1)
  410. pabort("can't get spi mode");
  411. /* Drivers can reject some mode bits without returning an error.
  412. * Read the current value to identify what mode it is in, and if it
  413. * differs from the requested mode, warn the user.
  414. */
  415. if (request != mode)
  416. printf("WARNING device does not support requested mode 0x%x\n",
  417. request);
  418. /*
  419. * bits per word
  420. */
  421. ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  422. if (ret == -1)
  423. pabort("can't set bits per word");
  424. ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  425. if (ret == -1)
  426. pabort("can't get bits per word");
  427. /*
  428. * max speed hz
  429. */
  430. ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  431. if (ret == -1)
  432. pabort("can't set max speed hz");
  433. ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  434. if (ret == -1)
  435. pabort("can't get max speed hz");
  436. printf("spi mode: 0x%x\n", mode);
  437. printf("bits per word: %u\n", bits);
  438. printf("max speed: %u Hz (%u kHz)\n", speed, speed/1000);
  439. if (input_tx)
  440. transfer_escaped_string(fd, input_tx);
  441. else if (input_file)
  442. transfer_file(fd, input_file);
  443. else if (transfer_size) {
  444. struct timespec last_stat;
  445. clock_gettime(CLOCK_MONOTONIC, &last_stat);
  446. while (iterations-- > 0) {
  447. struct timespec current;
  448. transfer_buf(fd, transfer_size);
  449. clock_gettime(CLOCK_MONOTONIC, &current);
  450. if (current.tv_sec - last_stat.tv_sec > interval) {
  451. show_transfer_rate();
  452. last_stat = current;
  453. }
  454. }
  455. printf("total: tx %.1fKB, rx %.1fKB\n",
  456. _write_count/1024.0, _read_count/1024.0);
  457. } else
  458. transfer(fd, default_tx, default_rx, sizeof(default_tx));
  459. close(fd);
  460. return ret;
  461. }