Anastasios Barlas
/
Watchdog_sample_nocoverage
Lab 6 Part 1
Fork of Watchdog_sample_nocoverage by
main.cpp@8:3a8c46bc268f, 2018-03-09 (annotated)
- Committer:
- AnastasiosBarlas
- Date:
- Fri Mar 09 23:25:44 2018 +0000
- Revision:
- 8:3a8c46bc268f
- Parent:
- 7:f356a62d5c52
Lab 6 Part 1
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:f356a62d5c52 | 112 | //void Detec_Thread() |
AnastasiosBarlas | 7:f356a62d5c52 | 113 | //{ |
AnastasiosBarlas | 7:f356a62d5c52 | 114 | // int counter=0; |
AnastasiosBarlas | 7:f356a62d5c52 | 115 | // while (1) |
AnastasiosBarlas | 7:f356a62d5c52 | 116 | // { |
AnastasiosBarlas | 7:f356a62d5c52 | 117 | // volt1 = ainled1; |
AnastasiosBarlas | 7:f356a62d5c52 | 118 | // // pc.printf("%f\n\r",volt1); |
AnastasiosBarlas | 7:f356a62d5c52 | 119 | // if (volt1>0.90) |
AnastasiosBarlas | 7:f356a62d5c52 | 120 | // { |
AnastasiosBarlas | 7:f356a62d5c52 | 121 | // pc.printf("short circuit\n\r"); |
AnastasiosBarlas | 7:f356a62d5c52 | 122 | // led_red=0; |
AnastasiosBarlas | 7:f356a62d5c52 | 123 | // } |
AnastasiosBarlas | 7:f356a62d5c52 | 124 | // else |
AnastasiosBarlas | 7:f356a62d5c52 | 125 | // { |
AnastasiosBarlas | 7:f356a62d5c52 | 126 | // if(led1==1) |
AnastasiosBarlas | 7:f356a62d5c52 | 127 | // { |
AnastasiosBarlas | 7:f356a62d5c52 | 128 | // if ((volt1<0.1)) |
AnastasiosBarlas | 7:f356a62d5c52 | 129 | // { |
AnastasiosBarlas | 7:f356a62d5c52 | 130 | // counter++; |
AnastasiosBarlas | 7:f356a62d5c52 | 131 | // if (counter>5) |
AnastasiosBarlas | 7:f356a62d5c52 | 132 | // { |
AnastasiosBarlas | 7:f356a62d5c52 | 133 | // pc.printf("open circuit\n\r"); |
AnastasiosBarlas | 7:f356a62d5c52 | 134 | // if(led_red==1)led_red=0; |
AnastasiosBarlas | 7:f356a62d5c52 | 135 | // counter =0; |
AnastasiosBarlas | 7:f356a62d5c52 | 136 | // } |
AnastasiosBarlas | 7:f356a62d5c52 | 137 | // } |
AnastasiosBarlas | 7:f356a62d5c52 | 138 | // else |
AnastasiosBarlas | 7:f356a62d5c52 | 139 | // { |
AnastasiosBarlas | 7:f356a62d5c52 | 140 | // pc.printf("%f\n\r",volt1); |
AnastasiosBarlas | 7:f356a62d5c52 | 141 | // if (led_red==0)led_red=1; |
AnastasiosBarlas | 7:f356a62d5c52 | 142 | // } |
AnastasiosBarlas | 7:f356a62d5c52 | 143 | // } |
AnastasiosBarlas | 7:f356a62d5c52 | 144 | // } |
AnastasiosBarlas | 7:f356a62d5c52 | 145 | // |
AnastasiosBarlas | 7:f356a62d5c52 | 146 | // waitButton(); // POSSIBLE FAULT HERE |
AnastasiosBarlas | 7:f356a62d5c52 | 147 | // } |
AnastasiosBarlas | 7:f356a62d5c52 | 148 | //} |
AnastasiosBarlas | 6:8311b4ad8438 | 149 | |
AnastasiosBarlas | 8:3a8c46bc268f | 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 | } |