Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rt_Event.c 6.4KB

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