Clone of mbed rtos 2 for easy accessibility.
Fork of mbed-rtos by
rtx/TARGET_CORTEX_M/rt_Task.c@118:6635230e06ba, 2016-07-25 (annotated)
- Committer:
- Kojto
- Date:
- Mon Jul 25 14:12:24 2016 +0100
- Revision:
- 118:6635230e06ba
- Parent:
- 112:53ace74b190c
- Child:
- 123:58563e6cba1e
RTOS rev118
Compatible with the mbed library v122
Changes:
- warnings about duplicated CM symbols fix
- init sequence update - allows init array to be run prior kernel start
- RTOS with OS_TIMERS=0 fix for thread id
- Thread ctor is deprecated, use start() method
- main stack fix for IAR (set via linker script)
- add TCB context pointer
- provide thread safety for toolchains (std lib locks)
- add MBED_RTOS_SINGLE_THREAD macro (sets TSKCNT to 1 and TIMERS to 0)
- nrf51, nucleo l423kc, nucleo f767zi, nucleo f446ze, efm32 support addition
- add OSObserver function
Who changed what in which revision?
User | Revision | Line number | New 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" |
Kojto | 118:6635230e06ba | 43 | #include "rt_OsEventObserver.h" |
mbed_official | 49:77c8e4604045 | 44 | |
mbed_official | 49:77c8e4604045 | 45 | /*---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 46 | * Global Variables |
mbed_official | 49:77c8e4604045 | 47 | *---------------------------------------------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 48 | |
mbed_official | 49:77c8e4604045 | 49 | /* Running and next task info. */ |
mbed_official | 49:77c8e4604045 | 50 | struct OS_TSK os_tsk; |
mbed_official | 49:77c8e4604045 | 51 | |
mbed_official | 49:77c8e4604045 | 52 | /* Task Control Blocks of idle demon */ |
mbed_official | 49:77c8e4604045 | 53 | struct OS_TCB os_idle_TCB; |
mbed_official | 49:77c8e4604045 | 54 | |
mbed_official | 49:77c8e4604045 | 55 | |
mbed_official | 49:77c8e4604045 | 56 | /*---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 57 | * Local Functions |
mbed_official | 49:77c8e4604045 | 58 | *---------------------------------------------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 59 | |
mbed_official | 112:53ace74b190c | 60 | static OS_TID rt_get_TID (void) { |
mbed_official | 49:77c8e4604045 | 61 | U32 tid; |
mbed_official | 49:77c8e4604045 | 62 | |
mbed_official | 112:53ace74b190c | 63 | for (tid = 1U; tid <= os_maxtaskrun; tid++) { |
mbed_official | 112:53ace74b190c | 64 | if (os_active_TCB[tid-1U] == NULL) { |
mbed_official | 49:77c8e4604045 | 65 | return ((OS_TID)tid); |
mbed_official | 49:77c8e4604045 | 66 | } |
mbed_official | 49:77c8e4604045 | 67 | } |
mbed_official | 112:53ace74b190c | 68 | return (0U); |
mbed_official | 49:77c8e4604045 | 69 | } |
mbed_official | 49:77c8e4604045 | 70 | |
mbed_official | 49:77c8e4604045 | 71 | |
mbed_official | 49:77c8e4604045 | 72 | /*--------------------------- rt_init_context -------------------------------*/ |
mbed_official | 49:77c8e4604045 | 73 | |
mbed_official | 112:53ace74b190c | 74 | static void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) { |
mbed_official | 49:77c8e4604045 | 75 | /* Initialize general part of the Task Control Block. */ |
mbed_official | 112:53ace74b190c | 76 | p_TCB->cb_type = TCB; |
mbed_official | 112:53ace74b190c | 77 | p_TCB->state = READY; |
mbed_official | 112:53ace74b190c | 78 | p_TCB->prio = priority; |
mbed_official | 112:53ace74b190c | 79 | p_TCB->prio_base = priority; |
mbed_official | 112:53ace74b190c | 80 | p_TCB->p_lnk = NULL; |
mbed_official | 112:53ace74b190c | 81 | p_TCB->p_rlnk = NULL; |
mbed_official | 112:53ace74b190c | 82 | p_TCB->p_dlnk = NULL; |
mbed_official | 112:53ace74b190c | 83 | p_TCB->p_blnk = NULL; |
mbed_official | 112:53ace74b190c | 84 | p_TCB->p_mlnk = NULL; |
mbed_official | 112:53ace74b190c | 85 | p_TCB->delta_time = 0U; |
mbed_official | 112:53ace74b190c | 86 | p_TCB->interval_time = 0U; |
mbed_official | 112:53ace74b190c | 87 | p_TCB->events = 0U; |
mbed_official | 112:53ace74b190c | 88 | p_TCB->waits = 0U; |
mbed_official | 112:53ace74b190c | 89 | p_TCB->stack_frame = 0U; |
mbed_official | 49:77c8e4604045 | 90 | |
mbed_official | 112:53ace74b190c | 91 | if (p_TCB->priv_stack == 0U) { |
mbed_official | 112:53ace74b190c | 92 | /* Allocate the memory space for the stack. */ |
mbed_official | 112:53ace74b190c | 93 | p_TCB->stack = rt_alloc_box (mp_stk); |
mbed_official | 112:53ace74b190c | 94 | } |
mbed_official | 49:77c8e4604045 | 95 | rt_init_stack (p_TCB, task_body); |
mbed_official | 49:77c8e4604045 | 96 | } |
mbed_official | 49:77c8e4604045 | 97 | |
mbed_official | 49:77c8e4604045 | 98 | |
mbed_official | 49:77c8e4604045 | 99 | /*--------------------------- rt_switch_req ---------------------------------*/ |
mbed_official | 49:77c8e4604045 | 100 | |
mbed_official | 49:77c8e4604045 | 101 | void rt_switch_req (P_TCB p_new) { |
mbed_official | 49:77c8e4604045 | 102 | /* Switch to next task (identified by "p_new"). */ |
mbed_official | 49:77c8e4604045 | 103 | os_tsk.new_tsk = p_new; |
mbed_official | 49:77c8e4604045 | 104 | p_new->state = RUNNING; |
Kojto | 118:6635230e06ba | 105 | if (osEventObs && osEventObs->thread_switch) { |
Kojto | 118:6635230e06ba | 106 | osEventObs->thread_switch(p_new->context); |
Kojto | 118:6635230e06ba | 107 | } |
mbed_official | 49:77c8e4604045 | 108 | DBG_TASK_SWITCH(p_new->task_id); |
mbed_official | 49:77c8e4604045 | 109 | } |
mbed_official | 49:77c8e4604045 | 110 | |
mbed_official | 49:77c8e4604045 | 111 | |
mbed_official | 49:77c8e4604045 | 112 | /*--------------------------- rt_dispatch -----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 113 | |
mbed_official | 49:77c8e4604045 | 114 | void rt_dispatch (P_TCB next_TCB) { |
mbed_official | 49:77c8e4604045 | 115 | /* Dispatch next task if any identified or dispatch highest ready task */ |
mbed_official | 49:77c8e4604045 | 116 | /* "next_TCB" identifies a task to run or has value NULL (=no next task) */ |
mbed_official | 49:77c8e4604045 | 117 | if (next_TCB == NULL) { |
mbed_official | 49:77c8e4604045 | 118 | /* Running task was blocked: continue with highest ready task */ |
mbed_official | 49:77c8e4604045 | 119 | next_TCB = rt_get_first (&os_rdy); |
mbed_official | 49:77c8e4604045 | 120 | rt_switch_req (next_TCB); |
mbed_official | 49:77c8e4604045 | 121 | } |
mbed_official | 49:77c8e4604045 | 122 | else { |
mbed_official | 49:77c8e4604045 | 123 | /* Check which task continues */ |
mbed_official | 49:77c8e4604045 | 124 | if (next_TCB->prio > os_tsk.run->prio) { |
mbed_official | 49:77c8e4604045 | 125 | /* preempt running task */ |
mbed_official | 49:77c8e4604045 | 126 | rt_put_rdy_first (os_tsk.run); |
mbed_official | 49:77c8e4604045 | 127 | os_tsk.run->state = READY; |
mbed_official | 49:77c8e4604045 | 128 | rt_switch_req (next_TCB); |
mbed_official | 49:77c8e4604045 | 129 | } |
mbed_official | 49:77c8e4604045 | 130 | else { |
mbed_official | 49:77c8e4604045 | 131 | /* put next task into ready list, no task switch takes place */ |
mbed_official | 49:77c8e4604045 | 132 | next_TCB->state = READY; |
mbed_official | 49:77c8e4604045 | 133 | rt_put_prio (&os_rdy, next_TCB); |
mbed_official | 49:77c8e4604045 | 134 | } |
mbed_official | 49:77c8e4604045 | 135 | } |
mbed_official | 49:77c8e4604045 | 136 | } |
mbed_official | 49:77c8e4604045 | 137 | |
mbed_official | 49:77c8e4604045 | 138 | |
mbed_official | 49:77c8e4604045 | 139 | /*--------------------------- rt_block --------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 140 | |
mbed_official | 49:77c8e4604045 | 141 | void rt_block (U16 timeout, U8 block_state) { |
mbed_official | 49:77c8e4604045 | 142 | /* Block running task and choose next ready task. */ |
mbed_official | 49:77c8e4604045 | 143 | /* "timeout" sets a time-out value or is 0xffff (=no time-out). */ |
mbed_official | 49:77c8e4604045 | 144 | /* "block_state" defines the appropriate task state */ |
mbed_official | 49:77c8e4604045 | 145 | P_TCB next_TCB; |
mbed_official | 49:77c8e4604045 | 146 | |
mbed_official | 49:77c8e4604045 | 147 | if (timeout) { |
mbed_official | 112:53ace74b190c | 148 | if (timeout < 0xFFFFU) { |
mbed_official | 49:77c8e4604045 | 149 | rt_put_dly (os_tsk.run, timeout); |
mbed_official | 49:77c8e4604045 | 150 | } |
mbed_official | 49:77c8e4604045 | 151 | os_tsk.run->state = block_state; |
mbed_official | 49:77c8e4604045 | 152 | next_TCB = rt_get_first (&os_rdy); |
mbed_official | 49:77c8e4604045 | 153 | rt_switch_req (next_TCB); |
mbed_official | 49:77c8e4604045 | 154 | } |
mbed_official | 49:77c8e4604045 | 155 | } |
mbed_official | 49:77c8e4604045 | 156 | |
mbed_official | 49:77c8e4604045 | 157 | |
mbed_official | 49:77c8e4604045 | 158 | /*--------------------------- rt_tsk_pass -----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 159 | |
mbed_official | 49:77c8e4604045 | 160 | void rt_tsk_pass (void) { |
mbed_official | 49:77c8e4604045 | 161 | /* Allow tasks of same priority level to run cooperatively.*/ |
mbed_official | 49:77c8e4604045 | 162 | P_TCB p_new; |
mbed_official | 49:77c8e4604045 | 163 | |
mbed_official | 49:77c8e4604045 | 164 | p_new = rt_get_same_rdy_prio(); |
mbed_official | 49:77c8e4604045 | 165 | if (p_new != NULL) { |
mbed_official | 49:77c8e4604045 | 166 | rt_put_prio ((P_XCB)&os_rdy, os_tsk.run); |
mbed_official | 49:77c8e4604045 | 167 | os_tsk.run->state = READY; |
mbed_official | 49:77c8e4604045 | 168 | rt_switch_req (p_new); |
mbed_official | 49:77c8e4604045 | 169 | } |
mbed_official | 49:77c8e4604045 | 170 | } |
mbed_official | 49:77c8e4604045 | 171 | |
mbed_official | 49:77c8e4604045 | 172 | |
mbed_official | 49:77c8e4604045 | 173 | /*--------------------------- rt_tsk_self -----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 174 | |
mbed_official | 49:77c8e4604045 | 175 | OS_TID rt_tsk_self (void) { |
mbed_official | 49:77c8e4604045 | 176 | /* Return own task identifier value. */ |
mbed_official | 49:77c8e4604045 | 177 | if (os_tsk.run == NULL) { |
mbed_official | 112:53ace74b190c | 178 | return (0U); |
mbed_official | 49:77c8e4604045 | 179 | } |
mbed_official | 112:53ace74b190c | 180 | return ((OS_TID)os_tsk.run->task_id); |
mbed_official | 49:77c8e4604045 | 181 | } |
mbed_official | 49:77c8e4604045 | 182 | |
mbed_official | 49:77c8e4604045 | 183 | |
mbed_official | 49:77c8e4604045 | 184 | /*--------------------------- rt_tsk_prio -----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 185 | |
mbed_official | 49:77c8e4604045 | 186 | OS_RESULT rt_tsk_prio (OS_TID task_id, U8 new_prio) { |
mbed_official | 49:77c8e4604045 | 187 | /* Change execution priority of a task to "new_prio". */ |
mbed_official | 49:77c8e4604045 | 188 | P_TCB p_task; |
mbed_official | 49:77c8e4604045 | 189 | |
mbed_official | 112:53ace74b190c | 190 | if (task_id == 0U) { |
mbed_official | 49:77c8e4604045 | 191 | /* Change execution priority of calling task. */ |
mbed_official | 112:53ace74b190c | 192 | os_tsk.run->prio = new_prio; |
mbed_official | 112:53ace74b190c | 193 | os_tsk.run->prio_base = new_prio; |
mbed_official | 49:77c8e4604045 | 194 | run:if (rt_rdy_prio() > new_prio) { |
mbed_official | 49:77c8e4604045 | 195 | rt_put_prio (&os_rdy, os_tsk.run); |
mbed_official | 49:77c8e4604045 | 196 | os_tsk.run->state = READY; |
mbed_official | 49:77c8e4604045 | 197 | rt_dispatch (NULL); |
mbed_official | 49:77c8e4604045 | 198 | } |
mbed_official | 49:77c8e4604045 | 199 | return (OS_R_OK); |
mbed_official | 49:77c8e4604045 | 200 | } |
mbed_official | 49:77c8e4604045 | 201 | |
mbed_official | 49:77c8e4604045 | 202 | /* Find the task in the "os_active_TCB" array. */ |
mbed_official | 112:53ace74b190c | 203 | if ((task_id > os_maxtaskrun) || (os_active_TCB[task_id-1U] == NULL)) { |
mbed_official | 49:77c8e4604045 | 204 | /* Task with "task_id" not found or not started. */ |
mbed_official | 49:77c8e4604045 | 205 | return (OS_R_NOK); |
mbed_official | 49:77c8e4604045 | 206 | } |
mbed_official | 112:53ace74b190c | 207 | p_task = os_active_TCB[task_id-1U]; |
mbed_official | 112:53ace74b190c | 208 | p_task->prio = new_prio; |
mbed_official | 112:53ace74b190c | 209 | p_task->prio_base = new_prio; |
mbed_official | 49:77c8e4604045 | 210 | if (p_task == os_tsk.run) { |
mbed_official | 49:77c8e4604045 | 211 | goto run; |
mbed_official | 49:77c8e4604045 | 212 | } |
mbed_official | 49:77c8e4604045 | 213 | rt_resort_prio (p_task); |
mbed_official | 49:77c8e4604045 | 214 | if (p_task->state == READY) { |
mbed_official | 49:77c8e4604045 | 215 | /* Task enqueued in a ready list. */ |
mbed_official | 49:77c8e4604045 | 216 | p_task = rt_get_first (&os_rdy); |
mbed_official | 49:77c8e4604045 | 217 | rt_dispatch (p_task); |
mbed_official | 49:77c8e4604045 | 218 | } |
mbed_official | 49:77c8e4604045 | 219 | return (OS_R_OK); |
mbed_official | 49:77c8e4604045 | 220 | } |
mbed_official | 49:77c8e4604045 | 221 | |
mbed_official | 112:53ace74b190c | 222 | |
mbed_official | 112:53ace74b190c | 223 | /*--------------------------- rt_tsk_create ---------------------------------*/ |
mbed_official | 112:53ace74b190c | 224 | |
mbed_official | 112:53ace74b190c | 225 | OS_TID rt_tsk_create (FUNCP task, U32 prio_stksz, void *stk, void *argv) { |
mbed_official | 112:53ace74b190c | 226 | /* Start a new task declared with "task". */ |
mbed_official | 112:53ace74b190c | 227 | P_TCB task_context; |
mbed_official | 112:53ace74b190c | 228 | U32 i; |
mbed_official | 112:53ace74b190c | 229 | |
mbed_official | 112:53ace74b190c | 230 | /* Priority 0 is reserved for idle task! */ |
mbed_official | 112:53ace74b190c | 231 | if ((prio_stksz & 0xFFU) == 0U) { |
mbed_official | 112:53ace74b190c | 232 | prio_stksz += 1U; |
mbed_official | 112:53ace74b190c | 233 | } |
mbed_official | 112:53ace74b190c | 234 | task_context = rt_alloc_box (mp_tcb); |
mbed_official | 112:53ace74b190c | 235 | if (task_context == NULL) { |
mbed_official | 112:53ace74b190c | 236 | return (0U); |
mbed_official | 112:53ace74b190c | 237 | } |
mbed_official | 112:53ace74b190c | 238 | /* If "size != 0" use a private user provided stack. */ |
mbed_official | 112:53ace74b190c | 239 | task_context->stack = stk; |
mbed_official | 112:53ace74b190c | 240 | task_context->priv_stack = prio_stksz >> 8; |
mbed_official | 112:53ace74b190c | 241 | |
mbed_official | 112:53ace74b190c | 242 | /* Find a free entry in 'os_active_TCB' table. */ |
mbed_official | 112:53ace74b190c | 243 | i = rt_get_TID (); |
mbed_official | 112:53ace74b190c | 244 | if (i == 0U) { |
mbed_official | 112:53ace74b190c | 245 | return (0U); |
mbed_official | 112:53ace74b190c | 246 | } |
mbed_official | 112:53ace74b190c | 247 | task_context->task_id = (U8)i; |
mbed_official | 112:53ace74b190c | 248 | /* Pass parameter 'argv' to 'rt_init_context' */ |
mbed_official | 112:53ace74b190c | 249 | task_context->msg = argv; |
mbed_official | 112:53ace74b190c | 250 | /* For 'size == 0' system allocates the user stack from the memory pool. */ |
mbed_official | 112:53ace74b190c | 251 | rt_init_context (task_context, (U8)(prio_stksz & 0xFFU), task); |
mbed_official | 112:53ace74b190c | 252 | |
mbed_official | 112:53ace74b190c | 253 | os_active_TCB[i-1U] = task_context; |
mbed_official | 112:53ace74b190c | 254 | DBG_TASK_NOTIFY(task_context, __TRUE); |
mbed_official | 112:53ace74b190c | 255 | rt_dispatch (task_context); |
mbed_official | 112:53ace74b190c | 256 | return ((OS_TID)i); |
mbed_official | 112:53ace74b190c | 257 | } |
mbed_official | 112:53ace74b190c | 258 | |
mbed_official | 112:53ace74b190c | 259 | |
mbed_official | 49:77c8e4604045 | 260 | /*--------------------------- rt_tsk_delete ---------------------------------*/ |
mbed_official | 49:77c8e4604045 | 261 | |
mbed_official | 49:77c8e4604045 | 262 | OS_RESULT rt_tsk_delete (OS_TID task_id) { |
mbed_official | 49:77c8e4604045 | 263 | /* Terminate the task identified with "task_id". */ |
mbed_official | 112:53ace74b190c | 264 | P_TCB task_context; |
mbed_official | 112:53ace74b190c | 265 | P_TCB p_TCB; |
mbed_official | 112:53ace74b190c | 266 | P_MUCB p_MCB, p_MCB0; |
mbed_official | 49:77c8e4604045 | 267 | |
mbed_official | 112:53ace74b190c | 268 | if ((task_id == 0U) || (task_id == os_tsk.run->task_id)) { |
mbed_official | 49:77c8e4604045 | 269 | /* Terminate itself. */ |
mbed_official | 49:77c8e4604045 | 270 | os_tsk.run->state = INACTIVE; |
mbed_official | 49:77c8e4604045 | 271 | os_tsk.run->tsk_stack = rt_get_PSP (); |
mbed_official | 49:77c8e4604045 | 272 | rt_stk_check (); |
mbed_official | 112:53ace74b190c | 273 | p_MCB = os_tsk.run->p_mlnk; |
mbed_official | 112:53ace74b190c | 274 | while (p_MCB) { |
mbed_official | 112:53ace74b190c | 275 | /* Release mutexes owned by this task */ |
mbed_official | 112:53ace74b190c | 276 | if (p_MCB->p_lnk) { |
mbed_official | 112:53ace74b190c | 277 | /* A task is waiting for mutex. */ |
mbed_official | 112:53ace74b190c | 278 | p_TCB = rt_get_first ((P_XCB)p_MCB); |
mbed_official | 112:53ace74b190c | 279 | #ifdef __CMSIS_RTOS |
mbed_official | 112:53ace74b190c | 280 | rt_ret_val (p_TCB, 0U/*osOK*/); |
mbed_official | 112:53ace74b190c | 281 | #else |
mbed_official | 112:53ace74b190c | 282 | rt_ret_val (p_TCB, OS_R_MUT); |
mbed_official | 112:53ace74b190c | 283 | #endif |
mbed_official | 112:53ace74b190c | 284 | rt_rmv_dly (p_TCB); |
mbed_official | 112:53ace74b190c | 285 | p_TCB->state = READY; |
mbed_official | 112:53ace74b190c | 286 | rt_put_prio (&os_rdy, p_TCB); |
mbed_official | 112:53ace74b190c | 287 | /* A waiting task becomes the owner of this mutex. */ |
mbed_official | 112:53ace74b190c | 288 | p_MCB0 = p_MCB->p_mlnk; |
mbed_official | 112:53ace74b190c | 289 | p_MCB->level = 1U; |
mbed_official | 112:53ace74b190c | 290 | p_MCB->owner = p_TCB; |
mbed_official | 112:53ace74b190c | 291 | p_MCB->p_mlnk = p_TCB->p_mlnk; |
mbed_official | 112:53ace74b190c | 292 | p_TCB->p_mlnk = p_MCB; |
mbed_official | 112:53ace74b190c | 293 | p_MCB = p_MCB0; |
mbed_official | 112:53ace74b190c | 294 | } |
mbed_official | 112:53ace74b190c | 295 | else { |
mbed_official | 112:53ace74b190c | 296 | p_MCB0 = p_MCB->p_mlnk; |
mbed_official | 112:53ace74b190c | 297 | p_MCB->level = 0U; |
mbed_official | 112:53ace74b190c | 298 | p_MCB->owner = NULL; |
mbed_official | 112:53ace74b190c | 299 | p_MCB->p_mlnk = NULL; |
mbed_official | 112:53ace74b190c | 300 | p_MCB = p_MCB0; |
mbed_official | 112:53ace74b190c | 301 | } |
mbed_official | 112:53ace74b190c | 302 | } |
mbed_official | 112:53ace74b190c | 303 | os_active_TCB[os_tsk.run->task_id-1U] = NULL; |
mbed_official | 112:53ace74b190c | 304 | rt_free_box (mp_stk, os_tsk.run->stack); |
mbed_official | 49:77c8e4604045 | 305 | os_tsk.run->stack = NULL; |
mbed_official | 49:77c8e4604045 | 306 | DBG_TASK_NOTIFY(os_tsk.run, __FALSE); |
mbed_official | 112:53ace74b190c | 307 | rt_free_box (mp_tcb, os_tsk.run); |
mbed_official | 49:77c8e4604045 | 308 | os_tsk.run = NULL; |
mbed_official | 49:77c8e4604045 | 309 | rt_dispatch (NULL); |
mbed_official | 49:77c8e4604045 | 310 | /* The program should never come to this point. */ |
mbed_official | 49:77c8e4604045 | 311 | } |
mbed_official | 49:77c8e4604045 | 312 | else { |
mbed_official | 49:77c8e4604045 | 313 | /* Find the task in the "os_active_TCB" array. */ |
mbed_official | 112:53ace74b190c | 314 | if ((task_id > os_maxtaskrun) || (os_active_TCB[task_id-1U] == NULL)) { |
mbed_official | 49:77c8e4604045 | 315 | /* Task with "task_id" not found or not started. */ |
mbed_official | 49:77c8e4604045 | 316 | return (OS_R_NOK); |
mbed_official | 49:77c8e4604045 | 317 | } |
mbed_official | 112:53ace74b190c | 318 | task_context = os_active_TCB[task_id-1U]; |
mbed_official | 49:77c8e4604045 | 319 | rt_rmv_list (task_context); |
mbed_official | 49:77c8e4604045 | 320 | rt_rmv_dly (task_context); |
mbed_official | 112:53ace74b190c | 321 | p_MCB = task_context->p_mlnk; |
mbed_official | 112:53ace74b190c | 322 | while (p_MCB) { |
mbed_official | 112:53ace74b190c | 323 | /* Release mutexes owned by this task */ |
mbed_official | 112:53ace74b190c | 324 | if (p_MCB->p_lnk) { |
mbed_official | 112:53ace74b190c | 325 | /* A task is waiting for mutex. */ |
mbed_official | 112:53ace74b190c | 326 | p_TCB = rt_get_first ((P_XCB)p_MCB); |
mbed_official | 112:53ace74b190c | 327 | #ifdef __CMSIS_RTOS |
mbed_official | 112:53ace74b190c | 328 | rt_ret_val (p_TCB, 0U/*osOK*/); |
mbed_official | 112:53ace74b190c | 329 | #else |
mbed_official | 112:53ace74b190c | 330 | rt_ret_val (p_TCB, OS_R_MUT); |
mbed_official | 112:53ace74b190c | 331 | #endif |
mbed_official | 112:53ace74b190c | 332 | rt_rmv_dly (p_TCB); |
mbed_official | 112:53ace74b190c | 333 | p_TCB->state = READY; |
mbed_official | 112:53ace74b190c | 334 | rt_put_prio (&os_rdy, p_TCB); |
mbed_official | 112:53ace74b190c | 335 | /* A waiting task becomes the owner of this mutex. */ |
mbed_official | 112:53ace74b190c | 336 | p_MCB0 = p_MCB->p_mlnk; |
mbed_official | 112:53ace74b190c | 337 | p_MCB->level = 1U; |
mbed_official | 112:53ace74b190c | 338 | p_MCB->owner = p_TCB; |
mbed_official | 112:53ace74b190c | 339 | p_MCB->p_mlnk = p_TCB->p_mlnk; |
mbed_official | 112:53ace74b190c | 340 | p_TCB->p_mlnk = p_MCB; |
mbed_official | 112:53ace74b190c | 341 | p_MCB = p_MCB0; |
mbed_official | 112:53ace74b190c | 342 | } |
mbed_official | 112:53ace74b190c | 343 | else { |
mbed_official | 112:53ace74b190c | 344 | p_MCB0 = p_MCB->p_mlnk; |
mbed_official | 112:53ace74b190c | 345 | p_MCB->level = 0U; |
mbed_official | 112:53ace74b190c | 346 | p_MCB->owner = NULL; |
mbed_official | 112:53ace74b190c | 347 | p_MCB->p_mlnk = NULL; |
mbed_official | 112:53ace74b190c | 348 | p_MCB = p_MCB0; |
mbed_official | 112:53ace74b190c | 349 | } |
mbed_official | 112:53ace74b190c | 350 | } |
mbed_official | 112:53ace74b190c | 351 | os_active_TCB[task_id-1U] = NULL; |
mbed_official | 112:53ace74b190c | 352 | rt_free_box (mp_stk, task_context->stack); |
mbed_official | 49:77c8e4604045 | 353 | task_context->stack = NULL; |
mbed_official | 49:77c8e4604045 | 354 | DBG_TASK_NOTIFY(task_context, __FALSE); |
mbed_official | 112:53ace74b190c | 355 | rt_free_box (mp_tcb, task_context); |
mbed_official | 112:53ace74b190c | 356 | if (rt_rdy_prio() > os_tsk.run->prio) { |
mbed_official | 112:53ace74b190c | 357 | /* Ready task has higher priority than running task. */ |
mbed_official | 112:53ace74b190c | 358 | os_tsk.run->state = READY; |
mbed_official | 112:53ace74b190c | 359 | rt_put_prio (&os_rdy, os_tsk.run); |
mbed_official | 112:53ace74b190c | 360 | rt_dispatch (NULL); |
mbed_official | 112:53ace74b190c | 361 | } |
mbed_official | 49:77c8e4604045 | 362 | } |
mbed_official | 49:77c8e4604045 | 363 | return (OS_R_OK); |
mbed_official | 49:77c8e4604045 | 364 | } |
mbed_official | 49:77c8e4604045 | 365 | |
mbed_official | 49:77c8e4604045 | 366 | |
mbed_official | 49:77c8e4604045 | 367 | /*--------------------------- rt_sys_init -----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 368 | |
mbed_official | 49:77c8e4604045 | 369 | #ifdef __CMSIS_RTOS |
mbed_official | 49:77c8e4604045 | 370 | void rt_sys_init (void) { |
mbed_official | 49:77c8e4604045 | 371 | #else |
mbed_official | 49:77c8e4604045 | 372 | void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) { |
mbed_official | 49:77c8e4604045 | 373 | #endif |
mbed_official | 49:77c8e4604045 | 374 | /* Initialize system and start up task declared with "first_task". */ |
mbed_official | 49:77c8e4604045 | 375 | U32 i; |
mbed_official | 49:77c8e4604045 | 376 | |
mbed_official | 49:77c8e4604045 | 377 | DBG_INIT(); |
mbed_official | 49:77c8e4604045 | 378 | |
mbed_official | 49:77c8e4604045 | 379 | /* Initialize dynamic memory and task TCB pointers to NULL. */ |
mbed_official | 112:53ace74b190c | 380 | for (i = 0U; i < os_maxtaskrun; i++) { |
mbed_official | 49:77c8e4604045 | 381 | os_active_TCB[i] = NULL; |
mbed_official | 49:77c8e4604045 | 382 | } |
mbed_official | 112:53ace74b190c | 383 | rt_init_box (mp_tcb, (U32)mp_tcb_size, sizeof(struct OS_TCB)); |
mbed_official | 112:53ace74b190c | 384 | rt_init_box (mp_stk, mp_stk_size, BOX_ALIGN_8 | (U16)(os_stackinfo)); |
mbed_official | 112:53ace74b190c | 385 | rt_init_box ((U32 *)m_tmr, (U32)mp_tmr_size, sizeof(struct OS_TMR)); |
mbed_official | 49:77c8e4604045 | 386 | |
mbed_official | 49:77c8e4604045 | 387 | /* Set up TCB of idle demon */ |
mbed_official | 112:53ace74b190c | 388 | os_idle_TCB.task_id = 255U; |
mbed_official | 112:53ace74b190c | 389 | os_idle_TCB.priv_stack = 0U; |
mbed_official | 112:53ace74b190c | 390 | rt_init_context (&os_idle_TCB, 0U, os_idle_demon); |
mbed_official | 49:77c8e4604045 | 391 | |
mbed_official | 49:77c8e4604045 | 392 | /* Set up ready list: initially empty */ |
mbed_official | 49:77c8e4604045 | 393 | os_rdy.cb_type = HCB; |
mbed_official | 49:77c8e4604045 | 394 | os_rdy.p_lnk = NULL; |
mbed_official | 49:77c8e4604045 | 395 | /* Set up delay list: initially empty */ |
mbed_official | 49:77c8e4604045 | 396 | os_dly.cb_type = HCB; |
mbed_official | 49:77c8e4604045 | 397 | os_dly.p_dlnk = NULL; |
mbed_official | 49:77c8e4604045 | 398 | os_dly.p_blnk = NULL; |
mbed_official | 112:53ace74b190c | 399 | os_dly.delta_time = 0U; |
mbed_official | 49:77c8e4604045 | 400 | |
mbed_official | 112:53ace74b190c | 401 | /* Fix SP and system variables to assume idle task is running */ |
mbed_official | 49:77c8e4604045 | 402 | /* Transform main program into idle task by assuming idle TCB */ |
mbed_official | 49:77c8e4604045 | 403 | #ifndef __CMSIS_RTOS |
mbed_official | 112:53ace74b190c | 404 | rt_set_PSP (os_idle_TCB.tsk_stack+32U); |
mbed_official | 49:77c8e4604045 | 405 | #endif |
mbed_official | 49:77c8e4604045 | 406 | os_tsk.run = &os_idle_TCB; |
mbed_official | 49:77c8e4604045 | 407 | os_tsk.run->state = RUNNING; |
mbed_official | 49:77c8e4604045 | 408 | |
Kojto | 118:6635230e06ba | 409 | /* Set the current thread to idle, so that on exit from this SVCall we do not |
Kojto | 118:6635230e06ba | 410 | * de-reference a NULL TCB. */ |
Kojto | 118:6635230e06ba | 411 | rt_switch_req(&os_idle_TCB); |
Kojto | 118:6635230e06ba | 412 | |
mbed_official | 49:77c8e4604045 | 413 | /* Initialize ps queue */ |
mbed_official | 112:53ace74b190c | 414 | os_psq->first = 0U; |
mbed_official | 112:53ace74b190c | 415 | os_psq->last = 0U; |
mbed_official | 49:77c8e4604045 | 416 | os_psq->size = os_fifo_size; |
mbed_official | 49:77c8e4604045 | 417 | |
mbed_official | 49:77c8e4604045 | 418 | rt_init_robin (); |
mbed_official | 49:77c8e4604045 | 419 | |
mbed_official | 112:53ace74b190c | 420 | #ifndef __CMSIS_RTOS |
mbed_official | 112:53ace74b190c | 421 | /* Initialize SVC and PendSV */ |
mbed_official | 49:77c8e4604045 | 422 | rt_svc_init (); |
mbed_official | 49:77c8e4604045 | 423 | |
mbed_official | 112:53ace74b190c | 424 | /* Initialize and start system clock timer */ |
mbed_official | 49:77c8e4604045 | 425 | os_tick_irqn = os_tick_init (); |
mbed_official | 49:77c8e4604045 | 426 | if (os_tick_irqn >= 0) { |
mbed_official | 112:53ace74b190c | 427 | OS_X_INIT((U32)os_tick_irqn); |
mbed_official | 49:77c8e4604045 | 428 | } |
mbed_official | 49:77c8e4604045 | 429 | |
mbed_official | 49:77c8e4604045 | 430 | /* Start up first user task before entering the endless loop */ |
mbed_official | 49:77c8e4604045 | 431 | rt_tsk_create (first_task, prio_stksz, stk, NULL); |
mbed_official | 49:77c8e4604045 | 432 | #endif |
mbed_official | 49:77c8e4604045 | 433 | } |
mbed_official | 49:77c8e4604045 | 434 | |
mbed_official | 49:77c8e4604045 | 435 | |
mbed_official | 49:77c8e4604045 | 436 | /*--------------------------- rt_sys_start ----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 437 | |
mbed_official | 49:77c8e4604045 | 438 | #ifdef __CMSIS_RTOS |
mbed_official | 49:77c8e4604045 | 439 | void rt_sys_start (void) { |
mbed_official | 49:77c8e4604045 | 440 | /* Start system */ |
mbed_official | 49:77c8e4604045 | 441 | |
mbed_official | 112:53ace74b190c | 442 | /* Initialize SVC and PendSV */ |
mbed_official | 112:53ace74b190c | 443 | rt_svc_init (); |
mbed_official | 112:53ace74b190c | 444 | |
mbed_official | 112:53ace74b190c | 445 | /* Initialize and start system clock timer */ |
mbed_official | 49:77c8e4604045 | 446 | os_tick_irqn = os_tick_init (); |
mbed_official | 49:77c8e4604045 | 447 | if (os_tick_irqn >= 0) { |
mbed_official | 112:53ace74b190c | 448 | OS_X_INIT((U32)os_tick_irqn); |
mbed_official | 49:77c8e4604045 | 449 | } |
mbed_official | 49:77c8e4604045 | 450 | } |
mbed_official | 49:77c8e4604045 | 451 | #endif |
mbed_official | 49:77c8e4604045 | 452 | |
mbed_official | 49:77c8e4604045 | 453 | /*---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 454 | * end of file |
mbed_official | 49:77c8e4604045 | 455 | *---------------------------------------------------------------------------*/ |