@@ -153,6 +153,77 @@ void powerDown()
153153 }
154154}
155155
156+ // Reset the Artemis
157+ void resetArtemis (void )
158+ {
159+ // Save files before resetting
160+ if (online.dataLogging == true )
161+ {
162+ sensorDataFile.sync ();
163+ updateDataFileAccess (&sensorDataFile); // Update the file access time & date
164+ sensorDataFile.close (); // No need to close files. https://forum.arduino.cc/index.php?topic=149504.msg1125098#msg1125098
165+ }
166+ if (online.serialLogging == true )
167+ {
168+ serialDataFile.sync ();
169+ updateDataFileAccess (&serialDataFile); // Update the file access time & date
170+ serialDataFile.close ();
171+ }
172+
173+ delay (sdPowerDownDelay); // Give the SD card time to finish writing ***** THIS IS CRITICAL *****
174+
175+ SerialFlush (); // Finish any prints
176+
177+ // Wire.end(); //Power down I2C
178+ qwiic.end (); // Power down I2C
179+
180+ SPI.end (); // Power down SPI
181+
182+ power_adc_disable (); // Power down ADC. It it started by default before setup().
183+
184+ Serial.end (); // Power down UART
185+ SerialLog.end ();
186+
187+ // Force the peripherals off
188+ am_hal_pwrctrl_periph_disable (AM_HAL_PWRCTRL_PERIPH_IOM0);
189+ am_hal_pwrctrl_periph_disable (AM_HAL_PWRCTRL_PERIPH_IOM1);
190+ am_hal_pwrctrl_periph_disable (AM_HAL_PWRCTRL_PERIPH_IOM2);
191+ am_hal_pwrctrl_periph_disable (AM_HAL_PWRCTRL_PERIPH_IOM3);
192+ am_hal_pwrctrl_periph_disable (AM_HAL_PWRCTRL_PERIPH_IOM4);
193+ am_hal_pwrctrl_periph_disable (AM_HAL_PWRCTRL_PERIPH_IOM5);
194+ am_hal_pwrctrl_periph_disable (AM_HAL_PWRCTRL_PERIPH_ADC);
195+ am_hal_pwrctrl_periph_disable (AM_HAL_PWRCTRL_PERIPH_UART0);
196+ am_hal_pwrctrl_periph_disable (AM_HAL_PWRCTRL_PERIPH_UART1);
197+
198+ // Disable pads
199+ for (int x = 0 ; x < 50 ; x++)
200+ {
201+ if ((x != ap3_gpio_pin2pad (PIN_POWER_LOSS)) &&
202+ // (x != ap3_gpio_pin2pad(PIN_LOGIC_DEBUG)) &&
203+ (x != ap3_gpio_pin2pad (PIN_MICROSD_POWER)) &&
204+ (x != ap3_gpio_pin2pad (PIN_QWIIC_POWER)) &&
205+ (x != ap3_gpio_pin2pad (PIN_IMU_POWER)))
206+ {
207+ am_hal_gpio_pinconfig (x, g_AM_HAL_GPIO_DISABLE);
208+ }
209+ }
210+
211+ // We can't leave these power control pins floating
212+ imuPowerOff ();
213+ microSDPowerOff ();
214+
215+ // Disable power for the Qwiic bus
216+ qwiicPowerOff ();
217+
218+ // Disable the power LED
219+ powerLEDOff ();
220+
221+ // Enable the Watchdog so it can reset the Artemis
222+ startWatchdog ();
223+ while (1 ) // That's all folks! Artemis will reset in 1.25 seconds
224+ ;
225+ }
226+
156227// Power everything down and wait for interrupt wakeup
157228void goToSleep (uint32_t sysTicksToSleep)
158229{
@@ -360,11 +431,11 @@ void wakeFromSleep()
360431 SerialLog.begin (settings.serialTerminalBaudRate ); // Start the serial port
361432 }
362433
363- printDebug (" wakeFromSleep: I'm awake!\r\n " );
434+ printDebug (F ( " wakeFromSleep: I'm awake!\r\n " ) );
364435 printDebug (" wakeFromSleep: adcError is " + (String)adcError + " ." );
365436 if (adcError > 0 )
366- printDebug (" This indicates an error was returned by ap3_adc_setup or one of the calls to ap3_set_pin_to_analog." );
367- printDebug (" \r\n " );
437+ printDebug (F ( " This indicates an error was returned by ap3_adc_setup or one of the calls to ap3_set_pin_to_analog." ) );
438+ printDebug (F ( " \r\n " ) );
368439
369440 beginQwiic (); // Power up Qwiic bus as early as possible
370441
@@ -560,3 +631,46 @@ int calculateDayOfYear(int day, int month, int year)
560631 doy += day;
561632 return doy;
562633}
634+
635+ // WatchDog Timer code by Adam Garbo:
636+ // https://forum.sparkfun.com/viewtopic.php?f=169&t=52431&p=213296#p213296
637+
638+ // Watchdog timer configuration structure.
639+ am_hal_wdt_config_t g_sWatchdogConfig = {
640+
641+ // Configuration values for generated watchdog timer event.
642+ .ui32Config = AM_HAL_WDT_LFRC_CLK_16HZ | AM_HAL_WDT_ENABLE_RESET | AM_HAL_WDT_ENABLE_INTERRUPT,
643+
644+ // Number of watchdog timer ticks allowed before a watchdog interrupt event is generated.
645+ .ui16InterruptCount = 16 , // Set WDT interrupt timeout for 1 second.
646+
647+ // Number of watchdog timer ticks allowed before the watchdog will issue a system reset.
648+ .ui16ResetCount = 20 // Set WDT reset timeout for 1.25 seconds.
649+ };
650+
651+ void startWatchdog ()
652+ {
653+ // LFRC must be turned on for this example as the watchdog only runs off of the LFRC.
654+ am_hal_clkgen_control (AM_HAL_CLKGEN_CONTROL_LFRC_START, 0 );
655+
656+ // Configure the watchdog.
657+ am_hal_wdt_init (&g_sWatchdogConfig);
658+
659+ // Enable the interrupt for the watchdog in the NVIC.
660+ NVIC_EnableIRQ (WDT_IRQn);
661+ // NVIC_SetPriority(WDT_IRQn, 0); // Set the interrupt priority to 0 = highest (255 = lowest)
662+ // am_hal_interrupt_master_enable(); // ap3_initialization.cpp does this - no need to do it here
663+
664+ // Enable the watchdog.
665+ am_hal_wdt_start ();
666+ }
667+
668+ // Interrupt handler for the watchdog.
669+ extern " C++" void am_watchdog_isr (void )
670+ {
671+ // Clear the watchdog interrupt.
672+ am_hal_wdt_int_clear ();
673+
674+ // DON'T Restart the watchdog.
675+ // am_hal_wdt_restart(); // "Pet" the dog.
676+ }
0 commit comments