event_groups.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * FreeRTOS Kernel V11.1.0
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. #ifndef EVENT_GROUPS_H
  29. #define EVENT_GROUPS_H
  30. #ifndef INC_FREERTOS_H
  31. #error "include FreeRTOS.h" must appear in source files before "include event_groups.h"
  32. #endif
  33. /* FreeRTOS includes. */
  34. #include "timers.h"
  35. /* The following bit fields convey control information in a task's event list
  36. * item value. It is important they don't clash with the
  37. * taskEVENT_LIST_ITEM_VALUE_IN_USE definition. */
  38. #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
  39. #define eventCLEAR_EVENTS_ON_EXIT_BIT ( ( uint16_t ) 0x0100U )
  40. #define eventUNBLOCKED_DUE_TO_BIT_SET ( ( uint16_t ) 0x0200U )
  41. #define eventWAIT_FOR_ALL_BITS ( ( uint16_t ) 0x0400U )
  42. #define eventEVENT_BITS_CONTROL_BYTES ( ( uint16_t ) 0xff00U )
  43. #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
  44. #define eventCLEAR_EVENTS_ON_EXIT_BIT ( ( uint32_t ) 0x01000000U )
  45. #define eventUNBLOCKED_DUE_TO_BIT_SET ( ( uint32_t ) 0x02000000U )
  46. #define eventWAIT_FOR_ALL_BITS ( ( uint32_t ) 0x04000000U )
  47. #define eventEVENT_BITS_CONTROL_BYTES ( ( uint32_t ) 0xff000000U )
  48. #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
  49. #define eventCLEAR_EVENTS_ON_EXIT_BIT ( ( uint64_t ) 0x0100000000000000U )
  50. #define eventUNBLOCKED_DUE_TO_BIT_SET ( ( uint64_t ) 0x0200000000000000U )
  51. #define eventWAIT_FOR_ALL_BITS ( ( uint64_t ) 0x0400000000000000U )
  52. #define eventEVENT_BITS_CONTROL_BYTES ( ( uint64_t ) 0xff00000000000000U )
  53. #endif /* if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS ) */
  54. /* *INDENT-OFF* */
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /* *INDENT-ON* */
  59. /**
  60. * An event group is a collection of bits to which an application can assign a
  61. * meaning. For example, an application may create an event group to convey
  62. * the status of various CAN bus related events in which bit 0 might mean "A CAN
  63. * message has been received and is ready for processing", bit 1 might mean "The
  64. * application has queued a message that is ready for sending onto the CAN
  65. * network", and bit 2 might mean "It is time to send a SYNC message onto the
  66. * CAN network" etc. A task can then test the bit values to see which events
  67. * are active, and optionally enter the Blocked state to wait for a specified
  68. * bit or a group of specified bits to be active. To continue the CAN bus
  69. * example, a CAN controlling task can enter the Blocked state (and therefore
  70. * not consume any processing time) until either bit 0, bit 1 or bit 2 are
  71. * active, at which time the bit that was actually active would inform the task
  72. * which action it had to take (process a received message, send a message, or
  73. * send a SYNC).
  74. *
  75. * The event groups implementation contains intelligence to avoid race
  76. * conditions that would otherwise occur were an application to use a simple
  77. * variable for the same purpose. This is particularly important with respect
  78. * to when a bit within an event group is to be cleared, and when bits have to
  79. * be set and then tested atomically - as is the case where event groups are
  80. * used to create a synchronisation point between multiple tasks (a
  81. * 'rendezvous').
  82. */
  83. /**
  84. * event_groups.h
  85. *
  86. * Type by which event groups are referenced. For example, a call to
  87. * xEventGroupCreate() returns an EventGroupHandle_t variable that can then
  88. * be used as a parameter to other event group functions.
  89. *
  90. * \defgroup EventGroupHandle_t EventGroupHandle_t
  91. * \ingroup EventGroup
  92. */
  93. struct EventGroupDef_t;
  94. typedef struct EventGroupDef_t * EventGroupHandle_t;
  95. /*
  96. * The type that holds event bits always matches TickType_t - therefore the
  97. * number of bits it holds is set by configTICK_TYPE_WIDTH_IN_BITS (16 bits if set to 0,
  98. * 32 bits if set to 1, 64 bits if set to 2.
  99. *
  100. * \defgroup EventBits_t EventBits_t
  101. * \ingroup EventGroup
  102. */
  103. typedef TickType_t EventBits_t;
  104. /**
  105. * event_groups.h
  106. * @code{c}
  107. * EventGroupHandle_t xEventGroupCreate( void );
  108. * @endcode
  109. *
  110. * Create a new event group.
  111. *
  112. * Internally, within the FreeRTOS implementation, event groups use a [small]
  113. * block of memory, in which the event group's structure is stored. If an event
  114. * groups is created using xEventGroupCreate() then the required memory is
  115. * automatically dynamically allocated inside the xEventGroupCreate() function.
  116. * (see https://www.FreeRTOS.org/a00111.html). If an event group is created
  117. * using xEventGroupCreateStatic() then the application writer must instead
  118. * provide the memory that will get used by the event group.
  119. * xEventGroupCreateStatic() therefore allows an event group to be created
  120. * without using any dynamic memory allocation.
  121. *
  122. * Although event groups are not related to ticks, for internal implementation
  123. * reasons the number of bits available for use in an event group is dependent
  124. * on the configTICK_TYPE_WIDTH_IN_BITS setting in FreeRTOSConfig.h. If
  125. * configTICK_TYPE_WIDTH_IN_BITS is 0 then each event group contains 8 usable bits (bit
  126. * 0 to bit 7). If configTICK_TYPE_WIDTH_IN_BITS is set to 1 then each event group has
  127. * 24 usable bits (bit 0 to bit 23). If configTICK_TYPE_WIDTH_IN_BITS is set to 2 then
  128. * each event group has 56 usable bits (bit 0 to bit 53). The EventBits_t type
  129. * is used to store event bits within an event group.
  130. *
  131. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for xEventGroupCreate()
  132. * to be available.
  133. *
  134. * @return If the event group was created then a handle to the event group is
  135. * returned. If there was insufficient FreeRTOS heap available to create the
  136. * event group then NULL is returned. See https://www.FreeRTOS.org/a00111.html
  137. *
  138. * Example usage:
  139. * @code{c}
  140. * // Declare a variable to hold the created event group.
  141. * EventGroupHandle_t xCreatedEventGroup;
  142. *
  143. * // Attempt to create the event group.
  144. * xCreatedEventGroup = xEventGroupCreate();
  145. *
  146. * // Was the event group created successfully?
  147. * if( xCreatedEventGroup == NULL )
  148. * {
  149. * // The event group was not created because there was insufficient
  150. * // FreeRTOS heap available.
  151. * }
  152. * else
  153. * {
  154. * // The event group was created.
  155. * }
  156. * @endcode
  157. * \defgroup xEventGroupCreate xEventGroupCreate
  158. * \ingroup EventGroup
  159. */
  160. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  161. EventGroupHandle_t xEventGroupCreate( void ) PRIVILEGED_FUNCTION;
  162. #endif
  163. /**
  164. * event_groups.h
  165. * @code{c}
  166. * EventGroupHandle_t xEventGroupCreateStatic( EventGroupHandle_t * pxEventGroupBuffer );
  167. * @endcode
  168. *
  169. * Create a new event group.
  170. *
  171. * Internally, within the FreeRTOS implementation, event groups use a [small]
  172. * block of memory, in which the event group's structure is stored. If an event
  173. * groups is created using xEventGroupCreate() then the required memory is
  174. * automatically dynamically allocated inside the xEventGroupCreate() function.
  175. * (see https://www.FreeRTOS.org/a00111.html). If an event group is created
  176. * using xEventGroupCreateStatic() then the application writer must instead
  177. * provide the memory that will get used by the event group.
  178. * xEventGroupCreateStatic() therefore allows an event group to be created
  179. * without using any dynamic memory allocation.
  180. *
  181. * Although event groups are not related to ticks, for internal implementation
  182. * reasons the number of bits available for use in an event group is dependent
  183. * on the configTICK_TYPE_WIDTH_IN_BITS setting in FreeRTOSConfig.h. If
  184. * configTICK_TYPE_WIDTH_IN_BITS is 0 then each event group contains 8 usable bits (bit
  185. * 0 to bit 7). If configTICK_TYPE_WIDTH_IN_BITS is set to 1 then each event group has
  186. * 24 usable bits (bit 0 to bit 23). If configTICK_TYPE_WIDTH_IN_BITS is set to 2 then
  187. * each event group has 56 usable bits (bit 0 to bit 53). The EventBits_t type
  188. * is used to store event bits within an event group.
  189. *
  190. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for xEventGroupCreateStatic()
  191. * to be available.
  192. *
  193. * @param pxEventGroupBuffer pxEventGroupBuffer must point to a variable of type
  194. * StaticEventGroup_t, which will be then be used to hold the event group's data
  195. * structures, removing the need for the memory to be allocated dynamically.
  196. *
  197. * @return If the event group was created then a handle to the event group is
  198. * returned. If pxEventGroupBuffer was NULL then NULL is returned.
  199. *
  200. * Example usage:
  201. * @code{c}
  202. * // StaticEventGroup_t is a publicly accessible structure that has the same
  203. * // size and alignment requirements as the real event group structure. It is
  204. * // provided as a mechanism for applications to know the size of the event
  205. * // group (which is dependent on the architecture and configuration file
  206. * // settings) without breaking the strict data hiding policy by exposing the
  207. * // real event group internals. This StaticEventGroup_t variable is passed
  208. * // into the xSemaphoreCreateEventGroupStatic() function and is used to store
  209. * // the event group's data structures
  210. * StaticEventGroup_t xEventGroupBuffer;
  211. *
  212. * // Create the event group without dynamically allocating any memory.
  213. * xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );
  214. * @endcode
  215. */
  216. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  217. EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t * pxEventGroupBuffer ) PRIVILEGED_FUNCTION;
  218. #endif
  219. /**
  220. * event_groups.h
  221. * @code{c}
  222. * EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  223. * const EventBits_t uxBitsToWaitFor,
  224. * const BaseType_t xClearOnExit,
  225. * const BaseType_t xWaitForAllBits,
  226. * const TickType_t xTicksToWait );
  227. * @endcode
  228. *
  229. * [Potentially] block to wait for one or more bits to be set within a
  230. * previously created event group.
  231. *
  232. * This function cannot be called from an interrupt.
  233. *
  234. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for xEventGroupWaitBits()
  235. * to be available.
  236. *
  237. * @param xEventGroup The event group in which the bits are being tested. The
  238. * event group must have previously been created using a call to
  239. * xEventGroupCreate().
  240. *
  241. * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
  242. * inside the event group. For example, to wait for bit 0 and/or bit 2 set
  243. * uxBitsToWaitFor to 0x05. To wait for bits 0 and/or bit 1 and/or bit 2 set
  244. * uxBitsToWaitFor to 0x07. Etc.
  245. *
  246. * @param xClearOnExit If xClearOnExit is set to pdTRUE then any bits within
  247. * uxBitsToWaitFor that are set within the event group will be cleared before
  248. * xEventGroupWaitBits() returns if the wait condition was met (if the function
  249. * returns for a reason other than a timeout). If xClearOnExit is set to
  250. * pdFALSE then the bits set in the event group are not altered when the call to
  251. * xEventGroupWaitBits() returns.
  252. *
  253. * @param xWaitForAllBits If xWaitForAllBits is set to pdTRUE then
  254. * xEventGroupWaitBits() will return when either all the bits in uxBitsToWaitFor
  255. * are set or the specified block time expires. If xWaitForAllBits is set to
  256. * pdFALSE then xEventGroupWaitBits() will return when any one of the bits set
  257. * in uxBitsToWaitFor is set or the specified block time expires. The block
  258. * time is specified by the xTicksToWait parameter.
  259. *
  260. * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
  261. * for one/all (depending on the xWaitForAllBits value) of the bits specified by
  262. * uxBitsToWaitFor to become set. A value of portMAX_DELAY can be used to block
  263. * indefinitely (provided INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
  264. *
  265. * @return The value of the event group at the time either the bits being waited
  266. * for became set, or the block time expired. Test the return value to know
  267. * which bits were set. If xEventGroupWaitBits() returned because its timeout
  268. * expired then not all the bits being waited for will be set. If
  269. * xEventGroupWaitBits() returned because the bits it was waiting for were set
  270. * then the returned value is the event group value before any bits were
  271. * automatically cleared in the case that xClearOnExit parameter was set to
  272. * pdTRUE.
  273. *
  274. * Example usage:
  275. * @code{c}
  276. * #define BIT_0 ( 1 << 0 )
  277. * #define BIT_4 ( 1 << 4 )
  278. *
  279. * void aFunction( EventGroupHandle_t xEventGroup )
  280. * {
  281. * EventBits_t uxBits;
  282. * const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
  283. *
  284. * // Wait a maximum of 100ms for either bit 0 or bit 4 to be set within
  285. * // the event group. Clear the bits before exiting.
  286. * uxBits = xEventGroupWaitBits(
  287. * xEventGroup, // The event group being tested.
  288. * BIT_0 | BIT_4, // The bits within the event group to wait for.
  289. * pdTRUE, // BIT_0 and BIT_4 should be cleared before returning.
  290. * pdFALSE, // Don't wait for both bits, either bit will do.
  291. * xTicksToWait ); // Wait a maximum of 100ms for either bit to be set.
  292. *
  293. * if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  294. * {
  295. * // xEventGroupWaitBits() returned because both bits were set.
  296. * }
  297. * else if( ( uxBits & BIT_0 ) != 0 )
  298. * {
  299. * // xEventGroupWaitBits() returned because just BIT_0 was set.
  300. * }
  301. * else if( ( uxBits & BIT_4 ) != 0 )
  302. * {
  303. * // xEventGroupWaitBits() returned because just BIT_4 was set.
  304. * }
  305. * else
  306. * {
  307. * // xEventGroupWaitBits() returned because xTicksToWait ticks passed
  308. * // without either BIT_0 or BIT_4 becoming set.
  309. * }
  310. * }
  311. * @endcode
  312. * \defgroup xEventGroupWaitBits xEventGroupWaitBits
  313. * \ingroup EventGroup
  314. */
  315. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  316. const EventBits_t uxBitsToWaitFor,
  317. const BaseType_t xClearOnExit,
  318. const BaseType_t xWaitForAllBits,
  319. TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  320. /**
  321. * event_groups.h
  322. * @code{c}
  323. * EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear );
  324. * @endcode
  325. *
  326. * Clear bits within an event group. This function cannot be called from an
  327. * interrupt.
  328. *
  329. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for xEventGroupClearBits()
  330. * to be available.
  331. *
  332. * @param xEventGroup The event group in which the bits are to be cleared.
  333. *
  334. * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear
  335. * in the event group. For example, to clear bit 3 only, set uxBitsToClear to
  336. * 0x08. To clear bit 3 and bit 0 set uxBitsToClear to 0x09.
  337. *
  338. * @return The value of the event group before the specified bits were cleared.
  339. *
  340. * Example usage:
  341. * @code{c}
  342. * #define BIT_0 ( 1 << 0 )
  343. * #define BIT_4 ( 1 << 4 )
  344. *
  345. * void aFunction( EventGroupHandle_t xEventGroup )
  346. * {
  347. * EventBits_t uxBits;
  348. *
  349. * // Clear bit 0 and bit 4 in xEventGroup.
  350. * uxBits = xEventGroupClearBits(
  351. * xEventGroup, // The event group being updated.
  352. * BIT_0 | BIT_4 );// The bits being cleared.
  353. *
  354. * if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  355. * {
  356. * // Both bit 0 and bit 4 were set before xEventGroupClearBits() was
  357. * // called. Both will now be clear (not set).
  358. * }
  359. * else if( ( uxBits & BIT_0 ) != 0 )
  360. * {
  361. * // Bit 0 was set before xEventGroupClearBits() was called. It will
  362. * // now be clear.
  363. * }
  364. * else if( ( uxBits & BIT_4 ) != 0 )
  365. * {
  366. * // Bit 4 was set before xEventGroupClearBits() was called. It will
  367. * // now be clear.
  368. * }
  369. * else
  370. * {
  371. * // Neither bit 0 nor bit 4 were set in the first place.
  372. * }
  373. * }
  374. * @endcode
  375. * \defgroup xEventGroupClearBits xEventGroupClearBits
  376. * \ingroup EventGroup
  377. */
  378. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup,
  379. const EventBits_t uxBitsToClear ) PRIVILEGED_FUNCTION;
  380. /**
  381. * event_groups.h
  382. * @code{c}
  383. * BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
  384. * @endcode
  385. *
  386. * A version of xEventGroupClearBits() that can be called from an interrupt.
  387. *
  388. * Setting bits in an event group is not a deterministic operation because there
  389. * are an unknown number of tasks that may be waiting for the bit or bits being
  390. * set. FreeRTOS does not allow nondeterministic operations to be performed
  391. * while interrupts are disabled, so protects event groups that are accessed
  392. * from tasks by suspending the scheduler rather than disabling interrupts. As
  393. * a result event groups cannot be accessed directly from an interrupt service
  394. * routine. Therefore xEventGroupClearBitsFromISR() sends a message to the
  395. * timer task to have the clear operation performed in the context of the timer
  396. * task.
  397. *
  398. * @note If this function returns pdPASS then the timer task is ready to run
  399. * and a portYIELD_FROM_ISR(pdTRUE) should be executed to perform the needed
  400. * clear on the event group. This behavior is different from
  401. * xEventGroupSetBitsFromISR because the parameter xHigherPriorityTaskWoken is
  402. * not present.
  403. *
  404. * @param xEventGroup The event group in which the bits are to be cleared.
  405. *
  406. * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear.
  407. * For example, to clear bit 3 only, set uxBitsToClear to 0x08. To clear bit 3
  408. * and bit 0 set uxBitsToClear to 0x09.
  409. *
  410. * @return If the request to execute the function was posted successfully then
  411. * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
  412. * if the timer service queue was full.
  413. *
  414. * Example usage:
  415. * @code{c}
  416. * #define BIT_0 ( 1 << 0 )
  417. * #define BIT_4 ( 1 << 4 )
  418. *
  419. * // An event group which it is assumed has already been created by a call to
  420. * // xEventGroupCreate().
  421. * EventGroupHandle_t xEventGroup;
  422. *
  423. * void anInterruptHandler( void )
  424. * {
  425. * // Clear bit 0 and bit 4 in xEventGroup.
  426. * xResult = xEventGroupClearBitsFromISR(
  427. * xEventGroup, // The event group being updated.
  428. * BIT_0 | BIT_4 ); // The bits being set.
  429. *
  430. * if( xResult == pdPASS )
  431. * {
  432. * // The message was posted successfully.
  433. * portYIELD_FROM_ISR(pdTRUE);
  434. * }
  435. * }
  436. * @endcode
  437. * \defgroup xEventGroupClearBitsFromISR xEventGroupClearBitsFromISR
  438. * \ingroup EventGroup
  439. */
  440. #if ( configUSE_TRACE_FACILITY == 1 )
  441. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup,
  442. const EventBits_t uxBitsToClear ) PRIVILEGED_FUNCTION;
  443. #else
  444. #define xEventGroupClearBitsFromISR( xEventGroup, uxBitsToClear ) \
  445. xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) ( xEventGroup ), ( uint32_t ) ( uxBitsToClear ), NULL )
  446. #endif
  447. /**
  448. * event_groups.h
  449. * @code{c}
  450. * EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
  451. * @endcode
  452. *
  453. * Set bits within an event group.
  454. * This function cannot be called from an interrupt. xEventGroupSetBitsFromISR()
  455. * is a version that can be called from an interrupt.
  456. *
  457. * Setting bits in an event group will automatically unblock tasks that are
  458. * blocked waiting for the bits.
  459. *
  460. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for xEventGroupSetBits()
  461. * to be available.
  462. *
  463. * @param xEventGroup The event group in which the bits are to be set.
  464. *
  465. * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
  466. * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
  467. * and bit 0 set uxBitsToSet to 0x09.
  468. *
  469. * @return The value of the event group at the time the call to
  470. * xEventGroupSetBits() returns. There are two reasons why the returned value
  471. * might have the bits specified by the uxBitsToSet parameter cleared. First,
  472. * if setting a bit results in a task that was waiting for the bit leaving the
  473. * blocked state then it is possible the bit will be cleared automatically
  474. * (see the xClearBitOnExit parameter of xEventGroupWaitBits()). Second, any
  475. * unblocked (or otherwise Ready state) task that has a priority above that of
  476. * the task that called xEventGroupSetBits() will execute and may change the
  477. * event group value before the call to xEventGroupSetBits() returns.
  478. *
  479. * Example usage:
  480. * @code{c}
  481. * #define BIT_0 ( 1 << 0 )
  482. * #define BIT_4 ( 1 << 4 )
  483. *
  484. * void aFunction( EventGroupHandle_t xEventGroup )
  485. * {
  486. * EventBits_t uxBits;
  487. *
  488. * // Set bit 0 and bit 4 in xEventGroup.
  489. * uxBits = xEventGroupSetBits(
  490. * xEventGroup, // The event group being updated.
  491. * BIT_0 | BIT_4 );// The bits being set.
  492. *
  493. * if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  494. * {
  495. * // Both bit 0 and bit 4 remained set when the function returned.
  496. * }
  497. * else if( ( uxBits & BIT_0 ) != 0 )
  498. * {
  499. * // Bit 0 remained set when the function returned, but bit 4 was
  500. * // cleared. It might be that bit 4 was cleared automatically as a
  501. * // task that was waiting for bit 4 was removed from the Blocked
  502. * // state.
  503. * }
  504. * else if( ( uxBits & BIT_4 ) != 0 )
  505. * {
  506. * // Bit 4 remained set when the function returned, but bit 0 was
  507. * // cleared. It might be that bit 0 was cleared automatically as a
  508. * // task that was waiting for bit 0 was removed from the Blocked
  509. * // state.
  510. * }
  511. * else
  512. * {
  513. * // Neither bit 0 nor bit 4 remained set. It might be that a task
  514. * // was waiting for both of the bits to be set, and the bits were
  515. * // cleared as the task left the Blocked state.
  516. * }
  517. * }
  518. * @endcode
  519. * \defgroup xEventGroupSetBits xEventGroupSetBits
  520. * \ingroup EventGroup
  521. */
  522. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
  523. const EventBits_t uxBitsToSet ) PRIVILEGED_FUNCTION;
  524. /**
  525. * event_groups.h
  526. * @code{c}
  527. * BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken );
  528. * @endcode
  529. *
  530. * A version of xEventGroupSetBits() that can be called from an interrupt.
  531. *
  532. * Setting bits in an event group is not a deterministic operation because there
  533. * are an unknown number of tasks that may be waiting for the bit or bits being
  534. * set. FreeRTOS does not allow nondeterministic operations to be performed in
  535. * interrupts or from critical sections. Therefore xEventGroupSetBitsFromISR()
  536. * sends a message to the timer task to have the set operation performed in the
  537. * context of the timer task - where a scheduler lock is used in place of a
  538. * critical section.
  539. *
  540. * @param xEventGroup The event group in which the bits are to be set.
  541. *
  542. * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
  543. * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
  544. * and bit 0 set uxBitsToSet to 0x09.
  545. *
  546. * @param pxHigherPriorityTaskWoken As mentioned above, calling this function
  547. * will result in a message being sent to the timer daemon task. If the
  548. * priority of the timer daemon task is higher than the priority of the
  549. * currently running task (the task the interrupt interrupted) then
  550. * *pxHigherPriorityTaskWoken will be set to pdTRUE by
  551. * xEventGroupSetBitsFromISR(), indicating that a context switch should be
  552. * requested before the interrupt exits. For that reason
  553. * *pxHigherPriorityTaskWoken must be initialised to pdFALSE. See the
  554. * example code below.
  555. *
  556. * @return If the request to execute the function was posted successfully then
  557. * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
  558. * if the timer service queue was full.
  559. *
  560. * Example usage:
  561. * @code{c}
  562. * #define BIT_0 ( 1 << 0 )
  563. * #define BIT_4 ( 1 << 4 )
  564. *
  565. * // An event group which it is assumed has already been created by a call to
  566. * // xEventGroupCreate().
  567. * EventGroupHandle_t xEventGroup;
  568. *
  569. * void anInterruptHandler( void )
  570. * {
  571. * BaseType_t xHigherPriorityTaskWoken, xResult;
  572. *
  573. * // xHigherPriorityTaskWoken must be initialised to pdFALSE.
  574. * xHigherPriorityTaskWoken = pdFALSE;
  575. *
  576. * // Set bit 0 and bit 4 in xEventGroup.
  577. * xResult = xEventGroupSetBitsFromISR(
  578. * xEventGroup, // The event group being updated.
  579. * BIT_0 | BIT_4 // The bits being set.
  580. * &xHigherPriorityTaskWoken );
  581. *
  582. * // Was the message posted successfully?
  583. * if( xResult == pdPASS )
  584. * {
  585. * // If xHigherPriorityTaskWoken is now set to pdTRUE then a context
  586. * // switch should be requested. The macro used is port specific and
  587. * // will be either portYIELD_FROM_ISR() or portEND_SWITCHING_ISR() -
  588. * // refer to the documentation page for the port being used.
  589. * portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  590. * }
  591. * }
  592. * @endcode
  593. * \defgroup xEventGroupSetBitsFromISR xEventGroupSetBitsFromISR
  594. * \ingroup EventGroup
  595. */
  596. #if ( configUSE_TRACE_FACILITY == 1 )
  597. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup,
  598. const EventBits_t uxBitsToSet,
  599. BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
  600. #else
  601. #define xEventGroupSetBitsFromISR( xEventGroup, uxBitsToSet, pxHigherPriorityTaskWoken ) \
  602. xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) ( xEventGroup ), ( uint32_t ) ( uxBitsToSet ), ( pxHigherPriorityTaskWoken ) )
  603. #endif
  604. /**
  605. * event_groups.h
  606. * @code{c}
  607. * EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
  608. * const EventBits_t uxBitsToSet,
  609. * const EventBits_t uxBitsToWaitFor,
  610. * TickType_t xTicksToWait );
  611. * @endcode
  612. *
  613. * Atomically set bits within an event group, then wait for a combination of
  614. * bits to be set within the same event group. This functionality is typically
  615. * used to synchronise multiple tasks, where each task has to wait for the other
  616. * tasks to reach a synchronisation point before proceeding.
  617. *
  618. * This function cannot be used from an interrupt.
  619. *
  620. * The function will return before its block time expires if the bits specified
  621. * by the uxBitsToWait parameter are set, or become set within that time. In
  622. * this case all the bits specified by uxBitsToWait will be automatically
  623. * cleared before the function returns.
  624. *
  625. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for xEventGroupSync()
  626. * to be available.
  627. *
  628. * @param xEventGroup The event group in which the bits are being tested. The
  629. * event group must have previously been created using a call to
  630. * xEventGroupCreate().
  631. *
  632. * @param uxBitsToSet The bits to set in the event group before determining
  633. * if, and possibly waiting for, all the bits specified by the uxBitsToWait
  634. * parameter are set.
  635. *
  636. * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
  637. * inside the event group. For example, to wait for bit 0 and bit 2 set
  638. * uxBitsToWaitFor to 0x05. To wait for bits 0 and bit 1 and bit 2 set
  639. * uxBitsToWaitFor to 0x07. Etc.
  640. *
  641. * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
  642. * for all of the bits specified by uxBitsToWaitFor to become set.
  643. *
  644. * @return The value of the event group at the time either the bits being waited
  645. * for became set, or the block time expired. Test the return value to know
  646. * which bits were set. If xEventGroupSync() returned because its timeout
  647. * expired then not all the bits being waited for will be set. If
  648. * xEventGroupSync() returned because all the bits it was waiting for were
  649. * set then the returned value is the event group value before any bits were
  650. * automatically cleared.
  651. *
  652. * Example usage:
  653. * @code{c}
  654. * // Bits used by the three tasks.
  655. * #define TASK_0_BIT ( 1 << 0 )
  656. * #define TASK_1_BIT ( 1 << 1 )
  657. * #define TASK_2_BIT ( 1 << 2 )
  658. *
  659. * #define ALL_SYNC_BITS ( TASK_0_BIT | TASK_1_BIT | TASK_2_BIT )
  660. *
  661. * // Use an event group to synchronise three tasks. It is assumed this event
  662. * // group has already been created elsewhere.
  663. * EventGroupHandle_t xEventBits;
  664. *
  665. * void vTask0( void *pvParameters )
  666. * {
  667. * EventBits_t uxReturn;
  668. * TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
  669. *
  670. * for( ;; )
  671. * {
  672. * // Perform task functionality here.
  673. *
  674. * // Set bit 0 in the event flag to note this task has reached the
  675. * // sync point. The other two tasks will set the other two bits defined
  676. * // by ALL_SYNC_BITS. All three tasks have reached the synchronisation
  677. * // point when all the ALL_SYNC_BITS are set. Wait a maximum of 100ms
  678. * // for this to happen.
  679. * uxReturn = xEventGroupSync( xEventBits, TASK_0_BIT, ALL_SYNC_BITS, xTicksToWait );
  680. *
  681. * if( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
  682. * {
  683. * // All three tasks reached the synchronisation point before the call
  684. * // to xEventGroupSync() timed out.
  685. * }
  686. * }
  687. * }
  688. *
  689. * void vTask1( void *pvParameters )
  690. * {
  691. * for( ;; )
  692. * {
  693. * // Perform task functionality here.
  694. *
  695. * // Set bit 1 in the event flag to note this task has reached the
  696. * // synchronisation point. The other two tasks will set the other two
  697. * // bits defined by ALL_SYNC_BITS. All three tasks have reached the
  698. * // synchronisation point when all the ALL_SYNC_BITS are set. Wait
  699. * // indefinitely for this to happen.
  700. * xEventGroupSync( xEventBits, TASK_1_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  701. *
  702. * // xEventGroupSync() was called with an indefinite block time, so
  703. * // this task will only reach here if the synchronisation was made by all
  704. * // three tasks, so there is no need to test the return value.
  705. * }
  706. * }
  707. *
  708. * void vTask2( void *pvParameters )
  709. * {
  710. * for( ;; )
  711. * {
  712. * // Perform task functionality here.
  713. *
  714. * // Set bit 2 in the event flag to note this task has reached the
  715. * // synchronisation point. The other two tasks will set the other two
  716. * // bits defined by ALL_SYNC_BITS. All three tasks have reached the
  717. * // synchronisation point when all the ALL_SYNC_BITS are set. Wait
  718. * // indefinitely for this to happen.
  719. * xEventGroupSync( xEventBits, TASK_2_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  720. *
  721. * // xEventGroupSync() was called with an indefinite block time, so
  722. * // this task will only reach here if the synchronisation was made by all
  723. * // three tasks, so there is no need to test the return value.
  724. * }
  725. * }
  726. *
  727. * @endcode
  728. * \defgroup xEventGroupSync xEventGroupSync
  729. * \ingroup EventGroup
  730. */
  731. EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
  732. const EventBits_t uxBitsToSet,
  733. const EventBits_t uxBitsToWaitFor,
  734. TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  735. /**
  736. * event_groups.h
  737. * @code{c}
  738. * EventBits_t xEventGroupGetBits( EventGroupHandle_t xEventGroup );
  739. * @endcode
  740. *
  741. * Returns the current value of the bits in an event group. This function
  742. * cannot be used from an interrupt.
  743. *
  744. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for xEventGroupGetBits()
  745. * to be available.
  746. *
  747. * @param xEventGroup The event group being queried.
  748. *
  749. * @return The event group bits at the time xEventGroupGetBits() was called.
  750. *
  751. * \defgroup xEventGroupGetBits xEventGroupGetBits
  752. * \ingroup EventGroup
  753. */
  754. #define xEventGroupGetBits( xEventGroup ) xEventGroupClearBits( ( xEventGroup ), 0 )
  755. /**
  756. * event_groups.h
  757. * @code{c}
  758. * EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup );
  759. * @endcode
  760. *
  761. * A version of xEventGroupGetBits() that can be called from an ISR.
  762. *
  763. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for xEventGroupGetBitsFromISR()
  764. * to be available.
  765. *
  766. * @param xEventGroup The event group being queried.
  767. *
  768. * @return The event group bits at the time xEventGroupGetBitsFromISR() was called.
  769. *
  770. * \defgroup xEventGroupGetBitsFromISR xEventGroupGetBitsFromISR
  771. * \ingroup EventGroup
  772. */
  773. EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup ) PRIVILEGED_FUNCTION;
  774. /**
  775. * event_groups.h
  776. * @code{c}
  777. * void xEventGroupDelete( EventGroupHandle_t xEventGroup );
  778. * @endcode
  779. *
  780. * Delete an event group that was previously created by a call to
  781. * xEventGroupCreate(). Tasks that are blocked on the event group will be
  782. * unblocked and obtain 0 as the event group's value.
  783. *
  784. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for vEventGroupDelete()
  785. * to be available.
  786. *
  787. * @param xEventGroup The event group being deleted.
  788. */
  789. void vEventGroupDelete( EventGroupHandle_t xEventGroup ) PRIVILEGED_FUNCTION;
  790. /**
  791. * event_groups.h
  792. * @code{c}
  793. * BaseType_t xEventGroupGetStaticBuffer( EventGroupHandle_t xEventGroup,
  794. * StaticEventGroup_t ** ppxEventGroupBuffer );
  795. * @endcode
  796. *
  797. * Retrieve a pointer to a statically created event groups's data structure
  798. * buffer. It is the same buffer that is supplied at the time of creation.
  799. *
  800. * The configUSE_EVENT_GROUPS configuration constant must be set to 1 for xEventGroupGetStaticBuffer()
  801. * to be available.
  802. *
  803. * @param xEventGroup The event group for which to retrieve the buffer.
  804. *
  805. * @param ppxEventGroupBuffer Used to return a pointer to the event groups's
  806. * data structure buffer.
  807. *
  808. * @return pdTRUE if the buffer was retrieved, pdFALSE otherwise.
  809. */
  810. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  811. BaseType_t xEventGroupGetStaticBuffer( EventGroupHandle_t xEventGroup,
  812. StaticEventGroup_t ** ppxEventGroupBuffer ) PRIVILEGED_FUNCTION;
  813. #endif /* configSUPPORT_STATIC_ALLOCATION */
  814. /* For internal use only. */
  815. void vEventGroupSetBitsCallback( void * pvEventGroup,
  816. uint32_t ulBitsToSet ) PRIVILEGED_FUNCTION;
  817. void vEventGroupClearBitsCallback( void * pvEventGroup,
  818. uint32_t ulBitsToClear ) PRIVILEGED_FUNCTION;
  819. #if ( configUSE_TRACE_FACILITY == 1 )
  820. UBaseType_t uxEventGroupGetNumber( void * xEventGroup ) PRIVILEGED_FUNCTION;
  821. void vEventGroupSetNumber( void * xEventGroup,
  822. UBaseType_t uxEventGroupNumber ) PRIVILEGED_FUNCTION;
  823. #endif
  824. /* *INDENT-OFF* */
  825. #ifdef __cplusplus
  826. }
  827. #endif
  828. /* *INDENT-ON* */
  829. #endif /* EVENT_GROUPS_H */