Nummer 0.1 Werkt nog niet volledig
Fork of mbed-rtos by
rtx/TARGET_CORTEX_A/rt_Task.c@109:f37e70028583, 2016-05-13 (annotated)
- Committer:
- Reynier
- Date:
- Fri May 13 21:42:20 2016 +0000
- Revision:
- 109:f37e70028583
- Parent:
- 92:bc9729798a19
Alles zonder werkende NRF
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 48:e9a2c7cb57a4 | 1 | /*---------------------------------------------------------------------------- |
mbed_official | 48:e9a2c7cb57a4 | 2 | * RL-ARM - RTX |
mbed_official | 48:e9a2c7cb57a4 | 3 | *---------------------------------------------------------------------------- |
mbed_official | 48:e9a2c7cb57a4 | 4 | * Name: RT_TASK.C |
mbed_official | 48:e9a2c7cb57a4 | 5 | * Purpose: Task functions and system start up. |
mbed_official | 92:bc9729798a19 | 6 | * Rev.: V4.73 |
mbed_official | 48:e9a2c7cb57a4 | 7 | *---------------------------------------------------------------------------- |
mbed_official | 48:e9a2c7cb57a4 | 8 | * |
mbed_official | 92:bc9729798a19 | 9 | * Copyright (c) 1999-2009 KEIL, 2009-2013 ARM Germany GmbH |
mbed_official | 48:e9a2c7cb57a4 | 10 | * All rights reserved. |
mbed_official | 48:e9a2c7cb57a4 | 11 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 48:e9a2c7cb57a4 | 12 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 48:e9a2c7cb57a4 | 13 | * - Redistributions of source code must retain the above copyright |
mbed_official | 48:e9a2c7cb57a4 | 14 | * notice, this list of conditions and the following disclaimer. |
mbed_official | 48:e9a2c7cb57a4 | 15 | * - Redistributions in binary form must reproduce the above copyright |
mbed_official | 48:e9a2c7cb57a4 | 16 | * notice, this list of conditions and the following disclaimer in the |
mbed_official | 48:e9a2c7cb57a4 | 17 | * documentation and/or other materials provided with the distribution. |
mbed_official | 68:d3d0e710b443 | 18 | * - Neither the name of ARM nor the names of its contributors may be used |
mbed_official | 68:d3d0e710b443 | 19 | * to endorse or promote products derived from this software without |
mbed_official | 48:e9a2c7cb57a4 | 20 | * specific prior written permission. |
mbed_official | 48:e9a2c7cb57a4 | 21 | * |
mbed_official | 68:d3d0e710b443 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 68:d3d0e710b443 | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 48:e9a2c7cb57a4 | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
mbed_official | 48:e9a2c7cb57a4 | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
mbed_official | 48:e9a2c7cb57a4 | 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
mbed_official | 68:d3d0e710b443 | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
mbed_official | 68:d3d0e710b443 | 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
mbed_official | 68:d3d0e710b443 | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
mbed_official | 68:d3d0e710b443 | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
mbed_official | 48:e9a2c7cb57a4 | 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 48:e9a2c7cb57a4 | 32 | * POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 48:e9a2c7cb57a4 | 33 | *---------------------------------------------------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 34 | |
mbed_official | 48:e9a2c7cb57a4 | 35 | #include "rt_TypeDef.h" |
mbed_official | 48:e9a2c7cb57a4 | 36 | #include "RTX_Config.h" |
mbed_official | 48:e9a2c7cb57a4 | 37 | #include "rt_System.h" |
mbed_official | 48:e9a2c7cb57a4 | 38 | #include "rt_Task.h" |
mbed_official | 48:e9a2c7cb57a4 | 39 | #include "rt_List.h" |
mbed_official | 48:e9a2c7cb57a4 | 40 | #include "rt_MemBox.h" |
mbed_official | 48:e9a2c7cb57a4 | 41 | #include "rt_Robin.h" |
mbed_official | 48:e9a2c7cb57a4 | 42 | #ifdef __CORTEX_A9 |
mbed_official | 48:e9a2c7cb57a4 | 43 | #include "rt_HAL_CA.h" |
mbed_official | 48:e9a2c7cb57a4 | 44 | #else |
mbed_official | 48:e9a2c7cb57a4 | 45 | #include "rt_HAL_CM.h" |
mbed_official | 48:e9a2c7cb57a4 | 46 | #endif |
mbed_official | 48:e9a2c7cb57a4 | 47 | |
mbed_official | 48:e9a2c7cb57a4 | 48 | /*---------------------------------------------------------------------------- |
mbed_official | 48:e9a2c7cb57a4 | 49 | * Global Variables |
mbed_official | 48:e9a2c7cb57a4 | 50 | *---------------------------------------------------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 51 | |
mbed_official | 48:e9a2c7cb57a4 | 52 | /* Running and next task info. */ |
mbed_official | 48:e9a2c7cb57a4 | 53 | struct OS_TSK os_tsk; |
mbed_official | 48:e9a2c7cb57a4 | 54 | |
mbed_official | 48:e9a2c7cb57a4 | 55 | /* Task Control Blocks of idle demon */ |
mbed_official | 48:e9a2c7cb57a4 | 56 | struct OS_TCB os_idle_TCB; |
mbed_official | 48:e9a2c7cb57a4 | 57 | |
mbed_official | 48:e9a2c7cb57a4 | 58 | |
mbed_official | 48:e9a2c7cb57a4 | 59 | /*---------------------------------------------------------------------------- |
mbed_official | 48:e9a2c7cb57a4 | 60 | * Local Functions |
mbed_official | 48:e9a2c7cb57a4 | 61 | *---------------------------------------------------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 62 | |
mbed_official | 48:e9a2c7cb57a4 | 63 | static OS_TID rt_get_TID (void) { |
mbed_official | 48:e9a2c7cb57a4 | 64 | U32 tid; |
mbed_official | 48:e9a2c7cb57a4 | 65 | |
mbed_official | 48:e9a2c7cb57a4 | 66 | for (tid = 1; tid <= os_maxtaskrun; tid++) { |
mbed_official | 48:e9a2c7cb57a4 | 67 | if (os_active_TCB[tid-1] == NULL) { |
mbed_official | 48:e9a2c7cb57a4 | 68 | return ((OS_TID)tid); |
mbed_official | 48:e9a2c7cb57a4 | 69 | } |
mbed_official | 48:e9a2c7cb57a4 | 70 | } |
mbed_official | 48:e9a2c7cb57a4 | 71 | return (0); |
mbed_official | 48:e9a2c7cb57a4 | 72 | } |
mbed_official | 48:e9a2c7cb57a4 | 73 | |
mbed_official | 48:e9a2c7cb57a4 | 74 | |
mbed_official | 48:e9a2c7cb57a4 | 75 | /*--------------------------- rt_init_context -------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 76 | |
mbed_official | 48:e9a2c7cb57a4 | 77 | static void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) { |
mbed_official | 48:e9a2c7cb57a4 | 78 | /* Initialize general part of the Task Control Block. */ |
mbed_official | 48:e9a2c7cb57a4 | 79 | p_TCB->cb_type = TCB; |
mbed_official | 48:e9a2c7cb57a4 | 80 | p_TCB->state = READY; |
mbed_official | 48:e9a2c7cb57a4 | 81 | p_TCB->prio = priority; |
mbed_official | 92:bc9729798a19 | 82 | p_TCB->prio_base = priority; |
mbed_official | 48:e9a2c7cb57a4 | 83 | p_TCB->p_lnk = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 84 | p_TCB->p_rlnk = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 85 | p_TCB->p_dlnk = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 86 | p_TCB->p_blnk = NULL; |
mbed_official | 92:bc9729798a19 | 87 | p_TCB->p_mlnk = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 88 | p_TCB->delta_time = 0; |
mbed_official | 48:e9a2c7cb57a4 | 89 | p_TCB->interval_time = 0; |
mbed_official | 48:e9a2c7cb57a4 | 90 | p_TCB->events = 0; |
mbed_official | 48:e9a2c7cb57a4 | 91 | p_TCB->waits = 0; |
mbed_official | 48:e9a2c7cb57a4 | 92 | p_TCB->stack_frame = 0; |
mbed_official | 48:e9a2c7cb57a4 | 93 | |
mbed_official | 48:e9a2c7cb57a4 | 94 | if (p_TCB->priv_stack == 0) { |
mbed_official | 48:e9a2c7cb57a4 | 95 | /* Allocate the memory space for the stack. */ |
mbed_official | 48:e9a2c7cb57a4 | 96 | p_TCB->stack = rt_alloc_box (mp_stk); |
mbed_official | 48:e9a2c7cb57a4 | 97 | } |
mbed_official | 48:e9a2c7cb57a4 | 98 | rt_init_stack (p_TCB, task_body); |
mbed_official | 48:e9a2c7cb57a4 | 99 | } |
mbed_official | 48:e9a2c7cb57a4 | 100 | |
mbed_official | 48:e9a2c7cb57a4 | 101 | |
mbed_official | 48:e9a2c7cb57a4 | 102 | /*--------------------------- rt_switch_req ---------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 103 | |
mbed_official | 48:e9a2c7cb57a4 | 104 | void rt_switch_req (P_TCB p_new) { |
mbed_official | 48:e9a2c7cb57a4 | 105 | /* Switch to next task (identified by "p_new"). */ |
mbed_official | 92:bc9729798a19 | 106 | os_tsk.new_tsk = p_new; |
mbed_official | 48:e9a2c7cb57a4 | 107 | p_new->state = RUNNING; |
mbed_official | 48:e9a2c7cb57a4 | 108 | DBG_TASK_SWITCH(p_new->task_id); |
mbed_official | 48:e9a2c7cb57a4 | 109 | } |
mbed_official | 48:e9a2c7cb57a4 | 110 | |
mbed_official | 48:e9a2c7cb57a4 | 111 | |
mbed_official | 48:e9a2c7cb57a4 | 112 | /*--------------------------- rt_dispatch -----------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 113 | |
mbed_official | 48:e9a2c7cb57a4 | 114 | void rt_dispatch (P_TCB next_TCB) { |
mbed_official | 48:e9a2c7cb57a4 | 115 | /* Dispatch next task if any identified or dispatch highest ready task */ |
mbed_official | 48:e9a2c7cb57a4 | 116 | /* "next_TCB" identifies a task to run or has value NULL (=no next task) */ |
mbed_official | 48:e9a2c7cb57a4 | 117 | if (next_TCB == NULL) { |
mbed_official | 48:e9a2c7cb57a4 | 118 | /* Running task was blocked: continue with highest ready task */ |
mbed_official | 48:e9a2c7cb57a4 | 119 | next_TCB = rt_get_first (&os_rdy); |
mbed_official | 48:e9a2c7cb57a4 | 120 | rt_switch_req (next_TCB); |
mbed_official | 48:e9a2c7cb57a4 | 121 | } |
mbed_official | 48:e9a2c7cb57a4 | 122 | else { |
mbed_official | 48:e9a2c7cb57a4 | 123 | /* Check which task continues */ |
mbed_official | 48:e9a2c7cb57a4 | 124 | if (next_TCB->prio > os_tsk.run->prio) { |
mbed_official | 48:e9a2c7cb57a4 | 125 | /* preempt running task */ |
mbed_official | 48:e9a2c7cb57a4 | 126 | rt_put_rdy_first (os_tsk.run); |
mbed_official | 48:e9a2c7cb57a4 | 127 | os_tsk.run->state = READY; |
mbed_official | 48:e9a2c7cb57a4 | 128 | rt_switch_req (next_TCB); |
mbed_official | 48:e9a2c7cb57a4 | 129 | } |
mbed_official | 48:e9a2c7cb57a4 | 130 | else { |
mbed_official | 48:e9a2c7cb57a4 | 131 | /* put next task into ready list, no task switch takes place */ |
mbed_official | 48:e9a2c7cb57a4 | 132 | next_TCB->state = READY; |
mbed_official | 48:e9a2c7cb57a4 | 133 | rt_put_prio (&os_rdy, next_TCB); |
mbed_official | 48:e9a2c7cb57a4 | 134 | } |
mbed_official | 48:e9a2c7cb57a4 | 135 | } |
mbed_official | 48:e9a2c7cb57a4 | 136 | } |
mbed_official | 48:e9a2c7cb57a4 | 137 | |
mbed_official | 48:e9a2c7cb57a4 | 138 | |
mbed_official | 48:e9a2c7cb57a4 | 139 | /*--------------------------- rt_block --------------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 140 | |
mbed_official | 48:e9a2c7cb57a4 | 141 | void rt_block (U16 timeout, U8 block_state) { |
mbed_official | 48:e9a2c7cb57a4 | 142 | /* Block running task and choose next ready task. */ |
mbed_official | 48:e9a2c7cb57a4 | 143 | /* "timeout" sets a time-out value or is 0xffff (=no time-out). */ |
mbed_official | 48:e9a2c7cb57a4 | 144 | /* "block_state" defines the appropriate task state */ |
mbed_official | 48:e9a2c7cb57a4 | 145 | P_TCB next_TCB; |
mbed_official | 48:e9a2c7cb57a4 | 146 | |
mbed_official | 48:e9a2c7cb57a4 | 147 | if (timeout) { |
mbed_official | 48:e9a2c7cb57a4 | 148 | if (timeout < 0xffff) { |
mbed_official | 48:e9a2c7cb57a4 | 149 | rt_put_dly (os_tsk.run, timeout); |
mbed_official | 48:e9a2c7cb57a4 | 150 | } |
mbed_official | 48:e9a2c7cb57a4 | 151 | os_tsk.run->state = block_state; |
mbed_official | 48:e9a2c7cb57a4 | 152 | next_TCB = rt_get_first (&os_rdy); |
mbed_official | 48:e9a2c7cb57a4 | 153 | rt_switch_req (next_TCB); |
mbed_official | 48:e9a2c7cb57a4 | 154 | } |
mbed_official | 48:e9a2c7cb57a4 | 155 | } |
mbed_official | 48:e9a2c7cb57a4 | 156 | |
mbed_official | 48:e9a2c7cb57a4 | 157 | |
mbed_official | 48:e9a2c7cb57a4 | 158 | /*--------------------------- rt_tsk_pass -----------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 159 | |
mbed_official | 48:e9a2c7cb57a4 | 160 | void rt_tsk_pass (void) { |
mbed_official | 48:e9a2c7cb57a4 | 161 | /* Allow tasks of same priority level to run cooperatively.*/ |
mbed_official | 48:e9a2c7cb57a4 | 162 | P_TCB p_new; |
mbed_official | 48:e9a2c7cb57a4 | 163 | |
mbed_official | 48:e9a2c7cb57a4 | 164 | p_new = rt_get_same_rdy_prio(); |
mbed_official | 48:e9a2c7cb57a4 | 165 | if (p_new != NULL) { |
mbed_official | 48:e9a2c7cb57a4 | 166 | rt_put_prio ((P_XCB)&os_rdy, os_tsk.run); |
mbed_official | 48:e9a2c7cb57a4 | 167 | os_tsk.run->state = READY; |
mbed_official | 48:e9a2c7cb57a4 | 168 | rt_switch_req (p_new); |
mbed_official | 48:e9a2c7cb57a4 | 169 | } |
mbed_official | 48:e9a2c7cb57a4 | 170 | } |
mbed_official | 48:e9a2c7cb57a4 | 171 | |
mbed_official | 48:e9a2c7cb57a4 | 172 | |
mbed_official | 48:e9a2c7cb57a4 | 173 | /*--------------------------- rt_tsk_self -----------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 174 | |
mbed_official | 48:e9a2c7cb57a4 | 175 | OS_TID rt_tsk_self (void) { |
mbed_official | 48:e9a2c7cb57a4 | 176 | /* Return own task identifier value. */ |
mbed_official | 48:e9a2c7cb57a4 | 177 | if (os_tsk.run == NULL) { |
mbed_official | 48:e9a2c7cb57a4 | 178 | return (0); |
mbed_official | 48:e9a2c7cb57a4 | 179 | } |
mbed_official | 48:e9a2c7cb57a4 | 180 | return (os_tsk.run->task_id); |
mbed_official | 48:e9a2c7cb57a4 | 181 | } |
mbed_official | 48:e9a2c7cb57a4 | 182 | |
mbed_official | 48:e9a2c7cb57a4 | 183 | |
mbed_official | 48:e9a2c7cb57a4 | 184 | /*--------------------------- rt_tsk_prio -----------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 185 | |
mbed_official | 48:e9a2c7cb57a4 | 186 | OS_RESULT rt_tsk_prio (OS_TID task_id, U8 new_prio) { |
mbed_official | 48:e9a2c7cb57a4 | 187 | /* Change execution priority of a task to "new_prio". */ |
mbed_official | 48:e9a2c7cb57a4 | 188 | P_TCB p_task; |
mbed_official | 48:e9a2c7cb57a4 | 189 | |
mbed_official | 48:e9a2c7cb57a4 | 190 | if (task_id == 0) { |
mbed_official | 48:e9a2c7cb57a4 | 191 | /* Change execution priority of calling task. */ |
mbed_official | 48:e9a2c7cb57a4 | 192 | os_tsk.run->prio = new_prio; |
mbed_official | 92:bc9729798a19 | 193 | os_tsk.run->prio_base = new_prio; |
mbed_official | 48:e9a2c7cb57a4 | 194 | run:if (rt_rdy_prio() > new_prio) { |
mbed_official | 48:e9a2c7cb57a4 | 195 | rt_put_prio (&os_rdy, os_tsk.run); |
mbed_official | 48:e9a2c7cb57a4 | 196 | os_tsk.run->state = READY; |
mbed_official | 48:e9a2c7cb57a4 | 197 | rt_dispatch (NULL); |
mbed_official | 48:e9a2c7cb57a4 | 198 | } |
mbed_official | 48:e9a2c7cb57a4 | 199 | return (OS_R_OK); |
mbed_official | 48:e9a2c7cb57a4 | 200 | } |
mbed_official | 48:e9a2c7cb57a4 | 201 | |
mbed_official | 48:e9a2c7cb57a4 | 202 | /* Find the task in the "os_active_TCB" array. */ |
mbed_official | 48:e9a2c7cb57a4 | 203 | if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) { |
mbed_official | 48:e9a2c7cb57a4 | 204 | /* Task with "task_id" not found or not started. */ |
mbed_official | 48:e9a2c7cb57a4 | 205 | return (OS_R_NOK); |
mbed_official | 48:e9a2c7cb57a4 | 206 | } |
mbed_official | 48:e9a2c7cb57a4 | 207 | p_task = os_active_TCB[task_id-1]; |
mbed_official | 48:e9a2c7cb57a4 | 208 | p_task->prio = new_prio; |
mbed_official | 92:bc9729798a19 | 209 | p_task->prio_base = new_prio; |
mbed_official | 48:e9a2c7cb57a4 | 210 | if (p_task == os_tsk.run) { |
mbed_official | 48:e9a2c7cb57a4 | 211 | goto run; |
mbed_official | 48:e9a2c7cb57a4 | 212 | } |
mbed_official | 48:e9a2c7cb57a4 | 213 | rt_resort_prio (p_task); |
mbed_official | 48:e9a2c7cb57a4 | 214 | if (p_task->state == READY) { |
mbed_official | 48:e9a2c7cb57a4 | 215 | /* Task enqueued in a ready list. */ |
mbed_official | 48:e9a2c7cb57a4 | 216 | p_task = rt_get_first (&os_rdy); |
mbed_official | 48:e9a2c7cb57a4 | 217 | rt_dispatch (p_task); |
mbed_official | 48:e9a2c7cb57a4 | 218 | } |
mbed_official | 48:e9a2c7cb57a4 | 219 | return (OS_R_OK); |
mbed_official | 48:e9a2c7cb57a4 | 220 | } |
mbed_official | 48:e9a2c7cb57a4 | 221 | |
mbed_official | 48:e9a2c7cb57a4 | 222 | |
mbed_official | 48:e9a2c7cb57a4 | 223 | /*--------------------------- rt_tsk_create ---------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 224 | |
mbed_official | 48:e9a2c7cb57a4 | 225 | OS_TID rt_tsk_create (FUNCP task, U32 prio_stksz, void *stk, void *argv) { |
mbed_official | 48:e9a2c7cb57a4 | 226 | /* Start a new task declared with "task". */ |
mbed_official | 48:e9a2c7cb57a4 | 227 | P_TCB task_context; |
mbed_official | 48:e9a2c7cb57a4 | 228 | U32 i; |
mbed_official | 48:e9a2c7cb57a4 | 229 | |
mbed_official | 48:e9a2c7cb57a4 | 230 | /* Priority 0 is reserved for idle task! */ |
mbed_official | 48:e9a2c7cb57a4 | 231 | if ((prio_stksz & 0xFF) == 0) { |
mbed_official | 48:e9a2c7cb57a4 | 232 | prio_stksz += 1; |
mbed_official | 48:e9a2c7cb57a4 | 233 | } |
mbed_official | 48:e9a2c7cb57a4 | 234 | task_context = rt_alloc_box (mp_tcb); |
mbed_official | 48:e9a2c7cb57a4 | 235 | if (task_context == NULL) { |
mbed_official | 48:e9a2c7cb57a4 | 236 | return (0); |
mbed_official | 48:e9a2c7cb57a4 | 237 | } |
mbed_official | 48:e9a2c7cb57a4 | 238 | /* If "size != 0" use a private user provided stack. */ |
mbed_official | 48:e9a2c7cb57a4 | 239 | task_context->stack = stk; |
mbed_official | 48:e9a2c7cb57a4 | 240 | task_context->priv_stack = prio_stksz >> 8; |
mbed_official | 48:e9a2c7cb57a4 | 241 | /* Pass parameter 'argv' to 'rt_init_context' */ |
mbed_official | 48:e9a2c7cb57a4 | 242 | task_context->msg = argv; |
mbed_official | 48:e9a2c7cb57a4 | 243 | /* For 'size == 0' system allocates the user stack from the memory pool. */ |
mbed_official | 48:e9a2c7cb57a4 | 244 | rt_init_context (task_context, prio_stksz & 0xFF, task); |
mbed_official | 48:e9a2c7cb57a4 | 245 | |
mbed_official | 48:e9a2c7cb57a4 | 246 | /* Find a free entry in 'os_active_TCB' table. */ |
mbed_official | 48:e9a2c7cb57a4 | 247 | i = rt_get_TID (); |
mbed_official | 48:e9a2c7cb57a4 | 248 | os_active_TCB[i-1] = task_context; |
mbed_official | 48:e9a2c7cb57a4 | 249 | task_context->task_id = i; |
mbed_official | 48:e9a2c7cb57a4 | 250 | DBG_TASK_NOTIFY(task_context, __TRUE); |
mbed_official | 48:e9a2c7cb57a4 | 251 | rt_dispatch (task_context); |
mbed_official | 48:e9a2c7cb57a4 | 252 | return ((OS_TID)i); |
mbed_official | 48:e9a2c7cb57a4 | 253 | } |
mbed_official | 48:e9a2c7cb57a4 | 254 | |
mbed_official | 48:e9a2c7cb57a4 | 255 | |
mbed_official | 48:e9a2c7cb57a4 | 256 | /*--------------------------- rt_tsk_delete ---------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 257 | |
mbed_official | 48:e9a2c7cb57a4 | 258 | OS_RESULT rt_tsk_delete (OS_TID task_id) { |
mbed_official | 48:e9a2c7cb57a4 | 259 | /* Terminate the task identified with "task_id". */ |
mbed_official | 48:e9a2c7cb57a4 | 260 | P_TCB task_context; |
mbed_official | 92:bc9729798a19 | 261 | P_TCB p_TCB; |
mbed_official | 92:bc9729798a19 | 262 | P_MUCB p_MCB, p_MCB0; |
mbed_official | 48:e9a2c7cb57a4 | 263 | |
mbed_official | 48:e9a2c7cb57a4 | 264 | if (task_id == 0 || task_id == os_tsk.run->task_id) { |
mbed_official | 48:e9a2c7cb57a4 | 265 | /* Terminate itself. */ |
mbed_official | 48:e9a2c7cb57a4 | 266 | os_tsk.run->state = INACTIVE; |
mbed_official | 48:e9a2c7cb57a4 | 267 | os_tsk.run->tsk_stack = rt_get_PSP (); |
mbed_official | 48:e9a2c7cb57a4 | 268 | rt_stk_check (); |
mbed_official | 92:bc9729798a19 | 269 | p_MCB = os_tsk.run->p_mlnk; |
mbed_official | 92:bc9729798a19 | 270 | while (p_MCB) { |
mbed_official | 92:bc9729798a19 | 271 | /* Release mutexes owned by this task */ |
mbed_official | 92:bc9729798a19 | 272 | if (p_MCB->p_lnk) { |
mbed_official | 92:bc9729798a19 | 273 | /* A task is waiting for mutex. */ |
mbed_official | 92:bc9729798a19 | 274 | p_TCB = rt_get_first ((P_XCB)p_MCB); |
mbed_official | 92:bc9729798a19 | 275 | #ifdef __CMSIS_RTOS |
mbed_official | 92:bc9729798a19 | 276 | rt_ret_val(p_TCB, 0/*osOK*/); |
mbed_official | 92:bc9729798a19 | 277 | #else |
mbed_official | 92:bc9729798a19 | 278 | rt_ret_val(p_TCB, OS_R_MUT); |
mbed_official | 92:bc9729798a19 | 279 | #endif |
mbed_official | 92:bc9729798a19 | 280 | rt_rmv_dly (p_TCB); |
mbed_official | 92:bc9729798a19 | 281 | p_TCB->state = READY; |
mbed_official | 92:bc9729798a19 | 282 | rt_put_prio (&os_rdy, p_TCB); |
mbed_official | 92:bc9729798a19 | 283 | /* A waiting task becomes the owner of this mutex. */ |
mbed_official | 92:bc9729798a19 | 284 | p_MCB0 = p_MCB; |
mbed_official | 92:bc9729798a19 | 285 | p_MCB->level = 1; |
mbed_official | 92:bc9729798a19 | 286 | p_MCB->owner = p_TCB; |
mbed_official | 92:bc9729798a19 | 287 | p_MCB->p_mlnk = p_TCB->p_mlnk; |
mbed_official | 92:bc9729798a19 | 288 | p_TCB->p_mlnk = p_MCB; |
mbed_official | 92:bc9729798a19 | 289 | p_MCB = p_MCB0->p_mlnk; |
mbed_official | 92:bc9729798a19 | 290 | } |
mbed_official | 92:bc9729798a19 | 291 | else { |
mbed_official | 92:bc9729798a19 | 292 | p_MCB = p_MCB->p_mlnk; |
mbed_official | 92:bc9729798a19 | 293 | } |
mbed_official | 92:bc9729798a19 | 294 | } |
mbed_official | 48:e9a2c7cb57a4 | 295 | os_active_TCB[os_tsk.run->task_id-1] = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 296 | rt_free_box (mp_stk, os_tsk.run->stack); |
mbed_official | 48:e9a2c7cb57a4 | 297 | os_tsk.run->stack = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 298 | DBG_TASK_NOTIFY(os_tsk.run, __FALSE); |
mbed_official | 48:e9a2c7cb57a4 | 299 | rt_free_box (mp_tcb, os_tsk.run); |
mbed_official | 48:e9a2c7cb57a4 | 300 | os_tsk.run = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 301 | rt_dispatch (NULL); |
mbed_official | 48:e9a2c7cb57a4 | 302 | /* The program should never come to this point. */ |
mbed_official | 48:e9a2c7cb57a4 | 303 | } |
mbed_official | 48:e9a2c7cb57a4 | 304 | else { |
mbed_official | 48:e9a2c7cb57a4 | 305 | /* Find the task in the "os_active_TCB" array. */ |
mbed_official | 48:e9a2c7cb57a4 | 306 | if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) { |
mbed_official | 48:e9a2c7cb57a4 | 307 | /* Task with "task_id" not found or not started. */ |
mbed_official | 48:e9a2c7cb57a4 | 308 | return (OS_R_NOK); |
mbed_official | 48:e9a2c7cb57a4 | 309 | } |
mbed_official | 48:e9a2c7cb57a4 | 310 | task_context = os_active_TCB[task_id-1]; |
mbed_official | 48:e9a2c7cb57a4 | 311 | rt_rmv_list (task_context); |
mbed_official | 48:e9a2c7cb57a4 | 312 | rt_rmv_dly (task_context); |
mbed_official | 92:bc9729798a19 | 313 | p_MCB = task_context->p_mlnk; |
mbed_official | 92:bc9729798a19 | 314 | while (p_MCB) { |
mbed_official | 92:bc9729798a19 | 315 | /* Release mutexes owned by this task */ |
mbed_official | 92:bc9729798a19 | 316 | if (p_MCB->p_lnk) { |
mbed_official | 92:bc9729798a19 | 317 | /* A task is waiting for mutex. */ |
mbed_official | 92:bc9729798a19 | 318 | p_TCB = rt_get_first ((P_XCB)p_MCB); |
mbed_official | 92:bc9729798a19 | 319 | #ifdef __CMSIS_RTOS |
mbed_official | 92:bc9729798a19 | 320 | rt_ret_val(p_TCB, 0/*osOK*/); |
mbed_official | 92:bc9729798a19 | 321 | #else |
mbed_official | 92:bc9729798a19 | 322 | rt_ret_val(p_TCB, OS_R_MUT); |
mbed_official | 92:bc9729798a19 | 323 | #endif |
mbed_official | 92:bc9729798a19 | 324 | rt_rmv_dly (p_TCB); |
mbed_official | 92:bc9729798a19 | 325 | p_TCB->state = READY; |
mbed_official | 92:bc9729798a19 | 326 | rt_put_prio (&os_rdy, p_TCB); |
mbed_official | 92:bc9729798a19 | 327 | /* A waiting task becomes the owner of this mutex. */ |
mbed_official | 92:bc9729798a19 | 328 | p_MCB0 = p_MCB; |
mbed_official | 92:bc9729798a19 | 329 | p_MCB->level = 1; |
mbed_official | 92:bc9729798a19 | 330 | p_MCB->owner = p_TCB; |
mbed_official | 92:bc9729798a19 | 331 | p_MCB->p_mlnk = p_TCB->p_mlnk; |
mbed_official | 92:bc9729798a19 | 332 | p_TCB->p_mlnk = p_MCB; |
mbed_official | 92:bc9729798a19 | 333 | p_MCB = p_MCB0->p_mlnk; |
mbed_official | 92:bc9729798a19 | 334 | } |
mbed_official | 92:bc9729798a19 | 335 | else { |
mbed_official | 92:bc9729798a19 | 336 | p_MCB = p_MCB->p_mlnk; |
mbed_official | 92:bc9729798a19 | 337 | } |
mbed_official | 92:bc9729798a19 | 338 | } |
mbed_official | 48:e9a2c7cb57a4 | 339 | os_active_TCB[task_id-1] = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 340 | rt_free_box (mp_stk, task_context->stack); |
mbed_official | 48:e9a2c7cb57a4 | 341 | task_context->stack = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 342 | DBG_TASK_NOTIFY(task_context, __FALSE); |
mbed_official | 48:e9a2c7cb57a4 | 343 | rt_free_box (mp_tcb, task_context); |
mbed_official | 92:bc9729798a19 | 344 | if (rt_rdy_prio() > os_tsk.run->prio) { |
mbed_official | 92:bc9729798a19 | 345 | /* Ready task has higher priority than running task. */ |
mbed_official | 92:bc9729798a19 | 346 | os_tsk.run->state = READY; |
mbed_official | 92:bc9729798a19 | 347 | rt_put_prio (&os_rdy, os_tsk.run); |
mbed_official | 92:bc9729798a19 | 348 | rt_dispatch (NULL); |
mbed_official | 92:bc9729798a19 | 349 | } |
mbed_official | 48:e9a2c7cb57a4 | 350 | } |
mbed_official | 48:e9a2c7cb57a4 | 351 | return (OS_R_OK); |
mbed_official | 48:e9a2c7cb57a4 | 352 | } |
mbed_official | 48:e9a2c7cb57a4 | 353 | |
mbed_official | 48:e9a2c7cb57a4 | 354 | |
mbed_official | 48:e9a2c7cb57a4 | 355 | /*--------------------------- rt_sys_init -----------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 356 | |
mbed_official | 48:e9a2c7cb57a4 | 357 | #ifdef __CMSIS_RTOS |
mbed_official | 48:e9a2c7cb57a4 | 358 | void rt_sys_init (void) { |
mbed_official | 48:e9a2c7cb57a4 | 359 | #else |
mbed_official | 48:e9a2c7cb57a4 | 360 | void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) { |
mbed_official | 48:e9a2c7cb57a4 | 361 | #endif |
mbed_official | 48:e9a2c7cb57a4 | 362 | /* Initialize system and start up task declared with "first_task". */ |
mbed_official | 48:e9a2c7cb57a4 | 363 | U32 i; |
mbed_official | 48:e9a2c7cb57a4 | 364 | |
mbed_official | 48:e9a2c7cb57a4 | 365 | DBG_INIT(); |
mbed_official | 48:e9a2c7cb57a4 | 366 | |
mbed_official | 48:e9a2c7cb57a4 | 367 | /* Initialize dynamic memory and task TCB pointers to NULL. */ |
mbed_official | 48:e9a2c7cb57a4 | 368 | for (i = 0; i < os_maxtaskrun; i++) { |
mbed_official | 48:e9a2c7cb57a4 | 369 | os_active_TCB[i] = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 370 | } |
mbed_official | 48:e9a2c7cb57a4 | 371 | rt_init_box (&mp_tcb, mp_tcb_size, sizeof(struct OS_TCB)); |
mbed_official | 48:e9a2c7cb57a4 | 372 | rt_init_box (&mp_stk, mp_stk_size, BOX_ALIGN_8 | (U16)(os_stackinfo)); |
mbed_official | 48:e9a2c7cb57a4 | 373 | rt_init_box ((U32 *)m_tmr, mp_tmr_size, sizeof(struct OS_TMR)); |
mbed_official | 48:e9a2c7cb57a4 | 374 | |
mbed_official | 48:e9a2c7cb57a4 | 375 | /* Set up TCB of idle demon */ |
mbed_official | 48:e9a2c7cb57a4 | 376 | os_idle_TCB.task_id = 255; |
mbed_official | 48:e9a2c7cb57a4 | 377 | os_idle_TCB.priv_stack = 0; |
mbed_official | 48:e9a2c7cb57a4 | 378 | rt_init_context (&os_idle_TCB, 0, os_idle_demon); |
mbed_official | 48:e9a2c7cb57a4 | 379 | |
mbed_official | 48:e9a2c7cb57a4 | 380 | /* Set up ready list: initially empty */ |
mbed_official | 48:e9a2c7cb57a4 | 381 | os_rdy.cb_type = HCB; |
mbed_official | 48:e9a2c7cb57a4 | 382 | os_rdy.p_lnk = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 383 | /* Set up delay list: initially empty */ |
mbed_official | 48:e9a2c7cb57a4 | 384 | os_dly.cb_type = HCB; |
mbed_official | 48:e9a2c7cb57a4 | 385 | os_dly.p_dlnk = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 386 | os_dly.p_blnk = NULL; |
mbed_official | 48:e9a2c7cb57a4 | 387 | os_dly.delta_time = 0; |
mbed_official | 48:e9a2c7cb57a4 | 388 | |
mbed_official | 48:e9a2c7cb57a4 | 389 | /* Fix SP and system variables to assume idle task is running */ |
mbed_official | 48:e9a2c7cb57a4 | 390 | /* Transform main program into idle task by assuming idle TCB */ |
mbed_official | 48:e9a2c7cb57a4 | 391 | #ifndef __CMSIS_RTOS |
mbed_official | 48:e9a2c7cb57a4 | 392 | rt_set_PSP (os_idle_TCB.tsk_stack+32); |
mbed_official | 48:e9a2c7cb57a4 | 393 | #endif |
mbed_official | 48:e9a2c7cb57a4 | 394 | os_tsk.run = &os_idle_TCB; |
mbed_official | 48:e9a2c7cb57a4 | 395 | os_tsk.run->state = RUNNING; |
mbed_official | 48:e9a2c7cb57a4 | 396 | |
mbed_official | 48:e9a2c7cb57a4 | 397 | /* Initialize ps queue */ |
mbed_official | 48:e9a2c7cb57a4 | 398 | os_psq->first = 0; |
mbed_official | 48:e9a2c7cb57a4 | 399 | os_psq->last = 0; |
mbed_official | 48:e9a2c7cb57a4 | 400 | os_psq->size = os_fifo_size; |
mbed_official | 48:e9a2c7cb57a4 | 401 | |
mbed_official | 48:e9a2c7cb57a4 | 402 | rt_init_robin (); |
mbed_official | 48:e9a2c7cb57a4 | 403 | |
mbed_official | 48:e9a2c7cb57a4 | 404 | /* Initialize SVC and PendSV */ |
mbed_official | 48:e9a2c7cb57a4 | 405 | rt_svc_init (); |
mbed_official | 48:e9a2c7cb57a4 | 406 | |
mbed_official | 48:e9a2c7cb57a4 | 407 | #ifndef __CMSIS_RTOS |
mbed_official | 48:e9a2c7cb57a4 | 408 | /* Initialize and start system clock timer */ |
mbed_official | 48:e9a2c7cb57a4 | 409 | os_tick_irqn = os_tick_init (); |
mbed_official | 48:e9a2c7cb57a4 | 410 | if (os_tick_irqn >= 0) { |
mbed_official | 48:e9a2c7cb57a4 | 411 | OS_X_INIT(os_tick_irqn); |
mbed_official | 48:e9a2c7cb57a4 | 412 | } |
mbed_official | 48:e9a2c7cb57a4 | 413 | |
mbed_official | 48:e9a2c7cb57a4 | 414 | /* Start up first user task before entering the endless loop */ |
mbed_official | 48:e9a2c7cb57a4 | 415 | rt_tsk_create (first_task, prio_stksz, stk, NULL); |
mbed_official | 48:e9a2c7cb57a4 | 416 | #endif |
mbed_official | 48:e9a2c7cb57a4 | 417 | } |
mbed_official | 48:e9a2c7cb57a4 | 418 | |
mbed_official | 48:e9a2c7cb57a4 | 419 | |
mbed_official | 48:e9a2c7cb57a4 | 420 | /*--------------------------- rt_sys_start ----------------------------------*/ |
mbed_official | 48:e9a2c7cb57a4 | 421 | |
mbed_official | 48:e9a2c7cb57a4 | 422 | #ifdef __CMSIS_RTOS |
mbed_official | 48:e9a2c7cb57a4 | 423 | void rt_sys_start (void) { |
mbed_official | 48:e9a2c7cb57a4 | 424 | /* Start system */ |
mbed_official | 48:e9a2c7cb57a4 | 425 | |
mbed_official | 48:e9a2c7cb57a4 | 426 | /* Initialize and start system clock timer */ |
mbed_official | 48:e9a2c7cb57a4 | 427 | os_tick_irqn = os_tick_init (); |
mbed_official | 48:e9a2c7cb57a4 | 428 | if (os_tick_irqn >= 0) { |
mbed_official | 48:e9a2c7cb57a4 | 429 | OS_X_INIT(os_tick_irqn); |
mbed_official | 48:e9a2c7cb57a4 | 430 | } |
mbed_official | 48:e9a2c7cb57a4 | 431 | } |
mbed_official | 48:e9a2c7cb57a4 | 432 | #endif |
mbed_official | 48:e9a2c7cb57a4 | 433 | |
mbed_official | 48:e9a2c7cb57a4 | 434 | /*---------------------------------------------------------------------------- |
mbed_official | 48:e9a2c7cb57a4 | 435 | * end of file |
mbed_official | 48:e9a2c7cb57a4 | 436 | *---------------------------------------------------------------------------*/ |