Lab 6 Code 1 Watchdog Fault Coverage

Fork of Watchdog_sample_nocoverage by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
WilliamMarshQMUL
Date:
Tue Feb 28 17:39:50 2017 +0000
Parent:
0:5ce3cfc57999
Child:
2:c31a1758ac38
Commit message:
First version

Changed in this revision

KL25Z_SystemInit.c Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
wdt.cpp Show annotated file Show diff for this revision Revisions of this file
wdt.h Show annotated file Show diff for this revision Revisions of this file
--- a/KL25Z_SystemInit.c	Sat Nov 21 13:24:29 2015 +0000
+++ b/KL25Z_SystemInit.c	Tue Feb 28 17:39:50 2017 +0000
@@ -114,18 +114,18 @@
   SIM->COPC = (uint32_t)0x00u;
 #endif /* (DISABLE_WDOG) */
 #if (CLOCK_SETUP == 0)
-  /* SIM->CLKDIV1: OUTDIV1=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,OUTDIV4=2,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
+  /* SIM->CLKDIV1: OUTDIV1=0 */
   SIM->CLKDIV1 = (uint32_t)0x00020000UL; /* Update system prescalers */
   /* Switch to FEI Mode */
   /* MCG->C1: CLKS=0,FRDIV=0,IREFS=1,IRCLKEN=1,IREFSTEN=0 */
   MCG->C1 = (uint8_t)0x06U;
-  /* MCG_C2: LOCRE0=0,??=0,RANGE0=0,HGO0=0,EREFS0=0,LP=0,IRCS=0 */
+  /* MCG_C2: LOCRE0=0, =0,RANGE0=0,HGO0=0,EREFS0=0,LP=0,IRCS=0 */
   MCG->C2 = (uint8_t)0x00U;
   /* MCG->C4: DMX32=0,DRST_DRS=1 */
   MCG->C4 = (uint8_t)((MCG->C4 & (uint8_t)~(uint8_t)0xC0U) | (uint8_t)0x20U);
-  /* OSC0->CR: ERCLKEN=1,??=0,EREFSTEN=0,??=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
+  /* OSC0->CR: ERCLKEN=1,=0,EREFSTEN=0,=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
   OSC0->CR = (uint8_t)0x80U;
-  /* MCG->C5: ??=0,PLLCLKEN0=0,PLLSTEN0=0,PRDIV0=0 */
+  /* MCG->C5: =0,PLLCLKEN0=0,PLLSTEN0=0,PRDIV0=0 */
   MCG->C5 = (uint8_t)0x00U;
   /* MCG->C6: LOLIE0=0,PLLS=0,CME0=0,VDIV0=0 */
   MCG->C6 = (uint8_t)0x00U;
@@ -136,22 +136,22 @@
 #elif (CLOCK_SETUP == 1)
   /* SIM->SCGC5: PORTA=1 */
   SIM->SCGC5 |= (uint32_t)0x0200UL;     /* Enable clock gate for ports to enable pin routing */
-  /* SIM->CLKDIV1: OUTDIV1=1,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,OUTDIV4=1,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
+  /* SIM->CLKDIV1: OUTDIV1=1,OUTDIV4=1 */
   SIM->CLKDIV1 = (uint32_t)0x10010000UL; /* Update system prescalers */
   /* PORTA->PCR18: ISF=0,MUX=0 */
   PORTA->PCR[18] &= (uint32_t)~0x01000700UL;
   /* PORTA->PCR19: ISF=0,MUX=0 */
   PORTA->PCR[19] &= (uint32_t)~0x01000700UL;
   /* Switch to FBE Mode */
-  /* OSC0->CR: ERCLKEN=1,??=0,EREFSTEN=0,??=0,SC2P=1,SC4P=0,SC8P=0,SC16P=1 */
+  /* OSC0->CR: ERCLKEN=1,EREFSTEN=0,SC2P=1,SC4P=0,SC8P=0,SC16P=1 */
   OSC0->CR = (uint8_t)0x89U;
