Lab 6 - Step 3: Add Detection of Hardware Failure

Fork of ADCandticker_sample by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
novinfard
Date:
Fri Mar 09 15:19:12 2018 +0000
Parent:
2:de96c8c234b6
Commit message:
Finalising Project

Changed in this revision

KL25Z_SystemInit.c Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json 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
diff -r de96c8c234b6 -r c6c88a1a58a8 KL25Z_SystemInit.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KL25Z_SystemInit.c	Fri Mar 09 15:19:12 2018 +0000
@@ -0,0 +1,233 @@
+/*
+** ###################################################################
+**     Processor:           MKL25Z128VLK4
+**     Compilers:           ARM Compiler
+**                          Freescale C/C++ for Embedded ARM
+**                          GNU C Compiler
+**                          IAR ANSI C/C++ Compiler for ARM
+**
+**     Reference manual:    KL25RM, Rev.1, Jun 2012
+**     Version:             rev. 1.1, 2012-06-21
+**
+**     Abstract:
+**         Provides a system configuration function and a global variable that
+**         contains the system frequency. It configures the device and initializes
+**         the oscillator (PLL) that is part of the microcontroller device.
+**
+**     Copyright: 2012 Freescale Semiconductor, Inc. All Rights Reserved.
+**
+**     http:                 www.freescale.com
+**     mail:                 support@freescale.com
+**
+**     Revisions:
+**     - rev. 1.0 (2012-06-13)
+**         Initial version.
+**     - rev. 1.1 (2012-06-21)
+**         Update according to reference manual rev. 1.
+**
+** ###################################################################
+*/
+
+/**
+ * @file MKL25Z4
+ * @version 1.1
+ * @date 2012-06-21
+ * @brief Device specific configuration file for MKL25Z4 (implementation file)
+ *
+ * Provides a system configuration function and a global variable that contains
+ * the system frequency. It configures the device and initializes the oscillator
+ * (PLL) that is part of the microcontroller device.
+ */
+
+#include <stdint.h>
+#include "MKL25Z4.h"
+
+//MODIFICATION: We DO want watchdog, uC default after reset is enabled with timeout=1024ms (2^10*LPO=1KHz)
+//#define DISABLE_WDOG    1
+
+#define CLOCK_SETUP     1
+/* Predefined clock setups
+   0 ... Multipurpose Clock Generator (MCG) in FLL Engaged Internal (FEI) mode
+         Reference clock source for MCG module is the slow internal clock source 32.768kHz
+         Core clock = 41.94MHz, BusClock = 13.98MHz
+   1 ... Multipurpose Clock Generator (MCG) in PLL Engaged External (PEE) mode
+         Reference clock source for MCG module is an external crystal 8MHz
+         Core clock = 48MHz, BusClock = 24MHz
+   2 ... Multipurpose Clock Generator (MCG) in Bypassed Low Power External (BLPE) mode
+         Core clock/Bus clock derived directly from an external crystal 8MHz with no multiplication
+         Core clock = 8MHz, BusClock = 8MHz
+   3 ... Multipurpose Clock Generator (MCG) in FLL Engaged External (FEE) mode
+         Reference clock source for MCG module is an external crystal 32.768kHz
+         Core clock = 47.97MHz, BusClock = 23.98MHz
+         This setup sets the RTC to be driven by the MCU clock directly without the need of an external source.
+         RTC register values are retained when MCU is reset although there will be a slight (mSec's)loss of time
+         accuracy durring the reset period. RTC will reset on power down. 
+*/
+
+/*----------------------------------------------------------------------------
+  Define clock source values
+ *----------------------------------------------------------------------------*/
+#if (CLOCK_SETUP == 0)
+    #define CPU_XTAL_CLK_HZ                 8000000u /* Value of the external crystal or oscillator clock frequency in Hz */
+    #define CPU_INT_SLOW_CLK_HZ             32768u   /* Value of the slow internal oscillator clock frequency in Hz  */
+    #define CPU_INT_FAST_CLK_HZ             4000000u /* Value of the fast internal oscillator clock frequency in Hz  */
+    #define DEFAULT_SYSTEM_CLOCK            41943040u /* Default System clock value */
+#elif (CLOCK_SETUP == 1)
+    #define CPU_XTAL_CLK_HZ                 8000000u /* Value of the external crystal or oscillator clock frequency in Hz */
+    #define CPU_INT_SLOW_CLK_HZ             32768u   /* Value of the slow internal oscillator clock frequency in Hz  */
+    #define CPU_INT_FAST_CLK_HZ             4000000u /* Value of the fast internal oscillator clock frequency in Hz  */
+    #define DEFAULT_SYSTEM_CLOCK            48000000u /* Default System clock value */
+#elif (CLOCK_SETUP == 2)
+    #define CPU_XTAL_CLK_HZ                 8000000u /* Value of the external crystal or oscillator clock frequency in Hz */
+    #define CPU_INT_SLOW_CLK_HZ             32768u   /* Value of the slow internal oscillator clock frequency in Hz  */
+    #define CPU_INT_FAST_CLK_HZ             4000000u /* Value of the fast internal oscillator clock frequency in Hz  */
+    #define DEFAULT_SYSTEM_CLOCK            8000000u /* Default System clock value */
+#elif (CLOCK_SETUP == 3)    
+    #define CPU_XTAL_CLK_HZ                 32768u   /* Value of the external crystal or oscillator clock frequency in Hz */
+    #define CPU_INT_SLOW_CLK_HZ             32768u   /* Value of the slow internal oscillator clock frequency in Hz  */
+    #define CPU_INT_FAST_CLK_HZ             4000000u /* Value of the fast internal oscillator clock frequency in Hz  */
+    #define DEFAULT_SYSTEM_CLOCK            47972352u /* Default System clock value */       
+#endif /* (CLOCK_SETUP == 3) */
+
+/* ----------------------------------------------------------------------------
+   -- Core clock
+   ---------------------------------------------------------------------------- */
+
+//MODIFICATION: That vartiable already exists
+// uint32_t SystemCoreClock = DEFAULT_SYSTEM_CLOCK;
+
+/* ----------------------------------------------------------------------------
+   -- SystemInit()
+   ---------------------------------------------------------------------------- */
+
+void $Sub$$SystemInit (void) {
+    
+    //MODIFICATION:
+    // That variable already exists, we set it here
+    SystemCoreClock = DEFAULT_SYSTEM_CLOCK;
+    // We want visual indication of boot time with red LED on
+    //TODO
+
+#if (DISABLE_WDOG)
+  /* Disable the WDOG module */
+  /* SIM_COPC: COPT=0,COPCLKS=0,COPW=0 */
+  SIM->COPC = (uint32_t)0x00u;
+#endif /* (DISABLE_WDOG) */
+#if (CLOCK_SETUP == 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 = (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 = (uint8_t)0x80U;
+  /* 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;
+  while((MCG->S & MCG_S_IREFST_MASK) == 0x00U) { /* Check that the source of the FLL reference clock is the internal reference clock. */
+  }
+  while((MCG->S & 0x0CU) != 0x00U) {    /* Wait until output of the FLL is selected */
+  }
+#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,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,EREFSTEN=0,SC2P=1,SC4P=0,SC8P=0,SC16P=1 */
+  OSC0->CR = (uint8_t)0x89U;
+  /* 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: 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;
+  while((MCG->S & MCG_S_IREFST_MASK) != 0x00U) { /* Check that the source of the FLL reference clock is the external reference clock. */
+  }
+  while((MCG->S & 0x0CU) != 0x08U) {    /* Wait until external reference clock is selected as MCG output */
+  }
+  /* Switch to PBE Mode */
+  /* MCG->C6: LOLIE0=0,PLLS=1,CME0=0,VDIV0=0 */
+  MCG->C6 = (uint8_t)0x40U;
+  while((MCG->S & 0x0CU) != 0x08U) {    /* Wait until external reference clock is selected as MCG output */
+  }
+  while((MCG->S & MCG_S_LOCK0_MASK) == 0x00U) { /* Wait until locked */
+  }
+  /* Switch to PEE Mode */
+  /* MCG->C1: CLKS=0,FRDIV=3,IREFS=0,IRCLKEN=1,IREFSTEN=0 */
+  MCG->C1 = (uint8_t)0x1AU;
+  while((MCG->S & 0x0CU) != 0x0CU) {    /* Wait until output of the PLL is selected */
+  }
+#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,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,EREFSTEN=0,SC2P=1,SC4P=0,SC8P=0,SC16P=1 */
+  OSC0->CR = (uint8_t)0x89U;
+  /* 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: 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;
+  while((MCG->S & MCG_S_IREFST_MASK) != 0x00U) { /* Check that the source of the FLL reference clock is the external reference clock. */
+  }
+  while((MCG->S & 0x0CU) != 0x08U) {    /* Wait until external reference clock is selected as MCG output */
+  }
+  /* Switch to BLPE Mode */
+  /* 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, 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,RANGE0=0,HGO0=0,EREFS0=1,LP=0,IRCS=0 */
+  MCG->C2 = (MCG_C2_RANGE0(0x00) | MCG_C2_EREFS0_MASK);
+  /* 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);
+  /* MCG->C4: DMX32=1,DRST_DRS=1 */
+  MCG->C4 = (uint8_t)((MCG->C4 & (uint8_t)~(uint8_t)(
+            MCG_C4_DRST_DRS(0x02)
+           )) | (uint8_t)(
+            MCG_C4_DMX32_MASK |
+            MCG_C4_DRST_DRS(0x01)
+           ));
+  while((MCG->S & MCG_S_IREFST_MASK) != 0x00U) { /* Check that the source of the FLL reference clock is the external reference clock. */
+  }
+  while((MCG->S & 0x0CU) != 0x00U) {    /* Wait until output of the FLL is selected */
+  }          
+#endif /* (CLOCK_SETUP == 3) */
+}
diff -r de96c8c234b6 -r c6c88a1a58a8 main.cpp
--- a/main.cpp	Thu Mar 08 14:41:22 2018 +0000
+++ b/main.cpp	Fri Mar 09 15:19:12 2018 +0000
@@ -1,46 +1,99 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "wdt.h"
 
