fix watchdog + library independence

This commit is contained in:
r4 2022-04-18 19:31:51 +02:00
parent 3101c3add6
commit 232df37f5c

View File

@ -29,12 +29,12 @@
*/ */
#include <EEPROM.h> #include <EEPROM.h>
#include <LowPower.h> /* https://github.com/rocketscream/Low-Power */
#include <SD.h> #include <SD.h>
#include <SPI.h> #include <SPI.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/wdt.h> #include <avr/wdt.h>
#include <avr/sleep.h>
/************************ /************************
BEGIN USER CONFIGURATION BEGIN USER CONFIGURATION
@ -61,6 +61,8 @@
END USER CONFIGURATION END USER CONFIGURATION
**********************/ **********************/
void (*full_reset)() = nullptr;
static int serial_putch(char c, FILE *f) { static int serial_putch(char c, FILE *f) {
(void)f; (void)f;
return Serial.write(c) == 1 ? 0 : 1; return Serial.write(c) == 1 ? 0 : 1;
@ -117,20 +119,40 @@ static bool fstreq(const char *a, const __FlashStringHelper *b_fsh) {
#define info(fmt, ...) { if (settings.serial_log) printf(fmt, ##__VA_ARGS__); } #define info(fmt, ...) { if (settings.serial_log) printf(fmt, ##__VA_ARGS__); }
#define info_special(x) { if (settings.serial_log) Serial.print(x); } #define info_special(x) { if (settings.serial_log) Serial.print(x); }
static void low_power_sleep_minutes(unsigned long t) { volatile bool wdt_int_sleep_mode = false;
for (unsigned long i = 0; 8ul * i < 60ul * t; i++) { ISR (WDT_vect) {
/* Power down for 8s. */ if (wdt_int_sleep_mode)
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); wdt_disable();
} else
full_reset();
} }
static void start_watchdog_with_full_reset() { /* Based on https://github.com/rocketscream/Low-Power. */
MCUSR &= ~B00001000; /* Clear reset flag. */ static void low_power_sleep_minutes(unsigned long t) {
WDTCSR |= B00011000; /* Prepare prescaler change. */ wdt_int_sleep_mode = true;
WDTCSR = B00100001; /* Set watchdog timeout to 8s. */ ADCSRA &= ~(1 << ADEN); /* Disable ADC. */
// Enable Watchdog Timer for (unsigned long i = 0; 8ul * i < 60ul * t; i++) {
WDTCSR |= B01000000; // Power Down for 8s
MCUSR = MCUSR & B11110111; wdt_enable(WDTO_8S); /* Start watchdog timer for 8s. */
WDTCSR |= (1 << WDIE); /* Enable watchdog interrupt. */
do {
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
cli();
sleep_enable();
sleep_bod_disable();
sei();
sleep_cpu();
sleep_disable();
sei();
} while (0);
}
ADCSRA |= (1 << ADEN); /* Re-enable ADC. */
wdt_int_sleep_mode = false;
}
static void wdt_enable_with_full_reset() {
wdt_enable(WDTO_8S); /* Start watchdog timer for 8s. */
WDTCSR |= (1 << WDIE); /* Enable watchdog interrupt. */
} }
static inline void disable_recording_interrupts() { static inline void disable_recording_interrupts() {
@ -397,7 +419,7 @@ void setup() {
delay(500); /* Wait for components to initialize. */ delay(500); /* Wait for components to initialize. */
#endif #endif
// Start Watchdog (wdt_enable() doesn't fully reset) // Start Watchdog (wdt_enable() doesn't fully reset)
start_watchdog_with_full_reset(); wdt_enable_with_full_reset();
// SD Card Setup // SD Card Setup
if (!SD.begin(PIN_SS)) if (!SD.begin(PIN_SS))
die(F("Error initializing SD card!\n")); die(F("Error initializing SD card!\n"));
@ -440,6 +462,7 @@ void setup() {
| _BV(OCIE1B); /* Enable "Output Compare B Match Interrupt". */ | _BV(OCIE1B); /* Enable "Output Compare B Match Interrupt". */
} }
void loop() { void loop() {
delay(2000); delay(2000);
wdt_reset(); /* Reset watchdog timer. */ wdt_reset(); /* Reset watchdog timer. */