一、前言
看门狗定时器(WDT,Watch Dog Timer)是Nordic52810的一个独立的模块,使用低速时钟源(LFCLK),通过START任务进行触发,但预设的自减计数器运行到0时,产生超时时间复位系统
百度百科
ps:如有错误,请留言指正,谢谢
二、功能详解
- 看门狗通过开始
START
任务启动,可以在CPU睡眠与debugger下被暂停(可配置) - 喂狗后,计数值会从
CRV
寄存器加载超时计数 - 超时公式 timeout[s] = (CRV+1)/32768
- 看门狗启动后,会自动启用32.768kHz RC振荡器
- 看门狗有8个独立的请求重载寄存器,写入特定值0x6E524635进行请求计数器重载
- 当使能看门狗中断后,系统会在事件生成后的两个时钟周期后(32.768kHZ)进行复位
三、寄存器
- INTENSET:中断使能寄存器,写1有效(可读)
- INTENCLR:中断禁用寄存器,写1有效(可读)
- RUNSTATUS:运行状态寄存器,只读,0 未运行 1 运行状态
- REQSTATUS:请求状态寄存器,查询RR寄存器是否被请求装载
- CRV:设置看门狗计算器值
- RREN:RR使能寄存器,总共可配置8路
- CONFIG:配置寄存器,配置系统睡眠(SLEEP)或停机(HALT)模式下是否运行看门狗
- RR:装载请求寄存器,进行喂狗
四、相关接口
WDT的驱动目录在modules\nrfx\drivers\src\nrfx_wdt.c中
- nrfx_wdt_init:初始化看门狗,通过形参p_config设置运行模式与超时时间,wdt_event_handler设置中断入口
- nrfx_wdt_channel_alloc:通道ID设置,形参获取到看门狗句柄
- nrf_drv_wdt_enable:使能看门狗
- nrf_drv_wdt_channel_feed:喂狗,请求重载
五、实例代码
- 实例工程目录为 examples\peripheral\wdt wdt.eww
在sdk_config.h中设置重载超时时间
#ifndef NRFX_WDT_CONFIG_RELOAD_VALUE
#define NRFX_WDT_CONFIG_RELOAD_VALUE 2000//2s
#endif
/**
* @brief Function for main application entry.
*/
int main(void)
{
uint32_t err_code = NRF_SUCCESS;
//BSP configuration for button support: button pushing will feed the dog.
err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);
nrf_drv_clock_lfclk_request(NULL);
err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
err_code = bsp_init(BSP_INIT_BUTTONS, bsp_event_callback);
APP_ERROR_CHECK(err_code);
//Configure all LEDs on board.
bsp_board_init(BSP_INIT_LEDS);
//Configure WDT.
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);
err_code = nrf_drv_wdt_channel_alloc(&m_channel_id);//获取可用通道
APP_ERROR_CHECK(err_code);
nrf_drv_wdt_enable();//使能
//Indicate program start on LEDs.
for (uint32_t i = 0; i < LEDS_NUMBER; i++)
{ nrf_delay_ms(200);
bsp_board_led_on(i);
}
err_code = bsp_buttons_enable();
APP_ERROR_CHECK(err_code);
while (1)
{
__SEV();
__WFE();
__WFE();
}
}
获取到按键事件后,进行喂狗
/**
* @brief BSP events callback.
*/
void bsp_event_callback(bsp_event_t event)
{
switch (event)
{
case BSP_EVENT_KEY_0:
nrf_drv_wdt_channel_feed(m_channel_id);
break;
default :
//Do nothing.
break;
}
}