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