Sebastian Barrera / Mbed OS Lab6
Revision:
9:5d1d36c2664f
Parent:
8:041c4841cd96
Child:
10:0edd1ab03f84
--- a/main.cpp	Thu Mar 26 09:54:53 2020 +0000
+++ b/main.cpp	Thu Mar 26 11:02:19 2020 +0000
@@ -13,6 +13,7 @@
 DigitalIn button(PTD0, PullUp);
 DigitalOut led1(PTC12, OFF);
 DigitalOut led2(PTC13, OFF);
+DigitalOut redled(LED1,ON);
 AnalogIn ain(PTB0) ; 
 AnalogIn anin(PTE20) ;
 Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
@@ -22,12 +23,14 @@
 Thread threadT(osPriorityNormal, 2000); ; // timer thread 
 Thread threadLED1(osPriorityNormal, 2000); ; // thread LED1
 Thread threadLED2(osPriorityNormal, 2000); ; // thread LED2
+Thread threadkick(osPriorityNormal, 2000); ; // thread LED2
 
 # define ON1 (1UL << 0)   
 # define ON2 (1UL << 1)
 # define OFF1 (1UL << 2)  
 # define OFF2 (1UL << 3)
-#define error (1UL << 4)
+# define kick1 (1UL << 4)  
+# define kick2 (1UL << 5)
 EventFlags signals;  // event flags for signalling
 
 
@@ -39,17 +42,23 @@
     while (!button) ;  
 }
 
+void kickthread(){
+    while(1){
+        signals.wait_all(kick1, kick2);
+        wdt_kickA();//kick watchdog channel A   
+    }   
+}
+
 // ---Thread for controlling LED 1----------------
 //   Turn LED1 on/off in response to signals 
 // -----------------------------------------------
 void led1_thread() {  // method to run in thread
     int evt ;
     while (true) {
-        wdt_kickA();//kick watchdog channel A
-        evt = signals.wait_any(ON1 | OFF1 | error); // wait for either signal
+        evt = signals.wait_any(ON1 | OFF1); // wait for either signal
         if (evt & ON1) led1 = ON ;
         if (evt & OFF1) led1 = OFF ;
-        if (evt & error) {while(1){}}}//If hardware error detected, wait until watchdog resets the system
+        signals.set(kick1);
         //waitButton() ;  // POSSIBLE FAULT HERE
     }
 }
@@ -60,11 +69,10 @@
 void led2_thread() {  // method to run in thread
     int evt ;
     while (true) {
-        wdt_kickB();//Kick watchdog channel B
-        evt = signals.wait_any(ON2 | OFF2 | error); // wait for any signal
+        evt = signals.wait_any(ON2 | OFF2); // wait for any signal
         if (evt & ON2) led2 = ON ;
         if (evt & OFF2) led2 = OFF ;
-        if (evt & error) {while(1){}}//If hardware error detected, wait until watchdog resets the system
+        signals.set(kick2);
         // waitButton() ; // POSSIBLE FAULT HERE
     }
 }
@@ -101,17 +109,19 @@
     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 
-    
+    threadkick.start(kickthread); // start the thread to kick watchdog
     //printf("\nRESTART\n");// Debugging
     while (true) {
         if (led1==ON || led2==ON){//Only check when LEDs are on
+            wdt_kickB();//Kick watchdog channel B
             volts=ain.read();
             volts=volts*3.3;
             volts2=anin.read()*3.3;
             //Voltage with LEDs on is around 1.2V, since the resistances available were not of 680ohms.
-            if (volts>1.5 || volts2>1.5){signals.set(error);}//Set a signal to detect fault
+            if (volts>1.5 || volts2>1.5){redled=0;}//Set a signal to detect fault
         }
         //printf("volts: %f volts 2: %f", volts, volts2);//Debugging
+        volts=0; volts2=0;
         wait(0.1f);
     }