Skip to content

Commit 38a241b

Browse files
committed
Minor updates
Added simplied defintions and error checking to setClock() and configure(). More detailed comments.
1 parent 6bda297 commit 38a241b

File tree

6 files changed

+63
-23
lines changed

6 files changed

+63
-23
lines changed

Diff for: libraries/WDT/examples/Example1_WDT_Basic/Example1_WDT_Basic.ino

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Tested with SparkFun Artemis Redboard.
2222
*/
2323

24-
#include <WDT.h>
24+
#include "WDT.h"
2525

2626
APM3_WDT wdt;
2727

@@ -57,7 +57,6 @@ void loop()
5757
Serial.println("Warning: Watchdog has triggered a system reset");
5858
}
5959
}
60-
6160
watchdogFlag = false; // Clear watchdog flag
6261
delay(1);
6362
}

Diff for: libraries/WDT/examples/Example2_WDT_Config/Example2_WDT_Config.ino

+20-13
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
This example demonstrates how to modify the configuration of the Artemis
77
Watchdog Timer (WDT).
88
9-
The watchdog timer is controlled by a clock divider, interrupt ticks and
9+
The watchdog timer is controlled by a clock divider, interrupt ticks and
1010
reset ticks. To achieve desired watchdog timing, a simple calculation can be made:
1111
12-
period = # ticks / clock divider frequency
12+
period = # ticks / clock divider frequency
1313
14-
Examples:
14+
Examples:
1515
128 interrupt ticks / 128 Hz clock = 1 second
1616
64 interrupt ticks / 16 Hz clock = 4 seconds
1717
32 interrupt ticks / 1 Hz clock = 32 seconds
1818
16 interrupt ticks / 1/16 Hz clock = 256 seconds
19-
19+
2020
The following code will configure the watchdog for both interrupt and reset
2121
generation, and immediately start the watchdog timer.
2222
The watchdog ISR provided will 'pet' the watchdog four times. On the fifth
@@ -29,7 +29,7 @@
2929
Tested with SparkFun Artemis Redboard.
3030
*/
3131

32-
#include <WDT.h>
32+
#include "WDT.h"
3333

3434
APM3_WDT wdt;
3535

@@ -47,17 +47,19 @@ void setup()
4747

4848
// Configure the watchdog
4949
/*
50-
Available watchdog timer clock dividers:
51-
0 = Low Power Mode. This setting disables the watch dog timer
52-
1 = 128 Hz
53-
2 = 16 Hz
54-
3 = 1 Hz
55-
4 = 1/16th Hz
50+
Available watchdog timer clock dividers.
51+
Users can choose either the clock definition (i.e. WDT_128HZ) or Apoll3 core enumeration (i.e. 1)
52+
WDT_OFF = 0 = Low Power Mode. This setting disables the watch dog timer
53+
WDT_128HZ = 1 = 128 Hz
54+
WDT_16HZ = 2 = 16 Hz
55+
WDT_1HZ = 3 = 1 Hz
56+
WDT1_16HZ = 4 = 1/16th Hz
5657
*/
5758
// Set watchdog timer clock to 128 Hz
5859
// Set watchdog interrupt to 1 seconds (128 ticks / 128 Hz = 1 second)
5960
// Set watchdog reset ~2 seconds (255 ticks / 128 Hz = 1.99 seconds)
60-
wdt.configure(1, 128, 255); // Note: Ticks are limited to 255 (8-bit)
61+
// Note: Ticks are limited to 255 (8-bit)
62+
wdt.configure(WDT_128HZ, 128, 255); // Equivalent to: wdt.configure(1, 128, 255);
6163
wdt.start(); // Start the watchdog
6264
}
6365

@@ -74,9 +76,14 @@ void loop()
7476
Serial.print(" Period: "); Serial.print(currentMillis); Serial.println(" ms");
7577

7678
// The watchdog configurations can also be set individually
77-
wdt.setClock(2); // Set watchdog timer clock to 16 Hz
79+
wdt.setClock(WDT_16HZ); // Set watchdog timer clock to 16 Hz
7880
wdt.setInterrupt(64); // Set watchdog interrupt to 4 second (64 ticks / 16 Hz = 4 seconds)
7981
wdt.setReset(96); // Set watchdog reset to 8 seconds (96 ticks / 16 Hz = 8 seconds)
82+
83+
if (watchdogInterrupt == 9)
84+
{
85+
Serial.println("Warning: Watchdog has triggered a system reset");
86+
}
8087
}
8188
watchdogFlag = false; // Clear watchdog flag
8289
delay(1);

Diff for: libraries/WDT/examples/Example3_WDT_LowPower/Example3_WDT_LowPower.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
the watchdog interrput counter.
1818
*/
1919

20-
#include <RTC.h>
21-
#include <WDT.h>
20+
#include "RTC.h"
21+
#include "WDT.h"
2222

