00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include "FreeRTOS.h"
00055 #include "task.h"
00056 #include "croutine.h"
00057
00058
00059
00060
00061
00062 #ifdef portREMOVE_STATIC_QUALIFIER
00063 #define static
00064 #endif
00065
00066
00067
00068 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ];
00069 static xList xDelayedCoRoutineList1;
00070 static xList xDelayedCoRoutineList2;
00071 static xList * pxDelayedCoRoutineList;
00072 static xList * pxOverflowDelayedCoRoutineList;
00073 static xList xPendingReadyCoRoutineList;
00074
00075
00076 corCRCB * pxCurrentCoRoutine = NULL;
00077 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
00078 static portTickType xCoRoutineTickCount = 0, xLastTickCount = 0, xPassedTicks = 0;
00079
00080
00081 #define corINITIAL_STATE ( 0 )
00082
00083
00084
00085
00086
00087
00088
00089
00090 #define prvAddCoRoutineToReadyQueue( pxCRCB ) \
00091 { \
00092 if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority ) \
00093 { \
00094 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority; \
00095 } \
00096 vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) ); \
00097 }
00098
00099
00100
00101
00102
00103 static void prvInitialiseCoRoutineLists( void );
00104
00105
00106
00107
00108
00109
00110
00111 static void prvCheckPendingReadyList( void );
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 static void prvCheckDelayedList( void );
00122
00123
00124
00125 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
00126 {
00127 signed portBASE_TYPE xReturn;
00128 corCRCB *pxCoRoutine;
00129
00130
00131 pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
00132 if( pxCoRoutine )
00133 {
00134
00135
00136 if( pxCurrentCoRoutine == NULL )
00137 {
00138 pxCurrentCoRoutine = pxCoRoutine;
00139 prvInitialiseCoRoutineLists();
00140 }
00141
00142
00143 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
00144 {
00145 uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
00146 }
00147
00148
00149 pxCoRoutine->uxState = corINITIAL_STATE;
00150 pxCoRoutine->uxPriority = uxPriority;
00151 pxCoRoutine->uxIndex = uxIndex;
00152 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
00153
00154
00155 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
00156 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
00157
00158
00159
00160
00161 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
00162 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
00163
00164
00165 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
00166
00167
00168
00169 prvAddCoRoutineToReadyQueue( pxCoRoutine );
00170
00171 xReturn = pdPASS;
00172 }
00173 else
00174 {
00175 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
00176 }
00177
00178 return xReturn;
00179 }
00180
00181
00182 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
00183 {
00184 portTickType xTimeToWake;
00185
00186
00187
00188 xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
00189
00190
00191
00192
00193 vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
00194
00195
00196 listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
00197
00198 if( xTimeToWake < xCoRoutineTickCount )
00199 {
00200
00201
00202 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
00203 }
00204 else
00205 {
00206
00207
00208 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
00209 }
00210
00211 if( pxEventList )
00212 {
00213
00214
00215 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
00216 }
00217 }
00218
00219
00220 static void prvCheckPendingReadyList( void )
00221 {
00222
00223
00224
00225 while( !listLIST_IS_EMPTY( &xPendingReadyCoRoutineList ) )
00226 {
00227 corCRCB *pxUnblockedCRCB;
00228
00229
00230 portDISABLE_INTERRUPTS();
00231 {
00232 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyCoRoutineList) );
00233 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
00234 }
00235 portENABLE_INTERRUPTS();
00236
00237 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
00238 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB );
00239 }
00240 }
00241
00242
00243 static void prvCheckDelayedList( void )
00244 {
00245 corCRCB *pxCRCB;
00246
00247 xPassedTicks = xTaskGetTickCount() - xLastTickCount;
00248 while( xPassedTicks )
00249 {
00250 xCoRoutineTickCount++;
00251 xPassedTicks--;
00252
00253
00254 if( xCoRoutineTickCount == 0 )
00255 {
00256 xList * pxTemp;
00257
00258
00259
00260 pxTemp = pxDelayedCoRoutineList;
00261 pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
00262 pxOverflowDelayedCoRoutineList = pxTemp;
00263 }
00264
00265
00266 while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
00267 {
00268 if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )
00269 {
00270
00271 break;
00272 }
00273
00274 portDISABLE_INTERRUPTS();
00275 {
00276
00277
00278
00279
00280
00281 vListRemove( &( pxCRCB->xGenericListItem ) );
00282
00283
00284 if( pxCRCB->xEventListItem.pvContainer )
00285 {
00286 vListRemove( &( pxCRCB->xEventListItem ) );
00287 }
00288 }
00289 portENABLE_INTERRUPTS();
00290
00291 prvAddCoRoutineToReadyQueue( pxCRCB );
00292 }
00293 }
00294
00295 xLastTickCount = xCoRoutineTickCount;
00296 }
00297
00298
00299 void vCoRoutineSchedule( void )
00300 {
00301
00302 prvCheckPendingReadyList();
00303
00304
00305 prvCheckDelayedList();
00306
00307
00308 while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
00309 {
00310 if( uxTopCoRoutineReadyPriority == 0 )
00311 {
00312
00313 return;
00314 }
00315 --uxTopCoRoutineReadyPriority;
00316 }
00317
00318
00319
00320 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
00321
00322
00323 ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
00324
00325 return;
00326 }
00327
00328
00329 static void prvInitialiseCoRoutineLists( void )
00330 {
00331 unsigned portBASE_TYPE uxPriority;
00332
00333 for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
00334 {
00335 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
00336 }
00337
00338 vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
00339 vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
00340 vListInitialise( ( xList * ) &xPendingReadyCoRoutineList );
00341
00342
00343
00344 pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
00345 pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
00346 }
00347
00348
00349 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
00350 {
00351 corCRCB *pxUnblockedCRCB;
00352 signed portBASE_TYPE xReturn;
00353
00354
00355
00356 pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
00357 vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
00358 vListInsertEnd( ( xList * ) &( xPendingReadyCoRoutineList ), &( pxUnblockedCRCB->xEventListItem ) );
00359
00360 if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
00361 {
00362 xReturn = pdTRUE;
00363 }
00364 else
00365 {
00366 xReturn = pdFALSE;
00367 }
00368
00369 return xReturn;
00370 }
00371