Example of enabling watchdog in KL25Z to prevent block of USBSerial when disconecting serial console

Dependencies:   USBDevice mbed

Code is selfdocumented.

I hope you enyoy it

Committer:
Manel_Marin
Date:
Sat Nov 21 13:24:29 2015 +0000
Revision:
0:5ce3cfc57999
Working fully documented example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Manel_Marin 0:5ce3cfc57999 1 #include "mbed.h"
Manel_Marin 0:5ce3cfc57999 2
Manel_Marin 0:5ce3cfc57999 3 //WATHDOG ENABLED IN OUR KL25Z_SystemInit.c (overloading library function SystemInit.c)
Manel_Marin 0:5ce3cfc57999 4 // It happens in this uC after reset watchdog is enabled with timeout=1024ms (2^10*LPO=1KHz)
Manel_Marin 0:5ce3cfc57999 5 // so mbed developers to allow a learning use of this board disable it on SystemInit.c
Manel_Marin 0:5ce3cfc57999 6 // **BUT** SIM_COPC register can only be written once after reset and so it can not be enabled later
Manel_Marin 0:5ce3cfc57999 7 // I think best solution is to overload SystemInit() using: void $Sub$$SystemInit (void) {
Manel_Marin 0:5ce3cfc57999 8 // original SystemInit() does not appear in export, it must be dowloaded from:
Manel_Marin 0:5ce3cfc57999 9 // https://developer.mbed.org/users/mbed_official/code/mbed-src/file/a11c0372f0ba/targets/cmsis/TARGET_Freescale/TARGET_KLXX/TARGET_KL25Z/system_MKL25Z4.c
Manel_Marin 0:5ce3cfc57999 10 // you can arrive there from:
Manel_Marin 0:5ce3cfc57999 11 // https://developer.mbed.org/handbook/mbed-SDK and https://developer.mbed.org/users/mbed_official/code/mbed-src/
Manel_Marin 0:5ce3cfc57999 12 //
Manel_Marin 0:5ce3cfc57999 13 // MORE on this on: https://developer.mbed.org/users/chris/notebook/Patching-functions-and-libraries/
Manel_Marin 0:5ce3cfc57999 14 // and searching "ARM Compiler toolchain Using the Linker" "Using $Super$$ and $Sub$$ to patch symbol definitions"
Manel_Marin 0:5ce3cfc57999 15
Manel_Marin 0:5ce3cfc57999 16 // Kick (feed, reload) our watchdog timer
Manel_Marin 0:5ce3cfc57999 17 void wdt_kick(){
Manel_Marin 0:5ce3cfc57999 18 SIM->SRVCOP = (uint32_t)0x55u;
Manel_Marin 0:5ce3cfc57999 19 SIM->SRVCOP = (uint32_t)0xAAu;
Manel_Marin 0:5ce3cfc57999 20 }
Manel_Marin 0:5ce3cfc57999 21
Manel_Marin 0:5ce3cfc57999 22 // That set RED LED ON, do that first to evaluate boot time of peripherals
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_green(LED_GREEN, DARK);
Manel_Marin 0:5ce3cfc57999 27 DigitalOut led_blue(LED_BLUE, DARK);
Manel_Marin 0:5ce3cfc57999 28
Manel_Marin 0:5ce3cfc57999 29 //Virtual serial port over USB using inboard second USB connector (USBKL25Z port)
Manel_Marin 0:5ce3cfc57999 30 #include "USBSerial.h"
Manel_Marin 0:5ce3cfc57999 31 //USBSerial pc;
Manel_Marin 0:5ce3cfc57999 32 //...( vendor_id, product_id, product_release, connect_blocking )
Manel_Marin 0:5ce3cfc57999 33 USBSerial pc( 0x1f00, 0x2012, 0x0001, false );
Manel_Marin 0:5ce3cfc57999 34 //REMEMBER TO USE: class USBSerial -> connect_blocking = false (default is true)
Manel_Marin 0:5ce3cfc57999 35 // to avoid block of program when no USB cable connected in USBKL25Z port
Manel_Marin 0:5ce3cfc57999 36 //NOTE: USBSerial still blocks program when closing serial terminal, that is fixed by our watchdog
Manel_Marin 0:5ce3cfc57999 37
Manel_Marin 0:5ce3cfc57999 38
Manel_Marin 0:5ce3cfc57999 39 int main(void) {
Manel_Marin 0:5ce3cfc57999 40 //to evaluate boot time I set led RED ON at the beginning of our main.cpp. TODO?: do it in SystemInit?
Manel_Marin 0:5ce3cfc57999 41 led_blue = DARK;
Manel_Marin 0:5ce3cfc57999 42 led_green = DARK;
Manel_Marin 0:5ce3cfc57999 43 led_red = DARK;
Manel_Marin 0:5ce3cfc57999 44 while(1)
Manel_Marin 0:5ce3cfc57999 45 {
Manel_Marin 0:5ce3cfc57999 46 led_blue = LIGHT;
Manel_Marin 0:5ce3cfc57999 47 pc.printf("I am a virtual serial port\r\n"); // THAT COULD BLOCK
Manel_Marin 0:5ce3cfc57999 48 wdt_kick();
Manel_Marin 0:5ce3cfc57999 49 led_blue = DARK;
Manel_Marin 0:5ce3cfc57999 50 wait(0.5);
Manel_Marin 0:5ce3cfc57999 51 }
Manel_Marin 0:5ce3cfc57999 52 }