1.目的
nrf51822外部中断
2.分析
在实际应用中经常要用到外部中断,比如按键唤醒。
3.平台:
协议栈版本:SDK10.0.0
编译软件:keil 5.12
硬件平台:微雪开发板nrf51822
例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4
4.步骤
首先来分析下主要的代码
/**@brief Function for application main entry. */ int main(void) { uint32_t err_code; bool erase_bonds; // Initialize. app_trace_init(); timers_init(); buttons_leds_init(&erase_bonds); //按键函数初始化 ble_stack_init(); device_manager_init(erase_bonds); gap_params_init(); advertising_init(); services_init(); sensor_simulator_init(); conn_params_init(); // Start execution. application_timers_start(); err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); // Enter main loop. for (;;) { power_manage(); } }
/**@brief Function for initializing buttons and leds. * * @param[out] p_erase_bonds Will be true if the clear bonding button was pressed to wake the application up. */ static void buttons_leds_init(bool * p_erase_bonds) { bsp_event_t startup_event; //按键和灯初始化函数,APP_TIMER_TICKS(100, APP_TIMER_PRESCALER):消抖时间 bsp_event_handler:回调函数 uint32_t err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), bsp_event_handler); APP_ERROR_CHECK(err_code); err_code = bsp_btn_ble_init(NULL, &startup_event); APP_ERROR_CHECK(err_code); *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA); }
<pre name="code" class="cpp">uint32_t bsp_init(uint32_t type, uint32_t ticks_per_100ms, bsp_event_callback_t callback) { uint32_t err_code = NRF_SUCCESS; //判断是否定义了LEDS_UNMBER数量和 是否定义BSP_SIMPLE
#if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE) m_app_ticks_per_100ms = ticks_per_100ms; //消抖动时间赋值 m_indication_type = type;//事件类型 #else UNUSED_VARIABLE(ticks_per_100ms); #endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE) //判断是否定义了按键和是否定义BSP_SIMPLE #if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE) m_registered_callback = callback;//回调函数赋值 及bsp_event_handler // BSP will support buttons and generate events //判断是否按键事件 if (type & BSP_INIT_BUTTONS) { uint32_t num; //BUTTONS_NUMBER是定义了几个中断, 中断类型 回调函数初始化 for (num = 0; ((num < BUTTONS_NUMBER) && (err_code == NRF_SUCCESS)); num++) { err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_DEFAULT); } if (err_code == NRF_SUCCESS) { //app_buttons 中断表 BUTTONS_NUMBER:中断数目 ticks_per_100ms:消抖动时间 这里是初始化按键设置 err_code = app_button_init((app_button_cfg_t *)app_buttons, BUTTONS_NUMBER, ticks_per_100ms / 2); } if (err_code == NRF_SUCCESS) { //使能中断 err_code = app_button_enable(); } if (err_code == NRF_SUCCESS) { //建立m_button_timer_id事件 err_code = app_timer_create(&m_button_timer_id, APP_TIMER_MODE_SINGLE_SHOT, button_timer_handler); } } #elif (BUTTONS_NUMBER > 0) && (defined BSP_SIMPLE) if (type & BSP_INIT_BUTTONS) { uint32_t cnt; uint32_t buttons[] = BUTTONS_LIST; for (cnt = 0; cnt < BUTTONS_NUMBER; cnt++) { nrf_gpio_cfg_input(buttons[cnt], BUTTON_PULL); } } #endif // (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE) #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE) if (type & BSP_INIT_LED) { LEDS_OFF(LEDS_MASK); NRF_GPIO->DIRSET = LEDS_MASK; } // timers module must be already initialized! if (err_code == NRF_SUCCESS) { err_code = app_timer_create(&m_leds_timer_id, APP_TIMER_MODE_SINGLE_SHOT, leds_timer_handler); } if (err_code == NRF_SUCCESS) { err_code = app_timer_create(&m_alert_timer_id, APP_TIMER_MODE_REPEATED, alert_timer_handler); } #endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE) return err_code; }<span> </span>
<span style="font-family: Arial;"> </span>
<span style="font-family: Arial;"> </span>这里来分析下bsp_event_to_button_action_assign()函数, 其中uint32_t button 是数组m_events_list的下标。可见如果在次初始化的时候可能把 event给改变。
#ifndef BSP_SIMPLE /**@brief Assign specific event to button. */ uint32_t bsp_event_to_button_action_assign(uint32_t button, bsp_button_action_t action, bsp_event_t event) { uint32_t err_code = NRF_SUCCESS; #if BUTTONS_NUMBER > 0 if (button < BUTTONS_NUMBER) //判断是否定义了按键 { if (event == BSP_EVENT_DEFAULT) //判断事件是否BSP_EVENT_DEFAULT事件 { //找到定义的哪个按键的事件, // Setting default action: BSP_EVENT_KEY_x for PUSH actions, BSP_EVENT_NOTHING for RELEASE and LONG_PUSH actions. event = (action == BSP_BUTTON_ACTION_PUSH) ? (bsp_event_t)(BSP_EVENT_KEY_0 + button) : BSP_EVENT_NOTHING; } switch (action) { case BSP_BUTTON_ACTION_PUSH: m_events_list[button].push_event = event; //给m_events_list[button].push_event 赋值 break; case BSP_BUTTON_ACTION_LONG_PUSH: m_events_list[button].long_push_event = event; break; case BSP_BUTTON_ACTION_RELEASE: m_events_list[button].release_event = event; break; default: err_code = NRF_ERROR_INVALID_PARAM; break; } } else { err_code = NRF_ERROR_INVALID_PARAM; } #else err_code = NRF_ERROR_INVALID_PARAM; #endif // BUTTONS_NUMBER > 0 return err_code; } #endif // BSP_SIMPLE
为什么这里 event = (action == BSP_BUTTON_ACTION_PUSH) ? (bsp_event_t)(BSP_EVENT_KEY_0 + button) : BSP_EVENT_NOTHING;是这样 ,请看下面
typedef enum { BSP_EVENT_NOTHING = 0, /**< Assign this event to an action to prevent the action from generating an event (disable the action). */ BSP_EVENT_DEFAULT, /**< Assign this event to an action to assign the default event to the action. */ BSP_EVENT_CLEAR_BONDING_DATA, /**< Persistent bonding data should be erased. */ BSP_EVENT_CLEAR_ALERT, /**< An alert should be cleared. */ BSP_EVENT_DISCONNECT, /**< A link should be disconnected. */ BSP_EVENT_ADVERTISING_START, /**< The device should start advertising. */ BSP_EVENT_ADVERTISING_STOP, /**< The device should stop advertising. */ BSP_EVENT_WHITELIST_OFF, /**< The device should remove its advertising whitelist. */ BSP_EVENT_BOND, /**< The device should bond to the currently connected peer. */ BSP_EVENT_RESET, /**< The device should reset. */ BSP_EVENT_SLEEP, /**< The device should enter sleep mode. */ BSP_EVENT_WAKEUP, /**< The device should wake up from sleep mode. */ BSP_EVENT_DFU, /**< The device should enter DFU mode. */ BSP_EVENT_KEY_0, /**< Default event of the push action of BSP_BUTTON_0 (only if this button is present). */ BSP_EVENT_KEY_1, /**< Default event of the push action of BSP_BUTTON_1 (only if this button is present). */ BSP_EVENT_KEY_2, /**< Default event of the push action of BSP_BUTTON_2 (only if this button is present). */ BSP_EVENT_KEY_3, /**< Default event of the push action of BSP_BUTTON_3 (only if this button is present). */ BSP_EVENT_KEY_4, /**< Default event of the push action of BSP_BUTTON_4 (only if this button is present). */ BSP_EVENT_KEY_5, /**< Default event of the push action of BSP_BUTTON_5 (only if this button is present). */ BSP_EVENT_KEY_6, /**< Default event of the push action of BSP_BUTTON_6 (only if this button is present). */ BSP_EVENT_KEY_7, /**< Default event of the push action of BSP_BUTTON_7 (only if this button is present). */ BSP_EVENT_KEY_LAST = BSP_EVENT_KEY_7, } bsp_event_t;这样就可以找到对应的小标 事件的case事件
上面既是给对应的长按,释放,短按事件赋值对应的事件。。同一个m_events_list只能同时有一个赋值。
配置端口:
app_button_init((app_button_cfg_t *)app_buttons,
BUTTONS_NUMBER,
ticks_per_100ms / 2);
BUTTONS_NUMBER:设置按键的个数,
ticks_per_100ms / 2 :消痘痘事件
app_buttons参数,游侠可见,需要配置哪个端口,低电平(false)还是高电平(ture)有效,管脚模式(上下拉等等),对应的回调函数
#ifndef BSP_SIMPLE static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] = { #ifdef BSP_BUTTON_0 {BSP_BUTTON_0, false, BUTTON_PULL, bsp_button_event_handler}, #endif // BUTTON_0 #ifdef BSP_BUTTON_1 {BSP_BUTTON_1, false, BUTTON_PULL, bsp_button_event_handler}, #endif // BUTTON_1 #ifdef BSP_BUTTON_2 {BSP_BUTTON_2, false, BUTTON_PULL, bsp_button_event_handler}, #endif // BUTTON_2 #ifdef BSP_BUTTON_3 {BSP_BUTTON_3, false, BUTTON_PULL, bsp_button_event_handler}, #endif // BUTTON_3 #ifdef BSP_BUTTON_4 {BSP_BUTTON_4, false, BUTTON_PULL, bsp_button_event_handler}, #endif // BUTTON_4 #ifdef BSP_BUTTON_5 {BSP_BUTTON_5, false, BUTTON_PULL, bsp_button_event_handler}, #endif // BUTTON_5 #ifdef BSP_BUTTON_6 {BSP_BUTTON_6, false, BUTTON_PULL, bsp_button_event_handler}, #endif // BUTTON_6 #ifdef BSP_BUTTON_7 {BSP_BUTTON_7, false, BUTTON_PULL, bsp_button_event_handler}, #endif // BUTTON_7 }; #endif // BSP_SIMPLE #endif // BUTTONS_NUMBER > 0
err_code = app_timer_create(&m_button_timer_id,
APP_TIMER_MODE_SINGLE_SHOT,
button_timer_handler);
每次中断来了就会跳到:
void GPIOTE_IRQHandler(void) 中断入口函数,然后跳到
#if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE) /**@brief Function for handling button events. * * @param[in] pin_no The pin number of the button pressed. * @param[in] button_action Action button. */ static void bsp_button_event_handler(uint8_t pin_no, uint8_t button_action) { bsp_event_t event = BSP_EVENT_NOTHING; uint32_t button = 0; uint32_t err_code; static uint8_t current_long_push_pin_no; /**< Pin number of a currently pushed button, that could become a long push if held long enough. */ static bsp_event_t release_event_at_push[BUTTONS_NUMBER]; /**< Array of what the release event of each button was last time it was pushed, so that no release event is sent if the event was bound after the push of the button. */ while ((button < BUTTONS_NUMBER) && (m_buttons_list[button] != pin_no)) { button++; } if (button < BUTTONS_NUMBER) { switch(button_action) { case APP_BUTTON_PUSH: event = m_events_list[button].push_event; if (m_events_list[button].long_push_event != BSP_EVENT_NOTHING) { err_code = app_timer_start(m_button_timer_id, BSP_MS_TO_TICK(BSP_LONG_PUSH_TIMEOUT_MS), (void*)¤t_long_push_pin_no); if (err_code == NRF_SUCCESS) { current_long_push_pin_no = pin_no; } } release_event_at_push[button] = m_events_list[button].release_event; break; case APP_BUTTON_RELEASE: (void)app_timer_stop(m_button_timer_id); if (release_event_at_push[button] == m_events_list[button].release_event) { event = m_events_list[button].release_event; } break; case BSP_BUTTON_ACTION_LONG_PUSH: event = m_events_list[button].long_push_event; } } if ((event != BSP_EVENT_NOTHING) && (m_registered_callback != NULL)) { m_registered_callback(event); } }
如果是长按, event = m_events_list[button].push_event; event是0(即BSP_EVENT_NOTHING),-->m_events_list[button].long_push_event不等于BSP_EVENT_NOTHING,然后会产生长按扫描事件m_button_timer_id-->赋值释放事件,如果长按没有到设定的时间则产生释放按键事件,停止app_timer_stop(m_button_timer_id);
注意:BSP_MS_TO_TICK(BSP_LONG_PUSH_TIMEOUT_MS)是长按的时间,设置这个参数可以改变长按的事件。
如果是短接事件, event = m_events_list[button].push_event; 不为0为定义的事件,-->m_events_list[button].long_push_event 等于 BSP_EVENT_NOTHING-->赋值释放按键事件。
m_registered_callback(event); 这个函数即:bsp_event_handler函数-,(请查阅上面 bsp_init()函数)
长按事件,时间到了,产生button_timer_handler事件
/**@brief Handle events from button timer. * * @param[in] p_context parameter registered in timer start function. */ static void button_timer_handler(void * p_context) { bsp_button_event_handler(*(uint8_t *)p_context, BSP_BUTTON_ACTION_LONG_PUSH); }
就是产生对应的长按事件。
注意:启动长按扫描的时候err_code = app_timer_start(m_button_timer_id, BSP_MS_TO_TICK(BSP_LONG_PUSH_TIMEOUT_MS), (void*)¤t_long_push_pin_no);
current_long_push_pin_no 这个参数也传进去了,这个就是哪个管脚。方便产生长按超时的时候把哪个长按参数传进去,
看代码:bsp_init()函数
for (num = 0; ((num < BUTTONS_NUMBER) && (err_code == NRF_SUCCESS)); num++) { err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_DEFAULT); }
函数bsp_event_to_button_action_assign()里面
if (event == BSP_EVENT_DEFAULT) { // Setting default action: BSP_EVENT_KEY_x for PUSH actions, BSP_EVENT_NOTHING for RELEASE and LONG_PUSH actions. event = (action == BSP_BUTTON_ACTION_PUSH) ? (bsp_event_t)(BSP_EVENT_KEY_0 + button) : BSP_EVENT_NOTHING; }可以看到只有当设置为BSP_BUTTON_ACTION_PUSH 和BSP_EVENT_DEFAULT的时候,event才有值不然为0.那假如要设置长按怎么办呢?