配置操作
在一个component下面可以创建多个看门狗,一般会有限制,由于它喂狗和逆初始化的特性,一般一个component下面挂一个看门狗就行。
看门狗比较简单,在configurations list里面新建看门狗,配置里面的名字、超时时间等配置即可。
代码对应
生成的代码在watchdog1.c和 watchdog1.h里面。
watchdog1是一个components,在watchdog1.h里面定义。
/*! @brief Device instance number */
#define INST_WATCHDOG1 0U
两个看门狗watchdog1_Config0和watchdog1_Config1在watchdog1.h里面声明,watchdog1.c里面定义。但是一般只需要用一个看门狗,也就是一个components下面只有一个看门狗。
/*! watchdog1 configuration structures */
const wdog_user_config_t watchdog1_Config0 = {
.clkSource = WDOG_LPO_CLOCK, /* WDOG clock source *///时钟源
.opMode = {
false, /* Wait Mode *///等待模式
false, /* Stop Mode *///停止模式
false /* Debug Mode *///调试模式
},
.updateEnable = true, /* Enable/Disable further updates of the WDOG configuration *///更新使能
.intEnable = false, /* Timeout interrupt disabled *///中断使能
.winEnable = false, /* Enable/Disable Window mode *///窗口模式
.windowValue = 0U, /* Window value *///窗口数值
.timeoutValue = 3000U, /* Timeout value *///超时值
.prescalerEnable = true /* WDOG prescaler *///分频使能
};
const wdog_user_config_t watchdog1_Config1 = {
.clkSource = WDOG_BUS_CLOCK, /* WDOG clock source */
.opMode = {
true, /* Wait Mode */
true, /* Stop Mode */
true /* Debug Mode */
},
.updateEnable = true, /* Enable/Disable further updates of the WDOG configuration */
.intEnable = true, /* Timeout interrupt disabled */
.winEnable = true, /* Enable/Disable Window mode */
.windowValue = 0U, /* Window value */
.timeoutValue = 1000U, /* Timeout value */
.prescalerEnable = true /* WDOG prescaler */
};
/* END watchdog1. */
一般配置成watchdog1_Config0那样就行,其他的基本用不到。
时钟源可以选择总线时钟、LPO内部低功耗时钟、SOSC外部时钟、SIRC内部低速时钟。
/*!
* @brief Clock sources for the WDOG.
* Implements : wdog_clk_source_t_Class
*/
typedef enum
{
WDOG_BUS_CLOCK = 0x00U, /*!< Bus clock */
WDOG_LPO_CLOCK = 0x01U, /*!< LPO clock */
WDOG_SOSC_CLOCK = 0x02U, /*!< SOSC clock */
WDOG_SIRC_CLOCK = 0x03U /*!< SIRC clock */
} wdog_clk_source_t;
分频使能是有true和false,一旦选择true就是256分频。
更新使能默认是true,允许看门狗在不复位的情况下更新配置。
中断使能就是当看门狗复位之前产生的中断,我们称为死前中断或者叫遗嘱中断,在中断函数里面我们应该出来最重要的事情,而且必须得快,因为递减计数器再减一次,就会产生系统复位。
窗口模式只是限制喂狗时间不能过早也不能过晚,需要在窗口上限和下限范围内。减到固定值自动复位,这个固定值称为窗口下限,我们可以设置值的是窗口的上限。
超时数值也就是当计数器累计超过这个次数就复位。
等待模式、停止模式和调试模式,基本都用不到,全部false就行。
接口使用
接口函数在Driver_Wdg.c文件里面实现,Driver_Wdg.h文件里面声明。
WDOG_DRV_Init
初始化函数,每次只能初始化一个看门狗。譬如初始化watchdog1_Config0就调用WDOG_DRV_Init(INST_WATCHDOG1, &watchdog1_Config0);
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_Init
* Description : initialize the WDOG driver
*
* Implements : WDOG_DRV_Init_Activity
*END**************************************************************************/
status_t WDOG_DRV_Init(uint32_t instance,
const wdog_user_config_t * userConfigPtr)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
DEV_ASSERT(userConfigPtr != NULL);
WDOG_Type * base = s_wdogBase[instance];
status_t status = STATUS_SUCCESS;
status_t statusClockSource = STATUS_SUCCESS;
#ifdef DEV_ERROR_DETECT
uint32_t prevClockHz, crtClockHz;
/* Check if the previous clock source and the configuration clock source
* are enabled(if not, the counter will not be incremented) */
prevClockHz = WDOG_DRV_GetClockSourceFreq(WDOG_GetClockSource(s_wdogBase[instance]));
crtClockHz = WDOG_DRV_GetClockSourceFreq(userConfigPtr->clkSource);
if ((prevClockHz == 0U) || (crtClockHz == 0U))
{
statusClockSource = STATUS_ERROR;
}
#endif
/* If clock source disabled */
if (statusClockSource == STATUS_SUCCESS)
{
/* If window mode enabled and window value greater than or equal to the timeout value. Or timeout value is 0 */
if (((userConfigPtr->winEnable) && (userConfigPtr->windowValue >= userConfigPtr->timeoutValue)) \
|| (userConfigPtr->timeoutValue <= FEATURE_WDOG_MINIMUM_TIMEOUT_VALUE))
{
status = STATUS_ERROR;
}
else
{
/* Configure the WDOG module */
status = WDOG_Config(base, userConfigPtr);
}
if (status == STATUS_SUCCESS)
{
/* enable WDOG timeout interrupt */
INT_SYS_EnableIRQ(s_wdogIrqId[instance]);
}
}
else
{
status = STATUS_ERROR;
}
return status;
}
如果想加个看门狗中断处理函数,初始化的时候就加这么一句。
INT_SYS_InstallHandler(WDOG_EWM_IRQn, WDOG_Handler, (isr_t *)NULL);
WDOG_DRV_Deinit
反初始化,但一反初始化就是一整个component里面的所有看门狗。
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_Deinit
* Description : De-initialize the WDOG driver
*
* Implements : WDOG_DRV_Deinit_Activity
*END**************************************************************************/
status_t WDOG_DRV_Deinit(uint32_t instance)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
WDOG_Type * base = s_wdogBase[instance];
status_t status = STATUS_SUCCESS;
INT_SYS_DisableIRQGlobal();
/* If allowed reconfigures WDOG */
if (WDOG_IsUpdateEnabled(base))
{
/* Disable WDOG timeout interrupt */
INT_SYS_DisableIRQ(s_wdogIrqId[instance]);
/* Disable WDOG */
WDOG_Deinit(base);
}
else
{
status = STATUS_ERROR;
}
/* Enable global interrupt */
INT_SYS_EnableIRQGlobal();
return status;
}
WDOG_DRV_GetConfig
获取配置,也就是上面配置那个结构体。
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_GetConfig
* Description : get the current configuration of the WDOG driver
*
* Implements : WDOG_DRV_GetConfig_Activity
*END**************************************************************************/
void WDOG_DRV_GetConfig(uint32_t instance,
wdog_user_config_t * const config)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
DEV_ASSERT(config != NULL);
const WDOG_Type *baseAddr = s_wdogBase[instance];
*config = WDOG_GetConfig(baseAddr);
}
WDOG_DRV_GetDefaultConfig
获取默认配置
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_GetDefaultConfig
* Description : get default configuration of the WDOG driver
*
* Implements : WDOG_DRV_GetDefaultConfig_Activity
*END**************************************************************************/
void WDOG_DRV_GetDefaultConfig(wdog_user_config_t * const config)
{
DEV_ASSERT(config != NULL);
/* Construct CS register new value */
config->winEnable = false;
config->prescalerEnable = false;
config->intEnable = false;
config->updateEnable = true;
config->opMode.debug = false;
config->opMode.wait = false;
config->opMode.stop = false;
config->clkSource = WDOG_LPO_CLOCK;
/* Construct TOVAL register new value */
config->timeoutValue = FEATURE_WDOG_TO_RESET_VALUE;
/* Construct WIN register new value */
config->windowValue = FEATURE_WDOG_WIN_RESET_VALUE;
}
WDOG_DRV_SetInt
使能/失能看门狗超时中断
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_SetInt
* Description : enable/disable the WDOG timeout interrupt
*
* Implements : WDOG_DRV_SetInt_Activity
*END**************************************************************************/
status_t WDOG_DRV_SetInt(uint32_t instance,
bool enable)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
WDOG_Type * base = s_wdogBase[instance];
status_t status = STATUS_SUCCESS;
/* If allowed reconfigures WDOG */
if (WDOG_IsUpdateEnabled(base))
{
/* Disable global interrupt */
INT_SYS_DisableIRQGlobal();
/* Enable/disable WDOG timeout interrupt */
WDOG_SetInt(base, enable);
/* Enable global interrupt */
INT_SYS_EnableIRQGlobal();
}
else
{
status = STATUS_ERROR;
}
return status;
}
WDOG_DRV_ClearIntFlag
清除看门狗超时中断
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_ClearIntFlag
* Description : Clear interrupt flag of the WDOG
*
* Implements : WDOG_DRV_ClearIntFlag_Activity
*END**************************************************************************/
void WDOG_DRV_ClearIntFlag(uint32_t instance)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
WDOG_Type * base = s_wdogBase[instance];
/* Clear interrupt flag of the WDOG */
WDOG_ClearIntFlag(base);
}
WDOG_DRV_Trigger
不要被它的名字迷惑了,其实就是喂个狗。
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_Trigger
* Description : Refreshes the WDOG counter
*
* Implements : WDOG_DRV_Trigger_Activity
*END**************************************************************************/
void WDOG_DRV_Trigger(uint32_t instance)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
WDOG_Type * base = s_wdogBase[instance];
WDOG_Trigger(base);
}
WDOG_DRV_GetCounter
看看多少轮没有喂狗了
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_GetCounter
* Description : Get the value of the WDOG counter.
*
* Implements : WDOG_DRV_GetCounter_Activity
*END**************************************************************************/
uint16_t WDOG_DRV_GetCounter(uint32_t instance)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
const WDOG_Type * base = s_wdogBase[instance];
return (uint16_t)base->CNT;
}
WDOG_DRV_SetWindow
设置窗口模式,使能失能,窗口值。
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_SetWindow
* Description : Set window mode and window value of the WDOG.
*
* Implements : WDOG_DRV_SetWindow_Activity
*END**************************************************************************/
status_t WDOG_DRV_SetWindow(uint32_t instance,
bool enable,
uint16_t windowvalue)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
WDOG_Type * base = s_wdogBase[instance];
status_t status = STATUS_SUCCESS;
/* If allowed reconfigures WDOG */
if (WDOG_IsUpdateEnabled(base))
{
/* Set WDOG window mode */
WDOG_SetWindowMode(base, enable);
/* If enable window mode */
if (enable)
{
/* Set window value for the WDOG */
WDOG_SetWindowValue(base, windowvalue);
}
}
else
{
status = STATUS_ERROR;
}
return status;
}
WDOG_DRV_SetMode
设置模式是调试、等待还是暂停。
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_SetMode
* Description : Set mode operation of the WDOG.
*
* Implements : WDOG_DRV_SetMode_Activity
*END**************************************************************************/
status_t WDOG_DRV_SetMode(uint32_t instance,
bool enable,
wdog_set_mode_t Setmode)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
WDOG_Type * base = s_wdogBase[instance];
status_t status = STATUS_SUCCESS;
/* If allowed reconfigures WDOG */
if (WDOG_IsUpdateEnabled(base))
{
switch (Setmode)
{
case WDOG_DEBUG_MODE:
/* Set WDOG debug mode */
WDOG_SetDebug(base, enable);
break;
case WDOG_WAIT_MODE:
/* Set WDOG wait mode */
WDOG_SetWait(base, enable);
break;
case WDOG_STOP_MODE:
/* Set WDOG stop mode */
WDOG_SetStop(base, enable);
break;
default:
/* Do nothings */
break;
}
}
else
{
status = STATUS_ERROR;
}
return status;
}
WDOG_DRV_SetTimeout
设置超时时间
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_SetTimeout
* Description : Set time value of the WDOG timeout.
*
* Implements : WDOG_DRV_SetTimeout_Activity
*END**************************************************************************/
status_t WDOG_DRV_SetTimeout(uint32_t instance,
uint16_t timeout)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
WDOG_Type * base = s_wdogBase[instance];
status_t status = STATUS_SUCCESS;
/* If allowed reconfigures WDOG */
if (WDOG_IsUpdateEnabled(base))
{
WDOG_UNLOCK(base);
base->TOVAL = timeout;
}
else
{
status = STATUS_ERROR;
}
return status;
}
WDOG_DRV_SetTestMode
设置测试模式,基本不会用到
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_SetTestMode
* Description : Set test mode of the WDOG.
*
* Implements : WDOG_DRV_SetTestMode_Activity
*END**************************************************************************/
status_t WDOG_DRV_SetTestMode(uint32_t instance,
wdog_test_mode_t testMode)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
WDOG_Type * base = s_wdogBase[instance];
uint32_t regValue = base->CS;
status_t status = STATUS_SUCCESS;
/* If allowed reconfigures WDOG */
if (WDOG_IsUpdateEnabled(base))
{
regValue &= ~(WDOG_CS_TST_MASK);
regValue |= WDOG_CS_TST(testMode);
WDOG_UNLOCK(base);
base->CS = regValue;
}
else
{
status = STATUS_ERROR;
}
return status;
}
WDOG_DRV_GetTestMode
获取当前测试模式,要打开测试模式才行。
/*FUNCTION**********************************************************************
*
* Function Name : WDOG_DRV_GetTestMode
* Description : Get test mode of the WDOG.
*
* Implements : WDOG_DRV_GetTestMode_Activity
*END**************************************************************************/
wdog_test_mode_t WDOG_DRV_GetTestMode(uint32_t instance)
{
DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
const WDOG_Type * base = s_wdogBase[instance];
wdog_test_mode_t testMode = WDOG_TST_DISABLED;
/* Gets test mode */
switch (WDOG_GetTestMode(base))
{
case 0U:
testMode = WDOG_TST_DISABLED;
break;
case 1U:
testMode = WDOG_TST_USER;
break;
case 2U:
testMode = WDOG_TST_LOW;
break;
case 3U:
testMode = WDOG_TST_HIGH;
break;
default:
testMode = WDOG_TST_DISABLED;
break;
}
return testMode;
}