event_groups.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /* Standard includes. */
  29. #include <stdlib.h>
  30. /* FreeRTOS includes. */
  31. #include "FreeRTOS.h"
  32. #include "task.h"
  33. #include "event_groups.h"
  34. typedef struct EventGroupDef_t
  35. {
  36. struct rt_event event;
  37. } EventGroup_t;
  38. static volatile rt_uint8_t event_index = 0;
  39. /*-----------------------------------------------------------*/
  40. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  41. EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t * pxEventGroupBuffer )
  42. {
  43. char name[RT_NAME_MAX] = {0};
  44. /* A StaticEventGroup_t object must be provided. */
  45. configASSERT( pxEventGroupBuffer );
  46. rt_snprintf( name, RT_NAME_MAX, "event%02d", event_index++ );
  47. rt_event_init( ( rt_event_t ) pxEventGroupBuffer, name, RT_IPC_FLAG_PRIO );
  48. return ( EventGroupHandle_t ) pxEventGroupBuffer;
  49. }
  50. #endif /* configSUPPORT_STATIC_ALLOCATION */
  51. /*-----------------------------------------------------------*/
  52. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  53. EventGroupHandle_t xEventGroupCreate( void )
  54. {
  55. EventGroup_t * pxEventBits;
  56. char name[RT_NAME_MAX] = {0};
  57. rt_snprintf( name, RT_NAME_MAX, "event%02d", event_index++ );
  58. pxEventBits = ( EventGroup_t * ) rt_event_create( name, RT_IPC_FLAG_PRIO );
  59. return pxEventBits;
  60. }
  61. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  62. /*-----------------------------------------------------------*/
  63. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  64. const EventBits_t uxBitsToWaitFor,
  65. const BaseType_t xClearOnExit,
  66. const BaseType_t xWaitForAllBits,
  67. TickType_t xTicksToWait )
  68. {
  69. rt_event_t event = ( rt_event_t ) xEventGroup;
  70. rt_uint8_t option = 0;
  71. rt_uint32_t recved;
  72. rt_base_t level;
  73. rt_err_t err;
  74. /* Check the user is not attempting to wait on the bits used by the kernel
  75. * itself, and that at least one bit is being requested. */
  76. configASSERT( xEventGroup );
  77. configASSERT( uxBitsToWaitFor != 0 );
  78. #if ( INCLUDE_xTaskGetSchedulerState == 1 )
  79. {
  80. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  81. }
  82. #endif
  83. if ( xWaitForAllBits != pdFALSE )
  84. {
  85. option |= RT_EVENT_FLAG_AND;
  86. }
  87. else
  88. {
  89. option |= RT_EVENT_FLAG_OR;
  90. }
  91. if ( xClearOnExit != pdFALSE )
  92. {
  93. option |= RT_EVENT_FLAG_CLEAR;
  94. }
  95. err = rt_event_recv( event, ( rt_uint32_t ) uxBitsToWaitFor, option, ( rt_int32_t ) xTicksToWait, &recved );
  96. if ( err != RT_EOK )
  97. {
  98. level = rt_hw_interrupt_disable();
  99. recved = event->set;
  100. rt_hw_interrupt_enable(level);
  101. }
  102. return ( EventBits_t ) recved;
  103. }
  104. /*-----------------------------------------------------------*/
  105. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup,
  106. const EventBits_t uxBitsToClear )
  107. {
  108. rt_event_t event = ( rt_event_t ) xEventGroup;
  109. EventBits_t uxReturn;
  110. rt_base_t level;
  111. configASSERT( xEventGroup );
  112. level = rt_hw_interrupt_disable();
  113. uxReturn = ( EventBits_t ) event->set;
  114. event->set &= ~( ( rt_uint32_t ) uxBitsToClear );
  115. rt_hw_interrupt_enable( level );
  116. return uxReturn;
  117. }
  118. /*-----------------------------------------------------------*/
  119. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup,
  120. const EventBits_t uxBitsToClear )
  121. {
  122. return xEventGroupClearBits( xEventGroup, uxBitsToClear );
  123. }
  124. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
  125. {
  126. rt_event_t event = ( rt_event_t ) xEventGroup;
  127. EventBits_t uxReturn;
  128. rt_base_t level;
  129. level = rt_hw_interrupt_disable();
  130. uxReturn = ( EventBits_t ) event->set;
  131. rt_hw_interrupt_enable( level );
  132. return uxReturn;
  133. }
  134. /*-----------------------------------------------------------*/
  135. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
  136. const EventBits_t uxBitsToSet )
  137. {
  138. rt_event_t event = ( rt_event_t ) xEventGroup;
  139. rt_base_t level;
  140. EventBits_t uxReturn;
  141. configASSERT( xEventGroup );
  142. rt_event_send( event, ( rt_uint32_t ) uxBitsToSet);
  143. level = rt_hw_interrupt_disable();
  144. uxReturn = ( EventBits_t ) event->set;
  145. rt_hw_interrupt_enable(level);
  146. return uxReturn;
  147. }
  148. /*-----------------------------------------------------------*/
  149. void vEventGroupDelete( EventGroupHandle_t xEventGroup )
  150. {
  151. rt_event_t event = ( rt_event_t ) xEventGroup;
  152. configASSERT( xEventGroup );
  153. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  154. if ( rt_object_is_systemobject( ( rt_object_t ) event ) )
  155. #endif
  156. {
  157. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  158. rt_event_detach( event );
  159. #endif
  160. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  161. }
  162. else
  163. {
  164. #endif
  165. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  166. rt_event_delete( event );
  167. #endif
  168. }
  169. }
  170. /*-----------------------------------------------------------*/
  171. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  172. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup,
  173. const EventBits_t uxBitsToSet,
  174. BaseType_t * pxHigherPriorityTaskWoken )
  175. {
  176. xEventGroupSetBits( xEventGroup, uxBitsToSet );
  177. if ( pxHigherPriorityTaskWoken != NULL)
  178. {
  179. pxHigherPriorityTaskWoken = pdFALSE;
  180. }
  181. return pdPASS;
  182. }
  183. #endif /* if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) ) */
  184. /*-----------------------------------------------------------*/