bsp_esb.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /********************** 头文件 *************************/
  2. #include "bsp_esb.h"
  3. #include "nrf_esb.h"
  4. #include "nrf_error.h"
  5. #include "nrf_esb_error_codes.h"
  6. #include "hal_imu.h"
  7. /********************** 变量区 *************************/
  8. static nrf_esb_payload_t tx_payload = NRF_ESB_CREATE_PAYLOAD(0, 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00);
  9. static nrf_esb_payload_t rx_payload;
  10. /********************** 环形缓存区 *************************/
  11. static const int RxLen = 1024;
  12. static volatile unsigned char RxBuf[RxLen];
  13. static volatile unsigned char* RxW=RxBuf;
  14. static volatile unsigned char* RxR=RxBuf;
  15. void ESB_Push(unsigned char* p,int len)
  16. {
  17. volatile unsigned char *W=RxW; //这里要与上面指针相同
  18. if(len<=0) return;
  19. for(int i=0;i<len;i++){
  20. W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底)
  21. if(W!=RxR){*RxW=*(p+i); RxW=W;}
  22. else break;
  23. }
  24. }
  25. unsigned int ESB_CheckLen(void) //检查RX接收了多少数据
  26. {
  27. unsigned int Len; //short
  28. volatile unsigned char *W=RxW; volatile unsigned char *R=RxR;
  29. if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果)
  30. return Len;
  31. }
  32. unsigned char ESB_ReadByte(void) //读RX中数锯,地指加一,和丢弃
  33. {
  34. unsigned char R=*RxR; //读数
  35. if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标
  36. return R;
  37. }
  38. unsigned char ESB_CheckByte(unsigned short n) //看RX中数锯,地指不变,
  39. {
  40. volatile unsigned char *R=RxR+n;
  41. if(R>=(RxBuf+RxLen))R-=RxLen;
  42. return *R;
  43. }
  44. void ESB_Discard(unsigned short n) //丢弃RX数据几位
  45. {
  46. while(n){ n--;
  47. if(RxR==RxW) return;
  48. if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标
  49. }
  50. }
  51. //static void Radio_State(void)
  52. //{
  53. // switch(NRF_RADIO->STATE)
  54. // {
  55. // case RADIO_STATE_STATE_Disabled:
  56. // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_Disabled\n");
  57. // break;
  58. // case RADIO_STATE_STATE_RxRu:
  59. // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_RxRu\n");
  60. // break;
  61. // case RADIO_STATE_STATE_RxIdle:
  62. // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_RxIdle\n");
  63. // break;
  64. // case RADIO_STATE_STATE_Rx:
  65. // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_Rx\n");
  66. // break;
  67. // case RADIO_STATE_STATE_RxDisable:
  68. // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_RxDisable\n");
  69. // break;
  70. // case RADIO_STATE_STATE_TxRu:
  71. // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_TxRu\n");
  72. // break;
  73. // case RADIO_STATE_STATE_TxIdle:
  74. // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_TxIdle\n");
  75. // break;
  76. // case RADIO_STATE_STATE_Tx:
  77. // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_Tx\n");
  78. // break;
  79. // case RADIO_STATE_STATE_TxDisable:
  80. // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_TxDisable\n");
  81. // break;
  82. // }
  83. //}
  84. #define ESB_COMMAND_LENGTH_MAX 200
  85. void ESB_SendBuff(unsigned char *p,int L)//发送缓存
  86. {
  87. uint8_t i;
  88. if(L>ESB_COMMAND_LENGTH_MAX) return;
  89. //
  90. (void) nrf_esb_stop_rx();
  91. // Radio_State();
  92. for(i=0;i<L;i++){
  93. tx_payload.data[i] = p[i];
  94. }
  95. tx_payload.noack = false;
  96. tx_payload.length = L;
  97. nrf_esb_write_payload(&tx_payload);
  98. }
  99. //*****************************************************************//
  100. /********************** 函数声明区 *************************/
  101. void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
  102. {
  103. switch (p_event->evt_id)
  104. {
  105. case NRF_ESB_EVENT_TX_SUCCESS:
  106. // SEGGER_RTT_printf(0,"NRF_ESB_EVENT_TX_SUCCESS\n");
  107. (void) nrf_esb_flush_tx();
  108. (void) nrf_esb_start_rx();
  109. break;
  110. case NRF_ESB_EVENT_TX_FAILED:
  111. // SEGGER_RTT_printf(0,"NRF_ESB_EVENT_TX_FAILED\n");
  112. (void) nrf_esb_flush_tx();
  113. (void) nrf_esb_start_rx();
  114. break;
  115. case NRF_ESB_EVENT_RX_RECEIVED:
  116. // SEGGER_RTT_printf(0,"NRF_ESB_EVENT_RX_RECEIVED\n");
  117. while (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS){
  118. if(rx_payload.length > 0){
  119. #if DEBUG_ESB_INT
  120. SEGGER_RTT_printf(0,"INT_ESB:"); for(int i=0;i<rx_payload.length;i++){SEGGER_RTT_printf(0,"%02X ",rx_payload.data[i]);} SEGGER_RTT_printf(0,"\r\n");
  121. #endif
  122. IMU_SetRssi(rx_payload.rssi);
  123. if(rx_payload.data[0]==0xAA && rx_payload.data[1]==(uint8_t)(~rx_payload.data[2])){
  124. if(rx_payload.data[3]==0){ //IMU data
  125. IMU_SetSlaveData(rx_payload.data,rx_payload.length);
  126. }else
  127. ESB_Push(rx_payload.data,rx_payload.length);
  128. }
  129. }
  130. }
  131. break;
  132. }
  133. }
  134. #define ADDR ESB_ADDR
  135. uint32_t ESB_Init(void)
  136. {
  137. uint32_t err_code;
  138. uint8_t base_addr_0[4] = {0x01, 0x01, ADDR, ADDR};
  139. uint8_t base_addr_1[4] = {0x01, 0x01, ADDR, ADDR};
  140. uint8_t addr_prefix[8] = {0x01, 0x01, 0x01, 0x01, ADDR, ADDR, ADDR, ADDR };
  141. nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
  142. // nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
  143. nrf_esb_config.payload_length = 250;
  144. // nrf_esb_config.retransmit_delay = 600;
  145. // nrf_esb_config.payload_length = ESB_COMMAND_LENGTH_MAX;
  146. nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
  147. nrf_esb_config.event_handler = nrf_esb_event_handler;
  148. nrf_esb_config.mode = NRF_ESB_MODE_PTX;
  149. nrf_esb_config.selective_auto_ack = false;
  150. err_code = nrf_esb_init(&nrf_esb_config);
  151. VERIFY_SUCCESS(err_code);
  152. err_code = nrf_esb_set_base_address_0(base_addr_0);
  153. VERIFY_SUCCESS(err_code);
  154. err_code = nrf_esb_set_base_address_1(base_addr_1);
  155. VERIFY_SUCCESS(err_code);
  156. err_code = nrf_esb_set_prefixes(addr_prefix, NRF_ESB_PIPE_COUNT);
  157. VERIFY_SUCCESS(err_code);
  158. (void) nrf_esb_flush_tx();
  159. (void) nrf_esb_start_rx();
  160. // uint8_t temp = 0;
  161. // ESB_SendBuff(&temp,1);
  162. return err_code;
  163. }