1. BSP-board support package , 主要针对板子的底层做了支持。 用bsp接口调用底层硬件可以降低硬件相关性。 对于nordic平台来说,BSP的底层硬件主要关联到灯和按键,这部分定义在boards.h中根据编译选项关联到对应的板子h文件,我们自定义的板子为hy0628.h
板子的.h文件主要就是灯和按键的引脚关联和数量,状态等定义,这部分是基本的
// LEDs definitions for dev 0628
#define LEDS_NUMBER 3
#define B_LED 9
#define R_LED 7
#define G_LED 5
#define LEDS_LIST { B_LED, R_LED, G_LED }
#define B_LED_Toggle nrf_gpio_pin_toggle(B_LED)
#define R_LED_Toggle nrf_gpio_pin_toggle(R_LED)
#define G_LED_Toggle nrf_gpio_pin_toggle(G_LED)
#define BSP_LED_0 B_LED
#define BSP_LED_1 R_LED
#define BSP_LED_2 G_LED
#define LEDS_ACTIVE_STATE 0
#define BUTTONS_NUMBER 3
#define USR_KEY1 2
#define USR_KEY2 3
#define USR_KEY3 4
#define BUTTONS_LIST { USR_KEY1, USR_KEY2, USR_KEY3}
#define BUTTONS_ACTIVE_STATE 0
#define BUTTON_PULL NRF_GPIO_PIN_PULLUP
#define BSP_BUTTON_0 USR_KEY1
#define BSP_BUTTON_1 USR_KEY2
#define BSP_BUTTON_2 USR_KEY3
2. bsp.h里定义了板级的标准事件
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_SYSOFF, /**< The device should enter system off mode (without wakeup). */
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;
3. 按键也有按下触发,释放触发或长按
typedef struct
{
bsp_event_t push_event; /**< The event to fire on regular button press. */
bsp_event_t long_push_event; /**< The event to fire on long button press. */
bsp_event_t release_event; /**< The event to fire on button release. */
} bsp_button_event_cfg_t;
4. 问题来了,如何在触发事件--状态切换--响应事件之间建立关联关系呢?
先看主程序,很简单,初始化,然后就低功耗等待了,所以中断,时钟调度,状态切换等都交给bsp去干活了,主循环不需要执行什么。下一个问题是: 如果要自定义功能呢? 留待下个例程分解
int main(void)
{
clock_initialization();
uint32_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("BSP example started.");
bsp_configuration();
while (true)
{
NRF_LOG_FLUSH();
__SEV();
__WFE();
__WFE();
// no implementation needed
}
}
应该BSP是有个timer来调度资源的,所以主程序一开始,先要clock配置和调用app_timer_init()
void clock_initialization()
{
NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
NRF_CLOCK->TASKS_LFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
{
// Do nothing.
}
}
app_timer_init()应该是标准函数,定义了rtc和overflow的处理
5. 来看bsp_configuration()干了啥
void bsp_configuration()
{
uint32_t err_code;
err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_evt_handler);
APP_ERROR_CHECK(err_code);
}
调用了bsp_init(),这个是标准函数还是自定义? 在bsp.c里,建立了触发条件和事件之间的关联
uint32_t bsp_init(uint32_t type, bsp_event_callback_t callback)
{
uint32_t err_code = NRF_SUCCESS;
#if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
m_indication_type = type;
#endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
#if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)
m_registered_callback = callback;
// BSP will support buttons and generate events
if (type & BSP_INIT_BUTTONS)
{
uint32_t num;
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)
{
err_code = app_button_init((app_button_cfg_t *)app_buttons,
BUTTONS_NUMBER,
APP_TIMER_TICKS(50));
}
if (err_code == NRF_SUCCESS)
{
err_code = app_button_enable();
}
if (err_code == NRF_SUCCESS)
{
err_code = app_timer_create(&m_bsp_button_tmr,
APP_TIMER_MODE_SINGLE_SHOT,
button_timer_handler);
}
}
#elif (BUTTONS_NUMBER > 0) && (defined BSP_SIMPLE)
bsp_board_init(type);
#endif // (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)
#if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
if (type & BSP_INIT_LEDS)
{
//handle LEDs only. Buttons are already handled.
bsp_board_init(BSP_INIT_LEDS);
// timers module must be already initialized!
if (err_code == NRF_SUCCESS)
{
err_code =
app_timer_create(&m_bsp_leds_tmr, APP_TIMER_MODE_SINGLE_SHOT, leds_timer_handler);
}
if (err_code == NRF_SUCCESS)
{
err_code =
app_timer_create(&m_bsp_alert_tmr, APP_TIMER_MODE_REPEATED, alert_timer_handler);
}
}
#endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
return err_code;
}
先看type用了位操作,来指示bsp初始化的操作方式,根据在boards.h文件里的定义其实就是LED和BUTTON,当然,后续可以在这个基础上扩展自己的板级硬件和初始化要求。 比如显示,比如其他接口等。
/**@defgroup BSP_BOARD_INIT_FLAGS Board initialization flags.
* @{ */
#define BSP_INIT_NONE 0 /**< No initialization of LEDs or buttons (@ref bsp_board_init).*/
#define BSP_INIT_LEDS (1 << 0) /**< Enable LEDs during initialization (@ref bsp_board_init).*/
#define BSP_INIT_BUTTONS (1 << 1) /**< Enable buttons during initialization (@ref bsp_board_init).*/
/**@} */
这个初始化里直接通过m_indication_type = type; 把type丢给了m_indication_type ,那我们来看看m_indication_type发挥了什么作用,依然是在bsp.c 里,全文搜索
uint32_t bsp_indication_set(bsp_indication_t indicate)
{
uint32_t err_code = NRF_SUCCESS;
#if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
if (m_indication_type & BSP_INIT_LEDS)
{
err_code = bsp_led_indication(indicate);
}
#endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
return err_code;
}
这里提示就是依据m_indication_type说白了来判断板子上的LED配置情况,然后进行led指示灯的设置。
问题:这个设置在哪儿调用? 工作机制是怎样的?
继续挖
先看调用的入口,查看得到
static void leds_timer_handler(void * p_context)
{
UNUSED_PARAMETER(p_context);
if (m_indication_type & BSP_INIT_LEDS)
{
UNUSED_VARIABLE(bsp_led_indication(m_stable_state));
}
}
所以这个看起来是leds用到的timer就是用来调用指示灯的驱动,完成状态和灯控的关联,那么leds的timer在哪儿启动和指定handler的呢?
原来就在bsp_init里
app_timer_create(&m_bsp_leds_tmr, APP_TIMER_MODE_SINGLE_SHOT, leds_timer_handler); 这一句通过app_timer的机制创建了led控制用的timer, 那么SIGLE_SHOT和REPEATED有什么区别? 为什么这里led控制用的是single shot呢?
先放一放,看看指示灯状态切换的控制逻辑
static uint32_t bsp_led_indication(bsp_indication_t indicate)
{
uint32_t err_code = NRF_SUCCESS;
uint32_t next_delay = 0;
if (m_leds_clear)
{
m_leds_clear = false;
leds_off();
}
switch (indicate)
{
case BSP_INDICATE_IDLE:
leds_off();
err_code = app_timer_stop(m_bsp_leds_tmr);
m_stable_state = indicate;
break;
case BSP_INDICATE_SCANNING:
case BSP_INDICATE_ADVERTISING:
// in advertising blink LED_0
if (bsp_board_led_state_get(BSP_LED_INDICATE_INDICATE_ADVERTISING))
{
bsp_board_led_off(BSP_LED_INDICATE_INDICATE_ADVERTISING);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_OFF_INTERVAL :
ADVERTISING_SLOW_LED_OFF_INTERVAL;
}
else
{
bsp_board_led_on(BSP_LED_INDICATE_INDICATE_ADVERTISING);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_ON_INTERVAL :
ADVERTISING_SLOW_LED_ON_INTERVAL;
}
m_stable_state = indicate;
err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
break;
case BSP_INDICATE_ADVERTISING_WHITELIST:
// in advertising quickly blink LED_0
if (bsp_board_led_state_get(BSP_LED_INDICATE_ADVERTISING_WHITELIST))
{
bsp_board_led_off(BSP_LED_INDICATE_ADVERTISING_WHITELIST);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING_WHITELIST ?
ADVERTISING_WHITELIST_LED_OFF_INTERVAL :
ADVERTISING_SLOW_LED_OFF_INTERVAL;
}
else
{
bsp_board_led_on(BSP_LED_INDICATE_ADVERTISING_WHITELIST);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING_WHITELIST ?
ADVERTISING_WHITELIST_LED_ON_INTERVAL :
ADVERTISING_SLOW_LED_ON_INTERVAL;
}
m_stable_state = indicate;
err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
break;
case BSP_INDICATE_ADVERTISING_SLOW:
// in advertising slowly blink LED_0
if (bsp_board_led_state_get(BSP_LED_INDICATE_ADVERTISING_SLOW))
{
bsp_board_led_off(BSP_LED_INDICATE_ADVERTISING_SLOW);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING_SLOW ? ADVERTISING_SLOW_LED_OFF_INTERVAL :
ADVERTISING_SLOW_LED_OFF_INTERVAL;
}
else
{
bsp_board_led_on(BSP_LED_INDICATE_ADVERTISING_SLOW);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING_SLOW ? ADVERTISING_SLOW_LED_ON_INTERVAL :
ADVERTISING_SLOW_LED_ON_INTERVAL;
}
m_stable_state = indicate;
err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
break;
case BSP_INDICATE_ADVERTISING_DIRECTED:
// in advertising very quickly blink LED_0
if (bsp_board_led_state_get(BSP_LED_INDICATE_ADVERTISING_DIRECTED))
{
bsp_board_led_off(BSP_LED_INDICATE_ADVERTISING_DIRECTED);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING_DIRECTED ?
ADVERTISING_DIRECTED_LED_OFF_INTERVAL :
ADVERTISING_SLOW_LED_OFF_INTERVAL;
}
else
{
bsp_board_led_on(BSP_LED_INDICATE_ADVERTISING_DIRECTED);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING_DIRECTED ?
ADVERTISING_DIRECTED_LED_ON_INTERVAL :
ADVERTISING_SLOW_LED_ON_INTERVAL;
}
m_stable_state = indicate;
err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
break;
case BSP_INDICATE_BONDING:
// in bonding fast blink LED_0
bsp_board_led_invert(BSP_LED_INDICATE_BONDING);
m_stable_state = indicate;
err_code =
app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(BONDING_INTERVAL), NULL);
break;
case BSP_INDICATE_CONNECTED:
bsp_board_led_on(BSP_LED_INDICATE_CONNECTED);
m_stable_state = indicate;
break;
case BSP_INDICATE_SENT_OK:
// when sending shortly invert LED_1
m_leds_clear = true;
bsp_board_led_invert(BSP_LED_INDICATE_SENT_OK);
err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(SENT_OK_INTERVAL), NULL);
break;
case BSP_INDICATE_SEND_ERROR:
// on receving error invert LED_1 for long time
m_leds_clear = true;
bsp_board_led_invert(BSP_LED_INDICATE_SEND_ERROR);
err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(SEND_ERROR_INTERVAL), NULL);
break;
case BSP_INDICATE_RCV_OK:
// when receving shortly invert LED_1
m_leds_clear = true;
bsp_board_led_invert(BSP_LED_INDICATE_RCV_OK);
err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(RCV_OK_INTERVAL), NULL);
break;
case BSP_INDICATE_RCV_ERROR:
// on receving error invert LED_1 for long time
m_leds_clear = true;
bsp_board_led_invert(BSP_LED_INDICATE_RCV_ERROR);
err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(RCV_ERROR_INTERVAL), NULL);
break;
case BSP_INDICATE_FATAL_ERROR:
// on fatal error turn on all leds
bsp_board_leds_on();
m_stable_state = indicate;
break;
case BSP_INDICATE_ALERT_0:
case BSP_INDICATE_ALERT_1:
case BSP_INDICATE_ALERT_2:
case BSP_INDICATE_ALERT_3:
case BSP_INDICATE_ALERT_OFF:
err_code = app_timer_stop(m_bsp_alert_tmr);
next_delay = (uint32_t)BSP_INDICATE_ALERT_OFF - (uint32_t)indicate;
// a little trick to find out that if it did not fall through ALERT_OFF
if (next_delay && (err_code == NRF_SUCCESS))
{
if (next_delay > 1)
{
err_code = app_timer_start(m_bsp_alert_tmr,
APP_TIMER_TICKS(((uint16_t)next_delay * ALERT_INTERVAL)),
NULL);
}
bsp_board_led_on(BSP_LED_ALERT);
m_alert_on = true;
}
else
{
bsp_board_led_off(BSP_LED_ALERT);
m_alert_on = false;
}
break;
case BSP_INDICATE_USER_STATE_OFF:
leds_off();
m_stable_state = indicate;
break;
case BSP_INDICATE_USER_STATE_0:
leds_off();
bsp_board_led_on(BSP_LED_INDICATE_USER_LED1);
m_stable_state = indicate;
break;
case BSP_INDICATE_USER_STATE_1:
leds_off();
bsp_board_led_on(BSP_LED_INDICATE_USER_LED2);
m_stable_state = indicate;
break;
case BSP_INDICATE_USER_STATE_2:
leds_off();
bsp_board_led_on(BSP_LED_INDICATE_USER_LED1);
bsp_board_led_on(BSP_LED_INDICATE_USER_LED2);
m_stable_state = indicate;
break;
case BSP_INDICATE_USER_STATE_3:
case BSP_INDICATE_USER_STATE_ON:
bsp_board_leds_on();
m_stable_state = indicate;
break;
default:
break;
}
return err_code;
}
基本上是个状态机,根据状态决定哪些灯要点亮 ,控制灯清楚了,可是控制闪亮的时间呢? 控制灯的对应关系在bsp_config.h文件里,包括了interval,但是interval是怎么控制间隔的呢?
#define ADVERTISING_LED_ON_INTERVAL 200
#define ADVERTISING_LED_OFF_INTERVAL 1800
#define ADVERTISING_DIRECTED_LED_ON_INTERVAL 200
#define ADVERTISING_DIRECTED_LED_OFF_INTERVAL 200
#define ADVERTISING_WHITELIST_LED_ON_INTERVAL 200
#define ADVERTISING_WHITELIST_LED_OFF_INTERVAL 800
#define ADVERTISING_SLOW_LED_ON_INTERVAL 400
#define ADVERTISING_SLOW_LED_OFF_INTERVAL 4000
#define BONDING_INTERVAL 100
#define SENT_OK_INTERVAL 100
#define SEND_ERROR_INTERVAL 500
#define RCV_OK_INTERVAL 100
#define RCV_ERROR_INTERVAL 500
#define ALERT_INTERVAL 200
#define BSP_LED_INDICATE_SENT_OK BSP_BOARD_LED_1
#define BSP_LED_INDICATE_SEND_ERROR BSP_BOARD_LED_1
#define BSP_LED_INDICATE_RCV_OK BSP_BOARD_LED_1
#define BSP_LED_INDICATE_RCV_ERROR BSP_BOARD_LED_1
#define BSP_LED_INDICATE_CONNECTED BSP_BOARD_LED_0
#define BSP_LED_INDICATE_BONDING BSP_BOARD_LED_0
#define BSP_LED_INDICATE_ADVERTISING_DIRECTED BSP_BOARD_LED_0
#define BSP_LED_INDICATE_ADVERTISING_SLOW BSP_BOARD_LED_0
#define BSP_LED_INDICATE_ADVERTISING_WHITELIST BSP_BOARD_LED_0
#define BSP_LED_INDICATE_INDICATE_ADVERTISING BSP_BOARD_LED_0
#define BSP_LED_INDICATE_USER_LED1 BSP_BOARD_LED_0
#define BSP_LED_INDICATE_USER_LED2 BSP_BOARD_LED_1
#define BSP_LED_INDICATE_USER_LED3 BSP_BOARD_LED_2
#define BSP_LED_INDICATE_USER_LED4 BSP_BOARD_LED_3
#define BSP_LED_ALERT BSP_BOARD_LED_2
以其中一个状态灯控制的为例:
case BSP_INDICATE_ADVERTISING:
// in advertising blink LED_0
if (bsp_board_led_state_get(BSP_LED_INDICATE_INDICATE_ADVERTISING))
{
bsp_board_led_off(BSP_LED_INDICATE_INDICATE_ADVERTISING);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_OFF_INTERVAL :
ADVERTISING_SLOW_LED_OFF_INTERVAL;
}
else
{
bsp_board_led_on(BSP_LED_INDICATE_INDICATE_ADVERTISING);
next_delay = indicate ==
BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_ON_INTERVAL :
ADVERTISING_SLOW_LED_ON_INTERVAL;
}
m_stable_state = indicate;
err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
break;
如果亮灯的,就灭掉,同时设置next_delay参数(下一个off动作的延时)
如果灭的,就点亮,同时设置next_delay参数(下一个on动作的延时)
启动timer,间隔对应的interval作为tick计时依据。有一句 m_stable_state = indicate;确保进来入口之后还来到这个处理分支。
回过头看问题,为什么用ONSHOT模式来启动timer,就是因为这种任务是进去之后再设定下次启动的间隙,而不是重复进入。
另一条线是处理按键button的操作,从逻辑上来说,要做两个动作:
1) 将button按下和系统标准事件绑定
2)处理事件的操作
main.c里的bsp_evt_handler就是处理事件操作
void bsp_evt_handler(bsp_event_t evt)
{
uint32_t err_code;
switch (evt)
{
case BSP_EVENT_KEY_0:
if (actual_state != BSP_INDICATE_FIRST)
actual_state--;
else
actual_state = BSP_INDICATE_LAST;
break;
case BSP_EVENT_KEY_1:
if (actual_state != BSP_INDICATE_LAST)
actual_state++;
else
actual_state = BSP_INDICATE_FIRST;
break;
default:
return; // no implementation needed
}
err_code = bsp_indication_set(actual_state);
NRF_LOG_INFO("%s", (uint32_t)indications_list[actual_state]);
APP_ERROR_CHECK(err_code);
}
那么,按键怎么和事件关联的呢? 其实是在上面的bsp_init里有一半没讲完,就是除了初始化led外,对button的初始化
显示定义callback绑定入口,就是上面的evt_handler
m_registered_callback = callback;
这个类型定义
static bsp_event_callback_t m_registered_callback = NULL;
这个明显是个callback句柄的类型定义
typedef void (* bsp_event_callback_t)(bsp_event_t);
其实就是针对枚举句柄的指针调用
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_SYSOFF, /**< The device should enter system off mode (without wakeup). */
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;
bsp_init操作里和按键设置相关的初始化包括三个动作:
if (type & BSP_INIT_BUTTONS)
{
uint32_t num;
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)
{
err_code = app_button_init((app_button_cfg_t *)app_buttons,
BUTTONS_NUMBER,
APP_TIMER_TICKS(50));
}
if (err_code == NRF_SUCCESS)
{
err_code = app_button_enable();
}
if (err_code == NRF_SUCCESS)
{
err_code = app_timer_create(&m_bsp_button_tmr,
APP_TIMER_MODE_SINGLE_SHOT,
button_timer_handler);
}
}
1)事件和动作关联bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_DEFAULT);
2) 按键应用初始化app_button_init((app_button_cfg_t *)app_buttons, BUTTONS_NUMBER, APP_TIMER_TICKS(50));
3)创建timer,app_timer_create(&m_bsp_button_tmr,
APP_TIMER_MODE_SINGLE_SHOT,
button_timer_handler);
一项项来看看;
第一项, 动作分配是因为按键有三个动作,按下,松开和长按,所以这句调用是把push动作和event进行了关联分配
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)
{
// 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;
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
第二项的按键初始化其实是完成了对延时去抖的处理
uint32_t app_button_init(app_button_cfg_t const * p_buttons,
uint8_t button_count,
uint32_t detection_delay)
{
uint32_t err_code;
if (detection_delay < 2*APP_TIMER_MIN_TIMEOUT_TICKS)
{
return NRF_ERROR_INVALID_PARAM;
}
if (!nrf_drv_gpiote_is_init())
{
err_code = nrf_drv_gpiote_init();
VERIFY_SUCCESS(err_code);
}
/* Save configuration. */
mp_buttons = p_buttons;
m_button_count = button_count;
m_detection_delay = detection_delay;
memset(m_pin_states, 0, sizeof(m_pin_states));
m_pin_active = 0;
while (button_count--)
{
app_button_cfg_t const * p_btn = &p_buttons[button_count];
#if defined(BUTTON_HIGH_ACCURACY_ENABLED) && (BUTTON_HIGH_ACCURACY_ENABLED == 1)
nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(p_btn->hi_accuracy);
#else
nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
#endif
config.pull = p_btn->pull_cfg;
err_code = nrf_drv_gpiote_in_init(p_btn->pin_no, &config, gpiote_event_handler);
VERIFY_SUCCESS(err_code);
}
/* Create polling timer. */
return app_timer_create(&m_detection_delay_timer_id,
APP_TIMER_MODE_SINGLE_SHOT,
detection_delay_timeout_handler);
}
第三项的timer处理其实是完成了actions的区分和处理
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. */
button = bsp_board_pin_to_button_idx(pin_no);
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_bsp_button_tmr, APP_TIMER_TICKS(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_bsp_button_tmr);
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);
}
}
至此,解析完毕。
例程的功能,就是在按键按下后在state之间切换,并且启动标准的state对应的闪灯程序,可以作为经典的状态切换例程来模仿和学习。