event_groups.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. EventGroupHandle_t xEventGroupCreate( void ) PRIVILEGED_FUNCTION;
  138. /**
  139. * event_groups.h
  140. * @code{c}
  141. * EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  142. * const EventBits_t uxBitsToWaitFor,
  143. * const BaseType_t xClearOnExit,
  144. * const BaseType_t xWaitForAllBits,
  145. * const TickType_t xTicksToWait );
  146. * @endcode
  147. *
  148. * [Potentially] block to wait for one or more bits to be set within a
  149. * previously created event group.
  150. *
  151. * This function cannot be called from an interrupt.
  152. *
  153. * @param xEventGroup The event group in which the bits are being tested. The
  154. * event group must have previously been created using a call to
  155. * xEventGroupCreate().
  156. *
  157. * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
  158. * inside the event group. For example, to wait for bit 0 and/or bit 2 set
  159. * uxBitsToWaitFor to 0x05. To wait for bits 0 and/or bit 1 and/or bit 2 set
  160. * uxBitsToWaitFor to 0x07. Etc.
  161. *
  162. * @param xClearOnExit If xClearOnExit is set to pdTRUE then any bits within
  163. * uxBitsToWaitFor that are set within the event group will be cleared before
  164. * xEventGroupWaitBits() returns if the wait condition was met (if the function
  165. * returns for a reason other than a timeout). If xClearOnExit is set to
  166. * pdFALSE then the bits set in the event group are not altered when the call to
  167. * xEventGroupWaitBits() returns.
  168. *
  169. * @param xWaitForAllBits If xWaitForAllBits is set to pdTRUE then
  170. * xEventGroupWaitBits() will return when either all the bits in uxBitsToWaitFor
  171. * are set or the specified block time expires. If xWaitForAllBits is set to
  172. * pdFALSE then xEventGroupWaitBits() will return when any one of the bits set
  173. * in uxBitsToWaitFor is set or the specified block time expires. The block
  174. * time is specified by the xTicksToWait parameter.
  175. *
  176. * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
  177. * for one/all (depending on the xWaitForAllBits value) of the bits specified by
  178. * uxBitsToWaitFor to become set. A value of portMAX_DELAY can be used to block
  179. * indefinitely (provided INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
  180. *
  181. * @return The value of the event group at the time either the bits being waited
  182. * for became set, or the block time expired. Test the return value to know
  183. * which bits were set. If xEventGroupWaitBits() returned because its timeout
  184. * expired then not all the bits being waited for will be set. If
  185. * xEventGroupWaitBits() returned because the bits it was waiting for were set
  186. * then the returned value is the event group value before any bits were
  187. * automatically cleared in the case that xClearOnExit parameter was set to
  188. * pdTRUE.
  189. *
  190. * Example usage:
  191. * @code{c}
  192. * #define BIT_0 ( 1 << 0 )
  193. * #define BIT_4 ( 1 << 4 )
  194. *
  195. * void aFunction( EventGroupHandle_t xEventGroup )
  196. * {
  197. * EventBits_t uxBits;
  198. * const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
  199. *
  200. * // Wait a maximum of 100ms for either bit 0 or bit 4 to be set within
  201. * // the event group. Clear the bits before exiting.
  202. * uxBits = xEventGroupWaitBits(
  203. * xEventGroup, // The event group being tested.
  204. * BIT_0 | BIT_4, // The bits within the event group to wait for.
  205. * pdTRUE, // BIT_0 and BIT_4 should be cleared before returning.
  206. * pdFALSE, // Don't wait for both bits, either bit will do.
  207. * xTicksToWait ); // Wait a maximum of 100ms for either bit to be set.
  208. *
  209. * if ( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  210. * {
  211. * // xEventGroupWaitBits() returned because both bits were set.
  212. * }
  213. * else if ( ( uxBits & BIT_0 ) != 0 )
  214. * {
  215. * // xEventGroupWaitBits() returned because just BIT_0 was set.
  216. * }
  217. * else if ( ( uxBits & BIT_4 ) != 0 )
  218. * {
  219. * // xEventGroupWaitBits() returned because just BIT_4 was set.
  220. * }
  221. * else
  222. * {
  223. * // xEventGroupWaitBits() returned because xTicksToWait ticks passed
  224. * // without either BIT_0 or BIT_4 becoming set.
  225. * }
  226. * }
  227. * @endcode
  228. * \defgroup xEventGroupWaitBits xEventGroupWaitBits
  229. * \ingroup EventGroup
  230. */
  231. EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,
  232. const EventBits_t uxBitsToWaitFor,
  233. const BaseType_t xClearOnExit,
  234. const BaseType_t xWaitForAllBits,
  235. TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  236. /**
  237. * event_groups.h
  238. * @code{c}
  239. * EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear );
  240. * @endcode
  241. *
  242. * Clear bits within an event group. This function cannot be called from an
  243. * interrupt.
  244. *
  245. * @param xEventGroup The event group in which the bits are to be cleared.
  246. *
  247. * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear
  248. * in the event group. For example, to clear bit 3 only, set uxBitsToClear to
  249. * 0x08. To clear bit 3 and bit 0 set uxBitsToClear to 0x09.
  250. *
  251. * @return The value of the event group before the specified bits were cleared.
  252. *
  253. * Example usage:
  254. * @code{c}
  255. * #define BIT_0 ( 1 << 0 )
  256. * #define BIT_4 ( 1 << 4 )
  257. *
  258. * void aFunction( EventGroupHandle_t xEventGroup )
  259. * {
  260. * EventBits_t uxBits;
  261. *
  262. * // Clear bit 0 and bit 4 in xEventGroup.
  263. * uxBits = xEventGroupClearBits(
  264. * xEventGroup, // The event group being updated.
  265. * BIT_0 | BIT_4 );// The bits being cleared.
  266. *
  267. * if ( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  268. * {
  269. * // Both bit 0 and bit 4 were set before xEventGroupClearBits() was
  270. * // called. Both will now be clear (not set).
  271. * }
  272. * else if ( ( uxBits & BIT_0 ) != 0 )
  273. * {
  274. * // Bit 0 was set before xEventGroupClearBits() was called. It will
  275. * // now be clear.
  276. * }
  277. * else if ( ( uxBits & BIT_4 ) != 0 )
  278. * {
  279. * // Bit 4 was set before xEventGroupClearBits() was called. It will
  280. * // now be clear.
  281. * }
  282. * else
  283. * {
  284. * // Neither bit 0 nor bit 4 were set in the first place.
  285. * }
  286. * }
  287. * @endcode
  288. * \defgroup xEventGroupClearBits xEventGroupClearBits
  289. * \ingroup EventGroup
  290. */
  291. EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup,
  292. const EventBits_t uxBitsToClear ) PRIVILEGED_FUNCTION;
  293. /**
  294. * event_groups.h
  295. * @code{c}
  296. * BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
  297. * @endcode
  298. *
  299. * A version of xEventGroupClearBits() that can be called from an interrupt.
  300. *
  301. * Setting bits in an event group is not a deterministic operation because there
  302. * are an unknown number of tasks that may be waiting for the bit or bits being
  303. * set. FreeRTOS does not allow nondeterministic operations to be performed
  304. * while interrupts are disabled, so protects event groups that are accessed
  305. * from tasks by suspending the scheduler rather than disabling interrupts. As
  306. * a result event groups cannot be accessed directly from an interrupt service
  307. * routine. Therefore xEventGroupClearBitsFromISR() sends a message to the
  308. * timer task to have the clear operation performed in the context of the timer
  309. * task.
  310. *
  311. * @note If this function returns pdPASS then the timer task is ready to run
  312. * and a portYIELD_FROM_ISR(pdTRUE) should be executed to perform the needed
  313. * clear on the event group. This behavior is different from
  314. * xEventGroupSetBitsFromISR because the parameter xHigherPriorityTaskWoken is
  315. * not present.
  316. *
  317. * @param xEventGroup The event group in which the bits are to be cleared.
  318. *
  319. * @param uxBitsToClear A bitwise value that indicates the bit or bits to clear.
  320. * For example, to clear bit 3 only, set uxBitsToClear to 0x08. To clear bit 3
  321. * and bit 0 set uxBitsToClear to 0x09.
  322. *
  323. * @return If the request to execute the function was posted successfully then
  324. * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
  325. * if the timer service queue was full.
  326. *
  327. * Example usage:
  328. * @code{c}
  329. * #define BIT_0 ( 1 << 0 )
  330. * #define BIT_4 ( 1 << 4 )
  331. *
  332. * // An event group which it is assumed has already been created by a call to
  333. * // xEventGroupCreate().
  334. * EventGroupHandle_t xEventGroup;
  335. *
  336. * void anInterruptHandler( void )
  337. * {
  338. * // Clear bit 0 and bit 4 in xEventGroup.
  339. * xResult = xEventGroupClearBitsFromISR(
  340. * xEventGroup, // The event group being updated.
  341. * BIT_0 | BIT_4 ); // The bits being set.
  342. *
  343. * if ( xResult == pdPASS )
  344. * {
  345. * // The message was posted successfully.
  346. * portYIELD_FROM_ISR(pdTRUE);
  347. * }
  348. * }
  349. * @endcode
  350. * \defgroup xEventGroupClearBitsFromISR xEventGroupClearBitsFromISR
  351. * \ingroup EventGroup
  352. */
  353. #if ( configUSE_TRACE_FACILITY == 1 )
  354. BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup,
  355. const EventBits_t uxBitsToClear ) PRIVILEGED_FUNCTION;
  356. #else
  357. #define xEventGroupClearBitsFromISR( xEventGroup, uxBitsToClear ) \
  358. xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) ( xEventGroup ), ( uint32_t ) ( uxBitsToClear ), NULL )
  359. #endif
  360. /**
  361. * event_groups.h
  362. * @code{c}
  363. * EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
  364. * @endcode
  365. *
  366. * Set bits within an event group.
  367. * This function cannot be called from an interrupt. xEventGroupSetBitsFromISR()
  368. * is a version that can be called from an interrupt.
  369. *
  370. * Setting bits in an event group will automatically unblock tasks that are
  371. * blocked waiting for the bits.
  372. *
  373. * @param xEventGroup The event group in which the bits are to be set.
  374. *
  375. * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
  376. * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
  377. * and bit 0 set uxBitsToSet to 0x09.
  378. *
  379. * @return The value of the event group at the time the call to
  380. * xEventGroupSetBits() returns. There are two reasons why the returned value
  381. * might have the bits specified by the uxBitsToSet parameter cleared. First,
  382. * if setting a bit results in a task that was waiting for the bit leaving the
  383. * blocked state then it is possible the bit will be cleared automatically
  384. * (see the xClearBitOnExit parameter of xEventGroupWaitBits()). Second, any
  385. * unblocked (or otherwise Ready state) task that has a priority above that of
  386. * the task that called xEventGroupSetBits() will execute and may change the
  387. * event group value before the call to xEventGroupSetBits() returns.
  388. *
  389. * Example usage:
  390. * @code{c}
  391. * #define BIT_0 ( 1 << 0 )
  392. * #define BIT_4 ( 1 << 4 )
  393. *
  394. * void aFunction( EventGroupHandle_t xEventGroup )
  395. * {
  396. * EventBits_t uxBits;
  397. *
  398. * // Set bit 0 and bit 4 in xEventGroup.
  399. * uxBits = xEventGroupSetBits(
  400. * xEventGroup, // The event group being updated.
  401. * BIT_0 | BIT_4 );// The bits being set.
  402. *
  403. * if ( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
  404. * {
  405. * // Both bit 0 and bit 4 remained set when the function returned.
  406. * }
  407. * else if ( ( uxBits & BIT_0 ) != 0 )
  408. * {
  409. * // Bit 0 remained set when the function returned, but bit 4 was
  410. * // cleared. It might be that bit 4 was cleared automatically as a
  411. * // task that was waiting for bit 4 was removed from the Blocked
  412. * // state.
  413. * }
  414. * else if ( ( uxBits & BIT_4 ) != 0 )
  415. * {
  416. * // Bit 4 remained set when the function returned, but bit 0 was
  417. * // cleared. It might be that bit 0 was cleared automatically as a
  418. * // task that was waiting for bit 0 was removed from the Blocked
  419. * // state.
  420. * }
  421. * else
  422. * {
  423. * // Neither bit 0 nor bit 4 remained set. It might be that a task
  424. * // was waiting for both of the bits to be set, and the bits were
  425. * // cleared as the task left the Blocked state.
  426. * }
  427. * }
  428. * @endcode
  429. * \defgroup xEventGroupSetBits xEventGroupSetBits
  430. * \ingroup EventGroup
  431. */
  432. EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
  433. const EventBits_t uxBitsToSet ) PRIVILEGED_FUNCTION;
  434. /**
  435. * event_groups.h
  436. * @code{c}
  437. * BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken );
  438. * @endcode
  439. *
  440. * A version of xEventGroupSetBits() that can be called from an interrupt.
  441. *
  442. * Setting bits in an event group is not a deterministic operation because there
  443. * are an unknown number of tasks that may be waiting for the bit or bits being
  444. * set. FreeRTOS does not allow nondeterministic operations to be performed in
  445. * interrupts or from critical sections. Therefore xEventGroupSetBitsFromISR()
  446. * sends a message to the timer task to have the set operation performed in the
  447. * context of the timer task - where a scheduler lock is used in place of a
  448. * critical section.
  449. *
  450. * @param xEventGroup The event group in which the bits are to be set.
  451. *
  452. * @param uxBitsToSet A bitwise value that indicates the bit or bits to set.
  453. * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3
  454. * and bit 0 set uxBitsToSet to 0x09.
  455. *
  456. * @param pxHigherPriorityTaskWoken As mentioned above, calling this function
  457. * will result in a message being sent to the timer daemon task. If the
  458. * priority of the timer daemon task is higher than the priority of the
  459. * currently running task (the task the interrupt interrupted) then
  460. * *pxHigherPriorityTaskWoken will be set to pdTRUE by
  461. * xEventGroupSetBitsFromISR(), indicating that a context switch should be
  462. * requested before the interrupt exits. For that reason
  463. * *pxHigherPriorityTaskWoken must be initialised to pdFALSE. See the
  464. * example code below.
  465. *
  466. * @return If the request to execute the function was posted successfully then
  467. * pdPASS is returned, otherwise pdFALSE is returned. pdFALSE will be returned
  468. * if the timer service queue was full.
  469. *
  470. * Example usage:
  471. * @code{c}
  472. * #define BIT_0 ( 1 << 0 )
  473. * #define BIT_4 ( 1 << 4 )
  474. *
  475. * // An event group which it is assumed has already been created by a call to
  476. * // xEventGroupCreate().
  477. * EventGroupHandle_t xEventGroup;
  478. *
  479. * void anInterruptHandler( void )
  480. * {
  481. * BaseType_t xHigherPriorityTaskWoken, xResult;
  482. *
  483. * // xHigherPriorityTaskWoken must be initialised to pdFALSE.
  484. * xHigherPriorityTaskWoken = pdFALSE;
  485. *
  486. * // Set bit 0 and bit 4 in xEventGroup.
  487. * xResult = xEventGroupSetBitsFromISR(
  488. * xEventGroup, // The event group being updated.
  489. * BIT_0 | BIT_4 // The bits being set.
  490. * &xHigherPriorityTaskWoken );
  491. *
  492. * // Was the message posted successfully?
  493. * if ( xResult == pdPASS )
  494. * {
  495. * // If xHigherPriorityTaskWoken is now set to pdTRUE then a context
  496. * // switch should be requested. The macro used is port specific and
  497. * // will be either portYIELD_FROM_ISR() or portEND_SWITCHING_ISR() -
  498. * // refer to the documentation page for the port being used.
  499. * portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  500. * }
  501. * }
  502. * @endcode
  503. * \defgroup xEventGroupSetBitsFromISR xEventGroupSetBitsFromISR
  504. * \ingroup EventGroup
  505. */
  506. #if ( configUSE_TRACE_FACILITY == 1 )
  507. BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup,
  508. const EventBits_t uxBitsToSet,
  509. BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
  510. #else
  511. #define xEventGroupSetBitsFromISR( xEventGroup, uxBitsToSet, pxHigherPriorityTaskWoken ) \
  512. xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) ( xEventGroup ), ( uint32_t ) ( uxBitsToSet ), ( pxHigherPriorityTaskWoken ) )
  513. #endif
  514. /**
  515. * event_groups.h
  516. * @code{c}
  517. * EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
  518. * const EventBits_t uxBitsToSet,
  519. * const EventBits_t uxBitsToWaitFor,
  520. * TickType_t xTicksToWait );
  521. * @endcode
  522. *
  523. * Atomically set bits within an event group, then wait for a combination of
  524. * bits to be set within the same event group. This functionality is typically
  525. * used to synchronise multiple tasks, where each task has to wait for the other
  526. * tasks to reach a synchronisation point before proceeding.
  527. *
  528. * This function cannot be used from an interrupt.
  529. *
  530. * The function will return before its block time expires if the bits specified
  531. * by the uxBitsToWait parameter are set, or become set within that time. In
  532. * this case all the bits specified by uxBitsToWait will be automatically
  533. * cleared before the function returns.
  534. *
  535. * @param xEventGroup The event group in which the bits are being tested. The
  536. * event group must have previously been created using a call to
  537. * xEventGroupCreate().
  538. *
  539. * @param uxBitsToSet The bits to set in the event group before determining
  540. * if, and possibly waiting for, all the bits specified by the uxBitsToWait
  541. * parameter are set.
  542. *
  543. * @param uxBitsToWaitFor A bitwise value that indicates the bit or bits to test
  544. * inside the event group. For example, to wait for bit 0 and bit 2 set
  545. * uxBitsToWaitFor to 0x05. To wait for bits 0 and bit 1 and bit 2 set
  546. * uxBitsToWaitFor to 0x07. Etc.
  547. *
  548. * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait
  549. * for all of the bits specified by uxBitsToWaitFor to become set.
  550. *
  551. * @return The value of the event group at the time either the bits being waited
  552. * for became set, or the block time expired. Test the return value to know
  553. * which bits were set. If xEventGroupSync() returned because its timeout
  554. * expired then not all the bits being waited for will be set. If
  555. * xEventGroupSync() returned because all the bits it was waiting for were
  556. * set then the returned value is the event group value before any bits were
  557. * automatically cleared.
  558. *
  559. * Example usage:
  560. * @code{c}
  561. * // Bits used by the three tasks.
  562. * #define TASK_0_BIT ( 1 << 0 )
  563. * #define TASK_1_BIT ( 1 << 1 )
  564. * #define TASK_2_BIT ( 1 << 2 )
  565. *
  566. * #define ALL_SYNC_BITS ( TASK_0_BIT | TASK_1_BIT | TASK_2_BIT )
  567. *
  568. * // Use an event group to synchronise three tasks. It is assumed this event
  569. * // group has already been created elsewhere.
  570. * EventGroupHandle_t xEventBits;
  571. *
  572. * void vTask0( void *pvParameters )
  573. * {
  574. * EventBits_t uxReturn;
  575. * TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
  576. *
  577. * for( ;; )
  578. * {
  579. * // Perform task functionality here.
  580. *
  581. * // Set bit 0 in the event flag to note this task has reached the
  582. * // sync point. The other two tasks will set the other two bits defined
  583. * // by ALL_SYNC_BITS. All three tasks have reached the synchronisation
  584. * // point when all the ALL_SYNC_BITS are set. Wait a maximum of 100ms
  585. * // for this to happen.
  586. * uxReturn = xEventGroupSync( xEventBits, TASK_0_BIT, ALL_SYNC_BITS, xTicksToWait );
  587. *
  588. * if ( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
  589. * {
  590. * // All three tasks reached the synchronisation point before the call
  591. * // to xEventGroupSync() timed out.
  592. * }
  593. * }
  594. * }
  595. *
  596. * void vTask1( void *pvParameters )
  597. * {
  598. * for( ;; )
  599. * {
  600. * // Perform task functionality here.
  601. *
  602. * // Set bit 1 in the event flag to note this task has reached the
  603. * // synchronisation point. The other two tasks will set the other two
  604. * // bits defined by ALL_SYNC_BITS. All three tasks have reached the
  605. * // synchronisation point when all the ALL_SYNC_BITS are set. Wait
  606. * // indefinitely for this to happen.
  607. * xEventGroupSync( xEventBits, TASK_1_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  608. *
  609. * // xEventGroupSync() was called with an indefinite block time, so
  610. * // this task will only reach here if the synchronisation was made by all
  611. * // three tasks, so there is no need to test the return value.
  612. * }
  613. * }
  614. *
  615. * void vTask2( void *pvParameters )
  616. * {
  617. * for( ;; )
  618. * {
  619. * // Perform task functionality here.
  620. *
  621. * // Set bit 2 in the event flag to note this task has reached the
  622. * // synchronisation point. The other two tasks will set the other two
  623. * // bits defined by ALL_SYNC_BITS. All three tasks have reached the
  624. * // synchronisation point when all the ALL_SYNC_BITS are set. Wait
  625. * // indefinitely for this to happen.
  626. * xEventGroupSync( xEventBits, TASK_2_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  627. *
  628. * // xEventGroupSync() was called with an indefinite block time, so
  629. * // this task will only reach here if the synchronisation was made by all
  630. * // three tasks, so there is no need to test the return value.
  631. * }
  632. * }
  633. *
  634. * @endcode
  635. * \defgroup xEventGroupSync xEventGroupSync
  636. * \ingroup EventGroup
  637. */
  638. EventBits_t xEventGroupSync(EventGroupHandle_t xEventGroup,
  639. const EventBits_t uxBitsToSet,
  640. const EventBits_t uxBitsToWaitFor,
  641. TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  642. /**
  643. * event_groups.h
  644. * @code{c}
  645. * EventBits_t xEventGroupGetBits( EventGroupHandle_t xEventGroup );
  646. * @endcode
  647. *
  648. * Returns the current value of the bits in an event group. This function
  649. * cannot be used from an interrupt.
  650. *
  651. * @param xEventGroup The event group being queried.
  652. *
  653. * @return The event group bits at the time xEventGroupGetBits() was called.
  654. *
  655. * \defgroup xEventGroupGetBits xEventGroupGetBits
  656. * \ingroup EventGroup
  657. * 获取当前事件标志组
  658. */
  659. #define xEventGroupGetBits(xEventGroup) xEventGroupClearBits((xEventGroup), 0)
  660. /**
  661. * event_groups.h
  662. * @code{c}
  663. * EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup );
  664. * @endcode
  665. *
  666. * A version of xEventGroupGetBits() that can be called from an ISR.
  667. *
  668. * @param xEventGroup The event group being queried.
  669. *
  670. * @return The event group bits at the time xEventGroupGetBitsFromISR() was called.
  671. *
  672. * \defgroup xEventGroupGetBitsFromISR xEventGroupGetBitsFromISR
  673. * \ingroup EventGroup
  674. */
  675. EventBits_t xEventGroupGetBitsFromISR(EventGroupHandle_t xEventGroup) PRIVILEGED_FUNCTION;
  676. /**
  677. * event_groups.h
  678. * @code{c}
  679. * void xEventGroupDelete( EventGroupHandle_t xEventGroup );
  680. * @endcode
  681. *
  682. * Delete an event group that was previously created by a call to
  683. * xEventGroupCreate(). Tasks that are blocked on the event group will be
  684. * unblocked and obtain 0 as the event group's value.
  685. *
  686. * @param xEventGroup The event group being deleted.
  687. */
  688. void vEventGroupDelete( EventGroupHandle_t xEventGroup ) PRIVILEGED_FUNCTION;
  689. /* For internal use only. */
  690. void vEventGroupSetBitsCallback( void * pvEventGroup,
  691. const uint32_t ulBitsToSet ) PRIVILEGED_FUNCTION;
  692. void vEventGroupClearBitsCallback( void * pvEventGroup,
  693. const uint32_t ulBitsToClear ) PRIVILEGED_FUNCTION;
  694. #if ( configUSE_TRACE_FACILITY == 1 )
  695. UBaseType_t uxEventGroupGetNumber( void * xEventGroup ) PRIVILEGED_FUNCTION;
  696. void vEventGroupSetNumber( void * xEventGroup,
  697. UBaseType_t uxEventGroupNumber ) PRIVILEGED_FUNCTION;
  698. #endif
  699. /* *INDENT-OFF* */
  700. #ifdef __cplusplus
  701. }
  702. #endif
  703. /* *INDENT-ON* */
  704. #endif /* EVENT_GROUPS_H */