croutine.h 28 KB

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