Lab 6 Code 2 Hardware Failure Detection

Fork of Watchdog_sample_nocoverage by William Marsh

Committer:
Tobden
Date:
Fri Mar 09 22:58:48 2018 +0000
Revision:
6:f375c710bd0e
Parent:
3:32a940251192
Lab 6 Code 2 Hardware Failure

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Manel_Marin 0:5ce3cfc57999 1 #include "mbed.h"
Tobden 6:f375c710bd0e 2 //#include "rtos.h"
WilliamMarshQMUL 1:159a09ac60ba 3 #include "wdt.h"
WilliamMarshQMUL 2:c31a1758ac38 4 // Sample program using the Watchdog
WilliamMarshQMUL 2:c31a1758ac38 5 // ---------------------------------
WilliamMarshQMUL 3:32a940251192 6 // * Three threads co-operate to flash two LEDs
WilliamMarshQMUL 3:32a940251192 7 // * A simple way to inject a fault, by pressing a button
WilliamMarshQMUL 3:32a940251192 8 // * The watchdog is configured with a 32ms timeout
WilliamMarshQMUL 3:32a940251192 9 #define ON 1
WilliamMarshQMUL 3:32a940251192 10 #define OFF 0
WilliamMarshQMUL 3:32a940251192 11 DigitalOut led_red(LED_RED, ON);
Tobden 6:f375c710bd0e 12 DigitalOut led_blue(LED_BLUE, ON);
Tobden 6:f375c710bd0e 13 DigitalOut led_green(LED_GREEN, ON);
WilliamMarshQMUL 3:32a940251192 14 DigitalIn button(PTD0, PullUp);
WilliamMarshQMUL 3:32a940251192 15 DigitalOut led1(PTC12, OFF);
WilliamMarshQMUL 3:32a940251192 16 DigitalOut led2(PTC13, OFF);
Tobden 6:f375c710bd0e 17 Serial pc(USBTX, USBRX);
WilliamMarshQMUL 3:32a940251192 18 // This ticker is used to feed the watch dog
WilliamMarshQMUL 3:32a940251192 19 Ticker tick;
Tobden 6:f375c710bd0e 20 // Threads
Tobden 6:f375c710bd0e 21 Thread threadT; // timer thread
Tobden 6:f375c710bd0e 22 Thread threadLED1; // thread LED1
Tobden 6:f375c710bd0e 23 Thread circuitfailure;
Tobden 6:f375c710bd0e 24 AnalogIn ain(A0);
WilliamMarshQMUL 3:32a940251192 25
Tobden 6:f375c710bd0e 26 float volts;
WilliamMarshQMUL 3:32a940251192 27
WilliamMarshQMUL 3:32a940251192 28 // ------------Fault Injection Button-------------
WilliamMarshQMUL 3:32a940251192 29 // Wait while the button is down
WilliamMarshQMUL 3:32a940251192 30 // Use this to simulate a STUCK fault
WilliamMarshQMUL 3:32a940251192 31 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 32 void waitButton() {
Tobden 6:f375c710bd0e 33
Tobden 6:f375c710bd0e 34 while (!button);
WilliamMarshQMUL 3:32a940251192 35 }
WilliamMarshQMUL 3:32a940251192 36 // ---Thread for controlling LED 1----------------
WilliamMarshQMUL 3:32a940251192 37 // Turn LED1 on/off in response to signals
WilliamMarshQMUL 3:32a940251192 38 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 39 void led1_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 40 osEvent evt ;
WilliamMarshQMUL 3:32a940251192 41 while (true) {
Tobden 6:f375c710bd0e 42 wdt_kick_all();
WilliamMarshQMUL 3:32a940251192 43 evt = Thread::signal_wait(0x0); // wait for any signal
WilliamMarshQMUL 3:32a940251192 44 if (evt.status == osEventSignal) {
Tobden 6:f375c710bd0e 45 if (evt.value.signals & 0x01)
Tobden 6:f375c710bd0e 46 {
Tobden 6:f375c710bd0e 47 led1 = ON ;
Tobden 6:f375c710bd0e 48
Tobden 6:f375c710bd0e 49 }
WilliamMarshQMUL 3:32a940251192 50 if (evt.value.signals & 0x02) led1 = OFF ;
WilliamMarshQMUL 3:32a940251192 51 }
WilliamMarshQMUL 3:32a940251192 52 }
WilliamMarshQMUL 3:32a940251192 53 }
Manel_Marin 0:5ce3cfc57999 54
WilliamMarshQMUL 3:32a940251192 55 // ---Thread for timing --------------------------
WilliamMarshQMUL 3:32a940251192 56 // Send signals to the other threads
WilliamMarshQMUL 3:32a940251192 57 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 58 void timer_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 59 while (true) {
WilliamMarshQMUL 3:32a940251192 60 Thread::wait(250) ;
Tobden 6:f375c710bd0e 61 threadLED1.signal_set(0x1) ;//on
WilliamMarshQMUL 3:32a940251192 62 Thread::wait(250) ;
Tobden 6:f375c710bd0e 63 threadLED1.signal_set(0x2) ;//off
WilliamMarshQMUL 3:32a940251192 64 }
WilliamMarshQMUL 3:32a940251192 65 }
Tobden 6:f375c710bd0e 66 void failed_circuit()
Tobden 6:f375c710bd0e 67 {
Tobden 6:f375c710bd0e 68 int counter=0;
Tobden 6:f375c710bd0e 69 while (true)
Tobden 6:f375c710bd0e 70 {
Tobden 6:f375c710bd0e 71 volts = ain*3.3;
Tobden 6:f375c710bd0e 72 if (volts>3)
Tobden 6:f375c710bd0e 73 {if(led_blue==1) {
Tobden 6:f375c710bd0e 74 pc.printf("Warning: Short Circuit\n\r");
Tobden 6:f375c710bd0e 75 led_green=0;}}//turn on the green LED
Tobden 6:f375c710bd0e 76
Tobden 6:f375c710bd0e 77 else
Tobden 6:f375c710bd0e 78 {
Tobden 6:f375c710bd0e 79 if(led1==1)
Tobden 6:f375c710bd0e 80 {
Tobden 6:f375c710bd0e 81 if (volts<0.1)
Tobden 6:f375c710bd0e 82 {
Tobden 6:f375c710bd0e 83 counter++;
Tobden 6:f375c710bd0e 84 if (counter>10)
Tobden 6:f375c710bd0e 85 { if(led_green==1){
Tobden 6:f375c710bd0e 86 pc.printf("Warning: Open Circuit\n\r");
Tobden 6:f375c710bd0e 87 led_blue=0;}//turn on the blue LED
Tobden 6:f375c710bd0e 88 counter =0;
Tobden 6:f375c710bd0e 89 }
Tobden 6:f375c710bd0e 90 }
Tobden 6:f375c710bd0e 91 else
Tobden 6:f375c710bd0e 92 { if(led_blue==1 && led_green==1){
Tobden 6:f375c710bd0e 93 pc.printf("%f volts\n\r",volts);}
Tobden 6:f375c710bd0e 94 }
Tobden 6:f375c710bd0e 95 }
Tobden 6:f375c710bd0e 96 }
Tobden 6:f375c710bd0e 97
Tobden 6:f375c710bd0e 98 waitButton(); // POSSIBLE FAULT HERE
Tobden 6:f375c710bd0e 99 }
Tobden 6:f375c710bd0e 100 }
WilliamMarshQMUL 3:32a940251192 101 // -----------MAIN-------------------------------
WilliamMarshQMUL 3:32a940251192 102 // Configure watchdog. Start threads.
WilliamMarshQMUL 3:32a940251192 103 // Show start up with RED for 1sec
WilliamMarshQMUL 3:32a940251192 104 // Remember the watchdog is running
WilliamMarshQMUL 3:32a940251192 105 // - 1024ms to set it once
WilliamMarshQMUL 3:32a940251192 106 // - then must feed it every 32ms
WilliamMarshQMUL 3:32a940251192 107 // ----------------------------------------------
Manel_Marin 0:5ce3cfc57999 108
Manel_Marin 0:5ce3cfc57999 109 int main(void) {
Tobden 6:f375c710bd0e 110
Tobden 6:f375c710bd0e 111 wdt_1sec() ; // initialise watchdog - 32ms timeout
WilliamMarshQMUL 1:159a09ac60ba 112
WilliamMarshQMUL 3:32a940251192 113 // start threads
WilliamMarshQMUL 3:32a940251192 114 threadT.start(&timer_thread) ; // start the timer thread
WilliamMarshQMUL 3:32a940251192 115 threadLED1.start(&led1_thread) ; // start the LED1 control thread
Tobden 6:f375c710bd0e 116 circuitfailure.start(&failed_circuit);
WilliamMarshQMUL 1:159a09ac60ba 117
WilliamMarshQMUL 1:159a09ac60ba 118 // show start-up
WilliamMarshQMUL 3:32a940251192 119 led_red = OFF;
Tobden 6:f375c710bd0e 120 Thread::wait(1000) ;//1ms
WilliamMarshQMUL 3:32a940251192 121 led_red = ON;
Tobden 6:f375c710bd0e 122
Manel_Marin 0:5ce3cfc57999 123 }