croutine.h 28 KB

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