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