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