Lab 6 part 2

Committer:
ec19664
Date:
Mon Mar 30 14:04:32 2020 +0000
Revision:
8:b7d0e550de66
Parent:
7:e49685bca7c7
Complete

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 ;
ec19664 8:b7d0e550de66 51 wdt_kickA();
ec19664 8:b7d0e550de66 52 //waitButton() ; // POSSIBLE FAULT HERE
WilliamMarshQMUL 3:32a940251192 53 }
WilliamMarshQMUL 3:32a940251192 54 }
Manel_Marin 0:5ce3cfc57999 55
WilliamMarshQMUL 3:32a940251192 56 // ---Thread for controlling LED 2----------------
WilliamMarshQMUL 3:32a940251192 57 // Turn LED2 on/off in response to signals
WilliamMarshQMUL 3:32a940251192 58 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 59 void led2_thread() { // method to run in thread
WilliamMarshQMUL 6:8b0ca28c88a4 60 int evt ;
WilliamMarshQMUL 3:32a940251192 61 while (true) {
WilliamMarshQMUL 6:8b0ca28c88a4 62 evt = signals.wait_any(ON2 | OFF2); // wait for any signal
WilliamMarshQMUL 6:8b0ca28c88a4 63 if (evt & ON2) led2 = ON ;
WilliamMarshQMUL 6:8b0ca28c88a4 64 if (evt & OFF2) led2 = OFF ;
ec19664 8:b7d0e550de66 65 wdt_kickB();
ec19664 8:b7d0e550de66 66 waitButton() ; // POSSIBLE FAULT HERE
WilliamMarshQMUL 3:32a940251192 67 }
WilliamMarshQMUL 3:32a940251192 68 }
Manel_Marin 0:5ce3cfc57999 69
WilliamMarshQMUL 3:32a940251192 70 // ---Thread for timing --------------------------
WilliamMarshQMUL 3:32a940251192 71 // Send signals to the other threads
WilliamMarshQMUL 3:32a940251192 72 // -----------------------------------------------
WilliamMarshQMUL 3:32a940251192 73 void timer_thread() { // method to run in thread
WilliamMarshQMUL 3:32a940251192 74 while (true) {
WilliamMarshQMUL 7:e49685bca7c7 75 ThisThread::sleep_for(1000) ;
WilliamMarshQMUL 6:8b0ca28c88a4 76 signals.set(ON1 | ON2) ;
WilliamMarshQMUL 7:e49685bca7c7 77 ThisThread::sleep_for(1000) ;
WilliamMarshQMUL 6:8b0ca28c88a4 78 signals.set(OFF1 | OFF2) ;
WilliamMarshQMUL 3:32a940251192 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) {
ec19664 8:b7d0e550de66 92 wdt_1sec() ; // initialise watchdog - 32ms timeout
WilliamMarshQMUL 6:8b0ca28c88a4 93 wdt_kick_all() ;
ec19664 8:b7d0e550de66 94 //tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms
WilliamMarshQMUL 1:159a09ac60ba 95
WilliamMarshQMUL 3:32a940251192 96 // start threads
WilliamMarshQMUL 6:8b0ca28c88a4 97 threadT.start(timer_thread) ; // start the timer thread
WilliamMarshQMUL 6:8b0ca28c88a4 98 threadLED1.start(led1_thread) ; // start the LED1 control thread
WilliamMarshQMUL 6:8b0ca28c88a4 99 threadLED2.start(led2_thread) ; // start the LED2 control thread
WilliamMarshQMUL 1:159a09ac60ba 100
WilliamMarshQMUL 1:159a09ac60ba 101 // show start-up
WilliamMarshQMUL 3:32a940251192 102 led_red = OFF;
WilliamMarshQMUL 7:e49685bca7c7 103 ThisThread::sleep_for(5000) ;
WilliamMarshQMUL 3:32a940251192 104 led_red = ON;
WilliamMarshQMUL 7:e49685bca7c7 105
WilliamMarshQMUL 7:e49685bca7c7 106 // main thread does nothing more
WilliamMarshQMUL 7:e49685bca7c7 107 while (true) {
WilliamMarshQMUL 7:e49685bca7c7 108 ThisThread::sleep_for(10000) ;
WilliamMarshQMUL 7:e49685bca7c7 109 }
Manel_Marin 0:5ce3cfc57999 110 }