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_EVENT.C
apatel336 0:c0dc3a76f3d4 5 * Purpose: Implements waits and wake-ups for event flags
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_System.h"
apatel336 0:c0dc3a76f3d4 38 #include "rt_Event.h"
apatel336 0:c0dc3a76f3d4 39 #include "rt_List.h"
apatel336 0:c0dc3a76f3d4 40 #include "rt_Task.h"
apatel336 0:c0dc3a76f3d4 41 #include "rt_HAL_CM.h"
apatel336 0:c0dc3a76f3d4 42
apatel336 0:c0dc3a76f3d4 43
apatel336 0:c0dc3a76f3d4 44 /*----------------------------------------------------------------------------
apatel336 0:c0dc3a76f3d4 45 * Functions
apatel336 0:c0dc3a76f3d4 46 *---------------------------------------------------------------------------*/
apatel336 0:c0dc3a76f3d4 47
apatel336 0:c0dc3a76f3d4 48
apatel336 0:c0dc3a76f3d4 49 /*--------------------------- rt_evt_wait -----------------------------------*/
apatel336 0:c0dc3a76f3d4 50
apatel336 0:c0dc3a76f3d4 51 OS_RESULT rt_evt_wait (U16 wait_flags, U16 timeout, BOOL and_wait) {
apatel336 0:c0dc3a76f3d4 52 /* Wait for one or more event flags with optional time-out. */
apatel336 0:c0dc3a76f3d4 53 /* "wait_flags" identifies the flags to wait for. */
apatel336 0:c0dc3a76f3d4 54 /* "timeout" is the time-out limit in system ticks (0xffff if no time-out) */
apatel336 0:c0dc3a76f3d4 55 /* "and_wait" specifies the AND-ing of "wait_flags" as condition to be met */
apatel336 0:c0dc3a76f3d4 56 /* to complete the wait. (OR-ing if set to 0). */
apatel336 0:c0dc3a76f3d4 57 U32 block_state;
apatel336 0:c0dc3a76f3d4 58
apatel336 0:c0dc3a76f3d4 59 if (and_wait) {
apatel336 0:c0dc3a76f3d4 60 /* Check for AND-connected events */
apatel336 0:c0dc3a76f3d4 61 if ((os_tsk.run->events & wait_flags) == wait_flags) {
apatel336 0:c0dc3a76f3d4 62 os_tsk.run->events &= ~wait_flags;
apatel336 0:c0dc3a76f3d4 63 return (OS_R_EVT);
apatel336 0:c0dc3a76f3d4 64 }
apatel336 0:c0dc3a76f3d4 65 block_state = WAIT_AND;
apatel336 0:c0dc3a76f3d4 66 }
apatel336 0:c0dc3a76f3d4 67 else {
apatel336 0:c0dc3a76f3d4 68 /* Check for OR-connected events */
apatel336 0:c0dc3a76f3d4 69 if (os_tsk.run->events & wait_flags) {
apatel336 0:c0dc3a76f3d4 70 os_tsk.run->waits = os_tsk.run->events & wait_flags;
apatel336 0:c0dc3a76f3d4 71 os_tsk.run->events &= ~wait_flags;
apatel336 0:c0dc3a76f3d4 72 return (OS_R_EVT);
apatel336 0:c0dc3a76f3d4 73 }
apatel336 0:c0dc3a76f3d4 74 block_state = WAIT_OR;
apatel336 0:c0dc3a76f3d4 75 }
apatel336 0:c0dc3a76f3d4 76 /* Task has to wait */
apatel336 0:c0dc3a76f3d4 77 os_tsk.run->waits = wait_flags;
apatel336 0:c0dc3a76f3d4 78 rt_block (timeout, (U8)block_state);
apatel336 0:c0dc3a76f3d4 79 return (OS_R_TMO);
apatel336 0:c0dc3a76f3d4 80 }
apatel336 0:c0dc3a76f3d4 81
apatel336 0:c0dc3a76f3d4 82
apatel336 0:c0dc3a76f3d4 83 /*--------------------------- rt_evt_set ------------------------------------*/
apatel336 0:c0dc3a76f3d4 84
apatel336 0:c0dc3a76f3d4 85 void rt_evt_set (U16 event_flags, OS_TID task_id) {
apatel336 0:c0dc3a76f3d4 86 /* Set one or more event flags of a selectable task. */
apatel336 0:c0dc3a76f3d4 87 P_TCB p_tcb;
apatel336 0:c0dc3a76f3d4 88
apatel336 0:c0dc3a76f3d4 89 p_tcb = os_active_TCB[task_id-1];
apatel336 0:c0dc3a76f3d4 90 if (p_tcb == NULL) {
apatel336 0:c0dc3a76f3d4 91 return;
apatel336 0:c0dc3a76f3d4 92 }
apatel336 0:c0dc3a76f3d4 93 p_tcb->events |= event_flags;
apatel336 0:c0dc3a76f3d4 94 event_flags = p_tcb->waits;
apatel336 0:c0dc3a76f3d4 95 /* If the task is not waiting for an event, it should not be put */
apatel336 0:c0dc3a76f3d4 96 /* to ready state. */
apatel336 0:c0dc3a76f3d4 97 if (p_tcb->state == WAIT_AND) {
apatel336 0:c0dc3a76f3d4 98 /* Check for AND-connected events */
apatel336 0:c0dc3a76f3d4 99 if ((p_tcb->events & event_flags) == event_flags) {
apatel336 0:c0dc3a76f3d4 100 goto wkup;
apatel336 0:c0dc3a76f3d4 101 }
apatel336 0:c0dc3a76f3d4 102 }
apatel336 0:c0dc3a76f3d4 103 if (p_tcb->state == WAIT_OR) {
apatel336 0:c0dc3a76f3d4 104 /* Check for OR-connected events */
apatel336 0:c0dc3a76f3d4 105 if (p_tcb->events & event_flags) {
apatel336 0:c0dc3a76f3d4 106 p_tcb->waits &= p_tcb->events;
apatel336 0:c0dc3a76f3d4 107 wkup: p_tcb->events &= ~event_flags;
apatel336 0:c0dc3a76f3d4 108 rt_rmv_dly (p_tcb);
apatel336 0:c0dc3a76f3d4 109 p_tcb->state = READY;
apatel336 0:c0dc3a76f3d4 110 #ifdef __CMSIS_RTOS
apatel336 0:c0dc3a76f3d4 111 rt_ret_val2(p_tcb, 0x08/*osEventSignal*/, p_tcb->waits);
apatel336 0:c0dc3a76f3d4 112 #else
apatel336 0:c0dc3a76f3d4 113 rt_ret_val (p_tcb, OS_R_EVT);
apatel336 0:c0dc3a76f3d4 114 #endif
apatel336 0:c0dc3a76f3d4 115 rt_dispatch (p_tcb);
apatel336 0:c0dc3a76f3d4 116 }
apatel336 0:c0dc3a76f3d4 117 }
apatel336 0:c0dc3a76f3d4 118 }
apatel336 0:c0dc3a76f3d4 119
apatel336 0:c0dc3a76f3d4 120
apatel336 0:c0dc3a76f3d4 121 /*--------------------------- rt_evt_clr ------------------------------------*/
apatel336 0:c0dc3a76f3d4 122
apatel336 0:c0dc3a76f3d4 123 void rt_evt_clr (U16 clear_flags, OS_TID task_id) {
apatel336 0:c0dc3a76f3d4 124 /* Clear one or more event flags (identified by "clear_flags") of a */
apatel336 0:c0dc3a76f3d4 125 /* selectable task (identified by "task"). */
apatel336 0:c0dc3a76f3d4 126 P_TCB task = os_active_TCB[task_id-1];
apatel336 0:c0dc3a76f3d4 127
apatel336 0:c0dc3a76f3d4 128 if (task == NULL) {
apatel336 0:c0dc3a76f3d4 129 return;
apatel336 0:c0dc3a76f3d4 130 }
apatel336 0:c0dc3a76f3d4 131 task->events &= ~clear_flags;
apatel336 0:c0dc3a76f3d4 132 }
apatel336 0:c0dc3a76f3d4 133
apatel336 0:c0dc3a76f3d4 134
apatel336 0:c0dc3a76f3d4 135 /*--------------------------- isr_evt_set -----------------------------------*/
apatel336 0:c0dc3a76f3d4 136
apatel336 0:c0dc3a76f3d4 137 void isr_evt_set (U16 event_flags, OS_TID task_id) {
apatel336 0:c0dc3a76f3d4 138 /* Same function as "os_evt_set", but to be called by ISRs. */
apatel336 0:c0dc3a76f3d4 139 P_TCB p_tcb = os_active_TCB[task_id-1];
apatel336 0:c0dc3a76f3d4 140
apatel336 0:c0dc3a76f3d4 141 if (p_tcb == NULL) {
apatel336 0:c0dc3a76f3d4 142 return;
apatel336 0:c0dc3a76f3d4 143 }
apatel336 0:c0dc3a76f3d4 144 rt_psq_enq (p_tcb, event_flags);
apatel336 0:c0dc3a76f3d4 145 rt_psh_req ();
apatel336 0:c0dc3a76f3d4 146 }
apatel336 0:c0dc3a76f3d4 147
apatel336 0:c0dc3a76f3d4 148
apatel336 0:c0dc3a76f3d4 149 /*--------------------------- rt_evt_get ------------------------------------*/
apatel336 0:c0dc3a76f3d4 150
apatel336 0:c0dc3a76f3d4 151 U16 rt_evt_get (void) {
apatel336 0:c0dc3a76f3d4 152 /* Get events of a running task after waiting for OR connected events. */
apatel336 0:c0dc3a76f3d4 153 return (os_tsk.run->waits);
apatel336 0:c0dc3a76f3d4 154 }
apatel336 0:c0dc3a76f3d4 155
apatel336 0:c0dc3a76f3d4 156
apatel336 0:c0dc3a76f3d4 157 /*--------------------------- rt_evt_psh ------------------------------------*/
apatel336 0:c0dc3a76f3d4 158
apatel336 0:c0dc3a76f3d4 159 void rt_evt_psh (P_TCB p_CB, U16 set_flags) {
apatel336 0:c0dc3a76f3d4 160 /* Check if task has to be waken up */
apatel336 0:c0dc3a76f3d4 161 U16 event_flags;
apatel336 0:c0dc3a76f3d4 162
apatel336 0:c0dc3a76f3d4 163 p_CB->events |= set_flags;
apatel336 0:c0dc3a76f3d4 164 event_flags = p_CB->waits;
apatel336 0:c0dc3a76f3d4 165 if (p_CB->state == WAIT_AND) {
apatel336 0:c0dc3a76f3d4 166 /* Check for AND-connected events */
apatel336 0:c0dc3a76f3d4 167 if ((p_CB->events & event_flags) == event_flags) {
apatel336 0:c0dc3a76f3d4 168 goto rdy;
apatel336 0:c0dc3a76f3d4 169 }
apatel336 0:c0dc3a76f3d4 170 }
apatel336 0:c0dc3a76f3d4 171 if (p_CB->state == WAIT_OR) {
apatel336 0:c0dc3a76f3d4 172 /* Check for OR-connected events */
apatel336 0:c0dc3a76f3d4 173 if (p_CB->events & event_flags) {
apatel336 0:c0dc3a76f3d4 174 p_CB->waits &= p_CB->events;
apatel336 0:c0dc3a76f3d4 175 rdy: p_CB->events &= ~event_flags;
apatel336 0:c0dc3a76f3d4 176 rt_rmv_dly (p_CB);
apatel336 0:c0dc3a76f3d4 177 p_CB->state = READY;
apatel336 0:c0dc3a76f3d4 178 #ifdef __CMSIS_RTOS
apatel336 0:c0dc3a76f3d4 179 rt_ret_val2(p_CB, 0x08/*osEventSignal*/, p_CB->waits);
apatel336 0:c0dc3a76f3d4 180 #else
apatel336 0:c0dc3a76f3d4 181 rt_ret_val (p_CB, OS_R_EVT);
apatel336 0:c0dc3a76f3d4 182 #endif
apatel336 0:c0dc3a76f3d4 183 rt_put_prio (&os_rdy, p_CB);
apatel336 0:c0dc3a76f3d4 184 }
apatel336 0:c0dc3a76f3d4 185 }
apatel336 0:c0dc3a76f3d4 186 }
apatel336 0:c0dc3a76f3d4 187
apatel336 0:c0dc3a76f3d4 188 /*----------------------------------------------------------------------------
apatel336 0:c0dc3a76f3d4 189 * end of file
apatel336 0:c0dc3a76f3d4 190 *---------------------------------------------------------------------------*/