skeleton for lab1

Dependencies:   AvailableMemory mbed-rtos mbed

Fork of helloaabbc by 32314 mbed

Committer:
y7jin
Date:
Thu Apr 03 22:56:32 2014 +0000
Revision:
0:1c8f2727e9f5
hello

Who changed what in which revision?

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