1 |
572 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
#include "FreeRTOS.h"
|
55 |
|
|
#include "task.h"
|
56 |
|
|
#include "croutine.h"
|
57 |
|
|
|
58 |
|
|
/*
|
59 |
|
|
* Some kernel aware debuggers require data to be viewed to be global, rather
|
60 |
|
|
* than file scope.
|
61 |
|
|
*/
|
62 |
|
|
#ifdef portREMOVE_STATIC_QUALIFIER
|
63 |
|
|
#define static
|
64 |
|
|
#endif
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
/* Lists for ready and blocked co-routines. --------------------*/
|
68 |
|
|
static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /*< Prioritised ready co-routines. */
|
69 |
|
|
static xList xDelayedCoRoutineList1; /*< Delayed co-routines. */
|
70 |
|
|
static xList xDelayedCoRoutineList2; /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
|
71 |
|
|
static xList * pxDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used. */
|
72 |
|
|
static xList * pxOverflowDelayedCoRoutineList; /*< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */
|
73 |
|
|
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. */
|
74 |
|
|
|
75 |
|
|
/* Other file private variables. --------------------------------*/
|
76 |
|
|
corCRCB * pxCurrentCoRoutine = NULL;
|
77 |
|
|
static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
|
78 |
|
|
static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
|
79 |
|
|
|
80 |
|
|
/* The initial state of the co-routine when it is created. */
|
81 |
|
|
#define corINITIAL_STATE ( 0 )
|
82 |
|
|
|
83 |
|
|
/*
|
84 |
|
|
* Place the co-routine represented by pxCRCB into the appropriate ready queue
|
85 |
|
|
* for the priority. It is inserted at the end of the list.
|
86 |
|
|
*
|
87 |
|
|
* This macro accesses the co-routine ready lists and therefore must not be
|
88 |
|
|
* used from within an ISR.
|
89 |
|
|
*/
|
90 |
|
|
#define prvAddCoRoutineToReadyQueue( pxCRCB ) \
|
91 |
|
|
{ \
|
92 |
|
|
if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
|
93 |
|
|
{ \
|
94 |
|
|
uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
|
95 |
|
|
} \
|
96 |
|
|
vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
/*
|
100 |
|
|
* Utility to ready all the lists used by the scheduler. This is called
|
101 |
|
|
* automatically upon the creation of the first co-routine.
|
102 |
|
|
*/
|
103 |
|
|
static void prvInitialiseCoRoutineLists( void );
|
104 |
|
|
|
105 |
|
|
/*
|
106 |
|
|
* Co-routines that are readied by an interrupt cannot be placed directly into
|
107 |
|
|
* the ready lists (there is no mutual exclusion). Instead they are placed in
|
108 |
|
|
* in the pending ready list in order that they can later be moved to the ready
|
109 |
|
|
* list by the co-routine scheduler.
|
110 |
|
|
*/
|
111 |
|
|
static void prvCheckPendingReadyList( void );
|
112 |
|
|
|
113 |
|
|
/*
|
114 |
|
|
* Macro that looks at the list of co-routines that are currently delayed to
|
115 |
|
|
* see if any require waking.
|
116 |
|
|
*
|
117 |
|
|
* Co-routines are stored in the queue in the order of their wake time -
|
118 |
|
|
* meaning once one co-routine has been found whose timer has not expired
|
119 |
|
|
* we need not look any further down the list.
|
120 |
|
|
*/
|
121 |
|
|
static void prvCheckDelayedList( void );
|
122 |
|
|
|
123 |
|
|
/*-----------------------------------------------------------*/
|
124 |
|
|
|
125 |
|
|
signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
|
126 |
|
|
{
|
127 |
|
|
signed portBASE_TYPE xReturn;
|
128 |
|
|
corCRCB *pxCoRoutine;
|
129 |
|
|
|
130 |
|
|
/* Allocate the memory that will store the co-routine control block. */
|
131 |
|
|
pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
|
132 |
|
|
if( pxCoRoutine )
|
133 |
|
|
{
|
134 |
|
|
/* If pxCurrentCoRoutine is NULL then this is the first co-routine to
|
135 |
|
|
be created and the co-routine data structures need initialising. */
|
136 |
|
|
if( pxCurrentCoRoutine == NULL )
|
137 |
|
|
{
|
138 |
|
|
pxCurrentCoRoutine = pxCoRoutine;
|
139 |
|
|
prvInitialiseCoRoutineLists();
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/* Check the priority is within limits. */
|
143 |
|
|
if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
|
144 |
|
|
{
|
145 |
|
|
uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
/* Fill out the co-routine control block from the function parameters. */
|
149 |
|
|
pxCoRoutine->uxState = corINITIAL_STATE;
|
150 |
|
|
pxCoRoutine->uxPriority = uxPriority;
|
151 |
|
|
pxCoRoutine->uxIndex = uxIndex;
|
152 |
|
|
pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
|
153 |
|
|
|
154 |
|
|
/* Initialise all the other co-routine control block parameters. */
|
155 |
|
|
vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
|
156 |
|
|
vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
|
157 |
|
|
|
158 |
|
|
/* Set the co-routine control block as a link back from the xListItem.
|
159 |
|
|
This is so we can get back to the containing CRCB from a generic item
|
160 |
|
|
in a list. */
|
161 |
|
|
listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
|
162 |
|
|
listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
|
163 |
|
|
|
164 |
|
|
/* Event lists are always in priority order. */
|
165 |
|
|
listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
|
166 |
|
|
|
167 |
|
|
/* Now the co-routine has been initialised it can be added to the ready
|
168 |
|
|
list at the correct priority. */
|
169 |
|
|
prvAddCoRoutineToReadyQueue( pxCoRoutine );
|
170 |
|
|
|
171 |
|
|
xReturn = pdPASS;
|
172 |
|
|
}
|
173 |
|
|
else
|
174 |
|
|
{
|
175 |
|
|
xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
return xReturn;
|
179 |
|
|
}
|
180 |
|
|
/*-----------------------------------------------------------*/
|
181 |
|
|
|
182 |
|
|
void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
|
183 |
|
|
{
|
184 |
|
|
portTickType xTimeToWake;
|
185 |
|
|
|
186 |
|
|
/* Calculate the time to wake - this may overflow but this is
|
187 |
|
|
not a problem. */
|
188 |
|
|
xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
|
189 |
|
|
|
190 |
|
|
/* We must remove ourselves from the ready list before adding
|
191 |
|
|
ourselves to the blocked list as the same list item is used for
|
192 |
|
|
both lists. */
|
193 |
|
|
vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
|
194 |
|
|
|
195 |
|
|
/* The list item will be inserted in wake time order. */
|
196 |
|
|
listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
|
197 |
|
|
|
198 |
|
|
if( xTimeToWake < xCoRoutineTickCount )
|
199 |
|
|
{
|
200 |
|
|
/* Wake time has overflowed. Place this item in the
|
201 |
|
|
overflow list. */
|
202 |
|
|
vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
|
203 |
|
|
}
|
204 |
|
|
else
|
205 |
|
|
{
|
206 |
|
|
/* The wake time has not overflowed, so we can use the
|
207 |
|
|
current block list. */
|
208 |
|
|
vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
if( pxEventList )
|
212 |
|
|
{
|
213 |
|
|
/* Also add the co-routine to an event list. If this is done then the
|
214 |
|
|
function must be called with interrupts disabled. */
|
215 |
|
|
vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
|
216 |
|
|
}
|
217 |
|
|
}
|
218 |
|
|
/*-----------------------------------------------------------*/
|
219 |
|
|
|
220 |
|
|
static void prvCheckPendingReadyList( void )
|
221 |
|
|
{
|
222 |
|
|
/* Are there any co-routines waiting to get moved to the ready list? These
|
223 |
|
|
are co-routines that have been readied by an ISR. The ISR cannot access
|
224 |
|
|
the ready lists itself. */
|
225 |
|
|
while( !listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) )
|
226 |
|
|
{
|
227 |
|
|
corCRCB *pxUnblockedCRCB;
|
228 |
|
|
|
229 |
|
|
/* The pending ready list can be accessed by an ISR. */
|
230 |
|
|
portDISABLE_INTERRUPTS();
|
231 |
|
|
{
|
232 |
|
|
pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
|
233 |
|
|
vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
|
234 |
|
|
}
|
235 |
|
|
portENABLE_INTERRUPTS();
|
236 |
|
|
|
237 |
|
|
vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
|
238 |
|
|
prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
|
239 |
|
|
}
|
240 |
|
|
}
|
241 |
|
|
/*-----------------------------------------------------------*/
|
242 |
|
|
|
243 |
|
|
static void prvCheckDelayedList( void )
|
244 |
|
|
{
|
245 |
|
|
corCRCB *pxCRCB;
|
246 |
|
|
|
247 |
|
|
xPassedTicks = xTaskGetTickCount() - xLastTickCount;
|
248 |
|
|
while( xPassedTicks )
|
249 |
|
|
{
|
250 |
|
|
xCoRoutineTickCount++;
|
251 |
|
|
xPassedTicks--;
|
252 |
|
|
|
253 |
|
|
/* If the tick count has overflowed we need to swap the ready lists. */
|
254 |
|
|
if( xCoRoutineTickCount == 0 )
|
255 |
|
|
{
|
256 |
|
|
xList * pxTemp;
|
257 |
|
|
|
258 |
|
|
/* Tick count has overflowed so we need to swap the delay lists. If there are
|
259 |
|
|
any items in pxDelayedCoRoutineList here then there is an error! */
|
260 |
|
|
pxTemp = pxDelayedCoRoutineList;
|
261 |
|
|
pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
|
262 |
|
|
pxOverflowDelayedCoRoutineList = pxTemp;
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
/* See if this tick has made a timeout expire. */
|
266 |
|
|
while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
|
267 |
|
|
{
|
268 |
|
|
if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
|
269 |
|
|
{
|
270 |
|
|
/* Timeout not yet expired. */
|
271 |
|
|
break;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
portDISABLE_INTERRUPTS();
|
275 |
|
|
{
|
276 |
|
|
/* The event could have occurred just before this critical
|
277 |
|
|
section. If this is the case then the generic list item will
|
278 |
|
|
have been moved to the pending ready list and the following
|
279 |
|
|
line is still valid. Also the pvContainer parameter will have
|
280 |
|
|
been set to NULL so the following lines are also valid. */
|
281 |
|
|
vListRemove( &( pxCRCB->xGenericListItem ) );
|
282 |
|
|
|
283 |
|
|
/* Is the co-routine waiting on an event also? */
|
284 |
|
|
if( pxCRCB->xEventListItem.pvContainer )
|
285 |
|
|
{
|
286 |
|
|
vListRemove( &( pxCRCB->xEventListItem ) );
|
287 |
|
|
}
|
288 |
|
|
}
|
289 |
|
|
portENABLE_INTERRUPTS();
|
290 |
|
|
|
291 |
|
|
prvAddCoRoutineToReadyQueue( pxCRCB );
|
292 |
|
|
}
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
xLastTickCount = xCoRoutineTickCount;
|
296 |
|
|
}
|
297 |
|
|
/*-----------------------------------------------------------*/
|
298 |
|
|
|
299 |
|
|
void vCoRoutineSchedule( void )
|
300 |
|
|
{
|
301 |
|
|
/* See if any co-routines readied by events need moving to the ready lists. */
|
302 |
|
|
prvCheckPendingReadyList();
|
303 |
|
|
|
304 |
|
|
/* See if any delayed co-routines have timed out. */
|
305 |
|
|
prvCheckDelayedList();
|
306 |
|
|
|
307 |
|
|
/* Find the highest priority queue that contains ready co-routines. */
|
308 |
|
|
while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
|
309 |
|
|
{
|
310 |
|
|
if( uxTopCoRoutineReadyPriority == 0 )
|
311 |
|
|
{
|
312 |
|
|
/* No more co-routines to check. */
|
313 |
|
|
return;
|
314 |
|
|
}
|
315 |
|
|
--uxTopCoRoutineReadyPriority;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
/* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
|
319 |
|
|
of the same priority get an equal share of the processor time. */
|
320 |
|
|
listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
|
321 |
|
|
|
322 |
|
|
/* Call the co-routine. */
|
323 |
|
|
( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
|
324 |
|
|
|
325 |
|
|
return;
|
326 |
|
|
}
|
327 |
|
|
/*-----------------------------------------------------------*/
|
328 |
|
|
|
329 |
|
|
static void prvInitialiseCoRoutineLists( void )
|
330 |
|
|
{
|
331 |
|
|
unsigned portBASE_TYPE uxPriority;
|
332 |
|
|
|
333 |
|
|
for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
|
334 |
|
|
{
|
335 |
|
|
vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
|
336 |
|
|
}
|
337 |
|
|
|
338 |
|
|
vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
|
339 |
|
|
vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
|
340 |
|
|
vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
|
341 |
|
|
|
342 |
|
|
/* Start with pxDelayedCoRoutineList using list1 and the
|
343 |
|
|
pxOverflowDelayedCoRoutineList using list2. */
|
344 |
|
|
pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
|
345 |
|
|
pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
|
346 |
|
|
}
|
347 |
|
|
/*-----------------------------------------------------------*/
|
348 |
|
|
|
349 |
|
|
signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
|
350 |
|
|
{
|
351 |
|
|
corCRCB *pxUnblockedCRCB;
|
352 |
|
|
signed portBASE_TYPE xReturn;
|
353 |
|
|
|
354 |
|
|
/* This function is called from within an interrupt. It can only access
|
355 |
|
|
event lists and the pending ready list. */
|
356 |
|
|
pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
|
357 |
|
|
vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
|
358 |
|
|
vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
|
359 |
|
|
|
360 |
|
|
if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
|
361 |
|
|
{
|
362 |
|
|
xReturn = pdTRUE;
|
363 |
|
|
}
|
364 |
|
|
else
|
365 |
|
|
{
|
366 |
|
|
xReturn = pdFALSE;
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
return xReturn;
|
370 |
|
|
}
|
371 |
|
|
|