2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 #include "FreeRTOS.h"
\r
67 #include "croutine.h"
\r
70 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
73 #ifdef portREMOVE_STATIC_QUALIFIER
\r
78 /* Lists for ready and blocked co-routines. --------------------*/
\r
79 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
\r
80 static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
\r
81 static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
\r
82 static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
\r
83 static xList * pxOverflowDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */
\r
84 static xList 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
86 /* Other file private variables. --------------------------------*/
\r
87 corCRCB * pxCurrentCoRoutine = NULL;
\r
88 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
\r
89 static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
\r
91 /* The initial state of the co-routine when it is created. */
\r
92 #define corINITIAL_STATE ( 0 )
\r
95 * Place the co-routine represented by pxCRCB into the appropriate ready queue
\r
96 * for the priority. It is inserted at the end of the list.
\r
98 * This macro accesses the co-routine ready lists and therefore must not be
\r
99 * used from within an ISR.
\r
101 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
\r
103 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
\r
105 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
\r
107 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
\r
111 * Utility to ready all the lists used by the scheduler. This is called
\r
112 * automatically upon the creation of the first co-routine.
\r
114 static void prvInitialiseCoRoutineLists( void );
\r
117 * Co-routines that are readied by an interrupt cannot be placed directly into
\r
118 * the ready lists (there is no mutual exclusion). Instead they are placed in
\r
119 * in the pending ready list in order that they can later be moved to the ready
\r
120 * list by the co-routine scheduler.
\r
122 static void prvCheckPendingReadyList( void );
\r
125 * Macro that looks at the list of co-routines that are currently delayed to
\r
126 * see if any require waking.
\r
128 * Co-routines are stored in the queue in the order of their wake time -
\r
129 * meaning once one co-routine has been found whose timer has not expired
\r
130 * we need not look any further down the list.
\r
132 static void prvCheckDelayedList( void );
\r
134 /*-----------------------------------------------------------*/
\r
136 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
\r
138 signed portBASE_TYPE xReturn;
\r
139 corCRCB *pxCoRoutine;
\r
141 /* Allocate the memory that will store the co-routine control block. */
\r
142 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
\r
145 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
\r
146 be created and the co-routine data structures need initialising. */
\r
147 if( pxCurrentCoRoutine == NULL )
\r
149 pxCurrentCoRoutine = pxCoRoutine;
\r
150 prvInitialiseCoRoutineLists();
\r
153 /* Check the priority is within limits. */
\r
154 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
\r
156 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
\r
159 /* Fill out the co-routine control block from the function parameters. */
\r
160 pxCoRoutine->uxState = corINITIAL_STATE;
\r
161 pxCoRoutine->uxPriority = uxPriority;
\r
162 pxCoRoutine->uxIndex = uxIndex;
\r
163 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
\r
165 /* Initialise all the other co-routine control block parameters. */
\r
166 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
\r
167 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
\r
169 /* Set the co-routine control block as a link back from the xListItem.
\r
170 This is so we can get back to the containing CRCB from a generic item
\r
172 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
\r
173 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
\r
175 /* Event lists are always in priority order. */
\r
176 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
178 /* Now the co-routine has been initialised it can be added to the ready
\r
179 list at the correct priority. */
\r
180 prvAddCoRoutineToReadyQueue( pxCoRoutine );
\r
186 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
191 /*-----------------------------------------------------------*/
\r
193 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
\r
195 portTickType xTimeToWake;
\r
197 /* Calculate the time to wake - this may overflow but this is
\r
199 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
\r
201 /* We must remove ourselves from the ready list before adding
\r
202 ourselves to the blocked list as the same list item is used for
\r
204 uxListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
206 /* The list item will be inserted in wake time order. */
\r
207 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
\r
209 if( xTimeToWake < xCoRoutineTickCount )
\r
211 /* Wake time has overflowed. Place this item in the
\r
213 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
217 /* The wake time has not overflowed, so we can use the
\r
218 current block list. */
\r
219 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
\r
224 /* Also add the co-routine to an event list. If this is done then the
\r
225 function must be called with interrupts disabled. */
\r
226 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
\r
229 /*-----------------------------------------------------------*/
\r
231 static void prvCheckPendingReadyList( void )
\r
233 /* Are there any co-routines waiting to get moved to the ready list? These
\r
234 are co-routines that have been readied by an ISR. The ISR cannot access
\r
235 the ready lists itself. */
\r
236 while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
\r
238 corCRCB *pxUnblockedCRCB;
\r
240 /* The pending ready list can be accessed by an ISR. */
\r
241 portDISABLE_INTERRUPTS();
\r
243 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
\r
244 uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
246 portENABLE_INTERRUPTS();
\r
248 uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
\r
249 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
\r
252 /*-----------------------------------------------------------*/
\r
254 static void prvCheckDelayedList( void )
\r
258 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
\r
259 while( xPassedTicks )
\r
261 xCoRoutineTickCount++;
\r
264 /* If the tick count has overflowed we need to swap the ready lists. */
\r
265 if( xCoRoutineTickCount == 0 )
\r
269 /* Tick count has overflowed so we need to swap the delay lists. If there are
\r
270 any items in pxDelayedCoRoutineList here then there is an error! */
\r
271 pxTemp = pxDelayedCoRoutineList;
\r
272 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
\r
273 pxOverflowDelayedCoRoutineList = pxTemp;
\r
276 /* See if this tick has made a timeout expire. */
\r
277 while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
\r
279 pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
\r
281 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
\r
283 /* Timeout not yet expired. */
\r
287 portDISABLE_INTERRUPTS();
\r
289 /* The event could have occurred just before this critical
\r
290 section. If this is the case then the generic list item will
\r
291 have been moved to the pending ready list and the following
\r
292 line is still valid. Also the pvContainer parameter will have
\r
293 been set to NULL so the following lines are also valid. */
\r
294 uxListRemove( &( pxCRCB->xGenericListItem ) );
\r
296 /* Is the co-routine waiting on an event also? */
\r
297 if( pxCRCB->xEventListItem.pvContainer )
\r
299 uxListRemove( &( pxCRCB->xEventListItem ) );
\r
302 portENABLE_INTERRUPTS();
\r
304 prvAddCoRoutineToReadyQueue( pxCRCB );
\r
308 xLastTickCount = xCoRoutineTickCount;
\r
310 /*-----------------------------------------------------------*/
\r
312 void vCoRoutineSchedule( void )
\r
314 /* See if any co-routines readied by events need moving to the ready lists. */
\r
315 prvCheckPendingReadyList();
\r
317 /* See if any delayed co-routines have timed out. */
\r
318 prvCheckDelayedList();
\r
320 /* Find the highest priority queue that contains ready co-routines. */
\r
321 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
\r
323 if( uxTopCoRoutineReadyPriority == 0 )
\r
325 /* No more co-routines to check. */
\r
328 --uxTopCoRoutineReadyPriority;
\r
331 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
\r
332 of the same priority get an equal share of the processor time. */
\r
333 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
\r
335 /* Call the co-routine. */
\r
336 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
\r
340 /*-----------------------------------------------------------*/
\r
342 static void prvInitialiseCoRoutineLists( void )
\r
344 unsigned portBASE_TYPE uxPriority;
\r
346 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
\r
348 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
\r
351 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
\r
352 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
\r
353 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
\r
355 /* Start with pxDelayedCoRoutineList using list1 and the
\r
356 pxOverflowDelayedCoRoutineList using list2. */
\r
357 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
\r
358 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
\r
360 /*-----------------------------------------------------------*/
\r
362 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
\r
364 corCRCB *pxUnblockedCRCB;
\r
365 signed portBASE_TYPE xReturn;
\r
367 /* This function is called from within an interrupt. It can only access
\r
368 event lists and the pending ready list. This function assumes that a
\r
369 check has already been made to ensure pxEventList is not empty. */
\r
370 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
371 uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
\r
372 vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
\r
374 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
\r