![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
LAB 6 PART 2
Fork of Watchdog_sample_nocoverage by
main.cpp@6:bd874c16ce2f, 2018-03-09 (annotated)
- Committer:
- Bossman
- Date:
- Fri Mar 09 18:46:54 2018 +0000
- Revision:
- 6:bd874c16ce2f
- Parent:
- 3:32a940251192
Version 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 |
Bossman | 6:bd874c16ce2f | 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); |
Bossman | 6:bd874c16ce2f | 15 | Serial pc(USBTX, USBRX); |
WilliamMarshQMUL | 3:32a940251192 | 16 | // This ticker is used to feed the watch dog |
WilliamMarshQMUL | 3:32a940251192 | 17 | Ticker tick; |
WilliamMarshQMUL | 3:32a940251192 | 18 | // Threads |
Bossman | 6:bd874c16ce2f | 19 | Thread threadT(osPriorityNormal,1000); // timer thread |
Bossman | 6:bd874c16ce2f | 20 | Thread threadLED1(osPriorityNormal,1000); // thread LED1 |
Bossman | 6:bd874c16ce2f | 21 | Thread threaddetection(osPriorityNormal,1000); |
Bossman | 6:bd874c16ce2f | 22 | //define the state of watch dog |
Bossman | 6:bd874c16ce2f | 23 | enum wState {Working, Stopped} ; |
Bossman | 6:bd874c16ce2f | 24 | wState watchdogflag; |
Bossman | 6:bd874c16ce2f | 25 | AnalogIn ainled1(A0); //LED1 ADC INPUT |
Bossman | 6:bd874c16ce2f | 26 | float volt1,volt2; |
WilliamMarshQMUL | 3:32a940251192 | 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 | // ----------------------------------------------- |
Bossman | 6:bd874c16ce2f | 32 | void waitButton() |
Bossman | 6:bd874c16ce2f | 33 | { |
WilliamMarshQMUL | 2:c31a1758ac38 | 34 | |
Bossman | 6:bd874c16ce2f | 35 | while (!button) { |
Bossman | 6:bd874c16ce2f | 36 | //pc.printf("stop watchdog"); |
Bossman | 6:bd874c16ce2f | 37 | watchdogflag=Stopped; |
Bossman | 6:bd874c16ce2f | 38 | } |
Bossman | 6:bd874c16ce2f | 39 | } |
WilliamMarshQMUL | 3:32a940251192 | 40 | // ---Thread for controlling LED 1---------------- |
Bossman | 6:bd874c16ce2f | 41 | // Turn LED1 on/off in response to signals |
WilliamMarshQMUL | 3:32a940251192 | 42 | // ----------------------------------------------- |
Bossman | 6:bd874c16ce2f | 43 | void led1_thread() // method to run in thread |
Bossman | 6:bd874c16ce2f | 44 | { |
WilliamMarshQMUL | 3:32a940251192 | 45 | osEvent evt ; |
WilliamMarshQMUL | 3:32a940251192 | 46 | while (true) { |
WilliamMarshQMUL | 3:32a940251192 | 47 | evt = Thread::signal_wait(0x0); // wait for any signal |
WilliamMarshQMUL | 3:32a940251192 | 48 | if (evt.status == osEventSignal) { |
Bossman | 6:bd874c16ce2f | 49 | if (evt.value.signals & 0x01) { |
Bossman | 6:bd874c16ce2f | 50 | led1 = ON ; |
Bossman | 6:bd874c16ce2f | 51 | } |
WilliamMarshQMUL | 3:32a940251192 | 52 | if (evt.value.signals & 0x02) led1 = OFF ; |
Bossman | 6:bd874c16ce2f | 53 | } |
WilliamMarshQMUL | 3:32a940251192 | 54 | } |
WilliamMarshQMUL | 3:32a940251192 | 55 | } |
WilliamMarshQMUL | 3:32a940251192 | 56 | // ---Thread for controlling LED 2---------------- |
Bossman | 6:bd874c16ce2f | 57 | // Turn LED2 on/off in response to signals |
WilliamMarshQMUL | 3:32a940251192 | 58 | // ----------------------------------------------- |
Bossman | 6:bd874c16ce2f | 59 | void led2_thread() // method to run in thread |
Bossman | 6:bd874c16ce2f | 60 | { |
WilliamMarshQMUL | 3:32a940251192 | 61 | osEvent evt ; |
WilliamMarshQMUL | 3:32a940251192 | 62 | while (true) { |
WilliamMarshQMUL | 3:32a940251192 | 63 | evt = Thread::signal_wait(0x0); // wait for any signal |
WilliamMarshQMUL | 3:32a940251192 | 64 | if (evt.status == osEventSignal) { |
Bossman | 6:bd874c16ce2f | 65 | if (evt.value.signals & 0x01) { |
Bossman | 6:bd874c16ce2f | 66 | led2 = ON ; |
Bossman | 6:bd874c16ce2f | 67 | } |
WilliamMarshQMUL | 3:32a940251192 | 68 | if (evt.value.signals & 0x02) led2 = OFF ; |
Bossman | 6:bd874c16ce2f | 69 | } |
WilliamMarshQMUL | 3:32a940251192 | 70 | // waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 71 | } |
WilliamMarshQMUL | 3:32a940251192 | 72 | } |
WilliamMarshQMUL | 3:32a940251192 | 73 | // ---Thread for timing -------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 74 | // Send signals to the other threads |
WilliamMarshQMUL | 3:32a940251192 | 75 | // ----------------------------------------------- |
Bossman | 6:bd874c16ce2f | 76 | void timer_thread() // method to run in thread |
Bossman | 6:bd874c16ce2f | 77 | { |
WilliamMarshQMUL | 3:32a940251192 | 78 | while (true) { |
WilliamMarshQMUL | 3:32a940251192 | 79 | Thread::wait(250) ; |
Bossman | 6:bd874c16ce2f | 80 | threadLED1.signal_set(0x1) ;//on |
WilliamMarshQMUL | 3:32a940251192 | 81 | Thread::wait(250) ; |
Bossman | 6:bd874c16ce2f | 82 | threadLED1.signal_set(0x2) ;//off |
WilliamMarshQMUL | 3:32a940251192 | 83 | // waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 84 | } |
WilliamMarshQMUL | 3:32a940251192 | 85 | } |
Bossman | 6:bd874c16ce2f | 86 | void detection_thread() |
Bossman | 6:bd874c16ce2f | 87 | { |
Bossman | 6:bd874c16ce2f | 88 | int counter=0; |
Bossman | 6:bd874c16ce2f | 89 | while (1) { |
Bossman | 6:bd874c16ce2f | 90 | volt1 = ainled1; |
Bossman | 6:bd874c16ce2f | 91 | if (volt1>0.90) { |
Bossman | 6:bd874c16ce2f | 92 | led_red=0; |
Bossman | 6:bd874c16ce2f | 93 | } else { |
Bossman | 6:bd874c16ce2f | 94 | if(led1==1) { |
Bossman | 6:bd874c16ce2f | 95 | if ((volt1<0.1)) { |
Bossman | 6:bd874c16ce2f | 96 | counter++; |
Bossman | 6:bd874c16ce2f | 97 | if (counter>5) { |
Bossman | 6:bd874c16ce2f | 98 | if(led_red==1)led_red=0; |
Bossman | 6:bd874c16ce2f | 99 | counter =0; |
Bossman | 6:bd874c16ce2f | 100 | } |
Bossman | 6:bd874c16ce2f | 101 | } else { |
Bossman | 6:bd874c16ce2f | 102 | if (led_red==0)led_red=1; |
Bossman | 6:bd874c16ce2f | 103 | } |
Bossman | 6:bd874c16ce2f | 104 | } |
Bossman | 6:bd874c16ce2f | 105 | } |
Bossman | 6:bd874c16ce2f | 106 | waitButton(); // POSSIBLE FAULT HERE |
Bossman | 6:bd874c16ce2f | 107 | } |
Bossman | 6:bd874c16ce2f | 108 | } |
Bossman | 6:bd874c16ce2f | 109 | // -----------MAIN------------------------------- |
Bossman | 6:bd874c16ce2f | 110 | // Configure watchdog. Start threads. |
Bossman | 6:bd874c16ce2f | 111 | // Show start up with RED for 1sec |
Bossman | 6:bd874c16ce2f | 112 | // Remember the watchdog is running |
Bossman | 6:bd874c16ce2f | 113 | // - 1024ms to set it once |
Bossman | 6:bd874c16ce2f | 114 | // - then must feed it every 32ms |
Bossman | 6:bd874c16ce2f | 115 | // ---------------------------------------------- |
Bossman | 6:bd874c16ce2f | 116 | void watchdog() |
Bossman | 6:bd874c16ce2f | 117 | { |
Bossman | 6:bd874c16ce2f | 118 | if (watchdogflag==Working) { |
Bossman | 6:bd874c16ce2f | 119 | wdt_kick_all(); |
Bossman | 6:bd874c16ce2f | 120 | } |
Bossman | 6:bd874c16ce2f | 121 | } |
WilliamMarshQMUL | 3:32a940251192 | 122 | |
Bossman | 6:bd874c16ce2f | 123 | int main(void) |
Bossman | 6:bd874c16ce2f | 124 | { |
Manel_Marin | 0:5ce3cfc57999 | 125 | |
WilliamMarshQMUL | 3:32a940251192 | 126 | wdt_32ms() ; // initialise watchdog - 32ms timeout |
Bossman | 6:bd874c16ce2f | 127 | watchdogflag = Working; |
Bossman | 6:bd874c16ce2f | 128 | tick.attach_us(callback(&watchdog), 20000); |
Bossman | 6:bd874c16ce2f | 129 | |
WilliamMarshQMUL | 3:32a940251192 | 130 | // start threads |
Bossman | 6:bd874c16ce2f | 131 | threadT.start(&timer_thread) ; // start the timer thread |
Bossman | 6:bd874c16ce2f | 132 | threadLED1.start(&led1_thread) ; // start the LED1 control thread |
Bossman | 6:bd874c16ce2f | 133 | threaddetection.start(&detection_thread); |
Bossman | 6:bd874c16ce2f | 134 | |
WilliamMarshQMUL | 1:159a09ac60ba | 135 | // show start-up |
WilliamMarshQMUL | 3:32a940251192 | 136 | led_red = OFF; |
Bossman | 6:bd874c16ce2f | 137 | Thread::wait(1000) ;//1ms |
WilliamMarshQMUL | 3:32a940251192 | 138 | led_red = ON; |
Bossman | 6:bd874c16ce2f | 139 | |
Bossman | 6:bd874c16ce2f | 140 | } |