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 Task617-mbedos54 by
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "string.h" 00004 #include <stdio.h> 00005 #include <ctype.h> 00006 00007 #define DELAY 200 00008 00009 //Digital outputs 00010 DigitalOut onBoardLED(LED1); 00011 DigitalOut redLED(D7); 00012 DigitalOut yellowLED(D6); 00013 DigitalOut greenLED(D5); 00014 00015 //Serial Interface 00016 Serial pc(USBTX, USBRX); 00017 00018 //Digital inputs 00019 DigitalIn onBoardSwitch(USER_BUTTON); 00020 DigitalIn SW1(D4); 00021 DigitalIn SW2(D3); 00022 00023 //Thread ID for the Main function (CMSIS API) 00024 osThreadId tidMain; 00025 00026 //Thread sychronisation primatives 00027 Mutex lock1; 00028 Mutex lock2; 00029 unsigned long sw1Count = 0; 00030 unsigned long sw2Count = 0; 00031 00032 void thread1() 00033 { 00034 pc.printf("Entering thread 1\n"); 00035 while (true) { 00036 yellowLED = 1; 00037 00038 //Start critical section 00039 lock1.lock(); 00040 00041 sw1Count++; 00042 printf("\nCount1 = %lu", sw1Count); 00043 00044 //Thread::wait(1); //1ms 00045 00046 if (SW1 == 1) { 00047 lock2.lock(); 00048 sw2Count--; 00049 lock2.unlock(); 00050 } 00051 00052 //End critical section 00053 lock1.unlock(); 00054 00055 yellowLED = 0; 00056 Thread::wait(DELAY); 00057 } 00058 } 00059 00060 void thread2() 00061 { 00062 pc.printf("Entering thread 2\n"); 00063 while (true) { 00064 redLED = 1; 00065 00066 //Start critical section 00067 lock2.lock(); 00068 00069 sw2Count++; 00070 printf("\nCount2 = %lu", sw2Count); 00071 00072 //Thread::wait(1); //1ms 00073 00074 if (SW2 == 1) { 00075 lock1.lock(); 00076 sw1Count--; 00077 lock1.unlock(); 00078 } 00079 //End critical section 00080 lock2.unlock(); 00081 00082 redLED = 0; 00083 Thread::wait(DELAY); 00084 } 00085 } 00086 00087 00088 //Main thread 00089 int main() { 00090 redLED = 0; 00091 yellowLED = 0; 00092 greenLED = 0; 00093 00094 //Main thread ID 00095 tidMain = Thread::gettid(); 00096 00097 //Threads 00098 Thread t1, t2; 00099 00100 t1.start(thread1); 00101 t2.start(thread2); 00102 00103 pc.printf("Main Thread\n"); 00104 while (true) { 00105 Thread::wait(osWaitForever); 00106 } 00107 00108 } 00109 00110
Generated on Sun Jul 24 2022 11:45:23 by
1.7.2