watchdog configuration
parent
a433db5648
commit
06b959bb66
|
|
@ -10,7 +10,7 @@ void Init() {
|
|||
|
||||
void Reset() {
|
||||
cli();
|
||||
watchdog::Enable(0); //minimum amount of watchdog
|
||||
watchdog::Enable(watchdog::configuration::compute(0)); //quickest watchdog reset
|
||||
for (;;)
|
||||
; //endless loop while waiting for the watchdog to reset
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@
|
|||
namespace hal {
|
||||
namespace watchdog {
|
||||
|
||||
void Enable(uint16_t period) {
|
||||
// @@TODO
|
||||
void Enable(const configuration &config) {
|
||||
wdt_enable(config.prescalerBits);
|
||||
}
|
||||
|
||||
void Disable() {
|
||||
|
|
|
|||
|
|
@ -6,8 +6,40 @@ namespace hal {
|
|||
/// Hardware Abstraction Layer for the CPU's internal 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
|
||||
void Enable(uint16_t period);
|
||||
void Enable(const configuration &config);
|
||||
void Disable();
|
||||
void Reset();
|
||||
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ void setup() {
|
|||
|
||||
mt::timebase.Init();
|
||||
|
||||
watchdog::Enable(8000); //8s timeout
|
||||
watchdog::Enable(watchdog::configuration::compute(8)); //set 8s timeout
|
||||
|
||||
mg::globals.Init();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue