lab6-1

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 
00005 // Sample program using the Watchdog
00006 // ---------------------------------
00007 //    * Three threads co-operate to flash two LEDs
00008 //    * A simple way to inject a fault, by pressing a button 
00009 //    * The watchdog is configured with a 32ms timeout
00010 
00011 #define ON 1
00012 #define OFF 0
00013 DigitalOut led_red(LED_RED, ON);
00014 DigitalIn button(PTD0, PullUp);
00015 DigitalOut led1(PTC12, OFF);
00016 DigitalOut led2(PTC13, OFF);
00017 Serial pc(USBTX, USBRX); 
00018 
00019 // This ticker is used to feed the watch dog
00020 Ticker tick;
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 
00027 //define the state of watch dog
00028 enum wState {Working, Stopped} ;
00029 wState watchdogflag;
00030 int led1counter1=0;
00031 int led1counter2=0;
00032 int led2counter1=0;
00033 int led2counter2=0;
00034 
00035 // ------------Fault Injection Button-------------
00036 //  Wait while the button is down
00037 //     Use this to simulate a STUCK fault
00038 // -----------------------------------------------
00039 void waitButton() {
00040     while (!button);
00041 }
00042 
00043 
00044 
00045 // ---Thread for controlling LED 1----------------
00046 //   Turn LED1 on/off in response to signals 
00047 // -----------------------------------------------
00048 void led1_thread() {  // method to run in thread
00049     osEvent evt ;
00050     
00051     while (true) {
00052      wdt_kickA();  
00053         evt = Thread::signal_wait(0x0); // wait for any signal
00054         if (evt.status == osEventSignal) {
00055             if (evt.value.signals & 0x01) led1 = ON ;
00056             if (evt.value.signals & 0x02) led1 = OFF ;
00057           
00058         } 
00059   
00060     
00061   //      waitButton() ;  // POSSIBLE FAULT HERE
00062     }
00063 }
00064 
00065 // ---Thread for controlling LED 2----------------
00066 //   Turn LED2 on/off in response to signals 
00067 // -----------------------------------------------
00068 void led2_thread() {  // method to run in thread
00069     osEvent evt ;
00070    
00071     
00072     while (true) {
00073        
00074         evt = Thread::signal_wait(0x0); // wait for any signal
00075         wdt_kickB();
00076         if (evt.status == osEventSignal) {
00077             if (evt.value.signals & 0x01) led2 = ON ;
00078             if (evt.value.signals & 0x02) led2 = OFF ;
00079            
00080         } 
00081     
00082     //     waitButton() ; // POSSIBLE FAULT HERE
00083     
00084     }
00085 }
00086 
00087 
00088 // ---Thread for timing --------------------------
00089 //   Send signals to the other threads
00090 // -----------------------------------------------
00091 void timer_thread() {  // method to run in thread
00092     while (true) {
00093         Thread::wait(250) ;
00094         threadLED1.signal_set(0x1) ;//on
00095         threadLED2.signal_set(0x1) ;//on
00096        
00097         Thread::wait(250) ;
00098         threadLED1.signal_set(0x2) ;//off
00099         threadLED2.signal_set(0x2) ;//off
00100         
00101          waitButton() ; // POSSIBLE FAULT HERE
00102     }
00103 }
00104 
00105 // -----------MAIN-------------------------------
00106 //    Configure watchdog. Start threads. 
00107 //    Show start up with RED for 1sec
00108 //    Remember the watchdog is running
00109 //       - 1024ms to set it once
00110 //       - then must feed it every 32ms  
00111 // ----------------------------------------------
00112 
00113 
00114 int main(void) {
00115     
00116     wdt_1sec() ; // initialise watchdog - 32ms timeout
00117     //tick.attach_us(callback(&wdt_kick_all), 20000);
00118     
00119     
00120     
00121     // start threads
00122     threadT.start(&timer_thread) ; // start the timer thread 
00123     threadLED1.start(&led1_thread) ; // start the LED1 control thread 
00124     threadLED2.start(&led2_thread) ; // start the LED2 control thread 
00125     
00126     led_red = OFF;
00127     Thread::wait(1000) ;
00128     led_red = ON;
00129     
00130  
00131 
00132     
00133 }