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.

133 lines
3.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "spiPort.h"
  2. #define LOG( X ) m_pLog->Log( "spiPort", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "spiPort", this, _LOG_ERROR, X )
  4. spiPort::spiPort():transportBus(){
  5. m_ioFID = -1;
  6. m_bConnected = false;
  7. m_bInitComplete = false;
  8. m_sDevicePath = NULL;
  9. m_nLineSpeed = 2500000;
  10. };
  11. spiPort::spiPort( CSTR DevicePath, int Speed ): transportSerial() {
  12. m_ioFID = -1;
  13. m_bConnected = false;
  14. m_nLineSpeed = Speed;
  15. m_pDevicePath = (CSTR) malloc( strlen(DevicePath)+1);
  16. strcpy( m_pDevicePath, DevicePath );
  17. m_bInitComplete = HW_init( m_pDevicePath, m_nLineSpeed );
  18. if( m_bInitComplete ) {
  19. #ifdef DEBUG
  20. char Buffer[CLOG];
  21. sprintf( Buffer,"Init %s [%d] OK", m_pDevicePath, m_nLineSpeed );
  22. LOG( Buffer );
  23. #endif
  24. };
  25. };
  26. // Destructor implementation
  27. serialPort::~serialPort(){
  28. };
  29. bool spiPort::HW_init( CSRT Path, uint32_t Speed ){
  30. uint8_t mode, lsb, bits;
  31. uint32_t speed = 2500000;
  32. if( ( m_ioFID = open( Path, O_RDWR ) ) < 0 ){
  33. LOG_ERROR("Failed to open the bus.");
  34. /* ERROR HANDLING; you can check errno to see what went wrong */
  35. return false;
  36. };
  37. ///////////////
  38. // Verifications
  39. ///////////////
  40. //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;
  41. //multiple possibilities using |
  42. /*
  43. if (ioctl(file, SPI_IOC_WR_MODE, &mode)<0) {
  44. perror("can't set spi mode");
  45. return;
  46. };
  47. //*/
  48. if( ioctl( m_ioFID, SPI_IOC_RD_MODE, &mode) < 0 ){
  49. LOG_ERROR("SPI rd_mode");
  50. return false;
  51. };
  52. if( ioctl( m_ioFID, SPI_IOC_RD_LSB_FIRST, &lsb) < 0 ){
  53. LOG_ERROR("SPI rd_lsb_fist");
  54. return;
  55. };
  56. //* sunxi supports only 8 bits
  57. if( ioctl( m_ioFID, SPI_IOC_WR_BITS_PER_WORD, 8)<0 ){
  58. LOG_ERROR("can't set bits per word");
  59. return false;
  60. };
  61. //*/
  62. if( ioctl( m_ioFID, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0 ){
  63. LOG_ERROR("SPI bits_per_word");
  64. return false;
  65. };
  66. //*
  67. if( ioctl( m_ioFID, SPI_IOC_WR_MAX_SPEED_HZ, &Speed)<0 ){
  68. LOG_ERROR("can't set max speed hz");
  69. return false;
  70. };
  71. //*/
  72. if( ioctl( m_ioFID, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0 ){
  73. LOG_ERROR("SPI max_speed_hz");
  74. return false;
  75. };
  76. #ifdef DEBUG
  77. char Buffer[256];
  78. sprintf( Buffer, "%s: spi mode %d, %d bits %sper word, %d Hz max\n", Path, mode, bits, lsb ? "(lsb first) " : "", speed );
  79. LOG( Buffer );
  80. #endif
  81. xfer[0].tx_buf = m_pOutputBuffer;
  82. xfer[0].len = 3; // Length of command to write
  83. xfer[0].cs_change = 0; // Keep CS activated
  84. xfer[0].delay_usecs = 0; // Delay in us
  85. xfer[0].speed_hz = Speed; // Speed
  86. xfer[0].bits_per_word = 8; // Bites per word 8
  87. xfer[1].rx_buf = m_pInputBuffer;
  88. xfer[1].len = 4; // Length of Data to read
  89. xfer[1].cs_change = 0; // Keep CS activated
  90. xfer[1].delay_usecs = 0;
  91. xfer[1].speed_hz = Speed;
  92. xfer[1].bits_per_word = 8;
  93. return true;
  94. };
  95. char * spi_read(int add1,int add2,int nbytes ){
  96. int status;
  97. memset(buf, 0, sizeof buf);
  98. memset(buf2, 0, sizeof buf2);
  99. buf[0] = 0x01;
  100. buf[1] = add1;
  101. buf[2] = add2;
  102. xfer[0].tx_buf = (unsigned long)buf;
  103. xfer[0].len = 3; /* Length of command to write*/
  104. xfer[1].rx_buf = (unsigned long) buf2;
  105. xfer[1].len = nbytes; /* Length of Data to read */
  106. status = ioctl(file, SPI_IOC_MESSAGE(2), xfer);
  107. if (status < 0){
  108. perror("SPI_IOC_MESSAGE");
  109. return;
  110. };
  111. printf("env: %02x %02x %02x\n", buf[0], buf[1], buf[2]);
  112. printf("ret: %02x %02x %02x %02x\n", buf2[0], buf2[1], buf2[2], buf2[3]);
  113. com_serial=1;
  114. failcount=0;
  115. return buf2;
  116. };
  117. #undef LOG
  118. #undef LOG_ERROR