skeleton for lab1
Dependencies: AvailableMemory mbed-rtos mbed
Fork of helloaabbc by
mbed-rtos/rtx/rt_Task.c@0:1c8f2727e9f5, 2014-04-03 (annotated)
- Committer:
- y7jin
- Date:
- Thu Apr 03 22:56:32 2014 +0000
- Revision:
- 0:1c8f2727e9f5
hello
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
y7jin | 0:1c8f2727e9f5 | 1 | /*---------------------------------------------------------------------------- |
y7jin | 0:1c8f2727e9f5 | 2 | * RL-ARM - RTX |
y7jin | 0:1c8f2727e9f5 | 3 | *---------------------------------------------------------------------------- |
y7jin | 0:1c8f2727e9f5 | 4 | * Name: RT_TASK.C |
y7jin | 0:1c8f2727e9f5 | 5 | * Purpose: Task functions and system start up. |
y7jin | 0:1c8f2727e9f5 | 6 | * Rev.: V4.60 |
y7jin | 0:1c8f2727e9f5 | 7 | *---------------------------------------------------------------------------- |
y7jin | 0:1c8f2727e9f5 | 8 | * |
y7jin | 0:1c8f2727e9f5 | 9 | * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH |
y7jin | 0:1c8f2727e9f5 | 10 | * All rights reserved. |
y7jin | 0:1c8f2727e9f5 | 11 | * Redistribution and use in source and binary forms, with or without |
y7jin | 0:1c8f2727e9f5 | 12 | * modification, are permitted provided that the following conditions are met: |
y7jin | 0:1c8f2727e9f5 | 13 | * - Redistributions of source code must retain the above copyright |
y7jin | 0:1c8f2727e9f5 | 14 | * notice, this list of conditions and the following disclaimer. |
y7jin | 0:1c8f2727e9f5 | 15 | * - Redistributions in binary form must reproduce the above copyright |
y7jin | 0:1c8f2727e9f5 | 16 | * notice, this list of conditions and the following disclaimer in the |
y7jin | 0:1c8f2727e9f5 | 17 | * documentation and/or other materials provided with the distribution. |
y7jin | 0:1c8f2727e9f5 | 18 | * - Neither the name of ARM nor the names of its contributors may be used |
y7jin | 0:1c8f2727e9f5 | 19 | * to endorse or promote products derived from this software without |
y7jin | 0:1c8f2727e9f5 | 20 | * specific prior written permission. |
y7jin | 0:1c8f2727e9f5 | 21 | * |
y7jin | 0:1c8f2727e9f5 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
y7jin | 0:1c8f2727e9f5 | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
y7jin | 0:1c8f2727e9f5 | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
y7jin | 0:1c8f2727e9f5 | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
y7jin | 0:1c8f2727e9f5 | 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
y7jin | 0:1c8f2727e9f5 | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
y7jin | 0:1c8f2727e9f5 | 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
y7jin | 0:1c8f2727e9f5 | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
y7jin | 0:1c8f2727e9f5 | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
y7jin | 0:1c8f2727e9f5 | 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
y7jin | 0:1c8f2727e9f5 | 32 | * POSSIBILITY OF SUCH DAMAGE. |
y7jin | 0:1c8f2727e9f5 | 33 | *---------------------------------------------------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 34 | |
y7jin | 0:1c8f2727e9f5 | 35 | #include "rt_TypeDef.h" |
y7jin | 0:1c8f2727e9f5 | 36 | #include "RTX_Config.h" |
y7jin | 0:1c8f2727e9f5 | 37 | #include "rt_System.h" |
y7jin | 0:1c8f2727e9f5 | 38 | #include "rt_Task.h" |
y7jin | 0:1c8f2727e9f5 | 39 | #include "rt_List.h" |
y7jin | 0:1c8f2727e9f5 | 40 | #include "rt_MemBox.h" |
y7jin | 0:1c8f2727e9f5 | 41 | #include "rt_Robin.h" |
y7jin | 0:1c8f2727e9f5 | 42 | #include "rt_HAL_CM.h" |
y7jin | 0:1c8f2727e9f5 | 43 | |
y7jin | 0:1c8f2727e9f5 | 44 | /*---------------------------------------------------------------------------- |
y7jin | 0:1c8f2727e9f5 | 45 | * Global Variables |
y7jin | 0:1c8f2727e9f5 | 46 | *---------------------------------------------------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 47 | |
y7jin | 0:1c8f2727e9f5 | 48 | /* Running and next task info. */ |
y7jin | 0:1c8f2727e9f5 | 49 | struct OS_TSK os_tsk; |
y7jin | 0:1c8f2727e9f5 | 50 | |
y7jin | 0:1c8f2727e9f5 | 51 | /* Task Control Blocks of idle demon */ |
y7jin | 0:1c8f2727e9f5 | 52 | struct OS_TCB os_idle_TCB; |
y7jin | 0:1c8f2727e9f5 | 53 | |
y7jin | 0:1c8f2727e9f5 | 54 | |
y7jin | 0:1c8f2727e9f5 | 55 | /*---------------------------------------------------------------------------- |
y7jin | 0:1c8f2727e9f5 | 56 | * Local Functions |
y7jin | 0:1c8f2727e9f5 | 57 | *---------------------------------------------------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 58 | |
y7jin | 0:1c8f2727e9f5 | 59 | OS_TID rt_get_TID (void) { |
y7jin | 0:1c8f2727e9f5 | 60 | U32 tid; |
y7jin | 0:1c8f2727e9f5 | 61 | |
y7jin | 0:1c8f2727e9f5 | 62 | for (tid = 1; tid <= os_maxtaskrun; tid++) { |
y7jin | 0:1c8f2727e9f5 | 63 | if (os_active_TCB[tid-1] == NULL) { |
y7jin | 0:1c8f2727e9f5 | 64 | return ((OS_TID)tid); |
y7jin | 0:1c8f2727e9f5 | 65 | } |
y7jin | 0:1c8f2727e9f5 | 66 | } |
y7jin | 0:1c8f2727e9f5 | 67 | return (0); |
y7jin | 0:1c8f2727e9f5 | 68 | } |
y7jin | 0:1c8f2727e9f5 | 69 | |
y7jin | 0:1c8f2727e9f5 | 70 | #if defined (__CC_ARM) && !defined (__MICROLIB) |
y7jin | 0:1c8f2727e9f5 | 71 | /*--------------------------- __user_perthread_libspace ---------------------*/ |
y7jin | 0:1c8f2727e9f5 | 72 | extern void *__libspace_start; |
y7jin | 0:1c8f2727e9f5 | 73 | |
y7jin | 0:1c8f2727e9f5 | 74 | void *__user_perthread_libspace (void) { |
y7jin | 0:1c8f2727e9f5 | 75 | /* Provide a separate libspace for each task. */ |
y7jin | 0:1c8f2727e9f5 | 76 | if (os_tsk.run == NULL) { |
y7jin | 0:1c8f2727e9f5 | 77 | /* RTX not running yet. */ |
y7jin | 0:1c8f2727e9f5 | 78 | return (&__libspace_start); |
y7jin | 0:1c8f2727e9f5 | 79 | } |
y7jin | 0:1c8f2727e9f5 | 80 | return (void *)(os_tsk.run->std_libspace); |
y7jin | 0:1c8f2727e9f5 | 81 | } |
y7jin | 0:1c8f2727e9f5 | 82 | #endif |
y7jin | 0:1c8f2727e9f5 | 83 | |
y7jin | 0:1c8f2727e9f5 | 84 | /*--------------------------- rt_init_context -------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 85 | |
y7jin | 0:1c8f2727e9f5 | 86 | void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) { |
y7jin | 0:1c8f2727e9f5 | 87 | /* Initialize general part of the Task Control Block. */ |
y7jin | 0:1c8f2727e9f5 | 88 | p_TCB->cb_type = TCB; |
y7jin | 0:1c8f2727e9f5 | 89 | p_TCB->state = READY; |
y7jin | 0:1c8f2727e9f5 | 90 | p_TCB->prio = priority; |
y7jin | 0:1c8f2727e9f5 | 91 | p_TCB->p_lnk = NULL; |
y7jin | 0:1c8f2727e9f5 | 92 | p_TCB->p_rlnk = NULL; |
y7jin | 0:1c8f2727e9f5 | 93 | p_TCB->p_dlnk = NULL; |
y7jin | 0:1c8f2727e9f5 | 94 | p_TCB->p_blnk = NULL; |
y7jin | 0:1c8f2727e9f5 | 95 | p_TCB->delta_time = 0; |
y7jin | 0:1c8f2727e9f5 | 96 | p_TCB->interval_time = 0; |
y7jin | 0:1c8f2727e9f5 | 97 | p_TCB->events = 0; |
y7jin | 0:1c8f2727e9f5 | 98 | p_TCB->waits = 0; |
y7jin | 0:1c8f2727e9f5 | 99 | p_TCB->stack_frame = 0; |
y7jin | 0:1c8f2727e9f5 | 100 | |
y7jin | 0:1c8f2727e9f5 | 101 | rt_init_stack (p_TCB, task_body); |
y7jin | 0:1c8f2727e9f5 | 102 | } |
y7jin | 0:1c8f2727e9f5 | 103 | |
y7jin | 0:1c8f2727e9f5 | 104 | |
y7jin | 0:1c8f2727e9f5 | 105 | /*--------------------------- rt_switch_req ---------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 106 | |
y7jin | 0:1c8f2727e9f5 | 107 | void rt_switch_req (P_TCB p_new) { |
y7jin | 0:1c8f2727e9f5 | 108 | /* Switch to next task (identified by "p_new"). */ |
y7jin | 0:1c8f2727e9f5 | 109 | os_tsk.new_tsk = p_new; |
y7jin | 0:1c8f2727e9f5 | 110 | p_new->state = RUNNING; |
y7jin | 0:1c8f2727e9f5 | 111 | DBG_TASK_SWITCH(p_new->task_id); |
y7jin | 0:1c8f2727e9f5 | 112 | } |
y7jin | 0:1c8f2727e9f5 | 113 | |
y7jin | 0:1c8f2727e9f5 | 114 | |
y7jin | 0:1c8f2727e9f5 | 115 | /*--------------------------- rt_dispatch -----------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 116 | |
y7jin | 0:1c8f2727e9f5 | 117 | void rt_dispatch (P_TCB next_TCB) { |
y7jin | 0:1c8f2727e9f5 | 118 | /* Dispatch next task if any identified or dispatch highest ready task */ |
y7jin | 0:1c8f2727e9f5 | 119 | /* "next_TCB" identifies a task to run or has value NULL (=no next task) */ |
y7jin | 0:1c8f2727e9f5 | 120 | if (next_TCB == NULL) { |
y7jin | 0:1c8f2727e9f5 | 121 | /* Running task was blocked: continue with highest ready task */ |
y7jin | 0:1c8f2727e9f5 | 122 | next_TCB = rt_get_first (&os_rdy); |
y7jin | 0:1c8f2727e9f5 | 123 | rt_switch_req (next_TCB); |
y7jin | 0:1c8f2727e9f5 | 124 | } |
y7jin | 0:1c8f2727e9f5 | 125 | else { |
y7jin | 0:1c8f2727e9f5 | 126 | /* Check which task continues */ |
y7jin | 0:1c8f2727e9f5 | 127 | if (next_TCB->prio > os_tsk.run->prio) { |
y7jin | 0:1c8f2727e9f5 | 128 | /* preempt running task */ |
y7jin | 0:1c8f2727e9f5 | 129 | rt_put_rdy_first (os_tsk.run); |
y7jin | 0:1c8f2727e9f5 | 130 | os_tsk.run->state = READY; |
y7jin | 0:1c8f2727e9f5 | 131 | rt_switch_req (next_TCB); |
y7jin | 0:1c8f2727e9f5 | 132 | } |
y7jin | 0:1c8f2727e9f5 | 133 | else { |
y7jin | 0:1c8f2727e9f5 | 134 | /* put next task into ready list, no task switch takes place */ |
y7jin | 0:1c8f2727e9f5 | 135 | next_TCB->state = READY; |
y7jin | 0:1c8f2727e9f5 | 136 | rt_put_prio (&os_rdy, next_TCB); |
y7jin | 0:1c8f2727e9f5 | 137 | } |
y7jin | 0:1c8f2727e9f5 | 138 | } |
y7jin | 0:1c8f2727e9f5 | 139 | } |
y7jin | 0:1c8f2727e9f5 | 140 | |
y7jin | 0:1c8f2727e9f5 | 141 | |
y7jin | 0:1c8f2727e9f5 | 142 | /*--------------------------- rt_block --------------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 143 | |
y7jin | 0:1c8f2727e9f5 | 144 | void rt_block (U16 timeout, U8 block_state) { |
y7jin | 0:1c8f2727e9f5 | 145 | /* Block running task and choose next ready task. */ |
y7jin | 0:1c8f2727e9f5 | 146 | /* "timeout" sets a time-out value or is 0xffff (=no time-out). */ |
y7jin | 0:1c8f2727e9f5 | 147 | /* "block_state" defines the appropriate task state */ |
y7jin | 0:1c8f2727e9f5 | 148 | P_TCB next_TCB; |
y7jin | 0:1c8f2727e9f5 | 149 | |
y7jin | 0:1c8f2727e9f5 | 150 | if (timeout) { |
y7jin | 0:1c8f2727e9f5 | 151 | if (timeout < 0xffff) { |
y7jin | 0:1c8f2727e9f5 | 152 | rt_put_dly (os_tsk.run, timeout); |
y7jin | 0:1c8f2727e9f5 | 153 | } |
y7jin | 0:1c8f2727e9f5 | 154 | os_tsk.run->state = block_state; |
y7jin | 0:1c8f2727e9f5 | 155 | next_TCB = rt_get_first (&os_rdy); |
y7jin | 0:1c8f2727e9f5 | 156 | rt_switch_req (next_TCB); |
y7jin | 0:1c8f2727e9f5 | 157 | } |
y7jin | 0:1c8f2727e9f5 | 158 | } |
y7jin | 0:1c8f2727e9f5 | 159 | |
y7jin | 0:1c8f2727e9f5 | 160 | |
y7jin | 0:1c8f2727e9f5 | 161 | /*--------------------------- rt_tsk_pass -----------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 162 | |
y7jin | 0:1c8f2727e9f5 | 163 | void rt_tsk_pass (void) { |
y7jin | 0:1c8f2727e9f5 | 164 | /* Allow tasks of same priority level to run cooperatively.*/ |
y7jin | 0:1c8f2727e9f5 | 165 | P_TCB p_new; |
y7jin | 0:1c8f2727e9f5 | 166 | |
y7jin | 0:1c8f2727e9f5 | 167 | p_new = rt_get_same_rdy_prio(); |
y7jin | 0:1c8f2727e9f5 | 168 | if (p_new != NULL) { |
y7jin | 0:1c8f2727e9f5 | 169 | rt_put_prio ((P_XCB)&os_rdy, os_tsk.run); |
y7jin | 0:1c8f2727e9f5 | 170 | os_tsk.run->state = READY; |
y7jin | 0:1c8f2727e9f5 | 171 | rt_switch_req (p_new); |
y7jin | 0:1c8f2727e9f5 | 172 | } |
y7jin | 0:1c8f2727e9f5 | 173 | } |
y7jin | 0:1c8f2727e9f5 | 174 | |
y7jin | 0:1c8f2727e9f5 | 175 | |
y7jin | 0:1c8f2727e9f5 | 176 | /*--------------------------- rt_tsk_self -----------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 177 | |
y7jin | 0:1c8f2727e9f5 | 178 | OS_TID rt_tsk_self (void) { |
y7jin | 0:1c8f2727e9f5 | 179 | /* Return own task identifier value. */ |
y7jin | 0:1c8f2727e9f5 | 180 | if (os_tsk.run == NULL) { |
y7jin | 0:1c8f2727e9f5 | 181 | return (0); |
y7jin | 0:1c8f2727e9f5 | 182 | } |
y7jin | 0:1c8f2727e9f5 | 183 | return (os_tsk.run->task_id); |
y7jin | 0:1c8f2727e9f5 | 184 | } |
y7jin | 0:1c8f2727e9f5 | 185 | |
y7jin | 0:1c8f2727e9f5 | 186 | |
y7jin | 0:1c8f2727e9f5 | 187 | /*--------------------------- rt_tsk_prio -----------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 188 | |
y7jin | 0:1c8f2727e9f5 | 189 | OS_RESULT rt_tsk_prio (OS_TID task_id, U8 new_prio) { |
y7jin | 0:1c8f2727e9f5 | 190 | /* Change execution priority of a task to "new_prio". */ |
y7jin | 0:1c8f2727e9f5 | 191 | P_TCB p_task; |
y7jin | 0:1c8f2727e9f5 | 192 | |
y7jin | 0:1c8f2727e9f5 | 193 | if (task_id == 0) { |
y7jin | 0:1c8f2727e9f5 | 194 | /* Change execution priority of calling task. */ |
y7jin | 0:1c8f2727e9f5 | 195 | os_tsk.run->prio = new_prio; |
y7jin | 0:1c8f2727e9f5 | 196 | run:if (rt_rdy_prio() > new_prio) { |
y7jin | 0:1c8f2727e9f5 | 197 | rt_put_prio (&os_rdy, os_tsk.run); |
y7jin | 0:1c8f2727e9f5 | 198 | os_tsk.run->state = READY; |
y7jin | 0:1c8f2727e9f5 | 199 | rt_dispatch (NULL); |
y7jin | 0:1c8f2727e9f5 | 200 | } |
y7jin | 0:1c8f2727e9f5 | 201 | return (OS_R_OK); |
y7jin | 0:1c8f2727e9f5 | 202 | } |
y7jin | 0:1c8f2727e9f5 | 203 | |
y7jin | 0:1c8f2727e9f5 | 204 | /* Find the task in the "os_active_TCB" array. */ |
y7jin | 0:1c8f2727e9f5 | 205 | if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) { |
y7jin | 0:1c8f2727e9f5 | 206 | /* Task with "task_id" not found or not started. */ |
y7jin | 0:1c8f2727e9f5 | 207 | return (OS_R_NOK); |
y7jin | 0:1c8f2727e9f5 | 208 | } |
y7jin | 0:1c8f2727e9f5 | 209 | p_task = os_active_TCB[task_id-1]; |
y7jin | 0:1c8f2727e9f5 | 210 | p_task->prio = new_prio; |
y7jin | 0:1c8f2727e9f5 | 211 | if (p_task == os_tsk.run) { |
y7jin | 0:1c8f2727e9f5 | 212 | goto run; |
y7jin | 0:1c8f2727e9f5 | 213 | } |
y7jin | 0:1c8f2727e9f5 | 214 | rt_resort_prio (p_task); |
y7jin | 0:1c8f2727e9f5 | 215 | if (p_task->state == READY) { |
y7jin | 0:1c8f2727e9f5 | 216 | /* Task enqueued in a ready list. */ |
y7jin | 0:1c8f2727e9f5 | 217 | p_task = rt_get_first (&os_rdy); |
y7jin | 0:1c8f2727e9f5 | 218 | rt_dispatch (p_task); |
y7jin | 0:1c8f2727e9f5 | 219 | } |
y7jin | 0:1c8f2727e9f5 | 220 | return (OS_R_OK); |
y7jin | 0:1c8f2727e9f5 | 221 | } |
y7jin | 0:1c8f2727e9f5 | 222 | |
y7jin | 0:1c8f2727e9f5 | 223 | /*--------------------------- rt_tsk_delete ---------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 224 | |
y7jin | 0:1c8f2727e9f5 | 225 | OS_RESULT rt_tsk_delete (OS_TID task_id) { |
y7jin | 0:1c8f2727e9f5 | 226 | /* Terminate the task identified with "task_id". */ |
y7jin | 0:1c8f2727e9f5 | 227 | P_TCB task_context; |
y7jin | 0:1c8f2727e9f5 | 228 | |
y7jin | 0:1c8f2727e9f5 | 229 | if (task_id == 0 || task_id == os_tsk.run->task_id) { |
y7jin | 0:1c8f2727e9f5 | 230 | /* Terminate itself. */ |
y7jin | 0:1c8f2727e9f5 | 231 | os_tsk.run->state = INACTIVE; |
y7jin | 0:1c8f2727e9f5 | 232 | os_tsk.run->tsk_stack = rt_get_PSP (); |
y7jin | 0:1c8f2727e9f5 | 233 | rt_stk_check (); |
y7jin | 0:1c8f2727e9f5 | 234 | os_active_TCB[os_tsk.run->task_id-1] = NULL; |
y7jin | 0:1c8f2727e9f5 | 235 | |
y7jin | 0:1c8f2727e9f5 | 236 | os_tsk.run->stack = NULL; |
y7jin | 0:1c8f2727e9f5 | 237 | DBG_TASK_NOTIFY(os_tsk.run, __FALSE); |
y7jin | 0:1c8f2727e9f5 | 238 | os_tsk.run = NULL; |
y7jin | 0:1c8f2727e9f5 | 239 | rt_dispatch (NULL); |
y7jin | 0:1c8f2727e9f5 | 240 | /* The program should never come to this point. */ |
y7jin | 0:1c8f2727e9f5 | 241 | } |
y7jin | 0:1c8f2727e9f5 | 242 | else { |
y7jin | 0:1c8f2727e9f5 | 243 | /* Find the task in the "os_active_TCB" array. */ |
y7jin | 0:1c8f2727e9f5 | 244 | if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) { |
y7jin | 0:1c8f2727e9f5 | 245 | /* Task with "task_id" not found or not started. */ |
y7jin | 0:1c8f2727e9f5 | 246 | return (OS_R_NOK); |
y7jin | 0:1c8f2727e9f5 | 247 | } |
y7jin | 0:1c8f2727e9f5 | 248 | task_context = os_active_TCB[task_id-1]; |
y7jin | 0:1c8f2727e9f5 | 249 | rt_rmv_list (task_context); |
y7jin | 0:1c8f2727e9f5 | 250 | rt_rmv_dly (task_context); |
y7jin | 0:1c8f2727e9f5 | 251 | os_active_TCB[task_id-1] = NULL; |
y7jin | 0:1c8f2727e9f5 | 252 | |
y7jin | 0:1c8f2727e9f5 | 253 | task_context->stack = NULL; |
y7jin | 0:1c8f2727e9f5 | 254 | DBG_TASK_NOTIFY(task_context, __FALSE); |
y7jin | 0:1c8f2727e9f5 | 255 | } |
y7jin | 0:1c8f2727e9f5 | 256 | return (OS_R_OK); |
y7jin | 0:1c8f2727e9f5 | 257 | } |
y7jin | 0:1c8f2727e9f5 | 258 | |
y7jin | 0:1c8f2727e9f5 | 259 | |
y7jin | 0:1c8f2727e9f5 | 260 | /*--------------------------- rt_sys_init -----------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 261 | |
y7jin | 0:1c8f2727e9f5 | 262 | #ifdef __CMSIS_RTOS |
y7jin | 0:1c8f2727e9f5 | 263 | void rt_sys_init (void) { |
y7jin | 0:1c8f2727e9f5 | 264 | #else |
y7jin | 0:1c8f2727e9f5 | 265 | void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) { |
y7jin | 0:1c8f2727e9f5 | 266 | #endif |
y7jin | 0:1c8f2727e9f5 | 267 | /* Initialize system and start up task declared with "first_task". */ |
y7jin | 0:1c8f2727e9f5 | 268 | U32 i; |
y7jin | 0:1c8f2727e9f5 | 269 | |
y7jin | 0:1c8f2727e9f5 | 270 | DBG_INIT(); |
y7jin | 0:1c8f2727e9f5 | 271 | |
y7jin | 0:1c8f2727e9f5 | 272 | /* Initialize dynamic memory and task TCB pointers to NULL. */ |
y7jin | 0:1c8f2727e9f5 | 273 | for (i = 0; i < os_maxtaskrun; i++) { |
y7jin | 0:1c8f2727e9f5 | 274 | os_active_TCB[i] = NULL; |
y7jin | 0:1c8f2727e9f5 | 275 | } |
y7jin | 0:1c8f2727e9f5 | 276 | |
y7jin | 0:1c8f2727e9f5 | 277 | /* Set up TCB of idle demon */ |
y7jin | 0:1c8f2727e9f5 | 278 | os_idle_TCB.task_id = 255; |
y7jin | 0:1c8f2727e9f5 | 279 | os_idle_TCB.priv_stack = idle_task_stack_size; |
y7jin | 0:1c8f2727e9f5 | 280 | os_idle_TCB.stack = idle_task_stack; |
y7jin | 0:1c8f2727e9f5 | 281 | rt_init_context (&os_idle_TCB, 0, os_idle_demon); |
y7jin | 0:1c8f2727e9f5 | 282 | |
y7jin | 0:1c8f2727e9f5 | 283 | /* Set up ready list: initially empty */ |
y7jin | 0:1c8f2727e9f5 | 284 | os_rdy.cb_type = HCB; |
y7jin | 0:1c8f2727e9f5 | 285 | os_rdy.p_lnk = NULL; |
y7jin | 0:1c8f2727e9f5 | 286 | /* Set up delay list: initially empty */ |
y7jin | 0:1c8f2727e9f5 | 287 | os_dly.cb_type = HCB; |
y7jin | 0:1c8f2727e9f5 | 288 | os_dly.p_dlnk = NULL; |
y7jin | 0:1c8f2727e9f5 | 289 | os_dly.p_blnk = NULL; |
y7jin | 0:1c8f2727e9f5 | 290 | os_dly.delta_time = 0; |
y7jin | 0:1c8f2727e9f5 | 291 | |
y7jin | 0:1c8f2727e9f5 | 292 | /* Fix SP and systemvariables to assume idle task is running */ |
y7jin | 0:1c8f2727e9f5 | 293 | /* Transform main program into idle task by assuming idle TCB */ |
y7jin | 0:1c8f2727e9f5 | 294 | #ifndef __CMSIS_RTOS |
y7jin | 0:1c8f2727e9f5 | 295 | rt_set_PSP (os_idle_TCB.tsk_stack+32); |
y7jin | 0:1c8f2727e9f5 | 296 | #endif |
y7jin | 0:1c8f2727e9f5 | 297 | os_tsk.run = &os_idle_TCB; |
y7jin | 0:1c8f2727e9f5 | 298 | os_tsk.run->state = RUNNING; |
y7jin | 0:1c8f2727e9f5 | 299 | |
y7jin | 0:1c8f2727e9f5 | 300 | /* Initialize ps queue */ |
y7jin | 0:1c8f2727e9f5 | 301 | os_psq->first = 0; |
y7jin | 0:1c8f2727e9f5 | 302 | os_psq->last = 0; |
y7jin | 0:1c8f2727e9f5 | 303 | os_psq->size = os_fifo_size; |
y7jin | 0:1c8f2727e9f5 | 304 | |
y7jin | 0:1c8f2727e9f5 | 305 | rt_init_robin (); |
y7jin | 0:1c8f2727e9f5 | 306 | |
y7jin | 0:1c8f2727e9f5 | 307 | /* Intitialize SVC and PendSV */ |
y7jin | 0:1c8f2727e9f5 | 308 | rt_svc_init (); |
y7jin | 0:1c8f2727e9f5 | 309 | |
y7jin | 0:1c8f2727e9f5 | 310 | #ifndef __CMSIS_RTOS |
y7jin | 0:1c8f2727e9f5 | 311 | /* Intitialize and start system clock timer */ |
y7jin | 0:1c8f2727e9f5 | 312 | os_tick_irqn = os_tick_init (); |
y7jin | 0:1c8f2727e9f5 | 313 | if (os_tick_irqn >= 0) { |
y7jin | 0:1c8f2727e9f5 | 314 | OS_X_INIT(os_tick_irqn); |
y7jin | 0:1c8f2727e9f5 | 315 | } |
y7jin | 0:1c8f2727e9f5 | 316 | |
y7jin | 0:1c8f2727e9f5 | 317 | /* Start up first user task before entering the endless loop */ |
y7jin | 0:1c8f2727e9f5 | 318 | rt_tsk_create (first_task, prio_stksz, stk, NULL); |
y7jin | 0:1c8f2727e9f5 | 319 | #endif |
y7jin | 0:1c8f2727e9f5 | 320 | } |
y7jin | 0:1c8f2727e9f5 | 321 | |
y7jin | 0:1c8f2727e9f5 | 322 | |
y7jin | 0:1c8f2727e9f5 | 323 | /*--------------------------- rt_sys_start ----------------------------------*/ |
y7jin | 0:1c8f2727e9f5 | 324 | |
y7jin | 0:1c8f2727e9f5 | 325 | #ifdef __CMSIS_RTOS |
y7jin | 0:1c8f2727e9f5 | 326 | void rt_sys_start (void) { |
y7jin | 0:1c8f2727e9f5 | 327 | /* Start system */ |
y7jin | 0:1c8f2727e9f5 | 328 | |
y7jin | 0:1c8f2727e9f5 | 329 | /* Intitialize and start system clock timer */ |
y7jin | 0:1c8f2727e9f5 | 330 | os_tick_irqn = os_tick_init (); |
y7jin | 0:1c8f2727e9f5 | 331 | if (os_tick_irqn >= 0) { |
y7jin | 0:1c8f2727e9f5 | 332 | OS_X_INIT(os_tick_irqn); |
y7jin | 0:1c8f2727e9f5 | 333 | } |
y7jin | 0:1c8f2727e9f5 | 334 | } |
y7jin | 0:1c8f2727e9f5 | 335 | #endif |
y7jin | 0:1c8f2727e9f5 | 336 | |
y7jin | 0:1c8f2727e9f5 | 337 | /*---------------------------------------------------------------------------- |
y7jin | 0:1c8f2727e9f5 | 338 | * end of file |
y7jin | 0:1c8f2727e9f5 | 339 | *---------------------------------------------------------------------------*/ |