bsp_esb.c 6.3 KB

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