实现参考相关文章:
https://blog.csdn.net/wuwovicky/article/details/88675021
https://www.docin.com/p-2116477780.html
基于最新SDK有几个相关文件需要修改如下:
1、timeslot.c文件修改:
#include <stdint.h>#include <stdbool.h>#include "nrf.h"#include "app_error.h"#include "nrf_gpio.h"#include "boards.h"#include "nrf_sdh.h" //新的SDK采用 nrf_sdh.h#include "my_esb.h"#include "nrf_esb.h"/**Constants for timeslot jad/static nrf_radio_request_t m_timeslot_request;static uint32_t m_slot_length;static nrf_radio_signal_callback_return_param_t signal_callback_return_param;/**@brief Request next timeslot event in earliest configuration */uint32_t request_next_event_earliest(void){ m_slot_length = 10000; m_timeslot_request.request_type = NRF_RADIO_REQ_TYPE_EARLIEST; m_timeslot_request.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED; m_timeslot_request.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL; m_timeslot_request.params.earliest.length_us = m_slot_length; m_timeslot_request.params.earliest.timeout_us = 1000000; return sd_radio_request(&m_timeslot_request);}/**@brief Configure next timeslot event in earliest configuration */void configure_next_event_earliest(void){ m_slot_length = 10000; m_timeslot_request.request_type = NRF_RADIO_REQ_TYPE_EARLIEST; m_timeslot_request.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED; m_timeslot_request.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL; m_timeslot_request.params.earliest.length_us = m_slot_length; m_timeslot_request.params.earliest.timeout_us = 1000000;}/**@brief Configure next timeslot event in normal configuration */void configure_next_event_normal(void){ m_slot_length = 15000; m_timeslot_request.request_type = NRF_RADIO_REQ_TYPE_NORMAL; m_timeslot_request.params.normal.hfclk = NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED; m_timeslot_request.params.normal.priority = NRF_RADIO_PRIORITY_HIGH; m_timeslot_request.params.normal.distance_us = 100000; m_timeslot_request.params.normal.length_us = m_slot_length;}/**@brief Timeslot signal handler */void nrf_evt_signal_handler(uint32_t evt_id){ uint32_t err_code; switch (evt_id) { case NRF_EVT_RADIO_SIGNAL_CALLBACK_INVALID_RETURN: //No implementation needed break; case NRF_EVT_RADIO_SESSION_IDLE://sd_radio_session_close(); //No implementation needed break; case NRF_EVT_RADIO_SESSION_CLOSED: //No implementation needed, session ended break; case NRF_EVT_RADIO_BLOCKED: //Fall through case NRF_EVT_RADIO_CANCELED: err_code = request_next_event_earliest(); APP_ERROR_CHECK(err_code); break; default: break; }}/**@brief Timeslot event handler */nrf_radio_signal_callback_return_param_t * radio_callback(uint8_t signal_type){ switch(signal_type) { case NRF_RADIO_CALLBACK_SIGNAL_TYPE_START:printf("NRF_RADIO_CALLBACK_SIGNAL_TYPE_STARTrn"); //Start of the timeslot - set up timer interrupt signal_callback_return_param.params.request.p_next = NULL; signal_callback_return_param.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_NONE; NRF_TIMER0->INTENSET = TIMER_INTENSET_COMPARE0_Msk; NRF_TIMER0->CC[0] = m_slot_length - 500;//射频这部分一定要打开,不然rf发送不成功 NRF_RADIO->POWER = (RADIO_POWER_POWER_Enabled << RADIO_POWER_POWER_Pos); NVIC_EnableIRQ(TIMER0_IRQn); nrf_gpio_pin_toggle(20); //Toggle LED4esb_init();esb_test_tx(); break; case NRF_RADIO_CALLBACK_SIGNAL_TYPE_RADIO:RADIO刻苦的路灯();printf("NRF_RADIO_CALLBACK_SIGNAL_TYPE_RADIOrn"); signal_callback_return_param.params.request.p_next = NULL; signal_callback_return_param.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_NONE; break; case NRF_RADIO_CALLBACK_SIGNAL_TYPE_TIMER0: printf("NRF_RADIO_CALLBACK_SIGNAL_TYPE_TIMER0rn"); //Timer interrupt - do graceful shutdown - schedule next timeslot configure_next_event_normal(); signal_callback_return_param.params.request.p_next = &m_timeslot_request; signal_callback_return_param.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_REQUEST_AND_END;//发送完清除 NRF_RADIO->POWER = (RADIO_POWER_POWER_Disabled << RADIO_POWER_POWER_Pos); break; case NRF_RADIO_CALLBACK_SIGNAL_TYPE_EXTEND_SUCCEEDED: printf("NRF_RADIO_CALLBACK_SIGNAL_TYPE_EXTEND_SUCCEEDEDrn"); //No implementation needed break; case NRF_RADIO_CALLBACK_SIGNAL_TYPE_EXTEND_FAILED:printf("NRF_RADIO_CALLBACK_SIGNAL_TYPE_EXTEND_FAILEDrn"); //Try scheduling a new timeslot configure_next_event_earliest(); signal_callback_return_param.params.request.p_next = &m_timeslot_request; signal_callback_return_param.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_REQUEST_AND_END; break; default: //No implementation needed break; } return (&signal_callback_return_param);}/**@brief Function for initializing the timeslot API. */uint32_t timeslot_sd_init(void){ uint32_t err_code; err_code = sd_radio_session_open(radio_callback); if (err_code != NRF_SUCCESS) { return err_code; } err_code = request_next_event_earliest(); if (err_code != NRF_SUCCESS) { (void)sd_radio_session_close(); return err_code; } nrf_gpio_cfg_output(20); return NRF_SUCCESS;}2、main函数需要修改:
准备两块开发板:
1、一块开发板,裸机烧录官方历程esb
2、第二块开发板烧录带蓝牙协议栈的,移植好timeslot的程序(我是基于ble串口历程),如下:
3、实验现象:
既可以正常广播蓝牙数据,也可以实现接收开发板的流水灯效果。