bsp_esb.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. int8_t s_rssi;
  7. volatile uint16_t s_press_buf[3];
  8. volatile uint16_t s_press_dex = 0;
  9. volatile uint16_t s_press = 0;
  10. volatile int16_t s_pos[3];
  11. volatile int16_t s_att[3];
  12. volatile int16_t s_zupt;
  13. extern int time_stamp;
  14. extern void load_pm_other(unsigned char* buf);
  15. /********************** 函数声明区 *************************/
  16. extern void send_motion_to_phone(uint8_t motion,uint16_t ts);
  17. void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
  18. {
  19. switch (p_event->evt_id)
  20. {
  21. case NRF_ESB_EVENT_TX_SUCCESS:
  22. // printf("T");
  23. break;
  24. case NRF_ESB_EVENT_TX_FAILED:
  25. // printf("F");
  26. // (void) nrf_esb_flush_tx();
  27. // (void) nrf_esb_start_tx();
  28. break;
  29. case NRF_ESB_EVENT_RX_RECEIVED:
  30. if (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS)
  31. {
  32. // app_uart_put('S');
  33. // printf("S");
  34. // nrf_gpio_pin_toggle(26);
  35. // for(int i=0;i<rx_payload.length;i++){
  36. // printf("%c", rx_payload.data[i]);
  37. // }
  38. s_rssi = rx_payload.rssi;
  39. switch (rx_payload.data[3])
  40. {
  41. case CMD_HEART:{
  42. uint8_t len = ~rx_payload.data[2];
  43. if(rx_payload.data[0]==0xAA && rx_payload.data[1]==len){
  44. // s_press_buf[2] = ((uint16_t)rx_payload.data[7]<<8)|((uint16_t)rx_payload.data[8]<<0);
  45. // s_press_buf[1] = ((uint16_t)rx_payload.data[9]<<8)|((uint16_t)rx_payload.data[10]<<0);
  46. // s_press_buf[0] = ((uint16_t)rx_payload.data[11]<<8)|((uint16_t)rx_payload.data[12]<<0);
  47. // s_press_dex = 2;
  48. s_press = ((uint16_t)rx_payload.data[4]<<8)|((uint16_t)rx_payload.data[5]<<0);
  49. s_pos[0] = ((int16_t)rx_payload.data[6]<<8)|((int16_t)rx_payload.data[7]<<0);
  50. s_pos[1] = ((int16_t)rx_payload.data[8]<<8)|((int16_t)rx_payload.data[9]<<0);
  51. s_pos[2] = ((int16_t)rx_payload.data[10]<<8)|((int16_t)rx_payload.data[11]<<0);
  52. s_att[0] = ((int16_t)rx_payload.data[12]<<8)|((int16_t)rx_payload.data[13]<<0);
  53. s_att[1] = ((int16_t)rx_payload.data[14]<<8)|((int16_t)rx_payload.data[15]<<0);
  54. s_att[2] = ((int16_t)rx_payload.data[16]<<8)|((int16_t)rx_payload.data[17]<<0);
  55. s_zupt = ((int16_t)rx_payload.data[18]<<0);
  56. }
  57. break;}
  58. case CMD_MOTION:
  59. {
  60. static uint16_t ts = 0;
  61. uint16_t t_temp = (((uint16_t)rx_payload.data[5]<<8) | ((uint16_t)rx_payload.data[6]<<0));
  62. if(ts != t_temp)
  63. {
  64. send_motion_to_phone(MOTION_RIGHT, time_stamp);
  65. ts = t_temp;
  66. gpio_mt_run(20);
  67. }
  68. }
  69. break;
  70. case CMD_UPDATE:
  71. switch(rx_payload.data[4])
  72. {
  73. case UPDATE_BASEINFO:
  74. {
  75. load_pm_other(rx_payload.data);
  76. }
  77. break;
  78. }
  79. break;
  80. // printf("P:%02X\n",s_press);
  81. }
  82. }
  83. break;
  84. }
  85. }
  86. #define ADDR 0xF2
  87. uint32_t bsp_esb_init(void)
  88. {
  89. uint32_t err_code;
  90. uint8_t base_addr_0[4] = {ADDR, ADDR, ADDR, ADDR};
  91. uint8_t base_addr_1[4] = {ADDR, ADDR, ADDR, ADDR};
  92. uint8_t addr_prefix[8] = {ADDR, ADDR, ADDR, ADDR, ADDR, ADDR, ADDR, ADDR };
  93. nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
  94. nrf_esb_config.payload_length = 32;
  95. nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
  96. nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
  97. nrf_esb_config.mode = NRF_ESB_MODE_PRX;
  98. nrf_esb_config.event_handler = nrf_esb_event_handler;
  99. nrf_esb_config.selective_auto_ack = false;
  100. err_code = nrf_esb_init(&nrf_esb_config);
  101. VERIFY_SUCCESS(err_code);
  102. err_code = nrf_esb_set_base_address_0(base_addr_0);
  103. VERIFY_SUCCESS(err_code);
  104. err_code = nrf_esb_set_base_address_1(base_addr_1);
  105. VERIFY_SUCCESS(err_code);
  106. err_code = nrf_esb_set_prefixes(addr_prefix, 8);
  107. VERIFY_SUCCESS(err_code);
  108. return err_code;
  109. }
  110. uint32_t bsp_esb_send(uint8_t * pdat, uint8_t len)
  111. {
  112. uint8_t i;
  113. if(len>ESP_MAX_DATA_LEN) return NRF_ERROR_DATA_SIZE;
  114. for(i=0;i<len;i++){
  115. tx_payload.data[i] = pdat[i];
  116. }
  117. tx_payload.noack = false;
  118. tx_payload.length = len;
  119. return nrf_esb_write_payload(&tx_payload);
  120. }