-  /* MCG->C2: LOCRE0=0,??=0,RANGE0=2,HGO0=0,EREFS0=1,LP=0,IRCS=0 */
+  /* MCG->C2: LOCRE0=0, RANGE0=2,HGO0=0,EREFS0=1,LP=0,IRCS=0 */
   MCG->C2 = (uint8_t)0x24U;
   /* MCG->C1: CLKS=2,FRDIV=3,IREFS=0,IRCLKEN=1,IREFSTEN=0 */
   MCG->C1 = (uint8_t)0x9AU;
   /* MCG->C4: DMX32=0,DRST_DRS=0 */
   MCG->C4 &= (uint8_t)~(uint8_t)0xE0U;
-  /* MCG->C5: ??=0,PLLCLKEN0=0,PLLSTEN0=0,PRDIV0=1 */
+  /* MCG->C5: PLLCLKEN0=0,PLLSTEN0=0,PRDIV0=1 */
   MCG->C5 = (uint8_t)0x01U;
   /* MCG->C6: LOLIE0=0,PLLS=0,CME0=0,VDIV0=0 */
   MCG->C6 = (uint8_t)0x00U;
@@ -174,22 +174,22 @@
 #elif (CLOCK_SETUP == 2)
   /* SIM->SCGC5: PORTA=1 */
   SIM->SCGC5 |= (uint32_t)0x0200UL;     /* Enable clock gate for ports to enable pin routing */
-  /* SIM->CLKDIV1: OUTDIV1=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,OUTDIV4=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
+  /* SIM->CLKDIV1: OUTDIV1=0,OUTDIV4=0 */
   SIM->CLKDIV1 = (uint32_t)0x00000000UL; /* Update system prescalers */
   /* PORTA->PCR18: ISF=0,MUX=0 */
   PORTA->PCR[18] &= (uint32_t)~0x01000700UL;
   /* PORTA->PCR19: ISF=0,MUX=0 */
   PORTA->PCR[19] &= (uint32_t)~0x01000700UL;
   /* Switch to FBE Mode */
-  /* OSC0->CR: ERCLKEN=1,??=0,EREFSTEN=0,??=0,SC2P=1,SC4P=0,SC8P=0,SC16P=1 */
+  /* OSC0->CR: ERCLKEN=1,EREFSTEN=0,SC2P=1,SC4P=0,SC8P=0,SC16P=1 */
   OSC0->CR = (uint8_t)0x89U;
-  /* MCG->C2: LOCRE0=0,??=0,RANGE0=2,HGO0=0,EREFS0=1,LP=0,IRCS=0 */
+  /* MCG->C2: LOCRE0=0,RANGE0=2,HGO0=0,EREFS0=1,LP=0,IRCS=0 */
   MCG->C2 = (uint8_t)0x24U;
   /* MCG->C1: CLKS=2,FRDIV=3,IREFS=0,IRCLKEN=1,IREFSTEN=0 */
   MCG->C1 = (uint8_t)0x9AU;
   /* MCG->C4: DMX32=0,DRST_DRS=0 */
   MCG->C4 &= (uint8_t)~(uint8_t)0xE0U;
-  /* MCG->C5: ??=0,PLLCLKEN0=0,PLLSTEN0=0,PRDIV0=0 */
+  /* MCG->C5: PLLCLKEN0=0,PLLSTEN0=0,PRDIV0=0 */
   MCG->C5 = (uint8_t)0x00U;
   /* MCG->C6: LOLIE0=0,PLLS=0,CME0=0,VDIV0=0 */
   MCG->C6 = (uint8_t)0x00U;
@@ -198,23 +198,23 @@
   while((MCG->S & 0x0CU) != 0x08U) {    /* Wait until external reference clock is selected as MCG output */
   }
   /* Switch to BLPE Mode */
-  /* MCG->C2: LOCRE0=0,??=0,RANGE0=2,HGO0=0,EREFS0=1,LP=1,IRCS=0 */
+  /* MCG->C2: LOCRE0=0,RANGE0=2,HGO0=0,EREFS0=1,LP=1,IRCS=0 */
   MCG->C2 = (uint8_t)0x26U;
   while((MCG->S & 0x0CU) != 0x08U) {    /* Wait until external reference clock is selected as MCG output */
   }
 #elif (CLOCK_SETUP == 3)  
 /* SIM->SCGC5: PORTA=1 */
   SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;   /* Enable clock gate for ports to enable pin routing */
