event_groups.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. #ifndef ESP_PLATFORM
  186. BaseType_t xAlreadyYielded;
  187. #endif // ESP_PLATFORM
  188. BaseType_t xTimeoutOccurred = pdFALSE;
  189. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  190. configASSERT( uxBitsToWaitFor != 0 );
  191. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  192. {
  193. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  194. }
  195. #endif
  196. #ifdef ESP_PLATFORM // IDF-3755
  197. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  198. #else
  199. vTaskSuspendAll();
  200. #endif // ESP_PLATFORM
  201. {
  202. uxOriginalBitValue = pxEventBits->uxEventBits;
  203. ( void ) xEventGroupSetBits( xEventGroup, uxBitsToSet );
  204. if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )
  205. {
  206. /* All the rendezvous bits are now set - no need to block. */
  207. uxReturn = ( uxOriginalBitValue | uxBitsToSet );
  208. /* Rendezvous always clear the bits. They will have been cleared
  209. * already unless this is the only task in the rendezvous. */
  210. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  211. xTicksToWait = 0;
  212. }
  213. else
  214. {
  215. if( xTicksToWait != ( TickType_t ) 0 )
  216. {
  217. traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );
  218. /* Store the bits that the calling task is waiting for in the
  219. * task's event list item so the kernel knows when a match is
  220. * found. Then enter the blocked state. */
  221. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait );
  222. /* This assignment is obsolete as uxReturn will get set after
  223. * the task unblocks, but some compilers mistakenly generate a
  224. * warning about uxReturn being returned without being set if the
  225. * assignment is omitted. */
  226. uxReturn = 0;
  227. }
  228. else
  229. {
  230. /* The rendezvous bits were not set, but no block time was
  231. * specified - just return the current event bit value. */
  232. uxReturn = pxEventBits->uxEventBits;
  233. xTimeoutOccurred = pdTRUE;
  234. }
  235. }
  236. }
  237. #ifdef ESP_PLATFORM // IDF-3755
  238. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  239. #else
  240. xAlreadyYielded = xTaskResumeAll();
  241. #endif // ESP_PLATFORM
  242. if( xTicksToWait != ( TickType_t ) 0 )
  243. {
  244. #ifdef ESP_PLATFORM
  245. portYIELD_WITHIN_API();
  246. #else
  247. if( xAlreadyYielded == pdFALSE )
  248. {
  249. portYIELD_WITHIN_API();
  250. }
  251. else
  252. {
  253. mtCOVERAGE_TEST_MARKER();
  254. }
  255. #endif // ESP_PLATFORM
  256. /* The task blocked to wait for its required bits to be set - at this
  257. * point either the required bits were set or the block time expired. If
  258. * the required bits were set they will have been stored in the task's
  259. * event list item, and they should now be retrieved then cleared. */
  260. uxReturn = uxTaskResetEventItemValue();
  261. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  262. {
  263. /* The task timed out, just return the current event bit value. */
  264. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  265. {
  266. uxReturn = pxEventBits->uxEventBits;
  267. /* Although the task got here because it timed out before the
  268. * bits it was waiting for were set, it is possible that since it
  269. * unblocked another task has set the bits. If this is the case
  270. * then it needs to clear the bits before exiting. */
  271. if( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor )
  272. {
  273. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  274. }
  275. else
  276. {
  277. mtCOVERAGE_TEST_MARKER();
  278. }
  279. }
  280. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  281. xTimeoutOccurred = pdTRUE;
  282. }
  283. else
  284. {
  285. /* The task unblocked because the bits were set. */
  286. }
  287. /* Control bits might be set as the task had blocked should not be
  288. * returned. */
  289. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  290. }
  291. traceEVENT_GROUP_SYNC_END( xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTimeoutOccurred );
  292. /* Prevent compiler warnings when trace macros are not used. */
  293. ( void ) xTimeoutOccurred;
  294. return uxReturn;
  295. }
  296. /*-----------------------------------------------------------*/
  297. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  298. const EventBits_t uxBitsToWaitFor,
  299. const BaseType_t xClearOnExit,
  300. const BaseType_t xWaitForAllBits,
  301. TickType_t xTicksToWait )
  302. {
  303. EventGroup_t * pxEventBits = xEventGroup;
  304. EventBits_t uxReturn, uxControlBits = 0;
  305. #ifdef ESP_PLATFORM
  306. BaseType_t xWaitConditionMet;
  307. #else
  308. BaseType_t xWaitConditionMet, xAlreadyYielded;
  309. #endif // ESP_PLATFORM
  310. BaseType_t xTimeoutOccurred = pdFALSE;
  311. /* Check the user is not attempting to wait on the bits used by the kernel
  312. * itself, and that at least one bit is being requested. */
  313. configASSERT( xEventGroup );
  314. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  315. configASSERT( uxBitsToWaitFor != 0 );
  316. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  317. {
  318. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  319. }
  320. #endif
  321. #ifdef ESP_PLATFORM // IDF-3755
  322. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  323. #else
  324. vTaskSuspendAll();
  325. #endif // ESP_PLATFORM
  326. {
  327. const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;
  328. /* Check to see if the wait condition is already met or not. */
  329. xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits );
  330. if( xWaitConditionMet != pdFALSE )
  331. {
  332. /* The wait condition has already been met so there is no need to
  333. * block. */
  334. uxReturn = uxCurrentEventBits;
  335. xTicksToWait = ( TickType_t ) 0;
  336. /* Clear the wait bits if requested to do so. */
  337. if( xClearOnExit != pdFALSE )
  338. {
  339. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  340. }
  341. else
  342. {
  343. mtCOVERAGE_TEST_MARKER();
  344. }
  345. }
  346. else if( xTicksToWait == ( TickType_t ) 0 )
  347. {
  348. /* The wait condition has not been met, but no block time was
  349. * specified, so just return the current value. */
  350. uxReturn = uxCurrentEventBits;
  351. xTimeoutOccurred = pdTRUE;
  352. }
  353. else
  354. {
  355. /* The task is going to block to wait for its required bits to be
  356. * set. uxControlBits are used to remember the specified behaviour of
  357. * this call to xEventGroupWaitBits() - for use when the event bits
  358. * unblock the task. */
  359. if( xClearOnExit != pdFALSE )
  360. {
  361. uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT;
  362. }
  363. else
  364. {
  365. mtCOVERAGE_TEST_MARKER();
  366. }
  367. if( xWaitForAllBits != pdFALSE )
  368. {
  369. uxControlBits |= eventWAIT_FOR_ALL_BITS;
  370. }
  371. else
  372. {
  373. mtCOVERAGE_TEST_MARKER();
  374. }
  375. /* Store the bits that the calling task is waiting for in the
  376. * task's event list item so the kernel knows when a match is
  377. * found. Then enter the blocked state. */
  378. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait );
  379. /* This is obsolete as it will get set after the task unblocks, but
  380. * some compilers mistakenly generate a warning about the variable
  381. * being returned without being set if it is not done. */
  382. uxReturn = 0;
  383. traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );
  384. }
  385. }
  386. #ifdef ESP_PLATFORM // IDF-3755
  387. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  388. #else
  389. xAlreadyYielded = xTaskResumeAll();
  390. #endif // ESP_PLATFORM
  391. if( xTicksToWait != ( TickType_t ) 0 )
  392. {
  393. #ifdef ESP_PLATFORM
  394. portYIELD_WITHIN_API();
  395. #else
  396. if( xAlreadyYielded == pdFALSE )
  397. {
  398. portYIELD_WITHIN_API();
  399. }
  400. else
  401. {
  402. mtCOVERAGE_TEST_MARKER();
  403. }
  404. #endif // ESP_PLATFORM
  405. /* The task blocked to wait for its required bits to be set - at this
  406. * point either the required bits were set or the block time expired. If
  407. * the required bits were set they will have been stored in the task's
  408. * event list item, and they should now be retrieved then cleared. */
  409. uxReturn = uxTaskResetEventItemValue();
  410. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  411. {
  412. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  413. {
  414. /* The task timed out, just return the current event bit value. */
  415. uxReturn = pxEventBits->uxEventBits;
  416. /* It is possible that the event bits were updated between this
  417. * task leaving the Blocked state and running again. */
  418. if( prvTestWaitCondition( uxReturn, uxBitsToWaitFor, xWaitForAllBits ) != pdFALSE )
  419. {
  420. if( xClearOnExit != pdFALSE )
  421. {
  422. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  423. }
  424. else
  425. {
  426. mtCOVERAGE_TEST_MARKER();
  427. }
  428. }
  429. else
  430. {
  431. mtCOVERAGE_TEST_MARKER();
  432. }
  433. xTimeoutOccurred = pdTRUE;
  434. }
  435. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  436. }
  437. else
  438. {
  439. /* The task unblocked because the bits were set. */
  440. }
  441. /* The task blocked so control bits may have been set. */
  442. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  443. }
  444. traceEVENT_GROUP_WAIT_BITS_END( xEventGroup, uxBitsToWaitFor, xTimeoutOccurred );
  445. /* Prevent compiler warnings when trace macros are not used. */
  446. ( void ) xTimeoutOccurred;
  447. return uxReturn;
  448. }
  449. /*-----------------------------------------------------------*/
  450. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup,
  451. const EventBits_t uxBitsToClear )
  452. {
  453. EventGroup_t * pxEventBits = xEventGroup;
  454. EventBits_t uxReturn;
  455. /* Check the user is not attempting to clear the bits used by the kernel
  456. * itself. */
  457. configASSERT( xEventGroup );
  458. configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  459. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  460. {
  461. traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );
  462. /* The value returned is the event group value prior to the bits being
  463. * cleared. */
  464. uxReturn = pxEventBits->uxEventBits;
  465. /* Clear the bits. */
  466. pxEventBits->uxEventBits &= ~uxBitsToClear;
  467. }
  468. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  469. return uxReturn;
  470. }
  471. /*-----------------------------------------------------------*/
  472. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  473. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup,
  474. const EventBits_t uxBitsToClear )
  475. {
  476. BaseType_t xReturn;
  477. traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear );
  478. 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. */
  479. return xReturn;
  480. }
  481. #endif /* if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) ) */
  482. /*-----------------------------------------------------------*/
  483. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
  484. {
  485. UBaseType_t uxSavedInterruptStatus;
  486. EventGroup_t const * const pxEventBits = xEventGroup;
  487. EventBits_t uxReturn;
  488. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  489. {
  490. uxReturn = pxEventBits->uxEventBits;
  491. }
  492. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  493. return uxReturn;
  494. } /*lint !e818 EventGroupHandle_t is a typedef used in other functions to so can't be pointer to const. */
  495. /*-----------------------------------------------------------*/
  496. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
  497. const EventBits_t uxBitsToSet )
  498. {
  499. ListItem_t * pxListItem, * pxNext;
  500. ListItem_t const * pxListEnd;
  501. List_t const * pxList;
  502. EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;
  503. EventGroup_t * pxEventBits = xEventGroup;
  504. BaseType_t xMatchFound = pdFALSE;
  505. /* Check the user is not attempting to set the bits used by the kernel
  506. * itself. */
  507. configASSERT( xEventGroup );
  508. configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  509. pxList = &( pxEventBits->xTasksWaitingForBits );
  510. 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. */
  511. #ifdef ESP_PLATFORM // IDF-3755
  512. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  513. /* We are about to traverse a task list which is a kernel data structure.
  514. * Thus we need to call vTaskTakeKernelLock() to take the kernel lock. */
  515. vTaskTakeKernelLock();
  516. #else
  517. vTaskSuspendAll();
  518. #endif // ESP_PLATFORM
  519. {
  520. traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );
  521. pxListItem = listGET_HEAD_ENTRY( pxList );
  522. /* Set the bits. */
  523. pxEventBits->uxEventBits |= uxBitsToSet;
  524. /* See if the new bit value should unblock any tasks. */
  525. while( pxListItem != pxListEnd )
  526. {
  527. pxNext = listGET_NEXT( pxListItem );
  528. uxBitsWaitedFor = listGET_LIST_ITEM_VALUE( pxListItem );
  529. xMatchFound = pdFALSE;
  530. /* Split the bits waited for from the control bits. */
  531. uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;
  532. uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;
  533. if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( EventBits_t ) 0 )
  534. {
  535. /* Just looking for single bit being set. */
  536. if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( EventBits_t ) 0 )
  537. {
  538. xMatchFound = pdTRUE;
  539. }
  540. else
  541. {
  542. mtCOVERAGE_TEST_MARKER();
  543. }
  544. }
  545. else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )
  546. {
  547. /* All bits are set. */
  548. xMatchFound = pdTRUE;
  549. }
  550. else
  551. {
  552. /* Need all bits to be set, but not all the bits were set. */
  553. }
  554. if( xMatchFound != pdFALSE )
  555. {
  556. /* The bits match. Should the bits be cleared on exit? */
  557. if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( EventBits_t ) 0 )
  558. {
  559. uxBitsToClear |= uxBitsWaitedFor;
  560. }
  561. else
  562. {
  563. mtCOVERAGE_TEST_MARKER();
  564. }
  565. /* Store the actual event flag value in the task's event list
  566. * item before removing the task from the event list. The
  567. * eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows
  568. * that is was unblocked due to its required bits matching, rather
  569. * than because it timed out. */
  570. vTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET );
  571. }
  572. /* Move onto the next list item. Note pxListItem->pxNext is not
  573. * used here as the list item may have been removed from the event list
  574. * and inserted into the ready/pending reading list. */
  575. pxListItem = pxNext;
  576. }
  577. /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT
  578. * bit was set in the control word. */
  579. pxEventBits->uxEventBits &= ~uxBitsToClear;
  580. }
  581. #ifdef ESP_PLATFORM // IDF-3755
  582. /* Release the previously taken kernel lock, then release the event group spinlock. */
  583. vTaskReleaseKernelLock();
  584. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  585. #else
  586. ( void ) xTaskResumeAll();
  587. #endif // ESP_PLATFORM
  588. return pxEventBits->uxEventBits;
  589. }
  590. /*-----------------------------------------------------------*/
  591. void vEventGroupDelete( EventGroupHandle_t xEventGroup )
  592. {
  593. EventGroup_t * pxEventBits = xEventGroup;
  594. const List_t * pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );
  595. traceEVENT_GROUP_DELETE( xEventGroup );
  596. // IDF-3755
  597. taskENTER_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  598. #ifdef ESP_PLATFORM
  599. /* We are about to traverse a task list which is a kernel data structure.
  600. * Thus we need to call vTaskTakeKernelLock() to take the kernel lock. */
  601. vTaskTakeKernelLock();
  602. #endif
  603. {
  604. while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )
  605. {
  606. /* Unblock the task, returning 0 as the event list is being deleted
  607. * and cannot therefore have any bits set. */
  608. configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( const ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );
  609. vTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );
  610. }
  611. }
  612. #ifdef ESP_PLATFORM
  613. /* Release the previously taken kernel lock. */
  614. vTaskReleaseKernelLock();
  615. #endif
  616. taskEXIT_CRITICAL( &( pxEventBits->xEventGroupLock ) );
  617. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
  618. {
  619. /* The event group can only have been allocated dynamically - free
  620. * it again. */
  621. vPortFree( pxEventBits );
  622. }
  623. #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  624. {
  625. /* The event group could have been allocated statically or
  626. * dynamically, so check before attempting to free the memory. */
  627. if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
  628. {
  629. vPortFree( pxEventBits );
  630. }
  631. else
  632. {
  633. mtCOVERAGE_TEST_MARKER();
  634. }
  635. }
  636. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  637. }
  638. /*-----------------------------------------------------------*/
  639. /* For internal use only - execute a 'set bits' command that was pended from
  640. * an interrupt. */
  641. void vEventGroupSetBitsCallback( void * pvEventGroup,
  642. const uint32_t ulBitsToSet )
  643. {
  644. ( 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. */
  645. }
  646. /*-----------------------------------------------------------*/
  647. /* For internal use only - execute a 'clear bits' command that was pended from
  648. * an interrupt. */
  649. void vEventGroupClearBitsCallback( void * pvEventGroup,
  650. const uint32_t ulBitsToClear )
  651. {
  652. ( 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. */
  653. }
  654. /*-----------------------------------------------------------*/
  655. static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits,
  656. const EventBits_t uxBitsToWaitFor,
  657. const BaseType_t xWaitForAllBits )
  658. {
  659. BaseType_t xWaitConditionMet = pdFALSE;
  660. if( xWaitForAllBits == pdFALSE )
  661. {
  662. /* Task only has to wait for one bit within uxBitsToWaitFor to be
  663. * set. Is one already set? */
  664. if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( EventBits_t ) 0 )
  665. {
  666. xWaitConditionMet = pdTRUE;
  667. }
  668. else
  669. {
  670. mtCOVERAGE_TEST_MARKER();
  671. }
  672. }
  673. else
  674. {
  675. /* Task has to wait for all the bits in uxBitsToWaitFor to be set.
  676. * Are they set already? */
  677. if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )
  678. {
  679. xWaitConditionMet = pdTRUE;
  680. }
  681. else
  682. {
  683. mtCOVERAGE_TEST_MARKER();
  684. }
  685. }
  686. return xWaitConditionMet;
  687. }
  688. /*-----------------------------------------------------------*/
  689. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  690. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup,
  691. const EventBits_t uxBitsToSet,
  692. BaseType_t * pxHigherPriorityTaskWoken )
  693. {
  694. BaseType_t xReturn;
  695. traceEVENT_GROUP_SET_BITS_FROM_ISR( xEventGroup, uxBitsToSet );
  696. 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. */
  697. return xReturn;
  698. }
  699. #endif /* if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) ) */
  700. /*-----------------------------------------------------------*/
  701. #if ( configUSE_TRACE_FACILITY == 1 )
  702. UBaseType_t uxEventGroupGetNumber( void * xEventGroup )
  703. {
  704. UBaseType_t xReturn;
  705. 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. */
  706. if( xEventGroup == NULL )
  707. {
  708. xReturn = 0;
  709. }
  710. else
  711. {
  712. xReturn = pxEventBits->uxEventGroupNumber;
  713. }
  714. return xReturn;
  715. }
  716. #endif /* configUSE_TRACE_FACILITY */
  717. /*-----------------------------------------------------------*/
  718. #if ( configUSE_TRACE_FACILITY == 1 )
  719. void vEventGroupSetNumber( void * xEventGroup,
  720. UBaseType_t uxEventGroupNumber )
  721. {
  722. ( ( 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. */
  723. }
  724. #endif /* configUSE_TRACE_FACILITY */
  725. /*-----------------------------------------------------------*/