CooCox 1.1.4 on mbed with simple blinky example

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers core.c Source File

core.c

Go to the documentation of this file.
00001  /**
00002  *******************************************************************************
00003  * @file       core.c
00004  * @version    V1.1.4    
00005  * @date       2011.04.20
00006  * @brief      Core implementation code of CooCox CoOS kernel.  
00007  *******************************************************************************
00008  * @copy
00009  *
00010  * INTERNAL FILE,DON'T PUBLIC.
00011  * 
00012  * <h2><center>&copy; COPYRIGHT 2009 CooCox </center></h2>
00013  *******************************************************************************
00014  */ 
00015 
00016 /*---------------------------- Include ---------------------------------------*/
00017 #include <coocox.h>
00018 
00019 /*---------------------------- Variable Define -------------------------------*/
00020 volatile U8     OSIntNesting   = 0;         /*!< Use to indicate interrupt nesting level*/
00021 volatile U8     OSSchedLock    = 0;         /*!< Task Switch lock.                      */
00022 volatile BOOL   TaskSchedReq  = Co_FALSE;
00023 
00024 
00025 /**
00026  *******************************************************************************
00027  * @brief      Enter a ISR.                        
00028  * @param[in]  None  
00029  * @param[out] None   
00030  * @retval     None  
00031  *
00032  * @par Description
00033  * @details    This function is called to notify OS when enter to an ISR.
00034  *
00035  * @note       When you call API in ISR,you must call CoEnterISR() before your
00036  *             interrupt handler code,and call CoExitISR() after your handler
00037  *             code and before exiting from ISR.     
00038  *******************************************************************************
00039  */
00040 void CoEnterISR(void)
00041 {
00042     Inc8(&OSIntNesting );                /* OSIntNesting increment             */
00043 }
00044 
00045 
00046 /**
00047  *******************************************************************************
00048  * @brief      Exit a ISR.   
00049  * @param[in]  None  
00050  * @param[out] None   
00051  * @retval     None      
00052  *
00053  * @par Description
00054  * @details    This function is called when exit from a ISR.        
00055  *
00056  * @note 
00057  *******************************************************************************
00058  */
00059 void CoExitISR(void)
00060 {
00061     Dec8(&OSIntNesting );                /* OSIntNesting decrease              */
00062     if( OSIntNesting  == 0)              /* Is OSIntNesting == 0?              */
00063     {
00064         if(TaskSchedReq == Co_TRUE)
00065         {
00066             OSSchedLock ++;
00067             Schedule();                 /* Call task schedule                 */
00068             OSSchedLock --;
00069         }
00070     }
00071 }
00072 
00073 
00074 
00075 /**
00076  *******************************************************************************
00077  * @brief      Unlock schedule    
00078  * @param[in]  None      
00079  * @param[out] None   
00080  * @retval     None      
00081  *
00082  * @par Description
00083  * @details   This function is called to unlock schedule(i.e.enable schedule again)          
00084  *
00085  * @note 
00086  *******************************************************************************
00087  */
00088 void OsSchedUnlock(void)
00089 {
00090     if(OSSchedLock  == 1)                /* Is OSSchedLock == 0?               */
00091     {
00092 #if CFG_TASK_WAITTING_EN > 0
00093         if(IsrReq == Co_TRUE)
00094         {
00095             RespondSRQ();               /* Respond service request            */    
00096         }
00097 #endif
00098         /* Judge task state change or higher PRI task coming in               */
00099         if(TaskSchedReq == Co_TRUE)
00100         {
00101             Schedule();                 /* Call task schedule                 */
00102         }
00103         OSSchedLock  = 0;
00104     }
00105     else
00106     {
00107         OSSchedLock --;  
00108     }
00109 }
00110 
00111 
00112 /**
00113  *******************************************************************************
00114  * @brief      Lock schedule     
00115  * @param[in]  None      
00116  * @param[out] None   
00117  * @retval     None      
00118  *
00119  * @par Description
00120  * @details    This function is called in application code to lock schedule.         
00121  *
00122  * @note 
00123  *******************************************************************************
00124  */
00125 void CoSchedLock(void)
00126 {                                       
00127     OsSchedLock();                      /* Lock schedule                      */
00128 }
00129 
00130 
00131 /**
00132  *******************************************************************************
00133  * @brief      Unlock schedule    
00134  * @param[in]  None      
00135  * @param[out] None   
00136  * @retval     None      
00137  *
00138  * @par Description
00139  * @details    This function is called in APP to unlock schedule.        
00140  *
00141  * @note 
00142  *******************************************************************************
00143  */
00144 void CoSchedUnlock(void)
00145 {
00146     OsSchedUnlock();                    /* Unlock schedule                    */
00147 }
00148 
00149 
00150 /**
00151  *******************************************************************************
00152  * @brief      Initialize OS      
00153  * @param[in]  None      
00154  * @param[out] None 
00155  * @retval     None 
00156  *
00157  * @par Description
00158  * @details   This function is called to initialize OS.
00159  *
00160  * @note      You must call this function first,before any other OS API function
00161  *                  
00162  * @code      There is a example for useage of this function,as follows: 
00163  *        e.g.                                                           
00164  *            ...                   // Your target initial code. 
00165  *              
00166  *            OsInit();             // Initial OS.              
00167  *            CreateTask(...);      // Create tasks.                
00168  *            ...
00169  *            OsStart();            // Start multitask.
00170  * @endcode 
00171  *******************************************************************************        
00172  */
00173 void CoInitOS(void)
00174 {
00175     InitSysTick();                /* Initialize system tick.                  */
00176     InitInt();                    /* Initialize PendSV,SVC,SysTick interrupt  */    
00177     CreateTCBList();              /* Create TCB list.                         */   
00178 #if CFG_EVENT_EN > 0                    
00179     CreateEventList();            /* Create event control list.               */
00180 #endif  
00181 #if CFG_KHEAP_EN > 0
00182     CoCreateKheap();              /* Create kernel heap within user define    */
00183 #endif   
00184     OsSchedLock();                /* Lock Schedule                            */ 
00185                                   /* Create first task -- IDLE task.          */ 
00186     CoCreateTask(                      CoIdleTask,
00187                                              Co_NULL,
00188                                   CFG_LOWEST_PRIO,
00189                  &idle_stk[CFG_IDLE_STACK_SIZE-1],
00190                               CFG_IDLE_STACK_SIZE
00191                  );
00192                                   /* Set PSP for CoIdleTask coming in */ 
00193     SetEnvironment(&idle_stk[CFG_IDLE_STACK_SIZE-1]);
00194 }
00195 
00196 
00197 /**
00198  *******************************************************************************
00199  * @brief      Start multitask   
00200  * @param[in]  None      
00201  * @param[out] None      
00202  * @retval     None  
00203  *
00204  * @par Description
00205  * @details    This function is called to start multitask.After it is called,
00206  *             OS start schedule task by priority or/and time slice.    
00207  * @note       This function must be called to start OS when you use CoOS,and must
00208  *             call after CoOsInit().
00209  *******************************************************************************
00210  */
00211 void CoStartOS(void)
00212 {
00213     TCBRunning   = &TCBTbl [0];           /* Get running task                     */
00214     TCBNext      = TCBRunning ;           /* Set next scheduled task as running task */
00215     TCBRunning ->state  = TASK_RUNNING;   /* Set running task status to RUNNING   */
00216     RemoveFromTCBRdyList(TCBRunning );   /* Remove running task from READY list  */
00217     OsSchedUnlock();                    /* Enable Schedule,call task schedule   */
00218 }
00219 
00220 
00221 /**
00222  *******************************************************************************
00223  * @brief      Get OS version      
00224  * @param[in]  None  
00225  * @param[out] None  
00226  * @retval     The value is version of OS mutipled by 100.       
00227  *
00228  * @par Description
00229  * @details    This function is used to return the version number of CooCox OS.
00230  *             the return value corresponds to CooCox's version number multiplied
00231  *             by 100. In other words, version 1.02 would be returned as 102.         
00232  *******************************************************************************
00233  */
00234 OS_VER CoGetOSVersion(void)
00235 {
00236     return OS_VERSION;                  /* Get CooCox CoOS version            */
00237 }
00238