2025-05-08 7:15 PM
Hi All,
I had the need to use the independent watchdog timer and rather than a straight reset of the system I wished to put the system into a fault mode. You can do this by examining the RCC control and status register (CSR) viz:
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* TODO: */
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
MX_USART2_UART_Init();
checkIWDG();
#if(IWDG_ENABLE == 1)
void checkIWDG(void)
{
if(RCC->CSR & RCC_CSR_IWDGRSTF)
{
/*** FAULT MODE ***/
/* The IWDG has reset the system */
printf("IWDG Reset\n\r");
/* Run fans flat out */
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pins : PA9_FAN1_POWER_Pin PA10_FAN2_POWER_Pin */
GPIO_InitStruct.Pin = PA9_FAN1_POWER_Pin|PA10_FAN2_POWER_Pin|PA11_TEST_OUTPUT_Pin|PA6_TIM3_CH1_FAN1_SPEED_DEMAND1_Pin|PA7_TIM3_CH2_FAN1_SPEED_DEMAND2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Set the GPIO Pins */
HAL_GPIO_WritePin(PA10_FAN2_POWER_GPIO_Port, PA10_FAN2_POWER_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(PA9_FAN1_POWER_GPIO_Port, PA9_FAN1_POWER_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(PA6_TIM3_CH1_FAN1_SPEED_DEMAND1_GPIO_Port, PA6_TIM3_CH1_FAN1_SPEED_DEMAND1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(PA7_TIM3_CH2_FAN1_SPEED_DEMAND2_GPIO_Port, PA7_TIM3_CH2_FAN1_SPEED_DEMAND2_Pin, GPIO_PIN_RESET);
while(1)
{
printf("Watchdog Timed out\n\r");
HAL_IWDG_Refresh(&hiwdg);
HAL_Delay(20000);
}
}
else
{
/*** OPERATING MODE (No Fault) ***/
printf("No IWDG Reset\n\r");
}
}
#endif
In my example I cam controlling a couple of computer fans each with a relay and a demand signal. If the fan algorithm fails for any reason after 32 seconds the watchdog will reset the cpu and the RCC_CSR_IWDGRSTF gets set. This forces places the system into a "fans full on mode" and it loops continuously in this mode.
When debugging if the RCC_CSR_IWDGRSTF gets set you can only clear it by a power down reset. Restarting the debugger which uses the reset pin on the cpu will not clear the RCC_CSR_IWDGRSTF bit.
Note that if you wish to use the virtual com port to print messages you will need to initialise the uart before checking the RCC_CSR_IWDGRSTF.
Hope someone finds this useful.
Regards
Rob