Tobore Denedo / Mbed OS Watchdog_Hardware_Failure_Detection

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 DigitalOut led_blue(LED_BLUE, ON);
00013 DigitalOut led_green(LED_GREEN, ON);
00014 DigitalIn button(PTD0, PullUp);
00015 DigitalOut led1(PTC12, OFF);
00016 DigitalOut led2(PTC13, OFF);
00017 Serial pc(USBTX, USBRX);
00018 // This ticker is used to feed the watch dog
00019 Ticker tick;
00020 // Threads
00021 Thread threadT; // timer thread 
00022 Thread threadLED1; // thread LED1
00023 Thread circuitfailure;
00024 AnalogIn ain(A0);
00025 
00026 float volts;
00027 
00028 // ------------Fault Injection Button-------------
00029 //  Wait while the button is down
00030 //     Use this to simulate a STUCK fault
00031 // -----------------------------------------------
00032 void waitButton() {
00033    
00034     while (!button);
00035 }
00036 // ---Thread for controlling LED 1----------------
00037 //   Turn LED1 on/off in response to signals 
00038 // -----------------------------------------------
00039 void led1_thread() {  // method to run in thread
00040     osEvent evt ;
00041     while (true) {
00042         wdt_kick_all();
00043         evt = Thread::signal_wait(0x0); // wait for any signal
00044         if (evt.status == osEventSignal) {
00045             if (evt.value.signals & 0x01) 
00046             {
00047                 led1 = ON ;
00048                 
00049             }
00050             if (evt.value.signals & 0x02) led1 = OFF ;
00051         } 
00052     }
00053 }
00054 
00055 // ---Thread for timing --------------------------
00056 //   Send signals to the other threads
00057 // -----------------------------------------------
00058 void timer_thread() {  // method to run in thread
00059     while (true) {
00060         Thread::wait(250) ;
00061         threadLED1.signal_set(0x1) ;//on
00062         Thread::wait(250) ;
00063         threadLED1.signal_set(0x2) ;//off
00064     }
00065 }
00066 void failed_circuit() 
00067 { 
00068     int counter=0;
00069     while (true) 
00070     {
00071         volts = ain*3.3;
00072         if (volts>3) 
00073         {if(led_blue==1) {
00074             pc.printf("Warning: Short Circuit\n\r");
00075            led_green=0;}}//turn on the green LED
00076         
00077         else
00078         {
00079            if(led1==1)
00080            {
00081                 if (volts<0.1)        
00082                 {    
00083                     counter++;
00084                     if (counter>10)
00085                     { if(led_green==1){
00086                          pc.printf("Warning: Open Circuit\n\r");
00087                        led_blue=0;}//turn on the blue LED
00088                         counter =0;
00089                     }
00090                 }
00091                else
00092                 { if(led_blue==1 && led_green==1){
00093                     pc.printf("%f volts\n\r",volts);}
00094                 } 
00095             }
00096         }
00097  
00098        waitButton(); // POSSIBLE FAULT HERE     
00099     }     
00100 }
00101 // -----------MAIN-------------------------------
00102 //    Configure watchdog. Start threads. 
00103 //    Show start up with RED for 1sec
00104 //    Remember the watchdog is running
00105 //       - 1024ms to set it once
00106 //       - then must feed it every 32ms  
00107 // ----------------------------------------------
00108 
00109 int main(void) {
00110     
00111     wdt_1sec() ; // initialise watchdog - 32ms timeout
00112     
00113     // start threads
00114     threadT.start(&timer_thread) ; // start the timer thread 
00115     threadLED1.start(&led1_thread) ; // start the LED1 control thread 
00116     circuitfailure.start(&failed_circuit);
00117     
00118     // show start-up
00119     led_red = OFF;
00120     Thread::wait(1000) ;//1ms
00121     led_red = ON;
00122    
00123 }