1.在sdk_config.h中加入宏
// app_button - buttons handling module
//========================================================== // BUTTON_ENABLED - Enables Button module
#ifndef BUTTON_ENABLED #define BUTTON_ENABLED 1 #endif
// BUTTON_HIGH_ACCURACY_ENABLED - Enables GPIOTE high accuracy for buttons
#ifndef BUTTON_HIGH_ACCURACY_ENABLED #define BUTTON_HIGH_ACCURACY_ENABLED 0 #endif
// APP_TIMER_ENABLED - app_timer - Application timer functionality //========================================================== #ifndef APP_TIMER_ENABLED #define APP_TIMER_ENABLED 1 #endif // APP_TIMER_CONFIG_RTC_FREQUENCY - Configure RTC prescaler. // 32768 Hz // 16384 Hz // 8192 Hz // 4096 Hz // 2048 Hz // 1024 Hz
#ifndef APP_TIMER_CONFIG_RTC_FREQUENCY #define APP_TIMER_CONFIG_RTC_FREQUENCY 0 #endif
// APP_TIMER_CONFIG_IRQ_PRIORITY - Interrupt priority
// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice // 0 (highest) // 1 // 2 // 3 // 4 // 5 // 6 // 7
#ifndef APP_TIMER_CONFIG_IRQ_PRIORITY #define APP_TIMER_CONFIG_IRQ_PRIORITY 6 #endif
// APP_TIMER_CONFIG_OP_QUEUE_SIZE - Capacity of timer requests queue. // Size of the queue depends on how many timers are used // in the system, how often timers are started and overall // system latency. If queue size is too small app_timer calls // will fail.
#ifndef APP_TIMER_CONFIG_OP_QUEUE_SIZE #define APP_TIMER_CONFIG_OP_QUEUE_SIZE 10 #endif
// APP_TIMER_CONFIG_USE_SCHEDULER - Enable scheduling app_timer events to app_scheduler
#ifndef APP_TIMER_CONFIG_USE_SCHEDULER #define APP_TIMER_CONFIG_USE_SCHEDULER 0 #endif
// APP_TIMER_KEEPS_RTC_ACTIVE - Enable RTC always on
// If option is enabled RTC is kept running even if there is no active timers. // This option can be used when app_timer is used for timestamping.
#ifndef APP_TIMER_KEEPS_RTC_ACTIVE #define APP_TIMER_KEEPS_RTC_ACTIVE 0 #endif
// APP_TIMER_SAFE_WINDOW_MS - Maximum possible latency (in milliseconds) of handling app_timer event. // Maximum possible timeout that can be set is reduced by safe window. // Example: RTC frequency 16384 Hz, maximum possible timeout 1024 seconds - APP_TIMER_SAFE_WINDOW_MS. // Since RTC is not stopped when processor is halted in debugging session, this value // must cover it if debugging is needed. It is possible to halt processor for APP_TIMER_SAFE_WINDOW_MS // without corrupting app_timer behavior.
#ifndef APP_TIMER_SAFE_WINDOW_MS #define APP_TIMER_SAFE_WINDOW_MS 300000 #endif
// App Timer Legacy configuration - Legacy configuration.
//========================================================== // APP_TIMER_WITH_PROFILER - Enable app_timer profiling
#ifndef APP_TIMER_WITH_PROFILER #define APP_TIMER_WITH_PROFILER 0 #endif
// APP_TIMER_CONFIG_SWI_NUMBER - Configure SWI instance used.
#ifndef APP_TIMER_CONFIG_SWI_NUMBER #define APP_TIMER_CONFIG_SWI_NUMBER 0 #endif
//========================================================== // GPIOTE_ENABLED - nrf_drv_gpiote - GPIOTE peripheral driver - legacy layer //========================================================== #ifndef GPIOTE_ENABLED #define GPIOTE_ENABLED 1 #endif // GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS - Number of lower power input pins #ifndef GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS #define GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 4 #endif
// GPIOTE_CONFIG_IRQ_PRIORITY - Interrupt priority
// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice // 0 (highest) // 1 // 2 // 3 // 4 // 5 // 6 // 7
#ifndef GPIOTE_CONFIG_IRQ_PRIORITY #define GPIOTE_CONFIG_IRQ_PRIORITY 6 #endif
//
// NRFX_GPIOTE_ENABLED - nrfx_gpiote - GPIOTE peripheral driver //========================================================== #ifndef NRFX_GPIOTE_ENABLED #define NRFX_GPIOTE_ENABLED 1 #endif // NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS - Number of lower power input pins #ifndef NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS #define NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 1 #endif
// NRFX_GPIOTE_CONFIG_IRQ_PRIORITY - Interrupt priority // 0 (highest) // 1 // 2 // 3 // 4 // 5 // 6 // 7
#ifndef NRFX_GPIOTE_CONFIG_IRQ_PRIORITY #define NRFX_GPIOTE_CONFIG_IRQ_PRIORITY 6 #endif
// NRFX_GPIOTE_CONFIG_LOG_ENABLED - Enables logging in the module. //========================================================== #ifndef NRFX_GPIOTE_CONFIG_LOG_ENABLED #define NRFX_GPIOTE_CONFIG_LOG_ENABLED 0 #endif // NRFX_GPIOTE_CONFIG_LOG_LEVEL - Default Severity level // Off // Error // Warning // Info // Debug
#ifndef NRFX_GPIOTE_CONFIG_LOG_LEVEL #define NRFX_GPIOTE_CONFIG_LOG_LEVEL 3 #endif
// NRFX_GPIOTE_CONFIG_INFO_COLOR - ANSI escape code prefix. // Default // Black // Red // Green // Yellow // Blue // Magenta // Cyan // White
#ifndef NRFX_GPIOTE_CONFIG_INFO_COLOR #define NRFX_GPIOTE_CONFIG_INFO_COLOR 0 #endif
// NRFX_GPIOTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. // Default // Black // Red // Green // Yellow // Blue // Magenta // Cyan // White
#ifndef NRFX_GPIOTE_CONFIG_DEBUG_COLOR #define NRFX_GPIOTE_CONFIG_DEBUG_COLOR 0 #endif 2.导入文件nrfx_gpiote.c,bsp.c,app_timer.c,app_button.c到工程中
3.在bsp_config.h中,确保#if !defined(BSP_DEFINES_ONLY) && !defined(BSP_SIMPLE)条件为真,否则会编绎错误
4.引入头文件
#include "bsp.h" #include "app_timer.h"
5.自定义按键分配的事件值 #define GPIO16_SHORT_PRESS 18 #define GPIO16_LONG_PRESS 19 #define GPIO16_RELEASE 20 6.定义按键事件处理函数 /**@brief Function for handling bsp events. */ void bsp_evt_handler(bsp_event_t evt) { uint32_t err_code; switch (evt) { case GPIO16_SHORT_PRESS: printf("GPIO16_SHORT_PRESS\n"); break; case GPIO16_LONG_PRESS: printf("GPIO16_LONG_PRESS\n"); break; case GPIO16_RELEASE: printf("GPIO16_RELEASE\n"); break;
default: return; // no implementation needed } } 7.低速时钟初时化
/**@brief Function for initializing low frequency clock. */ void clock_initialization() { NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal EVENTS_LFCLKSTARTED = 0; NRF_CLOCK->TASKS_LFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) { // Do nothing. } } 8.BSP初时化 /**@brief Function for initializing bsp module. */ void bsp_configuration() { uint32_t err_code;
err_code = bsp_init( BSP_INIT_BUTTONS, bsp_evt_handler); APP_ERROR_CHECK(err_code); err_code = bsp_event_to_button_action_assign(3, BSP_BUTTON_ACTION_PUSH, GPIO16_SHORT_PRESS); err_code = bsp_event_to_button_action_assign(3, BSP_BUTTON_ACTION_RELEASE, GPIO16_RELEASE); APP_ERROR_CHECK(err_code); err_code = bsp_event_to_button_action_assign(3, BSP_BUTTON_ACTION_LONG_PUSH, GPIO16_LONG_PRESS); APP_ERROR_CHECK(err_code); } 9.在主函数中的处理
clock_initialization();
err_code = app_timer_init(); APP_ERROR_CHECK(err_code); bsp_configuration();