Francisco Paez / freertos

Dependents:   frdm_k64f_freertos_lib

Committer:
fep
Date:
Wed May 31 02:27:10 2017 +0000
Revision:
0:62cd296ba2a7
FreeRTOS v9.0.0 for Cortex-M4F (FRDM-K64F and others...)

Who changed what in which revision?

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