spidev_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <getopt.h>
  18. #include <fcntl.h>
  19. #include <sys/ioctl.h>
  20. #include <linux/types.h>
  21. #include <linux/spi/spidev.h>
  22. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  23. static void pabort(const char *s)
  24. {
  25. perror(s);
  26. abort();
  27. }
  28. static const char *device = "/dev/spidev1.1";
  29. static uint32_t mode;
  30. static uint8_t bits = 8;
  31. static uint32_t speed = 500000;
  32. static uint16_t delay;
  33. static void transfer(int fd)
  34. {
  35. int ret;
  36. uint8_t tx[] = {
  37. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  38. 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
  39. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  40. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  41. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  42. 0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
  43. 0xF0, 0x0D,
  44. };
  45. uint8_t rx[ARRAY_SIZE(tx)] = {0, };
  46. struct spi_ioc_transfer tr = {
  47. .tx_buf = (unsigned long)tx,
  48. .rx_buf = (unsigned long)rx,
  49. .len = ARRAY_SIZE(tx),
  50. .delay_usecs = delay,
  51. .speed_hz = speed,
  52. .bits_per_word = bits,
  53. };
  54. if (mode & SPI_TX_QUAD)
  55. tr.tx_nbits = 4;
  56. else if (mode & SPI_TX_DUAL)
  57. tr.tx_nbits = 2;
  58. if (mode & SPI_RX_QUAD)
  59. tr.rx_nbits = 4;
  60. else if (mode & SPI_RX_DUAL)
  61. tr.rx_nbits = 2;
  62. if (!(mode & SPI_LOOP)) {
  63. if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
  64. tr.rx_buf = 0;
  65. else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
  66. tr.tx_buf = 0;
  67. }
  68. ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  69. if (ret < 1)
  70. pabort("can't send spi message");
  71. for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
  72. if (!(ret % 6))
  73. puts("");
  74. printf("%.2X ", rx[ret]);
  75. }
  76. puts("");
  77. }
  78. static void print_usage(const char *prog)
  79. {
  80. printf("Usage: %s [-DsbdlHOLC3]\n", prog);
  81. puts(" -D --device device to use (default /dev/spidev1.1)\n"
  82. " -s --speed max speed (Hz)\n"
  83. " -d --delay delay (usec)\n"
  84. " -b --bpw bits per word \n"
  85. " -l --loop loopback\n"
  86. " -H --cpha clock phase\n"
  87. " -O --cpol clock polarity\n"
  88. " -L --lsb least significant bit first\n"
  89. " -C --cs-high chip select active high\n"
  90. " -3 --3wire SI/SO signals shared\n"
  91. " -N --no-cs no chip select\n"
  92. " -R --ready slave pulls low to pause\n"
  93. " -2 --dual dual transfer\n"
  94. " -4 --quad quad transfer\n");
  95. exit(1);
  96. }
  97. static void parse_opts(int argc, char *argv[])
  98. {
  99. while (1) {
  100. static const struct option lopts[] = {
  101. { "device", 1, 0, 'D' },
  102. { "speed", 1, 0, 's' },
  103. { "delay", 1, 0, 'd' },
  104. { "bpw", 1, 0, 'b' },
  105. { "loop", 0, 0, 'l' },
  106. { "cpha", 0, 0, 'H' },
  107. { "cpol", 0, 0, 'O' },
  108. { "lsb", 0, 0, 'L' },
  109. { "cs-high", 0, 0, 'C' },
  110. { "3wire", 0, 0, '3' },
  111. { "no-cs", 0, 0, 'N' },
  112. { "ready", 0, 0, 'R' },
  113. { "dual", 0, 0, '2' },
  114. { "quad", 0, 0, '4' },
  115. { NULL, 0, 0, 0 },
  116. };
  117. int c;
  118. c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24", lopts, NULL);
  119. if (c == -1)
  120. break;
  121. switch (c) {
  122. case 'D':
  123. device = optarg;
  124. break;
  125. case 's':
  126. speed = atoi(optarg);
  127. break;
  128. case 'd':
  129. delay = atoi(optarg);
  130. break;
  131. case 'b':
  132. bits = atoi(optarg);
  133. break;
  134. case 'l':
  135. mode |= SPI_LOOP;
  136. break;
  137. case 'H':
  138. mode |= SPI_CPHA;
  139. break;
  140. case 'O':
  141. mode |= SPI_CPOL;
  142. break;
  143. case 'L':
  144. mode |= SPI_LSB_FIRST;
  145. break;
  146. case 'C':
  147. mode |= SPI_CS_HIGH;
  148. break;
  149. case '3':
  150. mode |= SPI_3WIRE;
  151. break;
  152. case 'N':
  153. mode |= SPI_NO_CS;
  154. break;
  155. case 'R':
  156. mode |= SPI_READY;
  157. break;
  158. case '2':
  159. mode |= SPI_TX_DUAL;
  160. break;
  161. case '4':
  162. mode |= SPI_TX_QUAD;
  163. break;
  164. default:
  165. print_usage(argv[0]);
  166. break;
  167. }
  168. }
  169. if (mode & SPI_LOOP) {
  170. if (mode & SPI_TX_DUAL)
  171. mode |= SPI_RX_DUAL;
  172. if (mode & SPI_TX_QUAD)
  173. mode |= SPI_RX_QUAD;
  174. }
  175. }
  176. int main(int argc, char *argv[])
  177. {
  178. int ret = 0;
  179. int fd;
  180. parse_opts(argc, argv);
  181. fd = open(device, O_RDWR);
  182. if (fd < 0)
  183. pabort("can't open device");
  184. /*
  185. * spi mode
  186. */
  187. ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
  188. if (ret == -1)
  189. pabort("can't set spi mode");
  190. ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
  191. if (ret == -1)
  192. pabort("can't get spi mode");
  193. /*
  194. * bits per word
  195. */
  196. ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  197. if (ret == -1)
  198. pabort("can't set bits per word");
  199. ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  200. if (ret == -1)
  201. pabort("can't get bits per word");
  202. /*
  203. * max speed hz
  204. */
  205. ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  206. if (ret == -1)
  207. pabort("can't set max speed hz");
  208. ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  209. if (ret == -1)
  210. pabort("can't get max speed hz");
  211. printf("spi mode: 0x%x\n", mode);
  212. printf("bits per word: %d\n", bits);
  213. printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
  214. transfer(fd);
  215. close(fd);
  216. return ret;
  217. }