Add C++ independent watchdog driver

This commit is contained in:
Thomas Klaehn 2021-01-08 10:44:56 +01:00
parent cca848eec0
commit 258c882c8c
2 changed files with 65 additions and 0 deletions

View File

@ -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;
}

View File

@ -0,0 +1,24 @@
#ifndef __PLATFORM_STM32G0XX_INDEPENDENTWATCHDOG_H__
#define __PLATFORM_STM32G0XX_INDEPENDENTWATCHDOG_H__
#include <cstdint>
#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