Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
apatel336
Date:
Thu Oct 17 13:26:38 2013 +0000
Revision:
0:1496281373a5
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apatel336 0:1496281373a5 1 /*----------------------------------------------------------------------------
apatel336 0:1496281373a5 2 * RL-ARM - RTX
apatel336 0:1496281373a5 3 *----------------------------------------------------------------------------
apatel336 0:1496281373a5 4 * Name: RT_TASK.C
apatel336 0:1496281373a5 5 * Purpose: Task functions and system start up.
apatel336 0:1496281373a5 6 * Rev.: V4.60
apatel336 0:1496281373a5 7 *----------------------------------------------------------------------------
apatel336 0:1496281373a5 8 *
apatel336 0:1496281373a5 9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
apatel336 0:1496281373a5 10 * All rights reserved.
apatel336 0:1496281373a5 11 * Redistribution and use in source and binary forms, with or without
apatel336 0:1496281373a5 12 * modification, are permitted provided that the following conditions are met:
apatel336 0:1496281373a5 13 * - Redistributions of source code must retain the above copyright
apatel336 0:1496281373a5 14 * notice, this list of conditions and the following disclaimer.
apatel336 0:1496281373a5 15 * - Redistributions in binary form must reproduce the above copyright
apatel336 0:1496281373a5 16 * notice, this list of conditions and the following disclaimer in the
apatel336 0:1496281373a5 17 * documentation and/or other materials provided with the distribution.
apatel336 0:1496281373a5 18 * - Neither the name of ARM nor the names of its contributors may be used
apatel336 0:1496281373a5 19 * to endorse or promote products derived from this software without
apatel336 0:1496281373a5 20 * specific prior written permission.
apatel336 0:1496281373a5 21 *
apatel336 0:1496281373a5 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
apatel336 0:1496281373a5 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
apatel336 0:1496281373a5 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
apatel336 0:1496281373a5 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
apatel336 0:1496281373a5 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
apatel336 0:1496281373a5 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
apatel336 0:1496281373a5 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
apatel336 0:1496281373a5 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
apatel336 0:1496281373a5 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
apatel336 0:1496281373a5 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
apatel336 0:1496281373a5 32 * POSSIBILITY OF SUCH DAMAGE.
apatel336 0:1496281373a5 33 *---------------------------------------------------------------------------*/
apatel336 0:1496281373a5 34
apatel336 0:1496281373a5 35 #include "rt_TypeDef.h"
apatel336 0:1496281373a5 36 #include "RTX_Conf.h"
apatel336 0:1496281373a5 37 #include "rt_System.h"
apatel336 0:1496281373a5 38 #include "rt_Task.h"
apatel336 0:1496281373a5 39 #include "rt_List.h"
apatel336 0:1496281373a5 40 #include "rt_MemBox.h"
apatel336 0:1496281373a5 41 #include "rt_Robin.h"
apatel336 0:1496281373a5 42 #include "rt_HAL_CM.h"
apatel336 0:1496281373a5 43
apatel336 0:1496281373a5 44 /*----------------------------------------------------------------------------
apatel336 0:1496281373a5 45 * Global Variables
apatel336 0:1496281373a5 46 *---------------------------------------------------------------------------*/
apatel336 0:1496281373a5 47
apatel336 0:1496281373a5 48 /* Running and next task info. */
apatel336 0:1496281373a5 49 struct OS_TSK os_tsk;
apatel336 0:1496281373a5 50
apatel336 0:1496281373a5 51 /* Task Control Blocks of idle demon */
apatel336 0:1496281373a5 52 struct OS_TCB os_idle_TCB;
apatel336 0:1496281373a5 53
apatel336 0:1496281373a5 54
apatel336 0:1496281373a5 55 /*----------------------------------------------------------------------------
apatel336 0:1496281373a5 56 * Local Functions
apatel336 0:1496281373a5 57 *---------------------------------------------------------------------------*/
apatel336 0:1496281373a5 58
apatel336 0:1496281373a5 59 OS_TID rt_get_TID (void) {
apatel336 0:1496281373a5 60 U32 tid;
apatel336 0:1496281373a5 61
apatel336 0:1496281373a5 62 for (tid = 1; tid <= os_maxtaskrun; tid++) {
apatel336 0:1496281373a5 63 if (os_active_TCB[tid-1] == NULL) {
apatel336 0:1496281373a5 64 return ((OS_TID)tid);
apatel336 0:1496281373a5 65 }
apatel336 0:1496281373a5 66 }
apatel336 0:1496281373a5 67 return (0);
apatel336 0:1496281373a5 68 }
apatel336 0:1496281373a5 69
apatel336 0:1496281373a5 70 #if defined (__CC_ARM) && !defined (__MICROLIB)
apatel336 0:1496281373a5 71 /*--------------------------- __user_perthread_libspace ---------------------*/
apatel336 0:1496281373a5 72 extern void *__libspace_start;
apatel336 0:1496281373a5 73
apatel336 0:1496281373a5 74 void *__user_perthread_libspace (void) {
apatel336 0:1496281373a5 75 /* Provide a separate libspace for each task. */
apatel336 0:1496281373a5 76 if (os_tsk.run == NULL) {
apatel336 0:1496281373a5 77 /* RTX not running yet. */
apatel336 0:1496281373a5 78 return (&__libspace_start);
apatel336 0:1496281373a5 79 }
apatel336 0:1496281373a5 80 return (void *)(os_tsk.run->std_libspace);
apatel336 0:1496281373a5 81 }
apatel336 0:1496281373a5 82 #endif
apatel336 0:1496281373a5 83
apatel336 0:1496281373a5 84 /*--------------------------- rt_init_context -------------------------------*/
apatel336 0:1496281373a5 85
apatel336 0:1496281373a5 86 void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) {
apatel336 0:1496281373a5 87 /* Initialize general part of the Task Control Block. */
apatel336 0:1496281373a5 88 p_TCB->cb_type = TCB;
apatel336 0:1496281373a5 89 p_TCB->state = READY;
apatel336 0:1496281373a5 90 p_TCB->prio = priority;
apatel336 0:1496281373a5 91 p_TCB->p_lnk = NULL;
apatel336 0:1496281373a5 92 p_TCB->p_rlnk = NULL;
apatel336 0:1496281373a5 93 p_TCB->p_dlnk = NULL;
apatel336 0:1496281373a5 94 p_TCB->p_blnk = NULL;
apatel336 0:1496281373a5 95 p_TCB->delta_time = 0;
apatel336 0:1496281373a5 96 p_TCB->interval_time = 0;
apatel336 0:1496281373a5 97 p_TCB->events = 0;
apatel336 0:1496281373a5 98 p_TCB->waits = 0;
apatel336 0:1496281373a5 99 p_TCB->stack_frame = 0;
apatel336 0:1496281373a5 100
apatel336 0:1496281373a5 101 rt_init_stack (p_TCB, task_body);
apatel336 0:1496281373a5 102 }
apatel336 0:1496281373a5 103
apatel336 0:1496281373a5 104
apatel336 0:1496281373a5 105 /*--------------------------- rt_switch_req ---------------------------------*/
apatel336 0:1496281373a5 106
apatel336 0:1496281373a5 107 void rt_switch_req (P_TCB p_new) {
apatel336 0:1496281373a5 108 /* Switch to next task (identified by "p_new"). */
apatel336 0:1496281373a5 109 os_tsk.new_tsk = p_new;
apatel336 0:1496281373a5 110 p_new->state = RUNNING;
apatel336 0:1496281373a5 111 DBG_TASK_SWITCH(p_new->task_id);
apatel336 0:1496281373a5 112 }
apatel336 0:1496281373a5 113
apatel336 0:1496281373a5 114
apatel336 0:1496281373a5 115 /*--------------------------- rt_dispatch -----------------------------------*/
apatel336 0:1496281373a5 116
apatel336 0:1496281373a5 117 void rt_dispatch (P_TCB next_TCB) {
apatel336 0:1496281373a5 118 /* Dispatch next task if any identified or dispatch highest ready task */
apatel336 0:1496281373a5 119 /* "next_TCB" identifies a task to run or has value NULL (=no next task) */
apatel336 0:1496281373a5 120 if (next_TCB == NULL) {
apatel336 0:1496281373a5 121 /* Running task was blocked: continue with highest ready task */
apatel336 0:1496281373a5 122 next_TCB = rt_get_first (&os_rdy);
apatel336 0:1496281373a5 123 rt_switch_req (next_TCB);
apatel336 0:1496281373a5 124 }
apatel336 0:1496281373a5 125 else {
apatel336 0:1496281373a5 126 /* Check which task continues */
apatel336 0:1496281373a5 127 if (next_TCB->prio > os_tsk.run->prio) {
apatel336 0:1496281373a5 128 /* preempt running task */
apatel336 0:1496281373a5 129 rt_put_rdy_first (os_tsk.run);
apatel336 0:1496281373a5 130 os_tsk.run->state = READY;
apatel336 0:1496281373a5 131 rt_switch_req (next_TCB);
apatel336 0:1496281373a5 132 }
apatel336 0:1496281373a5 133 else {
apatel336 0:1496281373a5 134 /* put next task into ready list, no task switch takes place */
apatel336 0:1496281373a5 135 next_TCB->state = READY;
apatel336 0:1496281373a5 136 rt_put_prio (&os_rdy, next_TCB);
apatel336 0:1496281373a5 137 }
apatel336 0:1496281373a5 138 }
apatel336 0:1496281373a5 139 }
apatel336 0:1496281373a5 140
apatel336 0:1496281373a5 141
apatel336 0:1496281373a5 142 /*--------------------------- rt_block --------------------------------------*/
apatel336 0:1496281373a5 143
apatel336 0:1496281373a5 144 void rt_block (U16 timeout, U8 block_state) {
apatel336 0:1496281373a5 145 /* Block running task and choose next ready task. */
apatel336 0:1496281373a5 146 /* "timeout" sets a time-out value or is 0xffff (=no time-out). */
apatel336 0:1496281373a5 147 /* "block_state" defines the appropriate task state */
apatel336 0:1496281373a5 148 P_TCB next_TCB;
apatel336 0:1496281373a5 149
apatel336 0:1496281373a5 150 if (timeout) {
apatel336 0:1496281373a5 151 if (timeout < 0xffff) {
apatel336 0:1496281373a5 152 rt_put_dly (os_tsk.run, timeout);
apatel336 0:1496281373a5 153 }
apatel336 0:1496281373a5 154 os_tsk.run->state = block_state;
apatel336 0:1496281373a5 155 next_TCB = rt_get_first (&os_rdy);
apatel336 0:1496281373a5 156 rt_switch_req (next_TCB);
apatel336 0:1496281373a5 157 }
apatel336 0:1496281373a5 158 }
apatel336 0:1496281373a5 159
apatel336 0:1496281373a5 160
apatel336 0:1496281373a5 161 /*--------------------------- rt_tsk_pass -----------------------------------*/
apatel336 0:1496281373a5 162
apatel336 0:1496281373a5 163 void rt_tsk_pass (void) {
apatel336 0:1496281373a5 164 /* Allow tasks of same priority level to run cooperatively.*/
apatel336 0:1496281373a5 165 P_TCB p_new;
apatel336 0:1496281373a5 166
apatel336 0:1496281373a5 167 p_new = rt_get_same_rdy_prio();
apatel336 0:1496281373a5 168 if (p_new != NULL) {
apatel336 0:1496281373a5 169 rt_put_prio ((P_XCB)&os_rdy, os_tsk.run);
apatel336 0:1496281373a5 170 os_tsk.run->state = READY;
apatel336 0:1496281373a5 171 rt_switch_req (p_new);
apatel336 0:1496281373a5 172 }
apatel336 0:1496281373a5 173 }
apatel336 0:1496281373a5 174
apatel336 0:1496281373a5 175
apatel336 0:1496281373a5 176 /*--------------------------- rt_tsk_self -----------------------------------*/
apatel336 0:1496281373a5 177
apatel336 0:1496281373a5 178 OS_TID rt_tsk_self (void) {
apatel336 0:1496281373a5 179 /* Return own task identifier value. */
apatel336 0:1496281373a5 180 if (os_tsk.run == NULL) {
apatel336 0:1496281373a5 181 return (0);
apatel336 0:1496281373a5 182 }
apatel336 0:1496281373a5 183 return (os_tsk.run->task_id);
apatel336 0:1496281373a5 184 }
apatel336 0:1496281373a5 185
apatel336 0:1496281373a5 186
apatel336 0:1496281373a5 187 /*--------------------------- rt_tsk_prio -----------------------------------*/
apatel336 0:1496281373a5 188
apatel336 0:1496281373a5 189 OS_RESULT rt_tsk_prio (OS_TID task_id, U8 new_prio) {
apatel336 0:1496281373a5 190 /* Change execution priority of a task to "new_prio". */
apatel336 0:1496281373a5 191 P_TCB p_task;
apatel336 0:1496281373a5 192
apatel336 0:1496281373a5 193 if (task_id == 0) {
apatel336 0:1496281373a5 194 /* Change execution priority of calling task. */
apatel336 0:1496281373a5 195 os_tsk.run->prio = new_prio;
apatel336 0:1496281373a5 196 run:if (rt_rdy_prio() > new_prio) {
apatel336 0:1496281373a5 197 rt_put_prio (&os_rdy, os_tsk.run);
apatel336 0:1496281373a5 198 os_tsk.run->state = READY;
apatel336 0:1496281373a5 199 rt_dispatch (NULL);
apatel336 0:1496281373a5 200 }
apatel336 0:1496281373a5 201 return (OS_R_OK);
apatel336 0:1496281373a5 202 }
apatel336 0:1496281373a5 203
apatel336 0:1496281373a5 204 /* Find the task in the "os_active_TCB" array. */
apatel336 0:1496281373a5 205 if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) {
apatel336 0:1496281373a5 206 /* Task with "task_id" not found or not started. */
apatel336 0:1496281373a5 207 return (OS_R_NOK);
apatel336 0:1496281373a5 208 }
apatel336 0:1496281373a5 209 p_task = os_active_TCB[task_id-1];
apatel336 0:1496281373a5 210 p_task->prio = new_prio;
apatel336 0:1496281373a5 211 if (p_task == os_tsk.run) {
apatel336 0:1496281373a5 212 goto run;
apatel336 0:1496281373a5 213 }
apatel336 0:1496281373a5 214 rt_resort_prio (p_task);
apatel336 0:1496281373a5 215 if (p_task->state == READY) {
apatel336 0:1496281373a5 216 /* Task enqueued in a ready list. */
apatel336 0:1496281373a5 217 p_task = rt_get_first (&os_rdy);
apatel336 0:1496281373a5 218 rt_dispatch (p_task);
apatel336 0:1496281373a5 219 }
apatel336 0:1496281373a5 220 return (OS_R_OK);
apatel336 0:1496281373a5 221 }
apatel336 0:1496281373a5 222
apatel336 0:1496281373a5 223 /*--------------------------- rt_tsk_delete ---------------------------------*/
apatel336 0:1496281373a5 224
apatel336 0:1496281373a5 225 OS_RESULT rt_tsk_delete (OS_TID task_id) {
apatel336 0:1496281373a5 226 /* Terminate the task identified with "task_id". */
apatel336 0:1496281373a5 227 P_TCB task_context;
apatel336 0:1496281373a5 228
apatel336 0:1496281373a5 229 if (task_id == 0 || task_id == os_tsk.run->task_id) {
apatel336 0:1496281373a5 230 /* Terminate itself. */
apatel336 0:1496281373a5 231 os_tsk.run->state = INACTIVE;
apatel336 0:1496281373a5 232 os_tsk.run->tsk_stack = rt_get_PSP ();
apatel336 0:1496281373a5 233 rt_stk_check ();
apatel336 0:1496281373a5 234 os_active_TCB[os_tsk.run->task_id-1] = NULL;
apatel336 0:1496281373a5 235
apatel336 0:1496281373a5 236 os_tsk.run->stack = NULL;
apatel336 0:1496281373a5 237 DBG_TASK_NOTIFY(os_tsk.run, __FALSE);
apatel336 0:1496281373a5 238 os_tsk.run = NULL;
apatel336 0:1496281373a5 239 rt_dispatch (NULL);
apatel336 0:1496281373a5 240 /* The program should never come to this point. */
apatel336 0:1496281373a5 241 }
apatel336 0:1496281373a5 242 else {
apatel336 0:1496281373a5 243 /* Find the task in the "os_active_TCB" array. */
apatel336 0:1496281373a5 244 if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) {
apatel336 0:1496281373a5 245 /* Task with "task_id" not found or not started. */
apatel336 0:1496281373a5 246 return (OS_R_NOK);
apatel336 0:1496281373a5 247 }
apatel336 0:1496281373a5 248 task_context = os_active_TCB[task_id-1];
apatel336 0:1496281373a5 249 rt_rmv_list (task_context);
apatel336 0:1496281373a5 250 rt_rmv_dly (task_context);
apatel336 0:1496281373a5 251 os_active_TCB[task_id-1] = NULL;
apatel336 0:1496281373a5 252
apatel336 0:1496281373a5 253 task_context->stack = NULL;
apatel336 0:1496281373a5 254 DBG_TASK_NOTIFY(task_context, __FALSE);
apatel336 0:1496281373a5 255 }
apatel336 0:1496281373a5 256 return (OS_R_OK);
apatel336 0:1496281373a5 257 }
apatel336 0:1496281373a5 258
apatel336 0:1496281373a5 259
apatel336 0:1496281373a5 260 /*--------------------------- rt_sys_init -----------------------------------*/
apatel336 0:1496281373a5 261
apatel336 0:1496281373a5 262 #ifdef __CMSIS_RTOS
apatel336 0:1496281373a5 263 void rt_sys_init (void) {
apatel336 0:1496281373a5 264 #else
apatel336 0:1496281373a5 265 void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) {
apatel336 0:1496281373a5 266 #endif
apatel336 0:1496281373a5 267 /* Initialize system and start up task declared with "first_task". */
apatel336 0:1496281373a5 268 U32 i;
apatel336 0:1496281373a5 269
apatel336 0:1496281373a5 270 DBG_INIT();
apatel336 0:1496281373a5 271
apatel336 0:1496281373a5 272 /* Initialize dynamic memory and task TCB pointers to NULL. */
apatel336 0:1496281373a5 273 for (i = 0; i < os_maxtaskrun; i++) {
apatel336 0:1496281373a5 274 os_active_TCB[i] = NULL;
apatel336 0:1496281373a5 275 }
apatel336 0:1496281373a5 276
apatel336 0:1496281373a5 277 /* Set up TCB of idle demon */
apatel336 0:1496281373a5 278 os_idle_TCB.task_id = 255;
apatel336 0:1496281373a5 279 os_idle_TCB.priv_stack = idle_task_stack_size;
apatel336 0:1496281373a5 280 os_idle_TCB.stack = idle_task_stack;
apatel336 0:1496281373a5 281 rt_init_context (&os_idle_TCB, 0, os_idle_demon);
apatel336 0:1496281373a5 282
apatel336 0:1496281373a5 283 /* Set up ready list: initially empty */
apatel336 0:1496281373a5 284 os_rdy.cb_type = HCB;
apatel336 0:1496281373a5 285 os_rdy.p_lnk = NULL;
apatel336 0:1496281373a5 286 /* Set up delay list: initially empty */
apatel336 0:1496281373a5 287 os_dly.cb_type = HCB;
apatel336 0:1496281373a5 288 os_dly.p_dlnk = NULL;
apatel336 0:1496281373a5 289 os_dly.p_blnk = NULL;
apatel336 0:1496281373a5 290 os_dly.delta_time = 0;
apatel336 0:1496281373a5 291
apatel336 0:1496281373a5 292 /* Fix SP and systemvariables to assume idle task is running */
apatel336 0:1496281373a5 293 /* Transform main program into idle task by assuming idle TCB */
apatel336 0:1496281373a5 294 #ifndef __CMSIS_RTOS
apatel336 0:1496281373a5 295 rt_set_PSP (os_idle_TCB.tsk_stack+32);
apatel336 0:1496281373a5 296 #endif
apatel336 0:1496281373a5 297 os_tsk.run = &os_idle_TCB;
apatel336 0:1496281373a5 298 os_tsk.run->state = RUNNING;
apatel336 0:1496281373a5 299
apatel336 0:1496281373a5 300 /* Initialize ps queue */
apatel336 0:1496281373a5 301 os_psq->first = 0;
apatel336 0:1496281373a5 302 os_psq->last = 0;
apatel336 0:1496281373a5 303 os_psq->size = os_fifo_size;
apatel336 0:1496281373a5 304
apatel336 0:1496281373a5 305 rt_init_robin ();
apatel336 0:1496281373a5 306
apatel336 0:1496281373a5 307 /* Intitialize SVC and PendSV */
apatel336 0:1496281373a5 308 rt_svc_init ();
apatel336 0:1496281373a5 309
apatel336 0:1496281373a5 310 #ifndef __CMSIS_RTOS
apatel336 0:1496281373a5 311 /* Intitialize and start system clock timer */
apatel336 0:1496281373a5 312 os_tick_irqn = os_tick_init ();
apatel336 0:1496281373a5 313 if (os_tick_irqn >= 0) {
apatel336 0:1496281373a5 314 OS_X_INIT(os_tick_irqn);
apatel336 0:1496281373a5 315 }
apatel336 0:1496281373a5 316
apatel336 0:1496281373a5 317 /* Start up first user task before entering the endless loop */
apatel336 0:1496281373a5 318 rt_tsk_create (first_task, prio_stksz, stk, NULL);
apatel336 0:1496281373a5 319 #endif
apatel336 0:1496281373a5 320 }
apatel336 0:1496281373a5 321
apatel336 0:1496281373a5 322
apatel336 0:1496281373a5 323 /*--------------------------- rt_sys_start ----------------------------------*/
apatel336 0:1496281373a5 324
apatel336 0:1496281373a5 325 #ifdef __CMSIS_RTOS
apatel336 0:1496281373a5 326 void rt_sys_start (void) {
apatel336 0:1496281373a5 327 /* Start system */
apatel336 0:1496281373a5 328
apatel336 0:1496281373a5 329 /* Intitialize and start system clock timer */
apatel336 0:1496281373a5 330 os_tick_irqn = os_tick_init ();
apatel336 0:1496281373a5 331 if (os_tick_irqn >= 0) {
apatel336 0:1496281373a5 332 OS_X_INIT(os_tick_irqn);
apatel336 0:1496281373a5 333 }
apatel336 0:1496281373a5 334 }
apatel336 0:1496281373a5 335 #endif
apatel336 0:1496281373a5 336
apatel336 0:1496281373a5 337 /*----------------------------------------------------------------------------
apatel336 0:1496281373a5 338 * end of file
apatel336 0:1496281373a5 339 *---------------------------------------------------------------------------*/