Threading of mbed os running in LPC4337 xplorer kit

Dependencies:   ST7567

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * @brief Simple threading 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 /* create a semaphore to synchronize the threads */
00027 Semaphore sync_sema;
00028 
00029 /* creates a instance of display */
00030 ST7567  disp(D11, D13, D12, D9, D10);
00031 
00032 
00033 
00034 
00035 /**
00036  * @brief real time prio task function 
00037  */
00038 static void rt_task(void) {
00039     const char rt_message[] = {"Real time prio Task: \0"};
00040     
00041     disp.locate(0, FONT_HEIGHT * 2);        
00042     
00043     disp.printf(rt_message);
00044     
00045     for(;;) {
00046         
00047         /* take the semaphore allowing less priority
00048          * tasks to run 
00049          */
00050          disp.locate(LCD_WIDTH - (sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2);
00051          disp.printf("WAIT!");
00052          sync_sema.wait(0);
00053         
00054          /* show the message by 1 second */
00055          disp.locate(LCD_WIDTH - (sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2);
00056          disp.printf("EXEC!");  
00057 
00058          disp.locate(LCD_WIDTH - (sizeof(rt_message) * FONT_WIDTH), FONT_HEIGHT * 2);
00059          disp.printf("DELAY");
00060          Thread::wait(50);  
00061                 
00062     }
00063     
00064 }
00065 
00066 /**
00067  * @brief high prio task function 
00068  */
00069 static void hp_task(void){
00070     const char hp_message[] = {"High priority Task: \0"};
00071     
00072     disp.locate(0, FONT_HEIGHT * 3);
00073     disp.printf(hp_message);
00074     
00075     for(;;) {
00076         /* take the semaphore allowing less priority
00077          * tasks to run 
00078          */
00079          disp.locate(LCD_WIDTH - (sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3);
00080          disp.printf("WAIT!");
00081          sync_sema.wait(0);
00082         
00083          /* show the message by 1 second */
00084          disp.locate(LCD_WIDTH - (sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3);
00085          disp.printf("EXEC!");  
00086 
00087          disp.locate(LCD_WIDTH - (sizeof(hp_message) * FONT_WIDTH), FONT_HEIGHT * 3);
00088          disp.printf("DELAY");
00089          Thread::wait(50);  
00090 
00091     }
00092 }
00093 
00094 /**
00095  * @brief normal prio task function 
00096  */
00097 static void np_task(void) {
00098     
00099     const char lp_message[] = {"Low priority Task: \0"};
00100     
00101     disp.locate(0, FONT_HEIGHT * 4);
00102     disp.printf(lp_message);
00103 
00104     
00105     for(;;) {
00106          /* show the message by 1 second */
00107          disp.locate(LCD_WIDTH - (sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
00108          disp.printf("EXEC!");  
00109 
00110          disp.locate(LCD_WIDTH - (sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
00111          disp.printf("DELAY");
00112          Thread::wait(50);  
00113          
00114          /* release the semaphore and syncrhonize the tasks */
00115          disp.locate(LCD_WIDTH - (sizeof(lp_message) * FONT_WIDTH), FONT_HEIGHT * 4);
00116          disp.printf("WAIT!");
00117          sync_sema.release();
00118 
00119     }
00120 }
00121 
00122 /**
00123  * @brief main loop 
00124  */
00125 int main(void) {
00126     const char banner[] = {"Embarcados MBED-OS Thread demo\0"};
00127 
00128     /* configures the display */
00129     disp.cls();
00130     disp.set_contrast(0x35);
00131     disp.locate((LCD_WIDTH - (sizeof(banner) * FONT_WIDTH)) / 2, 0);
00132     disp.printf(banner);
00133 
00134 
00135     /* starts the three tasks */
00136     real_time_thread.start(rt_task);   
00137     high_prio_thread.start(hp_task);   
00138     low_prio_thread.start(np_task);   
00139 
00140 
00141     return 0;
00142 }
00143