Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   denki-yohou_b TestY201 Network-RTOS NTPClient_HelloWorld ... more

Deprecated

This is the mbed 2 rtos library. mbed OS 5 integrates the mbed library with mbed-rtos. With this, we have provided thread safety for all mbed APIs. If you'd like to learn about using mbed OS 5, please see the docs.

Committer:
mbed_official
Date:
Thu Nov 06 13:00:11 2014 +0000
Revision:
49:77c8e4604045
Child:
112:53ace74b190c
Synchronized with git revision 7b90c2ba137baaf9769219e0e8a7b8e8d1299c4f

Full URL: https://github.com/mbedmicro/mbed/commit/7b90c2ba137baaf9769219e0e8a7b8e8d1299c4f/

This target is not yet tested, so it can't be released as part of the official
SDK build for now.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 49:77c8e4604045 1 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 2 * RL-ARM - RTX
mbed_official 49:77c8e4604045 3 *----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 4 * Name: RT_SYSTEM.C
mbed_official 49:77c8e4604045 5 * Purpose: System Task Manager
mbed_official 49:77c8e4604045 6 * Rev.: V4.60
mbed_official 49:77c8e4604045 7 *----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 8 *
mbed_official 49:77c8e4604045 9 * Copyright (c) 1999-2009 KEIL, 2009-2012 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 49:77c8e4604045 36 #include "RTX_Conf.h"
mbed_official 49:77c8e4604045 37 #include "rt_Task.h"
mbed_official 49:77c8e4604045 38 #include "rt_System.h"
mbed_official 49:77c8e4604045 39 #include "rt_Event.h"
mbed_official 49:77c8e4604045 40 #include "rt_List.h"
mbed_official 49:77c8e4604045 41 #include "rt_Mailbox.h"
mbed_official 49:77c8e4604045 42 #include "rt_Semaphore.h"
mbed_official 49:77c8e4604045 43 #include "rt_Time.h"
mbed_official 49:77c8e4604045 44 #include "rt_Robin.h"
mbed_official 49:77c8e4604045 45 #include "rt_HAL_CM.h"
mbed_official 49:77c8e4604045 46
mbed_official 49:77c8e4604045 47 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 48 * Global Variables
mbed_official 49:77c8e4604045 49 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 50
mbed_official 49:77c8e4604045 51 int os_tick_irqn;
mbed_official 49:77c8e4604045 52
mbed_official 49:77c8e4604045 53 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 54 * Local Variables
mbed_official 49:77c8e4604045 55 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 56
mbed_official 49:77c8e4604045 57 static volatile BIT os_lock;
mbed_official 49:77c8e4604045 58 static volatile BIT os_psh_flag;
mbed_official 49:77c8e4604045 59 static U8 pend_flags;
mbed_official 49:77c8e4604045 60
mbed_official 49:77c8e4604045 61 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 62 * Global Functions
mbed_official 49:77c8e4604045 63 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 64
mbed_official 49:77c8e4604045 65 #if defined (__CC_ARM)
mbed_official 49:77c8e4604045 66 __asm void $$RTX$$version (void) {
mbed_official 49:77c8e4604045 67 /* Export a version number symbol for a version control. */
mbed_official 49:77c8e4604045 68
mbed_official 49:77c8e4604045 69 EXPORT __RL_RTX_VER
mbed_official 49:77c8e4604045 70
mbed_official 49:77c8e4604045 71 __RL_RTX_VER EQU 0x450
mbed_official 49:77c8e4604045 72 }
mbed_official 49:77c8e4604045 73 #endif
mbed_official 49:77c8e4604045 74
mbed_official 49:77c8e4604045 75
mbed_official 49:77c8e4604045 76 /*--------------------------- rt_suspend ------------------------------------*/
mbed_official 49:77c8e4604045 77 U32 rt_suspend (void) {
mbed_official 49:77c8e4604045 78 /* Suspend OS scheduler */
mbed_official 49:77c8e4604045 79 U32 delta = 0xFFFF;
mbed_official 49:77c8e4604045 80
mbed_official 49:77c8e4604045 81 rt_tsk_lock();
mbed_official 49:77c8e4604045 82
mbed_official 49:77c8e4604045 83 if (os_dly.p_dlnk) {
mbed_official 49:77c8e4604045 84 delta = os_dly.delta_time;
mbed_official 49:77c8e4604045 85 }
mbed_official 49:77c8e4604045 86 #ifndef __CMSIS_RTOS
mbed_official 49:77c8e4604045 87 if (os_tmr.next) {
mbed_official 49:77c8e4604045 88 if (os_tmr.tcnt < delta) delta = os_tmr.tcnt;
mbed_official 49:77c8e4604045 89 }
mbed_official 49:77c8e4604045 90 #endif
mbed_official 49:77c8e4604045 91
mbed_official 49:77c8e4604045 92 return (delta);
mbed_official 49:77c8e4604045 93 }
mbed_official 49:77c8e4604045 94
mbed_official 49:77c8e4604045 95
mbed_official 49:77c8e4604045 96 /*--------------------------- rt_resume -------------------------------------*/
mbed_official 49:77c8e4604045 97 void rt_resume (U32 sleep_time) {
mbed_official 49:77c8e4604045 98 /* Resume OS scheduler after suspend */
mbed_official 49:77c8e4604045 99 P_TCB next;
mbed_official 49:77c8e4604045 100 U32 delta;
mbed_official 49:77c8e4604045 101
mbed_official 49:77c8e4604045 102 os_tsk.run->state = READY;
mbed_official 49:77c8e4604045 103 rt_put_rdy_first (os_tsk.run);
mbed_official 49:77c8e4604045 104
mbed_official 49:77c8e4604045 105 os_robin.task = NULL;
mbed_official 49:77c8e4604045 106
mbed_official 49:77c8e4604045 107 /* Update delays. */
mbed_official 49:77c8e4604045 108 if (os_dly.p_dlnk) {
mbed_official 49:77c8e4604045 109 delta = sleep_time;
mbed_official 49:77c8e4604045 110 if (delta >= os_dly.delta_time) {
mbed_official 49:77c8e4604045 111 delta -= os_dly.delta_time;
mbed_official 49:77c8e4604045 112 os_time += os_dly.delta_time;
mbed_official 49:77c8e4604045 113 os_dly.delta_time = 1;
mbed_official 49:77c8e4604045 114 while (os_dly.p_dlnk) {
mbed_official 49:77c8e4604045 115 rt_dec_dly();
mbed_official 49:77c8e4604045 116 if (delta == 0) break;
mbed_official 49:77c8e4604045 117 delta--;
mbed_official 49:77c8e4604045 118 os_time++;
mbed_official 49:77c8e4604045 119 }
mbed_official 49:77c8e4604045 120 } else {
mbed_official 49:77c8e4604045 121 os_time += delta;
mbed_official 49:77c8e4604045 122 os_dly.delta_time -= delta;
mbed_official 49:77c8e4604045 123 }
mbed_official 49:77c8e4604045 124 } else {
mbed_official 49:77c8e4604045 125 os_time += sleep_time;
mbed_official 49:77c8e4604045 126 }
mbed_official 49:77c8e4604045 127
mbed_official 49:77c8e4604045 128 #ifndef __CMSIS_RTOS
mbed_official 49:77c8e4604045 129 /* Check the user timers. */
mbed_official 49:77c8e4604045 130 if (os_tmr.next) {
mbed_official 49:77c8e4604045 131 delta = sleep_time;
mbed_official 49:77c8e4604045 132 if (delta >= os_tmr.tcnt) {
mbed_official 49:77c8e4604045 133 delta -= os_tmr.tcnt;
mbed_official 49:77c8e4604045 134 os_tmr.tcnt = 1;
mbed_official 49:77c8e4604045 135 while (os_tmr.next) {
mbed_official 49:77c8e4604045 136 rt_tmr_tick();
mbed_official 49:77c8e4604045 137 if (delta == 0) break;
mbed_official 49:77c8e4604045 138 delta--;
mbed_official 49:77c8e4604045 139 }
mbed_official 49:77c8e4604045 140 } else {
mbed_official 49:77c8e4604045 141 os_tmr.tcnt -= delta;
mbed_official 49:77c8e4604045 142 }
mbed_official 49:77c8e4604045 143 }
mbed_official 49:77c8e4604045 144 #endif
mbed_official 49:77c8e4604045 145
mbed_official 49:77c8e4604045 146 /* Switch back to highest ready task */
mbed_official 49:77c8e4604045 147 next = rt_get_first (&os_rdy);
mbed_official 49:77c8e4604045 148 rt_switch_req (next);
mbed_official 49:77c8e4604045 149
mbed_official 49:77c8e4604045 150 rt_tsk_unlock();
mbed_official 49:77c8e4604045 151 }
mbed_official 49:77c8e4604045 152
mbed_official 49:77c8e4604045 153
mbed_official 49:77c8e4604045 154 /*--------------------------- rt_tsk_lock -----------------------------------*/
mbed_official 49:77c8e4604045 155
mbed_official 49:77c8e4604045 156 void rt_tsk_lock (void) {
mbed_official 49:77c8e4604045 157 /* Prevent task switching by locking out scheduler */
mbed_official 49:77c8e4604045 158 if (os_tick_irqn < 0) {
mbed_official 49:77c8e4604045 159 OS_LOCK();
mbed_official 49:77c8e4604045 160 os_lock = __TRUE;
mbed_official 49:77c8e4604045 161 OS_UNPEND (&pend_flags);
mbed_official 49:77c8e4604045 162 } else {
mbed_official 49:77c8e4604045 163 OS_X_LOCK(os_tick_irqn);
mbed_official 49:77c8e4604045 164 os_lock = __TRUE;
mbed_official 49:77c8e4604045 165 OS_X_UNPEND (&pend_flags);
mbed_official 49:77c8e4604045 166 }
mbed_official 49:77c8e4604045 167 }
mbed_official 49:77c8e4604045 168
mbed_official 49:77c8e4604045 169
mbed_official 49:77c8e4604045 170 /*--------------------------- rt_tsk_unlock ---------------------------------*/
mbed_official 49:77c8e4604045 171
mbed_official 49:77c8e4604045 172 void rt_tsk_unlock (void) {
mbed_official 49:77c8e4604045 173 /* Unlock scheduler and re-enable task switching */
mbed_official 49:77c8e4604045 174 if (os_tick_irqn < 0) {
mbed_official 49:77c8e4604045 175 OS_UNLOCK();
mbed_official 49:77c8e4604045 176 os_lock = __FALSE;
mbed_official 49:77c8e4604045 177 OS_PEND (pend_flags, os_psh_flag);
mbed_official 49:77c8e4604045 178 os_psh_flag = __FALSE;
mbed_official 49:77c8e4604045 179 } else {
mbed_official 49:77c8e4604045 180 OS_X_UNLOCK(os_tick_irqn);
mbed_official 49:77c8e4604045 181 os_lock = __FALSE;
mbed_official 49:77c8e4604045 182 OS_X_PEND (pend_flags, os_psh_flag);
mbed_official 49:77c8e4604045 183 os_psh_flag = __FALSE;
mbed_official 49:77c8e4604045 184 }
mbed_official 49:77c8e4604045 185 }
mbed_official 49:77c8e4604045 186
mbed_official 49:77c8e4604045 187
mbed_official 49:77c8e4604045 188 /*--------------------------- rt_psh_req ------------------------------------*/
mbed_official 49:77c8e4604045 189
mbed_official 49:77c8e4604045 190 void rt_psh_req (void) {
mbed_official 49:77c8e4604045 191 /* Initiate a post service handling request if required. */
mbed_official 49:77c8e4604045 192 if (os_lock == __FALSE) {
mbed_official 49:77c8e4604045 193 OS_PEND_IRQ ();
mbed_official 49:77c8e4604045 194 }
mbed_official 49:77c8e4604045 195 else {
mbed_official 49:77c8e4604045 196 os_psh_flag = __TRUE;
mbed_official 49:77c8e4604045 197 }
mbed_official 49:77c8e4604045 198 }
mbed_official 49:77c8e4604045 199
mbed_official 49:77c8e4604045 200
mbed_official 49:77c8e4604045 201 /*--------------------------- rt_pop_req ------------------------------------*/
mbed_official 49:77c8e4604045 202
mbed_official 49:77c8e4604045 203 void rt_pop_req (void) {
mbed_official 49:77c8e4604045 204 /* Process an ISR post service requests. */
mbed_official 49:77c8e4604045 205 struct OS_XCB *p_CB;
mbed_official 49:77c8e4604045 206 P_TCB next;
mbed_official 49:77c8e4604045 207 U32 idx;
mbed_official 49:77c8e4604045 208
mbed_official 49:77c8e4604045 209 os_tsk.run->state = READY;
mbed_official 49:77c8e4604045 210 rt_put_rdy_first (os_tsk.run);
mbed_official 49:77c8e4604045 211
mbed_official 49:77c8e4604045 212 idx = os_psq->last;
mbed_official 49:77c8e4604045 213 while (os_psq->count) {
mbed_official 49:77c8e4604045 214 p_CB = os_psq->q[idx].id;
mbed_official 49:77c8e4604045 215 if (p_CB->cb_type == TCB) {
mbed_official 49:77c8e4604045 216 /* Is of TCB type */
mbed_official 49:77c8e4604045 217 rt_evt_psh ((P_TCB)p_CB, (U16)os_psq->q[idx].arg);
mbed_official 49:77c8e4604045 218 }
mbed_official 49:77c8e4604045 219 else if (p_CB->cb_type == MCB) {
mbed_official 49:77c8e4604045 220 /* Is of MCB type */
mbed_official 49:77c8e4604045 221 rt_mbx_psh ((P_MCB)p_CB, (void *)os_psq->q[idx].arg);
mbed_official 49:77c8e4604045 222 }
mbed_official 49:77c8e4604045 223 else {
mbed_official 49:77c8e4604045 224 /* Must be of SCB type */
mbed_official 49:77c8e4604045 225 rt_sem_psh ((P_SCB)p_CB);
mbed_official 49:77c8e4604045 226 }
mbed_official 49:77c8e4604045 227 if (++idx == os_psq->size) idx = 0;
mbed_official 49:77c8e4604045 228 rt_dec (&os_psq->count);
mbed_official 49:77c8e4604045 229 }
mbed_official 49:77c8e4604045 230 os_psq->last = idx;
mbed_official 49:77c8e4604045 231
mbed_official 49:77c8e4604045 232 next = rt_get_first (&os_rdy);
mbed_official 49:77c8e4604045 233 rt_switch_req (next);
mbed_official 49:77c8e4604045 234 }
mbed_official 49:77c8e4604045 235
mbed_official 49:77c8e4604045 236
mbed_official 49:77c8e4604045 237 /*--------------------------- os_tick_init ----------------------------------*/
mbed_official 49:77c8e4604045 238
mbed_official 49:77c8e4604045 239 __weak int os_tick_init (void) {
mbed_official 49:77c8e4604045 240 /* Initialize SysTick timer as system tick timer. */
mbed_official 49:77c8e4604045 241 rt_systick_init ();
mbed_official 49:77c8e4604045 242 return (-1); /* Return IRQ number of SysTick timer */
mbed_official 49:77c8e4604045 243 }
mbed_official 49:77c8e4604045 244
mbed_official 49:77c8e4604045 245
mbed_official 49:77c8e4604045 246 /*--------------------------- os_tick_irqack --------------------------------*/
mbed_official 49:77c8e4604045 247
mbed_official 49:77c8e4604045 248 __weak void os_tick_irqack (void) {
mbed_official 49:77c8e4604045 249 /* Acknowledge timer interrupt. */
mbed_official 49:77c8e4604045 250 }
mbed_official 49:77c8e4604045 251
mbed_official 49:77c8e4604045 252
mbed_official 49:77c8e4604045 253 /*--------------------------- rt_systick ------------------------------------*/
mbed_official 49:77c8e4604045 254
mbed_official 49:77c8e4604045 255 extern void sysTimerTick(void);
mbed_official 49:77c8e4604045 256
mbed_official 49:77c8e4604045 257 void rt_systick (void) {
mbed_official 49:77c8e4604045 258 /* Check for system clock update, suspend running task. */
mbed_official 49:77c8e4604045 259 P_TCB next;
mbed_official 49:77c8e4604045 260
mbed_official 49:77c8e4604045 261 os_tsk.run->state = READY;
mbed_official 49:77c8e4604045 262 rt_put_rdy_first (os_tsk.run);
mbed_official 49:77c8e4604045 263
mbed_official 49:77c8e4604045 264 /* Check Round Robin timeout. */
mbed_official 49:77c8e4604045 265 rt_chk_robin ();
mbed_official 49:77c8e4604045 266
mbed_official 49:77c8e4604045 267 /* Update delays. */
mbed_official 49:77c8e4604045 268 os_time++;
mbed_official 49:77c8e4604045 269 rt_dec_dly ();
mbed_official 49:77c8e4604045 270
mbed_official 49:77c8e4604045 271 /* Check the user timers. */
mbed_official 49:77c8e4604045 272 #ifdef __CMSIS_RTOS
mbed_official 49:77c8e4604045 273 sysTimerTick();
mbed_official 49:77c8e4604045 274 #else
mbed_official 49:77c8e4604045 275 rt_tmr_tick ();
mbed_official 49:77c8e4604045 276 #endif
mbed_official 49:77c8e4604045 277
mbed_official 49:77c8e4604045 278 /* Switch back to highest ready task */
mbed_official 49:77c8e4604045 279 next = rt_get_first (&os_rdy);
mbed_official 49:77c8e4604045 280 rt_switch_req (next);
mbed_official 49:77c8e4604045 281 }
mbed_official 49:77c8e4604045 282
mbed_official 49:77c8e4604045 283 /*--------------------------- rt_stk_check ----------------------------------*/
mbed_official 49:77c8e4604045 284 __weak void rt_stk_check (void) {
mbed_official 49:77c8e4604045 285 /* Check for stack overflow. */
mbed_official 49:77c8e4604045 286 if (os_tsk.run->task_id == 0x01) {
mbed_official 49:77c8e4604045 287 // TODO: For the main thread the check should be done against the main heap pointer
mbed_official 49:77c8e4604045 288 } else {
mbed_official 49:77c8e4604045 289 if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||
mbed_official 49:77c8e4604045 290 (os_tsk.run->stack[0] != MAGIC_WORD)) {
mbed_official 49:77c8e4604045 291 os_error (OS_ERR_STK_OVF);
mbed_official 49:77c8e4604045 292 }
mbed_official 49:77c8e4604045 293 }
mbed_official 49:77c8e4604045 294 }
mbed_official 49:77c8e4604045 295
mbed_official 49:77c8e4604045 296 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 297 * end of file
mbed_official 49:77c8e4604045 298 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 299