2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
96 #include "FreeRTOS.h"
\r
98 #include "croutine.h"
\r
101 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
104 #ifdef portREMOVE_STATIC_QUALIFIER
\r
109 /* Lists for ready and blocked co-routines. --------------------*/
\r
110 static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
111 static List_t xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
112 static List_t xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
113 static List_t * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
114 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
115 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
117 /* Other file private variables. --------------------------------*/
\r
118 CRCB_t * pxCurrentCoRoutine = NULL;
\r
119 static UBaseType_t uxTopCoRoutineReadyPriority = 0;
\r
120 static TickType_t xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
\r
122 /* The initial state of the co-routine when it is created. */
\r
123 #define corINITIAL_STATE ( 0 )
\r
126 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
127 * for the priority. It is inserted at the end of the list.
\r
129 * This macro accesses the co-routine ready lists and therefore must not be
\r
130 * used from within an ISR.
\r
132 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
134 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
136 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
138 vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
142 * Utility to ready all the lists used by the scheduler. This is called
\r
143 * automatically upon the creation of the first co-routine.
\r
145 static void prvInitialiseCoRoutineLists( void );
\r
148 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
149 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
150 * in the pending ready list in order that they can later be moved to the ready
\r
151 * list by the co-routine scheduler.
\r
153 static void prvCheckPendingReadyList( void );
\r
156 * Macro that looks at the list of co-routines that are currently delayed to
\r
157 * see if any require waking.
\r
159 * Co-routines are stored in the queue in the order of their wake time -
\r
160 * meaning once one co-routine has been found whose timer has not expired
\r
161 * we need not look any further down the list.
\r
163 static void prvCheckDelayedList( void );
\r
165 /*-----------------------------------------------------------*/
\r
167 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex )
\r
169 BaseType_t xReturn;
\r
170 CRCB_t *pxCoRoutine;
\r
172 /* Allocate the memory that will store the co-routine control block. */
\r
173 pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );
\r
176 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
177 be created and the co-routine data structures need initialising. */
\r
178 if( pxCurrentCoRoutine == NULL )
\r
180 pxCurrentCoRoutine = pxCoRoutine;
\r
181 prvInitialiseCoRoutineLists();
\r
184 /* Check the priority is within limits. */
\r
185 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
187 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
190 /* Fill out the co-routine control block from the function parameters. */
\r
191 pxCoRoutine->uxState = corINITIAL_STATE;
\r
192 pxCoRoutine->uxPriority = uxPriority;
\r
193 pxCoRoutine->uxIndex = uxIndex;
\r
194 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
196 /* Initialise all the other co-routine control block parameters. */
\r
197 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
198 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
200 /* Set the co-routine control block as a link back from the ListItem_t.
\r
201 This is so we can get back to the containing CRCB from a generic item
\r
203 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
204 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
206 /* Event lists are always in priority order. */
\r
207 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );
\r
209 /* Now the co-routine has been initialised it can be added to the ready
\r
210 list at the correct priority. */
\r
211 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
217 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
222 /*-----------------------------------------------------------*/
\r
224 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList )
\r
226 TickType_t xTimeToWake;
\r
228 /* Calculate the time to wake - this may overflow but this is
\r
230 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
232 /* We must remove ourselves from the ready list before adding
\r
233 ourselves to the blocked list as the same list item is used for
\r
235 ( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
237 /* The list item will be inserted in wake time order. */
\r
238 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
240 if( xTimeToWake < xCoRoutineTickCount )
\r
242 /* Wake time has overflowed. Place this item in the
\r
244 vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
248 /* The wake time has not overflowed, so we can use the
\r
249 current block list. */
\r
250 vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
255 /* Also add the co-routine to an event list. If this is done then the
\r
256 function must be called with interrupts disabled. */
\r
257 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
260 /*-----------------------------------------------------------*/
\r
262 static void prvCheckPendingReadyList( void )
\r
264 /* Are there any co-routines waiting to get moved to the ready list? These
\r
265 are co-routines that have been readied by an ISR. The ISR cannot access
\r
266 the ready lists itself. */
\r
267 while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
\r
269 CRCB_t *pxUnblockedCRCB;
\r
271 /* The pending ready list can be accessed by an ISR. */
\r
272 portDISABLE_INTERRUPTS();
\r
274 pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
275 ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
277 portENABLE_INTERRUPTS();
\r
279 ( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
280 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
283 /*-----------------------------------------------------------*/
\r
285 static void prvCheckDelayedList( void )
\r
289 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
290 while( xPassedTicks )
\r
292 xCoRoutineTickCount++;
\r
295 /* If the tick count has overflowed we need to swap the ready lists. */
\r
296 if( xCoRoutineTickCount == 0 )
\r
300 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
301 any items in pxDelayedCoRoutineList here then there is an error! */
\r
302 pxTemp = pxDelayedCoRoutineList;
\r
303 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
304 pxOverflowDelayedCoRoutineList = pxTemp;
\r
307 /* See if this tick has made a timeout expire. */
\r
308 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
\r
310 pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
\r
312 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
314 /* Timeout not yet expired. */
\r
318 portDISABLE_INTERRUPTS();
\r
320 /* The event could have occurred just before this critical
\r
321 section. If this is the case then the generic list item will
\r
322 have been moved to the pending ready list and the following
\r
323 line is still valid. Also the pvContainer parameter will have
\r
324 been set to NULL so the following lines are also valid. */
\r
325 ( void ) uxListRemove( &( pxCRCB->xGenericListItem ) );
\r
327 /* Is the co-routine waiting on an event also? */
\r
328 if( pxCRCB->xEventListItem.pvContainer )
\r
330 ( void ) uxListRemove( &( pxCRCB->xEventListItem ) );
\r
333 portENABLE_INTERRUPTS();
\r
335 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
339 xLastTickCount = xCoRoutineTickCount;
\r
341 /*-----------------------------------------------------------*/
\r
343 void vCoRoutineSchedule( void )
\r
345 /* See if any co-routines readied by events need moving to the ready lists. */
\r
346 prvCheckPendingReadyList();
\r
348 /* See if any delayed co-routines have timed out. */
\r
349 prvCheckDelayedList();
\r
351 /* Find the highest priority queue that contains ready co-routines. */
\r
352 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
354 if( uxTopCoRoutineReadyPriority == 0 )
\r
356 /* No more co-routines to check. */
\r
359 --uxTopCoRoutineReadyPriority;
\r
362 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
363 of the same priority get an equal share of the processor time. */
\r
364 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
366 /* Call the co-routine. */
\r
367 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
371 /*-----------------------------------------------------------*/
\r
373 static void prvInitialiseCoRoutineLists( void )
\r
375 UBaseType_t uxPriority;
\r
377 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
379 vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
382 vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );
\r
383 vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );
\r
384 vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );
\r
386 /* Start with pxDelayedCoRoutineList using list1 and the
\r
387 pxOverflowDelayedCoRoutineList using list2. */
\r
388 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
389 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
391 /*-----------------------------------------------------------*/
\r
393 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList )
\r
395 CRCB_t *pxUnblockedCRCB;
\r
396 BaseType_t xReturn;
\r
398 /* This function is called from within an interrupt. It can only access
\r
399 event lists and the pending ready list. This function assumes that a
\r
400 check has already been made to ensure pxEventList is not empty. */
\r
401 pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
402 ( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
403 vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
405 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r