spidev_test.c 10 KB

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