-// LAB 3 SAMPLE PROGRAM 1
-//   Revised for mbed 5
+// Sample program using the Watchdog
+// ---------------------------------
+//    * Three threads co-operate to flash two LEDs
+//    * A simple way to inject a fault, by pressing a button
+//    * The watchdog is configured with a 32ms timeout
 
-#include "mbed.h"
+#define ON 1
+#define OFF 0
+DigitalOut led_red(LED_RED, ON);
+DigitalIn button(PTD0, PullUp);
+DigitalOut led1(PTC12, OFF);
+DigitalOut led2(PTC13, OFF);
+
+AnalogIn ain1(A0) ; // Analog input
+AnalogIn ain2(A1) ; // Analog input
+Serial pc(USBTX, USBRX); // tx, rx, for debugging
 
 
-Ticker tick;                // Ticker for reading analog
-AnalogIn ain(A0) ;          // Analog input
-DigitalOut led1(LED_RED);   // Red LED
+// This ticker is used to feed the watch dog
+Ticker tick;
+
+// Threads
+Thread threadT(osPriorityNormal, 2000) ; // timer thread
+Thread threadLED1(osPriorityNormal, 2000) ; // thread LED1
+Thread threadLED2(osPriorityNormal, 2000) ; // thread LED2
 
-Serial pc(USBTX, USBRX); // tx, rx, for debugging
+// ------------Fault Injection Button-------------
+//  Wait while the button is down
+//     Use this to simulate a STUCK fault
+// -----------------------------------------------
+void waitButton()
+{
+    while (!button) ;
+}
 
 // Message type
 typedef struct {
-  uint16_t analog; /* Analog input value */
+    uint16_t analog; /* Analog input value */
 } message_t;
 
 // Mail box
