2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 #include "FreeRTOS.h"
\r
71 #include "croutine.h"
\r
74 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
77 #ifdef portREMOVE_STATIC_QUALIFIER
\r
82 /* Lists for ready and blocked co-routines. --------------------*/
\r
83 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
84 static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
85 static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
86 static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
87 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
88 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
90 /* Other file private variables. --------------------------------*/
\r
91 corCRCB * pxCurrentCoRoutine = NULL;
\r
92 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
\r
93 static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
\r
95 /* The initial state of the co-routine when it is created. */
\r
96 #define corINITIAL_STATE ( 0 )
\r
99 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
100 * for the priority. It is inserted at the end of the list.
\r
102 * This macro accesses the co-routine ready lists and therefore must not be
\r
103 * used from within an ISR.
\r
105 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
107 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
109 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
111 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
115 * Utility to ready all the lists used by the scheduler. This is called
\r
116 * automatically upon the creation of the first co-routine.
\r
118 static void prvInitialiseCoRoutineLists( void );
\r
121 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
122 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
123 * in the pending ready list in order that they can later be moved to the ready
\r
124 * list by the co-routine scheduler.
\r
126 static void prvCheckPendingReadyList( void );
\r
129 * Macro that looks at the list of co-routines that are currently delayed to
\r
130 * see if any require waking.
\r
132 * Co-routines are stored in the queue in the order of their wake time -
\r
133 * meaning once one co-routine has been found whose timer has not expired
\r
134 * we need not look any further down the list.
\r
136 static void prvCheckDelayedList( void );
\r
138 /*-----------------------------------------------------------*/
\r
140 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
142 signed portBASE_TYPE xReturn;
\r
143 corCRCB *pxCoRoutine;
\r
145 /* Allocate the memory that will store the co-routine control block. */
\r
146 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\r
149 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
150 be created and the co-routine data structures need initialising. */
\r
151 if( pxCurrentCoRoutine == NULL )
\r
153 pxCurrentCoRoutine = pxCoRoutine;
\r
154 prvInitialiseCoRoutineLists();
\r
157 /* Check the priority is within limits. */
\r
158 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
160 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
163 /* Fill out the co-routine control block from the function parameters. */
\r
164 pxCoRoutine->uxState = corINITIAL_STATE;
\r
165 pxCoRoutine->uxPriority = uxPriority;
\r
166 pxCoRoutine->uxIndex = uxIndex;
\r
167 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
169 /* Initialise all the other co-routine control block parameters. */
\r
170 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
171 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
173 /* Set the co-routine control block as a link back from the xListItem.
\r
174 This is so we can get back to the containing CRCB from a generic item
\r
176 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
177 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
179 /* Event lists are always in priority order. */
\r
180 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
182 /* Now the co-routine has been initialised it can be added to the ready
\r
183 list at the correct priority. */
\r
184 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
190 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
195 /*-----------------------------------------------------------*/
\r
197 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
199 portTickType xTimeToWake;
\r
201 /* Calculate the time to wake - this may overflow but this is
\r
203 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
205 /* We must remove ourselves from the ready list before adding
\r
206 ourselves to the blocked list as the same list item is used for
\r
208 uxListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
210 /* The list item will be inserted in wake time order. */
\r
211 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
213 if( xTimeToWake < xCoRoutineTickCount )
\r
215 /* Wake time has overflowed. Place this item in the
\r
217 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
221 /* The wake time has not overflowed, so we can use the
\r
222 current block list. */
\r
223 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
228 /* Also add the co-routine to an event list. If this is done then the
\r
229 function must be called with interrupts disabled. */
\r
230 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
233 /*-----------------------------------------------------------*/
\r
235 static void prvCheckPendingReadyList( void )
\r
237 /* Are there any co-routines waiting to get moved to the ready list? These
\r
238 are co-routines that have been readied by an ISR. The ISR cannot access
\r
239 the ready lists itself. */
\r
240 while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
\r
242 corCRCB *pxUnblockedCRCB;
\r
244 /* The pending ready list can be accessed by an ISR. */
\r
245 portDISABLE_INTERRUPTS();
\r
247 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
248 uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
250 portENABLE_INTERRUPTS();
\r
252 uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
253 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
256 /*-----------------------------------------------------------*/
\r
258 static void prvCheckDelayedList( void )
\r
262 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
263 while( xPassedTicks )
\r
265 xCoRoutineTickCount++;
\r
268 /* If the tick count has overflowed we need to swap the ready lists. */
\r
269 if( xCoRoutineTickCount == 0 )
\r
273 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
274 any items in pxDelayedCoRoutineList here then there is an error! */
\r
275 pxTemp = pxDelayedCoRoutineList;
\r
276 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
277 pxOverflowDelayedCoRoutineList = pxTemp;
\r
280 /* See if this tick has made a timeout expire. */
\r
281 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
\r
283 pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
\r
285 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
287 /* Timeout not yet expired. */
\r
291 portDISABLE_INTERRUPTS();
\r
293 /* The event could have occurred just before this critical
\r
294 section. If this is the case then the generic list item will
\r
295 have been moved to the pending ready list and the following
\r
296 line is still valid. Also the pvContainer parameter will have
\r
297 been set to NULL so the following lines are also valid. */
\r
298 uxListRemove( &( pxCRCB->xGenericListItem ) );
\r
300 /* Is the co-routine waiting on an event also? */
\r
301 if( pxCRCB->xEventListItem.pvContainer )
\r
303 uxListRemove( &( pxCRCB->xEventListItem ) );
\r
306 portENABLE_INTERRUPTS();
\r
308 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
312 xLastTickCount = xCoRoutineTickCount;
\r
314 /*-----------------------------------------------------------*/
\r
316 void vCoRoutineSchedule( void )
\r
318 /* See if any co-routines readied by events need moving to the ready lists. */
\r
319 prvCheckPendingReadyList();
\r
321 /* See if any delayed co-routines have timed out. */
\r
322 prvCheckDelayedList();
\r
324 /* Find the highest priority queue that contains ready co-routines. */
\r
325 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
327 if( uxTopCoRoutineReadyPriority == 0 )
\r
329 /* No more co-routines to check. */
\r
332 --uxTopCoRoutineReadyPriority;
\r
335 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
336 of the same priority get an equal share of the processor time. */
\r
337 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
339 /* Call the co-routine. */
\r
340 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
344 /*-----------------------------------------------------------*/
\r
346 static void prvInitialiseCoRoutineLists( void )
\r
348 unsigned portBASE_TYPE uxPriority;
\r
350 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
352 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
355 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
356 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
357 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
\r
359 /* Start with pxDelayedCoRoutineList using list1 and the
\r
360 pxOverflowDelayedCoRoutineList using list2. */
\r
361 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
362 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
364 /*-----------------------------------------------------------*/
\r
366 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
368 corCRCB *pxUnblockedCRCB;
\r
369 signed portBASE_TYPE xReturn;
\r
371 /* This function is called from within an interrupt. It can only access
\r
372 event lists and the pending ready list. This function assumes that a
\r
373 check has already been made to ensure pxEventList is not empty. */
\r
374 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
375 uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
376 vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
378 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r