Example of watchdog causing a reset

Fork of KL25Z_WATCHDOG_USBSERIAL by Manel Marin

Committer:
WilliamMarshQMUL
Date:
Wed Mar 11 08:52:19 2020 +0000
Revision:
6:cbbe7d3ff485
Parent:
5:c9b52b30a317
Update mbed library and wait API

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Manel_Marin 0:5ce3cfc57999 1 #include "mbed.h"
WilliamMarshQMUL 1:159a09ac60ba 2 #include "wdt.h"
Manel_Marin 0:5ce3cfc57999 3
WilliamMarshQMUL 2:c31a1758ac38 4 // Sample program using the Watchdog
WilliamMarshQMUL 2:c31a1758ac38 5 // ---------------------------------
WilliamMarshQMUL 2:c31a1758ac38 6 // * Watchdog is fed for 5 sec, after that
WilliamMarshQMUL 2:c31a1758ac38 7 // * ... the system resets
WilliamMarshQMUL 2:c31a1758ac38 8
WilliamMarshQMUL 2:c31a1758ac38 9 // Note on how the WATCHDOG is enabled, overloading library function SystemInit
WilliamMarshQMUL 2:c31a1758ac38 10 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WilliamMarshQMUL 1:159a09ac60ba 11 // The KL25Z watchdog is enabled after reset with timeout=1024ms (2^10*LPO=1KHz)
WilliamMarshQMUL 1:159a09ac60ba 12 // To make the dvice easy to use on mbed, th watchdog is disabled at startup
WilliamMarshQMUL 1:159a09ac60ba 13 // (in file SystemInit.c) **BUT** SIM_COPC register can only be written once
WilliamMarshQMUL 1:159a09ac60ba 14 // after reset and so it can not be enabled later. To resolve this,
WilliamMarshQMUL 1:159a09ac60ba 15 // SystemInit() is overloaded using: void $Sub$$SystemInit (void) {
Manel_Marin 0:5ce3cfc57999 16 //
WilliamMarshQMUL 1:159a09ac60ba 17 // The original SystemInit() can be dowloaded from:
WilliamMarshQMUL 1:159a09ac60ba 18 // https://developer.mbed.org/users/mbed_official/code/mbed-src/
WilliamMarshQMUL 1:159a09ac60ba 19 // navigating to ... targets/cmsis/TARGET_Freescale/TARGET_KLXX/
WilliamMarshQMUL 1:159a09ac60ba 20 // TARGET_KL25Z/system_MKL25Z4.c
WilliamMarshQMUL 1:159a09ac60ba 21 //
WilliamMarshQMUL 1:159a09ac60ba 22 // MORE on how this overides the original initialisation:
WilliamMarshQMUL 1:159a09ac60ba 23 // https://developer.mbed.org/users/chris/notebook/Patching-functions-and-libraries/
WilliamMarshQMUL 1:159a09ac60ba 24 // and searching "ARM Compiler toolchain Using the Linker"
WilliamMarshQMUL 1:159a09ac60ba 25 // "Using $Super$$ and $Sub$$ to patch symbol definitions"
WilliamMarshQMUL 2:c31a1758ac38 26 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Manel_Marin 0:5ce3cfc57999 27
Manel_Marin 0:5ce3cfc57999 28
Manel_Marin 0:5ce3cfc57999 29 #define LIGHT 0
Manel_Marin 0:5ce3cfc57999 30 #define DARK 1
Manel_Marin 0:5ce3cfc57999 31 DigitalOut led_red(LED_RED, LIGHT);
Manel_Marin 0:5ce3cfc57999 32 DigitalOut led_blue(LED_BLUE, DARK);
Manel_Marin 0:5ce3cfc57999 33
WilliamMarshQMUL 1:159a09ac60ba 34 // Demonstate the watchdog
WilliamMarshQMUL 1:159a09ac60ba 35 // The red LED is on after reset
WilliamMarshQMUL 5:c9b52b30a317 36 // The blue LED flashes and the watch dog is fed
WilliamMarshQMUL 5:c9b52b30a317 37 // After 5sec, stop feeding the watchdog
WilliamMarshQMUL 1:159a09ac60ba 38 enum wState {Working, Stopped} ;
Manel_Marin 0:5ce3cfc57999 39
Manel_Marin 0:5ce3cfc57999 40 int main(void) {
WilliamMarshQMUL 1:159a09ac60ba 41 // initialise watchdog
WilliamMarshQMUL 1:159a09ac60ba 42 wdt_1sec() ;
WilliamMarshQMUL 1:159a09ac60ba 43
WilliamMarshQMUL 1:159a09ac60ba 44 // state of the watch dog
WilliamMarshQMUL 1:159a09ac60ba 45 wState watchDog = Working ;
WilliamMarshQMUL 1:159a09ac60ba 46 int counter = 10 ;
WilliamMarshQMUL 1:159a09ac60ba 47
WilliamMarshQMUL 1:159a09ac60ba 48 // show start-up
Manel_Marin 0:5ce3cfc57999 49 led_blue = DARK;
WilliamMarshQMUL 1:159a09ac60ba 50 led_red = LIGHT;
WilliamMarshQMUL 6:cbbe7d3ff485 51 ThisThread::sleep_for(500) ; //wait(0.5) ;
Manel_Marin 0:5ce3cfc57999 52 led_red = DARK;
WilliamMarshQMUL 1:159a09ac60ba 53
WilliamMarshQMUL 1:159a09ac60ba 54 // Note: the red LED now stays OFF until processor reset
Manel_Marin 0:5ce3cfc57999 55 while(1)
Manel_Marin 0:5ce3cfc57999 56 {
Manel_Marin 0:5ce3cfc57999 57 led_blue = LIGHT;
WilliamMarshQMUL 6:cbbe7d3ff485 58 ThisThread::sleep_for(250) ; // wait(0.25);
WilliamMarshQMUL 1:159a09ac60ba 59 if (watchDog == Working) wdt_kick_all() ;
Manel_Marin 0:5ce3cfc57999 60 led_blue = DARK;
WilliamMarshQMUL 6:cbbe7d3ff485 61 ThisThread::sleep_for(250) ; // wait(0.25);
WilliamMarshQMUL 1:159a09ac60ba 62
WilliamMarshQMUL 6:cbbe7d3ff485 63 // When the counter reachs zero, change state to stop watchdog
WilliamMarshQMUL 1:159a09ac60ba 64 if (counter > 0) counter-- ; else watchDog = Stopped ;
Manel_Marin 0:5ce3cfc57999 65 }
Manel_Marin 0:5ce3cfc57999 66 }