event_groups.c 33 KB

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