spidev_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 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. void print_usage(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. void parse_opts(int argc, char *argv[])
  80. {
  81. while (1) {
  82. static 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. { NULL, 0, 0, 0 },
  94. };
  95. int c;
  96. c = getopt_long(argc, argv, "D:s:d:b:lHOLC3", lopts, NULL);
  97. if (c == -1)
  98. break;
  99. switch (c) {
  100. case 'D':
  101. device = optarg;
  102. break;
  103. case 's':
  104. speed = atoi(optarg);
  105. break;
  106. case 'd':
  107. delay = atoi(optarg);
  108. break;
  109. case 'b':
  110. bits = atoi(optarg);
  111. break;
  112. case 'l':
  113. mode |= SPI_LOOP;
  114. break;
  115. case 'H':
  116. mode |= SPI_CPHA;
  117. break;
  118. case 'O':
  119. mode |= SPI_CPOL;
  120. break;
  121. case 'L':
  122. mode |= SPI_LSB_FIRST;
  123. break;
  124. case 'C':
  125. mode |= SPI_CS_HIGH;
  126. break;
  127. case '3':
  128. mode |= SPI_3WIRE;
  129. break;
  130. default:
  131. print_usage(argv[0]);
  132. break;
  133. }
  134. }
  135. }
  136. int main(int argc, char *argv[])
  137. {
  138. int ret = 0;
  139. int fd;
  140. parse_opts(argc, argv);
  141. fd = open(device, O_RDWR);
  142. if (fd < 0)
  143. pabort("can't open device");
  144. /*
  145. * spi mode
  146. */
  147. ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
  148. if (ret == -1)
  149. pabort("can't set spi mode");
  150. ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
  151. if (ret == -1)
  152. pabort("can't get spi mode");
  153. /*
  154. * bits per word
  155. */
  156. ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  157. if (ret == -1)
  158. pabort("can't set bits per word");
  159. ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  160. if (ret == -1)
  161. pabort("can't get bits per word");
  162. /*
  163. * max speed hz
  164. */
  165. ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  166. if (ret == -1)
  167. pabort("can't set max speed hz");
  168. ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  169. if (ret == -1)
  170. pabort("can't get max speed hz");
  171. printf("spi mode: %d\n", mode);
  172. printf("bits per word: %d\n", bits);
  173. printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
  174. transfer(fd);
  175. close(fd);
  176. return ret;
  177. }