2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
75 * Some kernel aware debuggers require data to be viewed to be global, rather
78 #ifdef portREMOVE_STATIC_QUALIFIER
83 /* Lists for ready and blocked co-routines. --------------------*/
84 static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
85 static List_t xDelayedCoRoutineList1; /*< Delayed co-routines. */
86 static List_t xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
87 static List_t * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
88 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. */
89 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. */
91 /* Other file private variables. --------------------------------*/
92 CRCB_t * pxCurrentCoRoutine = NULL;
93 static UBaseType_t uxTopCoRoutineReadyPriority = 0;
94 static TickType_t xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
96 /* The initial state of the co-routine when it is created. */
97 #define corINITIAL_STATE ( 0 )
100 * Place the co-routine represented by pxCRCB into the appropriate ready queue
101 * for the priority. It is inserted at the end of the list.
103 * This macro accesses the co-routine ready lists and therefore must not be
104 * used from within an ISR.
106 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
108 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
110 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
112 vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
116 * Utility to ready all the lists used by the scheduler. This is called
117 * automatically upon the creation of the first co-routine.
119 static void prvInitialiseCoRoutineLists( void );
122 * Co-routines that are readied by an interrupt cannot be placed directly into
123 * the ready lists (there is no mutual exclusion). Instead they are placed in
124 * in the pending ready list in order that they can later be moved to the ready
125 * list by the co-routine scheduler.
127 static void prvCheckPendingReadyList( void );
130 * Macro that looks at the list of co-routines that are currently delayed to
131 * see if any require waking.
133 * Co-routines are stored in the queue in the order of their wake time -
134 * meaning once one co-routine has been found whose timer has not expired
135 * we need not look any further down the list.
137 static void prvCheckDelayedList( void );
139 /*-----------------------------------------------------------*/
141 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex )
146 UNTESTED_FUNCTION(); //Actually, coroutines are entirely unsupported
147 /* Allocate the memory that will store the co-routine control block. */
148 pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );
151 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
152 be created and the co-routine data structures need initialising. */
153 if( pxCurrentCoRoutine == NULL )
155 pxCurrentCoRoutine = pxCoRoutine;
156 prvInitialiseCoRoutineLists();
159 /* Check the priority is within limits. */
160 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
162 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
165 /* Fill out the co-routine control block from the function parameters. */
166 pxCoRoutine->uxState = corINITIAL_STATE;
167 pxCoRoutine->uxPriority = uxPriority;
168 pxCoRoutine->uxIndex = uxIndex;
169 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
171 /* Initialise all the other co-routine control block parameters. */
172 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
173 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
175 /* Set the co-routine control block as a link back from the ListItem_t.
176 This is so we can get back to the containing CRCB from a generic item
178 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
179 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
181 /* Event lists are always in priority order. */
182 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );
184 /* Now the co-routine has been initialised it can be added to the ready
185 list at the correct priority. */
186 prvAddCoRoutineToReadyQueue( pxCoRoutine );
192 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
197 /*-----------------------------------------------------------*/
199 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList )
201 TickType_t xTimeToWake;
203 /* Calculate the time to wake - this may overflow but this is
205 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
207 /* We must remove ourselves from the ready list before adding
208 ourselves to the blocked list as the same list item is used for
210 ( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
212 /* The list item will be inserted in wake time order. */
213 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
215 if( xTimeToWake < xCoRoutineTickCount )
217 /* Wake time has overflowed. Place this item in the
219 vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
223 /* The wake time has not overflowed, so we can use the
224 current block list. */
225 vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
230 /* Also add the co-routine to an event list. If this is done then the
231 function must be called with interrupts disabled. */
232 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
235 /*-----------------------------------------------------------*/
237 static void prvCheckPendingReadyList( void )
239 /* Are there any co-routines waiting to get moved to the ready list? These
240 are co-routines that have been readied by an ISR. The ISR cannot access
241 the ready lists itself. */
242 while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
244 CRCB_t *pxUnblockedCRCB;
246 /* The pending ready list can be accessed by an ISR. */
247 portDISABLE_INTERRUPTS();
249 pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
250 ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
252 portENABLE_INTERRUPTS();
254 ( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
255 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
258 /*-----------------------------------------------------------*/
260 static void prvCheckDelayedList( void )
264 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
265 while( xPassedTicks )
267 xCoRoutineTickCount++;
270 /* If the tick count has overflowed we need to swap the ready lists. */
271 if( xCoRoutineTickCount == 0 )
275 /* Tick count has overflowed so we need to swap the delay lists. If there are
276 any items in pxDelayedCoRoutineList here then there is an error! */
277 pxTemp = pxDelayedCoRoutineList;
278 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
279 pxOverflowDelayedCoRoutineList = pxTemp;
282 /* See if this tick has made a timeout expire. */
283 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
285 pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
287 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
289 /* Timeout not yet expired. */
293 portDISABLE_INTERRUPTS();
295 /* The event could have occurred just before this critical
296 section. If this is the case then the generic list item will
297 have been moved to the pending ready list and the following
298 line is still valid. Also the pvContainer parameter will have
299 been set to NULL so the following lines are also valid. */
300 ( void ) uxListRemove( &( pxCRCB->xGenericListItem ) );
302 /* Is the co-routine waiting on an event also? */
303 if( pxCRCB->xEventListItem.pvContainer )
305 ( void ) uxListRemove( &( pxCRCB->xEventListItem ) );
308 portENABLE_INTERRUPTS();
310 prvAddCoRoutineToReadyQueue( pxCRCB );
314 xLastTickCount = xCoRoutineTickCount;
316 /*-----------------------------------------------------------*/
318 void vCoRoutineSchedule( void )
320 /* See if any co-routines readied by events need moving to the ready lists. */
321 prvCheckPendingReadyList();
323 /* See if any delayed co-routines have timed out. */
324 prvCheckDelayedList();
326 /* Find the highest priority queue that contains ready co-routines. */
327 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
329 if( uxTopCoRoutineReadyPriority == 0 )
331 /* No more co-routines to check. */
334 --uxTopCoRoutineReadyPriority;
337 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
338 of the same priority get an equal share of the processor time. */
339 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
341 /* Call the co-routine. */
342 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
346 /*-----------------------------------------------------------*/
348 static void prvInitialiseCoRoutineLists( void )
350 UBaseType_t uxPriority;
352 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
354 vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
357 vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );
358 vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );
359 vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );
361 /* Start with pxDelayedCoRoutineList using list1 and the
362 pxOverflowDelayedCoRoutineList using list2. */
363 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
364 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
366 /*-----------------------------------------------------------*/
368 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList )
370 CRCB_t *pxUnblockedCRCB;
373 /* This function is called from within an interrupt. It can only access
374 event lists and the pending ready list. This function assumes that a
375 check has already been made to ensure pxEventList is not empty. */
376 pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
377 ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
378 vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
380 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )