Maintain legacy RTOS behavior before mbed-5

Fork of mbed-rtos by mbed official

Committer:
mbed_official
Date:
Thu May 05 20:45:13 2016 +0100
Revision:
112:53ace74b190c
Parent:
49:77c8e4604045
Child:
118:6635230e06ba
Synchronized with git revision 860fdd282b0dc3631a6c46b39442d4ab5343e534

Full URL: https://github.com/mbedmicro/mbed/commit/860fdd282b0dc3631a6c46b39442d4ab5343e534/

rtx update to v4.79

Who changed what in which revision?

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