event_groups.c 26 KB

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