watchdog configuration

pull/67/head^2
Alex Voinea 2021-07-20 17:13:20 +03:00
parent a433db5648
commit 06b959bb66
4 changed files with 37 additions and 5 deletions

View File

@ -10,7 +10,7 @@ void Init() {
void Reset() { void Reset() {
cli(); cli();
watchdog::Enable(0); //minimum amount of watchdog watchdog::Enable(watchdog::configuration::compute(0)); //quickest watchdog reset
for (;;) for (;;)
; //endless loop while waiting for the watchdog to reset ; //endless loop while waiting for the watchdog to reset
} }

View File

@ -4,8 +4,8 @@
namespace hal { namespace hal {
namespace watchdog { namespace watchdog {
void Enable(uint16_t period) { void Enable(const configuration &config) {
// @@TODO wdt_enable(config.prescalerBits);
} }
void Disable() { void Disable() {

View File

@ -6,8 +6,40 @@ namespace hal {
/// Hardware Abstraction Layer for the CPU's internal watchdog /// Hardware Abstraction Layer for the CPU's internal watchdog
namespace watchdog { namespace watchdog {
#if defined(__AVR__)
constexpr uint32_t F_WDT = 128000; //frequency of the watchdog unit in Hz
constexpr uint32_t basePrescaler = 2048; //what prescalerBits==0 actually does.
constexpr uint8_t maxPrescaler = 9; //the maximum value prescalerBits can take
constexpr uint8_t reloadBits = 0; //number of bits in the reload register
#elif defined(__STM32__) //@todo to be changed to the final form
constexpr uint32_t F_WDT = 32000; //frequency of the watchdog unit in Hz
constexpr uint32_t basePrescaler = 4; //what prescalerBits==0 actually does.
constexpr uint8_t maxPrescaler = 7; //the maximum value prescalerBits can take
constexpr uint8_t reloadBits = 12; //number of bits in the reload register
#endif
struct configuration {
uint8_t prescalerBits;
uint16_t reload;
public:
static constexpr configuration compute(float timeout) {
constexpr float tickPeriod = 1 / (float)F_WDT;
uint8_t prescalerBits = 0;
uint16_t reload = 0;
while (tickPeriod * (basePrescaler << prescalerBits) < timeout) {
prescalerBits++;
}
if (timeout)
reload = static_cast<uint16_t>(((timeout * (1 << reloadBits)) / (tickPeriod * (basePrescaler << prescalerBits)))) - 1;
configuration config = { prescalerBits, reload };
return config;
}
};
/// watchdog interface /// watchdog interface
void Enable(uint16_t period); void Enable(const configuration &config);
void Disable(); void Disable();
void Reset(); void Reset();

View File

@ -97,7 +97,7 @@ void setup() {
mt::timebase.Init(); mt::timebase.Init();
watchdog::Enable(8000); //8s timeout watchdog::Enable(watchdog::configuration::compute(8)); //set 8s timeout
mg::globals.Init(); mg::globals.Init();