event_groups.c 23 KB

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