croutine.h 29 KB

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