mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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