event_groups.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. FreeRTOS V9.0.0 - Copyright (C) 2016 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. /* The following bit fields convey control information in a task's event list
  72. item value. It is important they don't clash with the
  73. taskEVENT_LIST_ITEM_VALUE_IN_USE definition. */
  74. #if configUSE_16_BIT_TICKS == 1
  75. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x0100U
  76. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x0200U
  77. #define eventWAIT_FOR_ALL_BITS 0x0400U
  78. #define eventEVENT_BITS_CONTROL_BYTES 0xff00U
  79. #else
  80. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x01000000UL
  81. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x02000000UL
  82. #define eventWAIT_FOR_ALL_BITS 0x04000000UL
  83. #define eventEVENT_BITS_CONTROL_BYTES 0xff000000UL
  84. #endif
  85. typedef struct xEventGroupDefinition
  86. {
  87. EventBits_t uxEventBits;
  88. List_t xTasksWaitingForBits; /*< List of tasks waiting for a bit to be set. */
  89. #if( configUSE_TRACE_FACILITY == 1 )
  90. UBaseType_t uxEventGroupNumber;
  91. #endif
  92. #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  93. uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the event group is statically allocated to ensure no attempt is made to free the memory. */
  94. #endif
  95. } EventGroup_t;
  96. /*-----------------------------------------------------------*/
  97. /*
  98. * Test the bits set in uxCurrentEventBits to see if the wait condition is met.
  99. * The wait condition is defined by xWaitForAllBits. If xWaitForAllBits is
  100. * pdTRUE then the wait condition is met if all the bits set in uxBitsToWaitFor
  101. * are also set in uxCurrentEventBits. If xWaitForAllBits is pdFALSE then the
  102. * wait condition is met if any of the bits set in uxBitsToWait for are also set
  103. * in uxCurrentEventBits.
  104. */
  105. static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits ) PRIVILEGED_FUNCTION;
  106. /*-----------------------------------------------------------*/
  107. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  108. EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t *pxEventGroupBuffer )
  109. {
  110. EventGroup_t *pxEventBits;
  111. /* A StaticEventGroup_t object must be provided. */
  112. configASSERT( pxEventGroupBuffer );
  113. /* The user has provided a statically allocated event group - use it. */
  114. pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 EventGroup_t and StaticEventGroup_t are guaranteed to have the same size and alignment requirement - checked by configASSERT(). */
  115. if( pxEventBits != NULL )
  116. {
  117. pxEventBits->uxEventBits = 0;
  118. vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );
  119. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  120. {
  121. /* Both static and dynamic allocation can be used, so note that
  122. this event group was created statically in case the event group
  123. is later deleted. */
  124. pxEventBits->ucStaticallyAllocated = pdTRUE;
  125. }
  126. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  127. traceEVENT_GROUP_CREATE( pxEventBits );
  128. }
  129. else
  130. {
  131. traceEVENT_GROUP_CREATE_FAILED();
  132. }
  133. return ( EventGroupHandle_t ) pxEventBits;
  134. }
  135. #endif /* configSUPPORT_STATIC_ALLOCATION */
  136. /*-----------------------------------------------------------*/
  137. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  138. EventGroupHandle_t xEventGroupCreate( void )
  139. {
  140. EventGroup_t *pxEventBits;
  141. /* Allocate the event group. */
  142. pxEventBits = ( EventGroup_t * ) pvPortMalloc( sizeof( EventGroup_t ) );
  143. if( pxEventBits != NULL )
  144. {
  145. pxEventBits->uxEventBits = 0;
  146. vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );
  147. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  148. {
  149. /* Both static and dynamic allocation can be used, so note this
  150. event group was allocated statically in case the event group is
  151. later deleted. */
  152. pxEventBits->ucStaticallyAllocated = pdFALSE;
  153. }
  154. #endif /* configSUPPORT_STATIC_ALLOCATION */
  155. traceEVENT_GROUP_CREATE( pxEventBits );
  156. }
  157. else
  158. {
  159. traceEVENT_GROUP_CREATE_FAILED();
  160. }
  161. return ( EventGroupHandle_t ) pxEventBits;
  162. }
  163. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  164. /*-----------------------------------------------------------*/
  165. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait )
  166. {
  167. EventBits_t uxOriginalBitValue, uxReturn;
  168. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  169. BaseType_t xAlreadyYielded;
  170. BaseType_t xTimeoutOccurred = pdFALSE;
  171. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  172. configASSERT( uxBitsToWaitFor != 0 );
  173. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  174. {
  175. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  176. }
  177. #endif
  178. vTaskSuspendAll();
  179. {
  180. uxOriginalBitValue = pxEventBits->uxEventBits;
  181. ( void ) xEventGroupSetBits( xEventGroup, uxBitsToSet );
  182. if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )
  183. {
  184. /* All the rendezvous bits are now set - no need to block. */
  185. uxReturn = ( uxOriginalBitValue | uxBitsToSet );
  186. /* Rendezvous always clear the bits. They will have been cleared
  187. already unless this is the only task in the rendezvous. */
  188. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  189. xTicksToWait = 0;
  190. }
  191. else
  192. {
  193. if( xTicksToWait != ( TickType_t ) 0 )
  194. {
  195. traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );
  196. /* Store the bits that the calling task is waiting for in the
  197. task's event list item so the kernel knows when a match is
  198. found. Then enter the blocked state. */
  199. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait );
  200. /* This assignment is obsolete as uxReturn will get set after
  201. the task unblocks, but some compilers mistakenly generate a
  202. warning about uxReturn being returned without being set if the
  203. assignment is omitted. */
  204. uxReturn = 0;
  205. }
  206. else
  207. {
  208. /* The rendezvous bits were not set, but no block time was
  209. specified - just return the current event bit value. */
  210. uxReturn = pxEventBits->uxEventBits;
  211. }
  212. }
  213. }
  214. xAlreadyYielded = xTaskResumeAll();
  215. if( xTicksToWait != ( TickType_t ) 0 )
  216. {
  217. if( xAlreadyYielded == pdFALSE )
  218. {
  219. portYIELD_WITHIN_API();
  220. }
  221. else
  222. {
  223. mtCOVERAGE_TEST_MARKER();
  224. }
  225. /* The task blocked to wait for its required bits to be set - at this
  226. point either the required bits were set or the block time expired. If
  227. the required bits were set they will have been stored in the task's
  228. event list item, and they should now be retrieved then cleared. */
  229. uxReturn = uxTaskResetEventItemValue();
  230. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  231. {
  232. /* The task timed out, just return the current event bit value. */
  233. taskENTER_CRITICAL();
  234. {
  235. uxReturn = pxEventBits->uxEventBits;
  236. /* Although the task got here because it timed out before the
  237. bits it was waiting for were set, it is possible that since it
  238. unblocked another task has set the bits. If this is the case
  239. then it needs to clear the bits before exiting. */
  240. if( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor )
  241. {
  242. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  243. }
  244. else
  245. {
  246. mtCOVERAGE_TEST_MARKER();
  247. }
  248. }
  249. taskEXIT_CRITICAL();
  250. xTimeoutOccurred = pdTRUE;
  251. }
  252. else
  253. {
  254. /* The task unblocked because the bits were set. */
  255. }
  256. /* Control bits might be set as the task had blocked should not be
  257. returned. */
  258. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  259. }
  260. traceEVENT_GROUP_SYNC_END( xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTimeoutOccurred );
  261. return uxReturn;
  262. }
  263. /*-----------------------------------------------------------*/
  264. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait )
  265. {
  266. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  267. EventBits_t uxReturn, uxControlBits = 0;
  268. BaseType_t xWaitConditionMet, xAlreadyYielded;
  269. BaseType_t xTimeoutOccurred = pdFALSE;
  270. /* Check the user is not attempting to wait on the bits used by the kernel
  271. itself, and that at least one bit is being requested. */
  272. configASSERT( xEventGroup );
  273. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  274. configASSERT( uxBitsToWaitFor != 0 );
  275. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  276. {
  277. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  278. }
  279. #endif
  280. vTaskSuspendAll();
  281. {
  282. const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;
  283. /* Check to see if the wait condition is already met or not. */
  284. xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits );
  285. if( xWaitConditionMet != pdFALSE )
  286. {
  287. /* The wait condition has already been met so there is no need to
  288. block. */
  289. uxReturn = uxCurrentEventBits;
  290. xTicksToWait = ( TickType_t ) 0;
  291. /* Clear the wait bits if requested to do so. */
  292. if( xClearOnExit != pdFALSE )
  293. {
  294. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  295. }
  296. else
  297. {
  298. mtCOVERAGE_TEST_MARKER();
  299. }
  300. }
  301. else if( xTicksToWait == ( TickType_t ) 0 )
  302. {
  303. /* The wait condition has not been met, but no block time was
  304. specified, so just return the current value. */
  305. uxReturn = uxCurrentEventBits;
  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. }
  380. taskEXIT_CRITICAL();
  381. /* Prevent compiler warnings when trace macros are not used. */
  382. xTimeoutOccurred = pdFALSE;
  383. }
  384. else
  385. {
  386. /* The task unblocked because the bits were set. */
  387. }
  388. /* The task blocked so control bits may have been set. */
  389. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  390. }
  391. traceEVENT_GROUP_WAIT_BITS_END( xEventGroup, uxBitsToWaitFor, xTimeoutOccurred );
  392. return uxReturn;
  393. }
  394. /*-----------------------------------------------------------*/
  395. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )
  396. {
  397. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  398. EventBits_t uxReturn;
  399. /* Check the user is not attempting to clear the bits used by the kernel
  400. itself. */
  401. configASSERT( xEventGroup );
  402. configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  403. taskENTER_CRITICAL();
  404. {
  405. traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );
  406. /* The value returned is the event group value prior to the bits being
  407. cleared. */
  408. uxReturn = pxEventBits->uxEventBits;
  409. /* Clear the bits. */
  410. pxEventBits->uxEventBits &= ~uxBitsToClear;
  411. }
  412. taskEXIT_CRITICAL();
  413. return uxReturn;
  414. }
  415. /*-----------------------------------------------------------*/
  416. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  417. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )
  418. {
  419. BaseType_t xReturn;
  420. traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear );
  421. xReturn = xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL );
  422. return xReturn;
  423. }
  424. #endif
  425. /*-----------------------------------------------------------*/
  426. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
  427. {
  428. UBaseType_t uxSavedInterruptStatus;
  429. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  430. EventBits_t uxReturn;
  431. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  432. {
  433. uxReturn = pxEventBits->uxEventBits;
  434. }
  435. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  436. return uxReturn;
  437. }
  438. /*-----------------------------------------------------------*/
  439. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet )
  440. {
  441. ListItem_t *pxListItem, *pxNext;
  442. ListItem_t const *pxListEnd;
  443. List_t *pxList;
  444. EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;
  445. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  446. BaseType_t xMatchFound = pdFALSE;
  447. /* Check the user is not attempting to set the bits used by the kernel
  448. itself. */
  449. configASSERT( xEventGroup );
  450. configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  451. pxList = &( pxEventBits->xTasksWaitingForBits );
  452. 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. */
  453. vTaskSuspendAll();
  454. {
  455. traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );
  456. pxListItem = listGET_HEAD_ENTRY( pxList );
  457. /* Set the bits. */
  458. pxEventBits->uxEventBits |= uxBitsToSet;
  459. /* See if the new bit value should unblock any tasks. */
  460. while( pxListItem != pxListEnd )
  461. {
  462. pxNext = listGET_NEXT( pxListItem );
  463. uxBitsWaitedFor = listGET_LIST_ITEM_VALUE( pxListItem );
  464. xMatchFound = pdFALSE;
  465. /* Split the bits waited for from the control bits. */
  466. uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;
  467. uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;
  468. if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( EventBits_t ) 0 )
  469. {
  470. /* Just looking for single bit being set. */
  471. if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( EventBits_t ) 0 )
  472. {
  473. xMatchFound = pdTRUE;
  474. }
  475. else
  476. {
  477. mtCOVERAGE_TEST_MARKER();
  478. }
  479. }
  480. else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )
  481. {
  482. /* All bits are set. */
  483. xMatchFound = pdTRUE;
  484. }
  485. else
  486. {
  487. /* Need all bits to be set, but not all the bits were set. */
  488. }
  489. if( xMatchFound != pdFALSE )
  490. {
  491. /* The bits match. Should the bits be cleared on exit? */
  492. if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( EventBits_t ) 0 )
  493. {
  494. uxBitsToClear |= uxBitsWaitedFor;
  495. }
  496. else
  497. {
  498. mtCOVERAGE_TEST_MARKER();
  499. }
  500. /* Store the actual event flag value in the task's event list
  501. item before removing the task from the event list. The
  502. eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows
  503. that is was unblocked due to its required bits matching, rather
  504. than because it timed out. */
  505. ( void ) xTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET );
  506. }
  507. /* Move onto the next list item. Note pxListItem->pxNext is not
  508. used here as the list item may have been removed from the event list
  509. and inserted into the ready/pending reading list. */
  510. pxListItem = pxNext;
  511. }
  512. /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT
  513. bit was set in the control word. */
  514. pxEventBits->uxEventBits &= ~uxBitsToClear;
  515. }
  516. ( void ) xTaskResumeAll();
  517. return pxEventBits->uxEventBits;
  518. }
  519. /*-----------------------------------------------------------*/
  520. void vEventGroupDelete( EventGroupHandle_t xEventGroup )
  521. {
  522. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  523. const List_t *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );
  524. vTaskSuspendAll();
  525. {
  526. traceEVENT_GROUP_DELETE( xEventGroup );
  527. while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )
  528. {
  529. /* Unblock the task, returning 0 as the event list is being deleted
  530. and cannot therefore have any bits set. */
  531. configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );
  532. ( void ) xTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );
  533. }
  534. #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
  535. {
  536. /* The event group can only have been allocated dynamically - free
  537. it again. */
  538. vPortFree( pxEventBits );
  539. }
  540. #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  541. {
  542. /* The event group could have been allocated statically or
  543. dynamically, so check before attempting to free the memory. */
  544. if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
  545. {
  546. vPortFree( pxEventBits );
  547. }
  548. else
  549. {
  550. mtCOVERAGE_TEST_MARKER();
  551. }
  552. }
  553. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  554. }
  555. ( void ) xTaskResumeAll();
  556. }
  557. /*-----------------------------------------------------------*/
  558. /* For internal use only - execute a 'set bits' command that was pended from
  559. an interrupt. */
  560. void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet )
  561. {
  562. ( void ) xEventGroupSetBits( pvEventGroup, ( EventBits_t ) ulBitsToSet );
  563. }
  564. /*-----------------------------------------------------------*/
  565. /* For internal use only - execute a 'clear bits' command that was pended from
  566. an interrupt. */
  567. void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear )
  568. {
  569. ( void ) xEventGroupClearBits( pvEventGroup, ( EventBits_t ) ulBitsToClear );
  570. }
  571. /*-----------------------------------------------------------*/
  572. static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits )
  573. {
  574. BaseType_t xWaitConditionMet = pdFALSE;
  575. if( xWaitForAllBits == pdFALSE )
  576. {
  577. /* Task only has to wait for one bit within uxBitsToWaitFor to be
  578. set. Is one already set? */
  579. if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( EventBits_t ) 0 )
  580. {
  581. xWaitConditionMet = pdTRUE;
  582. }
  583. else
  584. {
  585. mtCOVERAGE_TEST_MARKER();
  586. }
  587. }
  588. else
  589. {
  590. /* Task has to wait for all the bits in uxBitsToWaitFor to be set.
  591. Are they set already? */
  592. if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )
  593. {
  594. xWaitConditionMet = pdTRUE;
  595. }
  596. else
  597. {
  598. mtCOVERAGE_TEST_MARKER();
  599. }
  600. }
  601. return xWaitConditionMet;
  602. }
  603. /*-----------------------------------------------------------*/
  604. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  605. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken )
  606. {
  607. BaseType_t xReturn;
  608. traceEVENT_GROUP_SET_BITS_FROM_ISR( xEventGroup, uxBitsToSet );
  609. xReturn = xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken );
  610. return xReturn;
  611. }
  612. #endif
  613. /*-----------------------------------------------------------*/
  614. #if (configUSE_TRACE_FACILITY == 1)
  615. UBaseType_t uxEventGroupGetNumber( void* xEventGroup )
  616. {
  617. UBaseType_t xReturn;
  618. EventGroup_t *pxEventBits = ( EventGroup_t * ) xEventGroup;
  619. if( xEventGroup == NULL )
  620. {
  621. xReturn = 0;
  622. }
  623. else
  624. {
  625. xReturn = pxEventBits->uxEventGroupNumber;
  626. }
  627. return xReturn;
  628. }
  629. #endif