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