Skip to content

Commit 6bda297

Browse files
committed
Merge remote-tracking branch 'upstream/master' into wdt
2 parents 73a813b + d9b3290 commit 6bda297

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/* Author: Stephen Fordyce (adapted from WDT example code by Adam Garbo - https://forum.sparkfun.com/viewtopic.php?f=169&t=52431&p=213296&hilit=watchdog#p213296)
2+
Created: 24th July 2020
3+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
4+
5+
This example demonstrates use of the watchdog timer, also different ways of resetting the Artemis as well as how to determine the last reset type
6+
*/
7+
8+
// Global variables
9+
volatile uint8_t watchdogCounter = 0; // Watchdog interrupt counter
10+
uint32_t resetStatus = 0; // Reset status register
11+
bool testWatchdogResetFlag = false;
12+
bool watchdogInterruptCallsEmergencyReset = false;
13+
bool resetWDTEveryLoop = true;
14+
15+
// Watchdog timer configuration structure.
16+
am_hal_wdt_config_t g_sWatchdogConfig = {
17+
//Substitude other values for AM_HAL_WDT_LFRC_CLK_16HZ to increase/decrease the range
18+
.ui32Config = AM_HAL_WDT_LFRC_CLK_16HZ | AM_HAL_WDT_ENABLE_RESET | AM_HAL_WDT_ENABLE_INTERRUPT, // Configuration values for generated watchdog timer event.
19+
//****** EVEN THOUGH THESE IMPLY 16-BIT, THEY ARE ONLY 8-BIT - 255 MAX!
20+
.ui16InterruptCount = 120, //MAX 255! // Set WDT interrupt timeout for 5 seconds (120 / 16 = 8). // Number of watchdog timer ticks allowed before a watchdog interrupt event is generated.
21+
.ui16ResetCount = 160 //MAX 255! // Set WDT reset timeout for 15 seconds (160 / 16 = 10). // Number of watchdog timer ticks allowed before the watchdog will issue a system reset.
22+
};
23+
24+
void setup(void)
25+
{
26+
pinMode(LED_BUILTIN, OUTPUT);
27+
Serial.begin(115200); delay(10);
28+
Serial.println();Serial.println();
29+
Serial.println("****Artemis Reset & Watchdog Reset Example****");
30+
31+
printResetReason(); //Explain reason for reset
32+
am_hal_reset_control(AM_HAL_RESET_CONTROL_STATUSCLEAR, 0); // Clear reset status register for next time we reset.
33+
delay(1000);
34+
35+
startWatchdogTimer(); //Initialise and start the watchdog timer, in case the program gets stuck in a while loop or waiting for user input
36+
printWatchdogDetails();
37+
delay(2000);
38+
39+
Serial.println("\nRunning a time-consuming loop with watchdog running");
40+
for(int i=0; i<5; i++)
41+
{
42+
delay(1000); //Phew, a bunch of hard processing
43+
printWatchdogValue();
44+
}
45+
Serial.println("Yikes, the time kept increasing, even though we were doing legitimate work\n");
46+
47+
Serial.println("\nRunning a time-consuming loop that restarts the watchdog timer each loop");
48+
for(int i=0; i<5; i++)
49+
{
50+
delay(1000); //Phew, a bunch of hard processing
51+
printWatchdogValue();
52+
am_hal_wdt_restart(); // "Pet" the dog. // Restart the watchdog.
53+
}
54+
Serial.println("Plenty of time, but the watchdog timer will still catch a freeze\n");
55+
delay(2000);
56+
}
57+
58+
void loop(void)
59+
{
60+
Serial.println("Start of loop()");
61+
if(resetWDTEveryLoop)
62+
am_hal_wdt_restart(); // "Pet" the dog. // Restart the watchdog. (every loop run seems good)
63+
64+
if(resetWDTEveryLoop)
65+
{
66+
Serial.println("Enter 'r' to do a scheduled software reset");
67+
Serial.println("Enter 's' to stop resetting the watchdog timer each loop");
68+
delay(1000);
69+
char option = Serial.read();
70+
if(option == 'r')
71+
myScheduledReset();
72+
if(option == 's')
73+
{
74+
Serial.println("I take no responsibility for the consequences!");
75+
Serial.println("(the watchdog interrupt routine when triggered will reset the timer 3 times, watch the timer values to see when it's reset by the ISR)");
76+
delay(2000);
77+
resetWDTEveryLoop = false;
78+
}
79+
}
80+
81+
if(resetWDTEveryLoop == false)
82+
{
83+
Serial.println("Enter 'e' to let the watchdog interrupt call emergencyReset()");
84+
delay(1000);
85+
char option = Serial.read();
86+
if(option == 'e')
87+
{
88+
Serial.println("Cool, that'll happen in a sec"); delay(20);
89+
watchdogInterruptCallsEmergencyReset = true;
90+
}
91+
}
92+
93+
if(testWatchdogResetFlag)
94+
{
95+
Serial.println("Just about to go down the rabbit hole...");
96+
delay(20); //Let serial buffer clear
97+
while(1); //Intentionally get stuck, and hope that the watchdog timer saves the day
98+
}
99+
100+
printWatchdogValue();
101+
Serial.println();
102+
delay(10);
103+
104+
//Optional code for sleeping - just halt the WDT before you disable everything, then re-initialise the WDT after waking up. Tested elsewhere, and does not have any appreciable impact on sleep currents or performance
105+
//am_hal_wdt_halt();
106+
//sleepForABit();
107+
//startWatchdog();
108+
}
109+
110+
// Interrupt handler for the watchdog.
111+
extern "C" void am_watchdog_isr(void)
112+
{
113+
am_hal_wdt_int_clear(); // Clear the watchdog interrupt.
114+
if(watchdogInterruptCallsEmergencyReset)
115+
emergencyReset();
116+
117+
if ( watchdogCounter < 3 ) // Catch the first three watchdog interrupts, but let the fourth through untouched.
118+
{
119+
digitalWrite(LED_BUILTIN, LOW);
120+
am_hal_wdt_restart(); // "Pet" the dog (reset the timer)
121+
}
122+
else
123+
{
124+
digitalWrite(LED_BUILTIN, HIGH); // Indicator that a reset will occur.
125+
testWatchdogResetFlag = true;
126+
}
127+
128+
watchdogCounter++; // Increment the number of watchdog interrupts.
129+
}
130+
131+
void startWatchdogTimer()
132+
{
133+
am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_LFRC_START, 0); // LFRC must be turned on for this example as the watchdog only runs off of the LFRC.
134+
am_hal_wdt_init(&g_sWatchdogConfig); // Configure the watchdog.
135+
NVIC_EnableIRQ(WDT_IRQn); // Enable the interrupt for the watchdog in the NVIC.
136+
am_hal_interrupt_master_enable();
137+
am_hal_wdt_start(); // Enable the watchdog.
138+
}
139+
140+
//IMPORTANT: IF THIS IS CALLED BY THE WATCHDOG TIMER INTERRUPT ROUTINE, IT NEEDS TO BE QUICK (NO PRINTING TO SERIAL OR DELAYS)
141+
void emergencyReset()
142+
{
143+
//Optional: write some bits to flash/EEPROM to help with recovery
144+
145+
am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOI,0); //Reset with option "Software Power On Initialization" SWPOI
146+
// am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOR,0); //Reset with option SWPOR (same as SWPOI but different am_hal_reset_status bit)
147+
}
148+
149+
void myScheduledReset()
150+
{
151+
//Optional: write some bits to flash/EEPROM to help with recovery
152+
153+
// am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOI,0); //Reset with option "Software Power On Initialization" SWPOI
154+
am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOR,0); //Reset with option SWPOR (same as SWPOI but different am_hal_reset_status bit)
155+
}
156+
157+
void printWatchdogDetails()
158+
{
159+
Serial.print("Interrupt Count = "); Serial.print(g_sWatchdogConfig.ui16InterruptCount ); Serial.println(" ticks");
160+
Serial.print("Reset Count = "); Serial.print(g_sWatchdogConfig.ui16ResetCount); Serial.println(" ticks");
161+
162+
// Print out reset status register.
163+
am_hal_reset_status_t sStatus; // (Note: See am_hal_reset.h for RESET status structure)
164+
am_hal_reset_status_get(&sStatus);
165+
resetStatus = sStatus.eStatus;
166+
char rStatus[30];
167+
sprintf(rStatus, "Reset Status Register = 0x%x", resetStatus); // (Note: Watch Dog Timer reset = 0x40)
168+
Serial.println(rStatus);
169+
}
170+
171+
void printWatchdogValue()
172+
{
173+
Serial.print("Watchdog timer:");
174+
Serial.println(am_hal_wdt_counter_get());
175+
}
176+
177+
void printResetReason()
178+
{
179+
am_hal_reset_status_t resetResult;
180+
uint32_t resultOk = am_hal_reset_status_get(&resetResult);
181+
Serial.print("Reset reason: ");
182+
if(resultOk == AM_HAL_STATUS_FAIL) Serial.println("Failed to get reset status");
183+
if(resetResult.bEXTStat) Serial.println("External reset");
184+
if(resetResult.bPORStat) Serial.println("Power-On reset");
185+
if(resetResult.bBODStat) Serial.println("Brown-Out reset");
186+
if(resetResult.bSWPORStat) Serial.println("SW Power-On reset or AIRCR reset - in this example, indicates reset by myScheduledReset()");
187+
if(resetResult.bSWPOIStat) Serial.println("SW Power On Initialization reset - in this example, indicates reset by emergencyReset()");
188+
if(resetResult.bDBGRStat) Serial.println("Debugger reset");
189+
if(resetResult.bWDTStat) Serial.println("Watch Dog Timer reset - nothing in the Watchdog Timer Interrupt routine got to restarting the Watchdog Timer");
190+
if(resetResult.bBOUnregStat) Serial.println("Unregulated Supply Brownout event");
191+
if(resetResult.bBOCOREStat) Serial.println("Core Regulator Brownout event");
192+
if(resetResult.bBOMEMStat) Serial.println("Memory Regulator Brownout event");
193+
if(resetResult.bBOBLEStat) Serial.println("BLE/Burst Regulator Brownout event");
194+
// am_hal_reset_control(AM_HAL_RESET_CONTROL_STATUSCLEAR, 0); // (do this in setup() Clear reset status register for next time we reset.
195+
Serial.println();
196+
}

0 commit comments

Comments
 (0)