nRF52832 — 外部中断BSP(Board Support Package)

原文链接:http://blog.csdn.net/a369000753/article/details/51312839(转载文章,若有不妥,通知后我会立即删除)

首先来分析下主要的代码

[cpp]  view plain  copy
  1. /**@brief Function for application main entry. 
  2.  */  
  3. int main(void)  
  4. {  
  5.     uint32_t err_code;  
  6.     bool erase_bonds;  
  7.   
  8.     // Initialize.  
  9.     app_trace_init();  
  10.     timers_init();      buttons_leds_init(&erase_bonds); //按键函数初始化  
  11.     ble_stack_init();  
  12.     device_manager_init(erase_bonds);  
  13.     gap_params_init();  
  14.     advertising_init();  
  15.     services_init();  
  16.     sensor_simulator_init();  
  17.     conn_params_init();  
  18.   
  19.     // Start execution.  
  20.     application_timers_start();  
  21.     err_code = ble_advertising_start(BLE_ADV_MODE_FAST);  
  22.     APP_ERROR_CHECK(err_code);  
  23.   
  24.     // Enter main loop.  
  25.     for (;;)  
  26.     {  
  27.         power_manage();  
  28.     }  
  29. }  
[cpp]  view plain  copy
  1. /**@brief Function for initializing buttons and leds. 
  2.  * 
  3.  * @param[out] p_erase_bonds  Will be true if the clear bonding button was pressed to wake the application up. 
  4.  */  
  5. static void buttons_leds_init(bool * p_erase_bonds)  
  6. {  
  7.     bsp_event_t startup_event;  
  8.     //按键和灯初始化函数,APP_TIMER_TICKS(100, APP_TIMER_PRESCALER):消抖时间 bsp_event_handler:回调函数  
  9.     uint32_t err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS,  
  10.                                  APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),   
  11.                                  bsp_event_handler);  
  12.     APP_ERROR_CHECK(err_code);  
  13.   
  14.     err_code = bsp_btn_ble_init(NULL, &startup_event);  
  15.     APP_ERROR_CHECK(err_code);  
  16.   
  17.     *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);  
  18. }  
[cpp]  view plain  copy
  1. <pre name="code" class="cpp">uint32_t bsp_init(uint32_t type, uint32_t ticks_per_100ms, bsp_event_callback_t callback)  
  2. {  
  3.     uint32_t err_code = NRF_SUCCESS;  
  4. //判断是否定义了LEDS_UNMBER数量和 是否定义BSP_SIMPLE  
[cpp]  view plain  copy
  1. #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)  
  2.     m_app_ticks_per_100ms = ticks_per_100ms; //消抖动时间赋值  
  3.     m_indication_type     = type;//事件类型  
  4. #else  
  5.     UNUSED_VARIABLE(ticks_per_100ms);  
  6. #endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)  
  7. //判断是否定义了按键和是否定义BSP_SIMPLE  
  8. #if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)  
  9.     m_registered_callback = callback;//回调函数赋值 及bsp_event_handler  
  10.   
  11.     // BSP will support buttons and generate events  //判断是否按键事件  
  12.     if (type & BSP_INIT_BUTTONS)   {  
  13.         uint32_t num;  
  14.         //BUTTONS_NUMBER是定义了几个中断, 中断类型 回调函数初始化  
  15.         for (num = 0; ((num < BUTTONS_NUMBER) && (err_code == NRF_SUCCESS)); num++)  
  16.         {  
  17.             err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_DEFAULT);  
  18.         }  
  19.   
  20.         if (err_code == NRF_SUCCESS)  
  21.         {    //app_buttons 中断表  BUTTONS_NUMBER:中断数目 ticks_per_100ms:消抖动时间  这里是初始化按键设置  
  22.             err_code = app_button_init((app_button_cfg_t *)app_buttons,  
  23.                                        BUTTONS_NUMBER,  
  24.                                        ticks_per_100ms / 2);  
  25.         }  
  26.   
  27.         if (err_code == NRF_SUCCESS)  
  28.         {  //使能中断  
  29.             err_code = app_button_enable();  
  30.         }  
  31.         if (err_code == NRF_SUCCESS)  
  32.         {  //建立m_button_timer_id事件  
  33.             err_code = app_timer_create(&m_button_timer_id,  
  34.                                         APP_TIMER_MODE_SINGLE_SHOT,  
  35.                                         button_timer_handler);  
  36.         }  
  37.     }  
  38. #elif (BUTTONS_NUMBER > 0) && (defined BSP_SIMPLE)  
  39.   
  40.     if (type & BSP_INIT_BUTTONS)  
  41.     {  
  42.         uint32_t cnt;  
  43.         uint32_t buttons[] = BUTTONS_LIST;  
  44.         for (cnt = 0; cnt < BUTTONS_NUMBER; cnt++)  
  45.         {  
  46.             nrf_gpio_cfg_input(buttons[cnt], BUTTON_PULL);  
  47.         }  
  48.     }  
  49. #endif // (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)  
  50.   
  51. #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)  
  52.   
  53.     if (type & BSP_INIT_LED)  
  54.     {  
  55.         LEDS_OFF(LEDS_MASK);  
  56.         NRF_GPIO->DIRSET = LEDS_MASK;  
  57.     }  
  58.   
  59.     // timers module must be already initialized!  
  60.     if (err_code == NRF_SUCCESS)  
  61.     {  
  62.         err_code =  
  63.             app_timer_create(&m_leds_timer_id, APP_TIMER_MODE_SINGLE_SHOT, leds_timer_handler);  
  64.     }  
  65.   
  66.     if (err_code == NRF_SUCCESS)  
  67.     {  
  68.         err_code =  
  69.             app_timer_create(&m_alert_timer_id, APP_TIMER_MODE_REPEATED, alert_timer_handler);  
  70.     }  
  71. #endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)  
  72.   
  73.     return err_code;  
  74. }<span>   </span>  
