Skip to content

Commit 00043b1

Browse files
authored
Merge branch 'release_candidate' into main
2 parents c8f196a + 0968f4c commit 00043b1

File tree

4 files changed

+81
-47
lines changed

4 files changed

+81
-47
lines changed
361 KB
Binary file not shown.

Firmware/OpenLog_Artemis/OpenLog_Artemis.ino

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ const int FIRMWARE_VERSION_MINOR = 3;
152152
// the minor firmware version
153153
#define OLA_IDENTIFIER 0x123 // Stored as 291 decimal in OLA_settings.txt
154154

155+
//#define noPowerLossProtection // Uncomment this line to disable the sleep-on-power-loss functionality
156+
155157
#include "Sensors.h"
158+
156159
#include "settings.h"
157160

158161
//Define the pin functions
@@ -358,15 +361,53 @@ Stream *ZSERIAL;
358361
// Serial output for debugging info for Zmodem
359362
Stream *DSERIAL;
360363

364+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
365+
#include "WDT.h" // WDT support
366+
367+
volatile static bool petTheDog = true; // Flag to control whether the WDT ISR pets (resets) the timer.
368+
369+
// Interrupt handler for the watchdog.
370+
extern "C" void am_watchdog_isr(void)
371+
{
372+
// Clear the watchdog interrupt.
373+
wdt.clear();
374+
375+
// Restart the watchdog if petTheDog is true
376+
if (petTheDog)
377+
wdt.restart(); // "Pet" the dog.
378+
}
379+
380+
void startWatchdog()
381+
{
382+
// Set watchdog timer clock to 16 Hz
383+
// Set watchdog interrupt to 1 seconds (16 ticks / 16 Hz = 1 second)
384+
// Set watchdog reset to 1.25 seconds (20 ticks / 16 Hz = 1.25 seconds)
385+
// Note: Ticks are limited to 255 (8-bit)
386+
wdt.configure(WDT_16HZ, 16, 20);
387+
wdt.start(); // Start the watchdog
388+
}
389+
390+
void stopWatchdog()
391+
{
392+
wdt.stop();
393+
}
394+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
395+
361396
void setup() {
362397
//If 3.3V rail drops below 3V, system will power down and maintain RTC
363398
pinMode(PIN_POWER_LOSS, INPUT); // BD49K30G-TL has CMOS output and does not need a pull-up
364399

365400
delay(1); // Let PIN_POWER_LOSS stabilize
366401

402+
#ifndef noPowerLossProtection
367403
if (digitalRead(PIN_POWER_LOSS) == LOW) powerDownOLA(); //Check PIN_POWER_LOSS just in case we missed the falling edge
368404
//attachInterrupt(PIN_POWER_LOSS, powerDownOLA, FALLING); // We can't do this with v2.1.0 as attachInterrupt causes a spontaneous interrupt
369405
attachInterrupt(PIN_POWER_LOSS, powerLossISR, FALLING);
406+
#else
407+
// No Power Loss Protection
408+
// Set up the WDT to generate a reset just in case the code crashes during a brown-out
409+
startWatchdog();
410+
#endif
370411
powerLossSeen = false; // Make sure the flag is clear
371412

372413
powerLEDOn(); // Turn the power LED on - if the hardware supports it

Firmware/OpenLog_Artemis/lowerPower.ino

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,21 @@ void checkBattery(void)
3030

3131
delay(sdPowerDownDelay); // Give the SD card time to finish writing ***** THIS IS CRITICAL *****
3232

33+
#ifdef noPowerLossProtection
34+
SerialPrintln(F("*** LOW BATTERY VOLTAGE DETECTED! RESETTING... ***"));
35+
SerialPrintln(F("*** PLEASE CHANGE THE POWER SOURCE TO CONTINUE ***"));
36+
37+
SerialFlush(); //Finish any prints
38+
39+
resetArtemis(); // Reset the Artemis so we don't get stuck in a low voltage infinite loop
40+
#else
3341
SerialPrintln(F("*** LOW BATTERY VOLTAGE DETECTED! GOING INTO POWERDOWN ***"));
3442
SerialPrintln(F("*** PLEASE CHANGE THE POWER SOURCE AND RESET THE OLA TO CONTINUE ***"));
3543

3644
SerialFlush(); //Finish any prints
3745

38-
powerDownOLA(); // power down and wait for reset
46+
powerDownOLA(); // Power down and wait for reset
47+
#endif
3948
}
4049
}
4150
else
@@ -45,8 +54,10 @@ void checkBattery(void)
4554
}
4655
#endif
4756

