croutine.h 27 KB

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