[cpp]  view plain  copy
  1. <span style="font-family: Arial;">    </span>  
[cpp]  view plain  copy
  1. <span style="font-family: Arial;">                                        </span>  

 这里来分析下bsp_event_to_button_action_assign()函数,其中uint32_t button 是数组m_events_list的下标。可见如果在次初始化的时候可能把 event给改变。

[cpp]  view plain  copy
  1. #ifndef BSP_SIMPLE  
  2. /**@brief Assign specific event to button. 
  3.  */  
  4. uint32_t bsp_event_to_button_action_assign(uint32_t button, bsp_button_action_t action, bsp_event_t event)  
  5. {  
  6.     uint32_t err_code = NRF_SUCCESS;  
  7.   
  8. #if BUTTONS_NUMBER > 0  
  9.     if (button < BUTTONS_NUMBER) //判断是否定义了按键  
  10.     {  
  11.         if (event == BSP_EVENT_DEFAULT) //判断事件是否BSP_EVENT_DEFAULT事件  
  12.         {  //找到定义的哪个按键的事件,  
  13.             // Setting default action: BSP_EVENT_KEY_x for PUSH actions, BSP_EVENT_NOTHING for RELEASE and LONG_PUSH actions.  
  14.             event = (action == BSP_BUTTON_ACTION_PUSH) ? (bsp_event_t)(BSP_EVENT_KEY_0 + button) : BSP_EVENT_NOTHING;  
  15.         }   
  16.         switch (action)  
  17.         {            case BSP_BUTTON_ACTION_PUSH:  
  18.                 m_events_list[button].push_event = event; //给m_events_list[button].push_event 赋值  
  19.                 break;  
  20.             case BSP_BUTTON_ACTION_LONG_PUSH:  
  21.                 m_events_list[button].long_push_event = event;  
  22.                 break;  
  23.             case BSP_BUTTON_ACTION_RELEASE:  
  24.                 m_events_list[button].release_event = event;  
  25.                 break;  
  26.             default:  
  27.                 err_code = NRF_ERROR_INVALID_PARAM;  
  28.                 break;  
  29.         }  
  30.     }  
  31.     else  
  32.     {  
  33.         err_code = NRF_ERROR_INVALID_PARAM;  
  34.     }  
  35. #else  
  36.     err_code = NRF_ERROR_INVALID_PARAM;  
  37. #endif // BUTTONS_NUMBER > 0  
  38.   
  39.     return err_code;  
  40. }  
  41.   
  42. #endif // BSP_SIMPLE  

为什么这里 event = (action == BSP_BUTTON_ACTION_PUSH) ? (bsp_event_t)(BSP_EVENT_KEY_0 + button) : BSP_EVENT_NOTHING;是这样 ,请看下面

