You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. spidevlib.c - A user-space program to comunicate using spidev.
  3. Gustavo Zamboni
  4. */
  5. #include <stdint.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <getopt.h>
  10. #include <fcntl.h>
  11. #include <sys/ioctl.h>
  12. #include <linux/types.h>
  13. #include <linux/spi/spidev.h>
  14. char buf[10];
  15. char buf2[10];
  16. int com_serial;
  17. int failcount;
  18. struct spi_ioc_transfer xfer[2];
  19. //////////
  20. // Init SPIdev
  21. //////////
  22. int spi_init( char filename[40] ){
  23. int file;
  24. uint8_t mode, lsb, bits;
  25. uint32_t speed = 2500000;
  26. if( ( file = open(filename,O_RDWR) ) < 0 ){
  27. printf("Failed to open the bus.");
  28. /* ERROR HANDLING; you can check errno to see what went wrong */
  29. com_serial=0;
  30. exit(1);
  31. };
  32. ///////////////
  33. // Verifications
  34. ///////////////
  35. //possible modes: mode |= SPI_LOOP; mode |= SPI_CPHA; mode |= SPI_CPOL; mode |= SPI_LSB_FIRST; mode |= SPI_CS_HIGH; mode |= SPI_3WIRE; mode |= SPI_NO_CS; mode |= SPI_READY;
  36. //multiple possibilities using |
  37. //*
  38. if (ioctl(file, SPI_IOC_WR_MODE, &mode)<0) {
  39. perror("can't set spi mode");
  40. return;
  41. };
  42. //*/
  43. if( ioctl(file, SPI_IOC_RD_MODE, &mode) < 0 ){
  44. perror("SPI rd_mode");
  45. return;
  46. };
  47. if( ioctl(file, SPI_IOC_RD_LSB_FIRST, &lsb) < 0 ){
  48. perror("SPI rd_lsb_fist");
  49. return;
  50. };
  51. /* sunxi supports only 8 bits
  52. if( ioctl(file, SPI_IOC_WR_BITS_PER_WORD, 8)<0 ){
  53. perror("can't set bits per word");
  54. return;
  55. };
  56. */
  57. if( ioctl(file, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0 ){
  58. perror("SPI bits_per_word");
  59. return;
  60. };
  61. //*
  62. if( ioctl(file, SPI_IOC_WR_MAX_SPEED_HZ, &speed)<0 ){
  63. perror("can't set max speed hz");
  64. return;
  65. };
  66. //*/
  67. if( ioctl(file, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0 ){
  68. perror("SPI max_speed_hz");
  69. return;
  70. };
  71. printf("%s: spi mode %d, %d bits %sper word, %d Hz max\n",filename, mode, bits, lsb ? "(lsb first) " : "", speed);
  72. //xfer[0].tx_buf = (unsigned long)buf;
  73. xfer[0].len = 3; // Length of command to write
  74. xfer[0].cs_change = 0; // Keep CS activated
  75. xfer[0].delay_usecs = 0; // Delay in us
  76. xfer[0].speed_hz = 2500000; // Speed
  77. xfer[0].bits_per_word = 8; // Bites per word 8
  78. //xfer[1].rx_buf = (unsigned long) buf2;
  79. xfer[1].len = 4; // Length of Data to read
  80. xfer[1].cs_change = 0; // Keep CS activated
  81. xfer[0].delay_usecs = 0;
  82. xfer[0].speed_hz = 2500000;
  83. xfer[0].bits_per_word = 8;
  84. return file;
  85. }
  86. //////////
  87. // Read n bytes from the 2 bytes add1 add2 address
  88. //////////
  89. char * spi_read(int add1,int add2,int nbytes,int file){
  90. int status;
  91. memset(buf, 0, sizeof buf);
  92. memset(buf2, 0, sizeof buf2);
  93. buf[0] = 0x01;
  94. buf[1] = add1;
  95. buf[2] = add2;
  96. xfer[0].tx_buf = (unsigned long)buf;
  97. xfer[0].len = 3; /* Length of command to write*/
  98. xfer[1].rx_buf = (unsigned long) buf2;
  99. xfer[1].len = nbytes; /* Length of Data to read */
  100. status = ioctl(file, SPI_IOC_MESSAGE(2), xfer);
  101. if (status < 0){
  102. perror("SPI_IOC_MESSAGE");
  103. return;
  104. };
  105. printf("env: %02x %02x %02x\n", buf[0], buf[1], buf[2]);
  106. printf("ret: %02x %02x %02x %02x\n", buf2[0], buf2[1], buf2[2], buf2[3]);
  107. com_serial=1;
  108. failcount=0;
  109. return buf2;
  110. };
  111. //////////
  112. // Write n bytes int the 2 bytes address add1 add2
  113. //////////
  114. void spi_write(int add1,int add2,int nbytes,char value[10],int file){
  115. unsigned char buf[32], buf2[32];
  116. int status;
  117. memset(buf, 0, sizeof buf);
  118. memset(buf2, 0, sizeof buf2);
  119. buf[0] = 0x00;
  120. buf[1] = add1;
  121. buf[2] = add2;
  122. if (nbytes>=1) buf[3] = value[0];
  123. if (nbytes>=2) buf[4] = value[1];
  124. if (nbytes>=3) buf[5] = value[2];
  125. if (nbytes>=4) buf[6] = value[3];
  126. xfer[0].tx_buf = (unsigned long)buf;
  127. xfer[0].len = nbytes+3; /* Length of command to write*/
  128. status = ioctl(file, SPI_IOC_MESSAGE(1), xfer);
  129. if (status < 0){
  130. perror("SPI_IOC_MESSAGE");
  131. return;
  132. };
  133. printf("env: %02x %02x %02x\n", buf[0], buf[1], buf[2]);
  134. printf("ret: %02x %02x %02x %02x\n", buf2[0], buf2[1], buf2[2], buf2[3]);
  135. com_serial=1;
  136. failcount=0;
  137. };
  138. /* Usage example:
  139. char *buffer;
  140. char buf[10];
  141. file=spi_init("/dev/spidev0.0"); //dev
  142. buf[0] = 0x41;
  143. buf[1] = 0xFF;
  144. spi_write(0xE6,0x0E,2,buf,file); //this will write value 0x41FF to the address 0xE60E
  145. buffer=(char *)spi_read(0xE6,0x0E,4,file); //reading the address 0xE60E
  146. close(file);
  147. */
  148. int main( void ){
  149. int fid;
  150. char *buffer;
  151. fid = spi_init("/dev/spidev0.0"); //dev
  152. buf[0] = 0x41;
  153. buf[1] = 0xFF;
  154. //spi_write(0xE6,0x0E,2,buf,fid); //this will write value 0x41FF to the address 0xE60E
  155. buffer = (char *)spi_read(0x00,0x01,4,fid); //reading the address 0xE60E
  156. close(fid);
  157. return 0;
  158. };