Search Code
About CoOS

Published 03 Dec 2010.

Last change message: Some basic LED-Flashing works in the CoOS-RTOS using Tasks

Import this program

CoOS

Published 03 Dec 2010, by   user Eric Ebert   tag CoOS, rtos
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <CoOS.h>
00003 
00004 Serial pc(USBTX, USBRX);
00005 
00006 /*---------------------------------- Define  ---------------------------------*/
00007 #define STK_SIZE        128                /*!< Stack size.                    */
00008 #define INIT_TASK_PRIO    11                  /*!< Priority of "init_task" task.    */
00009 #define A_TASK_PRIO        10                  /*!< Priority of "A" task.            */
00010 #define B_TASK_PRIO        10                  /*!< Priority of "B" task.            */
00011 #define CLK_TASK_PRIO    10                  /*!< Priority of "clock" task.        */
00012 
00013 /*---------------------------------- Variable Define -------------------------*/
00014 OS_STK    init_task_stk[STK_SIZE];        /*!< The stack of "init_task" task.    */
00015 OS_STK    a_task_stk[STK_SIZE];            /*!< The stack of "a" task.            */
00016 OS_STK    b_task_stk[STK_SIZE];            /*!< The stack of "b" task.            */
00017 OS_STK    clk_task_stk[STK_SIZE];            /*!< The stack of "clcok" task.        */
00018 
00019 OS_MutexID    mut_LED;                    /*!< Mutex id related to LED.        */                        
00020 
00021 OS_FlagID    a_flg;                                 
00022 OS_FlagID    b_flg;
00023 U8            errInfo[32];                /*!< Save error code.                */
00024 
00025 
00026 DigitalOut myled(LED1);
00027 
00028 /**
00029  *******************************************************************************
00030  * @brief        Switch on LED      
00031  * @param[in]    None      
00032  * @param[out]  None     
00033  * @retval        None 
00034  * @par Description
00035  * @details           This function use to Switch on specify led in LCD.
00036  *******************************************************************************
00037  */
00038 void LED_On  () 
00039 {
00040     CoEnterMutexSection(mut_LED);
00041     myled =1;
00042     CoLeaveMutexSection(mut_LED);
00043 }
00044 
00045 /**
00046  *******************************************************************************
00047  * @brief        Switch off LED      
00048  * @param[in]    None       
00049  * @param[out]  None     
00050  * @retval        None 
00051  * @par Description
00052  * @details           This function use to Switch off specify led in LCD.
00053  *******************************************************************************
00054  */
00055 void LED_Off () {
00056       
00057     CoEnterMutexSection(mut_LED);
00058     myled = 0;
00059     CoLeaveMutexSection(mut_LED);
00060 }
00061 
00062 
00063 /**
00064  *******************************************************************************
00065  * @brief        Function 'signal_func' called from multiple tasks     
00066  * @param[in]    None      
00067  * @param[out]  None     
00068  * @retval        None 
00069  * @par Description
00070  * @details           This function use to Set flag for multiple tasks.
00071  *******************************************************************************
00072  */
00073 void signal_func (const char* fromString)  
00074 {
00075 
00076     pc.printf(fromString);
00077     CoSetFlag(b_flg);
00078     CoTickDelay(50);
00079     CoSetFlag(b_flg);
00080     CoTickDelay(50);
00081     CoSetFlag(a_flg);
00082     CoTickDelay(50);
00083 }
00084 
00085 
00086 
00087 /**
00088  *******************************************************************************
00089  * @brief        Task 1 'phaseA': Phase A output     
00090  * @param[in]     pdata    A pointer to parameter passed to task.     
00091  * @param[out]    None   
00092  * @retval        None              
00093  *******************************************************************************
00094  */
00095 void taskA (void *pdata) {
00096    for (;;) {
00097         CoWaitForSingleFlag(a_flg,0);
00098         LED_On();
00099         signal_func("\r\nSignal from Task A");                     /*!< call common signal function */
00100         LED_Off();
00101       }
00102 }
00103 
00104 
00105 /**
00106  *******************************************************************************
00107  * @brief        Task 2 'phaseB': Phase B output     
00108  * @param[in]     pdata    A pointer to parameter passed to task.     
00109  * @param[out]    None   
00110  * @retval        None              
00111  *******************************************************************************
00112  */
00113 void taskB (void *pdata) {
00114       for (;;) {
00115     CoWaitForSingleFlag(a_flg,0);
00116     LED_On ();
00117     signal_func("\r\nSignal from Task B");                          /*!< call common signal function */
00118     LED_Off();
00119   }
00120 }
00121 
00122 
00123 
00124 /**
00125  *******************************************************************************
00126  * @brief        Task 5 'clock': Signal Clock     
00127  * @param[in]     pdata    A pointer to parameter passed to task.     
00128  * @param[out]    None   
00129  * @retval        None              
00130  *******************************************************************************
00131  */
00132 void clock (void *pdata) {
00133        for (;;) {
00134       CoWaitForSingleFlag(b_flg,0);
00135     LED_On ();
00136       CoTickDelay(10);
00137     LED_Off();
00138  }
00139 }
00140 
00141 
00142 /**
00143  *******************************************************************************
00144  * @brief        "init_task" task code     
00145  * @param[in]     pdata    A pointer to parameter passed to task.     
00146  * @param[out]    None   
00147  * @retval        None         
00148  * @par Description
00149  * @details        This task use to create other tasks, all mutexs and flags,and so on.     
00150  *******************************************************************************
00151  */
00152 void init_task (void *pdata)
00153 {
00154     pdata = pdata;
00155  
00156     mut_LED = CoCreateMutex();
00157     a_flg    = CoCreateFlag(TRUE,0);    
00158        b_flg   = CoCreateFlag(TRUE,0);
00159 
00160     CoCreateTask(taskA,0,A_TASK_PRIO,&a_task_stk[STK_SIZE-1],STK_SIZE);
00161     CoCreateTask(taskB,0,B_TASK_PRIO,&b_task_stk[STK_SIZE-1],STK_SIZE); 
00162     CoCreateTask(clock,0,CLK_TASK_PRIO,&clk_task_stk[STK_SIZE-1],STK_SIZE);
00163     CoSetFlag(a_flg);
00164     CoExitTask();                                /*!< Delete "init_task" task. */             
00165 }
00166 
00167 
00168 /**
00169  *******************************************************************************
00170  * @brief        main function       
00171  * @param[in]     None     
00172  * @param[out]     None  
00173  * @retval        None     
00174  *******************************************************************************
00175  */
00176 int main()
00177 {
00178       //SystemInit();                           /*!< Initialize the MCU clocks  */
00179 
00180     pc.printf ("\r\n");
00181     pc.printf ("\r\nCooCox RTOS Demo\n");
00182     CoInitOS();                              /*!< Initial CooCox RTOS.       */    
00183     pc.printf ("\r\n CoInitOS done.");
00184     CoCreateTask( init_task,0,INIT_TASK_PRIO,&init_task_stk[STK_SIZE-1],STK_SIZE);
00185     pc.printf ("\r\n CoCreateTask  init_task done.");
00186     CoStartOS();
00187     pc.printf ("\r\n CoStartOS done.");
00188     
00189     while (1);
00190 }