spidev_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 uint8_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. ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  55. if (ret < 1)
  56. pabort("can't send spi message");
  57. for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
  58. if (!(ret % 6))
  59. puts("");
  60. printf("%.2X ", rx[ret]);
  61. }
  62. puts("");
  63. }
  64. static void print_usage(const char *prog)
  65. {
  66. printf("Usage: %s [-DsbdlHOLC3]\n", prog);
  67. puts(" -D --device device to use (default /dev/spidev1.1)\n"
  68. " -s --speed max speed (Hz)\n"
  69. " -d --delay delay (usec)\n"
  70. " -b --bpw bits per word \n"
  71. " -l --loop loopback\n"
  72. " -H --cpha clock phase\n"
  73. " -O --cpol clock polarity\n"
  74. " -L --lsb least significant bit first\n"
  75. " -C --cs-high chip select active high\n"
  76. " -3 --3wire SI/SO signals shared\n");
  77. exit(1);
  78. }
  79. static void parse_opts(int argc, char *argv[])
  80. {
  81. while (1) {
  82. static const struct option lopts[] = {
  83. { "device", 1, 0, 'D' },
  84. { "speed", 1, 0, 's' },
  85. { "delay", 1, 0, 'd' },
  86. { "bpw", 1, 0, 'b' },
  87. { "loop", 0, 0, 'l' },
  88. { "cpha", 0, 0, 'H' },
  89. { "cpol", 0, 0, 'O' },
  90. { "lsb", 0, 0, 'L' },
  91. { "cs-high", 0, 0, 'C' },
  92. { "3wire", 0, 0, '3' },
  93. { "no-cs", 0, 0, 'N' },
  94. { "ready", 0, 0, 'R' },
  95. { NULL, 0, 0, 0 },
  96. };
  97. int c;
  98. c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);
  99. if (c == -1)
  100. break;
  101. switch (c) {
  102. case 'D':
  103. device = optarg;
  104. break;
  105. case 's':
  106. speed = atoi(optarg);
  107. break;
  108. case 'd':
  109. delay = atoi(optarg);
  110. break;
  111. case 'b':
  112. bits = atoi(optarg);
  113. break;
  114. case 'l':
  115. mode |= SPI_LOOP;
  116. break;
  117. case 'H':
  118. mode |= SPI_CPHA;
  119. break;
  120. case 'O':
  121. mode |= SPI_CPOL;
  122. break;
  123. case 'L':
  124. mode |= SPI_LSB_FIRST;
  125. break;
  126. case 'C':
  127. mode |= SPI_CS_HIGH;
  128. break;
  129. case '3':
  130. mode |= SPI_3WIRE;
  131. break;
  132. case 'N':
  133. mode |= SPI_NO_CS;
  134. break;
  135. case 'R':
  136. mode |= SPI_READY;
  137. break;
  138. default:
  139. print_usage(argv[0]);
  140. break;
  141. }
  142. }
  143. }
  144. int main(int argc, char *argv[])
  145. {
  146. int ret = 0;
  147. int fd;
  148. parse_opts(argc, argv);
  149. fd = open(device, O_RDWR);
  150. if (fd < 0)
  151. pabort("can't open device");
  152. /*
  153. * spi mode
  154. */
  155. ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
  156. if (ret == -1)
  157. pabort("can't set spi mode");
  158. ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
  159. if (ret == -1)
  160. pabort("can't get spi mode");
  161. /*
  162. * bits per word
  163. */
  164. ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  165. if (ret == -1)
  166. pabort("can't set bits per word");
  167. ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  168. if (ret == -1)
  169. pabort("can't get bits per word");
  170. /*
  171. * max speed hz
  172. */
  173. ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  174. if (ret == -1)
  175. pabort("can't set max speed hz");
  176. ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  177. if (ret == -1)
  178. pabort("can't get max speed hz");
  179. printf("spi mode: %d\n", mode);
  180. printf("bits per word: %d\n", bits);
  181. printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
  182. transfer(fd);
  183. close(fd);
  184. return ret;
  185. }