2 FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
52 #include "FreeRTOS.h"
\r
54 #include "croutine.h"
\r
57 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
60 #ifdef portREMOVE_STATIC_QUALIFIER
\r
65 /* Lists for ready and blocked co-routines. --------------------*/
\r
66 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
67 static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
68 static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
69 static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
70 static xList * pxOverflowDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */
\r
71 static xList xPendingReadyCoRoutineList; /*< Holds co-routines that have been readied by an external event. They cannot be added directly to the ready lists as the ready lists cannot be accessed by interrupts. */
\r
73 /* Other file private variables. --------------------------------*/
\r
74 corCRCB * pxCurrentCoRoutine = NULL;
\r
75 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
\r
76 static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
\r
78 /* The initial state of the co-routine when it is created. */
\r
79 #define corINITIAL_STATE ( 0 )
\r
82 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
83 * for the priority. It is inserted at the end of the list.
\r
85 * This macro accesses the co-routine ready lists and therefore must not be
\r
86 * used from within an ISR.
\r
88 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
90 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
92 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
94 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
98 * Utility to ready all the lists used by the scheduler. This is called
\r
99 * automatically upon the creation of the first co-routine.
\r
101 static void prvInitialiseCoRoutineLists( void );
\r
104 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
105 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
106 * in the pending ready list in order that they can later be moved to the ready
\r
107 * list by the co-routine scheduler.
\r
109 static void prvCheckPendingReadyList( void );
\r
112 * Macro that looks at the list of co-routines that are currently delayed to
\r
113 * see if any require waking.
\r
115 * Co-routines are stored in the queue in the order of their wake time -
\r
116 * meaning once one co-routine has been found whose timer has not expired
\r
117 * we need not look any further down the list.
\r
119 static void prvCheckDelayedList( void );
\r
121 /*-----------------------------------------------------------*/
\r
123 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
125 signed portBASE_TYPE xReturn;
\r
126 corCRCB *pxCoRoutine;
\r
128 /* Allocate the memory that will store the co-routine control block. */
\r
129 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\r
132 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
133 be created and the co-routine data structures need initialising. */
\r
134 if( pxCurrentCoRoutine == NULL )
\r
136 pxCurrentCoRoutine = pxCoRoutine;
\r
137 prvInitialiseCoRoutineLists();
\r
140 /* Check the priority is within limits. */
\r
141 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
143 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
146 /* Fill out the co-routine control block from the function parameters. */
\r
147 pxCoRoutine->uxState = corINITIAL_STATE;
\r
148 pxCoRoutine->uxPriority = uxPriority;
\r
149 pxCoRoutine->uxIndex = uxIndex;
\r
150 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
152 /* Initialise all the other co-routine control block parameters. */
\r
153 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
154 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
156 /* Set the co-routine control block as a link back from the xListItem.
\r
157 This is so we can get back to the containing CRCB from a generic item
\r
159 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
160 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
162 /* Event lists are always in priority order. */
\r
163 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
165 /* Now the co-routine has been initialised it can be added to the ready
\r
166 list at the correct priority. */
\r
167 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
173 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
178 /*-----------------------------------------------------------*/
\r
180 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
182 portTickType xTimeToWake;
\r
184 /* Calculate the time to wake - this may overflow but this is
\r
186 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
188 /* We must remove ourselves from the ready list before adding
\r
189 ourselves to the blocked list as the same list item is used for
\r
191 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
193 /* The list item will be inserted in wake time order. */
\r
194 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
196 if( xTimeToWake < xCoRoutineTickCount )
\r
198 /* Wake time has overflowed. Place this item in the
\r
200 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
204 /* The wake time has not overflowed, so we can use the
\r
205 current block list. */
\r
206 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
211 /* Also add the co-routine to an event list. If this is done then the
\r
212 function must be called with interrupts disabled. */
\r
213 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
216 /*-----------------------------------------------------------*/
\r
218 static void prvCheckPendingReadyList( void )
\r
220 /* Are there any co-routines waiting to get moved to the ready list? These
\r
221 are co-routines that have been readied by an ISR. The ISR cannot access
\r
222 the ready lists itself. */
\r
223 while( !listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) )
\r
225 corCRCB *pxUnblockedCRCB;
\r
227 /* The pending ready list can be accessed by an ISR. */
\r
228 portDISABLE_INTERRUPTS();
\r
230 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
231 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
233 portENABLE_INTERRUPTS();
\r
235 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
236 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
239 /*-----------------------------------------------------------*/
\r
241 static void prvCheckDelayedList( void )
\r
245 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
246 while( xPassedTicks )
\r
248 xCoRoutineTickCount++;
\r
251 /* If the tick count has overflowed we need to swap the ready lists. */
\r
252 if( xCoRoutineTickCount == 0 )
\r
256 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
257 any items in pxDelayedCoRoutineList here then there is an error! */
\r
258 pxTemp = pxDelayedCoRoutineList;
\r
259 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
260 pxOverflowDelayedCoRoutineList = pxTemp;
\r
263 /* See if this tick has made a timeout expire. */
\r
264 while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
\r
266 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
268 /* Timeout not yet expired. */
\r
272 portDISABLE_INTERRUPTS();
\r
274 /* The event could have occurred just before this critical
\r
275 section. If this is the case then the generic list item will
\r
276 have been moved to the pending ready list and the following
\r
277 line is still valid. Also the pvContainer parameter will have
\r
278 been set to NULL so the following lines are also valid. */
\r
279 vListRemove( &( pxCRCB->xGenericListItem ) );
\r
281 /* Is the co-routine waiting on an event also? */
\r
282 if( pxCRCB->xEventListItem.pvContainer )
\r
284 vListRemove( &( pxCRCB->xEventListItem ) );
\r
287 portENABLE_INTERRUPTS();
\r
289 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
293 xLastTickCount = xCoRoutineTickCount;
\r
295 /*-----------------------------------------------------------*/
\r
297 void vCoRoutineSchedule( void )
\r
299 /* See if any co-routines readied by events need moving to the ready lists. */
\r
300 prvCheckPendingReadyList();
\r
302 /* See if any delayed co-routines have timed out. */
\r
303 prvCheckDelayedList();
\r
305 /* Find the highest priority queue that contains ready co-routines. */
\r
306 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
308 if( uxTopCoRoutineReadyPriority == 0 )
\r
310 /* No more co-routines to check. */
\r
313 --uxTopCoRoutineReadyPriority;
\r
316 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
317 of the same priority get an equal share of the processor time. */
\r
318 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
320 /* Call the co-routine. */
\r
321 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
325 /*-----------------------------------------------------------*/
\r
327 static void prvInitialiseCoRoutineLists( void )
\r
329 unsigned portBASE_TYPE uxPriority;
\r
331 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
333 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
336 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
337 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
338 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
\r
340 /* Start with pxDelayedCoRoutineList using list1 and the
\r
341 pxOverflowDelayedCoRoutineList using list2. */
\r
342 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
343 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
345 /*-----------------------------------------------------------*/
\r
347 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
349 corCRCB *pxUnblockedCRCB;
\r
350 signed portBASE_TYPE xReturn;
\r
352 /* This function is called from within an interrupt. It can only access
\r
353 event lists and the pending ready list. */
\r
354 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
355 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
356 vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
358 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r