Watchdog example. Added fault coverage in all 3 threads.
Fork of Watchdog_sample_nocoverage by
main.cpp@6:0b769cc52024, 2018-03-09 (annotated)
- Committer:
- natgovor
- Date:
- Fri Mar 09 15:01:33 2018 +0000
- Revision:
- 6:0b769cc52024
- Parent:
- 3:32a940251192
- Child:
- 7:9def90a894b9
Added fault coverage in all 3 threads.
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 |
natgovor | 6:0b769cc52024 | 22 | Thread threadT(osPriorityNormal, 2000); // timer thread |
natgovor | 6:0b769cc52024 | 23 | Thread threadLED1(osPriorityNormal, 2000); // thread LED1 |
natgovor | 6:0b769cc52024 | 24 | Thread threadLED2(osPriorityNormal, 2000); // thread LED2 |
WilliamMarshQMUL | 3:32a940251192 | 25 | |
WilliamMarshQMUL | 3:32a940251192 | 26 | // ------------Fault Injection Button------------- |
WilliamMarshQMUL | 3:32a940251192 | 27 | // Wait while the button is down |
WilliamMarshQMUL | 3:32a940251192 | 28 | // Use this to simulate a STUCK fault |
WilliamMarshQMUL | 3:32a940251192 | 29 | // ----------------------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 30 | void waitButton() { |
WilliamMarshQMUL | 3:32a940251192 | 31 | while (!button) ; |
WilliamMarshQMUL | 3:32a940251192 | 32 | } |
WilliamMarshQMUL | 2:c31a1758ac38 | 33 | |
WilliamMarshQMUL | 3:32a940251192 | 34 | // ---Thread for controlling LED 1---------------- |
WilliamMarshQMUL | 3:32a940251192 | 35 | // Turn LED1 on/off in response to signals |
WilliamMarshQMUL | 3:32a940251192 | 36 | // ----------------------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 37 | void led1_thread() { // method to run in thread |
WilliamMarshQMUL | 3:32a940251192 | 38 | osEvent evt ; |
WilliamMarshQMUL | 3:32a940251192 | 39 | while (true) { |
WilliamMarshQMUL | 3:32a940251192 | 40 | evt = Thread::signal_wait(0x0); // wait for any signal |
WilliamMarshQMUL | 3:32a940251192 | 41 | if (evt.status == osEventSignal) { |
WilliamMarshQMUL | 3:32a940251192 | 42 | if (evt.value.signals & 0x01) led1 = ON ; |
WilliamMarshQMUL | 3:32a940251192 | 43 | if (evt.value.signals & 0x02) led1 = OFF ; |
natgovor | 6:0b769cc52024 | 44 | wdt_kickA(); |
WilliamMarshQMUL | 3:32a940251192 | 45 | } |
natgovor | 6:0b769cc52024 | 46 | //waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 47 | } |
WilliamMarshQMUL | 3:32a940251192 | 48 | } |
Manel_Marin | 0:5ce3cfc57999 | 49 | |
WilliamMarshQMUL | 3:32a940251192 | 50 | // ---Thread for controlling LED 2---------------- |
WilliamMarshQMUL | 3:32a940251192 | 51 | // Turn LED2 on/off in response to signals |
WilliamMarshQMUL | 3:32a940251192 | 52 | // ----------------------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 53 | void led2_thread() { // method to run in thread |
WilliamMarshQMUL | 3:32a940251192 | 54 | osEvent evt ; |
WilliamMarshQMUL | 3:32a940251192 | 55 | while (true) { |
WilliamMarshQMUL | 3:32a940251192 | 56 | evt = Thread::signal_wait(0x0); // wait for any signal |
WilliamMarshQMUL | 3:32a940251192 | 57 | if (evt.status == osEventSignal) { |
WilliamMarshQMUL | 3:32a940251192 | 58 | if (evt.value.signals & 0x01) led2 = ON ; |
WilliamMarshQMUL | 3:32a940251192 | 59 | if (evt.value.signals & 0x02) led2 = OFF ; |
natgovor | 6:0b769cc52024 | 60 | wdt_kickB(); |
WilliamMarshQMUL | 3:32a940251192 | 61 | } |
natgovor | 6:0b769cc52024 | 62 | waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 63 | } |
WilliamMarshQMUL | 3:32a940251192 | 64 | } |
Manel_Marin | 0:5ce3cfc57999 | 65 | |
WilliamMarshQMUL | 3:32a940251192 | 66 | // ---Thread for timing -------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 67 | // Send signals to the other threads |
WilliamMarshQMUL | 3:32a940251192 | 68 | // ----------------------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 69 | void timer_thread() { // method to run in thread |
WilliamMarshQMUL | 3:32a940251192 | 70 | while (true) { |
WilliamMarshQMUL | 3:32a940251192 | 71 | Thread::wait(250) ; |
WilliamMarshQMUL | 3:32a940251192 | 72 | threadLED1.signal_set(0x1) ; |
WilliamMarshQMUL | 3:32a940251192 | 73 | threadLED2.signal_set(0x1) ; |
WilliamMarshQMUL | 3:32a940251192 | 74 | Thread::wait(250) ; |
WilliamMarshQMUL | 3:32a940251192 | 75 | threadLED1.signal_set(0x2) ; |
WilliamMarshQMUL | 3:32a940251192 | 76 | threadLED2.signal_set(0x2) ; |
natgovor | 6:0b769cc52024 | 77 | //waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 78 | } |
WilliamMarshQMUL | 3:32a940251192 | 79 | } |
WilliamMarshQMUL | 3:32a940251192 | 80 | |
WilliamMarshQMUL | 3:32a940251192 | 81 | // -----------MAIN------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 82 | // Configure watchdog. Start threads. |
WilliamMarshQMUL | 3:32a940251192 | 83 | // Show start up with RED for 1sec |
WilliamMarshQMUL | 3:32a940251192 | 84 | // Remember the watchdog is running |
WilliamMarshQMUL | 3:32a940251192 | 85 | // - 1024ms to set it once |
WilliamMarshQMUL | 3:32a940251192 | 86 | // - then must feed it every 32ms |
WilliamMarshQMUL | 3:32a940251192 | 87 | // ---------------------------------------------- |
Manel_Marin | 0:5ce3cfc57999 | 88 | |
Manel_Marin | 0:5ce3cfc57999 | 89 | int main(void) { |
natgovor | 6:0b769cc52024 | 90 | wdt_1sec() ; // initialise watchdog - 1sec timeout |
WilliamMarshQMUL | 1:159a09ac60ba | 91 | |
WilliamMarshQMUL | 3:32a940251192 | 92 | // start threads |
WilliamMarshQMUL | 3:32a940251192 | 93 | threadT.start(&timer_thread) ; // start the timer thread |
WilliamMarshQMUL | 3:32a940251192 | 94 | threadLED1.start(&led1_thread) ; // start the LED1 control thread |
WilliamMarshQMUL | 3:32a940251192 | 95 | threadLED2.start(&led2_thread) ; // start the LED2 control thread |
WilliamMarshQMUL | 1:159a09ac60ba | 96 | |
WilliamMarshQMUL | 1:159a09ac60ba | 97 | // show start-up |
WilliamMarshQMUL | 3:32a940251192 | 98 | led_red = OFF; |
WilliamMarshQMUL | 3:32a940251192 | 99 | Thread::wait(1000) ; |
WilliamMarshQMUL | 3:32a940251192 | 100 | led_red = ON; |
Manel_Marin | 0:5ce3cfc57999 | 101 | } |