Skip to content

Add WDT Library #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2ca5c57
Create keywords.txt
nseidle Jun 26, 2020
9072743
Correct typo in HAL
nseidle Jul 10, 2020
ddda09e
Add checking of RTS/CTS pins to enable flow control. Fix typo in _beg…
nseidle Jul 10, 2020
16894c2
Merge pull request #230 from sparkfun/addFlowControl
Jul 14, 2020
33bd2d9
Merge pull request #191 from sparkfun/addKeywords
Jul 14, 2020
1287ee0
Merge pull request #232 from sparkfun/release-candidate
Jul 14, 2020
2cc5ab4
Merge pull request #234 from sparkfun/release-candidate
Jul 16, 2020
0af5eb0
Merge pull request #1 from sparkfun/master
stephenf7072 Jul 23, 2020
ece522d
Removed code using <time.h> non-core library (ie. Epoch stuff)
stephenf7072 Jul 23, 2020
847eb3d
Create Example8_ResetsAndWatchdog.ino
stephenf7072 Jul 24, 2020
38d783d
Create WDT.h
adamgarbo Jul 27, 2020
d913933
Create WDT.cpp
adamgarbo Jul 27, 2020
f8d1fb9
Create library.properties
adamgarbo Jul 27, 2020
fd8e357
Create keywords.txt
adamgarbo Jul 27, 2020
57c81bd
Revert "Removed code using <time.h> non-core library (ie. Epoch stuff)"
stephenf7072 Jul 30, 2020
66b6e6e
Update WDT.h
adamgarbo Jul 30, 2020
331b8d7
Update WDT.cpp
adamgarbo Jul 30, 2020
453196d
rename example
oclyke Jul 30, 2020
03ae640
rename the actual file
oclyke Jul 30, 2020
d9b3290
Merge pull request #241 from stephenf7072/master
Jul 30, 2020
e1a7d9b
Add examples
adamgarbo Jul 31, 2020
73a813b
Add WDT configuration
adamgarbo Aug 1, 2020
6bda297
Merge remote-tracking branch 'upstream/master' into wdt
adamgarbo Aug 4, 2020
38a241b
Minor updates
adamgarbo Aug 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cores/arduino/am_sdk_ap3/mcu/apollo3/hal/am_hal_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ am_hal_uart_transfer_t;

// Flow control
#define AM_HAL_UART_FLOW_CTRL_NONE 0
#define AM_HAL_UART_FLOW_CTRL_CTS_ONLY UART_CR_CTSEN_Msk
#define AM_HAL_UART_FLOW_CTRL_RTS_ONLY UART_CR_RTSEN_Msk
#define AM_HAL_UART_FLOW_CTRL_CTS_ONLY UART0_CR_CTSEN_Msk
#define AM_HAL_UART_FLOW_CTRL_RTS_ONLY UART0_CR_RTSEN_Msk
#define AM_HAL_UART_FLOW_CTRL_RTS_CTS (UART0_CR_CTSEN_Msk | \
UART0_CR_RTSEN_Msk)
// FIFO enable/disable.
Expand Down
20 changes: 18 additions & 2 deletions cores/arduino/ard_sup/uart/ap3_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,22 @@ ap3_err_t Uart::set_config(HardwareSerial_Config_e HWSconfig)
retval = AP3_INVALID_ARG;
break;
}

//Setup flow control
_config.ui32FlowControl = AM_HAL_UART_FLOW_CTRL_NONE;
if(_pinRTS != AP3_UART_PIN_UNUSED && _pinCTS != AP3_UART_PIN_UNUSED)
{
_config.ui32FlowControl = AM_HAL_UART_FLOW_CTRL_RTS_CTS;
}
else if(_pinRTS != AP3_UART_PIN_UNUSED)
{
_config.ui32FlowControl = AM_HAL_UART_FLOW_CTRL_RTS_ONLY;
}
else if(_pinCTS != AP3_UART_PIN_UNUSED)
{
_config.ui32FlowControl = AM_HAL_UART_FLOW_CTRL_CTS_ONLY;
}

