Stage-1 Students SoCEM / Mbed 2 deprecated Task622

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 #include "string.h"
00004 #include <stdio.h>
00005 #include <ctype.h>
00006 
00007 #define SWITCH1_RELEASE 1
00008 
00009 void thread1( const void*  );
00010 void thread2( const void*  );
00011 void switchISR();
00012 
00013 //Digital outputs
00014 DigitalOut onBoardLED(LED1);
00015 DigitalOut redLED(D7);
00016 DigitalOut yellowLED(D6);
00017 DigitalOut greenLED(D5);
00018 
00019 //Serial Interface
00020 Serial pc(USBTX, USBRX);
00021 
00022 //Digital inputs
00023 DigitalIn  onBoardSwitch(USER_BUTTON);
00024 DigitalIn  sw1(D4);                         //CONSIDER CHANGING THIS TO AN INTERRUPT
00025 DigitalIn  sw2(D3);
00026 
00027 //Threads
00028 Thread *t1;
00029 Thread *t2;
00030 
00031 //Thread ID for the Main function (CMSIS API)
00032 osThreadId tidMain;
00033 osThreadId tid1;
00034 osThreadId tid2;
00035 
00036 
00037 //TBD: Call this on the falling edge of SW1
00038 void switchISR() {
00039      //TBD
00040 }
00041 
00042 //High priority thread
00043 void thread1( const void* arg ) 
00044 {
00045     redLED = 1;
00046     while (true) {
00047          
00048          // THIS IS BAD BAD BAD BAD BAD BAD
00049          // BLOCK ON FALLING EDGE OF SW1 BY RAPID POLLING (SPINNING)
00050          // Thread is blocked in the RUNNING state 
00051          while (sw1 == 0);
00052          wait_ms(200);      //Wait for debounce
00053          while (sw1 == 1);
00054          // TODO: FIX THIS! GET THE INTERRUPT TO SIGNAL THIS THREAD
00055          
00056          redLED = !redLED;
00057          Thread::wait(1000);    //Thread in WAITING state
00058          redLED = !redLED;
00059          Thread::wait(1000);    //Thread in WAITING state
00060     }
00061 }
00062 
00063 // This thread has normal priority 
00064 // It is supposed to flash the green LED every second.
00065 // THIS IS NOT WORKING because it is currently being starved by thread1 (while polling the switch)
00066 void thread2( const void* arg ) 
00067 {
00068     greenLED = 1; 
00069     while (true) {   
00070         Thread::wait(500);
00071         greenLED = !greenLED;
00072     }
00073 }
00074 
00075 
00076 // Main thread
00077 int main() {
00078     redLED    = 0;
00079     yellowLED = 0;
00080     greenLED  = 0;
00081            
00082     //Threads
00083     t1 = new Thread(&thread1, NULL, osPriorityRealtime);    //HIGH PRIORITY
00084     t2 = new Thread(&thread2, NULL, osPriorityNormal);              
00085               
00086     // Thread IDs
00087     tidMain = Thread::gettid();  
00088     tid1    = t1->gettid();
00089     tid2    = t2->gettid();
00090     
00091     //TBD: Hook up interrupt    
00092         
00093     pc.printf("Main Thread\n");
00094     while (true) {
00095         Thread::wait(osWaitForever);
00096     }
00097 }
00098 
00099