croutine.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * SPDX-FileCopyrightText: 2020 Amazon.com, Inc. or its affiliates
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
  7. */
  8. /*
  9. * FreeRTOS Kernel V10.4.3
  10. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  13. * this software and associated documentation files (the "Software"), to deal in
  14. * the Software without restriction, including without limitation the rights to
  15. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  16. * the Software, and to permit persons to whom the Software is furnished to do so,
  17. * subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in all
  20. * copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  24. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  25. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  26. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. *
  29. * https://www.FreeRTOS.org
  30. * https://github.com/FreeRTOS
  31. *
  32. */
  33. #ifndef CO_ROUTINE_H
  34. #define CO_ROUTINE_H
  35. #ifndef INC_FREERTOS_H
  36. #error "include FreeRTOS.h must appear in source files before include croutine.h"
  37. #endif
  38. #include "list.h"
  39. /* *INDENT-OFF* */
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* *INDENT-ON* */
  44. /* Used to hide the implementation of the co-routine control block. The
  45. * control block structure however has to be included in the header due to
  46. * the macro implementation of the co-routine functionality. */
  47. typedef void * CoRoutineHandle_t;
  48. /* Defines the prototype to which co-routine functions must conform. */
  49. typedef void (* crCOROUTINE_CODE)( CoRoutineHandle_t,
  50. UBaseType_t );
  51. typedef struct corCoRoutineControlBlock
  52. {
  53. crCOROUTINE_CODE pxCoRoutineFunction;
  54. ListItem_t xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */
  55. ListItem_t xEventListItem; /*< List item used to place the CRCB in event lists. */
  56. UBaseType_t uxPriority; /*< The priority of the co-routine in relation to other co-routines. */
  57. UBaseType_t uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */
  58. uint16_t uxState; /*< Used internally by the co-routine implementation. */
  59. } CRCB_t; /* Co-routine control block. Note must be identical in size down to uxPriority with TCB_t. */
  60. /**
  61. * @cond
  62. * croutine. h
  63. * @code{c}
  64. * BaseType_t xCoRoutineCreate(
  65. * crCOROUTINE_CODE pxCoRoutineCode,
  66. * UBaseType_t uxPriority,
  67. * UBaseType_t uxIndex
  68. * );
  69. * @endcode
  70. * @endcond
  71. *
  72. * Create a new co-routine and add it to the list of co-routines that are
  73. * ready to run.
  74. *
  75. * @param pxCoRoutineCode Pointer to the co-routine function. Co-routine
  76. * functions require special syntax - see the co-routine section of the WEB
  77. * documentation for more information.
  78. *
  79. * @param uxPriority The priority with respect to other co-routines at which
  80. * the co-routine will run.
  81. *
  82. * @param uxIndex Used to distinguish between different co-routines that
  83. * execute the same function. See the example below and the co-routine section
  84. * of the WEB documentation for further information.
  85. *
  86. * @return pdPASS if the co-routine was successfully created and added to a ready
  87. * list, otherwise an error code defined with ProjDefs.h.
  88. *
  89. * Example usage:
  90. * @code{c}
  91. * // Co-routine to be created.
  92. * void vFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
  93. * {
  94. * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
  95. * // This may not be necessary for const variables.
  96. * static const char cLedToFlash[ 2 ] = { 5, 6 };
  97. * static const TickType_t uxFlashRates[ 2 ] = { 200, 400 };
  98. *
  99. * // Must start every co-routine with a call to crSTART();
  100. * crSTART( xHandle );
  101. *
  102. * for( ;; )
  103. * {
  104. * // This co-routine just delays for a fixed period, then toggles
  105. * // an LED. Two co-routines are created using this function, so
  106. * // the uxIndex parameter is used to tell the co-routine which
  107. * // LED to flash and how int32_t to delay. This assumes xQueue has
  108. * // already been created.
  109. * vParTestToggleLED( cLedToFlash[ uxIndex ] );
  110. * crDELAY( xHandle, uxFlashRates[ uxIndex ] );
  111. * }
  112. *
  113. * // Must end every co-routine with a call to crEND();
  114. * crEND();
  115. * }
  116. *
  117. * // Function that creates two co-routines.
  118. * void vOtherFunction( void )
  119. * {
  120. * uint8_t ucParameterToPass;
  121. * TaskHandle_t xHandle;
  122. *
  123. * // Create two co-routines at priority 0. The first is given index 0
  124. * // so (from the code above) toggles LED 5 every 200 ticks. The second
  125. * // is given index 1 so toggles LED 6 every 400 ticks.
  126. * for( uxIndex = 0; uxIndex < 2; uxIndex++ )
  127. * {
  128. * xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex );
  129. * }
  130. * }
  131. * @endcode
  132. * @cond
  133. * \defgroup xCoRoutineCreate xCoRoutineCreate
  134. * @endcond
  135. * \ingroup Tasks
  136. */
  137. BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode,
  138. UBaseType_t uxPriority,
  139. UBaseType_t uxIndex );
  140. /**
  141. * @cond
  142. * croutine. h
  143. * @code{c}
  144. * void vCoRoutineSchedule( void );
  145. * @endcode
  146. * @endcond
  147. * Run a co-routine.
  148. *
  149. * vCoRoutineSchedule() executes the highest priority co-routine that is able
  150. * to run. The co-routine will execute until it either blocks, yields or is
  151. * preempted by a task. Co-routines execute cooperatively so one
  152. * co-routine cannot be preempted by another, but can be preempted by a task.
  153. *
  154. * If an application comprises of both tasks and co-routines then
  155. * vCoRoutineSchedule should be called from the idle task (in an idle task
  156. * hook).
  157. *
  158. * Example usage:
  159. * @code{c}
  160. * // This idle task hook will schedule a co-routine each time it is called.
  161. * // The rest of the idle task will execute between co-routine calls.
  162. * void vApplicationIdleHook( void )
  163. * {
  164. * vCoRoutineSchedule();
  165. * }
  166. *
  167. * // Alternatively, if you do not require any other part of the idle task to
  168. * // execute, the idle task hook can call vCoRoutineScheduler() within an
  169. * // infinite loop.
  170. * void vApplicationIdleHook( void )
  171. * {
  172. * for( ;; )
  173. * {
  174. * vCoRoutineSchedule();
  175. * }
  176. * }
  177. * @endcode
  178. * @endcode
  179. * @cond
  180. * \defgroup vCoRoutineSchedule vCoRoutineSchedule
  181. * @endcond
  182. * \ingroup Tasks
  183. */
  184. void vCoRoutineSchedule( void );
  185. /**
  186. * @cond
  187. * croutine. h
  188. * @code{c}
  189. * crSTART( CoRoutineHandle_t xHandle );
  190. * @endcode
  191. * @endcond
  192. *
  193. * This macro MUST always be called at the start of a co-routine function.
  194. *
  195. * Example usage:
  196. * @code{c}
  197. * // Co-routine to be created.
  198. * void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
  199. * {
  200. * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
  201. * static int32_t ulAVariable;
  202. *
  203. * // Must start every co-routine with a call to crSTART();
  204. * crSTART( xHandle );
  205. *
  206. * for( ;; )
  207. * {
  208. * // Co-routine functionality goes here.
  209. * }
  210. *
  211. * // Must end every co-routine with a call to crEND();
  212. * crEND();
  213. * }
  214. * @endcode
  215. * @cond
  216. * \defgroup crSTART crSTART
  217. * @endcond
  218. * \ingroup Tasks
  219. */
  220. #define crSTART( pxCRCB ) \
  221. switch( ( ( CRCB_t * ) ( pxCRCB ) )->uxState ) { \
  222. case 0:
  223. /**
  224. * @cond
  225. * croutine. h
  226. * @code{c}
  227. * crEND();
  228. * @endcode
  229. * @endcond
  230. *
  231. * This macro MUST always be called at the end of a co-routine function.
  232. *
  233. * Example usage:
  234. * @code{c}
  235. * // Co-routine to be created.
  236. * void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
  237. * {
  238. * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
  239. * static int32_t ulAVariable;
  240. *
  241. * // Must start every co-routine with a call to crSTART();
  242. * crSTART( xHandle );
  243. *
  244. * for( ;; )
  245. * {
  246. * // Co-routine functionality goes here.
  247. * }
  248. *
  249. * // Must end every co-routine with a call to crEND();
  250. * crEND();
  251. * }
  252. * @endcode
  253. * @cond
  254. * \defgroup crSTART crSTART
  255. * @endcond
  256. * \ingroup Tasks
  257. */
  258. #define crEND() }
  259. /*
  260. * These macros are intended for internal use by the co-routine implementation
  261. * only. The macros should not be used directly by application writers.
  262. */
  263. #define crSET_STATE0( xHandle ) \
  264. ( ( CRCB_t * ) ( xHandle ) )->uxState = ( __LINE__ * 2 ); return; \
  265. case ( __LINE__ * 2 ):
  266. #define crSET_STATE1( xHandle ) \
  267. ( ( CRCB_t * ) ( xHandle ) )->uxState = ( ( __LINE__ * 2 ) + 1 ); return; \
  268. case ( ( __LINE__ * 2 ) + 1 ):
  269. /**
  270. * @cond
  271. * croutine. h
  272. * @code{c}
  273. * crDELAY( CoRoutineHandle_t xHandle, TickType_t xTicksToDelay );
  274. * @endcode
  275. * @endcond
  276. *
  277. * Delay a co-routine for a fixed period of time.
  278. *
  279. * crDELAY can only be called from the co-routine function itself - not
  280. * from within a function called by the co-routine function. This is because
  281. * co-routines do not maintain their own stack.
  282. *
  283. * @param xHandle The handle of the co-routine to delay. This is the xHandle
  284. * parameter of the co-routine function.
  285. *
  286. * @param xTickToDelay The number of ticks that the co-routine should delay
  287. * for. The actual amount of time this equates to is defined by
  288. * configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_PERIOD_MS
  289. * can be used to convert ticks to milliseconds.
  290. *
  291. * Example usage:
  292. * @code{c}
  293. * // Co-routine to be created.
  294. * void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
  295. * {
  296. * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
  297. * // This may not be necessary for const variables.
  298. * // We are to delay for 200ms.
  299. * static const xTickType xDelayTime = 200 / portTICK_PERIOD_MS;
  300. *
  301. * // Must start every co-routine with a call to crSTART();
  302. * crSTART( xHandle );
  303. *
  304. * for( ;; )
  305. * {
  306. * // Delay for 200ms.
  307. * crDELAY( xHandle, xDelayTime );
  308. *
  309. * // Do something here.
  310. * }
  311. *
  312. * // Must end every co-routine with a call to crEND();
  313. * crEND();
  314. * }
  315. * @endcode
  316. * @cond
  317. * \defgroup crDELAY crDELAY
  318. * @endcond
  319. * \ingroup Tasks
  320. */
  321. #define crDELAY( xHandle, xTicksToDelay ) \
  322. if( ( xTicksToDelay ) > 0 ) \
  323. { \
  324. vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
  325. } \
  326. crSET_STATE0( ( xHandle ) );
  327. /**
  328. * @cond
  329. * @code{c}
  330. * crQUEUE_SEND(
  331. * CoRoutineHandle_t xHandle,
  332. * QueueHandle_t pxQueue,
  333. * void *pvItemToQueue,
  334. * TickType_t xTicksToWait,
  335. * BaseType_t *pxResult
  336. * )
  337. * @endcode
  338. * @endcond
  339. *
  340. * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
  341. * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
  342. *
  343. * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
  344. * xQueueSend() and xQueueReceive() can only be used from tasks.
  345. *
  346. * crQUEUE_SEND can only be called from the co-routine function itself - not
  347. * from within a function called by the co-routine function. This is because
  348. * co-routines do not maintain their own stack.
  349. *
  350. * See the co-routine section of the WEB documentation for information on
  351. * passing data between tasks and co-routines and between ISR's and
  352. * co-routines.
  353. *
  354. * @param xHandle The handle of the calling co-routine. This is the xHandle
  355. * parameter of the co-routine function.
  356. *
  357. * @param pxQueue The handle of the queue on which the data will be posted.
  358. * The handle is obtained as the return value when the queue is created using
  359. * the xQueueCreate() API function.
  360. *
  361. * @param pvItemToQueue A pointer to the data being posted onto the queue.
  362. * The number of bytes of each queued item is specified when the queue is
  363. * created. This number of bytes is copied from pvItemToQueue into the queue
  364. * itself.
  365. *
  366. * @param xTickToDelay The number of ticks that the co-routine should block
  367. * to wait for space to become available on the queue, should space not be
  368. * available immediately. The actual amount of time this equates to is defined
  369. * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
  370. * portTICK_PERIOD_MS can be used to convert ticks to milliseconds (see example
  371. * below).
  372. *
  373. * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
  374. * data was successfully posted onto the queue, otherwise it will be set to an
  375. * error defined within ProjDefs.h.
  376. *
  377. * Example usage:
  378. * @code{c}
  379. * // Co-routine function that blocks for a fixed period then posts a number onto
  380. * // a queue.
  381. * static void prvCoRoutineFlashTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
  382. * {
  383. * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
  384. * static BaseType_t xNumberToPost = 0;
  385. * static BaseType_t xResult;
  386. *
  387. * // Co-routines must begin with a call to crSTART().
  388. * crSTART( xHandle );
  389. *
  390. * for( ;; )
  391. * {
  392. * // This assumes the queue has already been created.
  393. * crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult );
  394. *
  395. * if( xResult != pdPASS )
  396. * {
  397. * // The message was not posted!
  398. * }
  399. *
  400. * // Increment the number to be posted onto the queue.
  401. * xNumberToPost++;
  402. *
  403. * // Delay for 100 ticks.
  404. * crDELAY( xHandle, 100 );
  405. * }
  406. *
  407. * // Co-routines must end with a call to crEND().
  408. * crEND();
  409. * }
  410. * @endcode
  411. * @cond
  412. * \defgroup crQUEUE_SEND crQUEUE_SEND
  413. * @endcond
  414. * \ingroup Tasks
  415. */
  416. #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
  417. { \
  418. *( pxResult ) = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), ( xTicksToWait ) ); \
  419. if( *( pxResult ) == errQUEUE_BLOCKED ) \
  420. { \
  421. crSET_STATE0( ( xHandle ) ); \
  422. *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
  423. } \
  424. if( *pxResult == errQUEUE_YIELD ) \
  425. { \
  426. crSET_STATE1( ( xHandle ) ); \
  427. *pxResult = pdPASS; \
  428. } \
  429. }
  430. /**
  431. * @cond
  432. * croutine. h
  433. * @code{c}
  434. * crQUEUE_RECEIVE(
  435. * CoRoutineHandle_t xHandle,
  436. * QueueHandle_t pxQueue,
  437. * void *pvBuffer,
  438. * TickType_t xTicksToWait,
  439. * BaseType_t *pxResult
  440. * )
  441. * @endcode
  442. * @endcond
  443. *
  444. * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
  445. * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
  446. *
  447. * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
  448. * xQueueSend() and xQueueReceive() can only be used from tasks.
  449. *
  450. * crQUEUE_RECEIVE can only be called from the co-routine function itself - not
  451. * from within a function called by the co-routine function. This is because
  452. * co-routines do not maintain their own stack.
  453. *
  454. * See the co-routine section of the WEB documentation for information on
  455. * passing data between tasks and co-routines and between ISR's and
  456. * co-routines.
  457. *
  458. * @param xHandle The handle of the calling co-routine. This is the xHandle
  459. * parameter of the co-routine function.
  460. *
  461. * @param pxQueue The handle of the queue from which the data will be received.
  462. * The handle is obtained as the return value when the queue is created using
  463. * the xQueueCreate() API function.
  464. *
  465. * @param pvBuffer The buffer into which the received item is to be copied.
  466. * The number of bytes of each queued item is specified when the queue is
  467. * created. This number of bytes is copied into pvBuffer.
  468. *
  469. * @param xTickToDelay The number of ticks that the co-routine should block
  470. * to wait for data to become available from the queue, should data not be
  471. * available immediately. The actual amount of time this equates to is defined
  472. * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
  473. * portTICK_PERIOD_MS can be used to convert ticks to milliseconds (see the
  474. * crQUEUE_SEND example).
  475. *
  476. * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
  477. * data was successfully retrieved from the queue, otherwise it will be set to
  478. * an error code as defined within ProjDefs.h.
  479. *
  480. * Example usage:
  481. * @code{c}
  482. * // A co-routine receives the number of an LED to flash from a queue. It
  483. * // blocks on the queue until the number is received.
  484. * static void prvCoRoutineFlashWorkTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
  485. * {
  486. * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
  487. * static BaseType_t xResult;
  488. * static UBaseType_t uxLEDToFlash;
  489. *
  490. * // All co-routines must start with a call to crSTART().
  491. * crSTART( xHandle );
  492. *
  493. * for( ;; )
  494. * {
  495. * // Wait for data to become available on the queue.
  496. * crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
  497. *
  498. * if( xResult == pdPASS )
  499. * {
  500. * // We received the LED to flash - flash it!
  501. * vParTestToggleLED( uxLEDToFlash );
  502. * }
  503. * }
  504. *
  505. * crEND();
  506. * }
  507. * @endcode
  508. * @cond
  509. * \defgroup crQUEUE_RECEIVE crQUEUE_RECEIVE
  510. * @endcond
  511. * \ingroup Tasks
  512. */
  513. #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \
  514. { \
  515. *( pxResult ) = xQueueCRReceive( ( pxQueue ), ( pvBuffer ), ( xTicksToWait ) ); \
  516. if( *( pxResult ) == errQUEUE_BLOCKED ) \
  517. { \
  518. crSET_STATE0( ( xHandle ) ); \
  519. *( pxResult ) = xQueueCRReceive( ( pxQueue ), ( pvBuffer ), 0 ); \
  520. } \
  521. if( *( pxResult ) == errQUEUE_YIELD ) \
  522. { \
  523. crSET_STATE1( ( xHandle ) ); \
  524. *( pxResult ) = pdPASS; \
  525. } \
  526. }
  527. /**
  528. * @cond
  529. * croutine. h
  530. * @code{c}
  531. * crQUEUE_SEND_FROM_ISR(
  532. * QueueHandle_t pxQueue,
  533. * void *pvItemToQueue,
  534. * BaseType_t xCoRoutinePreviouslyWoken
  535. * )
  536. * @endcode
  537. * @endcond
  538. *
  539. * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
  540. * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
  541. * functions used by tasks.
  542. *
  543. * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
  544. * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
  545. * xQueueReceiveFromISR() can only be used to pass data between a task and and
  546. * ISR.
  547. *
  548. * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue
  549. * that is being used from within a co-routine.
  550. *
  551. * See the co-routine section of the WEB documentation for information on
  552. * passing data between tasks and co-routines and between ISR's and
  553. * co-routines.
  554. *
  555. * @param xQueue The handle to the queue on which the item is to be posted.
  556. *
  557. * @param pvItemToQueue A pointer to the item that is to be placed on the
  558. * queue. The size of the items the queue will hold was defined when the
  559. * queue was created, so this many bytes will be copied from pvItemToQueue
  560. * into the queue storage area.
  561. *
  562. * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto
  563. * the same queue multiple times from a single interrupt. The first call
  564. * should always pass in pdFALSE. Subsequent calls should pass in
  565. * the value returned from the previous call.
  566. *
  567. * @return pdTRUE if a co-routine was woken by posting onto the queue. This is
  568. * used by the ISR to determine if a context switch may be required following
  569. * the ISR.
  570. *
  571. * Example usage:
  572. * @code{c}
  573. * // A co-routine that blocks on a queue waiting for characters to be received.
  574. * static void vReceivingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
  575. * {
  576. * char cRxedChar;
  577. * BaseType_t xResult;
  578. *
  579. * // All co-routines must start with a call to crSTART().
  580. * crSTART( xHandle );
  581. *
  582. * for( ;; )
  583. * {
  584. * // Wait for data to become available on the queue. This assumes the
  585. * // queue xCommsRxQueue has already been created!
  586. * crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
  587. *
  588. * // Was a character received?
  589. * if( xResult == pdPASS )
  590. * {
  591. * // Process the character here.
  592. * }
  593. * }
  594. *
  595. * // All co-routines must end with a call to crEND().
  596. * crEND();
  597. * }
  598. *
  599. * // An ISR that uses a queue to send characters received on a serial port to
  600. * // a co-routine.
  601. * void vUART_ISR( void )
  602. * {
  603. * char cRxedChar;
  604. * BaseType_t xCRWokenByPost = pdFALSE;
  605. *
  606. * // We loop around reading characters until there are none left in the UART.
  607. * while( UART_RX_REG_NOT_EMPTY() )
  608. * {
  609. * // Obtain the character from the UART.
  610. * cRxedChar = UART_RX_REG;
  611. *
  612. * // Post the character onto a queue. xCRWokenByPost will be pdFALSE
  613. * // the first time around the loop. If the post causes a co-routine
  614. * // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE.
  615. * // In this manner we can ensure that if more than one co-routine is
  616. * // blocked on the queue only one is woken by this ISR no matter how
  617. * // many characters are posted to the queue.
  618. * xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost );
  619. * }
  620. * }
  621. * @endcode
  622. * @cond
  623. * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR
  624. * @endcond
  625. * \ingroup Tasks
  626. */
  627. #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) \
  628. xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
  629. /**
  630. * @cond
  631. * croutine. h
  632. * @code{c}
  633. * crQUEUE_SEND_FROM_ISR(
  634. * QueueHandle_t pxQueue,
  635. * void *pvBuffer,
  636. * BaseType_t * pxCoRoutineWoken
  637. * )
  638. * @endcode
  639. * @endcond
  640. *
  641. * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
  642. * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
  643. * functions used by tasks.
  644. *
  645. * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
  646. * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
  647. * xQueueReceiveFromISR() can only be used to pass data between a task and and
  648. * ISR.
  649. *
  650. * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data
  651. * from a queue that is being used from within a co-routine (a co-routine
  652. * posted to the queue).
  653. *
  654. * See the co-routine section of the WEB documentation for information on
  655. * passing data between tasks and co-routines and between ISR's and
  656. * co-routines.
  657. *
  658. * @param xQueue The handle to the queue on which the item is to be posted.
  659. *
  660. * @param pvBuffer A pointer to a buffer into which the received item will be
  661. * placed. The size of the items the queue will hold was defined when the
  662. * queue was created, so this many bytes will be copied from the queue into
  663. * pvBuffer.
  664. *
  665. * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become
  666. * available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a
  667. * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise
  668. * *pxCoRoutineWoken will remain unchanged.
  669. *
  670. * @return pdTRUE an item was successfully received from the queue, otherwise
  671. * pdFALSE.
  672. *
  673. * Example usage:
  674. * @code{c}
  675. * // A co-routine that posts a character to a queue then blocks for a fixed
  676. * // period. The character is incremented each time.
  677. * static void vSendingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
  678. * {
  679. * // cChar holds its value while this co-routine is blocked and must therefore
  680. * // be declared static.
  681. * static char cCharToTx = 'a';
  682. * BaseType_t xResult;
  683. *
  684. * // All co-routines must start with a call to crSTART().
  685. * crSTART( xHandle );
  686. *
  687. * for( ;; )
  688. * {
  689. * // Send the next character to the queue.
  690. * crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult );
  691. *
  692. * if( xResult == pdPASS )
  693. * {
  694. * // The character was successfully posted to the queue.
  695. * }
  696. * else
  697. * {
  698. * // Could not post the character to the queue.
  699. * }
  700. *
  701. * // Enable the UART Tx interrupt to cause an interrupt in this
  702. * // hypothetical UART. The interrupt will obtain the character
  703. * // from the queue and send it.
  704. * ENABLE_RX_INTERRUPT();
  705. *
  706. * // Increment to the next character then block for a fixed period.
  707. * // cCharToTx will maintain its value across the delay as it is
  708. * // declared static.
  709. * cCharToTx++;
  710. * if( cCharToTx > 'x' )
  711. * {
  712. * cCharToTx = 'a';
  713. * }
  714. * crDELAY( 100 );
  715. * }
  716. *
  717. * // All co-routines must end with a call to crEND().
  718. * crEND();
  719. * }
  720. *
  721. * // An ISR that uses a queue to receive characters to send on a UART.
  722. * void vUART_ISR( void )
  723. * {
  724. * char cCharToTx;
  725. * BaseType_t xCRWokenByPost = pdFALSE;
  726. *
  727. * while( UART_TX_REG_EMPTY() )
  728. * {
  729. * // Are there any characters in the queue waiting to be sent?
  730. * // xCRWokenByPost will automatically be set to pdTRUE if a co-routine
  731. * // is woken by the post - ensuring that only a single co-routine is
  732. * // woken no matter how many times we go around this loop.
  733. * if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) )
  734. * {
  735. * SEND_CHARACTER( cCharToTx );
  736. * }
  737. * }
  738. * }
  739. * @endcode
  740. * @cond
  741. * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR
  742. * @endcond
  743. * \ingroup Tasks
  744. */
  745. #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) \
  746. xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
  747. /*
  748. * This function is intended for internal use by the co-routine macros only.
  749. * The macro nature of the co-routine implementation requires that the
  750. * prototype appears here. The function should not be used by application
  751. * writers.
  752. *
  753. * Removes the current co-routine from its ready list and places it in the
  754. * appropriate delayed list.
  755. */
  756. void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay,
  757. List_t * pxEventList );
  758. /*
  759. * This function is intended for internal use by the queue implementation only.
  760. * The function should not be used by application writers.
  761. *
  762. * Removes the highest priority co-routine from the event list and places it in
  763. * the pending ready list.
  764. */
  765. BaseType_t xCoRoutineRemoveFromEventList( const List_t * pxEventList );
  766. /* *INDENT-OFF* */
  767. #ifdef __cplusplus
  768. }
  769. #endif
  770. /* *INDENT-ON* */
  771. #endif /* CO_ROUTINE_H */