Control a robot over the internet using UDP and a Wifly module (WiFi).

Dependencies:   Motor TextLCD WiflyInterface mbed-rtos mbed

Committer:
apatel336
Date:
Thu Oct 17 13:27:56 2013 +0000
Revision:
0:c0dc3a76f3d4
Initial Release

Who changed what in which revision?

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