nRF52(SDK12)添加定时器任务

本文探讨了在nRF52的BLE程序中如何正确使用APP Timer模块。详细介绍了从初始化到创建及启动定时器的过程,并解决了常见错误,如app_timer_create返回错误代码的问题。通过正确的宏定义和函数调用,实现了与BLE共存的定时器功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在裸机程序中,有timer例子,直接使用没有任何问题,但是不能直接用在BLE程序中。在BLE程序中使用就有些麻烦,BLE本身也使用了定时器,如果APP再使用算是借用。SDK12的BLE例子中也没有找到完整的例子。网上找到nrf52 Application Timernrf52-添加定时任务
前者例子:

// General application timer settings.
#define APP_TIMER_PRESCALER             16    // Value of the RTC1 PRESCALER register.
#define APP_TIMER_MAX_TIMERS            2     // Maximum number of timers in this application.
#define APP_TIMER_OP_QUEUE_SIZE         3     // Size of timer operation queues.

static app_timer_id_t                   m_led_a_timer_id;

static void lfclk_request(void)
{
    uint32_t err_code = nrf_drv_clock_init(NULL);
    APP_ERROR_CHECK(err_code);
    nrf_drv_clock_lfclk_request();
}

// Timeout handler for the repeated timer
static void timer_a_handler(void * p_context)
{
  nrf_drv_gpiote_out_toggle(LED_1_PIN);
}


// Create timers
static void create_timers()
{   
    uint32_t err_code;

    // Create timers
    err_code = app_timer_create(&m_led_a_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                timer_a_handler);
    APP_ERROR_CHECK(err_code);
}

int main()
{
	// Request LF clock.
	lfclk_request();
  
	// Initialize the application timer module.
	APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS,      APP_TIMER_OP_QUEUE_SIZE, false);

  create_timers();
  
  uint32_t err_code = app_timer_start(m_led_a_timer_id, APP_TIMER_TICKS(200, APP_TIMER_PRESCALER), NULL);
  APP_ERROR_CHECK(err_code);
  while(1);
}

experimental_ble_app_buttonless_dfu找到一些APP Timer使用的蛛丝马迹,重点有timer_initapplication_timers_start两个函数。

/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module.
 */
static void timers_init(void)
{
    // Initialize timer module, making it use the scheduler
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    // Create timers.

    /* YOUR_JOB: Create any timers to be used by the application.
                 Below is an example of how to create a timer.
                 For every new timer needed, increase the value of the macro APP_TIMER_MAX_TIMERS by
                 one.
    uint32_t err_code;
    err_code = app_timer_create(&m_app_timer_id, APP_TIMER_MODE_REPEATED, handle);
    APP_ERROR_CHECK(err_code);
    */
}
/**@brief Function for starting timers.
 */
static void application_timers_start(void)
{
    /* YOUR_JOB: Start your timers. below is an example of how to start a timer. 
    uint32_t err_code;
    err_code = app_timer_start(m_app_timer_id, TIMER_INTERVAL, NULL);
    APP_ERROR_CHECK(err_code);
	 */
}

将其uncomment之后并不能正常使用,BLE都搜索不到了。Debug之后定位到app_timer_create反回了一个0x08,而不是成功的0x00

过一段时间再去搜索原因,有一个帖子提到需要使用APP_TIMER_DEF(m_app_timer_id);的方式取代如下方式:

static app_timer_id_t m_timer_id;

来源:https://devzone.nordicsemi.com/f/nordic-q-a/17936/migrating-from-sdk8-to-sdk12-app_timer_create-crashes

这样就好了,完整如下:


//static app_timer_id_t m_app_timer_id;
APP_TIMER_DEF(m_app_timer_id);

/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module.
 */
static void timers_init(void)
{
    // Initialize timer module, making it use the scheduler
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    // Create timers.

    /* YOUR_JOB: Create any timers to be used by the application.
                 Below is an example of how to create a timer.
                 For every new timer needed, increase the value of the macro APP_TIMER_MAX_TIMERS by
                 one.
     */
    uint32_t err_code;
    err_code = app_timer_create(&m_app_timer_id, APP_TIMER_MODE_REPEATED, handle);
    APP_ERROR_CHECK(err_code);
}

#define TIMER_INTERVAL 20

/**@brief Function for starting timers.
 */
static void application_timers_start(void)
{
    /* YOUR_JOB: Start your timers. below is an example of how to start a timer.  */
    uint32_t err_code;
    err_code = app_timer_start(m_app_timer_id, TIMER_INTERVAL, NULL);
    APP_ERROR_CHECK(err_code);

}

其他被证实为假的假设:

  1. APP_TIMER_MAX_TIMERS需要+1,在sdk12版本中,似乎并没有使用到该宏;
  2. 需要nrf_drv_clock_lfclk_request,实测并不需要调用;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁保康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值