return retval;
}

Expand Down Expand Up @@ -375,7 +391,7 @@ ap3_err_t Uart::_begin(void)

if (_pinRTS != AP3_UART_PIN_UNUSED)
{
retval = ap3_uart_pad_funcsel(_instance, AP3_UART_TX, ap3_gpio_pin2pad(_pinRTS), &funcsel);
retval = ap3_uart_pad_funcsel(_instance, AP3_UART_RTS, ap3_gpio_pin2pad(_pinRTS), &funcsel);
if (retval != AP3_OK)
{
return retval;
Expand All @@ -391,7 +407,7 @@ ap3_err_t Uart::_begin(void)

if (_pinCTS != AP3_UART_PIN_UNUSED)
{
retval = ap3_uart_pad_funcsel(_instance, AP3_UART_RX, ap3_gpio_pin2pad(_pinCTS), &funcsel);
retval = ap3_uart_pad_funcsel(_instance, AP3_UART_CTS, ap3_gpio_pin2pad(_pinCTS), &funcsel);
if (retval != AP3_OK)
{
return retval;
Expand Down
39 changes: 39 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#######################################
# Syntax Coloring Map For Wire
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

#######################################
# Methods and Functions (KEYWORD2)
#######################################

enableBurstMode KEYWORD2
disableBurstMode KEYWORD2
getCpuFreqMHz KEYWORD2

getInternalTemp KEYWORD2

analogWriteResolution KEYWORD2
analogWriteFrameWidth KEYWORD2
analogWriteFrequency KEYWORD2
servoWrite KEYWORD2

enableFastShift KEYWORD2
fastShiftOut KEYWORD2
fastShiftIn KEYWORD2

secs KEYWORD2
systicks KEYWORD2
sysoverflows KEYWORD2

#######################################
# Instances (KEYWORD2)
#######################################

#######################################
# Constants (LITERAL1)
#######################################

Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/* 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)
Created: 24th July 2020
License: MIT. See SparkFun Arduino Apollo3 Project for more information

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
*/

// Global variables
volatile uint8_t watchdogCounter = 0; // Watchdog interrupt counter
uint32_t resetStatus = 0; // Reset status register
bool testWatchdogResetFlag = false;
bool watchdogInterruptCallsEmergencyReset = false;
bool resetWDTEveryLoop = true;

// Watchdog timer configuration structure.
am_hal_wdt_config_t g_sWatchdogConfig = {
//Substitude other values for AM_HAL_WDT_LFRC_CLK_16HZ to increase/decrease the range
.ui32Config = AM_HAL_WDT_LFRC_CLK_16HZ | AM_HAL_WDT_ENABLE_RESET | AM_HAL_WDT_ENABLE_INTERRUPT, // Configuration values for generated watchdog timer event.
//****** EVEN THOUGH THESE IMPLY 16-BIT, THEY ARE ONLY 8-BIT - 255 MAX!
.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.
.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.
};

void setup(void)
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200); delay(10);
Serial.println();Serial.println();
Serial.println("****Artemis Reset & Watchdog Reset Example****");

printResetReason(); //Explain reason for reset
am_hal_reset_control(AM_HAL_RESET_CONTROL_STATUSCLEAR, 0); // Clear reset status register for next time we reset.
delay(1000);

startWatchdogTimer(); //Initialise and start the watchdog timer, in case the program gets stuck in a while loop or waiting for user input
printWatchdogDetails();
delay(2000);

Serial.println("\nRunning a time-consuming loop with watchdog running");
for(int i=0; i<5; i++)
{
delay(1000); //Phew, a bunch of hard processing
printWatchdogValue();
}
Serial.println("Yikes, the time kept increasing, even though we were doing legitimate work\n");

Serial.println("\nRunning a time-consuming loop that restarts the watchdog timer each loop");
for(int i=0; i<5; i++)
{
delay(1000); //Phew, a bunch of hard processing
printWatchdogValue();
am_hal_wdt_restart(); // "Pet" the dog. // Restart the watchdog.
}
Serial.println("Plenty of time, but the watchdog timer will still catch a freeze\n");
delay(2000);
}

void loop(void)
{
Serial.println("Start of loop()");
if(resetWDTEveryLoop)
am_hal_wdt_restart(); // "Pet" the dog. // Restart the watchdog. (every loop run seems good)

if(resetWDTEveryLoop)
{
Serial.println("Enter 'r' to do a scheduled software reset");
Serial.println("Enter 's' to stop resetting the watchdog timer each loop");
delay(1000);
char option = Serial.read();
if(option == 'r')
myScheduledReset();
if(option == 's')
{
Serial.println("I take no responsibility for the consequences!");
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)");
delay(2000);
resetWDTEveryLoop = false;
}
}

if(resetWDTEveryLoop == false)
{
Serial.println("Enter 'e' to let the watchdog interrupt call emergencyReset()");
delay(1000);
char option = Serial.read();
if(option == 'e')
{
Serial.println("Cool, that'll happen in a sec"); delay(20);
watchdogInterruptCallsEmergencyReset = true;
}
}

if(testWatchdogResetFlag)
{
Serial.println("Just about to go down the rabbit hole...");
delay(20); //Let serial buffer clear
while(1); //Intentionally get stuck, and hope that the watchdog timer saves the day
}

printWatchdogValue();
Serial.println();
delay(10);

//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
//am_hal_wdt_halt();
//sleepForABit();
//startWatchdog();
}

// Interrupt handler for the watchdog.
extern "C" void am_watchdog_isr(void)
{
am_hal_wdt_int_clear(); // Clear the watchdog interrupt.
if(watchdogInterruptCallsEmergencyReset)
emergencyReset();

if ( watchdogCounter < 3 ) // Catch the first three watchdog interrupts, but let the fourth through untouched.
{
digitalWrite(LED_BUILTIN, LOW);
am_hal_wdt_restart(); // "Pet" the dog (reset the timer)
}
else
{
digitalWrite(LED_BUILTIN, HIGH); // Indicator that a reset will occur.
testWatchdogResetFlag = true;
}

watchdogCounter++; // Increment the number of watchdog interrupts.
}

void startWatchdogTimer()
{
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.
am_hal_wdt_init(&g_sWatchdogConfig); // Configure the watchdog.
NVIC_EnableIRQ(WDT_IRQn); // Enable the interrupt for the watchdog in the NVIC.
am_hal_interrupt_master_enable();
am_hal_wdt_start(); // Enable the watchdog.
}

//IMPORTANT: IF THIS IS CALLED BY THE WATCHDOG TIMER INTERRUPT ROUTINE, IT NEEDS TO BE QUICK (NO PRINTING TO SERIAL OR DELAYS)
void emergencyReset()
{
//Optional: write some bits to flash/EEPROM to help with recovery

am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOI,0); //Reset with option "Software Power On Initialization" SWPOI
// am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOR,0); //Reset with option SWPOR (same as SWPOI but different am_hal_reset_status bit)
}

