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.
Fork of Task615Solution-mbedos54 by
main.cpp@2:91200df5ccd6, 2017-03-30 (annotated)
- Committer:
- noutram
- Date:
- Thu Mar 30 14:24:24 2017 +0000
- Revision:
- 2:91200df5ccd6
- Parent:
- 1:4fb27aea76b2
updated for mbed 5.4
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
| noutram | 0:f916cefba2f4 | 2 | |
| noutram | 0:f916cefba2f4 | 3 | #define RED_DONE 1 |
| noutram | 0:f916cefba2f4 | 4 | #define YELLOW_DONE 2 |
| noutram | 0:f916cefba2f4 | 5 | |
| noutram | 0:f916cefba2f4 | 6 | |
| noutram | 0:f916cefba2f4 | 7 | //Function declarations |
| noutram | 2:91200df5ccd6 | 8 | void countUP(); |
| noutram | 2:91200df5ccd6 | 9 | void countDOWN(); |
| noutram | 0:f916cefba2f4 | 10 | |
| noutram | 0:f916cefba2f4 | 11 | //Digital outputs |
| noutram | 0:f916cefba2f4 | 12 | DigitalOut onBoardLED(LED1); |
| noutram | 0:f916cefba2f4 | 13 | DigitalOut redLED(D7); |
| noutram | 0:f916cefba2f4 | 14 | DigitalOut yellowLED(D6); |
| noutram | 0:f916cefba2f4 | 15 | DigitalOut greenLED(D5); |
| noutram | 0:f916cefba2f4 | 16 | |
| noutram | 0:f916cefba2f4 | 17 | //Digital inputs |
| noutram | 0:f916cefba2f4 | 18 | DigitalIn onBoardSwitch(USER_BUTTON); |
| noutram | 0:f916cefba2f4 | 19 | DigitalIn SW1(D4); |
| noutram | 0:f916cefba2f4 | 20 | DigitalIn SW2(D3); |
| noutram | 0:f916cefba2f4 | 21 | |
| noutram | 1:4fb27aea76b2 | 22 | //MUTEX Lock |
| noutram | 1:4fb27aea76b2 | 23 | Mutex *countLock; |
| noutram | 0:f916cefba2f4 | 24 | |
| noutram | 0:f916cefba2f4 | 25 | //Thread ID for the Main function (CMSIS API) |
| noutram | 0:f916cefba2f4 | 26 | osThreadId tidMain; |
| noutram | 0:f916cefba2f4 | 27 | |
| noutram | 0:f916cefba2f4 | 28 | //Stared mutable state |
| noutram | 0:f916cefba2f4 | 29 | volatile long long count = 0; |
| noutram | 0:f916cefba2f4 | 30 | |
| noutram | 0:f916cefba2f4 | 31 | //Threads |
| noutram | 2:91200df5ccd6 | 32 | void countUP() |
| noutram | 0:f916cefba2f4 | 33 | { |
| noutram | 0:f916cefba2f4 | 34 | redLED = 1; |
| noutram | 0:f916cefba2f4 | 35 | |
| noutram | 0:f916cefba2f4 | 36 | for (unsigned int n=0; n<10000; n++) { |
| noutram | 1:4fb27aea76b2 | 37 | //Take lock |
| noutram | 1:4fb27aea76b2 | 38 | countLock->lock(); |
| noutram | 1:4fb27aea76b2 | 39 | |
| noutram | 1:4fb27aea76b2 | 40 | //Critical section(s) |
| noutram | 0:f916cefba2f4 | 41 | count++; |
| noutram | 0:f916cefba2f4 | 42 | count++; |
| noutram | 0:f916cefba2f4 | 43 | count++; |
| noutram | 0:f916cefba2f4 | 44 | count++; |
| noutram | 0:f916cefba2f4 | 45 | count++; |
| noutram | 0:f916cefba2f4 | 46 | count++; |
| noutram | 0:f916cefba2f4 | 47 | count++; |
| noutram | 0:f916cefba2f4 | 48 | count++; |
| noutram | 0:f916cefba2f4 | 49 | count++; |
| noutram | 0:f916cefba2f4 | 50 | count++; |
| noutram | 1:4fb27aea76b2 | 51 | |
| noutram | 1:4fb27aea76b2 | 52 | //Release lock |
| noutram | 1:4fb27aea76b2 | 53 | countLock->unlock(); |
| noutram | 0:f916cefba2f4 | 54 | } |
| noutram | 0:f916cefba2f4 | 55 | |
| noutram | 0:f916cefba2f4 | 56 | redLED = 0; |
| noutram | 0:f916cefba2f4 | 57 | osSignalSet(tidMain, RED_DONE); //Signal main thread we are done |
| noutram | 0:f916cefba2f4 | 58 | } |
| noutram | 0:f916cefba2f4 | 59 | |
| noutram | 2:91200df5ccd6 | 60 | void countDOWN() |
| noutram | 0:f916cefba2f4 | 61 | { |
| noutram | 0:f916cefba2f4 | 62 | yellowLED = 1; |
| noutram | 0:f916cefba2f4 | 63 | |
| noutram | 0:f916cefba2f4 | 64 | for (unsigned int n=0; n<10000; n++) { |
| noutram | 1:4fb27aea76b2 | 65 | //Take lock |
| noutram | 1:4fb27aea76b2 | 66 | countLock->lock(); |
| noutram | 1:4fb27aea76b2 | 67 | |
| noutram | 1:4fb27aea76b2 | 68 | //Critical section(s) |
| noutram | 0:f916cefba2f4 | 69 | count--; |
| noutram | 0:f916cefba2f4 | 70 | count--; |
| noutram | 0:f916cefba2f4 | 71 | count--; |
| noutram | 0:f916cefba2f4 | 72 | count--; |
| noutram | 0:f916cefba2f4 | 73 | count--; |
| noutram | 0:f916cefba2f4 | 74 | count--; |
| noutram | 0:f916cefba2f4 | 75 | count--; |
| noutram | 0:f916cefba2f4 | 76 | count--; |
| noutram | 0:f916cefba2f4 | 77 | count--; |
| noutram | 1:4fb27aea76b2 | 78 | count--; |
| noutram | 1:4fb27aea76b2 | 79 | |
| noutram | 1:4fb27aea76b2 | 80 | //Release lock |
| noutram | 1:4fb27aea76b2 | 81 | countLock->unlock(); |
| noutram | 0:f916cefba2f4 | 82 | } |
| noutram | 0:f916cefba2f4 | 83 | |
| noutram | 0:f916cefba2f4 | 84 | osSignalSet(tidMain, YELLOW_DONE); //Signal main thread we are done |
| noutram | 0:f916cefba2f4 | 85 | yellowLED = 0; |
| noutram | 0:f916cefba2f4 | 86 | } |
| noutram | 0:f916cefba2f4 | 87 | |
| noutram | 0:f916cefba2f4 | 88 | |
| noutram | 0:f916cefba2f4 | 89 | //Main thread |
| noutram | 0:f916cefba2f4 | 90 | int main() { |
| noutram | 2:91200df5ccd6 | 91 | Thread t1; |
| noutram | 2:91200df5ccd6 | 92 | Thread t2; |
| noutram | 2:91200df5ccd6 | 93 | |
| noutram | 0:f916cefba2f4 | 94 | redLED = 0; |
| noutram | 0:f916cefba2f4 | 95 | yellowLED = 0; |
| noutram | 0:f916cefba2f4 | 96 | greenLED = 1; |
| noutram | 0:f916cefba2f4 | 97 | |
| noutram | 0:f916cefba2f4 | 98 | //Main thread ID |
| noutram | 0:f916cefba2f4 | 99 | tidMain = Thread::gettid(); |
| noutram | 0:f916cefba2f4 | 100 | |
| noutram | 1:4fb27aea76b2 | 101 | //Create lock |
| noutram | 1:4fb27aea76b2 | 102 | countLock = new Mutex(); |
| noutram | 1:4fb27aea76b2 | 103 | |
| noutram | 0:f916cefba2f4 | 104 | //Press the switch to run concurrently |
| noutram | 0:f916cefba2f4 | 105 | if (onBoardSwitch == 1) { |
| noutram | 0:f916cefba2f4 | 106 | printf("Running sequntially\n"); |
| noutram | 2:91200df5ccd6 | 107 | countUP(); |
| noutram | 2:91200df5ccd6 | 108 | countDOWN(); |
| noutram | 0:f916cefba2f4 | 109 | } else { |
| noutram | 0:f916cefba2f4 | 110 | printf("Running concurrently\n"); |
| noutram | 2:91200df5ccd6 | 111 | t1.start(countUP); |
| noutram | 2:91200df5ccd6 | 112 | t2.start(countDOWN); |
| noutram | 0:f916cefba2f4 | 113 | |
| noutram | 0:f916cefba2f4 | 114 | //Wait for the ALL_ON signal |
| noutram | 0:f916cefba2f4 | 115 | Thread::signal_wait(RED_DONE,osWaitForever); |
| noutram | 0:f916cefba2f4 | 116 | Thread::signal_wait(YELLOW_DONE,osWaitForever); |
| noutram | 0:f916cefba2f4 | 117 | } |
| noutram | 0:f916cefba2f4 | 118 | |
| noutram | 0:f916cefba2f4 | 119 | printf("Final result = %lld\n", count); |
| noutram | 0:f916cefba2f4 | 120 | if (count == 0) { |
| noutram | 0:f916cefba2f4 | 121 | greenLED = 0; |
| noutram | 0:f916cefba2f4 | 122 | } |
| noutram | 1:4fb27aea76b2 | 123 | |
| noutram | 1:4fb27aea76b2 | 124 | //Tidy |
| noutram | 1:4fb27aea76b2 | 125 | delete countLock; |
| noutram | 1:4fb27aea76b2 | 126 | |
| noutram | 0:f916cefba2f4 | 127 | while(true); |
| noutram | 0:f916cefba2f4 | 128 | } |