croutine.h 25 KB

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