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
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 #define RED_DONE 1 00005 #define YELLOW_DONE 2 00006 00007 00008 //Function declarations 00009 void countUP(void const *args); 00010 void countDOWN(void const *args); 00011 00012 //Digital outputs 00013 DigitalOut onBoardLED(LED1); 00014 DigitalOut redLED(D7); 00015 DigitalOut yellowLED(D6); 00016 DigitalOut greenLED(D5); 00017 00018 //Digital inputs 00019 DigitalIn onBoardSwitch(USER_BUTTON); 00020 DigitalIn SW1(D4); 00021 DigitalIn SW2(D3); 00022 00023 //Thread object references 00024 Thread* t1; 00025 Thread* t2; 00026 00027 //Thread ID for the Main function (CMSIS API) 00028 osThreadId tidMain; 00029 00030 //Stared mutable state 00031 volatile long long count = 0; 00032 00033 //Threads 00034 void countUP(void const *args) 00035 { 00036 redLED = 1; 00037 00038 for (unsigned int n=0; n<10000; n++) { 00039 count++; 00040 count++; 00041 count++; 00042 count++; 00043 count++; 00044 count++; 00045 count++; 00046 count++; 00047 count++; 00048 count++; 00049 } 00050 00051 redLED = 0; 00052 osSignalSet(tidMain, RED_DONE); //Signal main thread we are done 00053 } 00054 00055 void countDOWN(void const *args) 00056 { 00057 yellowLED = 1; 00058 00059 for (unsigned int n=0; n<10000; n++) { 00060 count--; 00061 count--; 00062 count--; 00063 count--; 00064 count--; 00065 count--; 00066 count--; 00067 count--; 00068 count--; 00069 count--; 00070 } 00071 00072 osSignalSet(tidMain, YELLOW_DONE); //Signal main thread we are done 00073 yellowLED = 0; 00074 } 00075 00076 00077 //Main thread 00078 int main() { 00079 redLED = 0; 00080 yellowLED = 0; 00081 greenLED = 1; 00082 00083 //Main thread ID 00084 tidMain = Thread::gettid(); 00085 00086 //Press the switch to run concurrently 00087 if (onBoardSwitch == 1) { 00088 printf("Running sequntially\n"); 00089 countUP(NULL); 00090 countDOWN(NULL); 00091 } else { 00092 printf("Running concurrently\n"); 00093 Thread t1(countUP); 00094 Thread t2(countDOWN); 00095 00096 //Wait for the ALL_ON signal 00097 Thread::signal_wait(RED_DONE,osWaitForever); 00098 Thread::signal_wait(YELLOW_DONE,osWaitForever); 00099 } 00100 00101 printf("Final result = %lld\n", count); 00102 if (count == 0) { 00103 greenLED = 0; 00104 } 00105 while(true); 00106 }
Generated on Sat Jul 16 2022 02:43:17 by
1.7.2