event_groups.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * FreeRTOS Kernel V10.0.1
  3. * Copyright (C) 2017 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 and e750 are suppressed as a MISRA exception justified because the
  39. MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
  40. header files above, but not in this file, in order to generate the correct
  41. privileged Vs unprivileged linkage and placement. */
  42. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
  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 xEventGroupDefinition
  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. }
  93. #endif /* configASSERT_DEFINED */
  94. /* The user has provided a statically allocated event group - use it. */
  95. pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 EventGroup_t and StaticEventGroup_t are 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. traceEVENT_GROUP_CREATE_FAILED();
  113. }
  114. return ( EventGroupHandle_t ) pxEventBits;
  115. }
  116. #endif /* configSUPPORT_STATIC_ALLOCATION */
  117. /*-----------------------------------------------------------*/
  118. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  119. EventGroupHandle_t xEventGroupCreate( void )
  120. {
  121. EventGroup_t *pxEventBits;
  122. /* Allocate the event group. */
  123. pxEventBits = ( EventGroup_t * ) pvPortMalloc( sizeof( EventGroup_t ) );
  124. if( pxEventBits != NULL )
  125. {
  126. pxEventBits->uxEventBits = 0;
  127. vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );
  128. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  129. {
  130. /* Both static and dynamic allocation can be used, so note this
  131. event group was allocated statically in case the event group is
  132. later deleted. */
  133. pxEventBits->ucStaticallyAllocated = pdFALSE;
  134. }
  135. #endif /* configSUPPORT_STATIC_ALLOCATION */
  136. traceEVENT_GROUP_CREATE( pxEventBits );
  137. }
  138. else
  139. {
  140. traceEVENT_GROUP_CREATE_FAILED();
  141. }
  142. return ( EventGroupHandle_t ) pxEventBits;
  143. }
  144. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  145. /*-----------------------------------------------------------*/
  146. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait )
  147. {
  148. EventBits_t uxOriginalBitValue, uxReturn;
  149. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  150. BaseType_t xAlreadyYielded;
  151. BaseType_t xTimeoutOccurred = pdFALSE;
  152. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  153. configASSERT( uxBitsToWaitFor != 0 );
  154. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  155. {
  156. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  157. }
  158. #endif
  159. vTaskSuspendAll();
  160. {
  161. uxOriginalBitValue = pxEventBits->uxEventBits;
  162. ( void ) xEventGroupSetBits( xEventGroup, uxBitsToSet );
  163. if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )
  164. {
  165. /* All the rendezvous bits are now set - no need to block. */
  166. uxReturn = ( uxOriginalBitValue | uxBitsToSet );
  167. /* Rendezvous always clear the bits. They will have been cleared
  168. already unless this is the only task in the rendezvous. */
  169. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  170. xTicksToWait = 0;
  171. }
  172. else
  173. {
  174. if( xTicksToWait != ( TickType_t ) 0 )
  175. {
  176. traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );
  177. /* Store the bits that the calling task is waiting for in the
  178. task's event list item so the kernel knows when a match is
  179. found. Then enter the blocked state. */
  180. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait );
  181. /* This assignment is obsolete as uxReturn will get set after
  182. the task unblocks, but some compilers mistakenly generate a
  183. warning about uxReturn being returned without being set if the
  184. assignment is omitted. */
  185. uxReturn = 0;
  186. }
  187. else
  188. {
  189. /* The rendezvous bits were not set, but no block time was
  190. specified - just return the current event bit value. */
  191. uxReturn = pxEventBits->uxEventBits;
  192. xTimeoutOccurred = pdTRUE;
  193. }
  194. }
  195. }
  196. xAlreadyYielded = xTaskResumeAll();
  197. if( xTicksToWait != ( TickType_t ) 0 )
  198. {
  199. if( xAlreadyYielded == pdFALSE )
  200. {
  201. portYIELD_WITHIN_API();
  202. }
  203. else
  204. {
  205. mtCOVERAGE_TEST_MARKER();
  206. }
  207. /* The task blocked to wait for its required bits to be set - at this
  208. point either the required bits were set or the block time expired. If
  209. the required bits were set they will have been stored in the task's
  210. event list item, and they should now be retrieved then cleared. */
  211. uxReturn = uxTaskResetEventItemValue();
  212. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  213. {
  214. /* The task timed out, just return the current event bit value. */
  215. taskENTER_CRITICAL();
  216. {
  217. uxReturn = pxEventBits->uxEventBits;
  218. /* Although the task got here because it timed out before the
  219. bits it was waiting for were set, it is possible that since it
  220. unblocked another task has set the bits. If this is the case
  221. then it needs to clear the bits before exiting. */
  222. if( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor )
  223. {
  224. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  225. }
  226. else
  227. {
  228. mtCOVERAGE_TEST_MARKER();
  229. }
  230. }
  231. taskEXIT_CRITICAL();
  232. xTimeoutOccurred = pdTRUE;
  233. }
  234. else
  235. {
  236. /* The task unblocked because the bits were set. */
  237. }
  238. /* Control bits might be set as the task had blocked should not be
  239. returned. */
  240. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  241. }
  242. traceEVENT_GROUP_SYNC_END( xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTimeoutOccurred );
  243. /* Prevent compiler warnings when trace macros are not used. */
  244. ( void ) xTimeoutOccurred;
  245. return uxReturn;
  246. }
  247. /*-----------------------------------------------------------*/
  248. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait )
  249. {
  250. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  251. EventBits_t uxReturn, uxControlBits = 0;
  252. BaseType_t xWaitConditionMet, xAlreadyYielded;
  253. BaseType_t xTimeoutOccurred = pdFALSE;
  254. /* Check the user is not attempting to wait on the bits used by the kernel
  255. itself, and that at least one bit is being requested. */
  256. configASSERT( xEventGroup );
  257. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  258. configASSERT( uxBitsToWaitFor != 0 );
  259. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  260. {
  261. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  262. }
  263. #endif
  264. vTaskSuspendAll();
  265. {
  266. const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;
  267. /* Check to see if the wait condition is already met or not. */
  268. xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits );
  269. if( xWaitConditionMet != pdFALSE )
  270. {
  271. /* The wait condition has already been met so there is no need to
  272. block. */
  273. uxReturn = uxCurrentEventBits;
  274. xTicksToWait = ( TickType_t ) 0;
  275. /* Clear the wait bits if requested to do so. */
  276. if( xClearOnExit != pdFALSE )
  277. {
  278. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  279. }
  280. else
  281. {
  282. mtCOVERAGE_TEST_MARKER();
  283. }
  284. }
  285. else if( xTicksToWait == ( TickType_t ) 0 )
  286. {
  287. /* The wait condition has not been met, but no block time was
  288. specified, so just return the current value. */
  289. uxReturn = uxCurrentEventBits;
  290. xTimeoutOccurred = pdTRUE;
  291. }
  292. else
  293. {
  294. /* The task is going to block to wait for its required bits to be
  295. set. uxControlBits are used to remember the specified behaviour of
  296. this call to xEventGroupWaitBits() - for use when the event bits
  297. unblock the task. */
  298. if( xClearOnExit != pdFALSE )
  299. {
  300. uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT;
  301. }
  302. else
  303. {
  304. mtCOVERAGE_TEST_MARKER();
  305. }
  306. if( xWaitForAllBits != pdFALSE )
  307. {
  308. uxControlBits |= eventWAIT_FOR_ALL_BITS;
  309. }
  310. else
  311. {
  312. mtCOVERAGE_TEST_MARKER();
  313. }
  314. /* Store the bits that the calling task is waiting for in the
  315. task's event list item so the kernel knows when a match is
  316. found. Then enter the blocked state. */
  317. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait );
  318. /* This is obsolete as it will get set after the task unblocks, but
  319. some compilers mistakenly generate a warning about the variable
  320. being returned without being set if it is not done. */
  321. uxReturn = 0;
  322. traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );
  323. }
  324. }
  325. xAlreadyYielded = xTaskResumeAll();
  326. if( xTicksToWait != ( TickType_t ) 0 )
  327. {
  328. if( xAlreadyYielded == pdFALSE )
  329. {
  330. portYIELD_WITHIN_API();
  331. }
  332. else
  333. {
  334. mtCOVERAGE_TEST_MARKER();
  335. }
  336. /* The task blocked to wait for its required bits to be set - at this
  337. point either the required bits were set or the block time expired. If
  338. the required bits were set they will have been stored in the task's
  339. event list item, and they should now be retrieved then cleared. */
  340. uxReturn = uxTaskResetEventItemValue();
  341. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  342. {
  343. taskENTER_CRITICAL();
  344. {
  345. /* The task timed out, just return the current event bit value. */
  346. uxReturn = pxEventBits->uxEventBits;
  347. /* It is possible that the event bits were updated between this
  348. task leaving the Blocked state and running again. */
  349. if( prvTestWaitCondition( uxReturn, uxBitsToWaitFor, xWaitForAllBits ) != pdFALSE )
  350. {
  351. if( xClearOnExit != pdFALSE )
  352. {
  353. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  354. }
  355. else
  356. {
  357. mtCOVERAGE_TEST_MARKER();
  358. }
  359. }
  360. else
  361. {
  362. mtCOVERAGE_TEST_MARKER();
  363. }
  364. xTimeoutOccurred = pdTRUE;
  365. }
  366. taskEXIT_CRITICAL();
  367. }
  368. else
  369. {
  370. /* The task unblocked because the bits were set. */
  371. }
  372. /* The task blocked so control bits may have been set. */
  373. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  374. }
  375. traceEVENT_GROUP_WAIT_BITS_END( xEventGroup, uxBitsToWaitFor, xTimeoutOccurred );
  376. /* Prevent compiler warnings when trace macros are not used. */
  377. ( void ) xTimeoutOccurred;
  378. return uxReturn;
  379. }
  380. /*-----------------------------------------------------------*/
  381. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )
  382. {
  383. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  384. EventBits_t uxReturn;
  385. /* Check the user is not attempting to clear the bits used by the kernel
  386. itself. */
  387. configASSERT( xEventGroup );
  388. configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  389. taskENTER_CRITICAL();
  390. {
  391. traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );
  392. /* The value returned is the event group value prior to the bits being
  393. cleared. */
  394. uxReturn = pxEventBits->uxEventBits;
  395. /* Clear the bits. */
  396. pxEventBits->uxEventBits &= ~uxBitsToClear;
  397. }
  398. taskEXIT_CRITICAL();
  399. return uxReturn;
  400. }
  401. /*-----------------------------------------------------------*/
  402. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  403. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )
  404. {
  405. BaseType_t xReturn;
  406. traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear );
  407. xReturn = xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL );
  408. return xReturn;
  409. }
  410. #endif
  411. /*-----------------------------------------------------------*/
  412. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
  413. {
  414. UBaseType_t uxSavedInterruptStatus;
  415. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  416. EventBits_t uxReturn;
  417. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  418. {
  419. uxReturn = pxEventBits->uxEventBits;
  420. }
  421. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  422. return uxReturn;
  423. }
  424. /*-----------------------------------------------------------*/
  425. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet )
  426. {
  427. ListItem_t *pxListItem, *pxNext;
  428. ListItem_t const *pxListEnd;
  429. List_t *pxList;
  430. EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;
  431. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  432. BaseType_t xMatchFound = pdFALSE;
  433. /* Check the user is not attempting to set the bits used by the kernel
  434. itself. */
  435. configASSERT( xEventGroup );
  436. configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  437. pxList = &( pxEventBits->xTasksWaitingForBits );
  438. pxListEnd = listGET_END_MARKER( pxList ); /*lint !e826 !e740 The mini list structure is used as the list end to save RAM. This is checked and valid. */
  439. vTaskSuspendAll();
  440. {
  441. traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );
  442. pxListItem = listGET_HEAD_ENTRY( pxList );
  443. /* Set the bits. */
  444. pxEventBits->uxEventBits |= uxBitsToSet;
  445. /* See if the new bit value should unblock any tasks. */
  446. while( pxListItem != pxListEnd )
  447. {
  448. pxNext = listGET_NEXT( pxListItem );
  449. uxBitsWaitedFor = listGET_LIST_ITEM_VALUE( pxListItem );
  450. xMatchFound = pdFALSE;
  451. /* Split the bits waited for from the control bits. */
  452. uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;
  453. uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;
  454. if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( EventBits_t ) 0 )
  455. {
  456. /* Just looking for single bit being set. */
  457. if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( EventBits_t ) 0 )
  458. {
  459. xMatchFound = pdTRUE;
  460. }
  461. else
  462. {
  463. mtCOVERAGE_TEST_MARKER();
  464. }
  465. }
  466. else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )
  467. {
  468. /* All bits are set. */
  469. xMatchFound = pdTRUE;
  470. }
  471. else
  472. {
  473. /* Need all bits to be set, but not all the bits were set. */
  474. }
  475. if( xMatchFound != pdFALSE )
  476. {
  477. /* The bits match. Should the bits be cleared on exit? */
  478. if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( EventBits_t ) 0 )
  479. {
  480. uxBitsToClear |= uxBitsWaitedFor;
  481. }
  482. else
  483. {
  484. mtCOVERAGE_TEST_MARKER();
  485. }
  486. /* Store the actual event flag value in the task's event list
  487. item before removing the task from the event list. The
  488. eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows
  489. that is was unblocked due to its required bits matching, rather
  490. than because it timed out. */
  491. vTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET );
  492. }
  493. /* Move onto the next list item. Note pxListItem->pxNext is not
  494. used here as the list item may have been removed from the event list
  495. and inserted into the ready/pending reading list. */
  496. pxListItem = pxNext;
  497. }
  498. /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT
  499. bit was set in the control word. */
  500. pxEventBits->uxEventBits &= ~uxBitsToClear;
  501. }
  502. ( void ) xTaskResumeAll();
  503. return pxEventBits->uxEventBits;
  504. }
  505. /*-----------------------------------------------------------*/
  506. void vEventGroupDelete( EventGroupHandle_t xEventGroup )
  507. {
  508. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  509. const List_t *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );
  510. vTaskSuspendAll();
  511. {
  512. traceEVENT_GROUP_DELETE( xEventGroup );
  513. while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )
  514. {
  515. /* Unblock the task, returning 0 as the event list is being deleted
  516. and cannot therefore have any bits set. */
  517. configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( const ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );
  518. vTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );
  519. }
  520. #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
  521. {
  522. /* The event group can only have been allocated dynamically - free
  523. it again. */
  524. vPortFree( pxEventBits );
  525. }
  526. #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  527. {
  528. /* The event group could have been allocated statically or
  529. dynamically, so check before attempting to free the memory. */
  530. if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
  531. {
  532. vPortFree( pxEventBits );
  533. }
  534. else
  535. {
  536. mtCOVERAGE_TEST_MARKER();
  537. }
  538. }
  539. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  540. }
  541. ( void ) xTaskResumeAll();
  542. }
  543. /*-----------------------------------------------------------*/
  544. /* For internal use only - execute a 'set bits' command that was pended from
  545. an interrupt. */
  546. void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet )
  547. {
  548. ( void ) xEventGroupSetBits( pvEventGroup, ( EventBits_t ) ulBitsToSet );
  549. }
  550. /*-----------------------------------------------------------*/
  551. /* For internal use only - execute a 'clear bits' command that was pended from
  552. an interrupt. */
  553. void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear )
  554. {
  555. ( void ) xEventGroupClearBits( pvEventGroup, ( EventBits_t ) ulBitsToClear );
  556. }
  557. /*-----------------------------------------------------------*/
  558. static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits )
  559. {
  560. BaseType_t xWaitConditionMet = pdFALSE;
  561. if( xWaitForAllBits == pdFALSE )
  562. {
  563. /* Task only has to wait for one bit within uxBitsToWaitFor to be
  564. set. Is one already set? */
  565. if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( EventBits_t ) 0 )
  566. {
  567. xWaitConditionMet = pdTRUE;
  568. }
  569. else
  570. {
  571. mtCOVERAGE_TEST_MARKER();
  572. }
  573. }
  574. else
  575. {
  576. /* Task has to wait for all the bits in uxBitsToWaitFor to be set.
  577. Are they set already? */
  578. if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )
  579. {
  580. xWaitConditionMet = pdTRUE;
  581. }
  582. else
  583. {
  584. mtCOVERAGE_TEST_MARKER();
  585. }
  586. }
  587. return xWaitConditionMet;
  588. }
  589. /*-----------------------------------------------------------*/
  590. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  591. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken )
  592. {
  593. BaseType_t xReturn;
  594. traceEVENT_GROUP_SET_BITS_FROM_ISR( xEventGroup, uxBitsToSet );
  595. xReturn = xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken );
  596. return xReturn;
  597. }
  598. #endif
  599. /*-----------------------------------------------------------*/
  600. #if (configUSE_TRACE_FACILITY == 1)
  601. UBaseType_t uxEventGroupGetNumber( void* xEventGroup )
  602. {
  603. UBaseType_t xReturn;
  604. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  605. if( xEventGroup == NULL )
  606. {
  607. xReturn = 0;
  608. }
  609. else
  610. {
  611. xReturn = pxEventBits->uxEventGroupNumber;
  612. }
  613. return xReturn;
  614. }
  615. #endif /* configUSE_TRACE_FACILITY */
  616. /*-----------------------------------------------------------*/
  617. #if ( configUSE_TRACE_FACILITY == 1 )
  618. void vEventGroupSetNumber( void * xEventGroup, UBaseType_t uxEventGroupNumber )
  619. {
  620. ( ( EventGroup_t * ) xEventGroup )->uxEventGroupNumber = uxEventGroupNumber;
  621. }
  622. #endif /* configUSE_TRACE_FACILITY */
  623. /*-----------------------------------------------------------*/