event_groups.c 33 KB

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