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.
main.cpp@0:9498eae934ed, 2014-02-01 (annotated)
- Committer:
- djbottrill
- Date:
- Sat Feb 01 01:03:05 2014 +0000
- Revision:
- 0:9498eae934ed
- Child:
- 1:bac7ddc3679a
Doesn't work on KL25Z
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
djbottrill | 0:9498eae934ed | 1 | #include "mbed.h" |
djbottrill | 0:9498eae934ed | 2 | |
djbottrill | 0:9498eae934ed | 3 | //DigitalOut led(LED1); |
djbottrill | 0:9498eae934ed | 4 | DigitalOut blueled (LED_BLUE); |
djbottrill | 0:9498eae934ed | 5 | DigitalOut greenled (LED_GREEN); |
djbottrill | 0:9498eae934ed | 6 | DigitalOut redled (LED_RED); |
djbottrill | 0:9498eae934ed | 7 | |
djbottrill | 0:9498eae934ed | 8 | Serial pc(USBTX,USBRX); |
djbottrill | 0:9498eae934ed | 9 | |
djbottrill | 0:9498eae934ed | 10 | |
djbottrill | 0:9498eae934ed | 11 | int main() { |
djbottrill | 0:9498eae934ed | 12 | blueled = 1; // Off |
djbottrill | 0:9498eae934ed | 13 | greenled = 1; // Off |
djbottrill | 0:9498eae934ed | 14 | redled = 1; // Off |
djbottrill | 0:9498eae934ed | 15 | |
djbottrill | 0:9498eae934ed | 16 | SIM->COPC= 0x0C; // Initialise Watchdog for 2^10 cycles of 1 KHz LPO clock ~ 1 second |
djbottrill | 0:9498eae934ed | 17 | // Use the Macro in 'MKL25Z4.h' to write to the COPC options field |
djbottrill | 0:9498eae934ed | 18 | // https://mbed.org/users/mbed_official/code/mbed-Freescale/file/3a8b2b3870fb/cmsis/KL25Z/MKL25Z4.h |
djbottrill | 0:9498eae934ed | 19 | // SIM_COPC_COPT(0x0C); // Initialise Watchdog for 2^10 cycles of 1 KHz LPO clock ~ 1 second |
djbottrill | 0:9498eae934ed | 20 | |
djbottrill | 0:9498eae934ed | 21 | blueled = 0; // turn on blue BEFORE we test comms |
djbottrill | 0:9498eae934ed | 22 | pc.printf("Hello World!\r\n");// Show that the code has started running at the beginning |
djbottrill | 0:9498eae934ed | 23 | blueled = 1; // turn off Blue |
djbottrill | 0:9498eae934ed | 24 | greenled = 0; // turn on Green AFTER we tested comms |
djbottrill | 0:9498eae934ed | 25 | |
djbottrill | 0:9498eae934ed | 26 | int iHang = 0; // Initialise our loop counter to Zero |
djbottrill | 0:9498eae934ed | 27 | |
djbottrill | 0:9498eae934ed | 28 | while(1) { // The start of the 'forever' main loop |
djbottrill | 0:9498eae934ed | 29 | pc.printf("loop %d\r\n", iHang); // Show that we are looping |
djbottrill | 0:9498eae934ed | 30 | wait(0.1); // wait .1 second |
djbottrill | 0:9498eae934ed | 31 | blueled = 0; // turn on blue (as well as green) while we loop |
djbottrill | 0:9498eae934ed | 32 | |
djbottrill | 0:9498eae934ed | 33 | SIM->SRVCOP = 0x55; // refresh the Watchdog to stop it resetting us |
djbottrill | 0:9498eae934ed | 34 | SIM->SRVCOP = 0xAA; |
djbottrill | 0:9498eae934ed | 35 | // Use the Macro in 'MKL25Z4.h' to write to the SRVCOP data field |
djbottrill | 0:9498eae934ed | 36 | // https://mbed.org/users/mbed_official/code/mbed-Freescale/file/3a8b2b3870fb/cmsis/KL25Z/MKL25Z4.h |
djbottrill | 0:9498eae934ed | 37 | // SIM_SRVCOP_SRVCOP(0x55);// refresh the Watchdog to stop it resetting us |
djbottrill | 0:9498eae934ed | 38 | // SIM_SRVCOP_SRVCOP(0xAA); |
djbottrill | 0:9498eae934ed | 39 | |
djbottrill | 0:9498eae934ed | 40 | iHang++; // Count the number of times we have prevented a reboot |
djbottrill | 0:9498eae934ed | 41 | |
djbottrill | 0:9498eae934ed | 42 | if(iHang == 10) { // after 10 times round the loop... |
djbottrill | 0:9498eae934ed | 43 | blueled = 1; // turn off Blue |
djbottrill | 0:9498eae934ed | 44 | greenled = 1; // turn off Green |
djbottrill | 0:9498eae934ed | 45 | redled = 0; // turn on Red |
djbottrill | 0:9498eae934ed | 46 | while(1) ; // Stall the program until the watchdog resets the micro' |
djbottrill | 0:9498eae934ed | 47 | } |
djbottrill | 0:9498eae934ed | 48 | |
djbottrill | 0:9498eae934ed | 49 | } // End of 'forever' loop |
djbottrill | 0:9498eae934ed | 50 | } |