spidev_test.c 6.6 KB

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