.

Dependencies:   mbed-rtos

Dependents:   Glove

Fork of mbed-rtos by mbed official

Committer:
jboettcher
Date:
Wed Dec 07 05:05:18 2016 +0000
Revision:
124:62fd81674a5d
Parent:
80:2dab120a94c2
.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 80:2dab120a94c2 1 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 2 * RL-ARM - RTX
mbed_official 80:2dab120a94c2 3 *----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 4 * Name: RT_SYSTEM.C
mbed_official 80:2dab120a94c2 5 * Purpose: System Task Manager
mbed_official 80:2dab120a94c2 6 * Rev.: V4.60
mbed_official 80:2dab120a94c2 7 *----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 8 *
mbed_official 80:2dab120a94c2 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
mbed_official 80:2dab120a94c2 10 * All rights reserved.
mbed_official 80:2dab120a94c2 11 * Redistribution and use in source and binary forms, with or without
mbed_official 80:2dab120a94c2 12 * modification, are permitted provided that the following conditions are met:
mbed_official 80:2dab120a94c2 13 * - Redistributions of source code must retain the above copyright
mbed_official 80:2dab120a94c2 14 * notice, this list of conditions and the following disclaimer.
mbed_official 80:2dab120a94c2 15 * - Redistributions in binary form must reproduce the above copyright
mbed_official 80:2dab120a94c2 16 * notice, this list of conditions and the following disclaimer in the
mbed_official 80:2dab120a94c2 17 * documentation and/or other materials provided with the distribution.
mbed_official 80:2dab120a94c2 18 * - Neither the name of ARM nor the names of its contributors may be used
mbed_official 80:2dab120a94c2 19 * to endorse or promote products derived from this software without
mbed_official 80:2dab120a94c2 20 * specific prior written permission.
mbed_official 80:2dab120a94c2 21 *
mbed_official 80:2dab120a94c2 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 80:2dab120a94c2 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 80:2dab120a94c2 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
mbed_official 80:2dab120a94c2 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
mbed_official 80:2dab120a94c2 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
mbed_official 80:2dab120a94c2 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
mbed_official 80:2dab120a94c2 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 80:2dab120a94c2 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 80:2dab120a94c2 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
mbed_official 80:2dab120a94c2 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 80:2dab120a94c2 32 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 80:2dab120a94c2 33 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 34
mbed_official 80:2dab120a94c2 35 #include "rt_TypeDef.h"
mbed_official 80:2dab120a94c2 36 #include "RTX_Conf.h"
mbed_official 80:2dab120a94c2 37 #include "rt_Task.h"
mbed_official 80:2dab120a94c2 38 #include "rt_System.h"
mbed_official 80:2dab120a94c2 39 #include "rt_Event.h"
mbed_official 80:2dab120a94c2 40 #include "rt_List.h"
mbed_official 80:2dab120a94c2 41 #include "rt_Mailbox.h"
mbed_official 80:2dab120a94c2 42 #include "rt_Semaphore.h"
mbed_official 80:2dab120a94c2 43 #include "rt_Time.h"
mbed_official 80:2dab120a94c2 44 #include "rt_Robin.h"
mbed_official 80:2dab120a94c2 45 #include "rt_HAL_CM.h"
mbed_official 80:2dab120a94c2 46
mbed_official 80:2dab120a94c2 47 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 48 * Global Variables
mbed_official 80:2dab120a94c2 49 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 50
mbed_official 80:2dab120a94c2 51 int os_tick_irqn;
mbed_official 80:2dab120a94c2 52
mbed_official 80:2dab120a94c2 53 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 54 * Local Variables
mbed_official 80:2dab120a94c2 55 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 56
mbed_official 80:2dab120a94c2 57 static volatile BIT os_lock;
mbed_official 80:2dab120a94c2 58 static volatile BIT os_psh_flag;
mbed_official 80:2dab120a94c2 59 static U8 pend_flags;
mbed_official 80:2dab120a94c2 60
mbed_official 80:2dab120a94c2 61 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 62 * Global Functions
mbed_official 80:2dab120a94c2 63 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 64
mbed_official 80:2dab120a94c2 65 #if defined (__CC_ARM)
mbed_official 80:2dab120a94c2 66 __asm void $$RTX$$version (void) {
mbed_official 80:2dab120a94c2 67 /* Export a version number symbol for a version control. */
mbed_official 80:2dab120a94c2 68
mbed_official 80:2dab120a94c2 69 EXPORT __RL_RTX_VER
mbed_official 80:2dab120a94c2 70
mbed_official 80:2dab120a94c2 71 __RL_RTX_VER EQU 0x450
mbed_official 80:2dab120a94c2 72 }
mbed_official 80:2dab120a94c2 73 #endif
mbed_official 80:2dab120a94c2 74
mbed_official 80:2dab120a94c2 75
mbed_official 80:2dab120a94c2 76 /*--------------------------- rt_suspend ------------------------------------*/
mbed_official 80:2dab120a94c2 77 U32 rt_suspend (void) {
mbed_official 80:2dab120a94c2 78 /* Suspend OS scheduler */
mbed_official 80:2dab120a94c2 79 U32 delta = 0xFFFF;
mbed_official 80:2dab120a94c2 80
mbed_official 80:2dab120a94c2 81 rt_tsk_lock();
mbed_official 80:2dab120a94c2 82
mbed_official 80:2dab120a94c2 83 if (os_dly.p_dlnk) {
mbed_official 80:2dab120a94c2 84 delta = os_dly.delta_time;
mbed_official 80:2dab120a94c2 85 }
mbed_official 80:2dab120a94c2 86 #ifndef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 87 if (os_tmr.next) {
mbed_official 80:2dab120a94c2 88 if (os_tmr.tcnt < delta) delta = os_tmr.tcnt;
mbed_official 80:2dab120a94c2 89 }
mbed_official 80:2dab120a94c2 90 #endif
mbed_official 80:2dab120a94c2 91
mbed_official 80:2dab120a94c2 92 return (delta);
mbed_official 80:2dab120a94c2 93 }
mbed_official 80:2dab120a94c2 94
mbed_official 80:2dab120a94c2 95
mbed_official 80:2dab120a94c2 96 /*--------------------------- rt_resume -------------------------------------*/
mbed_official 80:2dab120a94c2 97 void rt_resume (U32 sleep_time) {
mbed_official 80:2dab120a94c2 98 /* Resume OS scheduler after suspend */
mbed_official 80:2dab120a94c2 99 P_TCB next;
mbed_official 80:2dab120a94c2 100 U32 delta;
mbed_official 80:2dab120a94c2 101
mbed_official 80:2dab120a94c2 102 os_tsk.run->state = READY;
mbed_official 80:2dab120a94c2 103 rt_put_rdy_first (os_tsk.run);
mbed_official 80:2dab120a94c2 104
mbed_official 80:2dab120a94c2 105 os_robin.task = NULL;
mbed_official 80:2dab120a94c2 106
mbed_official 80:2dab120a94c2 107 /* Update delays. */
mbed_official 80:2dab120a94c2 108 if (os_dly.p_dlnk) {
mbed_official 80:2dab120a94c2 109 delta = sleep_time;
mbed_official 80:2dab120a94c2 110 if (delta >= os_dly.delta_time) {
mbed_official 80:2dab120a94c2 111 delta -= os_dly.delta_time;
mbed_official 80:2dab120a94c2 112 os_time += os_dly.delta_time;
mbed_official 80:2dab120a94c2 113 os_dly.delta_time = 1;
mbed_official 80:2dab120a94c2 114 while (os_dly.p_dlnk) {
mbed_official 80:2dab120a94c2 115 rt_dec_dly();
mbed_official 80:2dab120a94c2 116 if (delta == 0) break;
mbed_official 80:2dab120a94c2 117 delta--;
mbed_official 80:2dab120a94c2 118 os_time++;
mbed_official 80:2dab120a94c2 119 }
mbed_official 80:2dab120a94c2 120 } else {
mbed_official 80:2dab120a94c2 121 os_time += delta;
mbed_official 80:2dab120a94c2 122 os_dly.delta_time -= delta;
mbed_official 80:2dab120a94c2 123 }
mbed_official 80:2dab120a94c2 124 } else {
mbed_official 80:2dab120a94c2 125 os_time += sleep_time;
mbed_official 80:2dab120a94c2 126 }
mbed_official 80:2dab120a94c2 127
mbed_official 80:2dab120a94c2 128 #ifndef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 129 /* Check the user timers. */
mbed_official 80:2dab120a94c2 130 if (os_tmr.next) {
mbed_official 80:2dab120a94c2 131 delta = sleep_time;
mbed_official 80:2dab120a94c2 132 if (delta >= os_tmr.tcnt) {
mbed_official 80:2dab120a94c2 133 delta -= os_tmr.tcnt;
mbed_official 80:2dab120a94c2 134 os_tmr.tcnt = 1;
mbed_official 80:2dab120a94c2 135 while (os_tmr.next) {
mbed_official 80:2dab120a94c2 136 rt_tmr_tick();
mbed_official 80:2dab120a94c2 137 if (delta == 0) break;
mbed_official 80:2dab120a94c2 138 delta--;
mbed_official 80:2dab120a94c2 139 }
mbed_official 80:2dab120a94c2 140 } else {
mbed_official 80:2dab120a94c2 141 os_tmr.tcnt -= delta;
mbed_official 80:2dab120a94c2 142 }
mbed_official 80:2dab120a94c2 143 }
mbed_official 80:2dab120a94c2 144 #endif
mbed_official 80:2dab120a94c2 145
mbed_official 80:2dab120a94c2 146 /* Switch back to highest ready task */
mbed_official 80:2dab120a94c2 147 next = rt_get_first (&os_rdy);
mbed_official 80:2dab120a94c2 148 rt_switch_req (next);
mbed_official 80:2dab120a94c2 149
mbed_official 80:2dab120a94c2 150 rt_tsk_unlock();
mbed_official 80:2dab120a94c2 151 }
mbed_official 80:2dab120a94c2 152
mbed_official 80:2dab120a94c2 153
mbed_official 80:2dab120a94c2 154 /*--------------------------- rt_tsk_lock -----------------------------------*/
mbed_official 80:2dab120a94c2 155
mbed_official 80:2dab120a94c2 156 void rt_tsk_lock (void) {
mbed_official 80:2dab120a94c2 157 /* Prevent task switching by locking out scheduler */
mbed_official 80:2dab120a94c2 158 OS_X_LOCK(os_tick_irqn);
mbed_official 80:2dab120a94c2 159 os_lock = __TRUE;
mbed_official 80:2dab120a94c2 160 OS_X_UNPEND (&pend_flags);
mbed_official 80:2dab120a94c2 161 }
mbed_official 80:2dab120a94c2 162
mbed_official 80:2dab120a94c2 163
mbed_official 80:2dab120a94c2 164 /*--------------------------- rt_tsk_unlock ---------------------------------*/
mbed_official 80:2dab120a94c2 165
mbed_official 80:2dab120a94c2 166 void rt_tsk_unlock (void) {
mbed_official 80:2dab120a94c2 167 /* Unlock scheduler and re-enable task switching */
mbed_official 80:2dab120a94c2 168 OS_X_UNLOCK(os_tick_irqn);
mbed_official 80:2dab120a94c2 169 os_lock = __FALSE;
mbed_official 80:2dab120a94c2 170 OS_X_PEND (pend_flags, os_psh_flag);
mbed_official 80:2dab120a94c2 171 os_psh_flag = __FALSE;
mbed_official 80:2dab120a94c2 172 }
mbed_official 80:2dab120a94c2 173
mbed_official 80:2dab120a94c2 174
mbed_official 80:2dab120a94c2 175 /*--------------------------- rt_psh_req ------------------------------------*/
mbed_official 80:2dab120a94c2 176
mbed_official 80:2dab120a94c2 177 void rt_psh_req (void) {
mbed_official 80:2dab120a94c2 178 /* Initiate a post service handling request if required. */
mbed_official 80:2dab120a94c2 179 if (os_lock == __FALSE) {
mbed_official 80:2dab120a94c2 180 OS_PEND_IRQ ();
mbed_official 80:2dab120a94c2 181 }
mbed_official 80:2dab120a94c2 182 else {
mbed_official 80:2dab120a94c2 183 os_psh_flag = __TRUE;
mbed_official 80:2dab120a94c2 184 }
mbed_official 80:2dab120a94c2 185 }
mbed_official 80:2dab120a94c2 186
mbed_official 80:2dab120a94c2 187
mbed_official 80:2dab120a94c2 188 /*--------------------------- rt_pop_req ------------------------------------*/
mbed_official 80:2dab120a94c2 189
mbed_official 80:2dab120a94c2 190 void rt_pop_req (void) {
mbed_official 80:2dab120a94c2 191 /* Process an ISR post service requests. */
mbed_official 80:2dab120a94c2 192 struct OS_XCB *p_CB;
mbed_official 80:2dab120a94c2 193 P_TCB next;
mbed_official 80:2dab120a94c2 194 U32 idx;
mbed_official 80:2dab120a94c2 195
mbed_official 80:2dab120a94c2 196 os_tsk.run->state = READY;
mbed_official 80:2dab120a94c2 197 rt_put_rdy_first (os_tsk.run);
mbed_official 80:2dab120a94c2 198
mbed_official 80:2dab120a94c2 199 idx = os_psq->last;
mbed_official 80:2dab120a94c2 200 while (os_psq->count) {
mbed_official 80:2dab120a94c2 201 p_CB = os_psq->q[idx].id;
mbed_official 80:2dab120a94c2 202 if (p_CB->cb_type == TCB) {
mbed_official 80:2dab120a94c2 203 /* Is of TCB type */
mbed_official 80:2dab120a94c2 204 rt_evt_psh ((P_TCB)p_CB, (U16)os_psq->q[idx].arg);
mbed_official 80:2dab120a94c2 205 }
mbed_official 80:2dab120a94c2 206 else if (p_CB->cb_type == MCB) {
mbed_official 80:2dab120a94c2 207 /* Is of MCB type */
mbed_official 80:2dab120a94c2 208 rt_mbx_psh ((P_MCB)p_CB, (void *)os_psq->q[idx].arg);
mbed_official 80:2dab120a94c2 209 }
mbed_official 80:2dab120a94c2 210 else {
mbed_official 80:2dab120a94c2 211 /* Must be of SCB type */
mbed_official 80:2dab120a94c2 212 rt_sem_psh ((P_SCB)p_CB);
mbed_official 80:2dab120a94c2 213 }
mbed_official 80:2dab120a94c2 214 if (++idx == os_psq->size) idx = 0;
mbed_official 80:2dab120a94c2 215 rt_dec (&os_psq->count);
mbed_official 80:2dab120a94c2 216 }
mbed_official 80:2dab120a94c2 217 os_psq->last = idx;
mbed_official 80:2dab120a94c2 218
mbed_official 80:2dab120a94c2 219 next = rt_get_first (&os_rdy);
mbed_official 80:2dab120a94c2 220 rt_switch_req (next);
mbed_official 80:2dab120a94c2 221 }
mbed_official 80:2dab120a94c2 222
mbed_official 80:2dab120a94c2 223
mbed_official 80:2dab120a94c2 224 /*--------------------------- os_tick_init ----------------------------------*/
mbed_official 80:2dab120a94c2 225
mbed_official 80:2dab120a94c2 226 __weak int os_tick_init (void) {
mbed_official 80:2dab120a94c2 227 /* Initialize SysTick timer as system tick timer. */
mbed_official 80:2dab120a94c2 228 rt_systick_init ();
mbed_official 80:2dab120a94c2 229 return (SYS_TICK_IRQn); /* Return IRQ number of SysTick timer */
mbed_official 80:2dab120a94c2 230 }
mbed_official 80:2dab120a94c2 231
mbed_official 80:2dab120a94c2 232
mbed_official 80:2dab120a94c2 233 /*--------------------------- os_tick_irqack --------------------------------*/
mbed_official 80:2dab120a94c2 234
mbed_official 80:2dab120a94c2 235 __weak void os_tick_irqack (void) {
mbed_official 80:2dab120a94c2 236 /* Acknowledge timer interrupt. */
mbed_official 80:2dab120a94c2 237 }
mbed_official 80:2dab120a94c2 238
mbed_official 80:2dab120a94c2 239
mbed_official 80:2dab120a94c2 240 /*--------------------------- rt_systick ------------------------------------*/
mbed_official 80:2dab120a94c2 241
mbed_official 80:2dab120a94c2 242 extern void sysTimerTick(void);
mbed_official 80:2dab120a94c2 243
mbed_official 80:2dab120a94c2 244 void rt_systick (void) {
mbed_official 80:2dab120a94c2 245 if(NVIC_Pending(SYS_TICK_IRQn)){
mbed_official 80:2dab120a94c2 246 rt_pop_req();
mbed_official 80:2dab120a94c2 247 NVIC_UnpendIRQ(SYS_TICK_IRQn);
mbed_official 80:2dab120a94c2 248 SYS_TICK_TIMER->IR = 0xF; // clear timer interrupt
mbed_official 80:2dab120a94c2 249 return;
mbed_official 80:2dab120a94c2 250 }
mbed_official 80:2dab120a94c2 251 /* Check for system clock update, suspend running task. */
mbed_official 80:2dab120a94c2 252 P_TCB next;
mbed_official 80:2dab120a94c2 253
mbed_official 80:2dab120a94c2 254 os_tsk.run->state = READY;
mbed_official 80:2dab120a94c2 255 rt_put_rdy_first (os_tsk.run);
mbed_official 80:2dab120a94c2 256
mbed_official 80:2dab120a94c2 257 /* Check Round Robin timeout. */
mbed_official 80:2dab120a94c2 258 rt_chk_robin ();
mbed_official 80:2dab120a94c2 259
mbed_official 80:2dab120a94c2 260 /* Update delays. */
mbed_official 80:2dab120a94c2 261 os_time++;
mbed_official 80:2dab120a94c2 262 rt_dec_dly ();
mbed_official 80:2dab120a94c2 263
mbed_official 80:2dab120a94c2 264 /* Check the user timers. */
mbed_official 80:2dab120a94c2 265 #ifdef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 266 sysTimerTick();
mbed_official 80:2dab120a94c2 267 #else
mbed_official 80:2dab120a94c2 268 rt_tmr_tick ();
mbed_official 80:2dab120a94c2 269 #endif
mbed_official 80:2dab120a94c2 270
mbed_official 80:2dab120a94c2 271 /* Switch back to highest ready task */
mbed_official 80:2dab120a94c2 272 next = rt_get_first (&os_rdy);
mbed_official 80:2dab120a94c2 273 rt_switch_req (next);
mbed_official 80:2dab120a94c2 274 SYS_TICK_TIMER->IR = 0xF; // clear timer interrupt
mbed_official 80:2dab120a94c2 275 }
mbed_official 80:2dab120a94c2 276
mbed_official 80:2dab120a94c2 277 /*--------------------------- rt_stk_check ----------------------------------*/
mbed_official 80:2dab120a94c2 278 __weak void rt_stk_check (void) {
mbed_official 80:2dab120a94c2 279 /* Check for stack overflow. */
mbed_official 80:2dab120a94c2 280 if (os_tsk.run->task_id == 0x01) {
mbed_official 80:2dab120a94c2 281 // TODO: For the main thread the check should be done against the main heap pointer
mbed_official 80:2dab120a94c2 282 } else {
mbed_official 80:2dab120a94c2 283 if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||
mbed_official 80:2dab120a94c2 284 (os_tsk.run->stack[0] != MAGIC_WORD)) {
mbed_official 80:2dab120a94c2 285 os_error (OS_ERR_STK_OVF);
mbed_official 80:2dab120a94c2 286 }
mbed_official 80:2dab120a94c2 287 }
mbed_official 80:2dab120a94c2 288 }
mbed_official 80:2dab120a94c2 289
mbed_official 80:2dab120a94c2 290 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 291 * end of file
mbed_official 80:2dab120a94c2 292 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 293