event_groups.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * FreeRTOS Kernel V10.5.1 (ESP-IDF SMP modified)
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-FileCopyrightText: 2021 Amazon.com, Inc. or its affiliates
  6. *
  7. * SPDX-License-Identifier: MIT
  8. *
  9. * SPDX-FileContributor: 2023 Espressif Systems (Shanghai) CO LTD
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  12. * this software and associated documentation files (the "Software"), to deal in
  13. * the Software without restriction, including without limitation the rights to
  14. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  15. * the Software, and to permit persons to whom the Software is furnished to do so,
  16. * subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  23. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  25. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. * https://www.FreeRTOS.org
  29. * https://github.com/FreeRTOS
  30. *
  31. */
  32. /* Standard includes. */
  33. #include <stdlib.h>
  34. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  35. * all the API functions to use the MPU wrappers. That should only be done when
  36. * task.h is included from an application file. */
  37. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  38. /* FreeRTOS includes. */
  39. #include "FreeRTOS.h"
  40. #include "task.h"
  41. #include "timers.h"
  42. #include "event_groups.h"
  43. /* Include private IDF API additions for critical thread safety macros */
  44. #include "esp_private/freertos_idf_additions_priv.h"
  45. /* Lint e961, e750 and e9021 are suppressed as a MISRA exception justified
  46. * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
  47. * for the header files above, but not in this file, in order to generate the
  48. * correct privileged Vs unprivileged linkage and placement. */
  49. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021 See comment above. */
  50. /* The following bit fields convey control information in a task's event list
  51. * item value. It is important they don't clash with the
  52. * taskEVENT_LIST_ITEM_VALUE_IN_USE definition. */
  53. #if configUSE_16_BIT_TICKS == 1
  54. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x0100U
  55. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x0200U
  56. #define eventWAIT_FOR_ALL_BITS 0x0400U
  57. #define eventEVENT_BITS_CONTROL_BYTES 0xff00U
  58. #else
  59. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x01000000UL
  60. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x02000000UL
  61. #define eventWAIT_FOR_ALL_BITS 0x04000000UL
  62. #define eventEVENT_BITS_CONTROL_BYTES 0xff000000UL
  63. #endif
  64. typedef struct EventGroupDef_t
  65. {
  66. EventBits_t uxEventBits;
  67. List_t xTasksWaitingForBits; /*< List of tasks waiting for a bit to be set. */
  68. #if ( configUSE_TRACE_FACILITY == 1 )
  69. UBaseType_t uxEventGroupNumber;
  70. #endif
  71. #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  72. uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the event group is statically allocated to ensure no attempt is made to free the memory. */
  73. #endif
  74. portMUX_TYPE xEventGroupLock; /* Spinlock required for SMP critical sections */
  75. } EventGroup_t;
  76. /*-----------------------------------------------------------*/
  77. /*
  78. * Test the bits set in uxCurrentEventBits to see if the wait condition is met.
  79. * The wait condition is defined by xWaitForAllBits. If xWaitForAllBits is
  80. * pdTRUE then the wait condition is met if all the bits set in uxBitsToWaitFor
  81. * are also set in uxCurrentEventBits. If xWaitForAllBits is pdFALSE then the
  82. * wait condition is met if any of the bits set in uxBitsToWait for are also set
  83. * in uxCurrentEventBits.
  84. */
  85. static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits,
  86. const EventBits_t uxBitsToWaitFor,
  87. const BaseType_t xWaitForAllBits ) PRIVILEGED_FUNCTION;
  88. /*-----------------------------------------------------------*/
  89. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  90. EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t * pxEventGroupBuffer )
  91. {
  92. EventGroup_t * pxEventBits;
  93. /* A StaticEventGroup_t object must be provided. */
  94. configASSERT( pxEventGroupBuffer );
  95. #if ( configASSERT_DEFINED == 1 )
  96. {
  97. /* Sanity check that the size of the structure used to declare a
  98. * variable of type StaticEventGroup_t equals the size of the real
  99. * event group structure. */
  100. volatile size_t xSize = sizeof( StaticEventGroup_t );
  101. configASSERT( xSize == sizeof( EventGroup_t ) );
  102. } /*lint !e529 xSize is referenced if configASSERT() is defined. */
  103. #endif /* configASSERT_DEFINED */
  104. /* The user has provided a statically allocated event group - use it. */
  105. pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 !e9087 EventGroup_t and StaticEventGroup_t are deliberately aliased for data hiding purposes and guaranteed to have the same size and alignment requirement - checked by configASSERT(). */
  106. if( pxEventBits != NULL )
  107. {
  108. pxEventBits->uxEventBits = 0;
  109. vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );
  110. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  111. {
  112. /* Both static and dynamic allocation can be used, so note that
  113. * this event group was created statically in case the event group
  114. * is later deleted. */
  115. pxEventBits->ucStaticallyAllocated = pdTRUE;
  116. }
  117. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  118. /* Initialize the event group's spinlock. */
  119. portMUX_INITIALIZE( &pxEventBits->xEventGroupLock );
  120. traceEVENT_GROUP_CREATE( pxEventBits );
  121. }
  122. else
  123. {
  124. /* xEventGroupCreateStatic should only ever be called with
  125. * pxEventGroupBuffer pointing to a pre-allocated (compile time
  126. * allocated) StaticEventGroup_t variable. */
  127. traceEVENT_GROUP_CREATE_FAILED();
  128. }
  129. return pxEventBits;
  130. }
  131. #endif /* configSUPPORT_STATIC_ALLOCATION */
  132. /*-----------------------------------------------------------*/
  133. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  134. EventGroupHandle_t xEventGroupCreate( void )
  135. {
  136. EventGroup_t * pxEventBits;
  137. /* Allocate the event group. Justification for MISRA deviation as
  138. * follows: pvPortMalloc() always ensures returned memory blocks are
  139. * aligned per the requirements of the MCU stack. In this case
  140. * pvPortMalloc() must return a pointer that is guaranteed to meet the
  141. * alignment requirements of the EventGroup_t structure - which (if you
  142. * follow it through) is the alignment requirements of the TickType_t type
  143. * (EventBits_t being of TickType_t itself). Therefore, whenever the
  144. * stack alignment requirements are greater than or equal to the
  145. * TickType_t alignment requirements the cast is safe. In other cases,
  146. * where the natural word size of the architecture is less than
  147. * sizeof( TickType_t ), the TickType_t variables will be accessed in two
  148. * or more reads operations, and the alignment requirements is only that
  149. * of each individual read. */
  150. pxEventBits = ( EventGroup_t * ) pvPortMalloc( sizeof( EventGroup_t ) ); /*lint !e9087 !e9079 see comment above. */
  151. if( pxEventBits != NULL )
  152. {
  153. pxEventBits->uxEventBits = 0;
  154. vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );
  155. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  156. {
  157. /* Both static and dynamic allocation can be used, so note this
  158. * event group was allocated statically in case the event group is
  159. * later deleted. */
  160. pxEventBits->ucStaticallyAllocated = pdFALSE;
  161. }
  162. #endif /* configSUPPORT_STATIC_ALLOCATION */
  163. /* Initialize the event group's spinlock. */
  164. portMUX_INITIALIZE( &pxEventBits->xEventGroupLock );
  165. traceEVENT_GROUP_CREATE( pxEventBits );
  166. }
  167. else
  168. {
  169. traceEVENT_GROUP_CREATE_FAILED(); /*lint !e9063 Else branch only exists to allow tracing and does not generate code if trace macros are not defined. */
  170. }
  171. return pxEventBits;
  172. }
  173. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  174. /*-----------------------------------------------------------*/
  175. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
  176. const EventBits_t uxBitsToSet,
  177. const EventBits_t uxBitsToWaitFor,
  178. TickType_t xTicksToWait )
  179. {
  180. EventBits_t uxOriginalBitValue, uxReturn;
  181. EventGroup_t * pxEventBits = xEventGroup;
  182. BaseType_t xAlreadyYielded;
  183. BaseType_t xTimeoutOccurred = pdFALSE;
  184. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  185. configASSERT( uxBitsToWaitFor != 0 );
  186. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  187. {
  188. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  189. }
  190. #endif
  191. prvENTER_CRITICAL_OR_SUSPEND_ALL( &( pxEventBits->xEventGroupLock ) );
  192. {
  193. uxOriginalBitValue = pxEventBits->uxEventBits;
  194. ( void ) xEventGroupSetBits( xEventGroup, uxBitsToSet );
  195. if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )
  196. {
  197. /* All the rendezvous bits are now set - no need to block. */
  198. uxReturn = ( uxOriginalBitValue | uxBitsToSet );
  199. /* Rendezvous always clear the bits. They will have been cleared
  200. * already unless this is the only task in the rendezvous. */
  201. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  202. xTicksToWait = 0;
  203. }
  204. else
  205. {
  206. if( xTicksToWait != ( TickType_t ) 0 )
  207. {
  208. traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );
  209. /* Store the bits that the calling task is waiting for in the
  210. * task's event list item so the kernel knows when a match is
  211. * found. Then enter the blocked state. */
  212. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait );
  213. /* This assignment is obsolete as uxReturn will get set after
  214. * the task unblocks, but some compilers mistakenly generate a
  215. * warning about uxReturn being returned without being set if the
  216. * assignment is omitted. */
  217. uxReturn = 0;
  218. }
  219. else
  220. {
  221. /* The rendezvous bits were not set, but no block time was
  222. * specified - just return the current event bit value. */
  223. uxReturn = pxEventBits->uxEventBits;
  224. xTimeoutOccurred = pdTRUE;
  225. }
  226. }
  227. }
  228. xAlreadyYielded = prvEXIT_CRITICAL_OR_RESUME_ALL( &( pxEventBits->xEventGroupLock ) );
  229. if( xTicksToWait != ( TickType_t ) 0 )
  230. {
  231. if( xAlreadyYielded == pdFALSE )
  232. {
  233. portYIELD_WITHIN_API();
  234. }
  235. else
  236. {
  237. mtCOVERAGE_TEST_MARKER();
  238. }
  239. /* The task blocked to wait for its required bits to be set - at this
  240. * point either the required bits were set or the block time expired. If
  241. * the required bits were set they will have been stored in the task's
  242. * event list item, and they should now be retrieved then cleared. */
  243. uxReturn = uxTaskResetEventItemValue();
  244. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  245. {
  246. /* The task timed out, just return the current event bit value. */
  247. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  248. {
  249. uxReturn = pxEventBits->uxEventBits;
  250. /* Although the task got here because it timed out before the
  251. * bits it was waiting for were set, it is possible that since it
  252. * unblocked another task has set the bits. If this is the case
  253. * then it needs to clear the bits before exiting. */
  254. if( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor )
  255. {
  256. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  257. }
  258. else
  259. {
  260. mtCOVERAGE_TEST_MARKER();
  261. }
  262. }
  263. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  264. xTimeoutOccurred = pdTRUE;
  265. }
  266. else
  267. {
  268. /* The task unblocked because the bits were set. */
  269. }
  270. /* Control bits might be set as the task had blocked should not be
  271. * returned. */
  272. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  273. }
  274. traceEVENT_GROUP_SYNC_END( xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTimeoutOccurred );
  275. /* Prevent compiler warnings when trace macros are not used. */
  276. ( void ) xTimeoutOccurred;
  277. return uxReturn;
  278. }
  279. /*-----------------------------------------------------------*/
  280. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  281. const EventBits_t uxBitsToWaitFor,
  282. const BaseType_t xClearOnExit,
  283. const BaseType_t xWaitForAllBits,
  284. TickType_t xTicksToWait )
  285. {
  286. EventGroup_t * pxEventBits = xEventGroup;
  287. EventBits_t uxReturn, uxControlBits = 0;
  288. BaseType_t xWaitConditionMet, xAlreadyYielded;
  289. BaseType_t xTimeoutOccurred = pdFALSE;
  290. /* Check the user is not attempting to wait on the bits used by the kernel
  291. * itself, and that at least one bit is being requested. */
  292. configASSERT( xEventGroup );
  293. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  294. configASSERT( uxBitsToWaitFor != 0 );
  295. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  296. {
  297. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  298. }
  299. #endif
  300. prvENTER_CRITICAL_OR_SUSPEND_ALL( &( pxEventBits->xEventGroupLock ) );
  301. {
  302. const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;
  303. /* Check to see if the wait condition is already met or not. */
  304. xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits );
  305. if( xWaitConditionMet != pdFALSE )
  306. {
  307. /* The wait condition has already been met so there is no need to
  308. * block. */
  309. uxReturn = uxCurrentEventBits;
  310. xTicksToWait = ( TickType_t ) 0;
  311. /* Clear the wait bits if requested to do so. */
  312. if( xClearOnExit != pdFALSE )
  313. {
  314. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  315. }
  316. else
  317. {
  318. mtCOVERAGE_TEST_MARKER();
  319. }
  320. }
  321. else if( xTicksToWait == ( TickType_t ) 0 )
  322. {
  323. /* The wait condition has not been met, but no block time was
  324. * specified, so just return the current value. */
  325. uxReturn = uxCurrentEventBits;
  326. xTimeoutOccurred = pdTRUE;
  327. }
  328. else
  329. {
  330. /* The task is going to block to wait for its required bits to be
  331. * set. uxControlBits are used to remember the specified behaviour of
  332. * this call to xEventGroupWaitBits() - for use when the event bits
  333. * unblock the task. */
  334. if( xClearOnExit != pdFALSE )
  335. {
  336. uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT;
  337. }
  338. else
  339. {
  340. mtCOVERAGE_TEST_MARKER();
  341. }
  342. if( xWaitForAllBits != pdFALSE )
  343. {
  344. uxControlBits |= eventWAIT_FOR_ALL_BITS;
  345. }
  346. else
  347. {
  348. mtCOVERAGE_TEST_MARKER();
  349. }
  350. /* Store the bits that the calling task is waiting for in the
  351. * task's event list item so the kernel knows when a match is
  352. * found. Then enter the blocked state. */
  353. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait );
  354. /* This is obsolete as it will get set after the task unblocks, but
  355. * some compilers mistakenly generate a warning about the variable
  356. * being returned without being set if it is not done. */
  357. uxReturn = 0;
  358. traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );
  359. }
  360. }
  361. xAlreadyYielded = prvEXIT_CRITICAL_OR_RESUME_ALL( &( pxEventBits->xEventGroupLock ) );
  362. if( xTicksToWait != ( TickType_t ) 0 )
  363. {
  364. if( xAlreadyYielded == pdFALSE )
  365. {
  366. portYIELD_WITHIN_API();
  367. }
  368. else
  369. {
  370. mtCOVERAGE_TEST_MARKER();
  371. }
  372. /* The task blocked to wait for its required bits to be set - at this
  373. * point either the required bits were set or the block time expired. If
  374. * the required bits were set they will have been stored in the task's
  375. * event list item, and they should now be retrieved then cleared. */
  376. uxReturn = uxTaskResetEventItemValue();
  377. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  378. {
  379. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  380. {
  381. /* The task timed out, just return the current event bit value. */
  382. uxReturn = pxEventBits->uxEventBits;
  383. /* It is possible that the event bits were updated between this
  384. * task leaving the Blocked state and running again. */
  385. if( prvTestWaitCondition( uxReturn, uxBitsToWaitFor, xWaitForAllBits ) != pdFALSE )
  386. {
  387. if( xClearOnExit != pdFALSE )
  388. {
  389. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  390. }
  391. else
  392. {
  393. mtCOVERAGE_TEST_MARKER();
  394. }
  395. }
  396. else
  397. {
  398. mtCOVERAGE_TEST_MARKER();
  399. }
  400. xTimeoutOccurred = pdTRUE;
  401. }
  402. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  403. }
  404. else
  405. {
  406. /* The task unblocked because the bits were set. */
  407. }
  408. /* The task blocked so control bits may have been set. */
  409. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  410. }
  411. traceEVENT_GROUP_WAIT_BITS_END( xEventGroup, uxBitsToWaitFor, xTimeoutOccurred );
  412. /* Prevent compiler warnings when trace macros are not used. */
  413. ( void ) xTimeoutOccurred;
  414. return uxReturn;
  415. }
  416. /*-----------------------------------------------------------*/
  417. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup,
  418. const EventBits_t uxBitsToClear )
  419. {
  420. EventGroup_t * pxEventBits = xEventGroup;
  421. EventBits_t uxReturn;
  422. /* Check the user is not attempting to clear the bits used by the kernel
  423. * itself. */
  424. configASSERT( xEventGroup );
  425. configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  426. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  427. {
  428. traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );
  429. /* The value returned is the event group value prior to the bits being
  430. * cleared. */
  431. uxReturn = pxEventBits->uxEventBits;
  432. /* Clear the bits. */
  433. pxEventBits->uxEventBits &= ~uxBitsToClear;
  434. }
  435. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  436. return uxReturn;
  437. }
  438. /*-----------------------------------------------------------*/
  439. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  440. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup,
  441. const EventBits_t uxBitsToClear )
  442. {
  443. BaseType_t xReturn;
  444. traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear );
  445. xReturn = xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL ); /*lint !e9087 Can't avoid cast to void* as a generic callback function not specific to this use case. Callback casts back to original type so safe. */
  446. return xReturn;
  447. }
  448. #endif /* if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) ) */
  449. /*-----------------------------------------------------------*/
  450. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
  451. {
  452. UBaseType_t uxSavedInterruptStatus;
  453. EventGroup_t const * const pxEventBits = xEventGroup;
  454. EventBits_t uxReturn;
  455. prvENTER_CRITICAL_OR_MASK_ISR( ( portMUX_TYPE * ) &( pxEventBits->xEventGroupLock ), uxSavedInterruptStatus );
  456. {
  457. uxReturn = pxEventBits->uxEventBits;
  458. }
  459. prvEXIT_CRITICAL_OR_UNMASK_ISR( ( portMUX_TYPE * ) &( pxEventBits->xEventGroupLock ), uxSavedInterruptStatus );
  460. return uxReturn;
  461. } /*lint !e818 EventGroupHandle_t is a typedef used in other functions to so can't be pointer to const. */
  462. /*-----------------------------------------------------------*/
  463. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
  464. const EventBits_t uxBitsToSet )
  465. {
  466. ListItem_t * pxListItem;
  467. ListItem_t * pxNext;
  468. ListItem_t const * pxListEnd;
  469. List_t const * pxList;
  470. EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;
  471. EventGroup_t * pxEventBits = xEventGroup;
  472. BaseType_t xMatchFound = pdFALSE;
  473. /* Check the user is not attempting to set the bits used by the kernel
  474. * itself. */
  475. configASSERT( xEventGroup );
  476. configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  477. pxList = &( pxEventBits->xTasksWaitingForBits );
  478. pxListEnd = listGET_END_MARKER( pxList ); /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. */
  479. prvENTER_CRITICAL_OR_SUSPEND_ALL( &( pxEventBits->xEventGroupLock ) );
  480. #if ( configNUMBER_OF_CORES > 1 )
  481. /* We are about to traverse a task list which is a kernel data structure.
  482. * Thus we need to call prvTakeKernelLock() to take the kernel lock. */
  483. prvTakeKernelLock();
  484. #endif /* configNUMBER_OF_CORES > 1 */
  485. {
  486. traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );
  487. pxListItem = listGET_HEAD_ENTRY( pxList );
  488. /* Set the bits. */
  489. pxEventBits->uxEventBits |= uxBitsToSet;
  490. /* See if the new bit value should unblock any tasks. */
  491. while( pxListItem != pxListEnd )
  492. {
  493. pxNext = listGET_NEXT( pxListItem );
  494. uxBitsWaitedFor = listGET_LIST_ITEM_VALUE( pxListItem );
  495. xMatchFound = pdFALSE;
  496. /* Split the bits waited for from the control bits. */
  497. uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;
  498. uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;
  499. if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( EventBits_t ) 0 )
  500. {
  501. /* Just looking for single bit being set. */
  502. if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( EventBits_t ) 0 )
  503. {
  504. xMatchFound = pdTRUE;
  505. }
  506. else
  507. {
  508. mtCOVERAGE_TEST_MARKER();
  509. }
  510. }
  511. else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )
  512. {
  513. /* All bits are set. */
  514. xMatchFound = pdTRUE;
  515. }
  516. else
  517. {
  518. /* Need all bits to be set, but not all the bits were set. */
  519. }
  520. if( xMatchFound != pdFALSE )
  521. {
  522. /* The bits match. Should the bits be cleared on exit? */
  523. if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( EventBits_t ) 0 )
  524. {
  525. uxBitsToClear |= uxBitsWaitedFor;
  526. }
  527. else
  528. {
  529. mtCOVERAGE_TEST_MARKER();
  530. }
  531. /* Store the actual event flag value in the task's event list
  532. * item before removing the task from the event list. The
  533. * eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows
  534. * that is was unblocked due to its required bits matching, rather
  535. * than because it timed out. */
  536. vTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET );
  537. }
  538. /* Move onto the next list item. Note pxListItem->pxNext is not
  539. * used here as the list item may have been removed from the event list
  540. * and inserted into the ready/pending reading list. */
  541. pxListItem = pxNext;
  542. }
  543. /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT
  544. * bit was set in the control word. */
  545. pxEventBits->uxEventBits &= ~uxBitsToClear;
  546. }
  547. #if ( configNUMBER_OF_CORES > 1 )
  548. /* Release the previously taken kernel lock. */
  549. prvReleaseKernelLock();
  550. #endif /* configNUMBER_OF_CORES > 1 */
  551. ( void ) prvEXIT_CRITICAL_OR_RESUME_ALL( &( pxEventBits->xEventGroupLock ) );
  552. return pxEventBits->uxEventBits;
  553. }
  554. /*-----------------------------------------------------------*/
  555. void vEventGroupDelete( EventGroupHandle_t xEventGroup )
  556. {
  557. EventGroup_t * pxEventBits = xEventGroup;
  558. const List_t * pxTasksWaitingForBits;
  559. configASSERT( pxEventBits );
  560. pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );
  561. prvENTER_CRITICAL_OR_SUSPEND_ALL( &( pxEventBits->xEventGroupLock ) );
  562. #if ( configNUMBER_OF_CORES > 1 )
  563. /* We are about to traverse a task list which is a kernel data structure.
  564. * Thus we need to call prvTakeKernelLock() to take the kernel lock. */
  565. prvTakeKernelLock();
  566. #endif /* configNUMBER_OF_CORES > 1 */
  567. {
  568. traceEVENT_GROUP_DELETE( xEventGroup );
  569. while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )
  570. {
  571. /* Unblock the task, returning 0 as the event list is being deleted
  572. * and cannot therefore have any bits set. */
  573. configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( const ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );
  574. vTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );
  575. }
  576. }
  577. #if ( configNUMBER_OF_CORES > 1 )
  578. /* Release the previously taken kernel lock. */
  579. prvReleaseKernelLock();
  580. #endif /* configNUMBER_OF_CORES > 1 */
  581. prvEXIT_CRITICAL_OR_RESUME_ALL( &( pxEventBits->xEventGroupLock ) );
  582. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
  583. {
  584. /* The event group can only have been allocated dynamically - free
  585. * it again. */
  586. vPortFree( pxEventBits );
  587. }
  588. #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  589. {
  590. /* The event group could have been allocated statically or
  591. * dynamically, so check before attempting to free the memory. */
  592. if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
  593. {
  594. vPortFree( pxEventBits );
  595. }
  596. else
  597. {
  598. mtCOVERAGE_TEST_MARKER();
  599. }
  600. }
  601. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  602. }
  603. /*-----------------------------------------------------------*/
  604. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  605. BaseType_t xEventGroupGetStaticBuffer( EventGroupHandle_t xEventGroup,
  606. StaticEventGroup_t ** ppxEventGroupBuffer )
  607. {
  608. BaseType_t xReturn;
  609. EventGroup_t * pxEventBits = xEventGroup;
  610. configASSERT( pxEventBits );
  611. configASSERT( ppxEventGroupBuffer );
  612. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  613. {
  614. /* Check if the event group was statically allocated. */
  615. if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdTRUE )
  616. {
  617. *ppxEventGroupBuffer = ( StaticEventGroup_t * ) pxEventBits;
  618. xReturn = pdTRUE;
  619. }
  620. else
  621. {
  622. xReturn = pdFALSE;
  623. }
  624. }
  625. #else /* configSUPPORT_DYNAMIC_ALLOCATION */
  626. {
  627. /* Event group must have been statically allocated. */
  628. *ppxEventGroupBuffer = ( StaticEventGroup_t * ) pxEventBits;
  629. xReturn = pdTRUE;
  630. }
  631. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  632. return xReturn;
  633. }
  634. #endif /* configSUPPORT_STATIC_ALLOCATION */
  635. /*-----------------------------------------------------------*/
  636. /* For internal use only - execute a 'set bits' command that was pended from
  637. * an interrupt. */
  638. void vEventGroupSetBitsCallback( void * pvEventGroup,
  639. const uint32_t ulBitsToSet )
  640. {
  641. ( void ) xEventGroupSetBits( pvEventGroup, ( EventBits_t ) ulBitsToSet ); /*lint !e9079 Can't avoid cast to void* as a generic timer callback prototype. Callback casts back to original type so safe. */
  642. }
  643. /*-----------------------------------------------------------*/
  644. /* For internal use only - execute a 'clear bits' command that was pended from
  645. * an interrupt. */
  646. void vEventGroupClearBitsCallback( void * pvEventGroup,
  647. const uint32_t ulBitsToClear )
  648. {
  649. ( void ) xEventGroupClearBits( pvEventGroup, ( EventBits_t ) ulBitsToClear ); /*lint !e9079 Can't avoid cast to void* as a generic timer callback prototype. Callback casts back to original type so safe. */
  650. }
  651. /*-----------------------------------------------------------*/
  652. static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits,
  653. const EventBits_t uxBitsToWaitFor,
  654. const BaseType_t xWaitForAllBits )
  655. {
  656. BaseType_t xWaitConditionMet = pdFALSE;
  657. if( xWaitForAllBits == pdFALSE )
  658. {
  659. /* Task only has to wait for one bit within uxBitsToWaitFor to be
  660. * set. Is one already set? */
  661. if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( EventBits_t ) 0 )
  662. {
  663. xWaitConditionMet = pdTRUE;
  664. }
  665. else
  666. {
  667. mtCOVERAGE_TEST_MARKER();
  668. }
  669. }
  670. else
  671. {
  672. /* Task has to wait for all the bits in uxBitsToWaitFor to be set.
  673. * Are they set already? */
  674. if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )
  675. {
  676. xWaitConditionMet = pdTRUE;
  677. }
  678. else
  679. {
  680. mtCOVERAGE_TEST_MARKER();
  681. }
  682. }
  683. return xWaitConditionMet;
  684. }
  685. /*-----------------------------------------------------------*/
  686. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  687. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup,
  688. const EventBits_t uxBitsToSet,
  689. BaseType_t * pxHigherPriorityTaskWoken )
  690. {
  691. BaseType_t xReturn;
  692. traceEVENT_GROUP_SET_BITS_FROM_ISR( xEventGroup, uxBitsToSet );
  693. xReturn = xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken ); /*lint !e9087 Can't avoid cast to void* as a generic callback function not specific to this use case. Callback casts back to original type so safe. */
  694. return xReturn;
  695. }
  696. #endif /* if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) ) */
  697. /*-----------------------------------------------------------*/
  698. #if ( configUSE_TRACE_FACILITY == 1 )
  699. UBaseType_t uxEventGroupGetNumber( void * xEventGroup )
  700. {
  701. UBaseType_t xReturn;
  702. EventGroup_t const * pxEventBits = ( EventGroup_t * ) xEventGroup; /*lint !e9087 !e9079 EventGroupHandle_t is a pointer to an EventGroup_t, but EventGroupHandle_t is kept opaque outside of this file for data hiding purposes. */
  703. if( xEventGroup == NULL )
  704. {
  705. xReturn = 0;
  706. }
  707. else
  708. {
  709. xReturn = pxEventBits->uxEventGroupNumber;
  710. }
  711. return xReturn;
  712. }
  713. #endif /* configUSE_TRACE_FACILITY */
  714. /*-----------------------------------------------------------*/
  715. #if ( configUSE_TRACE_FACILITY == 1 )
  716. void vEventGroupSetNumber( void * xEventGroup,
  717. UBaseType_t uxEventGroupNumber )
  718. {
  719. ( ( EventGroup_t * ) xEventGroup )->uxEventGroupNumber = uxEventGroupNumber; /*lint !e9087 !e9079 EventGroupHandle_t is a pointer to an EventGroup_t, but EventGroupHandle_t is kept opaque outside of this file for data hiding purposes. */
  720. }
  721. #endif /* configUSE_TRACE_FACILITY */
  722. /*-----------------------------------------------------------*/