spidev_test.c 8.3 KB

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