Lab 6 Part 2

Fork of Watchdog_sample_nocoverage by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
AnastasiosBarlas
Date:
Fri Mar 09 20:27:59 2018 +0000
Parent:
5:9817756efc34
Commit message:
Lab 6 Part 2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Feb 27 10:29:30 2018 +0000
+++ b/main.cpp	Fri Mar 09 20:27:59 2018 +0000
@@ -1,36 +1,46 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "wdt.h"
-
 // 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
-
 #define ON 1
 #define OFF 0
 DigitalOut led_red(LED_RED, ON);
 DigitalIn button(PTD0, PullUp);
 DigitalOut led1(PTC12, OFF);
 DigitalOut led2(PTC13, OFF);
+Serial pc(USBTX, USBRX);
 
 // This ticker is used to feed the watch dog
 Ticker tick;
+// Threads
 
-// Threads
-Thread threadT ; // timer thread 
-Thread threadLED1 ; // thread LED1
-Thread threadLED2 ; // thread LED2
+Thread threadT(osPriorityNormal,1000); // timer thread 
+Thread threadLED1(osPriorityNormal,1000); // thread LED1
+//Thread threadLED2(osPriorityNormal,1000); // thread LED2
+Thread ThreadDetect(osPriorityNormal,1000);
+
+//define the state of watch dog
+enum wState {Working, Stopped} ;
+wState watchdogflag;
+AnalogIn ainled1(A0); //LED1 ADC INPUT
+//AnalogIn ainled2(A1); //LED2 ADC INPUT
+float volt1,volt2;
 
 // ------------Fault Injection Button-------------
 //  Wait while the button is down
 //     Use this to simulate a STUCK fault
 // -----------------------------------------------
 void waitButton() {
-    while (!button) ;  
+   
+    while (!button){
+        pc.printf("stop watchdog");
+        watchdogflag=Stopped;
+        }
 }
-
 // ---Thread for controlling LED 1----------------
 //   Turn LED1 on/off in response to signals 
 // -----------------------------------------------
@@ -39,13 +49,15 @@
     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 & 0x01) 
+            {
+                led1 = ON ;
+                
+            }
             if (evt.value.signals & 0x02) led1 = OFF ;
         } 
-        waitButton() ;  // POSSIBLE FAULT HERE
     }
 }
-
 // ---Thread for controlling LED 2----------------
 //   Turn LED2 on/off in response to signals 
 // -----------------------------------------------
@@ -54,28 +66,66 @@
     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 & 0x01) 
+            {
+               led2 = ON ;       
+            }
             if (evt.value.signals & 0x02) led2 = OFF ;
         } 
         // waitButton() ; // POSSIBLE FAULT HERE
     }
 }
-
 // ---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) ;
+        threadLED1.signal_set(0x1) ;//on
+ //       threadLED2.signal_set(0x1) ;//on
         Thread::wait(250) ;
-        threadLED1.signal_set(0x2) ;
-        threadLED2.signal_set(0x2) ;
+        threadLED1.signal_set(0x2) ;//off
+ //       threadLED2.signal_set(0x2) ;//off
         // waitButton() ; // POSSIBLE FAULT HERE
     }
 }
-
+void detection_thread() 
+{ 
+    int counter=0;
+    while (1) 
+    {
+        volt1 = ainled1;
+     //          pc.printf("%f\n\r",volt1);
+        if (volt1>0.90) 
+        {
+            pc.printf("short circuit\n\r");
+            led_red=0;
+        }
+        else
+        {
+           if(led1==1)
+           {
+                if ((volt1<0.1))        
+                {    
+                    counter++;
+                    if (counter>5)
+                    {
+                         pc.printf("open circuit\n\r");
+                        if(led_red==1)led_red=0;
+                        counter =0;
+                    }
+                }
+                else
+                {
+                    pc.printf("%f\n\r",volt1);
+                    if (led_red==0)led_red=1;
+                } 
+            }
+        }
+ 
+       waitButton(); // POSSIBLE FAULT HERE     
+    }     
+}
 // -----------MAIN-------------------------------
 //    Configure watchdog. Start threads. 
 //    Show start up with RED for 1sec
@@ -83,18 +133,29 @@
 //       - 1024ms to set it once
 //       - then must feed it every 32ms  
 // ----------------------------------------------
+void watchdog()
+{
+    
+    if (watchdogflag==Working)
+        {
+           wdt_kick_all(); 
+        } 
+}
 
 int main(void) {
+    
     wdt_32ms() ; // initialise watchdog - 32ms timeout
-    tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms    
+    watchdogflag = Working;
+    tick.attach_us(callback(&watchdog), 20000); 
     
     // 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 
+    ThreadDetect.start(&detection_thread);
     
     // show start-up
     led_red = OFF;
-    Thread::wait(1000) ;
+    Thread::wait(1000) ;//1ms
     led_red = ON;
+   
 }
\ No newline at end of file