57+
#ifndef noPowerLossProtection // Redundant - since the interrupt is not attached if noPowerLossProtection is defined... But you never know...
4858
if (powerLossSeen)
4959
powerDownOLA(); // power down and wait for reset
60+
#endif
5061
}
5162

5263
//Power down the entire system but maintain running of RTC
@@ -55,8 +66,10 @@ void checkBattery(void)
5566
//With leakage across the 3.3V protection diode, it's approx 3.00uA.
5667
void powerDownOLA(void)
5768
{
69+
#ifndef noPowerLossProtection // Probably redundant - included just in case detachInterrupt causes badness when it has not been attached
5870
//Prevent voltage supervisor from waking us from sleep
5971
detachInterrupt(PIN_POWER_LOSS);
72+
#endif
6073

6174
//Prevent stop logging button from waking us from sleep
6275
if (settings.useGPIO32ForStopLogging == true)
@@ -140,6 +153,10 @@ void powerDownOLA(void)
140153
qwiicPowerOff();
141154
#endif
142155

156+
#ifdef noPowerLossProtection // If noPowerLossProtection is defined, then the WDT will already be running
157+
stopWatchdog();
158+
#endif
159+
143160
//Power down cache, flash, SRAM
144161
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_ALL); // Power down all flash and cache
145162
am_hal_pwrctrl_memory_deepsleep_retain(AM_HAL_PWRCTRL_MEM_SRAM_384K); // Retain all SRAM
@@ -222,7 +239,10 @@ void resetArtemis(void)
222239
powerLEDOff();
223240

224241
//Enable the Watchdog so it can reset the Artemis
225-
startWatchdog();
242+
petTheDog = false; // Make sure the WDT will not restart
243+
#ifndef noPowerLossProtection // If noPowerLossProtection is defined, then the WDT will already be running
244+
startWatchdog(); // Start the WDT to generate a reset
245+
#endif
226246
while (1) // That's all folks! Artemis will reset in 1.25 seconds
227247
;
228248
}
@@ -232,8 +252,10 @@ void goToSleep(uint32_t sysTicksToSleep)
232252
{
233253
printDebug("goToSleep: sysTicksToSleep = " + (String)sysTicksToSleep + "\r\n");
234254

255+
#ifndef noPowerLossProtection // Probably redundant - included just in case detachInterrupt causes badness when it has not been attached
235256
//Prevent voltage supervisor from waking us from sleep
236257
detachInterrupt(PIN_POWER_LOSS);
258+
#endif
237259

238260
//Prevent stop logging button from waking us from sleep
239261
if (settings.useGPIO32ForStopLogging == true)
@@ -351,6 +373,13 @@ void goToSleep(uint32_t sysTicksToSleep)
351373
else
352374
powerLEDOff();
353375

376+
377+
#ifdef noPowerLossProtection
378+
// If noPowerLossProtection is defined, then the WDT will be running
379+
// We need to stop it otherwise it will wake the Artemis
380+
stopWatchdog();
381+
#endif
382+
354383
//Power down cache, flash, SRAM
355384
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_ALL); // Power down all flash and cache
356385
am_hal_pwrctrl_memory_deepsleep_retain(AM_HAL_PWRCTRL_MEM_SRAM_384K); // Retain all SRAM
@@ -420,12 +449,17 @@ void wakeFromSleep()
420449