-Mail<message_t, 2> mailbox;
+Mail<message_t, 2> mailbox_led1;
 
 // Function called every 10ms to read ADC
-// Low pass filter  
+// Low pass filter
 // Every 10th value is sent to mailbox
-volatile int samples = 0 ;
-volatile uint16_t smoothed = 0 ; 
-void readA0() {
-    smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; // divided by 2 - reduce the signal noise
-    samples++ ;
-    if (samples == 10) {
+volatile int samples_led1 = 0 ;
+volatile uint16_t smoothed_led1 = 0 ;
+void readA0()
+{
+    smoothed_led1 = (smoothed_led1 >> 1) + (ain1.read_u16() >> 1) ; // divided by 2 - reduce the signal noise
+    samples_led1++ ;
+    if (samples_led1 == 10) {
         // send to thread
-        message_t *mess = mailbox.alloc() ; // may fail but does not block
+        message_t *mess = mailbox_led1.alloc() ; // may fail but does not block
         if (mess) {
-            mess->analog = smoothed ;
-            mailbox.put(mess); // fails but does not block if full
+            mess->analog = smoothed_led1;
+            mailbox_led1.put(mess); // fails but does not block if full
         }
-        samples = 0;
-    }       
+        samples_led1 = 0;
+    }
 }
 
+// Mail box
+Mail<message_t, 2> mailbox_led2;
+
+// Function called every 10ms to read ADC
+// Low pass filter
+// Every 10th value is sent to mailbox
+volatile int samples_led2 = 0 ;
+volatile uint16_t smoothed_led2 = 0 ;
+void readA1()
+{
+    smoothed_led2 = (smoothed_led2 >> 1) + (ain2.read_u16() >> 1) ; // divided by 2 - reduce the signal noise
+    samples_led1++ ;
+    if (samples_led2 == 10) {
+        // send to thread
+        message_t *mess = mailbox_led2.alloc() ; // may fail but does not block
+        if (mess) {
+            mess->analog = smoothed_led2;
+            mailbox_led2.put(mess); // fails but does not block if full
+        }
+        samples_led2 = 0;
+    }
+}
+
+
+
 // Write voltage digits
 //   v  Voltage as scale int, e.g. 3.30 is 330
-void vToString(int v, char* s) {    
+void vToString(int v, char* s)
+{
     s[3] = '0' + (v % 10) ;
     v = v / 10 ;
     s[2] = '0' + (v % 10) ;
@@ -48,36 +101,140 @@
     s[0] = '0' + (v % 10) ;
 }
 
-// Main program
-//   Initialise variables
-//   Attach ISR for ticker
-//   Procss messages from mailbox    
-int main() {
-    led1 = 1 ; // turn off 
+// ---Thread for controlling LED 1----------------
+//   Turn LED1 on/off in response to signals
+// -----------------------------------------------
+void led1_thread()    // method to run in thread
+{
+    osEvent evt ;
+    while (true) {
+        evt = Thread::signal_wait(0x0); // wait for any signal
+        if (evt.status == osEventSignal) {
+            if (evt.value.signals & 0x01) led1 = ON ;
+            if (evt.value.signals & 0x02) led1 = OFF ;
+        }
+        waitButton() ;  // POSSIBLE FAULT HERE
+        wdt_kickA();
+    }
+}
+
+// ---Thread for controlling LED 2----------------
+//   Turn LED2 on/off in response to signals
+// -----------------------------------------------
+void led2_thread()    // method to run in thread
+{
+    osEvent evt ;
+    while (true) {
+        evt = Thread::signal_wait(0x0); // wait for any signal
+        if (evt.status == osEventSignal) {
+            if (evt.value.signals & 0x01) led2 = ON ;
+            if (evt.value.signals & 0x02) led2 = OFF ;
+        }
+        //waitButton() ; // POSSIBLE FAULT HERE
+        wdt_kickB();
+    }
+}
+
+// ---Thread for timing --------------------------
+//   Send signals to the other threads
+// -----------------------------------------------
+void timer_thread()    // method to run in thread
+{
+    while (true) {
+        Thread::wait(250) ;
+        threadLED1.signal_set(0x1) ;
+        threadLED2.signal_set(0x1) ;
+        Thread::wait(250) ;
+        threadLED1.signal_set(0x2) ;
+        threadLED2.signal_set(0x2) ;
+        //waitButton() ; // POSSIBLE FAULT HERE
+    }
+}
+
+// -----------MAIN-------------------------------
+//    Configure watchdog. Start threads.
+//    Show start up with RED for 1sec
+//    Remember the watchdog is running
+//       - 1024ms to set it once
+//       - then must feed it every 32ms
+// ----------------------------------------------
+
+int main(void)
+{
+    wdt_1sec() ; // initialise watchdog - 32ms timeout
+    //tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms
+
+    // start threads
+    threadT.start(&timer_thread) ; // start the timer thread
+    threadLED1.start(&led1_thread) ; // start the LED1 control thread
+    threadLED2.start(&led2_thread) ; // start the LED2 control thread
+
     int volts = 0 ;
     const int threshold = 100 ; // 1 vol
-    int counter = 0 ;
+    int counter1 = 0 ;
+    int counter2 = 0 ;
     char vstring[] = "X.XX\r\n" ;
 
     tick.attach_us(callback(&readA0), 10000); // ticks every 10ms -> 10000 micro second
+//    tick.attach_us(callback(&readA1), 10000); // ticks every 10ms -> 10000 micro second
+
+    // show start-up
+    led_red = OFF;
+    Thread::wait(1000) ;
+    led_red = ON;
+
     while (true) {
-        osEvent evt = mailbox.get(); // wait for mail 
-        
+
+        // LED1 process
+        osEvent evt1 = mailbox_led1.get(); // wait for mail
         // every 100 ms this loop operates
-        if (evt.status == osEventMail) {
-            message_t* mess = (message_t*)evt.value.p ;
+        if (evt1.status == osEventMail) {
+            message_t* mess = (message_t*)evt1.value.p ;
             volts = (mess->analog * 330) / 0xffff ; // 2 ^ 16
-            mailbox.free(mess) ;  // free the message space
-            if (volts > threshold) led1 = 0 ; else led1 = 1 ;
+            mailbox_led1.free(mess) ;  // free the message space
+            counter1++;
+
+
+
             vToString(volts, vstring) ;
-            counter++ ;
-            
             // every 1 s this loop will operate
-            if (counter == 10) {  // limit bandwidth of serial
+            if (counter1 == 10) {  // limit bandwidth of serial
                 pc.printf(vstring) ;
-                counter = 0 ;
+                 if(volts >= 150-threshold && volts <= 150+threshold) {
+                    pc.printf("LED1 - Nomral \r\n\r\n") ;
+                } else if (volts == 0) {
+                    pc.printf("LED1 - open circuit \r\n\r\n") ;
+                } else if(volts > 300) {
+                    pc.printf("LED1 - short circuit \r\n\r\n") ;
+                }
+                counter1 = 0 ;
             }
         }
+        
+        // LED2 process
+        if(false) {
+        osEvent evt2 = mailbox_led2.get(); // wait for mail
+        // every 100 ms this loop operates
+        if (evt2.status == osEventMail) {
+            message_t* mess = (message_t*)evt2.value.p ;
+            volts = (mess->analog * 330) / 0xffff ; // 2 ^ 16
+            mailbox_led1.free(mess) ;  // free the message space
+            vToString(volts, vstring) ;
+            counter2++;
 
+            // every 1 s this loop will operate
+            if (counter2 == 10) {  // limit bandwidth of serial
+                pc.printf(vstring) ;
+                 if(volts >= 150-threshold && volts <= 150+threshold) {
+                    pc.printf("LED2 - Nomral \r\n\r\n") ;
+                } else if (volts == 0) {
+                    pc.printf("LED2 - open circuit \r\n\r\n" ) ;
+                } else if(volts > 300) {
+                    pc.printf("LED2 - short circuit \r\n\r\n") ;
+                }
+                counter2 = 0 ;
+            }
+        }
+        }
     }
-}
+}
\ No newline at end of file
diff -r de96c8c234b6 -r c6c88a1a58a8 mbed_app.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Fri Mar 09 15:19:12 2018 +0000
@@ -0,0 +1,7 @@
+{
+    "config": {
+        "main-stack-size": {
+            "value": 2000
+        }
+    }
+}
\ No newline at end of file
diff -r de96c8c234b6 -r c6c88a1a58a8 wdt.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wdt.cpp	Fri Mar 09 15:19:12 2018 +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;
+}
+
+
diff -r de96c8c234b6 -r c6c88a1a58a8 wdt.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wdt.h	Fri Mar 09 15:19:12 2018 +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