Eric Ebert
/
CoOS
CoOS Demonstrator adapted to mbed Hardware.
core.c@0:57690853989a, 2010-12-03 (annotated)
- Committer:
- ericebert
- Date:
- Fri Dec 03 19:45:30 2010 +0000
- Revision:
- 0:57690853989a
Some basic LED-Flashing works in the CoOS-RTOS using Tasks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericebert | 0:57690853989a | 1 | /** |
ericebert | 0:57690853989a | 2 | ******************************************************************************* |
ericebert | 0:57690853989a | 3 | * @file core.c |
ericebert | 0:57690853989a | 4 | * @version V1.1.3 |
ericebert | 0:57690853989a | 5 | * @date 2010.04.26 |
ericebert | 0:57690853989a | 6 | * @brief Core implementation code of CooCox CoOS kernel. |
ericebert | 0:57690853989a | 7 | ******************************************************************************* |
ericebert | 0:57690853989a | 8 | * @copy |
ericebert | 0:57690853989a | 9 | * |
ericebert | 0:57690853989a | 10 | * INTERNAL FILE,DON'T PUBLIC. |
ericebert | 0:57690853989a | 11 | * |
ericebert | 0:57690853989a | 12 | * <h2><center>© COPYRIGHT 2009 CooCox </center></h2> |
ericebert | 0:57690853989a | 13 | ******************************************************************************* |
ericebert | 0:57690853989a | 14 | */ |
ericebert | 0:57690853989a | 15 | |
ericebert | 0:57690853989a | 16 | /*---------------------------- Include ---------------------------------------*/ |
ericebert | 0:57690853989a | 17 | #include <coocox.h> |
ericebert | 0:57690853989a | 18 | |
ericebert | 0:57690853989a | 19 | /*---------------------------- Variable Define -------------------------------*/ |
ericebert | 0:57690853989a | 20 | volatile U8 OSIntNesting = 0; /*!< Use to indicate interrupt nesting level*/ |
ericebert | 0:57690853989a | 21 | volatile U8 OSSchedLock = 0; /*!< Task Switch lock. */ |
ericebert | 0:57690853989a | 22 | volatile BOOL TaskSchedReq = FALSE; |
ericebert | 0:57690853989a | 23 | |
ericebert | 0:57690853989a | 24 | |
ericebert | 0:57690853989a | 25 | /** |
ericebert | 0:57690853989a | 26 | ******************************************************************************* |
ericebert | 0:57690853989a | 27 | * @brief Enter a ISR. |
ericebert | 0:57690853989a | 28 | * @param[in] None |
ericebert | 0:57690853989a | 29 | * @param[out] None |
ericebert | 0:57690853989a | 30 | * @retval None |
ericebert | 0:57690853989a | 31 | * |
ericebert | 0:57690853989a | 32 | * @par Description |
ericebert | 0:57690853989a | 33 | * @details This function is called to notify OS when enter to an ISR. |
ericebert | 0:57690853989a | 34 | * |
ericebert | 0:57690853989a | 35 | * @note When you call API in ISR,you must call CoEnterISR() before your |
ericebert | 0:57690853989a | 36 | * interrupt handler code,and call CoExitISR() after your handler |
ericebert | 0:57690853989a | 37 | * code and before exiting from ISR. |
ericebert | 0:57690853989a | 38 | ******************************************************************************* |
ericebert | 0:57690853989a | 39 | */ |
ericebert | 0:57690853989a | 40 | void CoEnterISR(void) |
ericebert | 0:57690853989a | 41 | { |
ericebert | 0:57690853989a | 42 | Inc8(&OSIntNesting); /* OSIntNesting increment */ |
ericebert | 0:57690853989a | 43 | } |
ericebert | 0:57690853989a | 44 | |
ericebert | 0:57690853989a | 45 | |
ericebert | 0:57690853989a | 46 | /** |
ericebert | 0:57690853989a | 47 | ******************************************************************************* |
ericebert | 0:57690853989a | 48 | * @brief Exit a ISR. |
ericebert | 0:57690853989a | 49 | * @param[in] None |
ericebert | 0:57690853989a | 50 | * @param[out] None |
ericebert | 0:57690853989a | 51 | * @retval None |
ericebert | 0:57690853989a | 52 | * |
ericebert | 0:57690853989a | 53 | * @par Description |
ericebert | 0:57690853989a | 54 | * @details This function is called when exit from a ISR. |
ericebert | 0:57690853989a | 55 | * |
ericebert | 0:57690853989a | 56 | * @note |
ericebert | 0:57690853989a | 57 | ******************************************************************************* |
ericebert | 0:57690853989a | 58 | */ |
ericebert | 0:57690853989a | 59 | void CoExitISR(void) |
ericebert | 0:57690853989a | 60 | { |
ericebert | 0:57690853989a | 61 | Dec8(&OSIntNesting); /* OSIntNesting decrease */ |
ericebert | 0:57690853989a | 62 | if( OSIntNesting == 0) /* Is OSIntNesting == 0? */ |
ericebert | 0:57690853989a | 63 | { |
ericebert | 0:57690853989a | 64 | if(TaskSchedReq == TRUE) |
ericebert | 0:57690853989a | 65 | { |
ericebert | 0:57690853989a | 66 | OSSchedLock++; |
ericebert | 0:57690853989a | 67 | Schedule(); /* Call task schedule */ |
ericebert | 0:57690853989a | 68 | OSSchedLock--; |
ericebert | 0:57690853989a | 69 | } |
ericebert | 0:57690853989a | 70 | } |
ericebert | 0:57690853989a | 71 | } |
ericebert | 0:57690853989a | 72 | |
ericebert | 0:57690853989a | 73 | |
ericebert | 0:57690853989a | 74 | |
ericebert | 0:57690853989a | 75 | /** |
ericebert | 0:57690853989a | 76 | ******************************************************************************* |
ericebert | 0:57690853989a | 77 | * @brief Unlock schedule |
ericebert | 0:57690853989a | 78 | * @param[in] None |
ericebert | 0:57690853989a | 79 | * @param[out] None |
ericebert | 0:57690853989a | 80 | * @retval None |
ericebert | 0:57690853989a | 81 | * |
ericebert | 0:57690853989a | 82 | * @par Description |
ericebert | 0:57690853989a | 83 | * @details This function is called to unlock schedule(i.e.enable schedule again) |
ericebert | 0:57690853989a | 84 | * |
ericebert | 0:57690853989a | 85 | * @note |
ericebert | 0:57690853989a | 86 | ******************************************************************************* |
ericebert | 0:57690853989a | 87 | */ |
ericebert | 0:57690853989a | 88 | void OsSchedUnlock(void) |
ericebert | 0:57690853989a | 89 | { |
ericebert | 0:57690853989a | 90 | if(OSSchedLock == 1) /* Is OSSchedLock == 0? */ |
ericebert | 0:57690853989a | 91 | { |
ericebert | 0:57690853989a | 92 | #if CFG_TASK_WAITTING_EN > 0 |
ericebert | 0:57690853989a | 93 | if(IsrReq == TRUE) |
ericebert | 0:57690853989a | 94 | { |
ericebert | 0:57690853989a | 95 | RespondSRQ(); /* Respond service request */ |
ericebert | 0:57690853989a | 96 | } |
ericebert | 0:57690853989a | 97 | #endif |
ericebert | 0:57690853989a | 98 | /* Judge task state change or higher PRI task coming in */ |
ericebert | 0:57690853989a | 99 | if(TaskSchedReq == TRUE) |
ericebert | 0:57690853989a | 100 | { |
ericebert | 0:57690853989a | 101 | Schedule(); /* Call task schedule */ |
ericebert | 0:57690853989a | 102 | } |
ericebert | 0:57690853989a | 103 | OSSchedLock = 0; |
ericebert | 0:57690853989a | 104 | } |
ericebert | 0:57690853989a | 105 | else |
ericebert | 0:57690853989a | 106 | { |
ericebert | 0:57690853989a | 107 | OSSchedLock--; |
ericebert | 0:57690853989a | 108 | } |
ericebert | 0:57690853989a | 109 | } |
ericebert | 0:57690853989a | 110 | |
ericebert | 0:57690853989a | 111 | |
ericebert | 0:57690853989a | 112 | /** |
ericebert | 0:57690853989a | 113 | ******************************************************************************* |
ericebert | 0:57690853989a | 114 | * @brief Lock schedule |
ericebert | 0:57690853989a | 115 | * @param[in] None |
ericebert | 0:57690853989a | 116 | * @param[out] None |
ericebert | 0:57690853989a | 117 | * @retval None |
ericebert | 0:57690853989a | 118 | * |
ericebert | 0:57690853989a | 119 | * @par Description |
ericebert | 0:57690853989a | 120 | * @details This function is called in application code to lock schedule. |
ericebert | 0:57690853989a | 121 | * |
ericebert | 0:57690853989a | 122 | * @note |
ericebert | 0:57690853989a | 123 | ******************************************************************************* |
ericebert | 0:57690853989a | 124 | */ |
ericebert | 0:57690853989a | 125 | void CoSchedLock(void) |
ericebert | 0:57690853989a | 126 | { |
ericebert | 0:57690853989a | 127 | OsSchedLock(); /* Lock schedule */ |
ericebert | 0:57690853989a | 128 | } |
ericebert | 0:57690853989a | 129 | |
ericebert | 0:57690853989a | 130 | |
ericebert | 0:57690853989a | 131 | /** |
ericebert | 0:57690853989a | 132 | ******************************************************************************* |
ericebert | 0:57690853989a | 133 | * @brief Unlock schedule |
ericebert | 0:57690853989a | 134 | * @param[in] None |
ericebert | 0:57690853989a | 135 | * @param[out] None |
ericebert | 0:57690853989a | 136 | * @retval None |
ericebert | 0:57690853989a | 137 | * |
ericebert | 0:57690853989a | 138 | * @par Description |
ericebert | 0:57690853989a | 139 | * @details This function is called in APP to unlock schedule. |
ericebert | 0:57690853989a | 140 | * |
ericebert | 0:57690853989a | 141 | * @note |
ericebert | 0:57690853989a | 142 | ******************************************************************************* |
ericebert | 0:57690853989a | 143 | */ |
ericebert | 0:57690853989a | 144 | void CoSchedUnlock(void) |
ericebert | 0:57690853989a | 145 | { |
ericebert | 0:57690853989a | 146 | OsSchedUnlock(); /* Unlock schedule */ |
ericebert | 0:57690853989a | 147 | } |
ericebert | 0:57690853989a | 148 | |
ericebert | 0:57690853989a | 149 | |
ericebert | 0:57690853989a | 150 | /** |
ericebert | 0:57690853989a | 151 | ******************************************************************************* |
ericebert | 0:57690853989a | 152 | * @brief Initialize OS |
ericebert | 0:57690853989a | 153 | * @param[in] None |
ericebert | 0:57690853989a | 154 | * @param[out] None |
ericebert | 0:57690853989a | 155 | * @retval None |
ericebert | 0:57690853989a | 156 | * |
ericebert | 0:57690853989a | 157 | * @par Description |
ericebert | 0:57690853989a | 158 | * @details This function is called to initialize OS. |
ericebert | 0:57690853989a | 159 | * |
ericebert | 0:57690853989a | 160 | * @note You must call this function first,before any other OS API function |
ericebert | 0:57690853989a | 161 | * |
ericebert | 0:57690853989a | 162 | * @code There is a example for useage of this function,as follows: |
ericebert | 0:57690853989a | 163 | * e.g. |
ericebert | 0:57690853989a | 164 | * ... // Your target initial code. |
ericebert | 0:57690853989a | 165 | * |
ericebert | 0:57690853989a | 166 | * OsInit(); // Initial OS. |
ericebert | 0:57690853989a | 167 | * CreateTask(...); // Create tasks. |
ericebert | 0:57690853989a | 168 | * ... |
ericebert | 0:57690853989a | 169 | * OsStart(); // Start multitask. |
ericebert | 0:57690853989a | 170 | * @endcode |
ericebert | 0:57690853989a | 171 | ******************************************************************************* |
ericebert | 0:57690853989a | 172 | */ |
ericebert | 0:57690853989a | 173 | void CoInitOS(void) |
ericebert | 0:57690853989a | 174 | { |
ericebert | 0:57690853989a | 175 | InitSysTick(); /* Initialize system tick. */ |
ericebert | 0:57690853989a | 176 | InitInt(); /* Initialize PendSV,SVC,SysTick interrupt */ |
ericebert | 0:57690853989a | 177 | CreateTCBList(); /* Create TCB list. */ |
ericebert | 0:57690853989a | 178 | #if CFG_EVENT_EN > 0 |
ericebert | 0:57690853989a | 179 | CreateEventList(); /* Create event control list. */ |
ericebert | 0:57690853989a | 180 | #endif |
ericebert | 0:57690853989a | 181 | #if CFG_KHEAP_EN > 0 |
ericebert | 0:57690853989a | 182 | CoCreateKheap(); /* Create kernel heap within user define */ |
ericebert | 0:57690853989a | 183 | #endif |
ericebert | 0:57690853989a | 184 | OsSchedLock(); /* Lock Schedule */ |
ericebert | 0:57690853989a | 185 | /* Create first task -- IDLE task. */ |
ericebert | 0:57690853989a | 186 | CoCreateTask( CoIdleTask, |
ericebert | 0:57690853989a | 187 | 0, |
ericebert | 0:57690853989a | 188 | CFG_LOWEST_PRIO, |
ericebert | 0:57690853989a | 189 | &idle_stk[CFG_IDLE_STACK_SIZE-1], |
ericebert | 0:57690853989a | 190 | CFG_IDLE_STACK_SIZE |
ericebert | 0:57690853989a | 191 | ); |
ericebert | 0:57690853989a | 192 | /* Set PSP for CoIdleTask coming in */ |
ericebert | 0:57690853989a | 193 | SetEnvironment(&idle_stk[CFG_IDLE_STACK_SIZE-1]); |
ericebert | 0:57690853989a | 194 | } |
ericebert | 0:57690853989a | 195 | |
ericebert | 0:57690853989a | 196 | |
ericebert | 0:57690853989a | 197 | /** |
ericebert | 0:57690853989a | 198 | ******************************************************************************* |
ericebert | 0:57690853989a | 199 | * @brief Start multitask |
ericebert | 0:57690853989a | 200 | * @param[in] None |
ericebert | 0:57690853989a | 201 | * @param[out] None |
ericebert | 0:57690853989a | 202 | * @retval None |
ericebert | 0:57690853989a | 203 | * |
ericebert | 0:57690853989a | 204 | * @par Description |
ericebert | 0:57690853989a | 205 | * @details This function is called to start multitask.After it is called, |
ericebert | 0:57690853989a | 206 | * OS start schedule task by priority or/and time slice. |
ericebert | 0:57690853989a | 207 | * @note This function must be called to start OS when you use CoOS,and must |
ericebert | 0:57690853989a | 208 | * call after CoOsInit(). |
ericebert | 0:57690853989a | 209 | ******************************************************************************* |
ericebert | 0:57690853989a | 210 | */ |
ericebert | 0:57690853989a | 211 | void CoStartOS(void) |
ericebert | 0:57690853989a | 212 | { |
ericebert | 0:57690853989a | 213 | TCBRunning = &TCBTbl[0]; /* Get running task */ |
ericebert | 0:57690853989a | 214 | TCBNext = TCBRunning; /* Set next scheduled task as running task */ |
ericebert | 0:57690853989a | 215 | TCBRunning->state = TASK_RUNNING; /* Set running task status to RUNNING */ |
ericebert | 0:57690853989a | 216 | RemoveFromTCBRdyList(TCBRunning); /* Remove running task from READY list */ |
ericebert | 0:57690853989a | 217 | OsSchedUnlock(); /* Enable Schedule,call task schedule */ |
ericebert | 0:57690853989a | 218 | } |
ericebert | 0:57690853989a | 219 | |
ericebert | 0:57690853989a | 220 | |
ericebert | 0:57690853989a | 221 | /** |
ericebert | 0:57690853989a | 222 | ******************************************************************************* |
ericebert | 0:57690853989a | 223 | * @brief Get OS version |
ericebert | 0:57690853989a | 224 | * @param[in] None |
ericebert | 0:57690853989a | 225 | * @param[out] None |
ericebert | 0:57690853989a | 226 | * @retval The value is version of OS mutipled by 100. |
ericebert | 0:57690853989a | 227 | * |
ericebert | 0:57690853989a | 228 | * @par Description |
ericebert | 0:57690853989a | 229 | * @details This function is used to return the version number of CooCox OS. |
ericebert | 0:57690853989a | 230 | * the return value corresponds to CooCox's version number multiplied |
ericebert | 0:57690853989a | 231 | * by 100. In other words, version 1.02 would be returned as 102. |
ericebert | 0:57690853989a | 232 | ******************************************************************************* |
ericebert | 0:57690853989a | 233 | */ |
ericebert | 0:57690853989a | 234 | OS_VER CoGetOSVersion(void) |
ericebert | 0:57690853989a | 235 | { |
ericebert | 0:57690853989a | 236 | return OS_VERSION; /* Get CooCox CoOS version */ |
ericebert | 0:57690853989a | 237 | } |
ericebert | 0:57690853989a | 238 |