Final (not completed)

Committer:
WilliamMarshQMUL
Date:
Wed Mar 01 23:01:51 2017 +0000
Revision:
3:32a940251192
Parent:
2:c31a1758ac38
Child:
6:8b0ca28c88a4
First version

Who changed what in which revision?

UserRevisionLine numberNew 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
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 ;
WilliamMarshQMUL 3:32a940251192 44 }
WilliamMarshQMUL 3:32a940251192 45 waitButton() ; // POSSIBLE FAULT HERE
WilliamMarshQMUL 3:32a940251192 46 }
WilliamMarshQMUL 3:32a940251192 47 }
Manel_Marin 0:5ce3cfc57999 48
WilliamMarshQMUL 3:32a940251192 49 // ---Thread for controlling LED 2----------------
WilliamMarshQMUL 3:32a940251192 50 // Turn LED2 on/off in response to signals
WilliamMarshQMUL 3:32a940251192 51 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 52 void led2_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 53 osEvent evt ;
WilliamMarshQMUL 3:32a940251192 54 while (true) {
WilliamMarshQMUL 3:32a940251192 55 evt = Thread::signal_wait(0x0); // wait for any signal
WilliamMarshQMUL 3:32a940251192 56 if (evt.status == osEventSignal) {
WilliamMarshQMUL 3:32a940251192 57 if (evt.value.signals & 0x01) led2 = ON ;
WilliamMarshQMUL 3:32a940251192 58 if (evt.value.signals & 0x02) led2 = OFF ;
WilliamMarshQMUL 3:32a940251192 59 }
WilliamMarshQMUL 3:32a940251192 60 // waitButton() ; // POSSIBLE FAULT HERE
WilliamMarshQMUL 3:32a940251192 61 }
WilliamMarshQMUL 3:32a940251192 62 }
Manel_Marin 0:5ce3cfc57999 63
WilliamMarshQMUL 3:32a940251192 64 // ---Thread for timing --------------------------
WilliamMarshQMUL 3:32a940251192 65 // Send signals to the other threads
WilliamMarshQMUL 3:32a940251192 66 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 67 void timer_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 68 while (true) {
WilliamMarshQMUL 3:32a940251192 69 Thread::wait(250) ;
WilliamMarshQMUL 3:32a940251192 70 threadLED1.signal_set(0x1) ;
WilliamMarshQMUL 3:32a940251192 71 threadLED2.signal_set(0x1) ;
WilliamMarshQMUL 3:32a940251192 72 Thread::wait(250) ;
WilliamMarshQMUL 3:32a940251192 73 threadLED1.signal_set(0x2) ;
WilliamMarshQMUL 3:32a940251192 74 threadLED2.signal_set(0x2) ;
WilliamMarshQMUL 3:32a940251192 75 // waitButton() ; // POSSIBLE FAULT HERE
WilliamMarshQMUL 3:32a940251192 76 }
WilliamMarshQMUL 3:32a940251192 77 }
WilliamMarshQMUL 3:32a940251192 78
WilliamMarshQMUL 3:32a940251192 79 // -----------MAIN-------------------------------
WilliamMarshQMUL 3:32a940251192 80 // Configure watchdog. Start threads.
WilliamMarshQMUL 3:32a940251192 81 // Show start up with RED for 1sec
WilliamMarshQMUL 3:32a940251192 82 // Remember the watchdog is running
WilliamMarshQMUL 3:32a940251192 83 // - 1024ms to set it once
WilliamMarshQMUL 3:32a940251192 84 // - then must feed it every 32ms
WilliamMarshQMUL 3:32a940251192 85 // ----------------------------------------------
Manel_Marin 0:5ce3cfc57999 86
Manel_Marin 0:5ce3cfc57999 87 int main(void) {
WilliamMarshQMUL 3:32a940251192 88 wdt_32ms() ; // initialise watchdog - 32ms timeout
WilliamMarshQMUL 3:32a940251192 89 tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms
WilliamMarshQMUL 1:159a09ac60ba 90
WilliamMarshQMUL 3:32a940251192 91 // start threads
WilliamMarshQMUL 3:32a940251192 92 threadT.start(&timer_thread) ; // start the timer thread
WilliamMarshQMUL 3:32a940251192 93 threadLED1.start(&led1_thread) ; // start the LED1 control thread
WilliamMarshQMUL 3:32a940251192 94 threadLED2.start(&led2_thread) ; // start the LED2 control thread
WilliamMarshQMUL 1:159a09ac60ba 95
WilliamMarshQMUL 1:159a09ac60ba 96 // show start-up
WilliamMarshQMUL 3:32a940251192 97 led_red = OFF;
WilliamMarshQMUL 3:32a940251192 98 Thread::wait(1000) ;
WilliamMarshQMUL 3:32a940251192 99 led_red = ON;
Manel_Marin 0:5ce3cfc57999 100 }