Anastasios Barlas / Mbed OS Watchdog_sample_nocoverage

Fork of Watchdog_sample_nocoverage by William Marsh

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "wdt.h"
00004 // Sample program using the Watchdog
00005 // ---------------------------------
00006 //    * Three threads co-operate to flash two LEDs
00007 //    * A simple way to inject a fault, by pressing a button 
00008 //    * The watchdog is configured with a 32ms timeout
00009 #define ON 1
00010 #define OFF 0
00011 DigitalOut led_red(LED_RED, ON);
00012 DigitalIn button(PTD0, PullUp);
00013 DigitalOut led1(PTC12, OFF);
00014 DigitalOut led2(PTC13, OFF);
00015 Serial pc(USBTX, USBRX);
00016 
00017 
00018 // This ticker is used to feed the watch dog
00019 Ticker tick;
00020 
00021 
00022 // Threads
00023 Thread threadT(osPriorityNormal,1000); // timer thread 
00024 Thread threadLED1(osPriorityNormal,1000); // thread LED1
00025 Thread threadLED2(osPriorityNormal,1000); // thread LED2
00026 Thread threaddetection(osPriorityNormal,1000);
00027 
00028 
00029 //define the state of watch dog
00030 enum wState {Working, Stopped} ;
00031 wState watchdogflag;
00032 
00033 
00034 AnalogIn ainled1(A0); //LED1 ADC INPUT
00035 //AnalogIn ainled2(A1); //LED2 ADC INPUT
00036 float volt1,volt2;
00037 
00038 // ------------Fault Injection Button-------------
00039 //  Wait while the button is down
00040 //     Use this to simulate a STUCK fault
00041 // -----------------------------------------------
00042 void waitButton() {
00043    
00044     while (!button); /*{
00045         pc.printf("stop watchdog");
00046         watchdogflag=Stopped;
00047         } */
00048 }
00049 
00050 
00051 
00052 
00053 // ---Thread for controlling LED 1----------------
00054 //   Turn LED1 on/off in response to signals 
00055 // -----------------------------------------------
00056 void led1_thread() {  // method to run in thread
00057     osEvent evt ;
00058     while (true) {
00059         evt = Thread::signal_wait(0x0); // wait for any signal
00060         if (evt.status == osEventSignal) {
00061             if (evt.value.signals & 0x01) 
00062             {
00063                 led1 = ON ;
00064                 
00065             }
00066             wdt_kickB();
00067             if (evt.value.signals & 0x02) led1 = OFF ;
00068         
00069         } 
00070         //waitButton() ;
00071     }
00072 }
00073 
00074 
00075 // ---Thread for controlling LED 2----------------
00076 //   Turn LED2 on/off in response to signals 
00077 // -----------------------------------------------
00078 void led2_thread() {  // method to run in thread
00079     osEvent evt ;
00080     while (true) {
00081         evt = Thread::signal_wait(0x0); // wait for any signal
00082         if (evt.status == osEventSignal) {
00083             if (evt.value.signals & 0x01) 
00084             {
00085                led2 = ON ;       
00086             }
00087             if (evt.value.signals & 0x02) led2 = OFF ;
00088         } 
00089         wdt_kickA();
00090         // waitButton() ; // POSSIBLE FAULT HERE
00091     }
00092 }
00093 
00094 
00095 // ---Thread for timing --------------------------
00096 //   Send signals to the other threads
00097 // -----------------------------------------------
00098 void timer_thread() {  // method to run in thread
00099     while (true) {
00100         Thread::wait(250) ;
00101         threadLED1.signal_set(0x1) ;//on
00102         threadLED2.signal_set(0x1) ;//on
00103         Thread::wait(250) ;
00104         threadLED1.signal_set(0x2) ;//off
00105         threadLED2.signal_set(0x2) ;//off
00106          waitButton() ; // POSSIBLE FAULT HERE
00107     }
00108 }
00109 
00110 
00111 
00112 //void Detec_Thread() 
00113 //{ 
00114 //    int counter=0;
00115 //    while (1) 
00116 //    {
00117 //        volt1 = ainled1;
00118 //     //          pc.printf("%f\n\r",volt1);
00119 //        if (volt1>0.90) 
00120 //        {
00121 //            pc.printf("short circuit\n\r");
00122 //            led_red=0;
00123 //        }
00124 //        else
00125 //        {
00126 //           if(led1==1)
00127 //           {
00128 //                if ((volt1<0.1))        
00129 //                {    
00130 //                    counter++;
00131 //                    if (counter>5)
00132 //                    {
00133 //                         pc.printf("open circuit\n\r");
00134 //                        if(led_red==1)led_red=0;
00135 //                        counter =0;
00136 //                    }
00137 //                }
00138 //                else
00139 //                {
00140 //                    pc.printf("%f\n\r",volt1);
00141 //                    if (led_red==0)led_red=1;
00142 //                } 
00143 //            }
00144 //        }
00145 // 
00146 //       waitButton(); // POSSIBLE FAULT HERE     
00147 //    }     
00148 //}
00149 
00150  
00151 // -----------MAIN-------------------------------
00152 //    Configure watchdog. Start threads. 
00153 //    Show start up with RED for 1sec
00154 //    Remember the watchdog is running
00155 //       - 1024ms to set it once
00156 //       - then must feed it every 32ms  
00157 // ----------------------------------------------
00158 
00159 
00160 void watchdog()
00161 {
00162     
00163     if (watchdogflag==Working)
00164         {
00165            wdt_kick_all(); 
00166         } 
00167 }
00168 
00169 int main(void) {
00170     
00171     wdt_1sec() ; // initialise watchdog - 32ms timeout
00172  //   watchdogflag = Working;
00173     //tick.attach_us(callback(&watchdog), 20000); 
00174     
00175     // start threads
00176     threadT.start(&timer_thread) ; // start the timer thread 
00177     threadLED1.start(&led1_thread) ; // start the LED1 control thread 
00178     threadLED2.start(&led2_thread) ; // start the LED1 control thread 
00179     threaddetection.start(&Detec_Thread);
00180     
00181     // show start-up
00182     led_red = OFF;
00183     Thread::wait(1000) ;//1ms
00184     led_red = ON;
00185    
00186 }