Example of watchdog causing a reset
Fork of KL25Z_WATCHDOG_USBSERIAL by
main.cpp@1:159a09ac60ba, 2017-02-28 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Tue Feb 28 17:39:50 2017 +0000
- Revision:
- 1:159a09ac60ba
- Parent:
- 0:5ce3cfc57999
- Child:
- 2:c31a1758ac38
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Manel_Marin | 0:5ce3cfc57999 | 1 | #include "mbed.h" |
WilliamMarshQMUL | 1:159a09ac60ba | 2 | #include "wdt.h" |
Manel_Marin | 0:5ce3cfc57999 | 3 | |
WilliamMarshQMUL | 1:159a09ac60ba | 4 | //WATCHDOG ENABLED (overloading library function SystemInit.c) |
WilliamMarshQMUL | 1:159a09ac60ba | 5 | // |
WilliamMarshQMUL | 1:159a09ac60ba | 6 | // The KL25Z watchdog is enabled after reset with timeout=1024ms (2^10*LPO=1KHz) |
WilliamMarshQMUL | 1:159a09ac60ba | 7 | // To make the dvice easy to use on mbed, th watchdog is disabled at startup |
WilliamMarshQMUL | 1:159a09ac60ba | 8 | // (in file SystemInit.c) **BUT** SIM_COPC register can only be written once |
WilliamMarshQMUL | 1:159a09ac60ba | 9 | // after reset and so it can not be enabled later. To resolve this, |
WilliamMarshQMUL | 1:159a09ac60ba | 10 | // SystemInit() is overloaded using: void $Sub$$SystemInit (void) { |
Manel_Marin | 0:5ce3cfc57999 | 11 | // |
WilliamMarshQMUL | 1:159a09ac60ba | 12 | // The original SystemInit() can be dowloaded from: |
WilliamMarshQMUL | 1:159a09ac60ba | 13 | // https://developer.mbed.org/users/mbed_official/code/mbed-src/ |
WilliamMarshQMUL | 1:159a09ac60ba | 14 | // navigating to ... targets/cmsis/TARGET_Freescale/TARGET_KLXX/ |
WilliamMarshQMUL | 1:159a09ac60ba | 15 | // TARGET_KL25Z/system_MKL25Z4.c |
WilliamMarshQMUL | 1:159a09ac60ba | 16 | // |
WilliamMarshQMUL | 1:159a09ac60ba | 17 | // MORE on how this overides the original initialisation: |
WilliamMarshQMUL | 1:159a09ac60ba | 18 | // https://developer.mbed.org/users/chris/notebook/Patching-functions-and-libraries/ |
WilliamMarshQMUL | 1:159a09ac60ba | 19 | // and searching "ARM Compiler toolchain Using the Linker" |
WilliamMarshQMUL | 1:159a09ac60ba | 20 | // "Using $Super$$ and $Sub$$ to patch symbol definitions" |
Manel_Marin | 0:5ce3cfc57999 | 21 | |
Manel_Marin | 0:5ce3cfc57999 | 22 | |
Manel_Marin | 0:5ce3cfc57999 | 23 | #define LIGHT 0 |
Manel_Marin | 0:5ce3cfc57999 | 24 | #define DARK 1 |
Manel_Marin | 0:5ce3cfc57999 | 25 | DigitalOut led_red(LED_RED, LIGHT); |
Manel_Marin | 0:5ce3cfc57999 | 26 | DigitalOut led_blue(LED_BLUE, DARK); |
Manel_Marin | 0:5ce3cfc57999 | 27 | |
WilliamMarshQMUL | 1:159a09ac60ba | 28 | // Demonstate the watchdog |
WilliamMarshQMUL | 1:159a09ac60ba | 29 | // The red LED is on after reset |
WilliamMarshQMUL | 1:159a09ac60ba | 30 | // Th blue LED flashes and the watch dog is fed |
WilliamMarshQMUL | 1:159a09ac60ba | 31 | // Aft 5sec, stop feeding the watchdog |
WilliamMarshQMUL | 1:159a09ac60ba | 32 | enum wState {Working, Stopped} ; |
Manel_Marin | 0:5ce3cfc57999 | 33 | |
Manel_Marin | 0:5ce3cfc57999 | 34 | int main(void) { |
WilliamMarshQMUL | 1:159a09ac60ba | 35 | // initialise watchdog |
WilliamMarshQMUL | 1:159a09ac60ba | 36 | wdt_1sec() ; |
WilliamMarshQMUL | 1:159a09ac60ba | 37 | |
WilliamMarshQMUL | 1:159a09ac60ba | 38 | // state of the watch dog |
WilliamMarshQMUL | 1:159a09ac60ba | 39 | wState watchDog = Working ; |
WilliamMarshQMUL | 1:159a09ac60ba | 40 | int counter = 10 ; |
WilliamMarshQMUL | 1:159a09ac60ba | 41 | |
WilliamMarshQMUL | 1:159a09ac60ba | 42 | // show start-up |
Manel_Marin | 0:5ce3cfc57999 | 43 | led_blue = DARK; |
WilliamMarshQMUL | 1:159a09ac60ba | 44 | led_red = LIGHT; |
WilliamMarshQMUL | 1:159a09ac60ba | 45 | wait(0.5) ; |
Manel_Marin | 0:5ce3cfc57999 | 46 | led_red = DARK; |
WilliamMarshQMUL | 1:159a09ac60ba | 47 | |
WilliamMarshQMUL | 1:159a09ac60ba | 48 | // Note: the red LED now stays OFF until processor reset |
Manel_Marin | 0:5ce3cfc57999 | 49 | while(1) |
Manel_Marin | 0:5ce3cfc57999 | 50 | { |
Manel_Marin | 0:5ce3cfc57999 | 51 | led_blue = LIGHT; |
WilliamMarshQMUL | 1:159a09ac60ba | 52 | wait(0.25); |
WilliamMarshQMUL | 1:159a09ac60ba | 53 | if (watchDog == Working) wdt_kick_all() ; |
Manel_Marin | 0:5ce3cfc57999 | 54 | led_blue = DARK; |
WilliamMarshQMUL | 1:159a09ac60ba | 55 | wait(0.25); |
WilliamMarshQMUL | 1:159a09ac60ba | 56 | |
WilliamMarshQMUL | 1:159a09ac60ba | 57 | // When the counter rachs zero, chang state to stop watchdog |
WilliamMarshQMUL | 1:159a09ac60ba | 58 | if (counter > 0) counter-- ; else watchDog = Stopped ; |
Manel_Marin | 0:5ce3cfc57999 | 59 | } |
Manel_Marin | 0:5ce3cfc57999 | 60 | } |