Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rt_System.c Source File

rt_System.c

00001 /*----------------------------------------------------------------------------
00002  *      RL-ARM - RTX
00003  *----------------------------------------------------------------------------
00004  *      Name:    RT_SYSTEM.C
00005  *      Purpose: System Task Manager
00006  *      Rev.:    V4.50
00007  *----------------------------------------------------------------------------
00008  *
00009  * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
00010  * All rights reserved.
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are met:
00013  *  - Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  *  - Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *  - Neither the name of ARM  nor the names of its contributors may be used 
00019  *    to endorse or promote products derived from this software without 
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
00026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  *---------------------------------------------------------------------------*/
00034 
00035 #include "rt_TypeDef.h"
00036 #include "RTX_Config.h"
00037 #include "rt_Task.h"
00038 #include "rt_System.h"
00039 #include "rt_Event.h"
00040 #include "rt_List.h"
00041 #include "rt_Mailbox.h"
00042 #include "rt_Semaphore.h"
00043 #include "rt_Time.h"
00044 #include "rt_Robin.h"
00045 #include "rt_HAL_CM.h"
00046 
00047 /*----------------------------------------------------------------------------
00048  *      Global Variables
00049  *---------------------------------------------------------------------------*/
00050 
00051 int os_tick_irqn;
00052 
00053 /*----------------------------------------------------------------------------
00054  *      Local Variables
00055  *---------------------------------------------------------------------------*/
00056 
00057 static volatile BIT os_lock;
00058 static volatile BIT os_psh_flag;
00059 static          U8  pend_flags;
00060 
00061 /*----------------------------------------------------------------------------
00062  *      Global Functions
00063  *---------------------------------------------------------------------------*/
00064 
00065 #if defined (__CC_ARM)
00066 __asm void $$RTX$$version (void) {
00067    /* Export a version number symbol for a version control. */
00068 
00069                 EXPORT  __RL_RTX_VER
00070 
00071 __RL_RTX_VER    EQU     0x450
00072 }
00073 #endif
00074 
00075 
00076 /*--------------------------- rt_suspend ------------------------------------*/
00077 U32 rt_suspend (void) {
00078   /* Suspend OS scheduler */
00079   U32 delta = 0xFFFF;
00080   
00081   rt_tsk_lock();
00082   
00083   if (os_dly.p_dlnk) {
00084     delta = os_dly.delta_time;
00085   }
00086   
00087   return (delta);
00088 }
00089 
00090 
00091 /*--------------------------- rt_resume -------------------------------------*/
00092 void rt_resume (U32 sleep_time) {
00093   /* Resume OS scheduler after suspend */
00094   P_TCB next;
00095   U32   delta;
00096 
00097   os_tsk.run->state = READY;
00098   rt_put_rdy_first (os_tsk.run);
00099 
00100   os_robin.task = NULL;
00101 
00102   /* Update delays. */
00103   if (os_dly.p_dlnk) {
00104     delta = sleep_time;
00105     if (delta >= os_dly.delta_time) {
00106       delta   -= os_dly.delta_time;
00107       os_time += os_dly.delta_time;
00108       os_dly.delta_time = 1;
00109       while (os_dly.p_dlnk) {
00110         rt_dec_dly();
00111         if (delta == 0) break;
00112         delta--;
00113         os_time++;
00114       }
00115     } else {
00116       os_time           += delta;
00117       os_dly.delta_time -= delta;
00118     }
00119   } else {
00120     os_time += sleep_time;
00121   }
00122   
00123   /* Switch back to highest ready task */
00124   next = rt_get_first (&os_rdy);
00125   rt_switch_req (next);
00126 
00127   rt_tsk_unlock();
00128 }
00129 
00130 
00131 /*--------------------------- rt_tsk_lock -----------------------------------*/
00132 
00133 void rt_tsk_lock (void) {
00134   /* Prevent task switching by locking out scheduler */
00135   if (os_tick_irqn < 0) {
00136     OS_LOCK();
00137     os_lock = __TRUE;
00138     OS_UNPEND (&pend_flags);
00139   } else {
00140     OS_X_LOCK(os_tick_irqn);
00141     os_lock = __TRUE;
00142     OS_X_UNPEND (&pend_flags);
00143   }
00144 }
00145 
00146 
00147 /*--------------------------- rt_tsk_unlock ---------------------------------*/
00148 
00149 void rt_tsk_unlock (void) {
00150   /* Unlock scheduler and re-enable task switching */
00151   if (os_tick_irqn < 0) {
00152     OS_UNLOCK();
00153     os_lock = __FALSE;
00154     OS_PEND (pend_flags, os_psh_flag);
00155     os_psh_flag = __FALSE;
00156   } else {
00157     OS_X_UNLOCK(os_tick_irqn);
00158     os_lock = __FALSE;
00159     OS_X_PEND (pend_flags, os_psh_flag);
00160     os_psh_flag = __FALSE;
00161   }
00162 }
00163 
00164 
00165 /*--------------------------- rt_psh_req ------------------------------------*/
00166 
00167 void rt_psh_req (void) {
00168   /* Initiate a post service handling request if required. */
00169   if (os_lock == __FALSE) {
00170     OS_PEND_IRQ ();
00171   }
00172   else {
00173     os_psh_flag = __TRUE;
00174   }
00175 }
00176 
00177 
00178 /*--------------------------- rt_pop_req ------------------------------------*/
00179 
00180 void rt_pop_req (void) {
00181   /* Process an ISR post service requests. */
00182   struct OS_XCB *p_CB;
00183   P_TCB next;
00184   U32  idx;
00185 
00186   os_tsk.run->state = READY;
00187   rt_put_rdy_first (os_tsk.run);
00188 
00189   idx = os_psq->last;
00190   while (os_psq->count) {
00191     p_CB = os_psq->q[idx].id;
00192     if (p_CB->cb_type == TCB) {
00193       /* Is of TCB type */
00194       rt_evt_psh ((P_TCB)p_CB, (U16)os_psq->q[idx].arg);
00195     }
00196     else if (p_CB->cb_type == MCB) {
00197       /* Is of MCB type */
00198       rt_mbx_psh ((P_MCB)p_CB, (void *)os_psq->q[idx].arg);
00199     }
00200     else {
00201       /* Must be of SCB type */
00202       rt_sem_psh ((P_SCB)p_CB);
00203     }
00204     if (++idx == os_psq->size) idx = 0;
00205     rt_dec (&os_psq->count);
00206   }
00207   os_psq->last = idx;
00208 
00209   next = rt_get_first (&os_rdy);
00210   rt_switch_req (next);
00211 }
00212 
00213 
00214 /*--------------------------- os_tick_init ----------------------------------*/
00215 
00216 __weak int os_tick_init (void) {
00217   /* Initialize SysTick timer as system tick timer. */
00218   rt_systick_init ();
00219   return (-1);  /* Return IRQ number of SysTick timer */
00220 }
00221 
00222 
00223 /*--------------------------- os_tick_irqack --------------------------------*/
00224 
00225 __weak void os_tick_irqack (void) {
00226   /* Acknowledge timer interrupt. */
00227 }
00228 
00229 
00230 /*--------------------------- rt_systick ------------------------------------*/
00231 
00232 extern void sysTimerTick(void);
00233 
00234 void rt_systick (void) {
00235   /* Check for system clock update, suspend running task. */
00236   P_TCB next;
00237 
00238   os_tsk.run->state = READY;
00239   rt_put_rdy_first (os_tsk.run);
00240 
00241   /* Check Round Robin timeout. */
00242   rt_chk_robin ();
00243 
00244   /* Update delays. */
00245   os_time++;
00246   rt_dec_dly ();
00247 
00248   /* Check the user timers. */
00249   sysTimerTick();
00250   
00251   /* Switch back to highest ready task */
00252   next = rt_get_first (&os_rdy);
00253   rt_switch_req (next);
00254 }
00255 
00256 /*--------------------------- rt_stk_check ----------------------------------*/
00257 #ifdef THREAD_STACK_DIAGNOSTIC
00258 typedef struct stack_boundaries_s {
00259     unsigned int top;
00260     unsigned int min;
00261 } stack_boundaries_t;
00262 stack_boundaries_t stack_boundaries[256];
00263 #endif
00264 
00265 __weak void rt_stk_check (void) {
00266 #ifdef THREAD_STACK_DIAGNOSTIC
00267     stack_boundaries_t* sb = &stack_boundaries[os_tsk.run->task_id];
00268     if (sb->top == 0) {
00269         sb->top = (unsigned int)os_tsk.run->stack + os_tsk.run->priv_stack;
00270     }
00271     if ((sb->min == 0) || (sb->min < os_tsk.run->tsk_stack))
00272         sb->min = os_tsk.run->tsk_stack;
00273 #endif
00274   /* Check for stack overflow */
00275   if (os_tsk.run->task_id == 0x01) {
00276       // TODO: For the main thread the check should be done against the heap pointer
00277   } else {
00278       if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) || 
00279           (os_tsk.run->stack[0] != MAGIC_WORD)) {
00280         os_error (OS_ERR_STK_OVF);
00281       }
00282   }
00283 }
00284 
00285 /*----------------------------------------------------------------------------
00286  * end of file
00287  *---------------------------------------------------------------------------*/
00288