-  /* SIM->CLKDIV1: OUTDIV1=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,OUTDIV4=1,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
+  /* SIM->CLKDIV1: OUTDIV1=0, OUTDIV4=1 */
   SIM->CLKDIV1 = (SIM_CLKDIV1_OUTDIV1(0x00) | SIM_CLKDIV1_OUTDIV4(0x01)); /* Update system prescalers */
   /* PORTA->PCR[3]: ISF=0,MUX=0 */
   PORTA->PCR[3] &= (uint32_t)~(uint32_t)((PORT_PCR_ISF_MASK | PORT_PCR_MUX(0x07)));
   /* PORTA->PCR[4]: ISF=0,MUX=0 */
   PORTA->PCR[4] &= (uint32_t)~(uint32_t)((PORT_PCR_ISF_MASK | PORT_PCR_MUX(0x07)));
   /* Switch to FEE Mode */
-  /* MCG->C2: LOCRE0=0,??=0,RANGE0=0,HGO0=0,EREFS0=1,LP=0,IRCS=0 */
+  /* MCG->C2: LOCRE0=0,RANGE0=0,HGO0=0,EREFS0=1,LP=0,IRCS=0 */
   MCG->C2 = (MCG_C2_RANGE0(0x00) | MCG_C2_EREFS0_MASK);
-  /* OSC0->CR: ERCLKEN=1,??=0,EREFSTEN=0,??=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
+  /* OSC0->CR: ERCLKEN=1,EREFSTEN=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
   OSC0->CR = OSC_CR_ERCLKEN_MASK | OSC_CR_SC16P_MASK | OSC_CR_SC4P_MASK | OSC_CR_SC2P_MASK;
   /* MCG->C1: CLKS=0,FRDIV=0,IREFS=0,IRCLKEN=1,IREFSTEN=0 */
   MCG->C1 = (MCG_C1_CLKS(0x00) | MCG_C1_FRDIV(0x00) | MCG_C1_IRCLKEN_MASK);
--- a/USBDevice.lib	Sat Nov 21 13:24:29 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/USBDevice/#2af474687369
--- 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wdt.cpp	Tue Feb 28 17:39:50 2017 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "wdt.h"
+
+
+// Simple Library for Watchdog
+// ---------------------------
+
+// Initialise watchdog using 1KHz clock
+//  To prevent overwriting, only a single write to the COPC register possible
+//  
+void wdt_1sec() {
+     // 1024ms, not windowed - this is the default 
+     /* SIM_COPC: COPT=11,COPCLKS=0,COPW=0 */
+     SIM->COPC = (uint32_t)0x0Cu;
+}
+
+void wdt_256ms() {
+     // 256ms, not windowed
+     /* SIM_COPC: COPT=10,COPCLKS=0,COPW=0 */
+     SIM->COPC = (uint32_t)0x08u;
+}
+
+void wdt_32ms() {
+     // 32ms, not windowed  
+     /* SIM_COPC: COPT=01,COPCLKS=0,COPW=0 */
+     SIM->COPC = (uint32_t)0x04u;
+}
+
+// Kick (feed, reload) our watchdog timer
+void wdt_kick_all(){
+    SIM->SRVCOP = (uint32_t)0x55u;
+    SIM->SRVCOP = (uint32_t)0xAAu;
+}
+
+void wdt_kickA(){
+    SIM->SRVCOP = (uint32_t)0x55u;
+}
+
+void wdt_kickB(){
+    SIM->SRVCOP = (uint32_t)0xAAu;
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wdt.h	Tue Feb 28 17:39:50 2017 +0000
@@ -0,0 +1,20 @@
+
+#ifndef WDT_H
+#define WDT_H
+
+// Simple Library for Watchdog
+// ---------------------------
+
+// Initialise watchdog using 1KHz clock
+//  To prevent overwriting, only a single write to the COPC register possible
+//  
+void wdt_1sec() ; // 1024ms, not windowed, DEFAULT
+void wdt_256ms(); // 256ms, not windowed
+void wdt_32ms();  // 32ms, not windowed  
+
+// Kick (feed, reload) our watchdog timer
+void wdt_kick_all(); // full sequence
+void wdt_kickA();    // first part
+void wdt_kickB();    // second part
+
+#endif
\ No newline at end of file