2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 #include "FreeRTOS.h"
\r
68 #include "croutine.h"
\r
71 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
74 #ifdef portREMOVE_STATIC_QUALIFIER
\r
79 /* Lists for ready and blocked co-routines. --------------------*/
\r
80 static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
81 static List_t xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
82 static List_t xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
83 static List_t * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
84 static List_t * pxOverflowDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */
\r
85 static List_t 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
87 /* Other file private variables. --------------------------------*/
\r
88 CRCB_t * pxCurrentCoRoutine = NULL;
\r
89 static UBaseType_t uxTopCoRoutineReadyPriority = 0;
\r
90 static TickType_t xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
\r
92 /* The initial state of the co-routine when it is created. */
\r
93 #define corINITIAL_STATE ( 0 )
\r
96 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
97 * for the priority. It is inserted at the end of the list.
\r
99 * This macro accesses the co-routine ready lists and therefore must not be
\r
100 * used from within an ISR.
\r
102 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
104 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
106 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
108 vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
112 * Utility to ready all the lists used by the scheduler. This is called
\r
113 * automatically upon the creation of the first co-routine.
\r
115 static void prvInitialiseCoRoutineLists( void );
\r
118 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
119 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
120 * in the pending ready list in order that they can later be moved to the ready
\r
121 * list by the co-routine scheduler.
\r
123 static void prvCheckPendingReadyList( void );
\r
126 * Macro that looks at the list of co-routines that are currently delayed to
\r
127 * see if any require waking.
\r
129 * Co-routines are stored in the queue in the order of their wake time -
\r
130 * meaning once one co-routine has been found whose timer has not expired
\r
131 * we need not look any further down the list.
\r
133 static void prvCheckDelayedList( void );
\r
135 /*-----------------------------------------------------------*/
\r
137 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex )
\r
139 BaseType_t xReturn;
\r
140 CRCB_t *pxCoRoutine;
\r
142 /* Allocate the memory that will store the co-routine control block. */
\r
143 pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );
\r
146 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
147 be created and the co-routine data structures need initialising. */
\r
148 if( pxCurrentCoRoutine == NULL )
\r
150 pxCurrentCoRoutine = pxCoRoutine;
\r
151 prvInitialiseCoRoutineLists();
\r
154 /* Check the priority is within limits. */
\r
155 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
157 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
160 /* Fill out the co-routine control block from the function parameters. */
\r
161 pxCoRoutine->uxState = corINITIAL_STATE;
\r
162 pxCoRoutine->uxPriority = uxPriority;
\r
163 pxCoRoutine->uxIndex = uxIndex;
\r
164 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
166 /* Initialise all the other co-routine control block parameters. */
\r
167 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
168 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
170 /* Set the co-routine control block as a link back from the ListItem_t.
\r
171 This is so we can get back to the containing CRCB from a generic item
\r
173 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
174 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
176 /* Event lists are always in priority order. */
\r
177 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );
\r
179 /* Now the co-routine has been initialised it can be added to the ready
\r
180 list at the correct priority. */
\r
181 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
187 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
192 /*-----------------------------------------------------------*/
\r
194 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList )
\r
196 TickType_t xTimeToWake;
\r
198 /* Calculate the time to wake - this may overflow but this is
\r
200 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
202 /* We must remove ourselves from the ready list before adding
\r
203 ourselves to the blocked list as the same list item is used for
\r
205 ( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
207 /* The list item will be inserted in wake time order. */
\r
208 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
210 if( xTimeToWake < xCoRoutineTickCount )
\r
212 /* Wake time has overflowed. Place this item in the
\r
214 vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
218 /* The wake time has not overflowed, so we can use the
\r
219 current block list. */
\r
220 vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
225 /* Also add the co-routine to an event list. If this is done then the
\r
226 function must be called with interrupts disabled. */
\r
227 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
230 /*-----------------------------------------------------------*/
\r
232 static void prvCheckPendingReadyList( void )
\r
234 /* Are there any co-routines waiting to get moved to the ready list? These
\r
235 are co-routines that have been readied by an ISR. The ISR cannot access
\r
236 the ready lists itself. */
\r
237 while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
\r
239 CRCB_t *pxUnblockedCRCB;
\r
241 /* The pending ready list can be accessed by an ISR. */
\r
242 portDISABLE_INTERRUPTS();
\r
244 pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
245 ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
247 portENABLE_INTERRUPTS();
\r
249 ( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
250 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
253 /*-----------------------------------------------------------*/
\r
255 static void prvCheckDelayedList( void )
\r
259 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
260 while( xPassedTicks )
\r
262 xCoRoutineTickCount++;
\r
265 /* If the tick count has overflowed we need to swap the ready lists. */
\r
266 if( xCoRoutineTickCount == 0 )
\r
270 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
271 any items in pxDelayedCoRoutineList here then there is an error! */
\r
272 pxTemp = pxDelayedCoRoutineList;
\r
273 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
274 pxOverflowDelayedCoRoutineList = pxTemp;
\r
277 /* See if this tick has made a timeout expire. */
\r
278 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
\r
280 pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
\r
282 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
284 /* Timeout not yet expired. */
\r
288 portDISABLE_INTERRUPTS();
\r
290 /* The event could have occurred just before this critical
\r
291 section. If this is the case then the generic list item will
\r
292 have been moved to the pending ready list and the following
\r
293 line is still valid. Also the pvContainer parameter will have
\r
294 been set to NULL so the following lines are also valid. */
\r
295 uxListRemove( &( pxCRCB->xGenericListItem ) );
\r
297 /* Is the co-routine waiting on an event also? */
\r
298 if( pxCRCB->xEventListItem.pvContainer )
\r
300 ( void ) uxListRemove( &( pxCRCB->xEventListItem ) );
\r
303 portENABLE_INTERRUPTS();
\r
305 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
309 xLastTickCount = xCoRoutineTickCount;
\r
311 /*-----------------------------------------------------------*/
\r
313 void vCoRoutineSchedule( void )
\r
315 /* See if any co-routines readied by events need moving to the ready lists. */
\r
316 prvCheckPendingReadyList();
\r
318 /* See if any delayed co-routines have timed out. */
\r
319 prvCheckDelayedList();
\r
321 /* Find the highest priority queue that contains ready co-routines. */
\r
322 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
324 if( uxTopCoRoutineReadyPriority == 0 )
\r
326 /* No more co-routines to check. */
\r
329 --uxTopCoRoutineReadyPriority;
\r
332 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
333 of the same priority get an equal share of the processor time. */
\r
334 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
336 /* Call the co-routine. */
\r
337 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
341 /*-----------------------------------------------------------*/
\r
343 static void prvInitialiseCoRoutineLists( void )
\r
345 UBaseType_t uxPriority;
\r
347 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
349 vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
352 vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );
\r
353 vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );
\r
354 vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );
\r
356 /* Start with pxDelayedCoRoutineList using list1 and the
\r
357 pxOverflowDelayedCoRoutineList using list2. */
\r
358 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
359 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
361 /*-----------------------------------------------------------*/
\r
363 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList )
\r
365 CRCB_t *pxUnblockedCRCB;
\r
366 BaseType_t xReturn;
\r
368 /* This function is called from within an interrupt. It can only access
\r
369 event lists and the pending ready list. This function assumes that a
\r
370 check has already been made to ensure pxEventList is not empty. */
\r
371 pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
372 ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
373 vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
375 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r