Merge pull request #67 from DRracer/watchdog
Add watchdog implementation + use it in main()pull/121/head
commit
1d1e2ef108
|
|
@ -8,4 +8,5 @@ target_sources(
|
||||||
tmc2130.cpp
|
tmc2130.cpp
|
||||||
adc.cpp
|
adc.cpp
|
||||||
avr/spi.cpp
|
avr/spi.cpp
|
||||||
|
avr/watchdog.cpp
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#include "../cpu.h"
|
#include "../cpu.h"
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include "../watchdog.h"
|
||||||
|
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace cpu {
|
namespace cpu {
|
||||||
|
|
@ -6,5 +8,12 @@ namespace cpu {
|
||||||
void Init() {
|
void Init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Reset() {
|
||||||
|
cli();
|
||||||
|
watchdog::Enable(watchdog::configuration::compute(0)); //quickest watchdog reset
|
||||||
|
for (;;)
|
||||||
|
; //endless loop while waiting for the watchdog to reset
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace CPU
|
} // namespace CPU
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
#include "../watchdog.h"
|
||||||
|
#include <avr/wdt.h>
|
||||||
|
|
||||||
|
namespace hal {
|
||||||
|
namespace watchdog {
|
||||||
|
|
||||||
|
void Enable(const configuration &config) {
|
||||||
|
wdt_enable(config.prescalerBits);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Disable() {
|
||||||
|
wdt_disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Reset() {
|
||||||
|
wdt_reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace watchdog
|
||||||
|
} // namespace hal
|
||||||
|
|
@ -12,6 +12,7 @@ namespace cpu {
|
||||||
|
|
||||||
/// CPU init routines (not really necessary for the AVR)
|
/// CPU init routines (not really necessary for the AVR)
|
||||||
void Init();
|
void Init();
|
||||||
|
void Reset();
|
||||||
|
|
||||||
} // namespace cpu
|
} // namespace cpu
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,55 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
namespace hal {
|
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 = 6; //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(uint16_t timeout) {
|
||||||
|
// uint8_t prescalerBits = 0;
|
||||||
|
// uint32_t ticks = timeout * F_WDT / (basePrescaler * (1 << prescalerBits));
|
||||||
|
// while ((ticks >= (1 << reloadBits)) && (prescalerBits < maxPrescaler)) {
|
||||||
|
// prescalerBits++;
|
||||||
|
// ticks >>= 1;
|
||||||
|
// }
|
||||||
|
// if ((prescalerBits == 0) && (ticks == 0))
|
||||||
|
// ticks = 1; //1 tick is minimum
|
||||||
|
// configuration config = { prescalerBits, static_cast<uint16_t>(ticks - 1) };
|
||||||
|
// return config;
|
||||||
|
uint8_t prescalerBits = 0;
|
||||||
|
uint32_t ticks = 1;
|
||||||
|
switch (timeout) {
|
||||||
|
case 8000:
|
||||||
|
prescalerBits = 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
configuration config = { prescalerBits, static_cast<uint16_t>(ticks - 1) };
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// watchdog interface
|
/// watchdog interface
|
||||||
void ConfigureWatchDog(uint16_t period);
|
void Enable(const configuration &config);
|
||||||
void ResetWatchDog();
|
void Disable();
|
||||||
|
void Reset();
|
||||||
|
|
||||||
} // namespace watchdog
|
} // namespace watchdog
|
||||||
} // namespace hal
|
} // namespace hal
|
||||||
|
|
|
||||||
11
src/main.cpp
11
src/main.cpp
|
|
@ -4,6 +4,7 @@
|
||||||
#include "hal/shr16.h"
|
#include "hal/shr16.h"
|
||||||
#include "hal/spi.h"
|
#include "hal/spi.h"
|
||||||
#include "hal/usart.h"
|
#include "hal/usart.h"
|
||||||
|
#include "hal/watchdog.h"
|
||||||
|
|
||||||
#include "pins.h"
|
#include "pins.h"
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
|
@ -96,6 +97,8 @@ void setup() {
|
||||||
|
|
||||||
mt::timebase.Init();
|
mt::timebase.Init();
|
||||||
|
|
||||||
|
watchdog::Enable(watchdog::configuration::compute(8000)); //set 8s timeout
|
||||||
|
|
||||||
mg::globals.Init();
|
mg::globals.Init();
|
||||||
|
|
||||||
// watchdog init
|
// watchdog init
|
||||||
|
|
@ -291,12 +294,13 @@ void ProcessRequestMsg(const mp::RequestMsg &rq) {
|
||||||
break;
|
break;
|
||||||
case mp::RequestMsgCodes::Reset:
|
case mp::RequestMsgCodes::Reset:
|
||||||
// immediately reset the board - there is no response in this case
|
// immediately reset the board - there is no response in this case
|
||||||
break; // @@TODO
|
hal::cpu::Reset();
|
||||||
|
break;
|
||||||
case mp::RequestMsgCodes::Version:
|
case mp::RequestMsgCodes::Version:
|
||||||
ReportVersion(rq);
|
ReportVersion(rq);
|
||||||
break;
|
break;
|
||||||
case mp::RequestMsgCodes::Wait:
|
case mp::RequestMsgCodes::Wait:
|
||||||
break; // @@TODO
|
break; // @@TODO - not used anywhere yet
|
||||||
case mp::RequestMsgCodes::Cut:
|
case mp::RequestMsgCodes::Cut:
|
||||||
case mp::RequestMsgCodes::Eject:
|
case mp::RequestMsgCodes::Eject:
|
||||||
case mp::RequestMsgCodes::Load:
|
case mp::RequestMsgCodes::Load:
|
||||||
|
|
@ -363,7 +367,8 @@ void loop() {
|
||||||
ms::selector.Step();
|
ms::selector.Step();
|
||||||
mui::userInput.Step();
|
mui::userInput.Step();
|
||||||
currentCommand->Step();
|
currentCommand->Step();
|
||||||
// add a watchdog reset
|
|
||||||
|
hal::watchdog::Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue