deca_spi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*! ----------------------------------------------------------------------------
  2. * @file deca_spi.c
  3. * @brief SPI access functions
  4. *
  5. * @attention
  6. *
  7. * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland.
  8. *
  9. * All rights reserved.
  10. *
  11. * @author DecaWave
  12. */
  13. #include "deca_spi.h"
  14. #include "deca_device_api.h"
  15. #include "port.h"
  16. #define DECAMAXDATALEN 1036
  17. volatile uint8_t decaTxBuf[DECAMAXDATALEN] = {0};
  18. int32_t writetospi_serial( uint16_t headerLength,
  19. const uint8_t *headerBuffer,
  20. uint32_t bodylength,
  21. const uint8_t *bodyBuffer
  22. );
  23. int32_t readfromspi_serial( uint16_t headerLength,
  24. const uint8_t *headerBuffer,
  25. uint32_t readlength,
  26. uint8_t *readBuffer );
  27. /*! ------------------------------------------------------------------------------------------------------------------
  28. * Function: openspi()
  29. *
  30. * Low level abstract function to open and initialise access to the SPI device.
  31. * returns 0 for success, or -1 for error
  32. */
  33. int openspi(/*SPI_TypeDef* SPIx*/)
  34. {
  35. // done by port.c, default SPI used is SPI1
  36. return 0;
  37. } // end openspi()
  38. /*! ------------------------------------------------------------------------------------------------------------------
  39. * Function: closespi()
  40. *
  41. * Low level abstract function to close the the SPI device.
  42. * returns 0 for success, or -1 for error
  43. */
  44. int closespi(void)
  45. {
  46. while (port_SPIx_busy_sending()); //wait for tx buffer to empty
  47. port_SPIx_disable();
  48. return 0;
  49. } // end closespi()
  50. /*! ------------------------------------------------------------------------------------------------------------------
  51. * Function: writetospi()
  52. *
  53. * Low level abstract function to write to the SPI
  54. * Takes two separate byte buffers for write header and write data
  55. * returns 0 for success, or -1 for error
  56. */
  57. #ifdef __GNUC__
  58. #pragma GCC optimize ("O3")
  59. #elif defined(__ICCARM__)
  60. #pragma optimize=speed high
  61. #endif
  62. int32_t writetospi_serial
  63. (
  64. uint16_t headerLength,
  65. const uint8_t *headerBuffer,
  66. uint32_t bodyLength,
  67. const uint8_t *bodyBuffer
  68. )
  69. {
  70. decaIrqStatus_t stat ;
  71. stat = decamutexon() ;
  72. port_SPIx_CS_clear();
  73. #if 0
  74. int i=0;
  75. for(i=0; i<headerLength; i++)
  76. {
  77. BSP_SPIx->DR = headerBuffer[i];
  78. while (port_SPIx_no_data());
  79. BSP_SPIx->DR ;
  80. }
  81. for(i=0; i<bodyLength; i++)
  82. {
  83. BSP_SPIx->DR = bodyBuffer[i];
  84. while(port_SPIx_no_data());
  85. BSP_SPIx->DR ;
  86. }
  87. #else
  88. /*
  89. HAL_SPI_Transmit(&DWM_SpiHandle, (uint8_t*)headerBuffer, headerLength, 1000);
  90. HAL_SPI_Transmit(&DWM_SpiHandle, (uint8_t*)bodyBuffer, bodyLength, 1000);*/
  91. memcpy((void*)decaTxBuf, headerBuffer, headerLength);
  92. memcpy((void*)(decaTxBuf + headerLength), bodyBuffer, bodyLength);
  93. HAL_SPI_Transmit(&DWM_SpiHandle, (uint8_t*)decaTxBuf, headerLength+bodyLength, 1000);
  94. #endif
  95. port_SPIx_CS_set();
  96. decamutexoff(stat) ;
  97. return 0;
  98. } // end writetospi()
  99. /*! ------------------------------------------------------------------------------------------------------------------
  100. * Function: readfromspi()
  101. *
  102. * Low level abstract function to read from the SPI
  103. * Takes two separate byte buffers for write header and read data
  104. * returns the offset into read buffer where first byte of read data may be found,
  105. * or returns -1 if there was an error
  106. */
  107. /*#ifdef __GNUC__
  108. #pragma GCC optimize ("O3")
  109. #elif defined(__ICCARM__)
  110. #pragma optimize=speed high
  111. #endif*/
  112. int32_t readfromspi_serial
  113. (
  114. uint16_t headerLength,
  115. const uint8_t *headerBuffer,
  116. uint32_t readlength,
  117. uint8_t *readBuffer
  118. )
  119. {
  120. decaIrqStatus_t stat ;
  121. stat = decamutexon() ;
  122. /* Wait for SPIx Tx buffer empty */
  123. //while (port_SPIx_busy_sending());
  124. port_SPIx_CS_clear();
  125. #if 0
  126. int i=0;
  127. for(i=0; i<headerLength; i++)
  128. {
  129. BSP_SPIx->DR = headerBuffer[i];
  130. while(port_SPIx_no_data());
  131. readBuffer[0] = BSP_SPIx->DR ; // Dummy read as we write the header
  132. }
  133. for(i=0; i<readlength; i++)
  134. {
  135. BSP_SPIx->DR = 0; // Dummy write as we read the message body
  136. while(port_SPIx_no_data() );
  137. readBuffer[i] = BSP_SPIx->DR ;//port_SPIx_receive_data(); //this clears RXNE bit
  138. }
  139. #else
  140. HAL_StatusTypeDef status;
  141. status = HAL_SPI_Transmit(&DWM_SpiHandle, (uint8_t*)headerBuffer, headerLength, 1000);
  142. status = HAL_SPI_Receive(&DWM_SpiHandle, readBuffer, readlength, 1000);
  143. #endif
  144. port_SPIx_CS_set();
  145. decamutexoff(stat);
  146. return 0;
  147. } // end readfromspi()