nRF52832学习记录(八、WDT看门狗 )

nRF52832 看门狗 使用 低频时钟源(LFCLK)提供时钟,是向下计数的定时器。
启动后,看门狗加载 CRV 寄存器中的指定值。然后开始计数,当计数到0后,会溢出产生 TIMEOUT 事件。看门狗 TIMEOUT 事件会导致系统复位 或者 TIMEOUT 中断。
看门狗的超时时间:
timeout [s] = ( CRV + 1 ) / 32768
看门狗喂狗的方式:
将特殊值 0x6E524635 写入所有使能的 重载寄存器 RR[n]
看门狗寄存器如下:

看门狗的使用方法(寄存器版):

/*** 看门狗中断*/
void WDT_IRQHandler(void)
{  if ((NRF_WDT->EVENTS_TIMEOUT!= 0) && ((NRF_WDT->INTENSET) != 0)) {//do something!!  //复位前就2个32.768khz时钟周期的时间执行这个操作,在此之后,将发生复位}  
}/** 启动内部LFCLK晶振功能,和RTC一样*/void lfclk_config(void)
{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) {}NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
}int main(void)
{/*时钟,外设,IO口按键,LED灯等的初始化*/...//*配置看门狗*////配置看门狗重载值,由timeout [s] = ( CRV + 1 ) / 32768计算得复位时间2sNRF_WDT->CRV=65535;//配置看门狗休眠下也运行NRF_WDT->CONFIG=0x01;//申请喂狗通道,也就是使用哪个RR,使用RR[0]NRF_WDT->RREN=0x01;	//启动WDTNRF_WDT->TASKS_START=1;//*配置看门狗中断*////使能看门狗定时器超时事件NRF_WDT->EVENTS_TIMEOUT=1; //使能看门狗中断NRF_WDT->INTENSET=1;//使能看门中断嵌套NVIC_EnableIRQ(WDT_IRQn);//...while (1){    //...if(nrf_gpio_pin_read(BUTTON_1) == 0)//使用按键喂狗{	//实现喂狗NRF_WDT->RR[0]=0x6E524635UL;}}
}

看门狗的使用方法(库函数版):

库函数使用相应的库文件得添加,头文件路径得添加

#include <stdbool.h>
#include <stdint.h>#include "nrf.h"
#include "bsp.h"
#include "app_timer.h"
#include "app_error.h"
#include "nrf_drv_wdt.h"
#include "nrf_drv_clock.h"
#include "nrf_delay.h"
#include "app_util_platform.h"//...
/*
#define nrf_drv_wdt_channel_id      nrfx_wdt_channel_idtypedef nrf_wdt_rr_register_t nrfx_wdt_channel_id;typedef enum
{NRF_WDT_RR0 = 0, /**< Reload request register 0. NRF_WDT_RR1,     /**< Reload request register 1. NRF_WDT_RR2,     /**< Reload request register 2.NRF_WDT_RR3,     /**< Reload request register 3. NRF_WDT_RR4,     /**< Reload request register 4. NRF_WDT_RR5,     /**< Reload request register 5.NRF_WDT_RR6,     /**< Reload request register 6. NRF_WDT_RR7      /**< Reload request register 7. 
} nrf_wdt_rr_register_t;
*/
nrf_drv_wdt_channel_id m_channel_id;/*** 看门狗事件回调*/
void wdt_event_handler(void)
{//do something!!  //复位前就2个32.768khz时钟周期的时间执行这个操作,在此之后,将发生复位
}int main(void)
{uint32_t err_code = NRF_SUCCESS;//初始化低速时钟err_code = nrf_drv_clock_init();APP_ERROR_CHECK(err_code);nrf_drv_clock_lfclk_request(NULL);//配置按键,LED灯,根据自己的板子nrf_gpio_cfg_output(12);    nrf_gpio_cfg_input(BUTTON_1,NRF_GPIO_PIN_PULLUP);bsp_board_init(BSP_INIT_LEDS);/*#define NRF_DRV_WDT_DEAFULT_CONFIG  NRFX_WDT_DEAFULT_CONFIG#define NRFX_WDT_DEAFULT_CONFIG                                               \{                                                                         \.behaviour          = (nrf_wdt_behaviour_t)NRFX_WDT_CONFIG_BEHAVIOUR, \.reload_value       = NRFX_WDT_CONFIG_RELOAD_VALUE,                   \.interrupt_priority = NRFX_WDT_CONFIG_IRQ_PRIORITY,                   \}在上面的默认定义中的数值,可以自行修改*/nrf_drv_wdt_config_t config = NRF_DRV_WDT_DEAFULT_CONFIG;   err_code = nrf_drv_wdt_init(&config, wdt_event_handler);APP_ERROR_CHECK(err_code);/*看门狗喂狗通道使能,从0开始自动查找可食用的RR[]#define nrf_drv_wdt_channel_alloc   nrfx_wdt_channel_allocnrfx_err_t nrfx_wdt_channel_alloc(nrfx_wdt_channel_id * p_channel_id)*/err_code = nrf_drv_wdt_channel_alloc(&m_channel_id);APP_ERROR_CHECK(err_code);/*使能#define nrf_drv_wdt_enable          nrfx_wdt_enablevoid nrfx_wdt_enable(void){NRFX_ASSERT(m_alloc_index != 0);NRFX_ASSERT(m_state == NRFX_DRV_STATE_INITIALIZED);nrf_wdt_int_enable(NRF_WDT_INT_TIMEOUT_MASK);nrf_wdt_task_trigger(NRF_WDT_TASK_START);m_state = NRFX_DRV_STATE_POWERED_ON;NRFX_LOG_INFO("Enabled.");}*/nrf_drv_wdt_enable();//... do something or notwhile (1){//... do something or notif(nrf_gpio_pin_read(BUTTON_1) == 0){/*喂狗void nrfx_wdt_channel_feed(nrfx_wdt_channel_id channel_id){NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);nrf_wdt_reload_request_set(channel_id);}*/nrfx_wdt_channel_feed(m_channel_id);}}
}

本文链接:https://my.lmcjl.com/post/1601.html

展开阅读全文

4 评论

留下您的评论.