aboutsummaryrefslogtreecommitdiffstats
path: root/sw/main.cpp
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2019-10-19 14:36:42 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2019-10-19 14:36:42 +0200
commitbf92e6739056e3a4b46a0febbddd860d32096d81 (patch)
tree3b3f482c253b1b7b4f973c790294e926d24b83dc /sw/main.cpp
parentf88680cdfd045ebc2e4c1bf2a25b32e14123b6fc (diff)
downloadglutte-batteries-bf92e6739056e3a4b46a0febbddd860d32096d81.tar.gz
glutte-batteries-bf92e6739056e3a4b46a0febbddd860d32096d81.tar.bz2
glutte-batteries-bf92e6739056e3a4b46a0febbddd860d32096d81.zip
Add watchdog
Diffstat (limited to 'sw/main.cpp')
-rw-r--r--sw/main.cpp13
1 files changed, 13 insertions, 0 deletions
diff --git a/sw/main.cpp b/sw/main.cpp
index b364b92..48871ec 100644
--- a/sw/main.cpp
+++ b/sw/main.cpp
@@ -30,6 +30,7 @@
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
+#include <avr/wdt.h>
// Definitions allocation pin
#define PIN(x) (1 << x)
@@ -97,6 +98,10 @@ uint32_t current_capacity;
volatile uint8_t timer_counter; /* Timer in 100ms steps */
volatile uint32_t timer_seconds; /* Timer in seconds */
+/* At reset, save the mcusr register to find out why we got reset.
+ * Datasheet 11.9.1, example code from wdt.h */
+uint8_t mcusr_mirror __attribute__ ((section (".noinit")));
+
ISR(TIMER0_COMPA_vect)
{
timer_counter++;
@@ -162,6 +167,14 @@ static void store_capacity_to_eeprom()
int main()
{
+ /* Save the reset source for debugging, then enable the watchdog.
+ * Attention: WDT may be already enabled if it triggered a reset!
+ * Datasheet 11.8.2 */
+ mcusr_mirror = MCUSR;
+ MCUSR = 0;
+ wdt_reset();
+ wdt_enable(WDTO_4S);
+
/* Setup GPIO */
// Active-low outputs must be high
PORTB = PINB_STATUSn | PINB_SPI_LTC_CSn;