spidev_test.c 12 KB

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