void myScheduledReset()
{
//Optional: write some bits to flash/EEPROM to help with recovery

// am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOI,0); //Reset with option "Software Power On Initialization" SWPOI
am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOR,0); //Reset with option SWPOR (same as SWPOI but different am_hal_reset_status bit)
}

void printWatchdogDetails()
{
Serial.print("Interrupt Count = "); Serial.print(g_sWatchdogConfig.ui16InterruptCount ); Serial.println(" ticks");
Serial.print("Reset Count = "); Serial.print(g_sWatchdogConfig.ui16ResetCount); Serial.println(" ticks");

// Print out reset status register.
am_hal_reset_status_t sStatus; // (Note: See am_hal_reset.h for RESET status structure)
am_hal_reset_status_get(&sStatus);
resetStatus = sStatus.eStatus;
char rStatus[30];
sprintf(rStatus, "Reset Status Register = 0x%x", resetStatus); // (Note: Watch Dog Timer reset = 0x40)
Serial.println(rStatus);
}

void printWatchdogValue()
{
Serial.print("Watchdog timer:");
Serial.println(am_hal_wdt_counter_get());
}

void printResetReason()
{
am_hal_reset_status_t resetResult;
uint32_t resultOk = am_hal_reset_status_get(&resetResult);
Serial.print("Reset reason: ");
if(resultOk == AM_HAL_STATUS_FAIL) Serial.println("Failed to get reset status");
if(resetResult.bEXTStat) Serial.println("External reset");
if(resetResult.bPORStat) Serial.println("Power-On reset");
if(resetResult.bBODStat) Serial.println("Brown-Out reset");
if(resetResult.bSWPORStat) Serial.println("SW Power-On reset or AIRCR reset - in this example, indicates reset by myScheduledReset()");
if(resetResult.bSWPOIStat) Serial.println("SW Power On Initialization reset - in this example, indicates reset by emergencyReset()");
if(resetResult.bDBGRStat) Serial.println("Debugger reset");
if(resetResult.bWDTStat) Serial.println("Watch Dog Timer reset - nothing in the Watchdog Timer Interrupt routine got to restarting the Watchdog Timer");
if(resetResult.bBOUnregStat) Serial.println("Unregulated Supply Brownout event");
if(resetResult.bBOCOREStat) Serial.println("Core Regulator Brownout event");
if(resetResult.bBOMEMStat) Serial.println("Memory Regulator Brownout event");
if(resetResult.bBOBLEStat) Serial.println("BLE/Burst Regulator Brownout event");
// am_hal_reset_control(AM_HAL_RESET_CONTROL_STATUSCLEAR, 0); // (do this in setup() Clear reset status register for next time we reset.
Serial.println();
}
81 changes: 81 additions & 0 deletions libraries/WDT/examples/Example1_WDT_Basic/Example1_WDT_Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Author: Adam Garbo
Created: August 1st, 2020
License: MIT. See SparkFun Arduino Apollo3 Project for more information

