event_groups.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /*
  2. * FreeRTOS Kernel V10.2.1
  3. * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. /* Standard includes. */
  28. #include <stdlib.h>
  29. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  30. all the API functions to use the MPU wrappers. That should only be done when
  31. task.h is included from an application file. */
  32. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  33. /* FreeRTOS includes. */
  34. #include "FreeRTOS.h"
  35. #include "task.h"
  36. #include "timers.h"
  37. #include "event_groups.h"
  38. /* Lint e961, e750 and e9021 are suppressed as a MISRA exception justified
  39. because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
  40. for the header files above, but not in this file, in order to generate the
  41. correct privileged Vs unprivileged linkage and placement. */
  42. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021 See comment above. */
  43. /* The following bit fields convey control information in a task's event list
  44. item value. It is important they don't clash with the
  45. taskEVENT_LIST_ITEM_VALUE_IN_USE definition. */
  46. #if configUSE_16_BIT_TICKS == 1
  47. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x0100U
  48. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x0200U
  49. #define eventWAIT_FOR_ALL_BITS 0x0400U
  50. #define eventEVENT_BITS_CONTROL_BYTES 0xff00U
  51. #else
  52. #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x01000000UL
  53. #define eventUNBLOCKED_DUE_TO_BIT_SET 0x02000000UL
  54. #define eventWAIT_FOR_ALL_BITS 0x04000000UL
  55. #define eventEVENT_BITS_CONTROL_BYTES 0xff000000UL
  56. #endif
  57. typedef struct EventGroupDef_t
  58. {
  59. EventBits_t uxEventBits;
  60. List_t xTasksWaitingForBits; /*< List of tasks waiting for a bit to be set. */
  61. #if( configUSE_TRACE_FACILITY == 1 )
  62. UBaseType_t uxEventGroupNumber;
  63. #endif
  64. #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  65. uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the event group is statically allocated to ensure no attempt is made to free the memory. */
  66. #endif
  67. portMUX_TYPE eventGroupMux; //Mutex required due to SMP
  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. } /*lint !e529 xSize is referenced if configASSERT() is defined. */
  94. #endif /* configASSERT_DEFINED */
  95. /* The user has provided a statically allocated event group - use it. */
  96. pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 !e9087 EventGroup_t and StaticEventGroup_t are deliberately aliased for data hiding purposes and 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. vPortCPUInitializeMutex( &pxEventBits->eventGroupMux );
  111. }
  112. else
  113. {
  114. /* xEventGroupCreateStatic should only ever be called with
  115. pxEventGroupBuffer pointing to a pre-allocated (compile time
  116. allocated) StaticEventGroup_t variable. */
  117. traceEVENT_GROUP_CREATE_FAILED();
  118. }
  119. return pxEventBits;
  120. }
  121. #endif /* configSUPPORT_STATIC_ALLOCATION */
  122. /*-----------------------------------------------------------*/
  123. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  124. EventGroupHandle_t xEventGroupCreate( void )
  125. {
  126. EventGroup_t *pxEventBits;
  127. /* Allocate the event group. Justification for MISRA deviation as
  128. follows: pvPortMalloc() always ensures returned memory blocks are
  129. aligned per the requirements of the MCU stack. In this case
  130. pvPortMalloc() must return a pointer that is guaranteed to meet the
  131. alignment requirements of the EventGroup_t structure - which (if you
  132. follow it through) is the alignment requirements of the TickType_t type
  133. (EventBits_t being of TickType_t itself). Therefore, whenever the
  134. stack alignment requirements are greater than or equal to the
  135. TickType_t alignment requirements the cast is safe. In other cases,
  136. where the natural word size of the architecture is less than
  137. sizeof( TickType_t ), the TickType_t variables will be accessed in two
  138. or more reads operations, and the alignment requirements is only that
  139. of each individual read. */
  140. pxEventBits = ( EventGroup_t * ) pvPortMalloc( sizeof( EventGroup_t ) ); /*lint !e9087 !e9079 see comment above. */
  141. if( pxEventBits != NULL )
  142. {
  143. pxEventBits->uxEventBits = 0;
  144. vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );
  145. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  146. {
  147. /* Both static and dynamic allocation can be used, so note this
  148. event group was allocated statically in case the event group is
  149. later deleted. */
  150. pxEventBits->ucStaticallyAllocated = pdFALSE;
  151. }
  152. #endif /* configSUPPORT_STATIC_ALLOCATION */
  153. vPortCPUInitializeMutex( &pxEventBits->eventGroupMux );
  154. traceEVENT_GROUP_CREATE( pxEventBits );
  155. }
  156. else
  157. {
  158. traceEVENT_GROUP_CREATE_FAILED(); /*lint !e9063 Else branch only exists to allow tracing and does not generate code if trace macros are not defined. */
  159. }
  160. return pxEventBits;
  161. }
  162. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  163. /*-----------------------------------------------------------*/
  164. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait )
  165. {
  166. EventBits_t uxOriginalBitValue, uxReturn;
  167. EventGroup_t *pxEventBits = xEventGroup;
  168. BaseType_t xAlreadyYielded = pdFALSE;
  169. BaseType_t xTimeoutOccurred = pdFALSE;
  170. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  171. configASSERT( uxBitsToWaitFor != 0 );
  172. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  173. {
  174. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  175. }
  176. #endif
  177. taskENTER_CRITICAL( &pxEventBits->eventGroupMux );
  178. {
  179. uxOriginalBitValue = pxEventBits->uxEventBits;
  180. ( void ) xEventGroupSetBits( xEventGroup, uxBitsToSet );
  181. if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )
  182. {
  183. /* All the rendezvous bits are now set - no need to block. */
  184. uxReturn = ( uxOriginalBitValue | uxBitsToSet );
  185. /* Rendezvous always clear the bits. They will have been cleared
  186. already unless this is the only task in the rendezvous. */
  187. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  188. xTicksToWait = 0;
  189. }
  190. else
  191. {
  192. if( xTicksToWait != ( TickType_t ) 0 )
  193. {
  194. traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );
  195. /* Store the bits that the calling task is waiting for in the
  196. task's event list item so the kernel knows when a match is
  197. found. Then enter the blocked state. */
  198. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait );
  199. /* This assignment is obsolete as uxReturn will get set after
  200. the task unblocks, but some compilers mistakenly generate a
  201. warning about uxReturn being returned without being set if the
  202. assignment is omitted. */
  203. uxReturn = 0;
  204. }
  205. else
  206. {
  207. /* The rendezvous bits were not set, but no block time was
  208. specified - just return the current event bit value. */
  209. uxReturn = pxEventBits->uxEventBits;
  210. xTimeoutOccurred = pdTRUE;
  211. }
  212. }
  213. }
  214. taskEXIT_CRITICAL( &pxEventBits->eventGroupMux );
  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( &pxEventBits->eventGroupMux );
  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( &pxEventBits->eventGroupMux );
  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. /* Prevent compiler warnings when trace macros are not used. */
  262. ( void ) xTimeoutOccurred;
  263. return uxReturn;
  264. }
  265. /*-----------------------------------------------------------*/
  266. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait )
  267. {
  268. EventGroup_t *pxEventBits = xEventGroup;
  269. EventBits_t uxReturn, uxControlBits = 0;
  270. BaseType_t xWaitConditionMet;
  271. BaseType_t xTimeoutOccurred = pdFALSE;
  272. /* Check the user is not attempting to wait on the bits used by the kernel
  273. itself, and that at least one bit is being requested. */
  274. configASSERT( xEventGroup );
  275. configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  276. configASSERT( uxBitsToWaitFor != 0 );
  277. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  278. {
  279. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  280. }
  281. #endif
  282. taskENTER_CRITICAL( &pxEventBits->eventGroupMux );
  283. {
  284. const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;
  285. /* Check to see if the wait condition is already met or not. */
  286. xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits );
  287. if( xWaitConditionMet != pdFALSE )
  288. {
  289. /* The wait condition has already been met so there is no need to
  290. block. */
  291. uxReturn = uxCurrentEventBits;
  292. xTicksToWait = ( TickType_t ) 0;
  293. /* Clear the wait bits if requested to do so. */
  294. if( xClearOnExit != pdFALSE )
  295. {
  296. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  297. }
  298. else
  299. {
  300. mtCOVERAGE_TEST_MARKER();
  301. }
  302. }
  303. else if( xTicksToWait == ( TickType_t ) 0 )
  304. {
  305. /* The wait condition has not been met, but no block time was
  306. specified, so just return the current value. */
  307. uxReturn = uxCurrentEventBits;
  308. xTimeoutOccurred = pdTRUE;
  309. }
  310. else
  311. {
  312. /* The task is going to block to wait for its required bits to be
  313. set. uxControlBits are used to remember the specified behaviour of
  314. this call to xEventGroupWaitBits() - for use when the event bits
  315. unblock the task. */
  316. if( xClearOnExit != pdFALSE )
  317. {
  318. uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT;
  319. }
  320. else
  321. {
  322. mtCOVERAGE_TEST_MARKER();
  323. }
  324. if( xWaitForAllBits != pdFALSE )
  325. {
  326. uxControlBits |= eventWAIT_FOR_ALL_BITS;
  327. }
  328. else
  329. {
  330. mtCOVERAGE_TEST_MARKER();
  331. }
  332. /* Store the bits that the calling task is waiting for in the
  333. task's event list item so the kernel knows when a match is
  334. found. Then enter the blocked state. */
  335. vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait );
  336. /* This is obsolete as it will get set after the task unblocks, but
  337. some compilers mistakenly generate a warning about the variable
  338. being returned without being set if it is not done. */
  339. uxReturn = 0;
  340. traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );
  341. }
  342. }
  343. taskEXIT_CRITICAL( &pxEventBits->eventGroupMux );
  344. if( xTicksToWait != ( TickType_t ) 0 )
  345. {
  346. portYIELD_WITHIN_API();
  347. /* The task blocked to wait for its required bits to be set - at this
  348. point either the required bits were set or the block time expired. If
  349. the required bits were set they will have been stored in the task's
  350. event list item, and they should now be retrieved then cleared. */
  351. uxReturn = uxTaskResetEventItemValue();
  352. if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
  353. {
  354. taskENTER_CRITICAL( &pxEventBits->eventGroupMux );
  355. {
  356. /* The task timed out, just return the current event bit value. */
  357. uxReturn = pxEventBits->uxEventBits;
  358. /* It is possible that the event bits were updated between this
  359. task leaving the Blocked state and running again. */
  360. if( prvTestWaitCondition( uxReturn, uxBitsToWaitFor, xWaitForAllBits ) != pdFALSE )
  361. {
  362. if( xClearOnExit != pdFALSE )
  363. {
  364. pxEventBits->uxEventBits &= ~uxBitsToWaitFor;
  365. }
  366. else
  367. {
  368. mtCOVERAGE_TEST_MARKER();
  369. }
  370. }
  371. else
  372. {
  373. mtCOVERAGE_TEST_MARKER();
  374. }
  375. xTimeoutOccurred = pdTRUE;
  376. }
  377. taskEXIT_CRITICAL( &pxEventBits->eventGroupMux );
  378. }
  379. else
  380. {
  381. /* The task unblocked because the bits were set. */
  382. }
  383. /* The task blocked so control bits may have been set. */
  384. uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;
  385. }
  386. traceEVENT_GROUP_WAIT_BITS_END( xEventGroup, uxBitsToWaitFor, xTimeoutOccurred );
  387. /* Prevent compiler warnings when trace macros are not used. */
  388. ( void ) xTimeoutOccurred;
  389. return uxReturn;
  390. }
  391. /*-----------------------------------------------------------*/
  392. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )
  393. {
  394. EventGroup_t *pxEventBits = xEventGroup;
  395. EventBits_t uxReturn;
  396. /* Check the user is not attempting to clear the bits used by the kernel
  397. itself. */
  398. configASSERT( xEventGroup );
  399. configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  400. taskENTER_CRITICAL( &pxEventBits->eventGroupMux );
  401. {
  402. traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );
  403. /* The value returned is the event group value prior to the bits being
  404. cleared. */
  405. uxReturn = pxEventBits->uxEventBits;
  406. /* Clear the bits. */
  407. pxEventBits->uxEventBits &= ~uxBitsToClear;
  408. }
  409. taskEXIT_CRITICAL( &pxEventBits->eventGroupMux );
  410. return uxReturn;
  411. }
  412. /*-----------------------------------------------------------*/
  413. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  414. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )
  415. {
  416. BaseType_t xReturn;
  417. traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear );
  418. xReturn = xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL ); /*lint !e9087 Can't avoid cast to void* as a generic callback function not specific to this use case. Callback casts back to original type so safe. */
  419. return xReturn;
  420. }
  421. #endif
  422. /*-----------------------------------------------------------*/
  423. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
  424. {
  425. UBaseType_t uxSavedInterruptStatus;
  426. EventGroup_t const * const pxEventBits = xEventGroup;
  427. EventBits_t uxReturn;
  428. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  429. {
  430. uxReturn = pxEventBits->uxEventBits;
  431. }
  432. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  433. return uxReturn;
  434. } /*lint !e818 EventGroupHandle_t is a typedef used in other functions to so can't be pointer to const. */
  435. /*-----------------------------------------------------------*/
  436. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet )
  437. {
  438. ListItem_t *pxListItem, *pxNext;
  439. ListItem_t const *pxListEnd;
  440. List_t const * pxList;
  441. EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;
  442. EventGroup_t *pxEventBits = xEventGroup;
  443. BaseType_t xMatchFound = pdFALSE;
  444. /* Check the user is not attempting to set the bits used by the kernel
  445. itself. */
  446. configASSERT( xEventGroup );
  447. configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
  448. pxList = &( pxEventBits->xTasksWaitingForBits );
  449. pxListEnd = listGET_END_MARKER( pxList ); /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. */
  450. taskENTER_CRITICAL( &pxEventBits->eventGroupMux );
  451. {
  452. traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );
  453. pxListItem = listGET_HEAD_ENTRY( pxList );
  454. /* Set the bits. */
  455. pxEventBits->uxEventBits |= uxBitsToSet;
  456. /* See if the new bit value should unblock any tasks. */
  457. while( pxListItem != pxListEnd )
  458. {
  459. pxNext = listGET_NEXT( pxListItem );
  460. uxBitsWaitedFor = listGET_LIST_ITEM_VALUE( pxListItem );
  461. xMatchFound = pdFALSE;
  462. /* Split the bits waited for from the control bits. */
  463. uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;
  464. uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;
  465. if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( EventBits_t ) 0 )
  466. {
  467. /* Just looking for single bit being set. */
  468. if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( EventBits_t ) 0 )
  469. {
  470. xMatchFound = pdTRUE;
  471. }
  472. else
  473. {
  474. mtCOVERAGE_TEST_MARKER();
  475. }
  476. }
  477. else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )
  478. {
  479. /* All bits are set. */
  480. xMatchFound = pdTRUE;
  481. }
  482. else
  483. {
  484. /* Need all bits to be set, but not all the bits were set. */
  485. }
  486. if( xMatchFound != pdFALSE )
  487. {
  488. /* The bits match. Should the bits be cleared on exit? */
  489. if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( EventBits_t ) 0 )
  490. {
  491. uxBitsToClear |= uxBitsWaitedFor;
  492. }
  493. else
  494. {
  495. mtCOVERAGE_TEST_MARKER();
  496. }
  497. /* Store the actual event flag value in the task's event list
  498. item before removing the task from the event list. The
  499. eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows
  500. that is was unblocked due to its required bits matching, rather
  501. than because it timed out. */
  502. xTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET );
  503. }
  504. /* Move onto the next list item. Note pxListItem->pxNext is not
  505. used here as the list item may have been removed from the event list
  506. and inserted into the ready/pending reading list. */
  507. pxListItem = pxNext;
  508. }
  509. /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT
  510. bit was set in the control word. */
  511. pxEventBits->uxEventBits &= ~uxBitsToClear;
  512. }
  513. taskEXIT_CRITICAL( &pxEventBits->eventGroupMux );
  514. return pxEventBits->uxEventBits;
  515. }
  516. /*-----------------------------------------------------------*/
  517. void vEventGroupDelete( EventGroupHandle_t xEventGroup )
  518. {
  519. EventGroup_t *pxEventBits = xEventGroup;
  520. const List_t *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );
  521. traceEVENT_GROUP_DELETE( xEventGroup );
  522. taskENTER_CRITICAL( &pxEventBits->eventGroupMux );
  523. {
  524. while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )
  525. {
  526. /* Unblock the task, returning 0 as the event list is being deleted
  527. and cannot therefore have any bits set. */
  528. configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( const ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );
  529. xTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );
  530. }
  531. }
  532. taskEXIT_CRITICAL( &pxEventBits->eventGroupMux );
  533. #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
  534. {
  535. /* The event group can only have been allocated dynamically - free
  536. it again. */
  537. vPortFree( pxEventBits );
  538. }
  539. #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  540. {
  541. /* The event group could have been allocated statically or
  542. dynamically, so check before attempting to free the memory. */
  543. if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
  544. {
  545. vPortFree( pxEventBits );
  546. }
  547. else
  548. {
  549. mtCOVERAGE_TEST_MARKER();
  550. }
  551. }
  552. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  553. }
  554. /*-----------------------------------------------------------*/
  555. /* For internal use only - execute a 'set bits' command that was pended from
  556. an interrupt. */
  557. void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet )
  558. {
  559. ( void ) xEventGroupSetBits( pvEventGroup, ( EventBits_t ) ulBitsToSet ); /*lint !e9079 Can't avoid cast to void* as a generic timer callback prototype. Callback casts back to original type so safe. */
  560. }
  561. /*-----------------------------------------------------------*/
  562. /* For internal use only - execute a 'clear bits' command that was pended from
  563. an interrupt. */
  564. void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear )
  565. {
  566. ( void ) xEventGroupClearBits( pvEventGroup, ( EventBits_t ) ulBitsToClear ); /*lint !e9079 Can't avoid cast to void* as a generic timer callback prototype. Callback casts back to original type so safe. */
  567. }
  568. /*-----------------------------------------------------------*/
  569. static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits )
  570. {
  571. BaseType_t xWaitConditionMet = pdFALSE;
  572. if( xWaitForAllBits == pdFALSE )
  573. {
  574. /* Task only has to wait for one bit within uxBitsToWaitFor to be
  575. set. Is one already set? */
  576. if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( EventBits_t ) 0 )
  577. {
  578. xWaitConditionMet = pdTRUE;
  579. }
  580. else
  581. {
  582. mtCOVERAGE_TEST_MARKER();
  583. }
  584. }
  585. else
  586. {
  587. /* Task has to wait for all the bits in uxBitsToWaitFor to be set.
  588. Are they set already? */
  589. if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )
  590. {
  591. xWaitConditionMet = pdTRUE;
  592. }
  593. else
  594. {
  595. mtCOVERAGE_TEST_MARKER();
  596. }
  597. }
  598. return xWaitConditionMet;
  599. }
  600. /*-----------------------------------------------------------*/
  601. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )
  602. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken )
  603. {
  604. BaseType_t xReturn;
  605. traceEVENT_GROUP_SET_BITS_FROM_ISR( xEventGroup, uxBitsToSet );
  606. xReturn = xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken ); /*lint !e9087 Can't avoid cast to void* as a generic callback function not specific to this use case. Callback casts back to original type so safe. */
  607. return xReturn;
  608. }
  609. #endif
  610. /*-----------------------------------------------------------*/
  611. #if (configUSE_TRACE_FACILITY == 1)
  612. UBaseType_t uxEventGroupGetNumber( void* xEventGroup )
  613. {
  614. UBaseType_t xReturn;
  615. EventGroup_t const *pxEventBits = ( EventGroup_t * ) xEventGroup; /*lint !e9087 !e9079 EventGroupHandle_t is a pointer to an EventGroup_t, but EventGroupHandle_t is kept opaque outside of this file for data hiding purposes. */
  616. if( xEventGroup == NULL )
  617. {
  618. xReturn = 0;
  619. }
  620. else
  621. {
  622. xReturn = pxEventBits->uxEventGroupNumber;
  623. }
  624. return xReturn;
  625. }
  626. #endif /* configUSE_TRACE_FACILITY */
  627. /*-----------------------------------------------------------*/
  628. #if ( configUSE_TRACE_FACILITY == 1 )
  629. void vEventGroupSetNumber( void * xEventGroup, UBaseType_t uxEventGroupNumber )
  630. {
  631. ( ( EventGroup_t * ) xEventGroup )->uxEventGroupNumber = uxEventGroupNumber; /*lint !e9087 !e9079 EventGroupHandle_t is a pointer to an EventGroup_t, but EventGroupHandle_t is kept opaque outside of this file for data hiding purposes. */
  632. }
  633. #endif /* configUSE_TRACE_FACILITY */
  634. /*-----------------------------------------------------------*/