1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
40
41
42
43
44
45
53
54
55
56
57
58
59
60
61
62
63
70
78
79
83
84
85
91
92
93
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
124
129
130
131
135
136
137
138
139
140
141
142
143
144
145
146
149
150
151
152
153
154
155
157
158
159
160
161
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
181
182
183
186
187
188
189
190
191
197
203
204
210
211
212
213
214
215
216
217
220
221
222
223
224
225
226
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
256
257
258
259
260
261
262
263
264
265
266
271
272
273
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
304
305
306
307
308
309
310
311
312
313
323
324
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
344
345
346
347
348
349
351
352
353
354
355
356
357
358
359
360
361
362
363
366
367
368
369
370
374
378
379
380
381
382
383
384
385
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* ... */
#include "FreeRTOS.h"
#include "task.h"
#include "croutine.h"
#if ( configUSE_CO_ROUTINES != 0 )
/* ... */
#ifdef portREMOVE_STATIC_QUALIFIER
#define static
#endif
static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ];
static List_t xDelayedCoRoutineList1;
static List_t xDelayedCoRoutineList2;
static List_t * pxDelayedCoRoutineList = NULL;
static List_t * pxOverflowDelayedCoRoutineList = NULL;
static List_t xPendingReadyCoRoutineList;
Lists for ready and blocked co-routines.
CRCB_t * pxCurrentCoRoutine = NULL;
static UBaseType_t uxTopCoRoutineReadyPriority = ( UBaseType_t ) 0U;
static TickType_t xCoRoutineTickCount = ( TickType_t ) 0U;
static TickType_t xLastTickCount = ( TickType_t ) 0U;
static TickType_t xPassedTicks = ( TickType_t ) 0U;
#define corINITIAL_STATE ( 0 )
/* ... */
#define prvAddCoRoutineToReadyQueue( pxCRCB ) \
do { \
if( ( pxCRCB )->uxPriority > uxTopCoRoutineReadyPriority ) \
{ \
uxTopCoRoutineReadyPriority = ( pxCRCB )->uxPriority; \
...} \
vListInsertEnd( ( List_t * ) &( pxReadyCoRoutineLists[ ( pxCRCB )->uxPriority ] ), &( ( pxCRCB )->xGenericListItem ) ); \
...} while( 0 )...
/* ... */
static void prvInitialiseCoRoutineLists( void );
/* ... */
static void prvCheckPendingReadyList( void );
/* ... */
static void prvCheckDelayedList( void );
BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode,
UBaseType_t uxPriority,
UBaseType_t uxIndex )
{
BaseType_t xReturn;
CRCB_t * pxCoRoutine;
traceENTER_xCoRoutineCreate( pxCoRoutineCode, uxPriority, uxIndex );
pxCoRoutine = ( CRCB_t * ) pvPortMalloc( sizeof( CRCB_t ) );
if( pxCoRoutine )
{
/* ... */
if( pxCurrentCoRoutine == NULL )
{
pxCurrentCoRoutine = pxCoRoutine;
prvInitialiseCoRoutineLists();
}if (pxCurrentCoRoutine == NULL) { ... }
if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
{
uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
}if (uxPriority >= configMAX_CO_ROUTINE_PRIORITIES) { ... }
pxCoRoutine->uxState = corINITIAL_STATE;
pxCoRoutine->uxPriority = uxPriority;
pxCoRoutine->uxIndex = uxIndex;
pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
/* ... */
listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), ( ( TickType_t ) configMAX_CO_ROUTINE_PRIORITIES - ( TickType_t ) uxPriority ) );
/* ... */
prvAddCoRoutineToReadyQueue( pxCoRoutine );
xReturn = pdPASS;
}if (pxCoRoutine) { ... }
else
{
xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
}else { ... }
traceRETURN_xCoRoutineCreate( xReturn );
return xReturn;
}xCoRoutineCreate (crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex) { ... }
void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay,
List_t * pxEventList )
{
TickType_t xTimeToWake;
traceENTER_vCoRoutineAddToDelayedList( xTicksToDelay, pxEventList );
/* ... */
xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
/* ... */
( void ) uxListRemove( ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
if( xTimeToWake < xCoRoutineTickCount )
{
/* ... */
vListInsert( ( List_t * ) pxOverflowDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
}if (xTimeToWake < xCoRoutineTickCount) { ... }
else
{
/* ... */
vListInsert( ( List_t * ) pxDelayedCoRoutineList, ( ListItem_t * ) &( pxCurrentCoRoutine->xGenericListItem ) );
}else { ... }
if( pxEventList )
{
/* ... */
vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
}if (pxEventList) { ... }
traceRETURN_vCoRoutineAddToDelayedList();
}vCoRoutineAddToDelayedList (TickType_t xTicksToDelay, List_t * pxEventList) { ... }
static void prvCheckPendingReadyList( void )
{
/* ... */
while( listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE )
{
CRCB_t * pxUnblockedCRCB;
portDISABLE_INTERRUPTS();
{
pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyCoRoutineList ) );
( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
...}
portENABLE_INTERRUPTS();
( void ) uxListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
}while (listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) == pdFALSE) { ... }
}prvCheckPendingReadyList (void) { ... }
static void prvCheckDelayedList( void )
{
CRCB_t * pxCRCB;
xPassedTicks = xTaskGetTickCount() - xLastTickCount;
while( xPassedTicks )
{
xCoRoutineTickCount++;
xPassedTicks--;
if( xCoRoutineTickCount == 0 )
{
List_t * pxTemp;
/* ... */
pxTemp = pxDelayedCoRoutineList;
pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
pxOverflowDelayedCoRoutineList = pxTemp;
}if (xCoRoutineTickCount == 0) { ... }
while( listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE )
{
pxCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList );
if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
{
break;
}if (xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) )) { ... }
portDISABLE_INTERRUPTS();
{
/* ... */
( void ) uxListRemove( &( pxCRCB->xGenericListItem ) );
if( pxCRCB->xEventListItem.pxContainer )
{
( void ) uxListRemove( &( pxCRCB->xEventListItem ) );
}if (pxCRCB->xEventListItem.pxContainer) { ... }
...}
portENABLE_INTERRUPTS();
prvAddCoRoutineToReadyQueue( pxCRCB );
}while (listLIST_IS_EMPTY( pxDelayedCoRoutineList ) == pdFALSE) { ... }
}while (xPassedTicks) { ... }
xLastTickCount = xCoRoutineTickCount;
}prvCheckDelayedList (void) { ... }
void vCoRoutineSchedule( void )
{
traceENTER_vCoRoutineSchedule();
/* ... */
if( pxDelayedCoRoutineList != NULL )
{
prvCheckPendingReadyList();
prvCheckDelayedList();
while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
{
if( uxTopCoRoutineReadyPriority == 0 )
{
return;
}if (uxTopCoRoutineReadyPriority == 0) { ... }
--uxTopCoRoutineReadyPriority;
}while (listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) )) { ... }
/* ... */
listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
}if (pxDelayedCoRoutineList != NULL) { ... }
traceRETURN_vCoRoutineSchedule();
}vCoRoutineSchedule (void) { ... }
static void prvInitialiseCoRoutineLists( void )
{
UBaseType_t uxPriority;
for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
{
vListInitialise( ( List_t * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
}for (uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++) { ... }
vListInitialise( ( List_t * ) &xDelayedCoRoutineList1 );
vListInitialise( ( List_t * ) &xDelayedCoRoutineList2 );
vListInitialise( ( List_t * ) &xPendingReadyCoRoutineList );
/* ... */
pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
}prvInitialiseCoRoutineLists (void) { ... }
BaseType_t xCoRoutineRemoveFromEventList( const List_t * pxEventList )
{
CRCB_t * pxUnblockedCRCB;
BaseType_t xReturn;
traceENTER_xCoRoutineRemoveFromEventList( pxEventList );
/* ... */
pxUnblockedCRCB = ( CRCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
( void ) uxListRemove( &( pxUnblockedCRCB->xEventListItem ) );
vListInsertEnd( ( List_t * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
{
xReturn = pdTRUE;
}if (pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority) { ... }
else
{
xReturn = pdFALSE;
}else { ... }
traceRETURN_xCoRoutineRemoveFromEventList( xReturn );
return xReturn;
}xCoRoutineRemoveFromEventList (const List_t * pxEventList) { ... }
/* ... */
void vCoRoutineResetState( void )
{
pxDelayedCoRoutineList = NULL;
pxOverflowDelayedCoRoutineList = NULL;
pxCurrentCoRoutine = NULL;
uxTopCoRoutineReadyPriority = ( UBaseType_t ) 0U;
xCoRoutineTickCount = ( TickType_t ) 0U;
xLastTickCount = ( TickType_t ) 0U;
xPassedTicks = ( TickType_t ) 0U;
}vCoRoutineResetState (void) { ... }
Other file private variables.
/* ... */#endif