event_groups.c 34 KB

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