spidev_fdx.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <linux/types.h>
  10. #include <linux/spi/spidev.h>
  11. static int verbose;
  12. static void do_read(int fd, int len)
  13. {
  14. unsigned char buf[32], *bp;
  15. int status;
  16. /* read at least 2 bytes, no more than 32 */
  17. if (len < 2)
  18. len = 2;
  19. else if (len > sizeof(buf))
  20. len = sizeof(buf);
  21. memset(buf, 0, sizeof buf);
  22. status = read(fd, buf, len);
  23. if (status < 0) {
  24. perror("read");
  25. return;
  26. }
  27. if (status != len) {
  28. fprintf(stderr, "short read\n");
  29. return;
  30. }
  31. printf("read(%2d, %2d): %02x %02x,", len, status,
  32. buf[0], buf[1]);
  33. status -= 2;
  34. bp = buf + 2;
  35. while (status-- > 0)
  36. printf(" %02x", *bp++);
  37. printf("\n");
  38. }
  39. static void do_msg(int fd, int len)
  40. {
  41. struct spi_ioc_transfer xfer[2];
  42. unsigned char buf[32], *bp;
  43. int status;
  44. memset(xfer, 0, sizeof xfer);
  45. memset(buf, 0, sizeof buf);
  46. if (len > sizeof buf)
  47. len = sizeof buf;
  48. buf[0] = 0xaa;
  49. xfer[0].tx_buf = (unsigned long)buf;
  50. xfer[0].len = 1;
  51. xfer[1].rx_buf = (unsigned long) buf;
  52. xfer[1].len = len;
  53. status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);
  54. if (status < 0) {
  55. perror("SPI_IOC_MESSAGE");
  56. return;
  57. }
  58. printf("response(%2d, %2d): ", len, status);
  59. for (bp = buf; len; len--)
  60. printf(" %02x", *bp++);
  61. printf("\n");
  62. }
  63. static void dumpstat(const char *name, int fd)
  64. {
  65. __u8 lsb, bits;
  66. __u32 mode, speed;
  67. if (ioctl(fd, SPI_IOC_RD_MODE32, &mode) < 0) {
  68. perror("SPI rd_mode");
  69. return;
  70. }
  71. if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0) {
  72. perror("SPI rd_lsb_fist");
  73. return;
  74. }
  75. if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) {
  76. perror("SPI bits_per_word");
  77. return;
  78. }
  79. if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) {
  80. perror("SPI max_speed_hz");
  81. return;
  82. }
  83. printf("%s: spi mode 0x%x, %d bits %sper word, %d Hz max\n",
  84. name, mode, bits, lsb ? "(lsb first) " : "", speed);
  85. }
  86. int main(int argc, char **argv)
  87. {
  88. int c;
  89. int readcount = 0;
  90. int msglen = 0;
  91. int fd;
  92. const char *name;
  93. while ((c = getopt(argc, argv, "hm:r:v")) != EOF) {
  94. switch (c) {
  95. case 'm':
  96. msglen = atoi(optarg);
  97. if (msglen < 0)
  98. goto usage;
  99. continue;
  100. case 'r':
  101. readcount = atoi(optarg);
  102. if (readcount < 0)
  103. goto usage;
  104. continue;
  105. case 'v':
  106. verbose++;
  107. continue;
  108. case 'h':
  109. case '?':
  110. usage:
  111. fprintf(stderr,
  112. "usage: %s [-h] [-m N] [-r N] /dev/spidevB.D\n",
  113. argv[0]);
  114. return 1;
  115. }
  116. }
  117. if ((optind + 1) != argc)
  118. goto usage;
  119. name = argv[optind];
  120. fd = open(name, O_RDWR);
  121. if (fd < 0) {
  122. perror("open");
  123. return 1;
  124. }
  125. dumpstat(name, fd);
  126. if (msglen)
  127. do_msg(fd, msglen);
  128. if (readcount)
  129. do_read(fd, readcount);
  130. close(fd);
  131. return 0;
  132. }