@@ -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.
5667void 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- }
0 commit comments