skeleton for lab1

Dependencies:   AvailableMemory mbed-rtos mbed

Fork of helloaabbc by 32314 mbed

Committer:
y7jin
Date:
Thu Apr 03 22:56:32 2014 +0000
Revision:
0:1c8f2727e9f5
hello

Who changed what in which revision?

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