Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Watchdog_sample_nocoverage by
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 Thread threaddetection(osPriorityNormal,1000); 00027 00028 //define the state of watch dog 00029 enum wState {Working, Stopped} ; 00030 wState watchdogflag; 00031 AnalogIn ainled1(A0); //LED1 ADC INPUT 00032 //AnalogIn ainled2(A1); //LED2 ADC INPUT 00033 float volt1,volt2; 00034 00035 00036 // ------------Fault Injection Button------------- 00037 // Wait while the button is down 00038 // Use this to simulate a STUCK fault 00039 // ----------------------------------------------- 00040 void waitButton() { 00041 00042 while (!button){ 00043 pc.printf("stop watchdog"); 00044 watchdogflag=Stopped; 00045 } 00046 } 00047 00048 // ---Thread for controlling LED 1---------------- 00049 // Turn LED1 on/off in response to signals 00050 // ----------------------------------------------- 00051 void led1_thread() { // method to run in thread 00052 osEvent evt ; 00053 while (true) { 00054 evt = Thread::signal_wait(0x0); // wait for any signal 00055 if (evt.status == osEventSignal) { 00056 if (evt.value.signals & 0x01) 00057 { 00058 led1 = ON ; 00059 00060 } 00061 if (evt.value.signals & 0x02) led1 = OFF ; 00062 } 00063 } 00064 } 00065 00066 // ---Thread for controlling LED 2---------------- 00067 // Turn LED2 on/off in response to signals 00068 // ----------------------------------------------- 00069 void led2_thread() { // method to run in thread 00070 osEvent evt ; 00071 while (true) { 00072 evt = Thread::signal_wait(0x0); // wait for any signal 00073 if (evt.status == osEventSignal) { 00074 if (evt.value.signals & 0x01) 00075 { 00076 led2 = ON ; 00077 } 00078 if (evt.value.signals & 0x02) led2 = OFF ; 00079 } 00080 // waitButton() ; // POSSIBLE FAULT HERE 00081 } 00082 } 00083 00084 // ---Thread for timing -------------------------- 00085 // Send signals to the other threads 00086 // ----------------------------------------------- 00087 void timer_thread() { // method to run in thread 00088 while (true) { 00089 Thread::wait(250) ; 00090 threadLED1.signal_set(0x1) ;//on 00091 // threadLED2.signal_set(0x1) ;//on 00092 Thread::wait(250) ; 00093 threadLED1.signal_set(0x2) ;//off 00094 // threadLED2.signal_set(0x2) ;//off 00095 // waitButton() ; // POSSIBLE FAULT HERE 00096 } 00097 } 00098 00099 void detection_thread() 00100 { 00101 int counter=0; 00102 while (1) 00103 { 00104 volt1 = ainled1; 00105 // pc.printf("%f\n\r",volt1); 00106 if (volt1>0.90) 00107 { 00108 pc.printf("short circuit\n\r"); 00109 led_red=0; 00110 } 00111 else 00112 { 00113 if(led1==1) 00114 { 00115 if ((volt1<0.1)) 00116 { 00117 counter++; 00118 if (counter>5) 00119 { 00120 pc.printf("open circuit\n\r"); 00121 if(led_red==1)led_red=0; 00122 counter =0; 00123 } 00124 } 00125 else 00126 { 00127 pc.printf("%f\n\r",volt1); 00128 if (led_red==0)led_red=1; 00129 } 00130 } 00131 } 00132 00133 waitButton(); // POSSIBLE FAULT HERE 00134 } 00135 } 00136 00137 // -----------MAIN------------------------------- 00138 // Configure watchdog. Start threads. 00139 // Show start up with RED for 1sec 00140 // Remember the watchdog is running 00141 // - 1024ms to set it once 00142 // - then must feed it every 32ms 00143 // ---------------------------------------------- 00144 00145 void watchdog() 00146 { 00147 00148 if (watchdogflag==Working) 00149 { 00150 wdt_kick_all(); 00151 } 00152 } 00153 00154 00155 int main(void) { 00156 00157 wdt_32ms() ; // initialise watchdog - 32ms timeout 00158 watchdogflag = Working; 00159 tick.attach_us(callback(&watchdog), 20000); 00160 00161 // start threads 00162 threadT.start(&timer_thread) ; // start the timer thread 00163 threadLED1.start(&led1_thread) ; // start the LED1 control thread 00164 threaddetection.start(&detection_thread); 00165 00166 // show start-up 00167 led_red = OFF; 00168 Thread::wait(1000) ;//1ms 00169 led_red = ON; 00170 00171 }
Generated on Tue Jul 26 2022 18:57:29 by
