Stage-1 Students SoCEM / Mbed 2 deprecated Task615Solution

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 //MUTEX Lock
00024 Mutex *countLock;
00025 
00026 //Thread ID for the Main function (CMSIS API)
00027 osThreadId tidMain;
00028 
00029 //Stared mutable state
00030 volatile long long count = 0;
00031 
00032 //Threads
00033 void countUP(void const *args)
00034 {
00035     redLED = 1;
00036     
00037     for (unsigned int n=0; n<10000; n++) {
00038         //Take lock
00039         countLock->lock();
00040         
00041         //Critical section(s)
00042         count++;
00043         count++;        
00044         count++;
00045         count++;    
00046         count++;
00047         count++; 
00048         count++;
00049         count++;
00050         count++;
00051         count++;   
00052         
00053         //Release lock
00054         countLock->unlock();
00055     }
00056     
00057     redLED = 0;
00058     osSignalSet(tidMain, RED_DONE);  //Signal main thread we are done
00059 }
00060 
00061 void countDOWN(void const *args)
00062 {
00063     yellowLED = 1;
00064     
00065     for (unsigned int n=0; n<10000; n++) {
00066         //Take lock
00067         countLock->lock();
00068         
00069         //Critical section(s)        
00070         count--;
00071         count--;        
00072         count--;
00073         count--;    
00074         count--;
00075         count--; 
00076         count--;
00077         count--;
00078         count--;
00079         count--;    
00080         
00081         //Release lock
00082         countLock->unlock();                   
00083     }   
00084     
00085     osSignalSet(tidMain, YELLOW_DONE); //Signal main thread we are done
00086     yellowLED = 0;
00087 }
00088 
00089 
00090 //Main thread
00091 int main() {
00092     redLED    = 0;
00093     yellowLED = 0;
00094     greenLED  = 1;
00095     
00096     //Main thread ID
00097     tidMain = Thread::gettid();  
00098     
00099     //Create lock
00100     countLock = new Mutex();
00101     
00102     //Press the switch to run concurrently
00103     if (onBoardSwitch == 1) {
00104         printf("Running sequntially\n");
00105         countUP(NULL);
00106         countDOWN(NULL);        
00107     } else {
00108         printf("Running concurrently\n");
00109         Thread t1(countUP);           
00110         Thread t2(countDOWN);    
00111   
00112         //Wait for the ALL_ON signal
00113         Thread::signal_wait(RED_DONE,osWaitForever);
00114         Thread::signal_wait(YELLOW_DONE,osWaitForever);        
00115     }
00116 
00117     printf("Final result = %lld\n", count);
00118     if (count == 0) {
00119         greenLED = 0;
00120     }
00121     
00122     //Tidy
00123     delete countLock;
00124     
00125     while(true);
00126 }