Lab6 part 1

Committer:
WilliamMarshQMUL
Date:
Wed Feb 27 15:54:07 2019 +0000
Revision:
6:8b0ca28c88a4
Parent:
3:32a940251192
Child:
7:e49685bca7c7
Update library. remove Thread.wait and Thread.signal using event flags instead. ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Manel_Marin 0:5ce3cfc57999 1 #include "mbed.h"
WilliamMarshQMUL 1:159a09ac60ba 2 #include "wdt.h"
Manel_Marin 0:5ce3cfc57999 3
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
WilliamMarshQMUL 3:32a940251192 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
WilliamMarshQMUL 3:32a940251192 10 #define ON 1
WilliamMarshQMUL 3:32a940251192 11 #define OFF 0
WilliamMarshQMUL 3:32a940251192 12 DigitalOut led_red(LED_RED, ON);
WilliamMarshQMUL 3:32a940251192 13 DigitalIn button(PTD0, PullUp);
WilliamMarshQMUL 3:32a940251192 14 DigitalOut led1(PTC12, OFF);
WilliamMarshQMUL 3:32a940251192 15 DigitalOut led2(PTC13, OFF);
WilliamMarshQMUL 3:32a940251192 16
WilliamMarshQMUL 6:8b0ca28c88a4 17 Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
WilliamMarshQMUL 6:8b0ca28c88a4 18
WilliamMarshQMUL 3:32a940251192 19 // This ticker is used to feed the watch dog
WilliamMarshQMUL 3:32a940251192 20 Ticker tick;
WilliamMarshQMUL 3:32a940251192 21
WilliamMarshQMUL 3:32a940251192 22 // Threads
WilliamMarshQMUL 3:32a940251192 23 Thread threadT ; // timer thread
WilliamMarshQMUL 3:32a940251192 24 Thread threadLED1 ; // thread LED1
WilliamMarshQMUL 3:32a940251192 25 Thread threadLED2 ; // thread LED2
WilliamMarshQMUL 3:32a940251192 26
WilliamMarshQMUL 6:8b0ca28c88a4 27 # define ON1 0x01
WilliamMarshQMUL 6:8b0ca28c88a4 28 # define ON2 0x02
WilliamMarshQMUL 6:8b0ca28c88a4 29 # define OFF1 0x04
WilliamMarshQMUL 6:8b0ca28c88a4 30 # define OFF2 0x08
WilliamMarshQMUL 6:8b0ca28c88a4 31 EventFlags signals; // event flags for signalling
WilliamMarshQMUL 6:8b0ca28c88a4 32
WilliamMarshQMUL 6:8b0ca28c88a4 33
WilliamMarshQMUL 3:32a940251192 34 // ------------Fault Injection Button-------------
WilliamMarshQMUL 3:32a940251192 35 // Wait while the button is down
WilliamMarshQMUL 3:32a940251192 36 // Use this to simulate a STUCK fault
WilliamMarshQMUL 3:32a940251192 37 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 38 void waitButton() {
WilliamMarshQMUL 3:32a940251192 39 while (!button) ;
WilliamMarshQMUL 3:32a940251192 40 }
WilliamMarshQMUL 2:c31a1758ac38 41
WilliamMarshQMUL 3:32a940251192 42 // ---Thread for controlling LED 1----------------
WilliamMarshQMUL 3:32a940251192 43 // Turn LED1 on/off in response to signals
WilliamMarshQMUL 3:32a940251192 44 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 45 void led1_thread() { // method to run in thread
WilliamMarshQMUL 6:8b0ca28c88a4 46 int evt ;
WilliamMarshQMUL 3:32a940251192 47 while (true) {
WilliamMarshQMUL 6:8b0ca28c88a4 48 evt = signals.wait_any(ON1 | OFF1); // wait for either signal
WilliamMarshQMUL 6:8b0ca28c88a4 49 if (evt & ON1) led1 = ON ;
WilliamMarshQMUL 6:8b0ca28c88a4 50 if (evt & OFF1) led1 = OFF ;
WilliamMarshQMUL 3:32a940251192 51 waitButton() ; // POSSIBLE FAULT HERE
WilliamMarshQMUL 3:32a940251192 52 }
WilliamMarshQMUL 3:32a940251192 53 }
Manel_Marin 0:5ce3cfc57999 54
WilliamMarshQMUL 3:32a940251192 55 // ---Thread for controlling LED 2----------------
WilliamMarshQMUL 3:32a940251192 56 // Turn LED2 on/off in response to signals
WilliamMarshQMUL 3:32a940251192 57 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 58 void led2_thread() { // method to run in thread
WilliamMarshQMUL 6:8b0ca28c88a4 59 int evt ;
WilliamMarshQMUL 3:32a940251192 60 while (true) {
WilliamMarshQMUL 6:8b0ca28c88a4 61 evt = signals.wait_any(ON2 | OFF2); // wait for any signal
WilliamMarshQMUL 6:8b0ca28c88a4 62 if (evt & ON2) led2 = ON ;
WilliamMarshQMUL 6:8b0ca28c88a4 63 if (evt & OFF2) led2 = OFF ;
WilliamMarshQMUL 3:32a940251192 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 6:8b0ca28c88a4 73 wait(1.0) ;
WilliamMarshQMUL 6:8b0ca28c88a4 74 signals.set(ON1 | ON2) ;
WilliamMarshQMUL 6:8b0ca28c88a4 75 wait(1.0) ;
WilliamMarshQMUL 6:8b0ca28c88a4 76 signals.set(OFF1 | OFF2) ;
WilliamMarshQMUL 3:32a940251192 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) {
WilliamMarshQMUL 3:32a940251192 90 wdt_32ms() ; // initialise watchdog - 32ms timeout
WilliamMarshQMUL 6:8b0ca28c88a4 91 wdt_kick_all() ;
WilliamMarshQMUL 3:32a940251192 92 tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms
WilliamMarshQMUL 1:159a09ac60ba 93
WilliamMarshQMUL 3:32a940251192 94 // start threads
WilliamMarshQMUL 6:8b0ca28c88a4 95 threadT.start(timer_thread) ; // start the timer thread
WilliamMarshQMUL 6:8b0ca28c88a4 96 threadLED1.start(led1_thread) ; // start the LED1 control thread
WilliamMarshQMUL 6:8b0ca28c88a4 97 threadLED2.start(led2_thread) ; // start the LED2 control thread
WilliamMarshQMUL 1:159a09ac60ba 98
WilliamMarshQMUL 1:159a09ac60ba 99 // show start-up
WilliamMarshQMUL 3:32a940251192 100 led_red = OFF;
WilliamMarshQMUL 6:8b0ca28c88a4 101 wait(5.0) ;
WilliamMarshQMUL 3:32a940251192 102 led_red = ON;
WilliamMarshQMUL 6:8b0ca28c88a4 103 // main thread terminates
Manel_Marin 0:5ce3cfc57999 104 }