Fix configuration compute() math

pull/67/head
Alex Voinea 2021-07-20 18:23:42 +03:00 committed by D.R.racer
parent 0e9802c4cd
commit 89a2bdc7e4
1 changed files with 7 additions and 8 deletions

View File

@ -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 #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 F_WDT = 32000; //frequency of the watchdog unit in Hz
constexpr uint32_t basePrescaler = 4; //what prescalerBits==0 actually does. 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 constexpr uint8_t reloadBits = 12; //number of bits in the reload register
#endif #endif
@ -24,16 +24,15 @@ struct configuration {
public: public:
static constexpr configuration compute(float timeout) { static constexpr configuration compute(float timeout) {
constexpr float tickPeriod = 1 / (float)F_WDT;
uint8_t prescalerBits = 0; uint8_t prescalerBits = 0;
uint16_t reload = 0; uint32_t ticks = timeout * F_WDT / (basePrescaler * (1 << prescalerBits));
while (tickPeriod * (basePrescaler << prescalerBits) < timeout) { while ((ticks >= (1 << reloadBits)) && (prescalerBits < maxPrescaler)) {
prescalerBits++; prescalerBits++;
ticks = timeout * F_WDT / (basePrescaler * (1 << prescalerBits));
} }
if (timeout) if ((prescalerBits == 0) && (ticks == 0))
reload = static_cast<uint16_t>(((timeout * (1 << reloadBits)) / (tickPeriod * (basePrescaler << prescalerBits)))) - 1; ticks = 1; //1 tick is minimum
configuration config = { prescalerBits, static_cast<uint16_t>(ticks - 1) };
configuration config = { prescalerBits, reload };
return config; return config;
} }
}; };