simple semaphore demo with lpc4337 board

Dependencies:   ST7567 lpc4337_mbed_os_semaphore_demo

Dependents:   lpc4337_mbed_os_semaphore_demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * @brief Simple Semaphore using the mbed os demo
00003  */
00004 #include "mbed.h"
00005 #include "rtos.h"
00006 #include "ST7567.h"
00007 
00008 /* LCD and font parameters */
00009 #define LCD_HEIGHT          64
00010 #define LCD_WIDTH           128
00011 #define FONT_HEIGHT         10
00012 #define FONT_WIDTH          5
00013 
00014 
00015 /* allocate statically stacks for the three threads */
00016 unsigned char rt_stk[1024];
00017 unsigned char hp_stk[1024];
00018 unsigned char lp_stk[1024];
00019 
00020 /* creates three tread objects with different priorities */
00021 Thread real_time_thread(osPriorityRealtime, 1024, &rt_stk[0]);
00022 Thread high_prio_thread(osPriorityHigh, 1024, &hp_stk[0]);
00023 Thread low_prio_thread(osPriorityNormal, 1024, &lp_stk[0]);
00024 
00025 
00026 /* creates semaphores to explore the capabilites of synchronization */
00027 Semaphore rt_sema;
00028 Semaphore hp_sema;
00029 const char rt_message[] = {"rt_task() sema:\0"};
00030 const char hp_message[] = {"hp_task() sema:\0"};
00031 const char lp_message[] = {"np_task() exec:\0"};
00032 
00033 
00034 
00035 /* creates a instance of display */
00036 ST7567  disp(D11, D13, D12, D9, D10);
00037 
00038 
00039 
00040 
00041 /**
00042  * @brief real time prio task function 
00043  */
00044 static void rt_task(void) {
00045 
00046     disp.locate(0, FONT_HEIGHT * 2);            
00047     disp.printf(rt_message);
00048     rt_sema.wait();               
00049     
00050     
00051     for(;;) {
00052             
00053          /* display the semaphore status */
00054          disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
00055          disp.printf("WAIT!");
00056 
00057          disp.locate((sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2);
00058          disp.printf("TOOK!");
00059          
00060          /* put a dumming process in order to consume CPU */
00061          for(int i = 0; i < 0x3FFFFFF; i++) (void)0;
00062          
00063          rt_sema.wait();           
00064     }
00065     
00066 }
00067 
00068 /**
00069  * @brief high prio task function 
00070  */
00071 static void hp_task(void){    
00072     disp.locate(0, FONT_HEIGHT * 3);
00073     disp.printf(hp_message);
00074     hp_sema.wait();           
00075 
00076     
00077     for(;;) {
00078 
00079          /* dsiplay the semaphore status */
00080          disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
00081          disp.printf("WAIT!");
00082 
00083          disp.locate((sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3);
00084          disp.printf("TOOK!");  
00085 
00086          /* put a dumming process in order to consume CPU */
00087          for(int i = 0; i < 0x3FFFFFF; i++) (void)0;
00088 
00089          hp_sema.wait();       
00090     }
00091 }
00092 
00093 /**
00094  * @brief normal prio task function 
00095  */
00096 static void np_task(void) {
00097         
00098     disp.locate(0, FONT_HEIGHT * 4);
00099     disp.printf(lp_message);
00100 
00101     disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
00102     disp.printf("RUNN!");
00103     disp.locate((sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2);
00104     disp.printf("WAIT!");
00105     disp.locate((sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3);
00106     disp.printf("WAIT!");
00107 
00108     
00109     for(;;) {
00110          rt_sema.release();        
00111          disp.locate((sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2);
00112          disp.printf("WAIT!");
00113          disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
00114          disp.printf("RUNN!");
00115          Thread::wait(1000);
00116 
00117 
00118          hp_sema.release();          
00119          disp.locate((sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3);
00120          disp.printf("WAIT!");
00121          disp.locate((sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
00122          disp.printf("RUNN!");
00123          Thread::wait(1000);
00124          
00125     }
00126 }
00127 
00128 /**
00129  * @brief main loop 
00130  */
00131 int main(void) {
00132     const char banner[] = {"Embarcados MBED-OS\0"};
00133 
00134     /* configures the display */
00135     disp.cls();
00136     disp.set_contrast(0x35);
00137     disp.locate((LCD_WIDTH - (sizeof(banner) * FONT_WIDTH)) / 2, (LCD_HEIGHT/2) - (FONT_HEIGHT/2));
00138     disp.printf(banner);
00139     
00140     Thread::wait(2500);
00141     disp.cls();
00142 
00143     /* starts the three tasks */
00144     real_time_thread.start(rt_task);   
00145     high_prio_thread.start(hp_task);   
00146     low_prio_thread.start(np_task);   
00147 
00148 
00149     return 0;
00150 }
00151