Skip to content

Commit

Permalink
Version 1.1.0
Browse files Browse the repository at this point in the history
Added wdt_reEnable() method to re-enable the wdt after a disable.
  • Loading branch information
gpb01 committed Jul 23, 2022
1 parent 0c2af24 commit ea1657e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 20 deletions.
37 changes: 28 additions & 9 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
# wdt_samd21 Library
© 2022 Guglielmo Braguglia
© 2022 Guglielmo Braguglia, updated on Jul 2022.

---



A very simple library to activate, reset and deactivate the WDT on ATSAMD21.

Based on the work of MartinL ([https://forum.arduino.cc/u/MartinL]()) on Arduino forum (Apr, 2018)

&nbsp;&nbsp;&nbsp;• © 2022 Guglielmo Braguglia<br>
&nbsp;&nbsp;&nbsp;• © 2018 MartinL

The wdt_samd21 library allows, in a very easy way, on ATSAMD21 MCU, to **activate**, to periodically **reset** and to **deactivate** the WDT (*Watch Dog Timer*), which are the normal required functions for a simple use of the WDT for checking the correct execution of an application program.
The wdt_samd21 library allows, in a very easy way, on ATSAMD21 MCU, to **activate**, to periodically **reset**, to **deactivate** and to **reactivate** the WDT (*Watch Dog Timer*), which are the normal required functions for a simple use of the WDT for checking the correct execution of an application program.

### Library usage and initialization

Expand Down Expand Up @@ -78,18 +76,30 @@ wdt_reset ( );

##### wdt_disable( )

Disable the WDT until it is reactivated again with a wdt_init( ).
Disable the WDT until it is reactivated again with a wdt_reEnable( ).

Example:

```
wdt_disable ( );
```

---

##### wdt_reEnable( )

Re-enable the WDT disabled by a previous wdt_disable( ).

Example:

```
wdt_reEnable ( );
```

---
### Demo Program

The following example initializes the WDT for a timeout of 2 seconds, after which, in the loop(), it performs a for structure with a delay() of one second at each iteration, but sending a wdt_reset() command to the WDT to avoid the restart. At the end of the for structure, a 4 second delay() is performed which causes the MCU to restart so, all that following the delay(), is never executed.
The following example initializes the WDT for a timeout of 2 seconds, after which, in the loop(), it performs a for structure with a delay() of one second at each iteration, but sending a wdt_reset() command to the WDT to avoid the restart. At the end of the for structure, the wdt is first disabled then, after a 3 second delay(), is enabled again and, finally, a 4 second delay() is performed which causes the MCU to restart so, all that following the last delay(), is never executed.

```
#include <wdt_samd21.h>
Expand Down Expand Up @@ -118,14 +128,23 @@ void loop() {
wdt_reset();
}
//
// now wait 4 seconds ... the WDT should restart the board
// now disable wdt and wait ...
wdt_disable();
Serial.println( "wdt disabled ..." );
Serial.println ( "Now waiting for 3 seconds ..." );
delay(3000);
//
// ... then reEnable the wdt ...
wdt_reEnable();
Serial.println( "wdt reEnabled ..." );
//
// ... and wait 4 seconds ... the WDT should restart the board
Serial.println ( "Now waiting for 4 seconds ..." );
delay ( 4000 );
//
Serial.println ( "You will never see this message printed" );
Serial.println ( "*** You will never see this message printed ***" );
delay ( 1000 );
}
```


---
Binary file modified ReadMe.pdf
Binary file not shown.
28 changes: 19 additions & 9 deletions examples/wdt_samd21_demo/wdt_samd21_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
void setup() {
delay ( 500 );
//
SerialUSB.begin ( 9600 );
while ( !SerialUSB ) {
Serial.begin ( 9600 );
while ( !Serial ) {
delay ( 100 );
}
//
Expand All @@ -16,18 +16,28 @@ void loop() {
for ( byte i = 0; i < 5; i++ ) {
// wait a second
delay ( 1000 );
// write on the native serial USB port
SerialUSB.print ( "Iteration " );
SerialUSB.print ( i + 1 );
SerialUSB.println ( " of 5" );
// write on the serial port
Serial.print ( "Iteration " );
Serial.print ( i + 1 );
Serial.println ( " of 5" );
// "feed" the WDT to avoid restart
wdt_reset();
}
//
// now wait 4 seconds ... the WDT should restart the board
SerialUSB.println ( "Now waiting for 4 seconds ..." );
// now disable wdt and wait ...
wdt_disable();
Serial.println( "wdt disabled ..." );
Serial.println ( "Now waiting for 3 seconds ..." );
delay(3000);
//
// ... then reEnable the wdt ...
wdt_reEnable();
Serial.println( "wdt reEnabled ..." );
//
// ... and wait 4 seconds ... the WDT should restart the board
Serial.println ( "Now waiting for 4 seconds ..." );
delay ( 4000 );
//
SerialUSB.println ( "You will never see this message printed" );
Serial.println ( "*** You will never see this message printed ***" );
delay ( 1000 );
}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ wdt_samd21 KEYWORD1
wdt_init KEYWORD2
wdt_reset KEYWORD2
wdt_disable KEYWORD2
wdt_reEnable KEYWORD2

WDT_CONFIG_PER_8 LITERAL1
WDT_CONFIG_PER_16 LITERAL1
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=wdt_samd21
version=1.0.0
version=1.1.0
author=Guglielmo Braguglia <[email protected]>
maintainer=Guglielmo Braguglia <[email protected]>
sentence=A very simple library for the management of the WDT on ATSAMD21.
paragraph=A simple library to activate, reset and deactivate the WDT on ATSAMD21. From the original work of MartinL (arduino forum: https://forum.arduino.cc/u/MartinL).
paragraph=A simple library to activate, reset, deactivate and reactivate the WDT on ATSAMD21. From the original work of MartinL (arduino forum: https://forum.arduino.cc/u/MartinL).
category=Device Control
url=https://github.com/gpb01/wdt_samd21
architectures=samd
10 changes: 10 additions & 0 deletions src/wdt_samd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ void wdt_disable ( void ) {
REG_WDT_CTRL = WDT_CTRL_RESETVALUE; // Disable the WDT in normal mode
while ( WDT->STATUS.bit.SYNCBUSY ); // Wait for synchronization
}

// ------------------ wdt_reEnable ----------------------

void wdt_reEnable ( void ) {
//
// Enable the WDT
while ( WDT->STATUS.bit.SYNCBUSY ); // Check if the WDT registers are synchronized
REG_WDT_CTRL = WDT_CTRL_ENABLE; // Enable the WDT in normal mode
while ( WDT->STATUS.bit.SYNCBUSY ); // Wait for synchronization
}
6 changes: 6 additions & 0 deletions src/wdt_samd21.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@

#include <Arduino.h>

#if !defined (__SAMD21G18A__)
#error "This library is only for SAMD21G18A, compilation aborted."
#endif

void wdt_init ( unsigned long wdt_config_per = WDT_CONFIG_PER_2K );

void wdt_reset ( void );

void wdt_disable ( void );

void wdt_reEnable ( void );

#endif

0 comments on commit ea1657e

Please sign in to comment.