event_groups.c 32 KB

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