croutine.h 25 KB

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