[STM32] reset function based on IWDG (independent watchdog)

Objectives:

Not pressing KEY1 (not feeding the dog): the LED turns on and off repeatedly

Press KEY1 (feed dog): keep resetting, and the LED is not on

I Principle analysis

1. Watchdog: iwdg (independent watchdog), WWDG (window watchdog)

2.

Computer crash - > forced shutdown and restart

CPU of instrument - > encounter strong electric field and high-power equipment - > processor flies - > pt pointer in the program jumps to another place for execution or cannot run - > instrument crashes - > Reset instrument - > independent watchdog implementation

3.STM32 register manual (3)

Key register (IWDG_KR): dog feeding value (reset): write 0xAAAA, allow access register: write 0x5555, start watchdog: write 0xCCCC

Pre frequency division register (IWDG_PR): the frequency division multiple is 0, 1, 2, 3 (I choose 4 times frequency) (IWDG power frequency 40KHZ)

40KHz / 4 * 2 ^ 4 = 625hz (reload value is set to 625)

Reload register (IWDG_RLR): determine the reset time (I set the 4x frequency + reload value to 625, one reset per second (dog feeding time))

The register knows how to set it. Is there a function encapsulated for IWDG in the library function?

4. Library function manual

1. The LED should be on and off, so initialize the LED

void Led_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	
}

tricks:

1. Define the structure object, assign value to the object, and call xxxx_Init function.

2. Turn on the clock of GPIOA peripheral mounted on APB2

2. Use the delay function to initialize SysTick

Interrupt handling function:

void SysTick_Handler(void)
{
        if(TimingDelay != 0)
        TimingDelay--;
}

Initialize SysTick function:

void SysTick_Configuration(void)
{

	while(SysTick_Config(72));
	
	SysTick->CTRL &= ~(1<<0);
	
}

Using interrupt to realize delay function:

void Delay_us(unsigned int i)
{
	TimingDelay = i;
	SysTick->CTRL |= (1<<0);
	while(TimingDelay); 
	SysTick->CTRL &= ~(1<<0);
}

3. Initialize IWDG

void IWDG_Configuration(void)
{
	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
	IWDG_SetPrescaler(IWDG_Prescaler_64);
	IWDG_SetReload(625);
	IWDG_ReloadCounter();
	IWDG_Enable();
}

be careful:

1. Enable the write operation of PR and RLR, frequency division and reload value. The reload value is placed in the IWDG timer to enable IWDG

2. Reset time is one second: power frequency / frequency division rate * incoming parameters (example: 1000000)

4.PA0 is configured for dog feeding (reset) operation

1. Initialize KEY1

void Key_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
}

be careful:

1. Structure object, object assignment, Init function, start the clock mounted on APB2 by GPIOA

2.KAY1 on PA0

2. Configuration of dog feeding operation

Write 0xAAAA to KR (key register) and reset (feed dog).

void IWDG_EeedDog(void)
{
	IWDG_ReloadCounter();
}

be careful:

1.IWDG_ReloadCounter(); (reload counter function)

The default value of this function is 0xAAAA, that is, the parameter. Write 0xAAAA to KR register to realize reset. Therefore, directly calling this function realizes the dog feeding (reset) function.

 

 

5.main function writing

int main(void)
{
    SysTick_Configuration();
	Led_Configuration();¯
    IWDG_Configuration();	
    Key_Configuration();
	
	
	LED_TOGGLE;
    Delay_us(100000);
	LED_TOGGLE;
    Delay_us(100000);
	
	while(1)
	{
		if(Key_Scan(GPIOA, GPIO_Pin_0) == KEY_ON)
		IWDG_EeedDog();
	    Delay_us(10);
	}
}

explain:

1. Call of each function.

2. Before pressing the key, the LED flashes; After pressing the button (after dog feeding reset), the LED goes out.

6. Effect display

Tags: C stm32

Posted by eagle1771 on Fri, 15 Apr 2022 22:45:33 +0930