radio.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*! @file radio.c
  2. * @brief This file contains functions to interface with the radio chip.
  3. *
  4. * @b COPYRIGHT
  5. * @n Silicon Laboratories Confidential
  6. * @n Copyright 2012 Silicon Laboratories, Inc.
  7. * @n http://www.silabs.com
  8. */
  9. #include "radio_config.h"
  10. #include "si446x_api_lib.h"
  11. #include "radio_hal.h"
  12. #include "radio.h"
  13. #include "si446x_cmd.h"
  14. uint8_t Radio_Configuration_Data_Array[]=RADIO_CONFIGURATION_DATA_ARRAY;
  15. tRadioConfiguration RadioConfiguration=RADIO_CONFIGURATION_DATA;
  16. tRadioConfiguration* pRadioConfiguration=&RadioConfiguration;
  17. uint8_t customRadioPacket[RADIO_MAX_PACKET_LENGTH];
  18. extern volatile uint8_t irq_flag;
  19. void vRadio_PowerUp(void)
  20. {
  21. uint16_t wDelay=0;
  22. /* Hardware reset the chip */
  23. si446x_reset();
  24. /* Wait until reset timeout or Reset IT signal */
  25. for (; wDelay < pRadioConfiguration->Radio_Delay_Cnt_After_Reset; wDelay++);
  26. }
  27. /*!
  28. * Radio Initialization.
  29. *
  30. * @author Sz. Papp
  31. *
  32. * @note
  33. *
  34. */
  35. void vRadio_Init(void)
  36. {
  37. uint16_t wDelay;
  38. /* Power Up the radio chip */
  39. vRadio_PowerUp();
  40. si446x_part_info();//get part information6
  41. if((Si446xCmd.PART_INFO.PART != 0x4438) && (Si446xCmd.PART_INFO.PART != 0x4463))//part information check
  42. {
  43. while(1)
  44. {
  45. /* ERROR!!!! SPI communicate error or part information error */
  46. #ifdef RADIO_COMM_ERROR_CALLBACK
  47. RADIO_COMM_ERROR_CALLBACK();
  48. #endif
  49. }
  50. }
  51. /* Load radio configuration */
  52. while (SI446X_SUCCESS != si446x_configuration_init(pRadioConfiguration->Radio_ConfigurationArray))
  53. {
  54. for (wDelay = 0x7FFF; wDelay--; ) ;
  55. /* Power Up the radio chip */
  56. vRadio_PowerUp();
  57. }
  58. // Read ITs, clear pending ones
  59. si446x_get_int_status(0, 0, 0);
  60. }
  61. /*!
  62. * Check if Packet sent IT flag or Packet Received IT is pending.
  63. *
  64. * @return SI4455_CMD_GET_INT_STATUS_REP_PACKET_SENT_PEND_BIT / SI4455_CMD_GET_INT_STATUS_REP_PACKET_RX_PEND_BIT
  65. *
  66. * @note
  67. *
  68. */
  69. uint8_t bRadio_Check_Tx_RX(void)
  70. {
  71. if(radio_hal_NirqLevel() == RESET)
  72. // if(irq_flag)//²úÉúÖжÏ
  73. {
  74. // irq_flag=0;
  75. /* Read ITs, clear pending ones */
  76. si446x_get_int_status(0, 0, 0);
  77. if (Si446xCmd.GET_INT_STATUS.CHIP_PEND & SI446X_CMD_GET_CHIP_STATUS_REP_CHIP_PEND_CMD_ERROR_PEND_BIT)//cmd EEROR
  78. {
  79. /* State change to */
  80. si446x_change_state(SI446X_CMD_CHANGE_STATE_ARG_NEXT_STATE1_NEW_STATE_ENUM_SLEEP);
  81. /* Reset FIFO */
  82. si446x_fifo_info(SI446X_CMD_FIFO_INFO_ARG_FIFO_RX_BIT);
  83. /* State change to */
  84. si446x_change_state(SI446X_CMD_CHANGE_STATE_ARG_NEXT_STATE1_NEW_STATE_ENUM_RX);
  85. }
  86. if(Si446xCmd.GET_INT_STATUS.MODEM_PEND & SI446X_CMD_GET_INT_STATUS_REP_MODEM_PEND_RSSI_PEND_BIT )
  87. {
  88. si446x_get_modem_status(0);
  89. }
  90. if(Si446xCmd.GET_INT_STATUS.PH_PEND & SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_SENT_PEND_BIT)//send finish
  91. {
  92. return SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_SENT_PEND_BIT;
  93. }
  94. if(Si446xCmd.GET_INT_STATUS.PH_PEND & SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_RX_PEND_BIT)//recive finish
  95. {
  96. /* Packet RX */
  97. /* Get payload length */
  98. si446x_fifo_info(0x00);
  99. if (Si446xCmd.FIFO_INFO.RX_FIFO_COUNT > sizeof(customRadioPacket))
  100. {
  101. Si446xCmd.FIFO_INFO.RX_FIFO_COUNT = sizeof(customRadioPacket);
  102. }
  103. si446x_read_rx_fifo(Si446xCmd.FIFO_INFO.RX_FIFO_COUNT, &customRadioPacket[0]);
  104. return SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_RX_PEND_BIT;
  105. }
  106. if(Si446xCmd.GET_INT_STATUS.MODEM_PEND & SI446X_CMD_GET_INT_STATUS_REP_MODEM_PEND_SYNC_DETECT_PEND_BIT )
  107. {
  108. //si446x_get_modem_status_fast_clear_read();
  109. si446x_get_modem_status(0);
  110. return SI446X_CMD_GET_INT_STATUS_REP_MODEM_PEND_SYNC_DETECT_PEND_BIT;
  111. }
  112. if(Si446xCmd.GET_INT_STATUS.PH_PEND & SI446X_CMD_GET_INT_STATUS_REP_PH_STATUS_CRC_ERROR_BIT)//crc error
  113. {
  114. /* Reset FIFO */
  115. si446x_fifo_info(SI446X_CMD_FIFO_INFO_ARG_FIFO_RX_BIT);
  116. return SI446X_CMD_GET_INT_STATUS_REP_PH_STATUS_CRC_ERROR_BIT;
  117. }
  118. }
  119. return 0;
  120. }
  121. /*!
  122. * Set Radio to RX mode. .
  123. *
  124. * @param channel Freq. Channel, packetLength : 0 Packet handler fields are used , nonzero: only Field1 is used
  125. *
  126. * @note
  127. *
  128. */
  129. void vRadio_StartRX(uint8_t channel, uint8_t packetLenght )
  130. {
  131. // Read ITs, clear pending ones
  132. si446x_get_int_status(0, 0, 0);
  133. // Reset the Rx Fifo
  134. si446x_fifo_info(SI446X_CMD_FIFO_INFO_ARG_FIFO_RX_BIT);
  135. /* Start Receiving packet, channel 0, START immediately, Packet length used or not according to packetLength */
  136. si446x_start_rx(channel, 0, packetLenght,
  137. SI446X_CMD_START_RX_ARG_NEXT_STATE1_RXTIMEOUT_STATE_ENUM_NOCHANGE,
  138. SI446X_CMD_START_RX_ARG_NEXT_STATE2_RXVALID_STATE_ENUM_READY,
  139. SI446X_CMD_START_RX_ARG_NEXT_STATE3_RXINVALID_STATE_ENUM_RX );
  140. }
  141. /*!
  142. * Set Radio to TX mode, variable packet length.
  143. *
  144. * @param channel Freq. Channel, Packet to be sent length of of the packet sent to TXFIFO
  145. *
  146. * @note
  147. *
  148. */
  149. void vRadio_StartTx_Variable_Packet(uint8_t channel, uint8_t *pioRadioPacket, uint8_t length)
  150. {
  151. /* Leave RX state */
  152. si446x_change_state(SI446X_CMD_CHANGE_STATE_ARG_NEXT_STATE1_NEW_STATE_ENUM_READY);
  153. /* Read ITs, clear pending ones */
  154. si446x_get_int_status(0, 0, 0);
  155. /* Reset the Tx Fifo */
  156. si446x_fifo_info(SI446X_CMD_FIFO_INFO_ARG_FIFO_TX_BIT);
  157. /* Fill the TX fifo with datas */
  158. si446x_write_tx_fifo(length, pioRadioPacket);
  159. /* Start sending packet, channel 0, START immediately */
  160. si446x_start_tx(channel, 0x80, length);
  161. }
  162. void vRadio_StartSleep(void)
  163. {
  164. /* Put radio into sleep state*/
  165. si446x_change_state(SI446X_CMD_CHANGE_STATE_ARG_NEXT_STATE1_NEW_STATE_ENUM_SLEEP);
  166. }
  167. uint16_t vRadio_getRxCount(void)
  168. {
  169. return Si446xCmd.FIFO_INFO.RX_FIFO_COUNT;
  170. }
  171. uint8_t vRadio_getRssi(void)
  172. {
  173. si446x_get_modem_status(0);
  174. return Si446xCmd.GET_MODEM_STATUS.CURR_RSSI;
  175. }
  176. uint8_t vRadio_getPacketRssi(void)
  177. {
  178. return Si446xCmd.GET_MODEM_STATUS.CURR_RSSI;
  179. }