2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 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
74 /* Remove the whole file is co-routines are not being used. */
\r
75 #if( configUSE_CO_ROUTINES != 0 )
\r
78 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
81 #ifdef portREMOVE_STATIC_QUALIFIER
\r
86 /* Lists for ready and blocked co-routines. --------------------*/
\r
87 static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
88 static List_t xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
89 static List_t xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
90 static List_t * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
91 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
92 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
94 /* Other file private variables. --------------------------------*/
\r
95 CRCB_t * pxCurrentCoRoutine = NULL;
\r
96 static UBaseType_t uxTopCoRoutineReadyPriority = 0;
\r
97 static TickType_t xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
\r
99 /* The initial state of the co-routine when it is created. */
\r
100 #define corINITIAL_STATE ( 0 )
\r
103 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
104 * for the priority. It is inserted at the end of the list.
\r
106 * This macro accesses the co-routine ready lists and therefore must not be
\r
107 * used from within an ISR.
\r
109 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
111 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
113 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
115 vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
119 * Utility to ready all the lists used by the scheduler. This is called
\r
120 * automatically upon the creation of the first co-routine.
\r
122 static void prvInitialiseCoRoutineLists( void );
\r
125 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
126 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
127 * in the pending ready list in order that they can later be moved to the ready
\r
128 * list by the co-routine scheduler.
\r
130 static void prvCheckPendingReadyList( void );
\r
133 * Macro that looks at the list of co-routines that are currently delayed to
\r
134 * see if any require waking.
\r
136 * Co-routines are stored in the queue in the order of their wake time -
\r
137 * meaning once one co-routine has been found whose timer has not expired
\r
138 * we need not look any further down the list.
\r
140 static void prvCheckDelayedList( void );
\r
142 /*-----------------------------------------------------------*/
\r
144 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex )
\r
146 BaseType_t xReturn;
\r
147 CRCB_t *pxCoRoutine;
\r
149 /* Allocate the memory that will store the co-routine control block. */
\r
150 pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );
\r
153 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
154 be created and the co-routine data structures need initialising. */
\r
155 if( pxCurrentCoRoutine == NULL )
\r
157 pxCurrentCoRoutine = pxCoRoutine;
\r
158 prvInitialiseCoRoutineLists();
\r
161 /* Check the priority is within limits. */
\r
162 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
164 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
167 /* Fill out the co-routine control block from the function parameters. */
\r
168 pxCoRoutine->uxState = corINITIAL_STATE;
\r
169 pxCoRoutine->uxPriority = uxPriority;
\r
170 pxCoRoutine->uxIndex = uxIndex;
\r
171 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
173 /* Initialise all the other co-routine control block parameters. */
\r
174 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
175 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
177 /* Set the co-routine control block as a link back from the ListItem_t.
\r
178 This is so we can get back to the containing CRCB from a generic item
\r
180 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
181 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
183 /* Event lists are always in priority order. */
\r
184 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );
\r
186 /* Now the co-routine has been initialised it can be added to the ready
\r
187 list at the correct priority. */
\r
188 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
194 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
199 /*-----------------------------------------------------------*/
\r
201 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList )
\r
203 TickType_t xTimeToWake;
\r
205 /* Calculate the time to wake - this may overflow but this is
\r
207 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
209 /* We must remove ourselves from the ready list before adding
\r
210 ourselves to the blocked list as the same list item is used for
\r
212 ( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
214 /* The list item will be inserted in wake time order. */
\r
215 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
217 if( xTimeToWake < xCoRoutineTickCount )
\r
219 /* Wake time has overflowed. Place this item in the
\r
221 vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
225 /* The wake time has not overflowed, so we can use the
\r
226 current block list. */
\r
227 vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
232 /* Also add the co-routine to an event list. If this is done then the
\r
233 function must be called with interrupts disabled. */
\r
234 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
237 /*-----------------------------------------------------------*/
\r
239 static void prvCheckPendingReadyList( void )
\r
241 /* Are there any co-routines waiting to get moved to the ready list? These
\r
242 are co-routines that have been readied by an ISR. The ISR cannot access
\r
243 the ready lists itself. */
\r
244 while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
\r
246 CRCB_t *pxUnblockedCRCB;
\r
248 /* The pending ready list can be accessed by an ISR. */
\r
249 portDISABLE_INTERRUPTS();
\r
251 pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
252 ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
254 portENABLE_INTERRUPTS();
\r
256 ( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
257 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
260 /*-----------------------------------------------------------*/
\r
262 static void prvCheckDelayedList( void )
\r
266 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
267 while( xPassedTicks )
\r
269 xCoRoutineTickCount++;
\r
272 /* If the tick count has overflowed we need to swap the ready lists. */
\r
273 if( xCoRoutineTickCount == 0 )
\r
277 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
278 any items in pxDelayedCoRoutineList here then there is an error! */
\r
279 pxTemp = pxDelayedCoRoutineList;
\r
280 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
281 pxOverflowDelayedCoRoutineList = pxTemp;
\r
284 /* See if this tick has made a timeout expire. */
\r
285 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
\r
287 pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
\r
289 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
291 /* Timeout not yet expired. */
\r
295 portDISABLE_INTERRUPTS();
\r
297 /* The event could have occurred just before this critical
\r
298 section. If this is the case then the generic list item will
\r
299 have been moved to the pending ready list and the following
\r
300 line is still valid. Also the pvContainer parameter will have
\r
301 been set to NULL so the following lines are also valid. */
\r
302 ( void ) uxListRemove( &( pxCRCB->xGenericListItem ) );
\r
304 /* Is the co-routine waiting on an event also? */
\r
305 if( pxCRCB->xEventListItem.pvContainer )
\r
307 ( void ) uxListRemove( &( pxCRCB->xEventListItem ) );
\r
310 portENABLE_INTERRUPTS();
\r
312 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
316 xLastTickCount = xCoRoutineTickCount;
\r
318 /*-----------------------------------------------------------*/
\r
320 void vCoRoutineSchedule( void )
\r
322 /* See if any co-routines readied by events need moving to the ready lists. */
\r
323 prvCheckPendingReadyList();
\r
325 /* See if any delayed co-routines have timed out. */
\r
326 prvCheckDelayedList();
\r
328 /* Find the highest priority queue that contains ready co-routines. */
\r
329 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
331 if( uxTopCoRoutineReadyPriority == 0 )
\r
333 /* No more co-routines to check. */
\r
336 --uxTopCoRoutineReadyPriority;
\r
339 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
340 of the same priority get an equal share of the processor time. */
\r
341 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
343 /* Call the co-routine. */
\r
344 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
348 /*-----------------------------------------------------------*/
\r
350 static void prvInitialiseCoRoutineLists( void )
\r
352 UBaseType_t uxPriority;
\r
354 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
356 vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
359 vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );
\r
360 vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );
\r
361 vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );
\r
363 /* Start with pxDelayedCoRoutineList using list1 and the
\r
364 pxOverflowDelayedCoRoutineList using list2. */
\r
365 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
366 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
368 /*-----------------------------------------------------------*/
\r
370 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList )
\r
372 CRCB_t *pxUnblockedCRCB;
\r
373 BaseType_t xReturn;
\r
375 /* This function is called from within an interrupt. It can only access
\r
376 event lists and the pending ready list. This function assumes that a
\r
377 check has already been made to ensure pxEventList is not empty. */
\r
378 pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
379 ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
380 vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
382 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r
394 #endif /* configUSE_CO_ROUTINES == 0 */
\r