diff --git a/stm-firmware/safety/watchdog.c b/stm-firmware/safety/watchdog.c index 845198a..a4f1f8c 100644 --- a/stm-firmware/safety/watchdog.c +++ b/stm-firmware/safety/watchdog.c @@ -70,15 +70,24 @@ int watchdog_setup(uint8_t prescaler) /** - Unlock registers */ IWDG->KR = STM32_WATCHDOG_REGISTER_ACCESS_KEY; + /** - Wait until prescaler can be written */ + while (IWDG->SR & IWDG_SR_PVU); + /** - Write prescaler value */ IWDG->PR = prescaler_reg_val; + /* - Wait until reload value can be written */ + while (IWDG->SR & IWDG_SR_RVU); + /** - Set reload value fixed to 0xFFF */ IWDG->RLR = 0xFFFU; /** - Write enable key */ IWDG->KR = STM32_WATCHDOG_ENABLE_KEY; + /** - Do a first reset of the counter. This also locks the config regs */ + watchdog_ack(WATCHDOG_MAGIC_KEY); + return 0; }