Lab Checkoff

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
doubster
Date:
Wed Nov 13 20:00:28 2013 +0000
Revision:
0:67dbd54e60d4
Lab Checkoff

Who changed what in which revision?

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