diff --git a/platform/stm32g0xx/IndependentWatchdog.cc b/platform/stm32g0xx/IndependentWatchdog.cc new file mode 100644 index 0000000..45e092f --- /dev/null +++ b/platform/stm32g0xx/IndependentWatchdog.cc @@ -0,0 +1,41 @@ +#include "platform/stm32g0xx/IndependentWatchdog.h" + +using namespace perinet::platform::stm32g0xx; + +IndependentWatchdog::IndependentWatchdog(uint32_t window, uint32_t reload) + : window(window) + , reload(reload) +{ +} + +void IndependentWatchdog::enable() +{ + // Enable IWDG. LSI is turned on automatically + IWDG->KR = IWDG_KEY_ENABLE; + + // Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing 0x5555 in KR + IWDG->KR = IWDG_KEY_WRITE_ACCESS_ENABLE; + + // Write to IWDG registers the Prescaler & Reload values to work with + IWDG->PR = IWDG_PRESCALER_4; + IWDG->RLR = this->reload; + + // If window parameter is different than current value, modify window register + if (IWDG->WINR != this->window) { + // Write to IWDG WINR the IWDG_Window value to compare with. In any case, + // even if window feature is disabled, Watchdog will be reloaded by writing + // windows register + IWDG->WINR = this->window; + } + else { + // Reload IWDG counter with value defined in the reload register + IWDG->KR = IWDG_KEY_RELOAD; + } + +} + +void IndependentWatchdog::trigger() +{ + // Reload IWDG counter with value defined in the reload register + IWDG->KR = IWDG_KEY_RELOAD; +} diff --git a/platform/stm32g0xx/IndependentWatchdog.h b/platform/stm32g0xx/IndependentWatchdog.h new file mode 100644 index 0000000..7390402 --- /dev/null +++ b/platform/stm32g0xx/IndependentWatchdog.h @@ -0,0 +1,24 @@ +#ifndef __PLATFORM_STM32G0XX_INDEPENDENTWATCHDOG_H__ +#define __PLATFORM_STM32G0XX_INDEPENDENTWATCHDOG_H__ + +#include + +#include "stm32g0xx.h" + +namespace perinet::platform::stm32g0xx { + +class IndependentWatchdog +{ +public: + IndependentWatchdog(uint32_t, uint32_t); + void enable(); + void trigger(); + +private: + uint32_t window; + uint32_t reload; +}; + +} + +#endif