void GPIO_setup(void)
{
GPIO_DeInit(GPIOB);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOB, GPIO_PIN_7, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST);
}
void IWDG_setup(void)
{
IWDG_Enable();
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(IWDG_Prescaler_128);
IWDG_SetReload(0x99);
IWDG_WriteAccessCmd(IWDG_WriteAccess_Disable);
}
Explanation
In this example, we need not to look at peripheral and CPU clock as IWDG is not dependent on them.
Still we can see that the CPU is running at 500kHz speed while the peripherals at 2MHz speed.
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV4);
To setup the IWDG, we need to enable it first and then apply Write Access Protection key (0x55). We
just need to set the prescaler and the counter value. The down counter will start from this value and
count down to zero unless refreshed. In this example, the prescaler is set to 128 and reload value is
set to 153 (0x99). Thus, with these we get a timeout of approximately 300ms. After entering these
values we must prevent accidental changes in the firmware and so to do so the write access must be
disabled.
void IWDG_setup(void)
{
IWDG_Enable();
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(IWDG_Prescaler_128);
IWDG_SetReload(0x99);
IWDG_WriteAccessCmd(IWDG_WriteAccess_Disable);
}
Disco board's user button and LED are used for the demo. At the very beginning, the LED is lit for some
time before the IWDG is configured, indicating the start of the firmware. In the main loop, the LED is
toggled with some delay arranged by a for loop. Inside the loop, the button's state is polled. If the
button is kept pressed it will always be in logic low state, reloading the IWDG counter. If its state
changes to logic high and 300ms passes out, a reset is triggered.
GPIO_WriteReverse(GPIOD, GPIO_PIN_0);
for(t = 0; t < 1000; t++)
{
if(GPIO_ReadInputPin(GPIOB, GPIO_PIN_7) == FALSE)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_ReloadCounter();
IWDG_WriteAccessCmd(IWDG_WriteAccess_Disable);
}
}
Need help?
Do you have a question about the STM8 and is the answer not in the manual?
Questions and answers