Stage-1 Students SoCEM
/
Task632-mbedos54
Demonstration of a message queue + memory pool
main.cpp@0:f916cefba2f4, 2016-03-08 (annotated)
- Committer:
- noutram
- Date:
- Tue Mar 08 11:43:39 2016 +0000
- Revision:
- 0:f916cefba2f4
- Child:
- 1:4fb27aea76b2
Task showing thread corruption
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 | #include "rtos.h" |
noutram | 0:f916cefba2f4 | 3 | |
noutram | 0:f916cefba2f4 | 4 | #define RED_DONE 1 |
noutram | 0:f916cefba2f4 | 5 | #define YELLOW_DONE 2 |
noutram | 0:f916cefba2f4 | 6 | |
noutram | 0:f916cefba2f4 | 7 | |
noutram | 0:f916cefba2f4 | 8 | //Function declarations |
noutram | 0:f916cefba2f4 | 9 | void countUP(void const *args); |
noutram | 0:f916cefba2f4 | 10 | void countDOWN(void const *args); |
noutram | 0:f916cefba2f4 | 11 | |
noutram | 0:f916cefba2f4 | 12 | //Digital outputs |
noutram | 0:f916cefba2f4 | 13 | DigitalOut onBoardLED(LED1); |
noutram | 0:f916cefba2f4 | 14 | DigitalOut redLED(D7); |
noutram | 0:f916cefba2f4 | 15 | DigitalOut yellowLED(D6); |
noutram | 0:f916cefba2f4 | 16 | DigitalOut greenLED(D5); |
noutram | 0:f916cefba2f4 | 17 | |
noutram | 0:f916cefba2f4 | 18 | //Digital inputs |
noutram | 0:f916cefba2f4 | 19 | DigitalIn onBoardSwitch(USER_BUTTON); |
noutram | 0:f916cefba2f4 | 20 | DigitalIn SW1(D4); |
noutram | 0:f916cefba2f4 | 21 | DigitalIn SW2(D3); |
noutram | 0:f916cefba2f4 | 22 | |
noutram | 0:f916cefba2f4 | 23 | //Thread object references |
noutram | 0:f916cefba2f4 | 24 | Thread* t1; |
noutram | 0:f916cefba2f4 | 25 | Thread* t2; |
noutram | 0:f916cefba2f4 | 26 | |
noutram | 0:f916cefba2f4 | 27 | //Thread ID for the Main function (CMSIS API) |
noutram | 0:f916cefba2f4 | 28 | osThreadId tidMain; |
noutram | 0:f916cefba2f4 | 29 | |
noutram | 0:f916cefba2f4 | 30 | //Stared mutable state |
noutram | 0:f916cefba2f4 | 31 | volatile long long count = 0; |
noutram | 0:f916cefba2f4 | 32 | |
noutram | 0:f916cefba2f4 | 33 | //Threads |
noutram | 0:f916cefba2f4 | 34 | void countUP(void const *args) |
noutram | 0:f916cefba2f4 | 35 | { |
noutram | 0:f916cefba2f4 | 36 | redLED = 1; |
noutram | 0:f916cefba2f4 | 37 | |
noutram | 0:f916cefba2f4 | 38 | for (unsigned int n=0; n<10000; n++) { |
noutram | 0:f916cefba2f4 | 39 | count++; |
noutram | 0:f916cefba2f4 | 40 | count++; |
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 | } |
noutram | 0:f916cefba2f4 | 50 | |
noutram | 0:f916cefba2f4 | 51 | redLED = 0; |
noutram | 0:f916cefba2f4 | 52 | osSignalSet(tidMain, RED_DONE); //Signal main thread we are done |
noutram | 0:f916cefba2f4 | 53 | } |
noutram | 0:f916cefba2f4 | 54 | |
noutram | 0:f916cefba2f4 | 55 | void countDOWN(void const *args) |
noutram | 0:f916cefba2f4 | 56 | { |
noutram | 0:f916cefba2f4 | 57 | yellowLED = 1; |
noutram | 0:f916cefba2f4 | 58 | |
noutram | 0:f916cefba2f4 | 59 | for (unsigned int n=0; n<10000; n++) { |
noutram | 0:f916cefba2f4 | 60 | count--; |
noutram | 0:f916cefba2f4 | 61 | count--; |
noutram | 0:f916cefba2f4 | 62 | count--; |
noutram | 0:f916cefba2f4 | 63 | count--; |
noutram | 0:f916cefba2f4 | 64 | count--; |
noutram | 0:f916cefba2f4 | 65 | count--; |
noutram | 0:f916cefba2f4 | 66 | count--; |
noutram | 0:f916cefba2f4 | 67 | count--; |
noutram | 0:f916cefba2f4 | 68 | count--; |
noutram | 0:f916cefba2f4 | 69 | count--; |
noutram | 0:f916cefba2f4 | 70 | } |
noutram | 0:f916cefba2f4 | 71 | |
noutram | 0:f916cefba2f4 | 72 | osSignalSet(tidMain, YELLOW_DONE); //Signal main thread we are done |
noutram | 0:f916cefba2f4 | 73 | yellowLED = 0; |
noutram | 0:f916cefba2f4 | 74 | } |
noutram | 0:f916cefba2f4 | 75 | |
noutram | 0:f916cefba2f4 | 76 | |
noutram | 0:f916cefba2f4 | 77 | //Main thread |
noutram | 0:f916cefba2f4 | 78 | int main() { |
noutram | 0:f916cefba2f4 | 79 | redLED = 0; |
noutram | 0:f916cefba2f4 | 80 | yellowLED = 0; |
noutram | 0:f916cefba2f4 | 81 | greenLED = 1; |
noutram | 0:f916cefba2f4 | 82 | |
noutram | 0:f916cefba2f4 | 83 | //Main thread ID |
noutram | 0:f916cefba2f4 | 84 | tidMain = Thread::gettid(); |
noutram | 0:f916cefba2f4 | 85 | |
noutram | 0:f916cefba2f4 | 86 | //Press the switch to run concurrently |
noutram | 0:f916cefba2f4 | 87 | if (onBoardSwitch == 1) { |
noutram | 0:f916cefba2f4 | 88 | printf("Running sequntially\n"); |
noutram | 0:f916cefba2f4 | 89 | countUP(NULL); |
noutram | 0:f916cefba2f4 | 90 | countDOWN(NULL); |
noutram | 0:f916cefba2f4 | 91 | } else { |
noutram | 0:f916cefba2f4 | 92 | printf("Running concurrently\n"); |
noutram | 0:f916cefba2f4 | 93 | Thread t1(countUP); |
noutram | 0:f916cefba2f4 | 94 | Thread t2(countDOWN); |
noutram | 0:f916cefba2f4 | 95 | |
noutram | 0:f916cefba2f4 | 96 | //Wait for the ALL_ON signal |
noutram | 0:f916cefba2f4 | 97 | Thread::signal_wait(RED_DONE,osWaitForever); |
noutram | 0:f916cefba2f4 | 98 | Thread::signal_wait(YELLOW_DONE,osWaitForever); |
noutram | 0:f916cefba2f4 | 99 | } |
noutram | 0:f916cefba2f4 | 100 | |
noutram | 0:f916cefba2f4 | 101 | printf("Final result = %lld\n", count); |
noutram | 0:f916cefba2f4 | 102 | if (count == 0) { |
noutram | 0:f916cefba2f4 | 103 | greenLED = 0; |
noutram | 0:f916cefba2f4 | 104 | } |
noutram | 0:f916cefba2f4 | 105 | while(true); |
noutram | 0:f916cefba2f4 | 106 | } |