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 #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( const void* arg ) 00033 { 00034 pc.printf("Entering thread 1\n"); 00035 while (true) { 00036 00037 //Start critical section 00038 lock1.lock(); 00039 00040 sw1Count++; 00041 printf("\nCount1 = %lu", sw1Count); 00042 00043 //Thread::wait(1); //1ms 00044 00045 if (SW1 == 1) { 00046 yellowLED = 1; 00047 lock2.lock(); 00048 sw2Count--; 00049 lock2.unlock(); 00050 yellowLED = 0; 00051 } 00052 00053 //End critical section 00054 lock1.unlock(); 00055 00056 Thread::wait(DELAY); 00057 } 00058 } 00059 00060 void thread2( const void* arg ) 00061 { 00062 pc.printf("Entering thread 2\n"); 00063 while (true) { 00064 00065 //Start critical section 00066 lock2.lock(); 00067 00068 sw2Count++; 00069 printf("\nCount2 = %lu", sw2Count); 00070 00071 //Thread::wait(1); //1ms 00072 00073 if (SW2 == 1) { 00074 redLED = 1; 00075 lock1.lock(); 00076 sw1Count--; 00077 lock1.unlock(); 00078 redLED = 0; 00079 } 00080 //End critical section 00081 lock2.unlock(); 00082 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(thread1); 00099 Thread t2(thread2); 00100 00101 pc.printf("Main Thread\n"); 00102 while (true) { 00103 Thread::wait(osWaitForever); 00104 } 00105 00106 } 00107 00108
Generated on Mon Aug 22 2022 22:20:36 by
1.7.2