spidev_test.c 8.3 KB

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