/********************** 头文件 *************************/ #include "bsp_esb.h" #include "nrf_esb.h" #include "nrf_error.h" #include "nrf_esb_error_codes.h" #include "hal_imu.h" /********************** 变量区 *************************/ static nrf_esb_payload_t tx_payload = NRF_ESB_CREATE_PAYLOAD(0, 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00); static nrf_esb_payload_t rx_payload; /********************** 环形缓存区 *************************/ static const int RxLen = 1024; static volatile unsigned char RxBuf[RxLen]; static volatile unsigned char* RxW=RxBuf; static volatile unsigned char* RxR=RxBuf; void ESB_Push(unsigned char* p,int len) { volatile unsigned char *W=RxW; //这里要与上面指针相同 if(len<=0) return; for(int i=0;i=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底) if(W!=RxR){*RxW=*(p+i); RxW=W;} else break; } } unsigned int ESB_CheckLen(void) //检查RX接收了多少数据 { unsigned int Len; //short volatile unsigned char *W=RxW; volatile unsigned char *R=RxR; if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果) return Len; } unsigned char ESB_ReadByte(void) //读RX中数锯,地指加一,和丢弃 { unsigned char R=*RxR; //读数 if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标 return R; } unsigned char ESB_CheckByte(unsigned short n) //看RX中数锯,地指不变, { volatile unsigned char *R=RxR+n; if(R>=(RxBuf+RxLen))R-=RxLen; return *R; } void ESB_Discard(unsigned short n) //丢弃RX数据几位 { while(n){ n--; if(RxR==RxW) return; if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标 } } //static void Radio_State(void) //{ // switch(NRF_RADIO->STATE) // { // case RADIO_STATE_STATE_Disabled: // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_Disabled\n"); // break; // case RADIO_STATE_STATE_RxRu: // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_RxRu\n"); // break; // case RADIO_STATE_STATE_RxIdle: // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_RxIdle\n"); // break; // case RADIO_STATE_STATE_Rx: // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_Rx\n"); // break; // case RADIO_STATE_STATE_RxDisable: // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_RxDisable\n"); // break; // case RADIO_STATE_STATE_TxRu: // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_TxRu\n"); // break; // case RADIO_STATE_STATE_TxIdle: // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_TxIdle\n"); // break; // case RADIO_STATE_STATE_Tx: // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_Tx\n"); // break; // case RADIO_STATE_STATE_TxDisable: // SEGGER_RTT_printf(0,"RADIO_STATE_STATE_TxDisable\n"); // break; // } //} #define ESB_COMMAND_LENGTH_MAX 200 void ESB_SendBuff(unsigned char *p,int L)//发送缓存 { uint8_t i; if(L>ESB_COMMAND_LENGTH_MAX) return; // (void) nrf_esb_stop_rx(); // Radio_State(); for(i=0;ievt_id) { case NRF_ESB_EVENT_TX_SUCCESS: // SEGGER_RTT_printf(0,"NRF_ESB_EVENT_TX_SUCCESS\n"); (void) nrf_esb_flush_tx(); (void) nrf_esb_start_rx(); break; case NRF_ESB_EVENT_TX_FAILED: // SEGGER_RTT_printf(0,"NRF_ESB_EVENT_TX_FAILED\n"); (void) nrf_esb_flush_tx(); (void) nrf_esb_start_rx(); break; case NRF_ESB_EVENT_RX_RECEIVED: // SEGGER_RTT_printf(0,"NRF_ESB_EVENT_RX_RECEIVED\n"); while (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS){ if(rx_payload.length > 0){ #if DEBUG_ESB_INT SEGGER_RTT_printf(0,"INT_ESB:"); for(int i=0;i