|
4 | 4 | This sketch shows how to enable the watchdog and
|
5 | 5 | refresh the timer to avoid resets
|
6 | 6 |
|
| 7 | + Watchdog intervals are limited to 7 timeout periods |
| 8 | + the library will select the best clock divisor and timeout |
| 9 | + according to the selected wdtInterval. |
| 10 | +
|
| 11 | + UNO R4 min wdtInterval 1ms / max wdtInterval 5592ms |
| 12 | + Comment out Serial.print() in the setup to make it work with |
| 13 | + small intervals |
| 14 | +
|
| 15 | + Portenta C33 min wdtInterval 1ms / max wdtInterval 2684ms |
| 16 | +
|
7 | 17 | Circuit:
|
8 | 18 | - Portenta C33
|
| 19 | + - UNO R4 |
9 | 20 | */
|
10 | 21 |
|
11 | 22 | #include <WDT.h>
|
12 | 23 |
|
| 24 | +const long ledInterval = 1000; |
| 25 | +unsigned long ledMillis = 0; |
| 26 | +bool ledState = true; |
| 27 | +const long wdtInterval = 2684; |
| 28 | +unsigned long wdtMillis = 0; |
| 29 | + |
13 | 30 | void setup() {
|
14 | 31 | Serial.begin(9600);
|
15 | 32 | while (!Serial);
|
16 | 33 |
|
17 |
| - wdt_cfg_t p_cfg; |
| 34 | + pinMode(LED_BUILTIN, OUTPUT); |
18 | 35 |
|
19 |
| - p_cfg.timeout = WDT_TIMEOUT_16384; |
20 |
| - p_cfg.clock_division = WDT_CLOCK_DIVISION_8192; |
21 |
| - p_cfg.window_start = WDT_WINDOW_START_100; |
22 |
| - p_cfg.window_end = WDT_WINDOW_END_0; |
23 |
| - p_cfg.reset_control = WDT_RESET_CONTROL_RESET; |
24 |
| - p_cfg.stop_control = WDT_STOP_CONTROL_ENABLE; |
| 36 | + if(wdtInterval < 1) { |
| 37 | + Serial.println("Invalid watchdog interval"); |
| 38 | + while(1){} |
| 39 | + } |
25 | 40 |
|
26 |
| - WDT.begin(p_cfg); |
| 41 | + if(WDT.begin(wdtInterval)) { |
| 42 | + Serial.print("WDT interval: "); |
| 43 | + WDT.refresh(); |
| 44 | + Serial.print(WDT.getTimeout()); |
| 45 | + WDT.refresh(); |
| 46 | + Serial.println(" ms"); |
| 47 | + WDT.refresh(); |
| 48 | + } else { |
| 49 | + Serial.println("Error initializing watchdog"); |
| 50 | + while(1){} |
| 51 | + } |
27 | 52 | }
|
28 | 53 |
|
29 | 54 | void loop() {
|
30 |
| - Serial.println("Still Alive..."); |
31 |
| - // Comment the line above to stop refreshing the watchdog |
32 |
| - WDT.refresh(); |
33 |
| - delay(1000); |
| 55 | + if(millis() - ledMillis >= ledInterval) { |
| 56 | + digitalWrite(LED_BUILTIN, ledState); |
| 57 | + ledState = !ledState; |
| 58 | + ledMillis = millis(); |
| 59 | + } |
| 60 | + |
| 61 | + if(millis() - wdtMillis >= wdtInterval - 1) { |
| 62 | + WDT.refresh(); // Comment this line to stop refreshing the watchdog |
| 63 | + wdtMillis = millis(); |
| 64 | + } |
| 65 | + |
34 | 66 | }
|
0 commit comments