Final (not completed)
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "wdt.h" 00003 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 00010 #define ON 1 00011 #define OFF 0 00012 DigitalOut led_red(LED_RED, ON); 00013 DigitalIn button(PTD0, PullUp); 00014 DigitalOut led1(PTC12, OFF); 00015 DigitalOut led2(PTC13, OFF); 00016 00017 Serial pc(USBTX, USBRX); // tx, rx, useful for debugging 00018 00019 // This ticker is used to feed the watch dog 00020 //Ticker tick; 00021 00022 // Threads 00023 Thread threadT ; // timer thread 00024 Thread threadLED1 ; // thread LED1 00025 Thread threadLED2 ; // thread LED2 00026 00027 # define ON1 0x01 00028 # define ON2 0x02 00029 # define OFF1 0x04 00030 # define OFF2 0x08 00031 # define firstthreadhappy 0x16 00032 # define secondthreadhappy 0x32 00033 # define thirdthreadhappy 0x64 00034 EventFlags signals; // event flags for signalling 00035 00036 00037 // ------------Fault Injection Button------------- 00038 // Wait while the button is down 00039 // Use this to simulate a STUCK fault 00040 // ----------------------------------------------- 00041 void waitButton() { 00042 while (!button) ; 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 int evt ; 00050 while (true) { 00051 evt = signals.wait_any(ON1 | OFF1); // wait for either signal 00052 if (evt & ON1) led1 = ON ; 00053 if (evt & OFF1) led1 = OFF ; 00054 signals.set(firstthreadhappy); 00055 //waitButton() ; // POSSIBLE FAULT HERE 00056 00057 } 00058 } 00059 00060 // ---Thread for controlling LED 2---------------- 00061 // Turn LED2 on/off in response to signals 00062 // ----------------------------------------------- 00063 void led2_thread() { // method to run in thread 00064 int evt ; 00065 while (true) { 00066 evt = signals.wait_any(ON2 | OFF2); // wait for any signal 00067 if (evt & ON2) led2 = ON ; 00068 if (evt & OFF2) led2 = OFF ; 00069 signals.set(secondthreadhappy); 00070 // waitButton() ; // POSSIBLE FAULT HERE 00071 00072 } 00073 } 00074 00075 // ---Thread for timing -------------------------- 00076 // Send signals to the other threads 00077 // ----------------------------------------------- 00078 void timer_thread() { // method to run in thread 00079 //int evt; 00080 while (true) { 00081 wait(0.8) ; 00082 signals.set(ON1 | ON2) ; 00083 signals.set(thirdthreadhappy); 00084 wait(0.8) ; 00085 signals.set(OFF1 | OFF2) ; 00086 signals.set(thirdthreadhappy); 00087 // waitButton() ; // POSSIBLE FAULT HERE 00088 } 00089 } 00090 00091 // -----------MAIN------------------------------- 00092 // Configure watchdog. Start threads. 00093 // Show start up with RED for 1sec 00094 // Remember the watchdog is running 00095 // - 1024ms to set it once 00096 // - then must feed it every 32ms 00097 // ---------------------------------------------- 00098 00099 int main(void) { 00100 wdt_1sec() ; // initialise watchdog - 1s timeout 00101 wdt_kick_all() ; 00102 //tick.attach_us(callback(&wdt_kick_all), 20000); 00103 int evt; 00104 int first; 00105 int second; 00106 int third; 00107 // start threads 00108 threadT.start(timer_thread) ; // start the timer thread 00109 threadLED1.start(led1_thread) ; // start the LED1 control thread 00110 threadLED2.start(led2_thread) ; // start the LED2 control thread 00111 00112 led_red = OFF; 00113 wait(0.5) ; 00114 led_red = ON; 00115 00116 while(1){ 00117 evt = signals.wait_any(firstthreadhappy | secondthreadhappy | thirdthreadhappy); 00118 if(evt & firstthreadhappy) first = 1; 00119 if(evt & secondthreadhappy) second = 1; 00120 if(evt & thirdthreadhappy) third = 1; 00121 00122 if(first + second + third == 3) { 00123 wdt_kick_all() ; 00124 first = 0; 00125 second = 0; 00126 third = 0; 00127 } 00128 00129 } 00130 //if(firstthreadhappy) callback(&wdt_kick_all); // ticks every 20ms 00131 00132 // show start-up 00133 00134 // main thread terminates 00135 00136 00137 }
Generated on Mon Aug 15 2022 04:00:49 by
1.7.2