event_groups.h 32 KB

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