2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 #include "FreeRTOS.h"
\r
69 #include "croutine.h"
\r
72 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
75 #ifdef portREMOVE_STATIC_QUALIFIER
\r
80 /* Lists for ready and blocked co-routines. --------------------*/
\r
81 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
82 static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
83 static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
84 static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
85 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
86 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
88 /* Other file private variables. --------------------------------*/
\r
89 corCRCB * pxCurrentCoRoutine = NULL;
\r
90 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
\r
91 static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
\r
93 /* The initial state of the co-routine when it is created. */
\r
94 #define corINITIAL_STATE ( 0 )
\r
97 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
98 * for the priority. It is inserted at the end of the list.
\r
100 * This macro accesses the co-routine ready lists and therefore must not be
\r
101 * used from within an ISR.
\r
103 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
105 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
107 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
109 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
113 * Utility to ready all the lists used by the scheduler. This is called
\r
114 * automatically upon the creation of the first co-routine.
\r
116 static void prvInitialiseCoRoutineLists( void );
\r
119 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
120 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
121 * in the pending ready list in order that they can later be moved to the ready
\r
122 * list by the co-routine scheduler.
\r
124 static void prvCheckPendingReadyList( void );
\r
127 * Macro that looks at the list of co-routines that are currently delayed to
\r
128 * see if any require waking.
\r
130 * Co-routines are stored in the queue in the order of their wake time -
\r
131 * meaning once one co-routine has been found whose timer has not expired
\r
132 * we need not look any further down the list.
\r
134 static void prvCheckDelayedList( void );
\r
136 /*-----------------------------------------------------------*/
\r
138 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
140 signed portBASE_TYPE xReturn;
\r
141 corCRCB *pxCoRoutine;
\r
143 /* Allocate the memory that will store the co-routine control block. */
\r
144 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\r
147 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
148 be created and the co-routine data structures need initialising. */
\r
149 if( pxCurrentCoRoutine == NULL )
\r
151 pxCurrentCoRoutine = pxCoRoutine;
\r
152 prvInitialiseCoRoutineLists();
\r
155 /* Check the priority is within limits. */
\r
156 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
158 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
161 /* Fill out the co-routine control block from the function parameters. */
\r
162 pxCoRoutine->uxState = corINITIAL_STATE;
\r
163 pxCoRoutine->uxPriority = uxPriority;
\r
164 pxCoRoutine->uxIndex = uxIndex;
\r
165 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
167 /* Initialise all the other co-routine control block parameters. */
\r
168 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
169 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
171 /* Set the co-routine control block as a link back from the xListItem.
\r
172 This is so we can get back to the containing CRCB from a generic item
\r
174 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
175 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
177 /* Event lists are always in priority order. */
\r
178 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
180 /* Now the co-routine has been initialised it can be added to the ready
\r
181 list at the correct priority. */
\r
182 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
188 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
193 /*-----------------------------------------------------------*/
\r
195 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
197 portTickType xTimeToWake;
\r
199 /* Calculate the time to wake - this may overflow but this is
\r
201 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
203 /* We must remove ourselves from the ready list before adding
\r
204 ourselves to the blocked list as the same list item is used for
\r
206 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
208 /* The list item will be inserted in wake time order. */
\r
209 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
211 if( xTimeToWake < xCoRoutineTickCount )
\r
213 /* Wake time has overflowed. Place this item in the
\r
215 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
219 /* The wake time has not overflowed, so we can use the
\r
220 current block list. */
\r
221 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
226 /* Also add the co-routine to an event list. If this is done then the
\r
227 function must be called with interrupts disabled. */
\r
228 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
231 /*-----------------------------------------------------------*/
\r
233 static void prvCheckPendingReadyList( void )
\r
235 /* Are there any co-routines waiting to get moved to the ready list? These
\r
236 are co-routines that have been readied by an ISR. The ISR cannot access
\r
237 the ready lists itself. */
\r
238 while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
\r
240 corCRCB *pxUnblockedCRCB;
\r
242 /* The pending ready list can be accessed by an ISR. */
\r
243 portDISABLE_INTERRUPTS();
\r
245 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
246 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
248 portENABLE_INTERRUPTS();
\r
250 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
251 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
254 /*-----------------------------------------------------------*/
\r
256 static void prvCheckDelayedList( void )
\r
260 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
261 while( xPassedTicks )
\r
263 xCoRoutineTickCount++;
\r
266 /* If the tick count has overflowed we need to swap the ready lists. */
\r
267 if( xCoRoutineTickCount == 0 )
\r
271 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
272 any items in pxDelayedCoRoutineList here then there is an error! */
\r
273 pxTemp = pxDelayedCoRoutineList;
\r
274 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
275 pxOverflowDelayedCoRoutineList = pxTemp;
\r
278 /* See if this tick has made a timeout expire. */
\r
279 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
\r
281 pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
\r
283 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
285 /* Timeout not yet expired. */
\r
289 portDISABLE_INTERRUPTS();
\r
291 /* The event could have occurred just before this critical
\r
292 section. If this is the case then the generic list item will
\r
293 have been moved to the pending ready list and the following
\r
294 line is still valid. Also the pvContainer parameter will have
\r
295 been set to NULL so the following lines are also valid. */
\r
296 vListRemove( &( pxCRCB->xGenericListItem ) );
\r
298 /* Is the co-routine waiting on an event also? */
\r
299 if( pxCRCB->xEventListItem.pvContainer )
\r
301 vListRemove( &( pxCRCB->xEventListItem ) );
\r
304 portENABLE_INTERRUPTS();
\r
306 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
310 xLastTickCount = xCoRoutineTickCount;
\r
312 /*-----------------------------------------------------------*/
\r
314 void vCoRoutineSchedule( void )
\r
316 /* See if any co-routines readied by events need moving to the ready lists. */
\r
317 prvCheckPendingReadyList();
\r
319 /* See if any delayed co-routines have timed out. */
\r
320 prvCheckDelayedList();
\r
322 /* Find the highest priority queue that contains ready co-routines. */
\r
323 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
325 if( uxTopCoRoutineReadyPriority == 0 )
\r
327 /* No more co-routines to check. */
\r
330 --uxTopCoRoutineReadyPriority;
\r
333 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
334 of the same priority get an equal share of the processor time. */
\r
335 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
337 /* Call the co-routine. */
\r
338 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
342 /*-----------------------------------------------------------*/
\r
344 static void prvInitialiseCoRoutineLists( void )
\r
346 unsigned portBASE_TYPE uxPriority;
\r
348 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
350 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
353 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
354 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
355 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
\r
357 /* Start with pxDelayedCoRoutineList using list1 and the
\r
358 pxOverflowDelayedCoRoutineList using list2. */
\r
359 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
360 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
362 /*-----------------------------------------------------------*/
\r
364 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
366 corCRCB *pxUnblockedCRCB;
\r
367 signed portBASE_TYPE xReturn;
\r
369 /* This function is called from within an interrupt. It can only access
\r
370 event lists and the pending ready list. This function assumes that a
\r
371 check has already been made to ensure pxEventList is not empty. */
\r
372 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
373 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
374 vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
376 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r