Lab 6 Code 2 Hardware Failure Detection
Fork of Watchdog_sample_nocoverage by
Diff: main.cpp
- Revision:
- 1:159a09ac60ba
- Parent:
- 0:5ce3cfc57999
- Child:
- 2:c31a1758ac38
--- a/main.cpp Sat Nov 21 13:24:29 2015 +0000 +++ b/main.cpp Tue Feb 28 17:39:50 2017 +0000 @@ -1,52 +1,60 @@ #include "mbed.h" +#include "wdt.h" -//WATHDOG ENABLED IN OUR KL25Z_SystemInit.c (overloading library function SystemInit.c) -// It happens in this uC after reset watchdog is enabled with timeout=1024ms (2^10*LPO=1KHz) -// so mbed developers to allow a learning use of this board disable it on SystemInit.c -// **BUT** SIM_COPC register can only be written once after reset and so it can not be enabled later -// I think best solution is to overload SystemInit() using: void $Sub$$SystemInit (void) { -// original SystemInit() does not appear in export, it must be dowloaded from: -// https://developer.mbed.org/users/mbed_official/code/mbed-src/file/a11c0372f0ba/targets/cmsis/TARGET_Freescale/TARGET_KLXX/TARGET_KL25Z/system_MKL25Z4.c -// you can arrive there from: -// https://developer.mbed.org/handbook/mbed-SDK and https://developer.mbed.org/users/mbed_official/code/mbed-src/ +//WATCHDOG ENABLED (overloading library function SystemInit.c) +// +// The KL25Z watchdog is enabled after reset with timeout=1024ms (2^10*LPO=1KHz) +// To make the dvice easy to use on mbed, th watchdog is disabled at startup +// (in file SystemInit.c) **BUT** SIM_COPC register can only be written once +// after reset and so it can not be enabled later. To resolve this, +// SystemInit() is overloaded using: void $Sub$$SystemInit (void) { // -// MORE on this on: https://developer.mbed.org/users/chris/notebook/Patching-functions-and-libraries/ -// and searching "ARM Compiler toolchain Using the Linker" "Using $Super$$ and $Sub$$ to patch symbol definitions" +// The original SystemInit() can be dowloaded from: +// https://developer.mbed.org/users/mbed_official/code/mbed-src/ +// navigating to ... targets/cmsis/TARGET_Freescale/TARGET_KLXX/ +// TARGET_KL25Z/system_MKL25Z4.c +// +// MORE on how this overides the original initialisation: +// https://developer.mbed.org/users/chris/notebook/Patching-functions-and-libraries/ +// and searching "ARM Compiler toolchain Using the Linker" +// "Using $Super$$ and $Sub$$ to patch symbol definitions" -// Kick (feed, reload) our watchdog timer -void wdt_kick(){ - SIM->SRVCOP = (uint32_t)0x55u; - SIM->SRVCOP = (uint32_t)0xAAu; -} -// That set RED LED ON, do that first to evaluate boot time of peripherals #define LIGHT 0 #define DARK 1 DigitalOut led_red(LED_RED, LIGHT); -DigitalOut led_green(LED_GREEN, DARK); DigitalOut led_blue(LED_BLUE, DARK); -//Virtual serial port over USB using inboard second USB connector (USBKL25Z port) -#include "USBSerial.h" -//USBSerial pc; -//...( vendor_id, product_id, product_release, connect_blocking ) -USBSerial pc( 0x1f00, 0x2012, 0x0001, false ); -//REMEMBER TO USE: class USBSerial -> connect_blocking = false (default is true) -// to avoid block of program when no USB cable connected in USBKL25Z port -//NOTE: USBSerial still blocks program when closing serial terminal, that is fixed by our watchdog - +// Demonstate the watchdog +// The red LED is on after reset +// Th blue LED flashes and the watch dog is fed +// Aft 5sec, stop feeding the watchdog +enum wState {Working, Stopped} ; int main(void) { - //to evaluate boot time I set led RED ON at the beginning of our main.cpp. TODO?: do it in SystemInit? + // initialise watchdog + wdt_1sec() ; + + // state of the watch dog + wState watchDog = Working ; + int counter = 10 ; + + // show start-up led_blue = DARK; - led_green = DARK; + led_red = LIGHT; + wait(0.5) ; led_red = DARK; + + // Note: the red LED now stays OFF until processor reset while(1) { led_blue = LIGHT; - pc.printf("I am a virtual serial port\r\n"); // THAT COULD BLOCK - wdt_kick(); + wait(0.25); + if (watchDog == Working) wdt_kick_all() ; led_blue = DARK; - wait(0.5); + wait(0.25); + + // When the counter rachs zero, chang state to stop watchdog + if (counter > 0) counter-- ; else watchDog = Stopped ; } } \ No newline at end of file