421450
delay(1); // Let PIN_POWER_LOSS stabilize
422451

452+
#ifndef noPowerLossProtection
423453
if (digitalRead(PIN_POWER_LOSS) == LOW) powerDownOLA(); //Check PIN_POWER_LOSS just in case we missed the falling edge
424-
425454
//attachInterrupt(PIN_POWER_LOSS, powerDownOLA, FALLING); // We can't do this with v2.1.0 as attachInterrupt causes a spontaneous interrupt
426455
attachInterrupt(PIN_POWER_LOSS, powerLossISR, FALLING);
456+
#else
457+
// No Power Loss Protection
458+
// Set up the WDT to generate a reset just in case the code crashes during a brown-out
459+
startWatchdog();
460+
#endif
427461
powerLossSeen = false; // Make sure the flag is clear
428-
462+
429463
if (settings.useGPIO32ForStopLogging == true)
430464
{
431465
pinMode(PIN_STOP_LOGGING, INPUT_PULLUP);
@@ -704,46 +738,3 @@ int calculateDayOfYear(int day, int month, int year)
704738
doy += day;
705739
return doy;
706740
}
707-
708-
//WatchDog Timer code by Adam Garbo:
709-
//https://forum.sparkfun.com/viewtopic.php?f=169&t=52431&p=213296#p213296
710-
711-
// Watchdog timer configuration structure.
712-
am_hal_wdt_config_t g_sWatchdogConfig = {
713-
714-
// Configuration values for generated watchdog timer event.
715-
.ui32Config = AM_HAL_WDT_LFRC_CLK_16HZ | AM_HAL_WDT_ENABLE_RESET | AM_HAL_WDT_ENABLE_INTERRUPT,
716-
717-
// Number of watchdog timer ticks allowed before a watchdog interrupt event is generated.
718-
.ui16InterruptCount = 16, // Set WDT interrupt timeout for 1 second.
719-
720-
// Number of watchdog timer ticks allowed before the watchdog will issue a system reset.
721-
.ui16ResetCount = 20 // Set WDT reset timeout for 1.25 seconds.
722-
};
723-
724-
void startWatchdog()
725-
{
726-
// LFRC must be turned on for this example as the watchdog only runs off of the LFRC.
727-
am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_LFRC_START, 0);
728-
729-
// Configure the watchdog.
730-
am_hal_wdt_init(&g_sWatchdogConfig);
731-
732-
// Enable the interrupt for the watchdog in the NVIC.
733-
NVIC_EnableIRQ(WDT_IRQn);
734-
//NVIC_SetPriority(WDT_IRQn, 0); // Set the interrupt priority to 0 = highest (255 = lowest)
735-
//am_hal_interrupt_master_enable(); // ap3_initialization.cpp does this - no need to do it here
736-
737-
// Enable the watchdog.
738-
am_hal_wdt_start();
739-
}
740-
741-
// Interrupt handler for the watchdog.
742-
extern "C++" void am_watchdog_isr(void)
743-
{
744-
// Clear the watchdog interrupt.
745-
am_hal_wdt_int_clear();
746-
747-
// DON'T Restart the watchdog.
748-
//am_hal_wdt_restart(); // "Pet" the dog.
749-
}

Firmware/OpenLog_Artemis/productionTest.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,9 @@ void productionTest()
562562
break;
563563
case 0x55: // Deep sleep
564564
{
565+
#ifndef noPowerLossProtection // Probably redundant - included just in case detachInterrupt causes badness when it has not been attached
565566
detachInterrupt(PIN_POWER_LOSS); // Disable power loss interrupt
567+
#endif
566568
Serial.end(); //Power down UART
567569
//Force the peripherals off
568570
//am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM0);

0 commit comments

Comments
 (0)