Lab6 code
Fork of Watchdog_sample_nocoverage by
Embed:
(wiki syntax)
Show/hide line numbers
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 00018 // This ticker is used to feed the watch dog 00019 Ticker tick; 00020 00021 // Threads 00022 Thread threadT(osPriorityNormal, 1000); // timer thread 00023 Thread threadLED1(osPriorityNormal, 1000); // thread LED1 00024 Thread threadLED2(osPriorityNormal, 1000); // thread LED2 00025 00026 // ------------Fault Injection Button------------- 00027 // Wait while the button is down 00028 // Use this to simulate a STUCK fault 00029 // ----------------------------------------------- 00030 void waitButton() { 00031 while (!button) ; 00032 } 00033 00034 // ---Thread for controlling LED 1---------------- 00035 // Turn LED1 on/off in response to signals 00036 // ----------------------------------------------- 00037 void led1_thread() { // method to run in thread 00038 osEvent evt ; 00039 while (true) { 00040 evt = Thread::signal_wait(0x0); // wait for any signal 00041 if (evt.status == osEventSignal) { 00042 if (evt.value.signals & 0x01) led1 = ON ; 00043 if (evt.value.signals & 0x02) led1 = OFF ; 00044 00045 wdt_kickA(); 00046 } 00047 00048 //waitButton() ; // POSSIBLE FAULT HERE 00049 } 00050 } 00051 00052 // ---Thread for controlling LED 2---------------- 00053 // Turn LED2 on/off in response to signals 00054 // ----------------------------------------------- 00055 void led2_thread() { // method to run in thread 00056 osEvent evt ; 00057 while (true) { 00058 evt = Thread::signal_wait(0x0); // wait for any signal 00059 if (evt.status == osEventSignal) { 00060 if (evt.value.signals & 0x01) led2 = ON ; 00061 if (evt.value.signals & 0x02) led2 = OFF ; 00062 00063 wdt_kickB(); 00064 } 00065 //waitButton() ; // POSSIBLE FAULT HERE 00066 } 00067 } 00068 00069 // ---Thread for timing -------------------------- 00070 // Send signals to the other threads 00071 // ----------------------------------------------- 00072 void timer_thread() { // method to run in thread 00073 while (true) { 00074 Thread::wait(250) ; 00075 threadLED1.signal_set(0x1) ; 00076 threadLED2.signal_set(0x1) ; 00077 Thread::wait(250) ; 00078 threadLED1.signal_set(0x2) ; 00079 threadLED2.signal_set(0x2) ; 00080 00081 00082 waitButton() ; // POSSIBLE FAULT HERE 00083 } 00084 } 00085 00086 // -----------MAIN------------------------------- 00087 // Configure watchdog. Start threads. 00088 // Show start up with RED for 1sec 00089 // Remember the watchdog is running 00090 // - 1024ms to set it once 00091 // - then must feed it every 32ms 00092 // ---------------------------------------------- 00093 00094 int main(void) { 00095 wdt_1sec() ; // initialise watchdog - 32ms timeout 00096 //tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms 00097 00098 00099 // start threads 00100 threadT.start(&timer_thread) ; // start the timer thread 00101 threadLED1.start(&led1_thread) ; // start the LED1 control thread 00102 threadLED2.start(&led2_thread) ; // start the LED2 control thread 00103 00104 // show start-up 00105 led_red = OFF; 00106 Thread::wait(1000) ; 00107 led_red = ON; 00108 }
Generated on Mon Jul 25 2022 13:58:36 by
1.7.2
