event_groups.c 25 KB

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