Nordic nRF5 SDK 学习笔记之一, 蓝牙协议栈的软件定时器 Timer 用法

Nordic nRF5 SDK 蓝牙协议栈

Timer 软件定时器简明用法

app_timer module 是静态配置, 位于 sdk_config.h 文件中. 使用函数 app_timer_init 初始化 Timer library 库.


主要步骤:

1. 引入头文件  app_timer.h;

#include "app_timer.h"

2. 定义 Timer 定时器识别号 _my_timer_id 和需要的时间常量 MY_TIMER_INTERVAL; 

APP_TIMER_DEF( _my_timer_id );

#define MY_TIMER_INTERVAL    APP_TIMER_TICKS( _milliseconds )    // milliseconds 单位是毫秒

3.  Timer 定时器初始化;

err_code = app_timer_init();

4. 新建一个 Timer 定时器应用;

err_code = app_timer_create(&_my_timer_id, mode, my_timeout_handler);

    参数 mode 有两个选项值, APP_TIMER_MODE_SINGLE_SHOT (动作一次),APP_TIMER_MODE_REPEATED(重复动作);

 参数 my_timeout_handler: 当前定时器 time-out 触发中断时对应的处理函数;

static void my_timeout_handler(void * p_context) { ... }

5. Timer 定时器启动;

扫描二维码关注公众号,回复: 2474172 查看本文章
 err_code = app_timer_start(_my_timer_id, MY_TIMER_INTERVAL, NULL);

    函数 app_timer_start 第三个参数是 void * p_context, 如没有返回值,则为 NULL;

6. 相关 Timer 定时器操作函数;

ret_code_t app_timer_stop(app_timer_id_t timer_id);

ret_code_t app_timer_stop_all(void);

void app_timer_resume(void);

void app_timer_pause(void);

uint8_t app_timer_op_queue_utilization_get(void);

uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,uint32_t ticks_from );

uint32_t app_timer_cnt_get(void);    

uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,uint32_t ticks_from);

官方资料查询:nRF5 SDK/Libraries/Timer library

Timer library

The timer library (also referred to as app_timer) enables the application to create multiple timer instances based on the RTC1 peripheral. Checking for time-outs and invoking the user time-out handlers is performed in the RTC1 interrupt handler. List handling is done using a software interrupt (SWI0). Both interrupt handlers are running at a priority level that can be configured in sdk_config.h.

Usage

Configuration of the app_timer module is static. It is located in the sdk_config.h file. Use function app_timer_init to initialize the library.

To define a timer, use the APP_TIMER_DEF macro. This macro allocates memory for the timer instance and declares an identifier that can later on be used to refer to the specific instance. Before starting a timer, the timer must be created:

APP_TIMER_DEF(my_timer_id);

err_code = app_timer_create(&my_timer_id, mode, timeout_handler)

After the timer is created, it can be controlled using app_timer_start and app_timer_stop.

When app_timer_start() or app_timer_stop() are called, the timer operation is only queued, not executed. A software interrupt is triggered and the actual timer start/stop operation is executed by the SWI0 interrupt handler. If the application code that calls the timer function is running at the same or higher interrupt priority level, the timer operation will not be performed until the application handler has returned. This is the case, for example, when stopping a timer from a time-out handler when not using the scheduler.

To use this library with Scheduler, use the APP_TIMER_CONFIG_USE_SCHEDULER option in sdk_config.h.

Debugging

The module exposes API for pausing and resuming RTC. It can be used in debugging hooks when entering and leaving breakpoints. Such hooks are available in J-Link Monitor, see Monitor Mode Debugging - Revolutionize the way you debug BLE applications.

Migration note

When migrating from an SDK released before version 13.0.0, keep in mind that some changes have been introduced to the timer library in SDK v13.0.0. It might be necessary to adjust the code of your application to comply with these changes.

  1. Static configuration of the module has been moved to sdk_config.h and thus the APP_TIMER_INIT macro has been removed. Therefore, you must change the following code:

    APP_TIMER_INIT(PRESCALER, OP_QUEUE_SIZE, SCHEDULER_FUNC);

    to:

    err_code = app_timer_init();

  2. The PRESCALER parameter has been removed from the APP_TIMER_TICKS macro. Therefore, you must change the following code:

    ticks = APP_TIMER_TICKS(MS, PRESCALER);

    to:

    ticks = APP_TIMER_TICKS(MS);

  3. app_timer_cnt_diff_compute API change - The tick diff value is now returned by the function and not by the pointer. Therefore, you must change the following code:

    err_code = app_timer_cnt_diff_compute(ticks_to, ticks_from, &ticks_diff);

    to:

    ticks_diff = app_timer_cnt_diff_compute(ticks_to, ticks_from);

猜你喜欢

转载自blog.csdn.net/weixin_42396877/article/details/81217283