Lab Checkoff

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
doubster
Date:
Wed Nov 13 20:00:28 2013 +0000
Revision:
0:67dbd54e60d4
Lab Checkoff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
doubster 0:67dbd54e60d4 1 /*----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 2 * RL-ARM - RTX
doubster 0:67dbd54e60d4 3 *----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 4 * Name: RT_EVENT.C
doubster 0:67dbd54e60d4 5 * Purpose: Implements waits and wake-ups for event flags
doubster 0:67dbd54e60d4 6 * Rev.: V4.60
doubster 0:67dbd54e60d4 7 *----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 8 *
doubster 0:67dbd54e60d4 9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
doubster 0:67dbd54e60d4 10 * All rights reserved.
doubster 0:67dbd54e60d4 11 * Redistribution and use in source and binary forms, with or without
doubster 0:67dbd54e60d4 12 * modification, are permitted provided that the following conditions are met:
doubster 0:67dbd54e60d4 13 * - Redistributions of source code must retain the above copyright
doubster 0:67dbd54e60d4 14 * notice, this list of conditions and the following disclaimer.
doubster 0:67dbd54e60d4 15 * - Redistributions in binary form must reproduce the above copyright
doubster 0:67dbd54e60d4 16 * notice, this list of conditions and the following disclaimer in the
doubster 0:67dbd54e60d4 17 * documentation and/or other materials provided with the distribution.
doubster 0:67dbd54e60d4 18 * - Neither the name of ARM nor the names of its contributors may be used
doubster 0:67dbd54e60d4 19 * to endorse or promote products derived from this software without
doubster 0:67dbd54e60d4 20 * specific prior written permission.
doubster 0:67dbd54e60d4 21 *
doubster 0:67dbd54e60d4 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
doubster 0:67dbd54e60d4 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
doubster 0:67dbd54e60d4 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
doubster 0:67dbd54e60d4 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
doubster 0:67dbd54e60d4 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
doubster 0:67dbd54e60d4 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
doubster 0:67dbd54e60d4 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
doubster 0:67dbd54e60d4 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
doubster 0:67dbd54e60d4 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
doubster 0:67dbd54e60d4 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
doubster 0:67dbd54e60d4 32 * POSSIBILITY OF SUCH DAMAGE.
doubster 0:67dbd54e60d4 33 *---------------------------------------------------------------------------*/
doubster 0:67dbd54e60d4 34
doubster 0:67dbd54e60d4 35 #include "rt_TypeDef.h"
doubster 0:67dbd54e60d4 36 #include "RTX_Config.h"
doubster 0:67dbd54e60d4 37 #include "rt_System.h"
doubster 0:67dbd54e60d4 38 #include "rt_Event.h"
doubster 0:67dbd54e60d4 39 #include "rt_List.h"
doubster 0:67dbd54e60d4 40 #include "rt_Task.h"
doubster 0:67dbd54e60d4 41 #include "rt_HAL_CM.h"
doubster 0:67dbd54e60d4 42
doubster 0:67dbd54e60d4 43
doubster 0:67dbd54e60d4 44 /*----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 45 * Functions
doubster 0:67dbd54e60d4 46 *---------------------------------------------------------------------------*/
doubster 0:67dbd54e60d4 47
doubster 0:67dbd54e60d4 48
doubster 0:67dbd54e60d4 49 /*--------------------------- rt_evt_wait -----------------------------------*/
doubster 0:67dbd54e60d4 50
doubster 0:67dbd54e60d4 51 OS_RESULT rt_evt_wait (U16 wait_flags, U16 timeout, BOOL and_wait) {
doubster 0:67dbd54e60d4 52 /* Wait for one or more event flags with optional time-out. */
doubster 0:67dbd54e60d4 53 /* "wait_flags" identifies the flags to wait for. */
doubster 0:67dbd54e60d4 54 /* "timeout" is the time-out limit in system ticks (0xffff if no time-out) */
doubster 0:67dbd54e60d4 55 /* "and_wait" specifies the AND-ing of "wait_flags" as condition to be met */
doubster 0:67dbd54e60d4 56 /* to complete the wait. (OR-ing if set to 0). */
doubster 0:67dbd54e60d4 57 U32 block_state;
doubster 0:67dbd54e60d4 58
doubster 0:67dbd54e60d4 59 if (and_wait) {
doubster 0:67dbd54e60d4 60 /* Check for AND-connected events */
doubster 0:67dbd54e60d4 61 if ((os_tsk.run->events & wait_flags) == wait_flags) {
doubster 0:67dbd54e60d4 62 os_tsk.run->events &= ~wait_flags;
doubster 0:67dbd54e60d4 63 return (OS_R_EVT);
doubster 0:67dbd54e60d4 64 }
doubster 0:67dbd54e60d4 65 block_state = WAIT_AND;
doubster 0:67dbd54e60d4 66 }
doubster 0:67dbd54e60d4 67 else {
doubster 0:67dbd54e60d4 68 /* Check for OR-connected events */
doubster 0:67dbd54e60d4 69 if (os_tsk.run->events & wait_flags) {
doubster 0:67dbd54e60d4 70 os_tsk.run->waits = os_tsk.run->events & wait_flags;
doubster 0:67dbd54e60d4 71 os_tsk.run->events &= ~wait_flags;
doubster 0:67dbd54e60d4 72 return (OS_R_EVT);
doubster 0:67dbd54e60d4 73 }
doubster 0:67dbd54e60d4 74 block_state = WAIT_OR;
doubster 0:67dbd54e60d4 75 }
doubster 0:67dbd54e60d4 76 /* Task has to wait */
doubster 0:67dbd54e60d4 77 os_tsk.run->waits = wait_flags;
doubster 0:67dbd54e60d4 78 rt_block (timeout, (U8)block_state);
doubster 0:67dbd54e60d4 79 return (OS_R_TMO);
doubster 0:67dbd54e60d4 80 }
doubster 0:67dbd54e60d4 81
doubster 0:67dbd54e60d4 82
doubster 0:67dbd54e60d4 83 /*--------------------------- rt_evt_set ------------------------------------*/
doubster 0:67dbd54e60d4 84
doubster 0:67dbd54e60d4 85 void rt_evt_set (U16 event_flags, OS_TID task_id) {
doubster 0:67dbd54e60d4 86 /* Set one or more event flags of a selectable task. */
doubster 0:67dbd54e60d4 87 P_TCB p_tcb;
doubster 0:67dbd54e60d4 88
doubster 0:67dbd54e60d4 89 p_tcb = os_active_TCB[task_id-1];
doubster 0:67dbd54e60d4 90 if (p_tcb == NULL) {
doubster 0:67dbd54e60d4 91 return;
doubster 0:67dbd54e60d4 92 }
doubster 0:67dbd54e60d4 93 p_tcb->events |= event_flags;
doubster 0:67dbd54e60d4 94 event_flags = p_tcb->waits;
doubster 0:67dbd54e60d4 95 /* If the task is not waiting for an event, it should not be put */
doubster 0:67dbd54e60d4 96 /* to ready state. */
doubster 0:67dbd54e60d4 97 if (p_tcb->state == WAIT_AND) {
doubster 0:67dbd54e60d4 98 /* Check for AND-connected events */
doubster 0:67dbd54e60d4 99 if ((p_tcb->events & event_flags) == event_flags) {
doubster 0:67dbd54e60d4 100 goto wkup;
doubster 0:67dbd54e60d4 101 }
doubster 0:67dbd54e60d4 102 }
doubster 0:67dbd54e60d4 103 if (p_tcb->state == WAIT_OR) {
doubster 0:67dbd54e60d4 104 /* Check for OR-connected events */
doubster 0:67dbd54e60d4 105 if (p_tcb->events & event_flags) {
doubster 0:67dbd54e60d4 106 p_tcb->waits &= p_tcb->events;
doubster 0:67dbd54e60d4 107 wkup: p_tcb->events &= ~event_flags;
doubster 0:67dbd54e60d4 108 rt_rmv_dly (p_tcb);
doubster 0:67dbd54e60d4 109 p_tcb->state = READY;
doubster 0:67dbd54e60d4 110 #ifdef __CMSIS_RTOS
doubster 0:67dbd54e60d4 111 rt_ret_val2(p_tcb, 0x08/*osEventSignal*/, p_tcb->waits);
doubster 0:67dbd54e60d4 112 #else
doubster 0:67dbd54e60d4 113 rt_ret_val (p_tcb, OS_R_EVT);
doubster 0:67dbd54e60d4 114 #endif
doubster 0:67dbd54e60d4 115 rt_dispatch (p_tcb);
doubster 0:67dbd54e60d4 116 }
doubster 0:67dbd54e60d4 117 }
doubster 0:67dbd54e60d4 118 }
doubster 0:67dbd54e60d4 119
doubster 0:67dbd54e60d4 120
doubster 0:67dbd54e60d4 121 /*--------------------------- rt_evt_clr ------------------------------------*/
doubster 0:67dbd54e60d4 122
doubster 0:67dbd54e60d4 123 void rt_evt_clr (U16 clear_flags, OS_TID task_id) {
doubster 0:67dbd54e60d4 124 /* Clear one or more event flags (identified by "clear_flags") of a */
doubster 0:67dbd54e60d4 125 /* selectable task (identified by "task"). */
doubster 0:67dbd54e60d4 126 P_TCB task = os_active_TCB[task_id-1];
doubster 0:67dbd54e60d4 127
doubster 0:67dbd54e60d4 128 if (task == NULL) {
doubster 0:67dbd54e60d4 129 return;
doubster 0:67dbd54e60d4 130 }
doubster 0:67dbd54e60d4 131 task->events &= ~clear_flags;
doubster 0:67dbd54e60d4 132 }
doubster 0:67dbd54e60d4 133
doubster 0:67dbd54e60d4 134
doubster 0:67dbd54e60d4 135 /*--------------------------- isr_evt_set -----------------------------------*/
doubster 0:67dbd54e60d4 136
doubster 0:67dbd54e60d4 137 void isr_evt_set (U16 event_flags, OS_TID task_id) {
doubster 0:67dbd54e60d4 138 /* Same function as "os_evt_set", but to be called by ISRs. */
doubster 0:67dbd54e60d4 139 P_TCB p_tcb = os_active_TCB[task_id-1];
doubster 0:67dbd54e60d4 140
doubster 0:67dbd54e60d4 141 if (p_tcb == NULL) {
doubster 0:67dbd54e60d4 142 return;
doubster 0:67dbd54e60d4 143 }
doubster 0:67dbd54e60d4 144 rt_psq_enq (p_tcb, event_flags);
doubster 0:67dbd54e60d4 145 rt_psh_req ();
doubster 0:67dbd54e60d4 146 }
doubster 0:67dbd54e60d4 147
doubster 0:67dbd54e60d4 148
doubster 0:67dbd54e60d4 149 /*--------------------------- rt_evt_get ------------------------------------*/
doubster 0:67dbd54e60d4 150
doubster 0:67dbd54e60d4 151 U16 rt_evt_get (void) {
doubster 0:67dbd54e60d4 152 /* Get events of a running task after waiting for OR connected events. */
doubster 0:67dbd54e60d4 153 return (os_tsk.run->waits);
doubster 0:67dbd54e60d4 154 }
doubster 0:67dbd54e60d4 155
doubster 0:67dbd54e60d4 156
doubster 0:67dbd54e60d4 157 /*--------------------------- rt_evt_psh ------------------------------------*/
doubster 0:67dbd54e60d4 158
doubster 0:67dbd54e60d4 159 void rt_evt_psh (P_TCB p_CB, U16 set_flags) {
doubster 0:67dbd54e60d4 160 /* Check if task has to be waken up */
doubster 0:67dbd54e60d4 161 U16 event_flags;
doubster 0:67dbd54e60d4 162
doubster 0:67dbd54e60d4 163 p_CB->events |= set_flags;
doubster 0:67dbd54e60d4 164 event_flags = p_CB->waits;
doubster 0:67dbd54e60d4 165 if (p_CB->state == WAIT_AND) {
doubster 0:67dbd54e60d4 166 /* Check for AND-connected events */
doubster 0:67dbd54e60d4 167 if ((p_CB->events & event_flags) == event_flags) {
doubster 0:67dbd54e60d4 168 goto rdy;
doubster 0:67dbd54e60d4 169 }
doubster 0:67dbd54e60d4 170 }
doubster 0:67dbd54e60d4 171 if (p_CB->state == WAIT_OR) {
doubster 0:67dbd54e60d4 172 /* Check for OR-connected events */
doubster 0:67dbd54e60d4 173 if (p_CB->events & event_flags) {
doubster 0:67dbd54e60d4 174 p_CB->waits &= p_CB->events;
doubster 0:67dbd54e60d4 175 rdy: p_CB->events &= ~event_flags;
doubster 0:67dbd54e60d4 176 rt_rmv_dly (p_CB);
doubster 0:67dbd54e60d4 177 p_CB->state = READY;
doubster 0:67dbd54e60d4 178 #ifdef __CMSIS_RTOS
doubster 0:67dbd54e60d4 179 rt_ret_val2(p_CB, 0x08/*osEventSignal*/, p_CB->waits);
doubster 0:67dbd54e60d4 180 #else
doubster 0:67dbd54e60d4 181 rt_ret_val (p_CB, OS_R_EVT);
doubster 0:67dbd54e60d4 182 #endif
doubster 0:67dbd54e60d4 183 rt_put_prio (&os_rdy, p_CB);
doubster 0:67dbd54e60d4 184 }
doubster 0:67dbd54e60d4 185 }
doubster 0:67dbd54e60d4 186 }
doubster 0:67dbd54e60d4 187
doubster 0:67dbd54e60d4 188 /*----------------------------------------------------------------------------
doubster 0:67dbd54e60d4 189 * end of file
doubster 0:67dbd54e60d4 190 *---------------------------------------------------------------------------*/
doubster 0:67dbd54e60d4 191