spidev_test.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 [-DsbdlHOLC3vpNR24SI]\n", prog);
  155. puts(" -D --device device to use (default /dev/spidev1.1)\n"
  156. " -s --speed max speed (Hz)\n"
  157. " -d --delay delay (usec)\n"
  158. " -b --bpw bits per word\n"
  159. " -i --input input data from a file (e.g. \"test.bin\")\n"
  160. " -o --output output data to a file (e.g. \"results.bin\")\n"
  161. " -l --loop loopback\n"
  162. " -H --cpha clock phase\n"
  163. " -O --cpol clock polarity\n"
  164. " -L --lsb least significant bit first\n"
  165. " -C --cs-high chip select active high\n"
  166. " -3 --3wire SI/SO signals shared\n"
  167. " -v --verbose Verbose (show tx buffer)\n"
  168. " -p Send data (e.g. \"1234\\xde\\xad\")\n"
  169. " -N --no-cs no chip select\n"
  170. " -R --ready slave pulls low to pause\n"
  171. " -2 --dual dual transfer\n"
  172. " -4 --quad quad transfer\n"
  173. " -8 --octal octal transfer\n"
  174. " -S --size transfer size\n"
  175. " -I --iter iterations\n");
  176. exit(1);
  177. }
  178. static void parse_opts(int argc, char *argv[])
  179. {
  180. while (1) {
  181. static const struct option lopts[] = {
  182. { "device", 1, 0, 'D' },
  183. { "speed", 1, 0, 's' },
  184. { "delay", 1, 0, 'd' },
  185. { "bpw", 1, 0, 'b' },
  186. { "input", 1, 0, 'i' },
  187. { "output", 1, 0, 'o' },
  188. { "loop", 0, 0, 'l' },
  189. { "cpha", 0, 0, 'H' },
  190. { "cpol", 0, 0, 'O' },
  191. { "lsb", 0, 0, 'L' },
  192. { "cs-high", 0, 0, 'C' },
  193. { "3wire", 0, 0, '3' },
  194. { "no-cs", 0, 0, 'N' },
  195. { "ready", 0, 0, 'R' },
  196. { "dual", 0, 0, '2' },
  197. { "verbose", 0, 0, 'v' },
  198. { "quad", 0, 0, '4' },
  199. { "octal", 0, 0, '8' },
  200. { "size", 1, 0, 'S' },
  201. { "iter", 1, 0, 'I' },
  202. { NULL, 0, 0, 0 },
  203. };
  204. int c;
  205. c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR248p:vS:I:",
  206. lopts, NULL);
  207. if (c == -1)
  208. break;
  209. switch (c) {
  210. case 'D':
  211. device = optarg;
  212. break;
  213. case 's':
  214. speed = atoi(optarg);
  215. break;
  216. case 'd':
  217. delay = atoi(optarg);
  218. break;
  219. case 'b':
  220. bits = atoi(optarg);
  221. break;
  222. case 'i':
  223. input_file = optarg;
  224. break;
  225. case 'o':
  226. output_file = optarg;
  227. break;
  228. case 'l':
  229. mode |= SPI_LOOP;
  230. break;
  231. case 'H':
  232. mode |= SPI_CPHA;
  233. break;
  234. case 'O':
  235. mode |= SPI_CPOL;
  236. break;
  237. case 'L':
  238. mode |= SPI_LSB_FIRST;
  239. break;
  240. case 'C':
  241. mode |= SPI_CS_HIGH;
  242. break;
  243. case '3':
  244. mode |= SPI_3WIRE;
  245. break;
  246. case 'N':
  247. mode |= SPI_NO_CS;
  248. break;
  249. case 'v':
  250. verbose = 1;
  251. break;
  252. case 'R':
  253. mode |= SPI_READY;
  254. break;
  255. case 'p':
  256. input_tx = optarg;
  257. break;
  258. case '2':
  259. mode |= SPI_TX_DUAL;
  260. break;
  261. case '4':
  262. mode |= SPI_TX_QUAD;
  263. break;
  264. case '8':
  265. mode |= SPI_TX_OCTAL;
  266. break;
  267. case 'S':
  268. transfer_size = atoi(optarg);
  269. break;
  270. case 'I':
  271. iterations = atoi(optarg);
  272. break;
  273. default:
  274. print_usage(argv[0]);
  275. }
  276. }
  277. if (mode & SPI_LOOP) {
  278. if (mode & SPI_TX_DUAL)
  279. mode |= SPI_RX_DUAL;
  280. if (mode & SPI_TX_QUAD)
  281. mode |= SPI_RX_QUAD;
  282. if (mode & SPI_TX_OCTAL)
  283. mode |= SPI_RX_OCTAL;
  284. }
  285. }
  286. static void transfer_escaped_string(int fd, char *str)
  287. {
  288. size_t size = strlen(str);
  289. uint8_t *tx;
  290. uint8_t *rx;
  291. tx = malloc(size);
  292. if (!tx)
  293. pabort("can't allocate tx buffer");
  294. rx = malloc(size);
  295. if (!rx)
  296. pabort("can't allocate rx buffer");
  297. size = unescape((char *)tx, str, size);
  298. transfer(fd, tx, rx, size);
  299. free(rx);
  300. free(tx);
  301. }
  302. static void transfer_file(int fd, char *filename)
  303. {
  304. ssize_t bytes;
  305. struct stat sb;
  306. int tx_fd;
  307. uint8_t *tx;
  308. uint8_t *rx;
  309. if (stat(filename, &sb) == -1)
  310. pabort("can't stat input file");
  311. tx_fd = open(filename, O_RDONLY);
  312. if (tx_fd < 0)
  313. pabort("can't open input file");
  314. tx = malloc(sb.st_size);
  315. if (!tx)
  316. pabort("can't allocate tx buffer");
  317. rx = malloc(sb.st_size);
  318. if (!rx)
  319. pabort("can't allocate rx buffer");
  320. bytes = read(tx_fd, tx, sb.st_size);
  321. if (bytes != sb.st_size)
  322. pabort("failed to read input file");
  323. transfer(fd, tx, rx, sb.st_size);
  324. free(rx);
  325. free(tx);
  326. close(tx_fd);
  327. }
  328. static uint64_t _read_count;
  329. static uint64_t _write_count;
  330. static void show_transfer_rate(void)
  331. {
  332. static uint64_t prev_read_count, prev_write_count;
  333. double rx_rate, tx_rate;
  334. rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0);
  335. tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0);
  336. printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate);
  337. prev_read_count = _read_count;
  338. prev_write_count = _write_count;
  339. }
  340. static void transfer_buf(int fd, int len)
  341. {
  342. uint8_t *tx;
  343. uint8_t *rx;
  344. int i;
  345. tx = malloc(len);
  346. if (!tx)
  347. pabort("can't allocate tx buffer");
  348. for (i = 0; i < len; i++)
  349. tx[i] = random();
  350. rx = malloc(len);
  351. if (!rx)
  352. pabort("can't allocate rx buffer");
  353. transfer(fd, tx, rx, len);
  354. _write_count += len;
  355. _read_count += len;
  356. if (mode & SPI_LOOP) {
  357. if (memcmp(tx, rx, len)) {
  358. fprintf(stderr, "transfer error !\n");
  359. hex_dump(tx, len, 32, "TX");
  360. hex_dump(rx, len, 32, "RX");
  361. exit(1);
  362. }
  363. }
  364. free(rx);
  365. free(tx);
  366. }
  367. int main(int argc, char *argv[])
  368. {
  369. int ret = 0;
  370. int fd;
  371. parse_opts(argc, argv);
  372. if (input_tx && input_file)
  373. pabort("only one of -p and --input may be selected");
  374. fd = open(device, O_RDWR);
  375. if (fd < 0)
  376. pabort("can't open device");
  377. /*
  378. * spi mode
  379. */
  380. ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
  381. if (ret == -1)
  382. pabort("can't set spi mode");
  383. ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
  384. if (ret == -1)
  385. pabort("can't get spi mode");
  386. /*
  387. * bits per word
  388. */
  389. ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  390. if (ret == -1)
  391. pabort("can't set bits per word");
  392. ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  393. if (ret == -1)
  394. pabort("can't get bits per word");
  395. /*
  396. * max speed hz
  397. */
  398. ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  399. if (ret == -1)
  400. pabort("can't set max speed hz");
  401. ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  402. if (ret == -1)
  403. pabort("can't get max speed hz");
  404. printf("spi mode: 0x%x\n", mode);
  405. printf("bits per word: %u\n", bits);
  406. printf("max speed: %u Hz (%u kHz)\n", speed, speed/1000);
  407. if (input_tx)
  408. transfer_escaped_string(fd, input_tx);
  409. else if (input_file)
  410. transfer_file(fd, input_file);
  411. else if (transfer_size) {
  412. struct timespec last_stat;
  413. clock_gettime(CLOCK_MONOTONIC, &last_stat);
  414. while (iterations-- > 0) {
  415. struct timespec current;
  416. transfer_buf(fd, transfer_size);
  417. clock_gettime(CLOCK_MONOTONIC, &current);
  418. if (current.tv_sec - last_stat.tv_sec > interval) {
  419. show_transfer_rate();
  420. last_stat = current;
  421. }
  422. }
  423. printf("total: tx %.1fKB, rx %.1fKB\n",
  424. _write_count/1024.0, _read_count/1024.0);
  425. } else
  426. transfer(fd, default_tx, default_rx, sizeof(default_tx));
  427. close(fd);
  428. return ret;
  429. }