[cpp]  view plain  copy
  1. typedef enum  
  2. {  
  3.     BSP_EVENT_NOTHING = 0,                  /**< Assign this event to an action to prevent the action from generating an event (disable the action). */  
  4.     BSP_EVENT_DEFAULT,                      /**< Assign this event to an action to assign the default event to the action. */  
  5.     BSP_EVENT_CLEAR_BONDING_DATA,           /**< Persistent bonding data should be erased. */  
  6.     BSP_EVENT_CLEAR_ALERT,                  /**< An alert should be cleared. */  
  7.     BSP_EVENT_DISCONNECT,                   /**< A link should be disconnected. */  
  8.     BSP_EVENT_ADVERTISING_START,            /**< The device should start advertising. */  
  9.     BSP_EVENT_ADVERTISING_STOP,             /**< The device should stop advertising. */  
  10.     BSP_EVENT_WHITELIST_OFF,                /**< The device should remove its advertising whitelist. */                                                       BSP_EVENT_BOND,                         /**< The device should bond to the currently connected peer. */   
  11.     BSP_EVENT_RESET,                        /**< The device should reset. */  
  12.     BSP_EVENT_SLEEP,                        /**< The device should enter sleep mode. */  
  13.     BSP_EVENT_WAKEUP,                       /**< The device should wake up from sleep mode. */  
  14.     BSP_EVENT_DFU,                          /**< The device should enter DFU mode. */  
  15.     BSP_EVENT_KEY_0,                        /**< Default event of the push action of BSP_BUTTON_0 (only if this button is present). */  
  16.     BSP_EVENT_KEY_1,                        /**< Default event of the push action of BSP_BUTTON_1 (only if this button is present). */  
  17.     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). */  
  18.     BSP_EVENT_KEY_4,                        /**< Default event of the push action of BSP_BUTTON_4 (only if this button is present). */  
  19.     BSP_EVENT_KEY_5,                        /**< Default event of the push action of BSP_BUTTON_5 (only if this button is present). */  
  20.     BSP_EVENT_KEY_6,                        /**< Default event of the push action of BSP_BUTTON_6 (only if this button is present). */  
  21.     BSP_EVENT_KEY_7,                        /**< Default event of the push action of BSP_BUTTON_7 (only if this button is present). */  
  22.     BSP_EVENT_KEY_LAST = BSP_EVENT_KEY_7,  
  23. } 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)有效,管脚模式(上下拉等等),对应的回调函数

[cpp]  view plain  copy
  1. #ifndef BSP_SIMPLE  
  2. static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] =  
  3. {  
  4.     #ifdef BSP_BUTTON_0  
  5.     {BSP_BUTTON_0, false, BUTTON_PULL, bsp_button_event_handler},  
  6.     #endif // BUTTON_0  
  7.   
  8.     #ifdef BSP_BUTTON_1  
  9.     {BSP_BUTTON_1, false, BUTTON_PULL, bsp_button_event_handler},  
  10.     #endif // BUTTON_1  
  11.   
  12.     #ifdef BSP_BUTTON_2  
  13.     {BSP_BUTTON_2, false, BUTTON_PULL, bsp_button_event_handler},  
  14.     #endif // BUTTON_2  
  15.   
  16.     #ifdef BSP_BUTTON_3  
  17.     {BSP_BUTTON_3, false, BUTTON_PULL, bsp_button_event_handler},  
  18.     #endif // BUTTON_3  
  19.   
  20.     #ifdef BSP_BUTTON_4  
  21.     {BSP_BUTTON_4, false, BUTTON_PULL, bsp_button_event_handler},  
  22.     #endif // BUTTON_4  
  23.     #ifdef BSP_BUTTON_5  
  24.     {BSP_BUTTON_5, false, BUTTON_PULL, bsp_button_event_handler},  
  25.     #endif // BUTTON_5  
  26.   
  27.     #ifdef BSP_BUTTON_6  
  28.     {BSP_BUTTON_6, false, BUTTON_PULL, bsp_button_event_handler},  
  29.     #endif // BUTTON_6  
  30.   
  31.     #ifdef BSP_BUTTON_7  
  32.     {BSP_BUTTON_7, false, BUTTON_PULL, bsp_button_event_handler},  
  33.     #endif // BUTTON_7  
  34. };  
  35. #endif // BSP_SIMPLE  
  36. #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) 中断入口函数,然后跳到

