123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /********************** 头文件 *************************/
- #include "bsp_esb.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;
- int8_t s_rssi;
- volatile uint16_t s_press_buf[3];
- volatile uint16_t s_press_dex = 0;
- volatile uint16_t s_press = 0;
- volatile int16_t s_pos[3];
- volatile int16_t s_att[3];
- volatile int16_t s_zupt;
- extern int time_stamp;
- extern void load_pm_other(unsigned char* buf);
- /********************** 函数声明区 *************************/
- extern void send_motion_to_phone(uint8_t motion,uint16_t ts);
- void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
- {
- switch (p_event->evt_id)
- {
- case NRF_ESB_EVENT_TX_SUCCESS:
- // printf("T");
- break;
- case NRF_ESB_EVENT_TX_FAILED:
- // printf("F");
- // (void) nrf_esb_flush_tx();
- // (void) nrf_esb_start_tx();
- break;
- case NRF_ESB_EVENT_RX_RECEIVED:
-
- if (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS)
- {
- // app_uart_put('S');
- // printf("S");
- // nrf_gpio_pin_toggle(26);
- // for(int i=0;i<rx_payload.length;i++){
- // printf("%c", rx_payload.data[i]);
- // }
-
- s_rssi = rx_payload.rssi;
-
- switch (rx_payload.data[3])
- {
- case CMD_HEART:{
- uint8_t len = ~rx_payload.data[2];
- if(rx_payload.data[0]==0xAA && rx_payload.data[1]==len){
- // s_press_buf[2] = ((uint16_t)rx_payload.data[7]<<8)|((uint16_t)rx_payload.data[8]<<0);
- // s_press_buf[1] = ((uint16_t)rx_payload.data[9]<<8)|((uint16_t)rx_payload.data[10]<<0);
- // s_press_buf[0] = ((uint16_t)rx_payload.data[11]<<8)|((uint16_t)rx_payload.data[12]<<0);
- // s_press_dex = 2;
- s_press = ((uint16_t)rx_payload.data[4]<<8)|((uint16_t)rx_payload.data[5]<<0);
- s_pos[0] = ((int16_t)rx_payload.data[6]<<8)|((int16_t)rx_payload.data[7]<<0);
- s_pos[1] = ((int16_t)rx_payload.data[8]<<8)|((int16_t)rx_payload.data[9]<<0);
- s_pos[2] = ((int16_t)rx_payload.data[10]<<8)|((int16_t)rx_payload.data[11]<<0);
- s_att[0] = ((int16_t)rx_payload.data[12]<<8)|((int16_t)rx_payload.data[13]<<0);
- s_att[1] = ((int16_t)rx_payload.data[14]<<8)|((int16_t)rx_payload.data[15]<<0);
- s_att[2] = ((int16_t)rx_payload.data[16]<<8)|((int16_t)rx_payload.data[17]<<0);
- s_zupt = ((int16_t)rx_payload.data[18]<<0);
-
- }
- break;}
-
- case CMD_MOTION:
- {
- static uint16_t ts = 0;
- uint16_t t_temp = (((uint16_t)rx_payload.data[5]<<8) | ((uint16_t)rx_payload.data[6]<<0));
-
- if(ts != t_temp)
- {
- send_motion_to_phone(MOTION_RIGHT, time_stamp);
- ts = t_temp;
- gpio_mt_run(20);
- }
- }
- break;
-
- case CMD_UPDATE:
- switch(rx_payload.data[4])
- {
- case UPDATE_BASEINFO:
- {
- load_pm_other(rx_payload.data);
- }
- break;
- }
- break;
- // printf("P:%02X\n",s_press);
- }
- }
- break;
- }
- }
- #define ADDR 0xF2
- 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 = 32;
- nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
- nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
- nrf_esb_config.mode = NRF_ESB_MODE_PRX;
- 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);
- return err_code;
- }
- uint32_t bsp_esb_send(uint8_t * pdat, uint8_t len)
- {
- uint8_t i;
- if(len>ESP_MAX_DATA_LEN) return NRF_ERROR_DATA_SIZE;
- for(i=0;i<len;i++){
- tx_payload.data[i] = pdat[i];
- }
- tx_payload.noack = false;
- tx_payload.length = len;
- return nrf_esb_write_payload(&tx_payload);
- }
|