Are you frustrated that the KL25Z watchdog timer refuses to work? This programme offers an alternate approach using a ticker interrupt to monitor the main loop and generate a hardware reset if the software controlled output pin is connected to the reset pin.
Diff: main.cpp
- Revision:
- 1:bac7ddc3679a
- Parent:
- 0:9498eae934ed
--- a/main.cpp Sat Feb 01 01:03:05 2014 +0000 +++ b/main.cpp Sat Feb 01 15:52:15 2014 +0000 @@ -1,50 +1,66 @@ +/* +For whatever reason the Watchdog timer cannot be made to work on a KL25Z using MBED. +This example is based on Sw_reset by Peter Andersson. +It uses a ticker routine that checks if random number generated in the main loop has not changed since the last ticker interrupt, +an indication that the main loop has frozen. Once this condition is detected a pin, PTB10 in this example, is forced low. +If this is linked to the opposite reset pin of the KL25Z be board will be restarted. + +*/ + #include "mbed.h" -//DigitalOut led(LED1); -DigitalOut blueled (LED_BLUE); -DigitalOut greenled (LED_GREEN); -DigitalOut redled (LED_RED); +void wd_check(void); //Define the watchdog check function "wd_check" -Serial pc(USBTX,USBRX); +Ticker watchdog; //Define a Ticker with the name "watchdog" -int main() { - blueled = 1; // Off - greenled = 1; // Off - redled = 1; // Off +//DigitalOut led(LED1); +DigitalOut blueled (LED_BLUE); +DigitalOut greenled (LED_GREEN); +DigitalOut redled (LED_RED); +DigitalInOut rst (PTB10); //Connect this to the reset pin + +Serial pc(USBTX,USBRX); //Send diagnostics via USB serrial port - SIM->COPC= 0x0C; // Initialise Watchdog for 2^10 cycles of 1 KHz LPO clock ~ 1 second - // Use the Macro in 'MKL25Z4.h' to write to the COPC options field - // https://mbed.org/users/mbed_official/code/mbed-Freescale/file/3a8b2b3870fb/cmsis/KL25Z/MKL25Z4.h -// SIM_COPC_COPT(0x0C); // Initialise Watchdog for 2^10 cycles of 1 KHz LPO clock ~ 1 second - - blueled = 0; // turn on blue BEFORE we test comms - pc.printf("Hello World!\r\n");// Show that the code has started running at the beginning - blueled = 1; // turn off Blue - greenled = 0; // turn on Green AFTER we tested comms +int iHang; //Hangup counter +int timeout; //Main loop generated random number +int timeout1; //Previous randon number - int iHang = 0; // Initialise our loop counter to Zero +//Ticker interrupt code +void wd_check() //the function that Ticker will call +{ + blueled=!blueled; //toggle the blue LED + if (timeout1==timeout){ //Is the new timeout randon number the same as the last + rst.output(); //If so the main loop must have frozen + rst.write(0); //so force a hardware reset + } //else + timeout1=timeout; //Store current timeout as the old number ready for next interrupt +} //return - while(1) { // The start of the 'forever' main loop - pc.printf("loop %d\r\n", iHang); // Show that we are looping - wait(0.1); // wait .1 second - blueled = 0; // turn on blue (as well as green) while we loop - - SIM->SRVCOP = 0x55; // refresh the Watchdog to stop it resetting us - SIM->SRVCOP = 0xAA; - // Use the Macro in 'MKL25Z4.h' to write to the SRVCOP data field - // https://mbed.org/users/mbed_official/code/mbed-Freescale/file/3a8b2b3870fb/cmsis/KL25Z/MKL25Z4.h -// SIM_SRVCOP_SRVCOP(0x55);// refresh the Watchdog to stop it resetting us -// SIM_SRVCOP_SRVCOP(0xAA); +//Main Program +int main() { + greenled = 1; //Off + redled = 1; //Off + + blueled = 0; //turn on blue BEFORE we test comms + pc.printf("Hello World!\r\n"); //Show that the code has started running at the beginning + greenled = 0; //turn on Green AFTER we tested comms + + watchdog.attach(&wd_check, 2); //Initialise the ticker + + int iHang = 0; //Initialise our loop counter to Zero - iHang++; // Count the number of times we have prevented a reboot + while(1) { //The start of the 'forever' main loop + pc.printf("loop %d\r\n", iHang); //Show that we are looping + wait(0.1); //wait .1 second + iHang++; //Count the number of times we have prevented a reboot + timeout== rand(); //set timeout to a random number if this doesn't change then the program must have frozen - if(iHang == 10) { // after 10 times round the loop... - blueled = 1; // turn off Blue - greenled = 1; // turn off Green - redled = 0; // turn on Red - while(1) ; // Stall the program until the watchdog resets the micro' + if(iHang == 10) { //after 10 times round the loop... + blueled = 1; //turn off Blue + greenled = 1; //turn off Green + redled = 0; //turn on Red + while(1) ; //Stall the program until the watchdog resets the micro' } - - } // End of 'forever' loop + } //End of 'forever' loop }