event_groups.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. /* Standard includes. */
  56. #include <stdlib.h>
  57. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  58. all the API functions to use the MPU wrappers. That should only be done when
  59. task.h is included from an application file. */
  60. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  61. /* FreeRTOS includes. */
  62. #include "FreeRTOS.h"
  63. #include "task.h"
  64. #include "timers.h"
  65. #include "event_groups.h"
  66. /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
  67. MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
  68. header files above, but not in this file, in order to generate the correct
  69. privileged Vs unprivileged linkage and placement. */
  70. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
  71. #if ( INCLUDE_xEventGroupSetBitFromISR == 1 ) && ( configUSE_TIMERS == 0 )
  72. #error configUSE_TIMERS must be set to 1 to make the xEventGroupSetBitFromISR() function available.
  73. #endif
  74. #if ( INCLUDE_xEventGroupSetBitFromISR == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 0 )
  75. #error INCLUDE_xTimerPendFunctionCall must also be set to one to make the xEventGroupSetBitFromISR() function available.
  76. #endif
  77. /* The following bit fields convey control information in a task's event list
  78. item value. It is important they don't clash with the
  79. taskEVENT_LIST_ITEM_VALUE_IN_USE definition. */
  80. #if configUSE_16_BIT_TICKS == 1
  81. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x0100U
  82. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x0200U
  83. #define eventWAIT_FOR_ALL_BITS 0x0400U
  84. #define eventEVENT_BITS_CONTROL_BYTES 0xff00U
  85. #else
  86. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x01000000UL
  87. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x02000000UL
  88. #define eventWAIT_FOR_ALL_BITS 0x04000000UL
  89. #define eventEVENT_BITS_CONTROL_BYTES 0xff000000UL
  90. #endif
  91. typedef struct xEventGroupDefinition
  92. {
  93. EventBits_t uxEventBits;
  94. List_t xTasksWaitingForBits; /*< List of tasks waiting for a bit to be set. */
  95. #if( configUSE_TRACE_FACILITY == 1 )
  96. UBaseType_t uxEventGroupNumber;
  97. #endif
  98. } EventGroup_t;
  99. /*-----------------------------------------------------------*/
  100. /*
  101. * Test the bits set in uxCurrentEventBits to see if the wait condition is met.
  102. * The wait condition is defined by xWaitForAllBits. If xWaitForAllBits is
  103. * pdTRUE then the wait condition is met if all the bits set in uxBitsToWaitFor
  104. * are also set in uxCurrentEventBits. If xWaitForAllBits is pdFALSE then the
  105. * wait condition is met if any of the bits set in uxBitsToWait for are also set
  106. * in uxCurrentEventBits.
  107. */
  108. static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits );
  109. /*-----------------------------------------------------------*/
  110. EventGroupHandle_t xEventGroupCreate( void )
  111. {
  112. EventGroup_t *pxEventBits;
  113. pxEventBits = ( EventGroup_t * ) pvPortMalloc( sizeof( EventGroup_t ) );
  114. if( pxEventBits != NULL )
  115. {
  116. pxEventBits->uxEventBits = 0;
  117. vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );
  118. traceEVENT_GROUP_CREATE( pxEventBits );
  119. }
  120. else
  121. {
  122. traceEVENT_GROUP_CREATE_FAILED();
  123. }
  124. return ( EventGroupHandle_t ) pxEventBits;
  125. }
  126. /*-----------------------------------------------------------*/
  127. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait )
  128. {
  129. EventBits_t uxOriginalBitValue, uxReturn;
  130. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  131. BaseType_t xAlreadyYielded;
  132. BaseType_t xTimeoutOccurred = pdFALSE;
  133. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  134. configASSERT( uxBitsToWaitFor != 0 );
  135. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  136. {
  137. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  138. }
  139. #endif
  140. vTaskSuspendAll();
  141. {
  142. uxOriginalBitValue = pxEventBits->uxEventBits;
  143. ( void ) xEventGroupSetBits( xEventGroup, uxBitsToSet );
  144. if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )
  145. {
  146. /* All the rendezvous bits are now set - no need to block. */
  147. uxReturn = ( uxOriginalBitValue | uxBitsToSet );
  148. /* Rendezvous always clear the bits. They will have been cleared
  149. already unless this is the only task in the rendezvous. */
  150. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  151. xTicksToWait = 0;
  152. }
  153. else
  154. {
  155. if( xTicksToWait != ( TickType_t ) 0 )
  156. {
  157. traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );
  158. /* Store the bits that the calling task is waiting for in the
  159. task's event list item so the kernel knows when a match is
  160. found. Then enter the blocked state. */
  161. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait );
  162. /* This assignment is obsolete as uxReturn will get set after
  163. the task unblocks, but some compilers mistakenly generate a
  164. warning about uxReturn being returned without being set if the
  165. assignment is omitted. */
  166. uxReturn = 0;
  167. }
  168. else
  169. {
  170. /* The rendezvous bits were not set, but no block time was
  171. specified - just return the current event bit value. */
  172. uxReturn = pxEventBits->uxEventBits;
  173. }
  174. }
  175. }
  176. xAlreadyYielded = xTaskResumeAll();
  177. if( xTicksToWait != ( TickType_t ) 0 )
  178. {
  179. if( xAlreadyYielded == pdFALSE )
  180. {
  181. portYIELD_WITHIN_API();
  182. }
  183. else
  184. {
  185. mtCOVERAGE_TEST_MARKER();
  186. }
  187. /* The task blocked to wait for its required bits to be set - at this
  188. point either the required bits were set or the block time expired. If
  189. the required bits were set they will have been stored in the task's
  190. event list item, and they should now be retrieved then cleared. */
  191. uxReturn = uxTaskResetEventItemValue();
  192. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  193. {
  194. /* The task timed out, just return the current event bit value. */
  195. taskENTER_CRITICAL();
  196. {
  197. uxReturn = pxEventBits->uxEventBits;
  198. /* Although the task got here because it timed out before the
  199. bits it was waiting for were set, it is possible that since it
  200. unblocked another task has set the bits. If this is the case
  201. then it needs to clear the bits before exiting. */
  202. if( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor )
  203. {
  204. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  205. }
  206. else
  207. {
  208. mtCOVERAGE_TEST_MARKER();
  209. }
  210. }
  211. taskEXIT_CRITICAL();
  212. xTimeoutOccurred = pdTRUE;
  213. }
  214. else
  215. {
  216. /* The task unblocked because the bits were set. */
  217. }
  218. /* Control bits might be set as the task had blocked should not be
  219. returned. */
  220. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  221. }
  222. traceEVENT_GROUP_SYNC_END( xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTimeoutOccurred );
  223. return uxReturn;
  224. }
  225. /*-----------------------------------------------------------*/
  226. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait )
  227. {
  228. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  229. EventBits_t uxReturn, uxControlBits = 0;
  230. BaseType_t xWaitConditionMet, xAlreadyYielded;
  231. BaseType_t xTimeoutOccurred = pdFALSE;
  232. /* Check the user is not attempting to wait on the bits used by the kernel
  233. itself, and that at least one bit is being requested. */
  234. configASSERT( xEventGroup );
  235. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  236. configASSERT( uxBitsToWaitFor != 0 );
  237. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  238. {
  239. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  240. }
  241. #endif
  242. vTaskSuspendAll();
  243. {
  244. const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;
  245. /* Check to see if the wait condition is already met or not. */
  246. xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits );
  247. if( xWaitConditionMet != pdFALSE )
  248. {
  249. /* The wait condition has already been met so there is no need to
  250. block. */
  251. uxReturn = uxCurrentEventBits;
  252. xTicksToWait = ( TickType_t ) 0;
  253. /* Clear the wait bits if requested to do so. */
  254. if( xClearOnExit != pdFALSE )
  255. {
  256. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  257. }
  258. else
  259. {
  260. mtCOVERAGE_TEST_MARKER();
  261. }
  262. }
  263. else if( xTicksToWait == ( TickType_t ) 0 )
  264. {
  265. /* The wait condition has not been met, but no block time was
  266. specified, so just return the current value. */
  267. uxReturn = uxCurrentEventBits;
  268. }
  269. else
  270. {
  271. /* The task is going to block to wait for its required bits to be
  272. set. uxControlBits are used to remember the specified behaviour of
  273. this call to xEventGroupWaitBits() - for use when the event bits
  274. unblock the task. */
  275. if( xClearOnExit != pdFALSE )
  276. {
  277. uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT;
  278. }
  279. else
  280. {
  281. mtCOVERAGE_TEST_MARKER();
  282. }
  283. if( xWaitForAllBits != pdFALSE )
  284. {
  285. uxControlBits |= eventWAIT_FOR_ALL_BITS;
  286. }
  287. else
  288. {
  289. mtCOVERAGE_TEST_MARKER();
  290. }
  291. /* Store the bits that the calling task is waiting for in the
  292. task's event list item so the kernel knows when a match is
  293. found. Then enter the blocked state. */
  294. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait );
  295. /* This is obsolete as it will get set after the task unblocks, but
  296. some compilers mistakenly generate a warning about the variable
  297. being returned without being set if it is not done. */
  298. uxReturn = 0;
  299. traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );
  300. }
  301. }
  302. xAlreadyYielded = xTaskResumeAll();
  303. if( xTicksToWait != ( TickType_t ) 0 )
  304. {
  305. if( xAlreadyYielded == pdFALSE )
  306. {
  307. portYIELD_WITHIN_API();
  308. }
  309. else
  310. {
  311. mtCOVERAGE_TEST_MARKER();
  312. }
  313. /* The task blocked to wait for its required bits to be set - at this
  314. point either the required bits were set or the block time expired. If
  315. the required bits were set they will have been stored in the task's
  316. event list item, and they should now be retrieved then cleared. */
  317. uxReturn = uxTaskResetEventItemValue();
  318. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  319. {
  320. taskENTER_CRITICAL();
  321. {
  322. /* The task timed out, just return the current event bit value. */
  323. uxReturn = pxEventBits->uxEventBits;
  324. /* It is possible that the event bits were updated between this
  325. task leaving the Blocked state and running again. */
  326. if( prvTestWaitCondition( uxReturn, uxBitsToWaitFor, xWaitForAllBits ) != pdFALSE )
  327. {
  328. if( xClearOnExit != pdFALSE )
  329. {
  330. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  331. }
  332. else
  333. {
  334. mtCOVERAGE_TEST_MARKER();
  335. }
  336. }
  337. else
  338. {
  339. mtCOVERAGE_TEST_MARKER();
  340. }
  341. }
  342. taskEXIT_CRITICAL();
  343. /* Prevent compiler warnings when trace macros are not used. */
  344. xTimeoutOccurred = pdFALSE;
  345. }
  346. else
  347. {
  348. /* The task unblocked because the bits were set. */
  349. }
  350. /* The task blocked so control bits may have been set. */
  351. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  352. }
  353. traceEVENT_GROUP_WAIT_BITS_END( xEventGroup, uxBitsToWaitFor, xTimeoutOccurred );
  354. return uxReturn;
  355. }
  356. /*-----------------------------------------------------------*/
  357. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )
  358. {
  359. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  360. EventBits_t uxReturn;
  361. /* Check the user is not attempting to clear the bits used by the kernel
  362. itself. */
  363. configASSERT( xEventGroup );
  364. configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  365. taskENTER_CRITICAL();
  366. {
  367. traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );
  368. /* The value returned is the event group value prior to the bits being
  369. cleared. */
  370. uxReturn = pxEventBits->uxEventBits;
  371. /* Clear the bits. */
  372. pxEventBits->uxEventBits &= ~uxBitsToClear;
  373. }
  374. taskEXIT_CRITICAL();
  375. return uxReturn;
  376. }
  377. /*-----------------------------------------------------------*/
  378. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  379. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )
  380. {
  381. BaseType_t xReturn;
  382. traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear );
  383. xReturn = xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL );
  384. return xReturn;
  385. }
  386. #endif
  387. /*-----------------------------------------------------------*/
  388. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
  389. {
  390. UBaseType_t uxSavedInterruptStatus;
  391. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  392. EventBits_t uxReturn;
  393. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  394. {
  395. uxReturn = pxEventBits->uxEventBits;
  396. }
  397. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  398. return uxReturn;
  399. }
  400. /*-----------------------------------------------------------*/
  401. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet )
  402. {
  403. ListItem_t *pxListItem, *pxNext;
  404. ListItem_t const *pxListEnd;
  405. List_t *pxList;
  406. EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;
  407. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  408. BaseType_t xMatchFound = pdFALSE;
  409. /* Check the user is not attempting to set the bits used by the kernel
  410. itself. */
  411. configASSERT( xEventGroup );
  412. configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  413. pxList = &( pxEventBits->xTasksWaitingForBits );
  414. 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. */
  415. vTaskSuspendAll();
  416. {
  417. traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );
  418. pxListItem = listGET_HEAD_ENTRY( pxList );
  419. /* Set the bits. */
  420. pxEventBits->uxEventBits |= uxBitsToSet;
  421. /* See if the new bit value should unblock any tasks. */
  422. while( pxListItem != pxListEnd )
  423. {
  424. pxNext = listGET_NEXT( pxListItem );
  425. uxBitsWaitedFor = listGET_LIST_ITEM_VALUE( pxListItem );
  426. xMatchFound = pdFALSE;
  427. /* Split the bits waited for from the control bits. */
  428. uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;
  429. uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;
  430. if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( EventBits_t ) 0 )
  431. {
  432. /* Just looking for single bit being set. */
  433. if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( EventBits_t ) 0 )
  434. {
  435. xMatchFound = pdTRUE;
  436. }
  437. else
  438. {
  439. mtCOVERAGE_TEST_MARKER();
  440. }
  441. }
  442. else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )
  443. {
  444. /* All bits are set. */
  445. xMatchFound = pdTRUE;
  446. }
  447. else
  448. {
  449. /* Need all bits to be set, but not all the bits were set. */
  450. }
  451. if( xMatchFound != pdFALSE )
  452. {
  453. /* The bits match. Should the bits be cleared on exit? */
  454. if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( EventBits_t ) 0 )
  455. {
  456. uxBitsToClear |= uxBitsWaitedFor;
  457. }
  458. else
  459. {
  460. mtCOVERAGE_TEST_MARKER();
  461. }
  462. /* Store the actual event flag value in the task's event list
  463. item before removing the task from the event list. The
  464. eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows
  465. that is was unblocked due to its required bits matching, rather
  466. than because it timed out. */
  467. ( void ) xTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET );
  468. }
  469. /* Move onto the next list item. Note pxListItem->pxNext is not
  470. used here as the list item may have been removed from the event list
  471. and inserted into the ready/pending reading list. */
  472. pxListItem = pxNext;
  473. }
  474. /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT
  475. bit was set in the control word. */
  476. pxEventBits->uxEventBits &= ~uxBitsToClear;
  477. }
  478. ( void ) xTaskResumeAll();
  479. return pxEventBits->uxEventBits;
  480. }
  481. /*-----------------------------------------------------------*/
  482. void vEventGroupDelete( EventGroupHandle_t xEventGroup )
  483. {
  484. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  485. const List_t *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );
  486. vTaskSuspendAll();
  487. {
  488. traceEVENT_GROUP_DELETE( xEventGroup );
  489. while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )
  490. {
  491. /* Unblock the task, returning 0 as the event list is being deleted
  492. and cannot therefore have any bits set. */
  493. configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );
  494. ( void ) xTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );
  495. }
  496. vPortFree( pxEventBits );
  497. }
  498. ( void ) xTaskResumeAll();
  499. }
  500. /*-----------------------------------------------------------*/
  501. /* For internal use only - execute a 'set bits' command that was pended from
  502. an interrupt. */
  503. void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet )
  504. {
  505. ( void ) xEventGroupSetBits( pvEventGroup, ( EventBits_t ) ulBitsToSet );
  506. }
  507. /*-----------------------------------------------------------*/
  508. /* For internal use only - execute a 'clear bits' command that was pended from
  509. an interrupt. */
  510. void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear )
  511. {
  512. ( void ) xEventGroupClearBits( pvEventGroup, ( EventBits_t ) ulBitsToClear );
  513. }
  514. /*-----------------------------------------------------------*/
  515. static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits )
  516. {
  517. BaseType_t xWaitConditionMet = pdFALSE;
  518. if( xWaitForAllBits == pdFALSE )
  519. {
  520. /* Task only has to wait for one bit within uxBitsToWaitFor to be
  521. set. Is one already set? */
  522. if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( EventBits_t ) 0 )
  523. {
  524. xWaitConditionMet = pdTRUE;
  525. }
  526. else
  527. {
  528. mtCOVERAGE_TEST_MARKER();
  529. }
  530. }
  531. else
  532. {
  533. /* Task has to wait for all the bits in uxBitsToWaitFor to be set.
  534. Are they set already? */
  535. if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )
  536. {
  537. xWaitConditionMet = pdTRUE;
  538. }
  539. else
  540. {
  541. mtCOVERAGE_TEST_MARKER();
  542. }
  543. }
  544. return xWaitConditionMet;
  545. }
  546. /*-----------------------------------------------------------*/
  547. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  548. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken )
  549. {
  550. BaseType_t xReturn;
  551. traceEVENT_GROUP_SET_BITS_FROM_ISR( xEventGroup, uxBitsToSet );
  552. xReturn = xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken );
  553. return xReturn;
  554. }
  555. #endif
  556. /*-----------------------------------------------------------*/
  557. #if (configUSE_TRACE_FACILITY == 1)
  558. UBaseType_t uxEventGroupGetNumber( void* xEventGroup )
  559. {
  560. UBaseType_t xReturn;
  561. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  562. if( xEventGroup == NULL )
  563. {
  564. xReturn = 0;
  565. }
  566. else
  567. {
  568. xReturn = pxEventBits->uxEventGroupNumber;
  569. }
  570. return xReturn;
  571. }
  572. #endif