Watchdog_Step-2_Edwin_Kadavy
Fork of Watchdog_sample_nocoverage by
main.cpp@6:554f0a4b681b, 2018-03-09 (annotated)
- Committer:
- edwinkad
- Date:
- Fri Mar 09 23:15:14 2018 +0000
- Revision:
- 6:554f0a4b681b
- Parent:
- 3:32a940251192
Edwin Kadavy
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" |
Manel_Marin | 0:5ce3cfc57999 | 4 | |
WilliamMarshQMUL | 2:c31a1758ac38 | 5 | // Sample program using the Watchdog |
WilliamMarshQMUL | 2:c31a1758ac38 | 6 | // --------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 7 | // * Three threads co-operate to flash two LEDs |
WilliamMarshQMUL | 3:32a940251192 | 8 | // * A simple way to inject a fault, by pressing a button |
WilliamMarshQMUL | 3:32a940251192 | 9 | // * The watchdog is configured with a 32ms timeout |
WilliamMarshQMUL | 3:32a940251192 | 10 | |
WilliamMarshQMUL | 3:32a940251192 | 11 | #define ON 1 |
WilliamMarshQMUL | 3:32a940251192 | 12 | #define OFF 0 |
WilliamMarshQMUL | 3:32a940251192 | 13 | DigitalOut led_red(LED_RED, 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); |
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 | |
WilliamMarshQMUL | 3:32a940251192 | 21 | // Threads |
WilliamMarshQMUL | 3:32a940251192 | 22 | Thread threadT ; // timer thread |
WilliamMarshQMUL | 3:32a940251192 | 23 | Thread threadLED1 ; // thread LED1 |
WilliamMarshQMUL | 3:32a940251192 | 24 | Thread threadLED2 ; // thread LED2 |
WilliamMarshQMUL | 3:32a940251192 | 25 | |
edwinkad | 6:554f0a4b681b | 26 | enum wState {Working, Stopped} ; |
edwinkad | 6:554f0a4b681b | 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() { |
WilliamMarshQMUL | 3:32a940251192 | 33 | while (!button) ; |
WilliamMarshQMUL | 3:32a940251192 | 34 | } |
WilliamMarshQMUL | 2:c31a1758ac38 | 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) { |
edwinkad | 6:554f0a4b681b | 42 | wdt_kickA(); // second part |
WilliamMarshQMUL | 3:32a940251192 | 43 | evt = Thread::signal_wait(0x0); // wait for any signal |
WilliamMarshQMUL | 3:32a940251192 | 44 | if (evt.status == osEventSignal) { |
WilliamMarshQMUL | 3:32a940251192 | 45 | if (evt.value.signals & 0x01) led1 = ON ; |
WilliamMarshQMUL | 3:32a940251192 | 46 | if (evt.value.signals & 0x02) led1 = OFF ; |
WilliamMarshQMUL | 3:32a940251192 | 47 | } |
WilliamMarshQMUL | 3:32a940251192 | 48 | waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 49 | } |
WilliamMarshQMUL | 3:32a940251192 | 50 | } |
Manel_Marin | 0:5ce3cfc57999 | 51 | |
WilliamMarshQMUL | 3:32a940251192 | 52 | // ---Thread for controlling LED 2---------------- |
WilliamMarshQMUL | 3:32a940251192 | 53 | // Turn LED2 on/off in response to signals |
WilliamMarshQMUL | 3:32a940251192 | 54 | // ----------------------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 55 | void led2_thread() { // method to run in thread |
WilliamMarshQMUL | 3:32a940251192 | 56 | osEvent evt ; |
WilliamMarshQMUL | 3:32a940251192 | 57 | while (true) { |
edwinkad | 6:554f0a4b681b | 58 | wdt_kickB(); // second part |
WilliamMarshQMUL | 3:32a940251192 | 59 | evt = Thread::signal_wait(0x0); // wait for any signal |
WilliamMarshQMUL | 3:32a940251192 | 60 | if (evt.status == osEventSignal) { |
WilliamMarshQMUL | 3:32a940251192 | 61 | if (evt.value.signals & 0x01) led2 = ON ; |
WilliamMarshQMUL | 3:32a940251192 | 62 | if (evt.value.signals & 0x02) led2 = OFF ; |
WilliamMarshQMUL | 3:32a940251192 | 63 | } |
edwinkad | 6:554f0a4b681b | 64 | //waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 65 | } |
WilliamMarshQMUL | 3:32a940251192 | 66 | } |
Manel_Marin | 0:5ce3cfc57999 | 67 | |
WilliamMarshQMUL | 3:32a940251192 | 68 | // ---Thread for timing -------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 69 | // Send signals to the other threads |
WilliamMarshQMUL | 3:32a940251192 | 70 | // ----------------------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 71 | void timer_thread() { // method to run in thread |
WilliamMarshQMUL | 3:32a940251192 | 72 | while (true) { |
WilliamMarshQMUL | 3:32a940251192 | 73 | Thread::wait(250) ; |
edwinkad | 6:554f0a4b681b | 74 | //wdt_kickA(); // first part |
WilliamMarshQMUL | 3:32a940251192 | 75 | threadLED1.signal_set(0x1) ; |
WilliamMarshQMUL | 3:32a940251192 | 76 | threadLED2.signal_set(0x1) ; |
WilliamMarshQMUL | 3:32a940251192 | 77 | Thread::wait(250) ; |
edwinkad | 6:554f0a4b681b | 78 | //wdt_kickA(); // first part |
WilliamMarshQMUL | 3:32a940251192 | 79 | threadLED1.signal_set(0x2) ; |
WilliamMarshQMUL | 3:32a940251192 | 80 | threadLED2.signal_set(0x2) ; |
edwinkad | 6:554f0a4b681b | 81 | //waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 82 | } |
WilliamMarshQMUL | 3:32a940251192 | 83 | } |
WilliamMarshQMUL | 3:32a940251192 | 84 | |
WilliamMarshQMUL | 3:32a940251192 | 85 | // -----------MAIN------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 86 | // Configure watchdog. Start threads. |
WilliamMarshQMUL | 3:32a940251192 | 87 | // Show start up with RED for 1sec |
WilliamMarshQMUL | 3:32a940251192 | 88 | // Remember the watchdog is running |
WilliamMarshQMUL | 3:32a940251192 | 89 | // - 1024ms to set it once |
WilliamMarshQMUL | 3:32a940251192 | 90 | // - then must feed it every 32ms |
WilliamMarshQMUL | 3:32a940251192 | 91 | // ---------------------------------------------- |
Manel_Marin | 0:5ce3cfc57999 | 92 | |
Manel_Marin | 0:5ce3cfc57999 | 93 | int main(void) { |
edwinkad | 6:554f0a4b681b | 94 | wdt_256ms() ; // initialise watchdog - 256ms timeout |
edwinkad | 6:554f0a4b681b | 95 | //wdt_kick_all; |
edwinkad | 6:554f0a4b681b | 96 | //tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms |
WilliamMarshQMUL | 1:159a09ac60ba | 97 | |
WilliamMarshQMUL | 3:32a940251192 | 98 | // start threads |
WilliamMarshQMUL | 3:32a940251192 | 99 | threadT.start(&timer_thread) ; // start the timer thread |
WilliamMarshQMUL | 3:32a940251192 | 100 | threadLED1.start(&led1_thread) ; // start the LED1 control thread |
WilliamMarshQMUL | 3:32a940251192 | 101 | threadLED2.start(&led2_thread) ; // start the LED2 control thread |
WilliamMarshQMUL | 1:159a09ac60ba | 102 | |
WilliamMarshQMUL | 1:159a09ac60ba | 103 | // show start-up |
WilliamMarshQMUL | 3:32a940251192 | 104 | led_red = OFF; |
WilliamMarshQMUL | 3:32a940251192 | 105 | Thread::wait(1000) ; |
WilliamMarshQMUL | 3:32a940251192 | 106 | led_red = ON; |
Manel_Marin | 0:5ce3cfc57999 | 107 | } |