This example demonstrates a simple configuration of the Artemis Watchdog Timer.

The code will configure the watchdog for both interrupt and reset generation,
and immediately start the watchdog timer.

The watchdog ISR provided will 'pet' the watchdog four times. On the fifth
interrupt, the watchdog will not be pet, so the 'reset' action can occur.
On the sixth timeout event, the WDT will issue a system reset, and the
program will start over from the beginning.

The watchdogCounter will show the number of ticks before the watchdog
reset occurs.

This example is based on the Ambiq SDK 2.4.2 watchdog.c example.

Tested with SparkFun Artemis Redboard.
*/

#include "WDT.h"

APM3_WDT wdt;

volatile bool watchdogFlag = false; // Watchdog Timer ISR flag
volatile int watchdogInterrupt = 0; // Watchdog interrupt counter
unsigned long currentMillis = 0,
previousMillis = 0;

void setup() {

Serial.begin(115200);

Serial.println("Artemis Watchdog Timer Example");

// Start the watchdog
wdt.start();
}

void loop()
{
// Check for watchdog interrupts
if (watchdogFlag)
{
// Calculate duration between watchdog interrupts
currentMillis = millis() - previousMillis;
previousMillis = millis();

Serial.print("Interrupt: "); Serial.print(watchdogInterrupt);
Serial.print(" Period: "); Serial.print(currentMillis); Serial.println(" ms ");

if (watchdogInterrupt == 5)
{
Serial.println("Warning: Watchdog has triggered a system reset");
}
}
watchdogFlag = false; // Clear watchdog flag
delay(1);
}

// Interrupt handler for the watchdog
extern "C" void am_watchdog_isr(void)
{
// Clear the watchdog interrupt
wdt.clear();

// Catch the first four watchdog interrupts, but let the fifth through untouched
if ( watchdogInterrupt < 4 )
{
wdt.restart(); // "Pet" the dog
}
else {
digitalWrite(LED_BUILTIN, HIGH); // Visual indication of system reset trigger
}

watchdogFlag = true; // Set watchdog flag
watchdogInterrupt++; // Increment watchdog interrupt counter
}
Loading