2 FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 FreeRTOS supports many tools and architectures. V7.0.0 is sponsored by:
\r
6 Atollic AB - Atollic provides professional embedded systems development
\r
7 tools for C/C++ development, code analysis and test automation.
\r
8 See http://www.atollic.com
\r
11 ***************************************************************************
\r
13 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
14 * Complete, revised, and edited pdf reference manuals are also *
\r
17 * Purchasing FreeRTOS documentation will not only help you, by *
\r
18 * ensuring you get running as quickly as possible and with an *
\r
19 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
20 * the FreeRTOS project to continue with its mission of providing *
\r
21 * professional grade, cross platform, de facto standard solutions *
\r
22 * for microcontrollers - completely free of charge! *
\r
24 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
26 * Thank you for using FreeRTOS, and thank you for your support! *
\r
28 ***************************************************************************
\r
31 This file is part of the FreeRTOS distribution.
\r
33 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
34 the terms of the GNU General Public License (version 2) as published by the
\r
35 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
36 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
37 distribute a combined work that includes FreeRTOS without being obliged to
\r
38 provide the source code for proprietary components outside of the FreeRTOS
\r
39 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
40 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
41 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
42 more details. You should have received a copy of the GNU General Public
\r
43 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
44 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
45 by writing to Richard Barry, contact details for whom are available on the
\r
50 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
53 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
56 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
57 licensing and training services.
\r
60 #include "FreeRTOS.h"
\r
62 #include "croutine.h"
\r
65 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
68 #ifdef portREMOVE_STATIC_QUALIFIER
\r
73 /* Lists for ready and blocked co-routines. --------------------*/
\r
74 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
75 static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
76 static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
77 static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
78 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
79 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
81 /* Other file private variables. --------------------------------*/
\r
82 corCRCB * pxCurrentCoRoutine = NULL;
\r
83 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
\r
84 static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
\r
86 /* The initial state of the co-routine when it is created. */
\r
87 #define corINITIAL_STATE ( 0 )
\r
90 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
91 * for the priority. It is inserted at the end of the list.
\r
93 * This macro accesses the co-routine ready lists and therefore must not be
\r
94 * used from within an ISR.
\r
96 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
98 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
100 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
102 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
106 * Utility to ready all the lists used by the scheduler. This is called
\r
107 * automatically upon the creation of the first co-routine.
\r
109 static void prvInitialiseCoRoutineLists( void );
\r
112 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
113 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
114 * in the pending ready list in order that they can later be moved to the ready
\r
115 * list by the co-routine scheduler.
\r
117 static void prvCheckPendingReadyList( void );
\r
120 * Macro that looks at the list of co-routines that are currently delayed to
\r
121 * see if any require waking.
\r
123 * Co-routines are stored in the queue in the order of their wake time -
\r
124 * meaning once one co-routine has been found whose timer has not expired
\r
125 * we need not look any further down the list.
\r
127 static void prvCheckDelayedList( void );
\r
129 /*-----------------------------------------------------------*/
\r
131 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
133 signed portBASE_TYPE xReturn;
\r
134 corCRCB *pxCoRoutine;
\r
136 /* Allocate the memory that will store the co-routine control block. */
\r
137 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\r
140 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
141 be created and the co-routine data structures need initialising. */
\r
142 if( pxCurrentCoRoutine == NULL )
\r
144 pxCurrentCoRoutine = pxCoRoutine;
\r
145 prvInitialiseCoRoutineLists();
\r
148 /* Check the priority is within limits. */
\r
149 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
151 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
154 /* Fill out the co-routine control block from the function parameters. */
\r
155 pxCoRoutine->uxState = corINITIAL_STATE;
\r
156 pxCoRoutine->uxPriority = uxPriority;
\r
157 pxCoRoutine->uxIndex = uxIndex;
\r
158 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
160 /* Initialise all the other co-routine control block parameters. */
\r
161 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
162 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
164 /* Set the co-routine control block as a link back from the xListItem.
\r
165 This is so we can get back to the containing CRCB from a generic item
\r
167 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
168 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
170 /* Event lists are always in priority order. */
\r
171 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
173 /* Now the co-routine has been initialised it can be added to the ready
\r
174 list at the correct priority. */
\r
175 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
181 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
186 /*-----------------------------------------------------------*/
\r
188 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
190 portTickType xTimeToWake;
\r
192 /* Calculate the time to wake - this may overflow but this is
\r
194 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
196 /* We must remove ourselves from the ready list before adding
\r
197 ourselves to the blocked list as the same list item is used for
\r
199 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
201 /* The list item will be inserted in wake time order. */
\r
202 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
204 if( xTimeToWake < xCoRoutineTickCount )
\r
206 /* Wake time has overflowed. Place this item in the
\r
208 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
212 /* The wake time has not overflowed, so we can use the
\r
213 current block list. */
\r
214 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
219 /* Also add the co-routine to an event list. If this is done then the
\r
220 function must be called with interrupts disabled. */
\r
221 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
224 /*-----------------------------------------------------------*/
\r
226 static void prvCheckPendingReadyList( void )
\r
228 /* Are there any co-routines waiting to get moved to the ready list? These
\r
229 are co-routines that have been readied by an ISR. The ISR cannot access
\r
230 the ready lists itself. */
\r
231 while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
\r
233 corCRCB *pxUnblockedCRCB;
\r
235 /* The pending ready list can be accessed by an ISR. */
\r
236 portDISABLE_INTERRUPTS();
\r
238 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
239 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
241 portENABLE_INTERRUPTS();
\r
243 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
244 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
247 /*-----------------------------------------------------------*/
\r
249 static void prvCheckDelayedList( void )
\r
253 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
254 while( xPassedTicks )
\r
256 xCoRoutineTickCount++;
\r
259 /* If the tick count has overflowed we need to swap the ready lists. */
\r
260 if( xCoRoutineTickCount == 0 )
\r
264 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
265 any items in pxDelayedCoRoutineList here then there is an error! */
\r
266 pxTemp = pxDelayedCoRoutineList;
\r
267 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
268 pxOverflowDelayedCoRoutineList = pxTemp;
\r
271 /* See if this tick has made a timeout expire. */
\r
272 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
\r
274 pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
\r
276 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
278 /* Timeout not yet expired. */
\r
282 portDISABLE_INTERRUPTS();
\r
284 /* The event could have occurred just before this critical
\r
285 section. If this is the case then the generic list item will
\r
286 have been moved to the pending ready list and the following
\r
287 line is still valid. Also the pvContainer parameter will have
\r
288 been set to NULL so the following lines are also valid. */
\r
289 vListRemove( &( pxCRCB->xGenericListItem ) );
\r
291 /* Is the co-routine waiting on an event also? */
\r
292 if( pxCRCB->xEventListItem.pvContainer )
\r
294 vListRemove( &( pxCRCB->xEventListItem ) );
\r
297 portENABLE_INTERRUPTS();
\r
299 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
303 xLastTickCount = xCoRoutineTickCount;
\r
305 /*-----------------------------------------------------------*/
\r
307 void vCoRoutineSchedule( void )
\r
309 /* See if any co-routines readied by events need moving to the ready lists. */
\r
310 prvCheckPendingReadyList();
\r
312 /* See if any delayed co-routines have timed out. */
\r
313 prvCheckDelayedList();
\r
315 /* Find the highest priority queue that contains ready co-routines. */
\r
316 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
318 if( uxTopCoRoutineReadyPriority == 0 )
\r
320 /* No more co-routines to check. */
\r
323 --uxTopCoRoutineReadyPriority;
\r
326 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
327 of the same priority get an equal share of the processor time. */
\r
328 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
330 /* Call the co-routine. */
\r
331 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
335 /*-----------------------------------------------------------*/
\r
337 static void prvInitialiseCoRoutineLists( void )
\r
339 unsigned portBASE_TYPE uxPriority;
\r
341 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
343 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
346 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
347 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
348 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
\r
350 /* Start with pxDelayedCoRoutineList using list1 and the
\r
351 pxOverflowDelayedCoRoutineList using list2. */
\r
352 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
353 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
355 /*-----------------------------------------------------------*/
\r
357 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
359 corCRCB *pxUnblockedCRCB;
\r
360 signed portBASE_TYPE xReturn;
\r
362 /* This function is called from within an interrupt. It can only access
\r
363 event lists and the pending ready list. This function assumes that a
\r
364 check has already been made to ensure pxEventList is not empty. */
\r
365 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
366 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
367 vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
369 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r