STM32菜鳥(niǎo)成長(zhǎng)記錄---窗口看門(mén)狗
stm32有兩個(gè)看門(mén)狗,獨(dú)立看門(mén)狗和窗口看門(mén)狗,其實(shí)兩者的功能是類似的,只是喂狗的限制時(shí)間不同。
獨(dú)立看門(mén)狗是限制喂狗時(shí)間在0-x內(nèi),x由你的相關(guān)寄存器決定。喂狗的時(shí)間不能過(guò)晚。
窗口看門(mén)狗,所以稱之為窗口就是因?yàn)槠湮构窌r(shí)間是一個(gè)有上下限的范圍內(nèi),你可以通過(guò)設(shè)定相關(guān)寄存器,設(shè)定其上限時(shí)間和下限時(shí)間。喂狗的時(shí)間不能過(guò)早也不能過(guò)晚。
圖1
窗口看門(mén)狗的
上窗口就是配置寄存器WWDG->CFR里設(shè)定的W[6:0];
下窗口是固定的0x40;
當(dāng)窗口看門(mén)狗的計(jì)數(shù)器在上窗口值之外,或是低于下窗口值都會(huì)產(chǎn)生復(fù)位。
圖2
&&上窗口的值可以只有設(shè)定,7位二進(jìn)制數(shù)最大只可以設(shè)定為127(0x7F),最小又必須大于下窗口的0x40,所以其取值范圍為64~127(即:0x40~0x7F);
&&配置寄存器WWDG->CFR中為計(jì)數(shù)器設(shè)定時(shí)鐘分頻系數(shù),確定這個(gè)計(jì)數(shù)器可以定時(shí)的時(shí)間范圍,從而確定窗口的時(shí)間范圍。
&&窗口看門(mén)狗的時(shí)鐘來(lái)自于PCLK1,在時(shí)鐘配置中,其頻率為外部時(shí)鐘經(jīng)倍頻器后的二分頻時(shí)鐘,即為36MHz,根據(jù)手冊(cè)可以知道其定時(shí)時(shí)間計(jì)算方法:
上窗口時(shí)間:T_min = 4096 * (2^WDGTB)*(WWDG_CR[6:0] - WWDG_CFR[6:0])/36 (us)
下窗口時(shí)間:T_max = 4096 * (2^WDGTB)*(WWDG_CR[6:0] - 0x40)/36 (us)
36Mhz下相關(guān)窗口看門(mén)狗的喂狗時(shí)間范圍:
WDGTB(計(jì)數(shù)器分頻值)最早喂狗時(shí)間/us最晚喂狗時(shí)間/ms
0 113 7.28
1 227 14.56
2 455 29.12
3 910 58.25
本例直接寄存器實(shí)現(xiàn)4種測(cè)試模式,測(cè)試窗口看門(mén)狗的復(fù)位原理。
MODE_1 0 //在30ms時(shí)喂狗,在窗口范圍內(nèi)喂狗,LED燈閃爍
MODE_2 0 //在10ms時(shí)喂狗,在窗口范圍外導(dǎo)致復(fù)位,LED燈常亮
MODE_3 0 //在100ms時(shí)喂狗,在窗口范圍外喂狗導(dǎo)致復(fù)位
MODE_4 1 //主函數(shù)不執(zhí)行喂狗,開(kāi)啟提前喚醒中斷,在WWDG中斷函數(shù)中喂狗
庫(kù)函數(shù)實(shí)現(xiàn)在提前喚醒中斷中,喂狗操作,PA4口LED正常閃爍。當(dāng)外部中斷發(fā)生(按下PA0按鍵),長(zhǎng)時(shí)間不喂狗,引發(fā)窗口看門(mén)狗復(fù)位。
直接操作寄存器
相關(guān)寄存器如下:
控制寄存器WWDG->CR:低8位有效。
第8位為WDGA,看門(mén)狗激活位。低7位[6:0]為看門(mén)狗計(jì)數(shù)器的計(jì)數(shù)值。
配置寄存器WWDG->CFR:低十位有效。
第10位為EWI:提醒喚起中斷。此位置1,當(dāng)計(jì)數(shù)器值到達(dá)0x40時(shí),將產(chǎn)生中斷。
第8,9位WDGTB:為計(jì)數(shù)器分頻系數(shù)設(shè)置為:
00: CK計(jì)時(shí)器時(shí)鐘(PCLK1除以4096)除以1 01: CK計(jì)時(shí)器時(shí)鐘(PCLK1除以4096)除以2
10: CK計(jì)時(shí)器時(shí)鐘(PCLK1除以4096)除以4 11: CK計(jì)時(shí)器時(shí)鐘(PCLK1除以4096)除以8
低7位[6:0]:為窗口值。
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System Clocks Configuration ---------------------------------------------*/
RCC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
/*注釋-hope:利用窗口看門(mén)狗的提前喚醒中斷服務(wù)子程序來(lái)進(jìn)行獨(dú)立看門(mén)狗計(jì)數(shù)值的
重載,每次重載一次計(jì)數(shù)值LED2就閃爍一次。*/
/* Check if the system has resumed from WWDG reset -------------------------*/
if(RCC_GetFlagStatus(RCC_FLAG_WWDGRST) != RESET)
{ /* WWDGRST flag set */
/* Turn on led connected to PC.06 */
GPIO_WriteBit(GPIOC, GPIO_Pin_6, Bit_SET);
/* Clear reset flags */
RCC_ClearFlag();
}
else
{ /* WWDGRST flag is not set */
/* Turn off led connected to PC.06 */
GPIO_WriteBit(GPIOC, GPIO_Pin_6, Bit_RESET);
}
/* Configure EXTI Line9 to generate an interrupt on falling edge -----------*/
EXTI_Configuration();
/* NVIC configuration --------------------------------------------------------*/
NVIC_Configuration();
/* WWDG configuration --------------------------------------------------------*/
/* Enable WWDG clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
/* This parameter can be one of the following values:
* @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1
* @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2
* @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4
* @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8
/* WWDG clock counter = (PCLK1/4096)/8 = 244 Hz (~4 ms)---PCLK1=8MHZ */
WWDG_SetPrescaler(WWDG_Prescaler_8);
/* Set Window value to 0x41 ---設(shè)置上窗口邊界值*/
WWDG_SetWindowValue(0x41);
/* Enable WWDG and set counter value to 0x7F, 0x7f-0x41=64 --WWDG timeout = ~4 ms * 64 = 262 ms */
/*看門(mén)狗計(jì)數(shù)器的計(jì)數(shù)值0x7F---結(jié)合上面上窗口邊界,計(jì)數(shù)值從ox7f遞減到0x40*/
WWDG_Enable(0x7F);
/* Clear EWI flag */
WWDG_ClearFlag();
/* Enable EW interrupt */
WWDG_EnableIT();
while (1)
{
}
}
/******按鍵的優(yōu)先級(jí)為0高于窗口看門(mén)狗的優(yōu)先級(jí)1*****/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 2 bits for Preemption Priority and 2 bits for Sub Priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
/* Connect EXTI Line9 to PB.9 */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
/* Configure EXTI Line9 to generate an interrupt on falling edge */
EXTI_ClearITPendingBit(EXTI_Line9);
EXTI_InitStructure.EXTI_Line = EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿有效
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
在Stm32f10x_it.c文件中:
void WWDG_IRQHandler(void)
{
/* Update WWDG counter */
WWDG_SetCounter(0x7F);
/* Clear EWI flag */
WWDG_ClearFlag();
/* Toggle led connected to PC.07 */
GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));
}
/********按鍵中斷處理程序********/
//注意:這里中斷函數(shù)里沒(méi)有清中斷標(biāo)志的操作,這會(huì)導(dǎo)致窗口看門(mén)狗的提前喚醒中斷
無(wú)法進(jìn)入,最終導(dǎo)致因無(wú)法重載看門(mén)狗計(jì)數(shù)器而系統(tǒng)復(fù)位;
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line9) != RESET)
{
/* Turn off led connected to PC.07 */
GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_RESET);
/* As EXTI line9 pending bit is not cleared, the CPU will execute indefinitely
this ISR and when the WWDG counter falls to 3Fh the WWDG reset occurs */
}
}