![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
2.2
Fork of Watchdog_sample_nocoverage by
main.cpp@6:19129251ae4a, 2018-03-09 (annotated)
- Committer:
- diviavad
- Date:
- Fri Mar 09 22:07:20 2018 +0000
- Revision:
- 6:19129251ae4a
- Parent:
- 3:32a940251192
Lab 2.2
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 |
diviavad | 6:19129251ae4a | 22 | Thread threadT(osPriorityNormal, 1000) ; // timer thread |
diviavad | 6:19129251ae4a | 23 | Thread threadLED1(osPriorityNormal, 1000) ; // thread LED1 |
diviavad | 6:19129251ae4a | 24 | Thread threadLED2(osPriorityNormal, 1000) ; // 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 ; |
diviavad | 6:19129251ae4a | 44 | wdt_kickA(); |
WilliamMarshQMUL | 3:32a940251192 | 45 | } |
diviavad | 6:19129251ae4a | 46 | |
diviavad | 6:19129251ae4a | 47 | //waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 48 | } |
WilliamMarshQMUL | 3:32a940251192 | 49 | } |
Manel_Marin | 0:5ce3cfc57999 | 50 | |
WilliamMarshQMUL | 3:32a940251192 | 51 | // ---Thread for controlling LED 2---------------- |
WilliamMarshQMUL | 3:32a940251192 | 52 | // Turn LED2 on/off in response to signals |
WilliamMarshQMUL | 3:32a940251192 | 53 | // ----------------------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 54 | void led2_thread() { // method to run in thread |
WilliamMarshQMUL | 3:32a940251192 | 55 | osEvent evt ; |
WilliamMarshQMUL | 3:32a940251192 | 56 | while (true) { |
WilliamMarshQMUL | 3:32a940251192 | 57 | evt = Thread::signal_wait(0x0); // wait for any signal |
WilliamMarshQMUL | 3:32a940251192 | 58 | if (evt.status == osEventSignal) { |
WilliamMarshQMUL | 3:32a940251192 | 59 | if (evt.value.signals & 0x01) led2 = ON ; |
WilliamMarshQMUL | 3:32a940251192 | 60 | if (evt.value.signals & 0x02) led2 = OFF ; |
diviavad | 6:19129251ae4a | 61 | wdt_kickB(); |
WilliamMarshQMUL | 3:32a940251192 | 62 | } |
diviavad | 6:19129251ae4a | 63 | |
diviavad | 6:19129251ae4a | 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) ; |
WilliamMarshQMUL | 3:32a940251192 | 74 | threadLED1.signal_set(0x1) ; |
WilliamMarshQMUL | 3:32a940251192 | 75 | threadLED2.signal_set(0x1) ; |
WilliamMarshQMUL | 3:32a940251192 | 76 | Thread::wait(250) ; |
WilliamMarshQMUL | 3:32a940251192 | 77 | threadLED1.signal_set(0x2) ; |
WilliamMarshQMUL | 3:32a940251192 | 78 | threadLED2.signal_set(0x2) ; |
diviavad | 6:19129251ae4a | 79 | waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 80 | } |
WilliamMarshQMUL | 3:32a940251192 | 81 | } |
WilliamMarshQMUL | 3:32a940251192 | 82 | |
WilliamMarshQMUL | 3:32a940251192 | 83 | // -----------MAIN------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 84 | // Configure watchdog. Start threads. |
WilliamMarshQMUL | 3:32a940251192 | 85 | // Show start up with RED for 1sec |
WilliamMarshQMUL | 3:32a940251192 | 86 | // Remember the watchdog is running |
WilliamMarshQMUL | 3:32a940251192 | 87 | // - 1024ms to set it once |
WilliamMarshQMUL | 3:32a940251192 | 88 | // - then must feed it every 32ms |
WilliamMarshQMUL | 3:32a940251192 | 89 | // ---------------------------------------------- |
Manel_Marin | 0:5ce3cfc57999 | 90 | |
Manel_Marin | 0:5ce3cfc57999 | 91 | int main(void) { |
diviavad | 6:19129251ae4a | 92 | wdt_1sec(); |
diviavad | 6:19129251ae4a | 93 | |
diviavad | 6:19129251ae4a | 94 | //wdt_32ms() ; // initialise watchdog - 32ms timeout |
diviavad | 6:19129251ae4a | 95 | //tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms |
WilliamMarshQMUL | 1:159a09ac60ba | 96 | |
WilliamMarshQMUL | 3:32a940251192 | 97 | // start threads |
WilliamMarshQMUL | 3:32a940251192 | 98 | threadT.start(&timer_thread) ; // start the timer thread |
WilliamMarshQMUL | 3:32a940251192 | 99 | threadLED1.start(&led1_thread) ; // start the LED1 control thread |
WilliamMarshQMUL | 3:32a940251192 | 100 | threadLED2.start(&led2_thread) ; // start the LED2 control thread |
WilliamMarshQMUL | 1:159a09ac60ba | 101 | |
WilliamMarshQMUL | 1:159a09ac60ba | 102 | // show start-up |
WilliamMarshQMUL | 3:32a940251192 | 103 | led_red = OFF; |
WilliamMarshQMUL | 3:32a940251192 | 104 | Thread::wait(1000) ; |
WilliamMarshQMUL | 3:32a940251192 | 105 | led_red = ON; |
Manel_Marin | 0:5ce3cfc57999 | 106 | } |