Lab 6 Part 1

Fork of Watchdog_sample_nocoverage by William Marsh

Committer:
AnastasiosBarlas
Date:
Fri Mar 09 20:29:19 2018 +0000
Revision:
6:8311b4ad8438
Parent:
3:32a940251192
Child:
7:f356a62d5c52
Lab 6 Part 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Manel_Marin 0:5ce3cfc57999 1 #include "mbed.h"
WilliamMarshQMUL 3:32a940251192 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);
WilliamMarshQMUL 3:32a940251192 12 DigitalIn button(PTD0, PullUp);
WilliamMarshQMUL 3:32a940251192 13 DigitalOut led1(PTC12, OFF);
WilliamMarshQMUL 3:32a940251192 14 DigitalOut led2(PTC13, OFF);
AnastasiosBarlas 6:8311b4ad8438 15 Serial pc(USBTX, USBRX);
AnastasiosBarlas 6:8311b4ad8438 16
WilliamMarshQMUL 3:32a940251192 17
WilliamMarshQMUL 3:32a940251192 18 // This ticker is used to feed the watch dog
WilliamMarshQMUL 3:32a940251192 19 Ticker tick;
WilliamMarshQMUL 3:32a940251192 20
AnastasiosBarlas 6:8311b4ad8438 21
WilliamMarshQMUL 3:32a940251192 22 // Threads
AnastasiosBarlas 6:8311b4ad8438 23 Thread threadT(osPriorityNormal,1000); // timer thread
AnastasiosBarlas 6:8311b4ad8438 24 Thread threadLED1(osPriorityNormal,1000); // thread LED1
AnastasiosBarlas 6:8311b4ad8438 25 Thread threadLED2(osPriorityNormal,1000); // thread LED2
AnastasiosBarlas 6:8311b4ad8438 26 Thread threaddetection(osPriorityNormal,1000);
AnastasiosBarlas 6:8311b4ad8438 27
AnastasiosBarlas 6:8311b4ad8438 28
AnastasiosBarlas 6:8311b4ad8438 29 //define the state of watch dog
AnastasiosBarlas 6:8311b4ad8438 30 enum wState {Working, Stopped} ;
AnastasiosBarlas 6:8311b4ad8438 31 wState watchdogflag;
AnastasiosBarlas 6:8311b4ad8438 32
AnastasiosBarlas 6:8311b4ad8438 33
AnastasiosBarlas 6:8311b4ad8438 34 AnalogIn ainled1(A0); //LED1 ADC INPUT
AnastasiosBarlas 6:8311b4ad8438 35 //AnalogIn ainled2(A1); //LED2 ADC INPUT
AnastasiosBarlas 6:8311b4ad8438 36 float volt1,volt2;
WilliamMarshQMUL 3:32a940251192 37
WilliamMarshQMUL 3:32a940251192 38 // ------------Fault Injection Button-------------
WilliamMarshQMUL 3:32a940251192 39 // Wait while the button is down
WilliamMarshQMUL 3:32a940251192 40 // Use this to simulate a STUCK fault
WilliamMarshQMUL 3:32a940251192 41 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 42 void waitButton() {
AnastasiosBarlas 6:8311b4ad8438 43
AnastasiosBarlas 6:8311b4ad8438 44 while (!button); /*{
AnastasiosBarlas 6:8311b4ad8438 45 pc.printf("stop watchdog");
AnastasiosBarlas 6:8311b4ad8438 46 watchdogflag=Stopped;
AnastasiosBarlas 6:8311b4ad8438 47 } */
WilliamMarshQMUL 3:32a940251192 48 }
WilliamMarshQMUL 2:c31a1758ac38 49
AnastasiosBarlas 6:8311b4ad8438 50
AnastasiosBarlas 6:8311b4ad8438 51
AnastasiosBarlas 6:8311b4ad8438 52
WilliamMarshQMUL 3:32a940251192 53 // ---Thread for controlling LED 1----------------
WilliamMarshQMUL 3:32a940251192 54 // Turn LED1 on/off in response to signals
WilliamMarshQMUL 3:32a940251192 55 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 56 void led1_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 57 osEvent evt ;
WilliamMarshQMUL 3:32a940251192 58 while (true) {
WilliamMarshQMUL 3:32a940251192 59 evt = Thread::signal_wait(0x0); // wait for any signal
WilliamMarshQMUL 3:32a940251192 60 if (evt.status == osEventSignal) {
AnastasiosBarlas 6:8311b4ad8438 61 if (evt.value.signals & 0x01)
AnastasiosBarlas 6:8311b4ad8438 62 {
AnastasiosBarlas 6:8311b4ad8438 63 led1 = ON ;
AnastasiosBarlas 6:8311b4ad8438 64
AnastasiosBarlas 6:8311b4ad8438 65 }
AnastasiosBarlas 6:8311b4ad8438 66 wdt_kickB();
WilliamMarshQMUL 3:32a940251192 67 if (evt.value.signals & 0x02) led1 = OFF ;
AnastasiosBarlas 6:8311b4ad8438 68
WilliamMarshQMUL 3:32a940251192 69 }
AnastasiosBarlas 6:8311b4ad8438 70 //waitButton() ;
WilliamMarshQMUL 3:32a940251192 71 }
WilliamMarshQMUL 3:32a940251192 72 }
Manel_Marin 0:5ce3cfc57999 73
AnastasiosBarlas 6:8311b4ad8438 74
WilliamMarshQMUL 3:32a940251192 75 // ---Thread for controlling LED 2----------------
WilliamMarshQMUL 3:32a940251192 76 // Turn LED2 on/off in response to signals
WilliamMarshQMUL 3:32a940251192 77 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 78 void led2_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 79 osEvent evt ;
WilliamMarshQMUL 3:32a940251192 80 while (true) {
WilliamMarshQMUL 3:32a940251192 81 evt = Thread::signal_wait(0x0); // wait for any signal
WilliamMarshQMUL 3:32a940251192 82 if (evt.status == osEventSignal) {
AnastasiosBarlas 6:8311b4ad8438 83 if (evt.value.signals & 0x01)
AnastasiosBarlas 6:8311b4ad8438 84 {
AnastasiosBarlas 6:8311b4ad8438 85 led2 = ON ;
AnastasiosBarlas 6:8311b4ad8438 86 }
WilliamMarshQMUL 3:32a940251192 87 if (evt.value.signals & 0x02) led2 = OFF ;
WilliamMarshQMUL 3:32a940251192 88 }
AnastasiosBarlas 6:8311b4ad8438 89 wdt_kickA();
WilliamMarshQMUL 3:32a940251192 90 // waitButton() ; // POSSIBLE FAULT HERE
WilliamMarshQMUL 3:32a940251192 91 }
WilliamMarshQMUL 3:32a940251192 92 }
Manel_Marin 0:5ce3cfc57999 93
AnastasiosBarlas 6:8311b4ad8438 94
WilliamMarshQMUL 3:32a940251192 95 // ---Thread for timing --------------------------
WilliamMarshQMUL 3:32a940251192 96 // Send signals to the other threads
WilliamMarshQMUL 3:32a940251192 97 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 98 void timer_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 99 while (true) {
WilliamMarshQMUL 3:32a940251192 100 Thread::wait(250) ;
AnastasiosBarlas 6:8311b4ad8438 101 threadLED1.signal_set(0x1) ;//on
AnastasiosBarlas 6:8311b4ad8438 102 threadLED2.signal_set(0x1) ;//on
WilliamMarshQMUL 3:32a940251192 103 Thread::wait(250) ;
AnastasiosBarlas 6:8311b4ad8438 104 threadLED1.signal_set(0x2) ;//off
AnastasiosBarlas 6:8311b4ad8438 105 threadLED2.signal_set(0x2) ;//off
AnastasiosBarlas 6:8311b4ad8438 106 waitButton() ; // POSSIBLE FAULT HERE
WilliamMarshQMUL 3:32a940251192 107 }
WilliamMarshQMUL 3:32a940251192 108 }
WilliamMarshQMUL 3:32a940251192 109
AnastasiosBarlas 6:8311b4ad8438 110
AnastasiosBarlas 6:8311b4ad8438 111
AnastasiosBarlas 6:8311b4ad8438 112 void Detec_Thread()
AnastasiosBarlas 6:8311b4ad8438 113 {
AnastasiosBarlas 6:8311b4ad8438 114 int counter=0;
AnastasiosBarlas 6:8311b4ad8438 115 while (1)
AnastasiosBarlas 6:8311b4ad8438 116 {
AnastasiosBarlas 6:8311b4ad8438 117 volt1 = ainled1;
AnastasiosBarlas 6:8311b4ad8438 118 // pc.printf("%f\n\r",volt1);
AnastasiosBarlas 6:8311b4ad8438 119 if (volt1>0.90)
AnastasiosBarlas 6:8311b4ad8438 120 {
AnastasiosBarlas 6:8311b4ad8438 121 pc.printf("short circuit\n\r");
AnastasiosBarlas 6:8311b4ad8438 122 led_red=0;
AnastasiosBarlas 6:8311b4ad8438 123 }
AnastasiosBarlas 6:8311b4ad8438 124 else
AnastasiosBarlas 6:8311b4ad8438 125 {
AnastasiosBarlas 6:8311b4ad8438 126 if(led1==1)
AnastasiosBarlas 6:8311b4ad8438 127 {
AnastasiosBarlas 6:8311b4ad8438 128 if ((volt1<0.1))
AnastasiosBarlas 6:8311b4ad8438 129 {
AnastasiosBarlas 6:8311b4ad8438 130 counter++;
AnastasiosBarlas 6:8311b4ad8438 131 if (counter>5)
AnastasiosBarlas 6:8311b4ad8438 132 {
AnastasiosBarlas 6:8311b4ad8438 133 pc.printf("open circuit\n\r");
AnastasiosBarlas 6:8311b4ad8438 134 if(led_red==1)led_red=0;
AnastasiosBarlas 6:8311b4ad8438 135 counter =0;
AnastasiosBarlas 6:8311b4ad8438 136 }
AnastasiosBarlas 6:8311b4ad8438 137 }
AnastasiosBarlas 6:8311b4ad8438 138 else
AnastasiosBarlas 6:8311b4ad8438 139 {
AnastasiosBarlas 6:8311b4ad8438 140 pc.printf("%f\n\r",volt1);
AnastasiosBarlas 6:8311b4ad8438 141 if (led_red==0)led_red=1;
AnastasiosBarlas 6:8311b4ad8438 142 }
AnastasiosBarlas 6:8311b4ad8438 143 }
AnastasiosBarlas 6:8311b4ad8438 144 }
AnastasiosBarlas 6:8311b4ad8438 145
AnastasiosBarlas 6:8311b4ad8438 146 waitButton(); // POSSIBLE FAULT HERE
AnastasiosBarlas 6:8311b4ad8438 147 }
AnastasiosBarlas 6:8311b4ad8438 148 }
AnastasiosBarlas 6:8311b4ad8438 149
AnastasiosBarlas 6:8311b4ad8438 150
WilliamMarshQMUL 3:32a940251192 151 // -----------MAIN-------------------------------
WilliamMarshQMUL 3:32a940251192 152 // Configure watchdog. Start threads.
WilliamMarshQMUL 3:32a940251192 153 // Show start up with RED for 1sec
WilliamMarshQMUL 3:32a940251192 154 // Remember the watchdog is running
WilliamMarshQMUL 3:32a940251192 155 // - 1024ms to set it once
WilliamMarshQMUL 3:32a940251192 156 // - then must feed it every 32ms
WilliamMarshQMUL 3:32a940251192 157 // ----------------------------------------------
Manel_Marin 0:5ce3cfc57999 158
AnastasiosBarlas 6:8311b4ad8438 159
AnastasiosBarlas 6:8311b4ad8438 160 void watchdog()
AnastasiosBarlas 6:8311b4ad8438 161 {
AnastasiosBarlas 6:8311b4ad8438 162
AnastasiosBarlas 6:8311b4ad8438 163 if (watchdogflag==Working)
AnastasiosBarlas 6:8311b4ad8438 164 {
AnastasiosBarlas 6:8311b4ad8438 165 wdt_kick_all();
AnastasiosBarlas 6:8311b4ad8438 166 }
AnastasiosBarlas 6:8311b4ad8438 167 }
AnastasiosBarlas 6:8311b4ad8438 168
Manel_Marin 0:5ce3cfc57999 169 int main(void) {
AnastasiosBarlas 6:8311b4ad8438 170
AnastasiosBarlas 6:8311b4ad8438 171 wdt_1sec() ; // initialise watchdog - 32ms timeout
AnastasiosBarlas 6:8311b4ad8438 172 // watchdogflag = Working;
AnastasiosBarlas 6:8311b4ad8438 173 //tick.attach_us(callback(&watchdog), 20000);
WilliamMarshQMUL 1:159a09ac60ba 174
WilliamMarshQMUL 3:32a940251192 175 // start threads
WilliamMarshQMUL 3:32a940251192 176 threadT.start(&timer_thread) ; // start the timer thread
WilliamMarshQMUL 3:32a940251192 177 threadLED1.start(&led1_thread) ; // start the LED1 control thread
AnastasiosBarlas 6:8311b4ad8438 178 threadLED2.start(&led2_thread) ; // start the LED1 control thread
AnastasiosBarlas 6:8311b4ad8438 179 threaddetection.start(&Detec_Thread);
WilliamMarshQMUL 1:159a09ac60ba 180
WilliamMarshQMUL 1:159a09ac60ba 181 // show start-up
WilliamMarshQMUL 3:32a940251192 182 led_red = OFF;
AnastasiosBarlas 6:8311b4ad8438 183 Thread::wait(1000) ;//1ms
WilliamMarshQMUL 3:32a940251192 184 led_red = ON;
AnastasiosBarlas 6:8311b4ad8438 185
Manel_Marin 0:5ce3cfc57999 186 }