2 FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 #include "FreeRTOS.h"
\r
56 #include "croutine.h"
\r
59 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
62 #ifdef portREMOVE_STATIC_QUALIFIER
\r
67 /* Lists for ready and blocked co-routines. --------------------*/
\r
68 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
69 static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
70 static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
71 static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
72 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
73 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
75 /* Other file private variables. --------------------------------*/
\r
76 corCRCB * pxCurrentCoRoutine = NULL;
\r
77 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
\r
78 static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
\r
80 /* The initial state of the co-routine when it is created. */
\r
81 #define corINITIAL_STATE ( 0 )
\r
84 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
85 * for the priority. It is inserted at the end of the list.
\r
87 * This macro accesses the co-routine ready lists and therefore must not be
\r
88 * used from within an ISR.
\r
90 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
92 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
94 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
96 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
100 * Utility to ready all the lists used by the scheduler. This is called
\r
101 * automatically upon the creation of the first co-routine.
\r
103 static void prvInitialiseCoRoutineLists( void );
\r
106 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
107 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
108 * in the pending ready list in order that they can later be moved to the ready
\r
109 * list by the co-routine scheduler.
\r
111 static void prvCheckPendingReadyList( void );
\r
114 * Macro that looks at the list of co-routines that are currently delayed to
\r
115 * see if any require waking.
\r
117 * Co-routines are stored in the queue in the order of their wake time -
\r
118 * meaning once one co-routine has been found whose timer has not expired
\r
119 * we need not look any further down the list.
\r
121 static void prvCheckDelayedList( void );
\r
123 /*-----------------------------------------------------------*/
\r
125 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
127 signed portBASE_TYPE xReturn;
\r
128 corCRCB *pxCoRoutine;
\r
130 /* Allocate the memory that will store the co-routine control block. */
\r
131 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\r
134 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
135 be created and the co-routine data structures need initialising. */
\r
136 if( pxCurrentCoRoutine == NULL )
\r
138 pxCurrentCoRoutine = pxCoRoutine;
\r
139 prvInitialiseCoRoutineLists();
\r
142 /* Check the priority is within limits. */
\r
143 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
145 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
148 /* Fill out the co-routine control block from the function parameters. */
\r
149 pxCoRoutine->uxState = corINITIAL_STATE;
\r
150 pxCoRoutine->uxPriority = uxPriority;
\r
151 pxCoRoutine->uxIndex = uxIndex;
\r
152 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
154 /* Initialise all the other co-routine control block parameters. */
\r
155 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
156 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
158 /* Set the co-routine control block as a link back from the xListItem.
\r
159 This is so we can get back to the containing CRCB from a generic item
\r
161 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
162 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
164 /* Event lists are always in priority order. */
\r
165 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
167 /* Now the co-routine has been initialised it can be added to the ready
\r
168 list at the correct priority. */
\r
169 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
175 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
180 /*-----------------------------------------------------------*/
\r
182 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
184 portTickType xTimeToWake;
\r
186 /* Calculate the time to wake - this may overflow but this is
\r
188 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
190 /* We must remove ourselves from the ready list before adding
\r
191 ourselves to the blocked list as the same list item is used for
\r
193 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
195 /* The list item will be inserted in wake time order. */
\r
196 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
198 if( xTimeToWake < xCoRoutineTickCount )
\r
200 /* Wake time has overflowed. Place this item in the
\r
202 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
206 /* The wake time has not overflowed, so we can use the
\r
207 current block list. */
\r
208 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
213 /* Also add the co-routine to an event list. If this is done then the
\r
214 function must be called with interrupts disabled. */
\r
215 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
218 /*-----------------------------------------------------------*/
\r
220 static void prvCheckPendingReadyList( void )
\r
222 /* Are there any co-routines waiting to get moved to the ready list? These
\r
223 are co-routines that have been readied by an ISR. The ISR cannot access
\r
224 the ready lists itself. */
\r
225 while( !listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) )
\r
227 corCRCB *pxUnblockedCRCB;
\r
229 /* The pending ready list can be accessed by an ISR. */
\r
230 portDISABLE_INTERRUPTS();
\r
232 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
233 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
235 portENABLE_INTERRUPTS();
\r
237 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
238 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
241 /*-----------------------------------------------------------*/
\r
243 static void prvCheckDelayedList( void )
\r
247 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
248 while( xPassedTicks )
\r
250 xCoRoutineTickCount++;
\r
253 /* If the tick count has overflowed we need to swap the ready lists. */
\r
254 if( xCoRoutineTickCount == 0 )
\r
258 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
259 any items in pxDelayedCoRoutineList here then there is an error! */
\r
260 pxTemp = pxDelayedCoRoutineList;
\r
261 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
262 pxOverflowDelayedCoRoutineList = pxTemp;
\r
265 /* See if this tick has made a timeout expire. */
\r
266 while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
\r
268 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
270 /* Timeout not yet expired. */
\r
274 portDISABLE_INTERRUPTS();
\r
276 /* The event could have occurred just before this critical
\r
277 section. If this is the case then the generic list item will
\r
278 have been moved to the pending ready list and the following
\r
279 line is still valid. Also the pvContainer parameter will have
\r
280 been set to NULL so the following lines are also valid. */
\r
281 vListRemove( &( pxCRCB->xGenericListItem ) );
\r
283 /* Is the co-routine waiting on an event also? */
\r
284 if( pxCRCB->xEventListItem.pvContainer )
\r
286 vListRemove( &( pxCRCB->xEventListItem ) );
\r
289 portENABLE_INTERRUPTS();
\r
291 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
295 xLastTickCount = xCoRoutineTickCount;
\r
297 /*-----------------------------------------------------------*/
\r
299 void vCoRoutineSchedule( void )
\r
301 /* See if any co-routines readied by events need moving to the ready lists. */
\r
302 prvCheckPendingReadyList();
\r
304 /* See if any delayed co-routines have timed out. */
\r
305 prvCheckDelayedList();
\r
307 /* Find the highest priority queue that contains ready co-routines. */
\r
308 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
310 if( uxTopCoRoutineReadyPriority == 0 )
\r
312 /* No more co-routines to check. */
\r
315 --uxTopCoRoutineReadyPriority;
\r
318 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
319 of the same priority get an equal share of the processor time. */
\r
320 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
322 /* Call the co-routine. */
\r
323 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
327 /*-----------------------------------------------------------*/
\r
329 static void prvInitialiseCoRoutineLists( void )
\r
331 unsigned portBASE_TYPE uxPriority;
\r
333 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
335 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
338 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
339 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
340 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
\r
342 /* Start with pxDelayedCoRoutineList using list1 and the
\r
343 pxOverflowDelayedCoRoutineList using list2. */
\r
344 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
345 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
347 /*-----------------------------------------------------------*/
\r
349 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
351 corCRCB *pxUnblockedCRCB;
\r
352 signed portBASE_TYPE xReturn;
\r
354 /* This function is called from within an interrupt. It can only access
\r
355 event lists and the pending ready list. */
\r
356 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
357 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
358 vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
360 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r