[cpp]  view plain  copy
  1. #if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)  
  2. /**@brief Function for handling button events. 
  3.  * 
  4.  * @param[in]   pin_no          The pin number of the button pressed. 
  5.  * @param[in]   button_action   Action button. 
  6.  */  
  7. static void bsp_button_event_handler(uint8_t pin_no, uint8_t button_action)  
  8. {  
  9.     bsp_event_t        event  = BSP_EVENT_NOTHING;  
  10.     uint32_t           button = 0;  
  11.     uint32_t           err_code;  
  12.     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. */  
  13.     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. */  
  14.   
  15.     while ((button < BUTTONS_NUMBER) && (m_buttons_list[button] != pin_no))  
  16.     {  
  17.         button++;  
  18.     }  
  19.   
  20.     if (button < BUTTONS_NUMBER)  
  21.     {  
  22.         switch(button_action)  
  23.         {  
  24.             case APP_BUTTON_PUSH:  
  25.                 event = m_events_list[button].push_event;  
  26.                 if (m_events_list[button].long_push_event != BSP_EVENT_NOTHING)  
  27.                 {  
  28.                     err_code = app_timer_start(m_button_timer_id, BSP_MS_TO_TICK(BSP_LONG_PUSH_TIMEOUT_MS), (void*)¤t_long_push_pin_no);  
  29.                     if (err_code == NRF_SUCCESS)  
  30.                     {  
  31.                         current_long_push_pin_no = pin_no;  
  32.                     }  
  33.                 }                                                                                                                                                         release_event_at_push[button] = m_events_list[button].release_event;  
  34.                 break;  
  35.             case APP_BUTTON_RELEASE:  
  36.                 (void)app_timer_stop(m_button_timer_id);  
  37.                 if (release_event_at_push[button] == m_events_list[button].release_event)  
  38.                 {  
  39.                     event = m_events_list[button].release_event;  
  40.                 }  
  41.                 break;  
  42.             case BSP_BUTTON_ACTION_LONG_PUSH:  
  43.                 event = m_events_list[button].long_push_event;  
  44.                           
  45.         }  
  46.     }  
  47.   
  48.     if ((event != BSP_EVENT_NOTHING) && (m_registered_callback != NULL))  
  49.     {    
  50.         m_registered_callback(event);  
  51.     }  
  52. }  


这里来分析下如何实现长按和段按的。

 如果是长按, 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事件

[cpp]  view plain  copy
  1. /**@brief Handle events from button timer. 
  2.  * 
  3.  * @param[in]   p_context   parameter registered in timer start function. 
  4.  */  
  5. static void button_timer_handler(void * p_context)  
  6. {  
  7.     bsp_button_event_handler(*(uint8_t *)p_context, BSP_BUTTON_ACTION_LONG_PUSH);  
  8. }  

就是产生对应的长按事件。

注意:启动长按扫描的时候err_code = app_timer_start(m_button_timer_id, BSP_MS_TO_TICK(BSP_LONG_PUSH_TIMEOUT_MS), (void*)&current_long_push_pin_no);

current_long_push_pin_no 这个参数也传进去了,这个就是哪个管脚。方便产生长按超时的时候把哪个长按参数传进去,

看代码:bsp_init()函数

[cpp]  view plain  copy
  1. for (num = 0; ((num < BUTTONS_NUMBER) && (err_code == NRF_SUCCESS)); num++)  
  2. {  
  3.     err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_DEFAULT);  
  4. }  

这里穿进去的参数是BSP_BUTTON_ACTION_PUSH 和 BSP_EVENT_DEFAULT

函数bsp_event_to_button_action_assign()里面

[cpp]  view plain  copy
  1. if (event == BSP_EVENT_DEFAULT)  
  2. {  
  3.     // Setting default action: BSP_EVENT_KEY_x for PUSH actions, BSP_EVENT_NOTHING for RELEASE and LONG_PUSH actions.  
  4.     event = (action == BSP_BUTTON_ACTION_PUSH) ? (bsp_event_t)(BSP_EVENT_KEY_0 + button) : BSP_EVENT_NOTHING;  
  5.    
  6. }  

可以看到只有当设置为BSP_BUTTON_ACTION_PUSH  和BSP_EVENT_DEFAULT的时候,event才有值不然为0.那假如要设置长按怎么办呢?

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值