Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rt_Event.c Source File

rt_Event.c

Go to the documentation of this file.
00001 /**
00002  * @file    rt_Event.c
00003  * @brief   
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "rt_TypeDef.h"
00023 #include "RTX_Config.h"
00024 #include "rt_System.h"
00025 #include "rt_Event.h"
00026 #include "rt_List.h"
00027 #include "rt_Task.h"
00028 
00029 
00030 /*----------------------------------------------------------------------------
00031  *      Functions
00032  *---------------------------------------------------------------------------*/
00033 
00034 
00035 /*--------------------------- rt_evt_wait -----------------------------------*/
00036 
00037 OS_RESULT rt_evt_wait (U16 wait_flags, U16 timeout, BOOL and_wait) {
00038   /* Wait for one or more event flags with optional time-out.                */
00039   /* "wait_flags" identifies the flags to wait for.                          */
00040   /* "timeout" is the time-out limit in system ticks (0xffff if no time-out) */
00041   /* "and_wait" specifies the AND-ing of "wait_flags" as condition to be met */
00042   /* to complete the wait. (OR-ing if set to 0).                             */
00043   U32 block_state;
00044 
00045   if (and_wait) {
00046     /* Check for AND-connected events */
00047     if ((os_tsk.run->events & wait_flags) == wait_flags) {
00048       os_tsk.run->events &= ~wait_flags;
00049       return (OS_R_EVT);
00050     }
00051     block_state = WAIT_AND;
00052   }
00053   else {
00054     /* Check for OR-connected events */
00055     if (os_tsk.run->events & wait_flags) {
00056       os_tsk.run->waits = os_tsk.run->events & wait_flags;
00057       os_tsk.run->events &= ~wait_flags;
00058       return (OS_R_EVT);
00059     }
00060     block_state = WAIT_OR;
00061   }
00062   /* Task has to wait */
00063   os_tsk.run->waits = wait_flags;
00064   rt_block (timeout, (U8)block_state);
00065   return (OS_R_TMO);
00066 }
00067 
00068 
00069 /*--------------------------- rt_evt_set ------------------------------------*/
00070 
00071 void rt_evt_set (U16 event_flags, OS_TID task_id) {
00072   /* Set one or more event flags of a selectable task. */
00073   P_TCB p_tcb;
00074 
00075   p_tcb = os_active_TCB[task_id-1];
00076   if (p_tcb == NULL) {
00077     return;
00078   }
00079   p_tcb->events |= event_flags;
00080   event_flags    = p_tcb->waits;
00081   /* If the task is not waiting for an event, it should not be put */
00082   /* to ready state. */
00083   if (p_tcb->state == WAIT_AND) {
00084     /* Check for AND-connected events */
00085     if ((p_tcb->events & event_flags) == event_flags) {
00086       goto wkup;
00087     }
00088   }
00089   if (p_tcb->state == WAIT_OR) {
00090     /* Check for OR-connected events */
00091     if (p_tcb->events & event_flags) {
00092       p_tcb->waits  &= p_tcb->events;
00093 wkup: p_tcb->events &= ~event_flags;
00094       rt_rmv_dly (p_tcb);
00095       p_tcb->events &= ~event_flags;
00096       p_tcb->state   = READY;
00097       p_tcb->ret_val = OS_R_EVT;
00098       rt_dispatch (p_tcb);
00099     }
00100   }
00101 }
00102 
00103 
00104 /*--------------------------- rt_evt_clr ------------------------------------*/
00105 
00106 void rt_evt_clr (U16 clear_flags, OS_TID task_id) {
00107   /* Clear one or more event flags (identified by "clear_flags") of a */
00108   /* selectable task (identified by "task"). */
00109   P_TCB task = os_active_TCB[task_id-1];
00110 
00111   if (task == NULL) {
00112     return;
00113   }
00114   task->events &= ~clear_flags;
00115 }
00116 
00117 
00118 /*--------------------------- isr_evt_set -----------------------------------*/
00119 
00120 void isr_evt_set (U16 event_flags, OS_TID task_id) {
00121   /* Same function as "os_evt_set", but to be called by ISRs. */
00122   P_TCB p_tcb = os_active_TCB[task_id-1];
00123 
00124   if (p_tcb == NULL) {
00125     return;
00126   }
00127   rt_psq_enq (p_tcb, event_flags);
00128   rt_psh_req ();
00129 }
00130 
00131 
00132 /*--------------------------- rt_evt_get ------------------------------------*/
00133 
00134 U16 rt_evt_get (void) {
00135   /* Get events of a running task after waiting for OR connected events. */
00136   return (os_tsk.run->waits);
00137 }
00138 
00139 
00140 /*--------------------------- rt_evt_psh ------------------------------------*/
00141 
00142 void rt_evt_psh (P_TCB p_CB, U16 set_flags) {
00143   /* Check if task has to be waken up */
00144   U16 event_flags;
00145 
00146   p_CB->events |= set_flags;
00147   event_flags = p_CB->waits;
00148   if (p_CB->state == WAIT_AND) {
00149     /* Check for AND-connected events */
00150     if ((p_CB->events & event_flags) == event_flags) {
00151       goto rdy;
00152     }
00153   }
00154   if (p_CB->state == WAIT_OR) {
00155     /* Check for OR-connected events */
00156     if (p_CB->events & event_flags) {
00157       p_CB->waits  &= p_CB->events;
00158 rdy:  p_CB->events &= ~event_flags;
00159       rt_rmv_dly (p_CB);
00160       p_CB->state   = READY;
00161       p_CB->ret_val = OS_R_EVT;
00162       rt_put_prio (&os_rdy, p_CB);
00163     }
00164   }
00165 }
00166 
00167 /*----------------------------------------------------------------------------
00168  * end of file
00169  *---------------------------------------------------------------------------*/