在51822这个芯片的协议栈初始化函数ble_stack_init里的ble_evt_dispatch和sys_evt_dispatch这两个函数注册了这两个事件的回调函数,代码如下:
/**@brief Function for initializing the BLE stack.
*
* @Details Initializes the SoftDevice and the BLE event interrupt.
*/
static void ble_stack_init(void)
{
uint32_t err_code;
// Initialize the SoftDevice handler module.
SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
// Register with the SoftDevice handler module for BLE events.
err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
APP_ERROR_CHECK(err_code);
// Register with the SoftDevice handler module for BLE events.
err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
APP_ERROR_CHECK(err_code);
}
先看softdevice_sys_evt_handler_set系统回调函数:
uint32_t softdevice_sys_evt_handler_set(sys_evt_handler_t sys_evt_handler)
{
if (sys_evt_handler == NULL)
{
return NRF_ERROR_NULL;
}
m_sys_evt_handler = sys_evt_handler;
return NRF_SUCCESS;
}
这里就是初始化m_sys_evt_handler变量,该变量在intern_softdevice_events_execute这个函数使用,代码如下:
void intern_softdevice_events_execute(void)
{
if (!m_softdevice_enabled)
{
// SoftDevice not enabled. This can be possible if the SoftDevice was enabled by the
// application without using this module's API (i.e softdevice_handler_init)
return;
}
bool no_more_soc_evts = (m_sys_evt_handler == NULL);
#ifdef BLE_STACK_SUPPORT_REQD
bool no_more_ble_evts = (m_ble_evt_handler == NULL);
#endif
#ifdef ANT_STACK_SUPPORT_REQD
bool no_more_ant_evts = (m_ant_evt_handler == NULL);
#endif
for (;;)
{
uint32_t err_code;
if (!no_more_soc_evts)
{
uint32_t evt_id;
// Pull event from SOC.
err_code = sd_evt_get(&evt_id);
if (err_code == NRF_ERROR_NOT_FOUND)
{
no_more_soc_evts = true;
}
else if (err_code != NRF_SUCCESS)
{
APP_ERROR_HANDLER(err_code);
}
else
{
// Call application's SOC event handler.
m_sys_evt_handler(evt_id);
}
}
#ifdef BLE_STACK_SUPPORT_REQD
// Fetch BLE Events.
if (!no_more_ble_evts)
{
// Pull event from stack
uint16_t evt_len = m_ble_evt_buffer_size;
err_code = sd_ble_evt_get(m_evt_buffer, &evt_len);
if (err_code == NRF_ERROR_NOT_FOUND)
{
no_more_ble_evts = true;
}
else if (err_code != NRF_SUCCESS)
{
APP_ERROR_HANDLER(err_code);
}
else
{
// Call application's BLE stack event handler.
m_ble_evt_handler((ble_evt_t *)m_evt_buffer);
}
}
#endif
#ifdef ANT_STACK_SUPPORT_REQD
// Fetch ANT Events.
if (!no_more_ant_evts)
{
// Pull event from stack
err_code = sd_ant_event_get(&((ant_evt_t *)m_evt_buffer)->channel,
&((ant_evt_t *)m_evt_buffer)->event,
((ant_evt_t *)m_evt_buffer)->evt_buffer);
if (err_code == NRF_ERROR_NOT_FOUND)
{
no_more_ant_evts = true;
}
else if (err_code != NRF_SUCCESS)
{
APP_ERROR_HANDLER(err_code);
}
else
{
// Call application's ANT stack event handler.
m_ant_evt_handler((ant_evt_t *)m_evt_buffer);
}
}
#endif
if (no_more_soc_evts)
{
// There are no remaining System (SOC) events to be fetched from the SoftDevice.
#if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
// Check if there are any remaining BLE and ANT events.
if (no_more_ble_evts && no_more_ant_evts)
{
break;
}
#elif defined(BLE_STACK_SUPPORT_REQD)
// Check if there are any remaining BLE events.
if (no_more_ble_evts)
{
break;
}
#elif defined(ANT_STACK_SUPPORT_REQD)
// Check if there are any remaining ANT events.
if (no_more_ant_evts)
{
break;
}
#else
// No need to check for BLE or ANT events since there is no support for BLE and ANT
// required.
break;
#endif
}
}
}
对于系统时间的关键代码是这样的:
if (!no_more_soc_evts)
{
uint32_t evt_id;
// Pull event from SOC.
err_code = sd_evt_get(&evt_id);
if (err_code == NRF_ERROR_NOT_FOUND)
{
no_more_soc_evts = true;
}
else if (err_code != NRF_SUCCESS)
{
APP_ERROR_HANDLER(err_code);
}
else
{
// Call application's SOC event handler.
m_sys_evt_handler(evt_id);
}
类似的对于BLE时间的关键代码如下:
if (!no_more_ble_evts)
{
// Pull event from stack
uint16_t evt_len = m_ble_evt_buffer_size;
err_code = sd_ble_evt_get(m_evt_buffer, &evt_len);
if (err_code == NRF_ERROR_NOT_FOUND)
{
no_more_ble_evts = true;
}
else if (err_code != NRF_SUCCESS)
{
APP_ERROR_HANDLER(err_code);
}
else
{
// Call application's BLE stack event handler.
m_ble_evt_handler((ble_evt_t *)m_evt_buffer);
}
}
那么他们的不同就在于sd_evt_get和sd_ble_evt_get,在手册中对sd_evt_get解释如下:
uint32_t sd_evt_get | ( | uint32_t * | p_evt_id | ) |
Gets any pending events generated by the SoC API.
The application should keep calling this function to get events, until NRF_ERROR_NOT_FOUND is returned.
Parameters
[out] | p_evt_id | Set to one of the values in NRF_SOC_EVTS, if any events are pending. |
Return values
NRF_SUCCESS | An event was pending. The event id is written in the p_evt_id parameter. |
NRF_ERROR_NOT_FOUND | No pending events. |
有这个提示再看看SoC API:
FunctionsSoC Library API
Functions | |
uint32_t
|
sd_mutex_new (nrf_mutex_t *p_mutex)
|
Initialize a mutex. More... | |
uint32_t
|
sd_mutex_acquire (nrf_mutex_t *p_mutex)
|
Attempt to acquire a mutex. More... | |
uint32_t
|
sd_mutex_release (nrf_mutex_t *p_mutex)
|
Release a mutex. More... | |
uint32_t
|
sd_nvic_EnableIRQ (IRQn_Type IRQn)
|
Enable External Interrupt. More... | |
uint32_t
|
sd_nvic_DisableIRQ (IRQn_Type IRQn)
|
Disable External Interrupt. More... | |
uint32_t
|
sd_nvic_GetPendingIRQ (IRQn_Type IRQn, uint32_t *p_pending_irq)
|
Get Pending Interrupt. More... | |
uint32_t
|
sd_nvic_SetPendingIRQ (IRQn_Type IRQn)
|
Set Pending Interrupt. More... | |
uint32_t
|
sd_nvic_ClearPendingIRQ (IRQn_Type IRQn)
|
Clear Pending Interrupt. More... | |
uint32_t
|
sd_nvic_SetPriority (IRQn_Type IRQn,nrf_app_irq_priority_t priority)
|
Set Interrupt Priority. More... | |
uint32_t
|
sd_nvic_GetPriority (IRQn_Type IRQn,nrf_app_irq_priority_t *p_priority)
|
Get Interrupt Priority. More... | |
uint32_t
|
sd_nvic_SystemReset (void)
|
System Reset. More... | |
uint32_t
|
sd_nvic_critical_region_enter (uint8_t *p_is_nested_critical_region)
|
Enters critical region. More... | |
uint32_t
|
sd_nvic_critical_region_exit (uint8_t is_nested_critical_region)
|
Exit critical region. More... | |
uint32_t
|
sd_rand_application_pool_capacity_get (uint8_t *p_pool_capacity)
|
Query the capacity of the application random pool. More... | |
uint32_t
|
sd_rand_application_bytes_available_get (uint8_t *p_bytes_available)
|
Get number of random bytes available to the application. More... | |
uint32_t
|
sd_rand_application_vector_get (uint8_t *p_buff, uint8_t length)
|
Get random bytes from the application pool. More... | |
uint32_t
|
sd_power_reset_reason_get (uint32_t *p_reset_reason)
|
Gets the reset reason register. More... | |
uint32_t
|
sd_power_reset_reason_clr (uint32_t reset_reason_clr_msk)
|
Clears the bits of the reset reason register. More... | |
uint32_t
|
sd_power_mode_set (nrf_power_mode_t power_mode)
|
Sets the power mode when in CPU sleep. More... | |
uint32_t
|
sd_power_system_off (void)
|
从这里就可以看出系统事件就是那些和BLE没有关系的是关于芯片中断的时间,比如NVIC和电源之类,BLE事件就是协议栈相关的事件,比如蓝牙发现等等