2323
APM3_RTC rtc;
2424
APM3_WDT wdt;
@@ -43,7 +43,7 @@ void setup()
4343

4444
// Configure the watchdog timer
4545
// See Example2_WDT_Config for more information on how to configure the watchdog
46-
wdt.configure(2, 160, 240); // 16 Hz clock, 10-second interrupt period, 15-second reset period
46+
wdt.configure(WDT_16HZ, 160, 240); // 16 Hz clock, 10-second interrupt period, 15-second reset period
4747

4848
// Start the watchdog
4949
wdt.start();

Diff for: libraries/WDT/keywords.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ configure KEYWORD2
2222
setClock KEYWORD2
2323
setInterrupt KEYWORD2
2424
setReset KEYWORD2
25-
25+
WDT_OFF KEYWORD2
26+
WDT_128HZ KEYWORD2
27+
WDT_16 KEYWORD2
28+
WDT_1HZ KEYWORD2
29+
WDT1_16HZ KEYWORD2
30+
2631
#######################################
2732
# Constants (LITERAL1)
2833
#######################################

Diff for: libraries/WDT/src/WDT.cpp

+26-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,39 @@ APM3_WDT::APM3_WDT()
99
}
1010

1111
// Configure the watchdog timer
12+
// clock: set the LFRC clock frequency of the watchdog timer (see setClock below)
13+
// interrupt: number of watchdog timer ticks allowed before a watchdog interrupt event is generated
14+
// reset: number of watchdog timer ticks allowed before the watchdog will issue a system reset
1215
void APM3_WDT::configure(uint8_t clock, uint8_t interrupt, uint8_t reset)
1316
{
14-
WDT->CFG_b.CLKSEL = clock;
17+
if(clock <= 4)
18+
{
19+
WDT->CFG_b.CLKSEL = clock;
20+
}
21+
else
22+
{
23+
WDT->CFG_b.CLKSEL = WDT_CFG_CLKSEL_16HZ; // Default to 16Hz LFRC clock divider
24+
}
1525
WDT->CFG_b.INTVAL = interrupt;
1626
WDT->CFG_b.RESVAL = reset;
1727
}
1828

1929
// Set the LFRC clock frequency of the watchdog timer
30+
// WDT_OFF WDT_CFG_CLKSEL_OFF // = 0 Low Power Mode. This setting disables the watch dog timer
31+
// WDT_128HZ WDT_CFG_CLKSEL_128HZ // = 1, 128 Hz LFRC clock
32+
// WDT_16HZ WDT_CFG_CLKSEL_16HZ // = 2 16 Hz LFRC clock
33+
// WDT_1HZ WDT_CFG_CLKSEL_1HZ // = 3, 1 Hz LFRC clock
34+
// WDT_1_16HZ WDT_CFG_CLKSEL_1_16HZ // = 4, 1/16th Hz LFRC clock
2035
void APM3_WDT::setClock(uint8_t clock)
2136
{
22-
WDT->CFG_b.CLKSEL = clock;
37+
if(clock <= 4)
38+
{
39+
WDT->CFG_b.CLKSEL = clock;
40+
}
41+
else
42+
{
43+
WDT->CFG_b.CLKSEL = WDT_CFG_CLKSEL_16HZ; // Default to 16Hz LFRC clock divider
44+
}
2345
}
2446

2547
// Set number of watchdog timer ticks allowed before a watchdog interrupt event is generated
@@ -37,8 +59,8 @@ void APM3_WDT::setReset(uint8_t reset)
3759
// Enable the watchdog
3860
void APM3_WDT::start()
3961
{
40-
// Enable the interrupt for the watchdog in the NVIC
41-
NVIC_EnableIRQ(WDT_IRQn);
62+
am_hal_interrupt_master_enable(); // Enable interrupts to the core
63+
NVIC_EnableIRQ(WDT_IRQn); // Enable the interrupt for the watchdog in the NVIC
4264
am_hal_wdt_start();
4365
}
4466

Diff for: libraries/WDT/src/WDT.h

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
#include <Arduino.h>
55

6+
// Simplified WDT Clock Divider Selections
7+
#define WDT_OFF WDT_CFG_CLKSEL_OFF // = 0 Low Power Mode. This setting disables the watch dog timer
8+
#define WDT_128HZ WDT_CFG_CLKSEL_128HZ // = 1, 128 Hz LFRC clock
9+
#define WDT_16HZ WDT_CFG_CLKSEL_16HZ // = 2 16 Hz LFRC clock
10+
#define WDT_1HZ WDT_CFG_CLKSEL_1HZ // = 3, 1 Hz LFRC clock
11+
#define WDT_1_16HZ WDT_CFG_CLKSEL_1_16HZ // = 4, 1/16th Hz LFRC clock
12+
613
class APM3_WDT
714
{
815
public:

0 commit comments

Comments
 (0)