From cf636ec095f94f451436599eddc836c2783053e2 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Tue, 20 Jul 2021 18:23:42 +0300 Subject: [PATCH] Fix configuration compute() math --- src/hal/watchdog.h | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/hal/watchdog.h b/src/hal/watchdog.h index 84a839e..9a3edde 100644 --- a/src/hal/watchdog.h +++ b/src/hal/watchdog.h @@ -14,7 +14,7 @@ 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 maxPrescaler = 6; //the maximum value prescalerBits can take constexpr uint8_t reloadBits = 12; //number of bits in the reload register #endif @@ -24,16 +24,15 @@ struct configuration { 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) { + uint32_t ticks = timeout * F_WDT / (basePrescaler * (1 << prescalerBits)); + while ((ticks >= (1 << reloadBits)) && (prescalerBits < maxPrescaler)) { prescalerBits++; + ticks = timeout * F_WDT / (basePrescaler * (1 << prescalerBits)); } - if (timeout) - reload = static_cast(((timeout * (1 << reloadBits)) / (tickPeriod * (basePrescaler << prescalerBits)))) - 1; - - configuration config = { prescalerBits, reload }; + if ((prescalerBits == 0) && (ticks == 0)) + ticks = 1; //1 tick is minimum + configuration config = { prescalerBits, static_cast(ticks - 1) }; return config; } };