bsp_esb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /********************** 头文件 *************************/
  2. #include "bsp_esb.h"
  3. #include "main.h"
  4. #include "User_Task.h"
  5. #include "nrf_log.h"
  6. #include "nrf_log_ctrl.h"
  7. #include "nrf_log_default_backends.h"
  8. /********************** 变量区 *************************/
  9. static nrf_esb_payload_t tx_payload = NRF_ESB_CREATE_PAYLOAD(0, 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00);
  10. static nrf_esb_payload_t rx_payload;
  11. extern uint8_t SystemTimer_Minutes;//分钟计数器
  12. extern TranforStep_type m_tranforStep;
  13. extern ShoesInforMation_type m_shoes_info;
  14. extern Motion_Detect_type m_motion_detect;
  15. extern motor_control_type m_motor_control;
  16. APP_TIMER_DEF(m_ESB_SendOver_Protect_timer);
  17. static volatile bool ESB_Send_Finish_Flag =false;
  18. static volatile uint16_t ESB_Send_Error_Flag =0;
  19. volatile bool SetpSendFLag =false;
  20. /********************** 函数声明区 *************************/
  21. /**********************************************************
  22. * 函数名字:nrf_esb_event_handler
  23. * 函数作用:ESB 事件回调
  24. * 函数参数:p_event: ESB事件回调
  25. * 函数返回值:无
  26. ***********************************************************/
  27. void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
  28. {
  29. switch (p_event->evt_id)
  30. {
  31. case NRF_ESB_EVENT_TX_SUCCESS:
  32. OY_SetValue_True(ESB_Send_Finish_Flag);
  33. ESB_Send_Error_Flag =0;
  34. (void) nrf_esb_flush_tx();
  35. (void) nrf_esb_start_rx();
  36. break;
  37. case NRF_ESB_EVENT_TX_FAILED:
  38. OY_SetValue_True(ESB_Send_Finish_Flag);
  39. ESB_Send_Error_Flag =0;
  40. (void) nrf_esb_flush_tx();
  41. (void) nrf_esb_start_rx();
  42. break;
  43. case NRF_ESB_EVENT_RX_RECEIVED:
  44. if(nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS){
  45. if(rx_payload.length >= 6 && rx_payload.data[0] == 0xAA && (0xFF == (rx_payload.data[1]+rx_payload.data[2]))){
  46. #if DEBUG_EN
  47. SEGGER_RTT_printf(0,"ESB_RX:cmd:%02x,%02x,%02x\n",rx_payload.data[3],rx_payload.data[4],rx_payload.data[5]);
  48. #endif
  49. #if !ESB_RECV_SEND_TEST
  50. switch (rx_payload.data[3]){
  51. case 0xA1:
  52. if(0x02 == rx_payload.data[4]){
  53. if(0 == rx_payload.data[5]){
  54. if(m_tranforStep.StartFlag)
  55. TranforMainEvent_Fifo(Stop_SendStep_Packet_Event);
  56. TranforMainEvent_Fifo(Resquest_Step_Event);
  57. memcpy(&m_tranforStep.systemtime[0], &rx_payload.data[6],8);
  58. SystemTimer_Minutes = rx_payload.data[14];//距离上一个整点的分钟数
  59. }
  60. else{
  61. if(rx_payload.data[5] == m_tranforStep.CurrentPacket && m_tranforStep.StartFlag){//判断一下是不是当前发送包的反馈数据
  62. if(m_tranforStep.CurrentPacket >=m_tranforStep.Packetlength){//发送完成
  63. m_tranforStep.StopFlag =true;
  64. #if !STEP_TEST
  65. Clear_Stepflash();//清空脚步记录
  66. if(m_motion_detect.state != Game_Mode)TranforMainEvent_Fifo(UpdateStep_Event);//游戏模式下数据不保存到flash
  67. #endif
  68. TranforMainEvent_Fifo(Stop_SendStep_Packet_Event);
  69. }
  70. else{
  71. m_tranforStep.CurrentPacket++;
  72. SetpSendFLag =true;
  73. if(m_tranforStep.BeSentDataLength >=ESB_MAX_SEND_DATA)
  74. m_tranforStep.BeSentDataLength -= ESB_MAX_SEND_DATA;
  75. m_tranforStep.NumberTransfers =0;
  76. }
  77. }
  78. }
  79. }
  80. break;
  81. case 0xA2:
  82. if(0x01 == rx_payload.data[4] && m_motion_detect.state != Game_Mode)TranforMainEvent_Fifo(Into_GameMode_Event);
  83. else if(0x00 == rx_payload.data[4])TranforMainEvent_Fifo(Quit_GameMode_Event);
  84. break;
  85. case 0xA4:
  86. m_motor_control.flag =true;
  87. m_motor_control.delay_ms = (((uint16_t)rx_payload.data[4]<<8 )+rx_payload.data[5]);
  88. nrf_gpio_pin_write(MT_EN,1);
  89. break;
  90. default:
  91. break;
  92. }
  93. #endif
  94. }
  95. }break;
  96. default:
  97. break;
  98. }
  99. }
  100. /**********************************************************
  101. * 函数名字:ESB_SendOver_Protect_handler
  102. * 函数作用:鞋子运动检测,检测气压传感器判断鞋子是否在运动
  103. * 函数参数:p_context:上下文
  104. * 函数返回值:无
  105. ***********************************************************/
  106. static void ESB_SendOver_Protect_handler(void * p_context)
  107. {
  108. static uint8_t temp_flag = 0;
  109. if(!ESB_Send_Finish_Flag){
  110. temp_flag++;
  111. }
  112. else temp_flag =0;
  113. if(temp_flag >10){
  114. (void) nrf_esb_flush_tx();
  115. (void) nrf_esb_start_rx();
  116. temp_flag =0;
  117. }
  118. }
  119. static void Create_Esb_Protect_timer(void)
  120. {
  121. static bool flag_create =false;
  122. uint32_t err_code;
  123. if(!flag_create){
  124. //创建并启动软件定时器
  125. err_code = app_timer_create(&m_ESB_SendOver_Protect_timer,
  126. APP_TIMER_MODE_REPEATED,
  127. ESB_SendOver_Protect_handler);
  128. APP_ERROR_CHECK(err_code);
  129. flag_create = true;
  130. }
  131. }
  132. static void Esb_Protect_timer_set(bool value)
  133. {
  134. static bool timer_able =false;
  135. uint32_t err_code;
  136. if(value){
  137. if(timer_able){
  138. err_code = app_timer_stop(m_ESB_SendOver_Protect_timer);
  139. APP_ERROR_CHECK(err_code);
  140. err_code = app_timer_start(m_ESB_SendOver_Protect_timer, APP_TIMER_TICKS(1), NULL);
  141. APP_ERROR_CHECK(err_code);
  142. }
  143. timer_able = value;
  144. }
  145. else{
  146. if(timer_able){
  147. err_code = app_timer_stop(m_ESB_SendOver_Protect_timer);
  148. APP_ERROR_CHECK(err_code);
  149. }
  150. timer_able = value;
  151. }
  152. }
  153. /**********************************************************
  154. * 函数名字:bsp_esb_init
  155. * 函数作用:ESP模块功能初始化
  156. * 函数参数:
  157. * 函数返回值:无
  158. ***********************************************************/
  159. static uint32_t bsp_esb_init(void)
  160. {
  161. uint32_t err_code;
  162. uint8_t base_addr_0[4] = {ADDR, ADDR, ADDR, ADDR};
  163. uint8_t base_addr_1[4] = {ADDR, ADDR, ADDR, ADDR};
  164. uint8_t addr_prefix[8] = {ADDR, ADDR, ADDR, ADDR, ADDR, ADDR, ADDR, ADDR };
  165. nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
  166. nrf_esb_config.payload_length = 250;
  167. nrf_esb_config.retransmit_delay = 600;
  168. nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
  169. nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
  170. nrf_esb_config.mode = NRF_ESB_MODE_PTX;
  171. nrf_esb_config.event_handler = nrf_esb_event_handler;
  172. nrf_esb_config.selective_auto_ack = false;
  173. err_code = nrf_esb_init(&nrf_esb_config);
  174. VERIFY_SUCCESS(err_code);
  175. err_code = nrf_esb_set_base_address_0(base_addr_0);
  176. VERIFY_SUCCESS(err_code);
  177. err_code = nrf_esb_set_base_address_1(base_addr_1);
  178. VERIFY_SUCCESS(err_code);
  179. err_code = nrf_esb_set_prefixes(addr_prefix, 8);
  180. VERIFY_SUCCESS(err_code);
  181. //(void) nrf_esb_flush_tx();
  182. //(void) nrf_esb_start_rx();
  183. Create_Esb_Protect_timer();
  184. // NRF_RADIO->BASE0 = 0x52648abd;
  185. // NRF_RADIO->BASE1 = 0xabc25451;
  186. // NRF_RADIO->PREFIX0 = 0x9899eeda;
  187. // NRF_RADIO->PREFIX1 = 0x57621adf;
  188. return err_code;
  189. }
  190. /**********************************************************
  191. * 函数名字:User_Esb_DisOrEnable
  192. * 函数作用:ESP模块的开关,关闭的时候需要考虑低功耗,所以需要关闭高速晶振和时钟
  193. * 函数参数:value: true 打开
  194. * false:关闭
  195. * 函数返回值:无
  196. ***********************************************************/
  197. void User_Esb_Set(bool value)
  198. {
  199. static bool Esb_Openflag =false;
  200. ret_code_t err_code;
  201. if(Esb_Openflag == value)return;
  202. if(value){//打开ESB
  203. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  204. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  205. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
  206. nrf_drv_clock_hfclk_request(NULL);
  207. while (!nrf_drv_clock_hfclk_is_running()){}
  208. err_code = bsp_esb_init(); //初始化ESB
  209. APP_ERROR_CHECK(err_code);
  210. err_code = nrf_esb_start_rx();
  211. APP_ERROR_CHECK(err_code);
  212. Esb_Openflag =value;
  213. }
  214. else if(m_motion_detect.state != Game_Mode && !m_tranforStep.StartFlag){//关闭ESB
  215. ESB_Send_Error_Flag =0;
  216. Esb_Protect_timer_set(false);
  217. nrf_esb_flush_tx();
  218. nrf_esb_flush_rx();
  219. nrf_esb_stop_rx();
  220. if(NRF_SUCCESS == nrf_esb_disable())
  221. {
  222. nrf_drv_clock_hfclk_release();
  223. NRF_CLOCK->TASKS_HFCLKSTOP = 1;
  224. }
  225. Esb_Openflag =value;
  226. }
  227. }
  228. /**********************************************************
  229. * 函数名字:bsp_esb_send
  230. * 函数作用:通过ESB接口发送数据
  231. * 函数参数:pdat:需要发送的数据
  232. * len: 需要发送的数据长度
  233. * 函数返回值:无
  234. ***********************************************************/
  235. uint32_t bsp_esb_send(uint8_t * pdat, uint8_t len)
  236. {
  237. uint32_t err_code=0;
  238. tx_payload.noack=false;
  239. tx_payload.length=0;
  240. tx_payload.pid=0x11;
  241. tx_payload.pipe=0x01;
  242. nrf_esb_stop_rx();
  243. OY_SetValue_False(ESB_Send_Finish_Flag);
  244. Esb_Protect_timer_set(true);
  245. ESB_Send_Error_Flag++;
  246. if(ESB_Send_Error_Flag > 200){
  247. ESB_Send_Error_Flag =0;
  248. TranforMainEvent_Fifo(EsbError_Event);
  249. }
  250. for(int i=0;i<len;i++){
  251. tx_payload.data[tx_payload.length++]=pdat[i];
  252. }
  253. err_code=nrf_esb_write_payload(&tx_payload);
  254. #if DEBUG_EN
  255. if(err_code!=0)SEGGER_RTT_printf(0,"bsp_esb_send -> error = %d\n",err_code);
  256. #endif
  257. return 1;
  258. }