]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_A/rt_System.c
1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: System Task Manager
7 *----------------------------------------------------------------------------
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.
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 *---------------------------------------------------------------------------*/
35 #include "rt_TypeDef.h"
36 #include "RTX_Config.h"
38 #include "rt_System.h"
41 #include "rt_Mailbox.h"
42 #include "rt_Semaphore.h"
47 #include "rt_HAL_CA.h"
49 #include "rt_HAL_CM.h"
52 /*----------------------------------------------------------------------------
54 *---------------------------------------------------------------------------*/
58 /*----------------------------------------------------------------------------
60 *---------------------------------------------------------------------------*/
62 static volatile BIT os_lock
;
63 static volatile BIT os_psh_flag
;
67 /*----------------------------------------------------------------------------
69 *---------------------------------------------------------------------------*/
71 #if defined (__CC_ARM)
72 __asm
void $$RTX$$
version (void) {
73 /* Export a version number symbol for a version control. */
77 __RL_RTX_VER EQU
0x450
82 /*--------------------------- rt_suspend ------------------------------------*/
84 U32
rt_suspend (void) {
85 /* Suspend OS scheduler */
91 delta
= os_dly
.delta_time
;
95 if (os_tmr
.tcnt
< delta
) delta
= os_tmr
.tcnt
;
103 /*--------------------------- rt_resume -------------------------------------*/
105 void rt_resume (U32 sleep_time
) {
106 /* Resume OS scheduler after suspend */
110 os_tsk
.run
->state
= READY
;
111 rt_put_rdy_first (os_tsk
.run
);
113 os_robin
.task
= NULL
;
118 if (delta
>= os_dly
.delta_time
) {
119 delta
-= os_dly
.delta_time
;
120 os_time
+= os_dly
.delta_time
;
121 os_dly
.delta_time
= 1;
122 while (os_dly
.p_dlnk
) {
124 if (delta
== 0) break;
130 os_dly
.delta_time
-= delta
;
133 os_time
+= sleep_time
;
137 /* Check the user timers. */
140 if (delta
>= os_tmr
.tcnt
) {
141 delta
-= os_tmr
.tcnt
;
143 while (os_tmr
.next
) {
145 if (delta
== 0) break;
149 os_tmr
.tcnt
-= delta
;
154 /* Switch back to highest ready task */
155 next
= rt_get_first (&os_rdy
);
156 rt_switch_req (next
);
162 /*--------------------------- rt_tsk_lock -----------------------------------*/
164 void rt_tsk_lock (void) {
165 /* Prevent task switching by locking out scheduler */
166 if (os_tick_irqn
< 0) {
169 OS_UNPEND (&pend_flags
);
171 OS_X_LOCK(os_tick_irqn
);
173 OS_X_UNPEND (&pend_flags
);
178 /*--------------------------- rt_tsk_unlock ---------------------------------*/
180 void rt_tsk_unlock (void) {
181 /* Unlock scheduler and re-enable task switching */
182 if (os_tick_irqn
< 0) {
185 OS_PEND (pend_flags
, os_psh_flag
);
186 os_psh_flag
= __FALSE
;
188 OS_X_UNLOCK(os_tick_irqn
);
190 OS_X_PEND (pend_flags
, os_psh_flag
);
191 os_psh_flag
= __FALSE
;
196 /*--------------------------- rt_psh_req ------------------------------------*/
198 void rt_psh_req (void) {
199 /* Initiate a post service handling request if required. */
200 if (os_lock
== __FALSE
) {
204 os_psh_flag
= __TRUE
;
209 /*--------------------------- rt_pop_req ------------------------------------*/
211 void rt_pop_req (void) {
212 /* Process an ISR post service requests. */
217 os_tsk
.run
->state
= READY
;
218 rt_put_rdy_first (os_tsk
.run
);
221 while (os_psq
->count
) {
222 p_CB
= os_psq
->q
[idx
].id
;
223 if (p_CB
->cb_type
== TCB
) {
225 rt_evt_psh ((P_TCB
)p_CB
, (U16
)os_psq
->q
[idx
].arg
);
227 else if (p_CB
->cb_type
== MCB
) {
229 rt_mbx_psh ((P_MCB
)p_CB
, (void *)os_psq
->q
[idx
].arg
);
232 /* Must be of SCB type */
233 rt_sem_psh ((P_SCB
)p_CB
);
235 if (++idx
== os_psq
->size
) idx
= 0;
236 rt_dec (&os_psq
->count
);
240 next
= rt_get_first (&os_rdy
);
241 rt_switch_req (next
);
245 /*--------------------------- os_tick_init ----------------------------------*/
247 __weak
int os_tick_init (void) {
248 /* Initialize SysTick timer as system tick timer. */
250 return (-1); /* Return IRQ number of SysTick timer */
254 /*--------------------------- os_tick_irqack --------------------------------*/
256 __weak
void os_tick_irqack (void) {
257 /* Acknowledge timer interrupt. */
261 /*--------------------------- rt_systick ------------------------------------*/
263 extern void sysTimerTick(void);
265 void rt_systick (void) {
266 /* Check for system clock update, suspend running task. */
269 os_tsk
.run
->state
= READY
;
270 rt_put_rdy_first (os_tsk
.run
);
272 /* Check Round Robin timeout. */
279 /* Check the user timers. */
286 /* Switch back to highest ready task */
287 next
= rt_get_first (&os_rdy
);
288 rt_switch_req (next
);
291 /*--------------------------- rt_stk_check ----------------------------------*/
293 __weak
void rt_stk_check (void) {
294 /* Check for stack overflow. */
295 if ((os_tsk
.run
->tsk_stack
< (U32
)os_tsk
.run
->stack
) ||
296 (os_tsk
.run
->stack
[0] != MAGIC_WORD
)) {
297 os_error (OS_ERR_STK_OVF
);
301 /*----------------------------------------------------------------------------
303 *---------------------------------------------------------------------------*/