Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@8:1f2ac4a0744c, 2020-04-10 (annotated)
- Committer:
- hzsun
- Date:
- Fri Apr 10 00:36:47 2020 +0000
- Revision:
- 8:1f2ac4a0744c
- Parent:
- 7:e49685bca7c7
- Child:
- 9:cec9620ad205
sdad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Manel_Marin | 0:5ce3cfc57999 | 1 | #include "mbed.h" |
WilliamMarshQMUL | 1:159a09ac60ba | 2 | #include "wdt.h" |
hzsun | 8:1f2ac4a0744c | 3 | |
hzsun | 8:1f2ac4a0744c | 4 | // ------------Fault Injection Button------------- |
hzsun | 8:1f2ac4a0744c | 5 | // lab6 first part of the demostration, |
hzsun | 8:1f2ac4a0744c | 6 | //all the functions have been tested |
hzsun | 8:1f2ac4a0744c | 7 | // ----------------------------------------------- |
Manel_Marin | 0:5ce3cfc57999 | 8 | |
hzsun | 8:1f2ac4a0744c | 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); |
hzsun | 8:1f2ac4a0744c | 14 | DigitalOut led2(PTC13, OFF); // all the beginning sets |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 15 | Serial pc(USBTX, USBRX); // tx, rx, useful for debugging |
hzsun | 8:1f2ac4a0744c | 16 | |
WilliamMarshQMUL | 3:32a940251192 | 17 | // This ticker is used to feed the watch dog |
WilliamMarshQMUL | 3:32a940251192 | 18 | Ticker tick; |
hzsun | 8:1f2ac4a0744c | 19 | |
WilliamMarshQMUL | 3:32a940251192 | 20 | // Threads |
WilliamMarshQMUL | 3:32a940251192 | 21 | Thread threadT ; // timer thread |
WilliamMarshQMUL | 3:32a940251192 | 22 | Thread threadLED1 ; // thread LED1 |
WilliamMarshQMUL | 3:32a940251192 | 23 | Thread threadLED2 ; // thread LED2 |
hzsun | 8:1f2ac4a0744c | 24 | |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 25 | # define ON1 0x01 |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 26 | # define ON2 0x02 |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 27 | # define OFF1 0x04 |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 28 | # define OFF2 0x08 |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 29 | EventFlags signals; // event flags for signalling |
hzsun | 8:1f2ac4a0744c | 30 | |
hzsun | 8:1f2ac4a0744c | 31 | |
WilliamMarshQMUL | 3:32a940251192 | 32 | void waitButton() { |
WilliamMarshQMUL | 3:32a940251192 | 33 | while (!button) ; |
WilliamMarshQMUL | 3:32a940251192 | 34 | } |
hzsun | 8:1f2ac4a0744c | 35 | |
WilliamMarshQMUL | 3:32a940251192 | 36 | // ---Thread for controlling LED 1---------------- |
WilliamMarshQMUL | 3:32a940251192 | 37 | // Turn LED1 on/off in response to signals |
WilliamMarshQMUL | 3:32a940251192 | 38 | // ----------------------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 39 | void led1_thread() { // method to run in thread |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 40 | int evt ; |
WilliamMarshQMUL | 3:32a940251192 | 41 | while (true) { |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 42 | evt = signals.wait_any(ON1 | OFF1); // wait for either signal |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 43 | if (evt & ON1) led1 = ON ; |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 44 | if (evt & OFF1) led1 = OFF ; |
hzsun | 8:1f2ac4a0744c | 45 | wdt_kickA(); |
hzsun | 8:1f2ac4a0744c | 46 | //waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 47 | } |
WilliamMarshQMUL | 3:32a940251192 | 48 | } |
hzsun | 8:1f2ac4a0744c | 49 | |
WilliamMarshQMUL | 3:32a940251192 | 50 | // ---Thread for controlling LED 2---------------- |
WilliamMarshQMUL | 3:32a940251192 | 51 | // Turn LED2 on/off in response to signals |
WilliamMarshQMUL | 3:32a940251192 | 52 | // ----------------------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 53 | void led2_thread() { // method to run in thread |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 54 | int evt ; |
WilliamMarshQMUL | 3:32a940251192 | 55 | while (true) { |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 56 | evt = signals.wait_any(ON2 | OFF2); // wait for any signal |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 57 | if (evt & ON2) led2 = ON ; |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 58 | if (evt & OFF2) led2 = OFF ; |
hzsun | 8:1f2ac4a0744c | 59 | wdt_kickB(); |
hzsun | 8:1f2ac4a0744c | 60 | //waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 61 | } |
WilliamMarshQMUL | 3:32a940251192 | 62 | } |
hzsun | 8:1f2ac4a0744c | 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 | 7:e49685bca7c7 | 69 | ThisThread::sleep_for(1000) ; |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 70 | signals.set(ON1 | ON2) ; |
WilliamMarshQMUL | 7:e49685bca7c7 | 71 | ThisThread::sleep_for(1000) ; |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 72 | signals.set(OFF1 | OFF2) ; |
hzsun | 8:1f2ac4a0744c | 73 | waitButton() ; // POSSIBLE FAULT HERE |
WilliamMarshQMUL | 3:32a940251192 | 74 | } |
WilliamMarshQMUL | 3:32a940251192 | 75 | } |
hzsun | 8:1f2ac4a0744c | 76 | |
WilliamMarshQMUL | 3:32a940251192 | 77 | // -----------MAIN------------------------------- |
WilliamMarshQMUL | 3:32a940251192 | 78 | // Configure watchdog. Start threads. |
WilliamMarshQMUL | 3:32a940251192 | 79 | // Show start up with RED for 1sec |
WilliamMarshQMUL | 3:32a940251192 | 80 | // Remember the watchdog is running |
WilliamMarshQMUL | 3:32a940251192 | 81 | // - 1024ms to set it once |
WilliamMarshQMUL | 3:32a940251192 | 82 | // - then must feed it every 32ms |
WilliamMarshQMUL | 3:32a940251192 | 83 | // ---------------------------------------------- |
hzsun | 8:1f2ac4a0744c | 84 | |
Manel_Marin | 0:5ce3cfc57999 | 85 | int main(void) { |
hzsun | 8:1f2ac4a0744c | 86 | wdt_1sec() ; // initialise watchdog - 1sec timeout |
hzsun | 8:1f2ac4a0744c | 87 | wdt_kick_all() ; |
WilliamMarshQMUL | 3:32a940251192 | 88 | // start threads |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 89 | threadT.start(timer_thread) ; // start the timer thread |
WilliamMarshQMUL | 6:8b0ca28c88a4 | 90 | threadLED1.start(led1_thread) ; // start the LED1 control thread |
hzsun | 8:1f2ac4a0744c | 91 | threadLED2.start(led2_thread) ; // start the LED2 control thread |
WilliamMarshQMUL | 1:159a09ac60ba | 92 | // show start-up |
WilliamMarshQMUL | 3:32a940251192 | 93 | led_red = OFF; |
WilliamMarshQMUL | 7:e49685bca7c7 | 94 | ThisThread::sleep_for(5000) ; |
WilliamMarshQMUL | 3:32a940251192 | 95 | led_red = ON; |
WilliamMarshQMUL | 7:e49685bca7c7 | 96 | |
WilliamMarshQMUL | 7:e49685bca7c7 | 97 | // main thread does nothing more |
WilliamMarshQMUL | 7:e49685bca7c7 | 98 | while (true) { |
hzsun | 8:1f2ac4a0744c | 99 | ThisThread::sleep_for(10000) ; //delay |
WilliamMarshQMUL | 7:e49685bca7c7 | 100 | } |
Manel_Marin | 0:5ce3cfc57999 | 101 | } |