123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /********************** 头文件 *************************/
- #include "bsp_esb.h"
- #include "main.h"
- #include "User_Task.h"
- #include "nrf_log.h"
- #include "nrf_log_ctrl.h"
- #include "nrf_log_default_backends.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;
- extern uint8_t SystemTimer_Minutes;//分钟计数器
- extern TranforStep_type m_tranforStep;
- extern ShoesInforMation_type m_shoes_info;
- extern Motion_Detect_type m_motion_detect;
- extern motor_control_type m_motor_control;
- APP_TIMER_DEF(m_ESB_SendOver_Protect_timer);
- static volatile bool ESB_Send_Finish_Flag =false;
- static volatile uint16_t ESB_Send_Error_Flag =0;
- volatile bool SetpSendFLag =false;
- /********************** 函数声明区 *************************/
- /**********************************************************
- * 函数名字:nrf_esb_event_handler
- * 函数作用:ESB 事件回调
- * 函数参数:p_event: ESB事件回调
- * 函数返回值:无
- ***********************************************************/
- void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
- {
- switch (p_event->evt_id)
- {
- case NRF_ESB_EVENT_TX_SUCCESS:
- OY_SetValue_True(ESB_Send_Finish_Flag);
- ESB_Send_Error_Flag =0;
- (void) nrf_esb_flush_tx();
- (void) nrf_esb_start_rx();
- break;
- case NRF_ESB_EVENT_TX_FAILED:
- OY_SetValue_True(ESB_Send_Finish_Flag);
- ESB_Send_Error_Flag =0;
- (void) nrf_esb_flush_tx();
- (void) nrf_esb_start_rx();
- break;
- case NRF_ESB_EVENT_RX_RECEIVED:
- if(nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS){
- if(rx_payload.length >= 6 && rx_payload.data[0] == 0xAA && (0xFF == (rx_payload.data[1]+rx_payload.data[2]))){
- #if DEBUG_EN
- SEGGER_RTT_printf(0,"ESB_RX:cmd:%02x,%02x,%02x\n",rx_payload.data[3],rx_payload.data[4],rx_payload.data[5]);
- #endif
- #if !ESB_RECV_SEND_TEST
- switch (rx_payload.data[3]){
- case 0xA1:
- if(0x02 == rx_payload.data[4]){
- if(0 == rx_payload.data[5]){
- if(m_tranforStep.StartFlag)
- TranforMainEvent_Fifo(Stop_SendStep_Packet_Event);
- TranforMainEvent_Fifo(Resquest_Step_Event);
- memcpy(&m_tranforStep.systemtime[0], &rx_payload.data[6],8);
- SystemTimer_Minutes = rx_payload.data[14];//距离上一个整点的分钟数
- }
- else{
- if(rx_payload.data[5] == m_tranforStep.CurrentPacket && m_tranforStep.StartFlag){//判断一下是不是当前发送包的反馈数据
- if(m_tranforStep.CurrentPacket >=m_tranforStep.Packetlength){//发送完成
- m_tranforStep.StopFlag =true;
- #if !STEP_TEST
- Clear_Stepflash();//清空脚步记录
- if(m_motion_detect.state != Game_Mode)TranforMainEvent_Fifo(UpdateStep_Event);//游戏模式下数据不保存到flash
- #endif
- TranforMainEvent_Fifo(Stop_SendStep_Packet_Event);
- }
- else{
- m_tranforStep.CurrentPacket++;
- SetpSendFLag =true;
- if(m_tranforStep.BeSentDataLength >=ESB_MAX_SEND_DATA)
- m_tranforStep.BeSentDataLength -= ESB_MAX_SEND_DATA;
- m_tranforStep.NumberTransfers =0;
- }
- }
- }
- }
- break;
- case 0xA2:
- if(0x01 == rx_payload.data[4] && m_motion_detect.state != Game_Mode)TranforMainEvent_Fifo(Into_GameMode_Event);
- else if(0x00 == rx_payload.data[4])TranforMainEvent_Fifo(Quit_GameMode_Event);
- break;
- case 0xA4:
- m_motor_control.flag =true;
- m_motor_control.delay_ms = (((uint16_t)rx_payload.data[4]<<8 )+rx_payload.data[5]);
- nrf_gpio_pin_write(MT_EN,1);
- break;
- default:
- break;
- }
- #endif
- }
- }break;
-
- default:
- break;
- }
- }
- /**********************************************************
- * 函数名字:ESB_SendOver_Protect_handler
- * 函数作用:鞋子运动检测,检测气压传感器判断鞋子是否在运动
- * 函数参数:p_context:上下文
- * 函数返回值:无
- ***********************************************************/
- static void ESB_SendOver_Protect_handler(void * p_context)
- {
- static uint8_t temp_flag = 0;
- if(!ESB_Send_Finish_Flag){
- temp_flag++;
- }
- else temp_flag =0;
- if(temp_flag >10){
- (void) nrf_esb_flush_tx();
- (void) nrf_esb_start_rx();
- temp_flag =0;
- }
- }
- static void Create_Esb_Protect_timer(void)
- {
- static bool flag_create =false;
- uint32_t err_code;
- if(!flag_create){
- //创建并启动软件定时器
- err_code = app_timer_create(&m_ESB_SendOver_Protect_timer,
- APP_TIMER_MODE_REPEATED,
- ESB_SendOver_Protect_handler);
- APP_ERROR_CHECK(err_code);
- flag_create = true;
- }
- }
- static void Esb_Protect_timer_set(bool value)
- {
- static bool timer_able =false;
- uint32_t err_code;
- if(value){
- if(timer_able){
- err_code = app_timer_stop(m_ESB_SendOver_Protect_timer);
- APP_ERROR_CHECK(err_code);
- err_code = app_timer_start(m_ESB_SendOver_Protect_timer, APP_TIMER_TICKS(1), NULL);
- APP_ERROR_CHECK(err_code);
- }
- timer_able = value;
- }
- else{
- if(timer_able){
- err_code = app_timer_stop(m_ESB_SendOver_Protect_timer);
- APP_ERROR_CHECK(err_code);
- }
- timer_able = value;
- }
-
-
- }
- /**********************************************************
- * 函数名字:bsp_esb_init
- * 函数作用:ESP模块功能初始化
- * 函数参数:
- * 函数返回值:无
- ***********************************************************/
- static uint32_t bsp_esb_init(void)
- {
- uint32_t err_code;
- uint8_t base_addr_0[4] = {ADDR, ADDR, ADDR, ADDR};
- uint8_t base_addr_1[4] = {ADDR, ADDR, ADDR, ADDR};
- uint8_t addr_prefix[8] = {ADDR, ADDR, ADDR, ADDR, ADDR, ADDR, ADDR, ADDR };
- nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
- nrf_esb_config.payload_length = 250;
- nrf_esb_config.retransmit_delay = 600;
- nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
- nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
- nrf_esb_config.mode = NRF_ESB_MODE_PTX;
- nrf_esb_config.event_handler = nrf_esb_event_handler;
- nrf_esb_config.selective_auto_ack = false;
- err_code = nrf_esb_init(&nrf_esb_config);
- VERIFY_SUCCESS(err_code);
- err_code = nrf_esb_set_base_address_0(base_addr_0);
- VERIFY_SUCCESS(err_code);
- err_code = nrf_esb_set_base_address_1(base_addr_1);
- VERIFY_SUCCESS(err_code);
- err_code = nrf_esb_set_prefixes(addr_prefix, 8);
- VERIFY_SUCCESS(err_code);
- //(void) nrf_esb_flush_tx();
- //(void) nrf_esb_start_rx();
- Create_Esb_Protect_timer();
- // NRF_RADIO->BASE0 = 0x52648abd;
- // NRF_RADIO->BASE1 = 0xabc25451;
- // NRF_RADIO->PREFIX0 = 0x9899eeda;
- // NRF_RADIO->PREFIX1 = 0x57621adf;
- return err_code;
- }
- /**********************************************************
- * 函数名字:User_Esb_DisOrEnable
- * 函数作用:ESP模块的开关,关闭的时候需要考虑低功耗,所以需要关闭高速晶振和时钟
- * 函数参数:value: true 打开
- * false:关闭
- * 函数返回值:无
- ***********************************************************/
- void User_Esb_Set(bool value)
- {
- static bool Esb_Openflag =false;
- ret_code_t err_code;
- if(Esb_Openflag == value)return;
- if(value){//打开ESB
- NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
- NRF_CLOCK->TASKS_HFCLKSTART = 1;
- while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
- nrf_drv_clock_hfclk_request(NULL);
- while (!nrf_drv_clock_hfclk_is_running()){}
- err_code = bsp_esb_init(); //初始化ESB
- APP_ERROR_CHECK(err_code);
- err_code = nrf_esb_start_rx();
- APP_ERROR_CHECK(err_code);
- Esb_Openflag =value;
- }
- else if(m_motion_detect.state != Game_Mode && !m_tranforStep.StartFlag){//关闭ESB
- ESB_Send_Error_Flag =0;
- Esb_Protect_timer_set(false);
- nrf_esb_flush_tx();
- nrf_esb_flush_rx();
- nrf_esb_stop_rx();
- if(NRF_SUCCESS == nrf_esb_disable())
- {
- nrf_drv_clock_hfclk_release();
- NRF_CLOCK->TASKS_HFCLKSTOP = 1;
- }
- Esb_Openflag =value;
- }
-
- }
- /**********************************************************
- * 函数名字:bsp_esb_send
- * 函数作用:通过ESB接口发送数据
- * 函数参数:pdat:需要发送的数据
- * len: 需要发送的数据长度
- * 函数返回值:无
- ***********************************************************/
- uint32_t bsp_esb_send(uint8_t * pdat, uint8_t len)
- {
- uint32_t err_code=0;
- tx_payload.noack=false;
- tx_payload.length=0;
- tx_payload.pid=0x11;
- tx_payload.pipe=0x01;
- nrf_esb_stop_rx();
- OY_SetValue_False(ESB_Send_Finish_Flag);
- Esb_Protect_timer_set(true);
- ESB_Send_Error_Flag++;
- if(ESB_Send_Error_Flag > 200){
- ESB_Send_Error_Flag =0;
- TranforMainEvent_Fifo(EsbError_Event);
- }
- for(int i=0;i<len;i++){
- tx_payload.data[tx_payload.length++]=pdat[i];
- }
- err_code=nrf_esb_write_payload(&tx_payload);
- #if DEBUG_EN
- if(err_code!=0)SEGGER_RTT_printf(0,"bsp_esb_send -> error = %d\n",err_code);
- #endif
- return 1;
- }
|