Autres articles / Other articles

Watchdog management on ARDUINO with FlashForth

published: 3 November 2020 / updated 3 November 2020

Lire cette page en français

 

What is the watchdog?

A watchdog is an electronic circuit or software used in digital electronics to ensure that a PLC or computer does not does not get stuck at a particular stage of the processing it performs. It's a protection generally intended to restart the system, if a defined action is not not executed within a specified time.

You decide to put NASA on the pole by sending an automated laboratory to Mars. You sources have found a very promising landing site. Your probe will travel for 10 months, enter the Martian atmosphere, deploy a parachute, then enter head first in a mud lake, sink into this mud, deploy your antenna to transmit the results analysis ... and ... Damn! Plant!

Oh yes! You were two minutes away from delivering the most shattering revelation in the history of the conquest of space: "there is life on Mars!".

But a stupid bug in the deployment of the antenna remains frozen on this damn deployment, because the sensor of limit switch does not confirm full deployment. In fact, this sensor was damaged during the phase of penetration into Martian mud.

There are several alternatives available to you:

Obviously, we do not envision the outright abandonment of the exploration mission.

And this is where the ARDUINO-FORTH.COM site explains to you in a very well documented article, entitled: "Watchdog management on ARDUINO with FlashForth" that there was a way to avoid these inconveniences by using the watchdog function of the processor.

Because, with the watchdog, the ARDUINO card would have carried out this RESET without calling the financial assistance from Jeff BEZOS or by sacrificing a trained dog.

Here is a video that explains very well how to use the watchdog on a card ARDUINO with FlashForth:

The watchdog in detail

On ARDUINO cards, the watchdog or Watchdog is a counter which allows of the blocking of the microcontroller. This blocking can be of a software nature (back impossible, put in infinite loop or quite simply error of structuring), or material (interference, voltage drop). In all, blocking the program can have very annoying consequences.

The watchdog is operated through the WDTCSR registry

\ WATCHDOG 
96 constant WDTCSR	\ Watchdog Timer Control Register 
bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
WDIF WDIE WDP3 WDCE WDE WDP2 WDP1 WDP0

WDIF

Sets an interrupt flag but you wont need to worry about this. It is automatically flagged high and low by the system.

WDIE

Enables Interrupts. This will give you the chance to include one last dying wish (or a few lines of code...) before the board is reset. This is a great way of performing interrupts on a regular interval should the watchdog be configured to not reset on time-out.

WDCE

This is a safety to enable a configuration mode that will last 4 clock cycles. Set this bit and WDE high before attempting any changes to the watchdog register. There isnʼt really any logic behind this, you just have to set WDCE and WDE to ʻ1ʼ to enter a sort of ʻsetup modeʼ in the watchdog timer.

WDE

Enables system reset on time-out. Whenever the Watchdog timer times out the micocontroller will be reset. This is probably what you were all looking for. Set this to ʻ1ʼ to activate.

WDP0/WDP1/WDP2/WDP3

These four bits determine how long the timer will count for before resetting. The exact time is set by setting combinations of the 4 bits in such a pattern.

Normalement, vous n'avez pas besoin d'avoir accès à ce registre si vous souhaitez simplement limiter la temporisation d'activation du chien de garde à deux secondes ou moins. Voici les valeurs possibles pour cette temporisation:

WDP3 WDP2 WDP1 WDP0 time out
(ms)
0000 16
0001 32
0010 64
0011 125
0100 250
0101 500
0110 1000
0111 2000
1000 4000
1001 8000

Software implementation of the watchdog

The watchdog is activated from the word wd+ preceded by a value includes in the interval 0..7 (see table above).

The watchdog

is deactivated using the word wd-.

The word cwd (cwd for Clear Watch Dog), resets the counter of the watchdog. Without this periodic reset, the watchdog will trigger the restart FlashForth.

Here is a very simple example. We are going to create an infinite loop which we can only be exited by pressing the RESET button on the ARDUINO card:

: infinite ( ---) 
    begin again ; 

In particular, do not launch infinite before having activated the watchdog. Here, we will set it to a two-second delay:

cwd 7 wd+ infinite 
\ normally, FlashForth will restar after 2 seconds 

Running infinite brings us into an infinite loop. The watchdog, after two seconds, reset FlashForth and restore control at the terminal!

Here is a more practical example. The word tempKey waits for support on a keyboard key. If no key is pressed, the watchdog resets FlashForth:

: tempKey ( ---) 
    7 wd+ 
    begin  
        key? dup 
        if  
            drop 
            key emit 
            wd- 
            exit 
        then 
    until 
 ; 

Execution off tempKey:

\ press 'h' key on keyboard before 2 secnds delay
tempKey h ok<#,ram>
 
\ press no key, restart after 2 seconds
tempKey W FlashForth 5 ATmega328 13.01.2019

Watchdog et turnkey

The word turnkey is used to indicate which word FORTH should run when FlashForth starts:

: test ."  -> restart" cr ;  
' test is turnkey 

Running test displays "-> restart".

Pressing the RESET button on the ARDUINO card displays:

E FlashForth 5 ATmega328 13.01.2019
ESC -> restart

So our word test works fine when FlashForth is restarted.

If we now test our word tempKey without pressing a key we will have on display:

tempKey W FlashForth 5 ATmega328 13.01.2019
ESC -> restart

Here! We saved our Martian mission!

Increase the watchdog trigger time

The word wd+ accepts as a parameter only the values included in the interval 0..7. Any other value will be treated modulo 7. So how to increase this trigger delay?

We can increase our timing to four and eight seconds as follows:

\ WATCHDOG 
96 constant WDTCSR	\ Watchdog Timer Control Register 
: 4secWd+ ( ---)        \ watchdog time out 4 seconds 
    %00100000 WDTCSR mset 
    0 wd+ ; 
: 8secWd+ ( ---)        \ watchdog time out 8 seconds 
    %00100000 WDTCSR mset 
    1 wd+ ; 

It is not possible to set a longer activation time.

Conclusion

Without having the ambition of a mission on Mars, there are many situations where the watchdog is required. In communication protocols, we often expect a response from a device. This response is often expected within a reasonable time.

The watchdog is a solution. It is certainly not the best solution. But this solution allows you to get out of a real crash, whether software or hardware.