radio_comm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*!
  2. * File:
  3. * radio_comm.h
  4. *
  5. * Description:
  6. * This file contains the RADIO communication layer.
  7. *
  8. * Silicon Laboratories Confidential
  9. * Copyright 2012 Silicon Laboratories, Inc.
  10. */
  11. #include "radio_comm.h"
  12. #include "radio_hal.h"
  13. uint8_t ctsWentHigh = 0;
  14. /*!
  15. * Gets a command response from the radio chip
  16. *
  17. * @param byteCount Number of bytes to get from the radio chip
  18. * @param pData Pointer to where to put the data
  19. *
  20. * @return CTS value
  21. */
  22. uint8_t radio_comm_GetResp(uint8_t byteCount, uint8_t* pData)
  23. {
  24. uint8_t ctsVal=0;
  25. uint16_t errCnt=RADIO_CTS_TIMEOUT;
  26. while (errCnt != 0) //wait until radio IC is ready with the data
  27. {
  28. radio_hal_ClearNsel();
  29. radio_hal_SpiWriteByte(0x44); //read CMD buffer
  30. ctsVal = radio_hal_SpiReadByte();
  31. if (ctsVal == 0xFF)
  32. {
  33. if (byteCount)
  34. {
  35. radio_hal_SpiReadData(byteCount, pData);
  36. }
  37. radio_hal_SetNsel();
  38. break;
  39. }
  40. radio_hal_SetNsel();
  41. errCnt--;
  42. }
  43. if (errCnt == 0)
  44. {
  45. while(1)
  46. {
  47. /* ERROR!!!! CTS should never take this long. */
  48. #ifdef RADIO_COMM_ERROR_CALLBACK
  49. RADIO_COMM_ERROR_CALLBACK();
  50. #endif
  51. }
  52. }
  53. if (ctsVal == 0xFF)
  54. {
  55. ctsWentHigh = 1;
  56. }
  57. return ctsVal;
  58. }
  59. /*!
  60. * Sends a command to the radio chip
  61. *
  62. * @param byteCount Number of bytes in the command to send to the radio device
  63. * @param pData Pointer to the command to send.
  64. */
  65. void radio_comm_SendCmd(uint8_t byteCount, uint8_t* pData)
  66. {
  67. while (!ctsWentHigh)
  68. {
  69. radio_comm_PollCTS();
  70. }
  71. radio_hal_ClearNsel();
  72. radio_hal_SpiWriteData(byteCount, pData);
  73. radio_hal_SetNsel();
  74. ctsWentHigh = 0;
  75. }
  76. /*!
  77. * Gets a command response from the radio chip
  78. *
  79. * @param cmd Command ID
  80. * @param pollCts Set to poll CTS
  81. * @param byteCount Number of bytes to get from the radio chip.
  82. * @param pData Pointer to where to put the data.
  83. */
  84. void radio_comm_ReadData(uint8_t cmd, uint8_t pollCts, uint8_t byteCount, uint8_t* pData)
  85. {
  86. if(pollCts)
  87. {
  88. while(!ctsWentHigh)
  89. {
  90. radio_comm_PollCTS();
  91. }
  92. }
  93. radio_hal_ClearNsel();
  94. radio_hal_SpiWriteByte(cmd);
  95. radio_hal_SpiReadData(byteCount, pData);
  96. radio_hal_SetNsel();
  97. ctsWentHigh = 0;
  98. }
  99. /*!
  100. * Gets a command response from the radio chip
  101. *
  102. * @param cmd Command ID
  103. * @param pollCts Set to poll CTS
  104. * @param byteCount Number of bytes to get from the radio chip
  105. * @param pData Pointer to where to put the data
  106. */
  107. void radio_comm_WriteData(uint8_t cmd, uint8_t pollCts, uint8_t byteCount, uint8_t* pData)
  108. {
  109. if(pollCts)
  110. {
  111. while(!ctsWentHigh)
  112. {
  113. radio_comm_PollCTS();
  114. }
  115. }
  116. radio_hal_ClearNsel();
  117. radio_hal_SpiWriteByte(cmd);
  118. radio_hal_SpiWriteData(byteCount, pData);
  119. radio_hal_SetNsel();
  120. ctsWentHigh = 0;
  121. }
  122. /*!
  123. * Waits for CTS to be high
  124. *
  125. * @return CTS value
  126. */
  127. uint8_t radio_comm_PollCTS(void)
  128. {
  129. #ifdef RADIO_USER_CFG_USE_GPIO1_FOR_CTS
  130. while(!radio_hal_Gpio1Level())
  131. {
  132. /* Wait...*/
  133. }
  134. ctsWentHigh = 1;
  135. return 0xFF;
  136. #else
  137. return radio_comm_GetResp(0, 0);
  138. #endif
  139. }
  140. /**
  141. * Clears the CTS state variable.
  142. */
  143. void radio_comm_ClearCTS()
  144. {
  145. ctsWentHigh = 0;
  146. }
  147. /*!
  148. * Sends a command to the radio chip and gets a response
  149. *
  150. * @param cmdByteCount Number of bytes in the command to send to the radio device
  151. * @param pCmdData Pointer to the command data
  152. * @param respByteCount Number of bytes in the response to fetch
  153. * @param pRespData Pointer to where to put the response data
  154. *
  155. * @return CTS value
  156. */
  157. uint8_t radio_comm_SendCmdGetResp(uint8_t cmdByteCount, uint8_t* pCmdData, uint8_t respByteCount, uint8_t* pRespData)
  158. {
  159. radio_comm_SendCmd(cmdByteCount, pCmdData);
  160. return radio_comm_GetResp(respByteCount, pRespData);
  161. }