nRF52(SDK12)添加定时器任务

在裸机程序中,有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,实测并不需要调用;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在nRF52832上启用低功耗定时器(Low Power Timer),你需要进行以下步骤: 1. 配置低功耗定时器 首先,你需要配置低功耗定时器的时钟源和预分频器。你可以选择使用低频时钟(LFCLK)源,例如内部RC振荡器或外部晶体振荡器。然后,选择适当的预分频器来设置定时器的时钟速度。 2. 初始化低功耗定时器 使用nRF SDK或直接操作寄存器来初始化低功耗定时器。你需要设置定时器的模式(定时器模式或计数器模式)、位宽(8位或16位)、比较值(如果需要)等。 3. 启动低功耗定时器 在初始化完成后,启动低功耗定时器以开始计时或计数。你可以使用相应的函数或操作寄存器来启动定时器。 下面是一个示例代码片段,展示了如何在nRF52832上启用低功耗定时器: ```c #include <stdbool.h> #include "nrf.h" #include "nrf_drv_clock.h" #include "nrf_delay.h" void lptimer_init(void) { // 配置LFCLK源为内部RC振荡器 NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos; NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; NRF_CLOCK->TASKS_LFCLKSTART = 1; // 等待LFCLK稳定 while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) { // 等待 } // 配置低功耗定时器 NRF_LPTIMER->PRESCALER = 0; // 预分频器设置为1 NRF_LPTIMER->BITMODE = LPTIMER_BITMODE_BITMODE_16Bit; // 使用16位模式 // 初始化低功耗定时器 NRF_LPTIMER->TASKS_START = 1; // 启动低功耗定时器 } int main(void) { // 初始化时钟 nrf_drv_clock_init(); nrf_drv_clock_lfclk_request(NULL); // 初始化低功耗定时器 lptimer_init(); while (true) { // 主循环代码 } } ``` 在这个例程中,我们首先需要初始化时钟,然后调用`lptimer_init()`函数来配置和初始化低功耗定时器。最后,在主循环中,你可以添加自己的代码来处理定时器中断或检查定时器的当前值。 请注意,以上代码仅为示例,你可能需要根据自己的具体应用场景进行适当的修改和配置。在实际使用中,请参考nRF52832的技术参考手册和开发环境文档以获取更多详细信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

袁保康

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

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

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

打赏作者

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

抵扣说明:

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

余额充值