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.

Dependencies:   mbed

Committer:
djbottrill
Date:
Sat Feb 01 15:52:15 2014 +0000
Revision:
1:bac7ddc3679a
Parent:
0:9498eae934ed
Working example of a ticker driven Watchdog for the KL25Z. This requires a link from one of the pins "PTB10" to the reset pin.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
djbottrill 1:bac7ddc3679a 1 /*
djbottrill 1:bac7ddc3679a 2 For whatever reason the Watchdog timer cannot be made to work on a KL25Z using MBED.
djbottrill 1:bac7ddc3679a 3 This example is based on Sw_reset by Peter Andersson.
djbottrill 1:bac7ddc3679a 4 It uses a ticker routine that checks if random number generated in the main loop has not changed since the last ticker interrupt,
djbottrill 1:bac7ddc3679a 5 an indication that the main loop has frozen. Once this condition is detected a pin, PTB10 in this example, is forced low.
djbottrill 1:bac7ddc3679a 6 If this is linked to the opposite reset pin of the KL25Z be board will be restarted.
djbottrill 1:bac7ddc3679a 7
djbottrill 1:bac7ddc3679a 8 */
djbottrill 1:bac7ddc3679a 9
djbottrill 0:9498eae934ed 10 #include "mbed.h"
djbottrill 0:9498eae934ed 11
djbottrill 1:bac7ddc3679a 12 void wd_check(void); //Define the watchdog check function "wd_check"
djbottrill 0:9498eae934ed 13
djbottrill 1:bac7ddc3679a 14 Ticker watchdog; //Define a Ticker with the name "watchdog"
djbottrill 0:9498eae934ed 15
djbottrill 0:9498eae934ed 16
djbottrill 1:bac7ddc3679a 17 //DigitalOut led(LED1);
djbottrill 1:bac7ddc3679a 18 DigitalOut blueled (LED_BLUE);
djbottrill 1:bac7ddc3679a 19 DigitalOut greenled (LED_GREEN);
djbottrill 1:bac7ddc3679a 20 DigitalOut redled (LED_RED);
djbottrill 1:bac7ddc3679a 21 DigitalInOut rst (PTB10); //Connect this to the reset pin
djbottrill 1:bac7ddc3679a 22
djbottrill 1:bac7ddc3679a 23 Serial pc(USBTX,USBRX); //Send diagnostics via USB serrial port
djbottrill 0:9498eae934ed 24
djbottrill 1:bac7ddc3679a 25 int iHang; //Hangup counter
djbottrill 1:bac7ddc3679a 26 int timeout; //Main loop generated random number
djbottrill 1:bac7ddc3679a 27 int timeout1; //Previous randon number
djbottrill 0:9498eae934ed 28
djbottrill 1:bac7ddc3679a 29 //Ticker interrupt code
djbottrill 1:bac7ddc3679a 30 void wd_check() //the function that Ticker will call
djbottrill 1:bac7ddc3679a 31 {
djbottrill 1:bac7ddc3679a 32 blueled=!blueled; //toggle the blue LED
djbottrill 1:bac7ddc3679a 33 if (timeout1==timeout){ //Is the new timeout randon number the same as the last
djbottrill 1:bac7ddc3679a 34 rst.output(); //If so the main loop must have frozen
djbottrill 1:bac7ddc3679a 35 rst.write(0); //so force a hardware reset
djbottrill 1:bac7ddc3679a 36 } //else
djbottrill 1:bac7ddc3679a 37 timeout1=timeout; //Store current timeout as the old number ready for next interrupt
djbottrill 1:bac7ddc3679a 38 } //return
djbottrill 0:9498eae934ed 39
djbottrill 1:bac7ddc3679a 40 //Main Program
djbottrill 1:bac7ddc3679a 41 int main() {
djbottrill 1:bac7ddc3679a 42 greenled = 1; //Off
djbottrill 1:bac7ddc3679a 43 redled = 1; //Off
djbottrill 1:bac7ddc3679a 44
djbottrill 1:bac7ddc3679a 45 blueled = 0; //turn on blue BEFORE we test comms
djbottrill 1:bac7ddc3679a 46 pc.printf("Hello World!\r\n"); //Show that the code has started running at the beginning
djbottrill 1:bac7ddc3679a 47 greenled = 0; //turn on Green AFTER we tested comms
djbottrill 1:bac7ddc3679a 48
djbottrill 1:bac7ddc3679a 49 watchdog.attach(&wd_check, 2); //Initialise the ticker
djbottrill 1:bac7ddc3679a 50
djbottrill 1:bac7ddc3679a 51 int iHang = 0; //Initialise our loop counter to Zero
djbottrill 0:9498eae934ed 52
djbottrill 1:bac7ddc3679a 53 while(1) { //The start of the 'forever' main loop
djbottrill 1:bac7ddc3679a 54 pc.printf("loop %d\r\n", iHang); //Show that we are looping
djbottrill 1:bac7ddc3679a 55 wait(0.1); //wait .1 second
djbottrill 1:bac7ddc3679a 56 iHang++; //Count the number of times we have prevented a reboot
djbottrill 1:bac7ddc3679a 57 timeout== rand(); //set timeout to a random number if this doesn't change then the program must have frozen
djbottrill 0:9498eae934ed 58
djbottrill 1:bac7ddc3679a 59 if(iHang == 10) { //after 10 times round the loop...
djbottrill 1:bac7ddc3679a 60 blueled = 1; //turn off Blue
djbottrill 1:bac7ddc3679a 61 greenled = 1; //turn off Green
djbottrill 1:bac7ddc3679a 62 redled = 0; //turn on Red
djbottrill 1:bac7ddc3679a 63 while(1) ; //Stall the program until the watchdog resets the micro'
djbottrill 0:9498eae934ed 64 }
djbottrill 1:bac7ddc3679a 65 } //End of 'forever' loop
djbottrill 0:9498eae934ed 66 }