FreeRTOS Real Time Operating System, Modified from Kenji Arai's initial port. See freertos.org for full documentation.

Fork of FreeRTOS_on_mbed_v1 by Kenji Arai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tasks.c Source File

tasks.c

00001 /*
00002     FreeRTOS V6.0.3 - Copyright (C) 2010 Real Time Engineers Ltd.
00003 
00004     ***************************************************************************
00005     *                                                                         *
00006     * If you are:                                                             *
00007     *                                                                         *
00008     *    + New to FreeRTOS,                                                   *
00009     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
00010     *    + Looking for basic training,                                        *
00011     *    + Wanting to improve your FreeRTOS skills and productivity           *
00012     *                                                                         *
00013     * then take a look at the FreeRTOS eBook                                  *
00014     *                                                                         *
00015     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
00016     *                  http://www.FreeRTOS.org/Documentation                  *
00017     *                                                                         *
00018     * A pdf reference manual is also available.  Both are usually delivered   *
00019     * to your inbox within 20 minutes to two hours when purchased between 8am *
00020     * and 8pm GMT (although please allow up to 24 hours in case of            *
00021     * exceptional circumstances).  Thank you for your support!                *
00022     *                                                                         *
00023     ***************************************************************************
00024 
00025     This file is part of the FreeRTOS distribution.
00026 
00027     FreeRTOS is free software; you can redistribute it and/or modify it under
00028     the terms of the GNU General Public License (version 2) as published by the
00029     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
00030     ***NOTE*** The exception to the GPL is included to allow you to distribute
00031     a combined work that includes FreeRTOS without being obliged to provide the
00032     source code for proprietary components outside of the FreeRTOS kernel.
00033     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
00034     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00035     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00036     more details. You should have received a copy of the GNU General Public 
00037     License and the FreeRTOS license exception along with FreeRTOS; if not it 
00038     can be viewed here: http://www.freertos.org/a00114.html and also obtained 
00039     by writing to Richard Barry, contact details for whom are available on the
00040     FreeRTOS WEB site.
00041 
00042     1 tab == 4 spaces!
00043 
00044     http://www.FreeRTOS.org - Documentation, latest information, license and
00045     contact details.
00046 
00047     http://www.SafeRTOS.com - A version that is certified for use in safety
00048     critical systems.
00049 
00050     http://www.OpenRTOS.com - Commercial support, development, porting,
00051     licensing and training services.
00052 */
00053 
00054 
00055 #include <stdio.h>
00056 #include <stdlib.h>
00057 #include <string.h>
00058 
00059 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
00060 all the API functions to use the MPU wrappers.  That should only be done when
00061 task.h is included from an application file. */
00062 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
00063 
00064 #include "FreeRTOS.h"
00065 #include "task.h"
00066 #include "StackMacros.h"
00067 
00068 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
00069 
00070 #if 0
00071 /*
00072  * Macro to define the amount of stack available to the idle task.
00073  */
00074 #define tskIDLE_STACK_SIZE    configMINIMAL_STACK_SIZE
00075 #endif
00076 
00077 /*
00078  * Task control block.  A task control block (TCB) is allocated to each task,
00079  * and stores the context of the task.
00080  */
00081 typedef struct tskTaskControlBlock
00082 {
00083     volatile portSTACK_TYPE    *pxTopOfStack;        /*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE STRUCT. */
00084 
00085     #if ( portUSING_MPU_WRAPPERS == 1 )
00086         xMPU_SETTINGS xMPUSettings;                /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE STRUCT. */
00087     #endif    
00088     
00089     xListItem                xGenericListItem;    /*< List item used to place the TCB in ready and blocked queues. */
00090     xListItem                xEventListItem;        /*< List item used to place the TCB in event lists. */
00091     unsigned portBASE_TYPE    uxPriority;            /*< The priority of the task where 0 is the lowest priority. */
00092     portSTACK_TYPE            *pxStack;            /*< Points to the start of the stack. */
00093     signed char                pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */
00094 
00095     #if ( portSTACK_GROWTH > 0 )
00096         portSTACK_TYPE *pxEndOfStack;            /*< Used for stack overflow checking on architectures where the stack grows up from low memory. */
00097     #endif
00098 
00099     #if ( portCRITICAL_NESTING_IN_TCB == 1 )
00100         unsigned portBASE_TYPE uxCriticalNesting;
00101     #endif
00102 
00103     #if ( configUSE_TRACE_FACILITY == 1 )
00104         unsigned portBASE_TYPE    uxTCBNumber;    /*< This is used for tracing the scheduler and making debugging easier only. */
00105     #endif
00106 
00107     #if ( configUSE_MUTEXES == 1 )
00108         unsigned portBASE_TYPE uxBasePriority;    /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
00109     #endif
00110 
00111     #if ( configUSE_APPLICATION_TASK_TAG == 1 )
00112         pdTASK_HOOK_CODE pxTaskTag;
00113     #endif
00114 
00115     #if ( configGENERATE_RUN_TIME_STATS == 1 )
00116         unsigned long ulRunTimeCounter;        /*< Used for calculating how much CPU time each task is utilising. */
00117     #endif
00118 
00119 } tskTCB;
00120 
00121 
00122 /*
00123  * Some kernel aware debuggers require data to be viewed to be global, rather
00124  * than file scope.
00125  */
00126 #ifdef portREMOVE_STATIC_QUALIFIER
00127     #define static
00128 #endif
00129 
00130 /*lint -e956 */
00131 PRIVILEGED_DATA tskTCB * volatile pxCurrentTCB = NULL;
00132 
00133 /* Lists for ready and blocked tasks. --------------------*/
00134 
00135 PRIVILEGED_DATA static xList pxReadyTasksLists[ configMAX_PRIORITIES ];    /*< Prioritised ready tasks. */
00136 PRIVILEGED_DATA static xList xDelayedTaskList1;                            /*< Delayed tasks. */
00137 PRIVILEGED_DATA static xList xDelayedTaskList2;                            /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
00138 PRIVILEGED_DATA static xList * volatile pxDelayedTaskList ;                /*< Points to the delayed task list currently being used. */
00139 PRIVILEGED_DATA static xList * volatile pxOverflowDelayedTaskList;        /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
00140 PRIVILEGED_DATA static xList xPendingReadyList;                            /*< Tasks that have been readied while the scheduler was suspended.  They will be moved to the ready queue when the scheduler is resumed. */
00141 
00142 #if ( INCLUDE_vTaskDelete == 1 )
00143 
00144     PRIVILEGED_DATA static volatile xList xTasksWaitingTermination;        /*< Tasks that have been deleted - but the their memory not yet freed. */
00145     PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTasksDeleted = ( unsigned portBASE_TYPE ) 0;
00146 
00147 #endif
00148 
00149 #if ( INCLUDE_vTaskSuspend == 1 )
00150 
00151     PRIVILEGED_DATA static xList xSuspendedTaskList;                    /*< Tasks that are currently suspended. */
00152 
00153 #endif
00154 
00155 /* File private variables. --------------------------------*/
00156 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks     = ( unsigned portBASE_TYPE ) 0;
00157 PRIVILEGED_DATA static volatile portTickType xTickCount                         = ( portTickType ) 0;
00158 PRIVILEGED_DATA static unsigned portBASE_TYPE uxTopUsedPriority                     = tskIDLE_PRIORITY;
00159 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTopReadyPriority         = tskIDLE_PRIORITY;
00160 PRIVILEGED_DATA static volatile signed portBASE_TYPE xSchedulerRunning             = pdFALSE;
00161 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxSchedulerSuspended         = ( unsigned portBASE_TYPE ) pdFALSE;
00162 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxMissedTicks             = ( unsigned portBASE_TYPE ) 0;
00163 PRIVILEGED_DATA static volatile portBASE_TYPE xMissedYield                         = ( portBASE_TYPE ) pdFALSE;
00164 PRIVILEGED_DATA static volatile portBASE_TYPE xNumOfOverflows                     = ( portBASE_TYPE ) 0;
00165 PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                         = ( unsigned portBASE_TYPE ) 0;
00166 
00167 #if ( configGENERATE_RUN_TIME_STATS == 1 )
00168 
00169     PRIVILEGED_DATA static char pcStatsString[ 50 ] ;
00170     PRIVILEGED_DATA static unsigned long ulTaskSwitchedInTime = 0UL;    /*< Holds the value of a timer/counter the last time a task was switched in. */
00171     static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime ) PRIVILEGED_FUNCTION;
00172 
00173 #endif
00174 
00175 /* Debugging and trace facilities private variables and macros. ------------*/
00176 
00177 /*
00178  * The value used to fill the stack of a task when the task is created.  This
00179  * is used purely for checking the high water mark for tasks.
00180  */
00181 #define tskSTACK_FILL_BYTE    ( 0xa5 )
00182 
00183 /*
00184  * Macros used by vListTask to indicate which state a task is in.
00185  */
00186 #define tskBLOCKED_CHAR        ( ( signed char ) 'B' )
00187 #define tskREADY_CHAR        ( ( signed char ) 'R' )
00188 #define tskDELETED_CHAR        ( ( signed char ) 'D' )
00189 #define tskSUSPENDED_CHAR    ( ( signed char ) 'S' )
00190 
00191 /*
00192  * Macros and private variables used by the trace facility.
00193  */
00194 #if ( configUSE_TRACE_FACILITY == 1 )
00195 
00196     #define tskSIZE_OF_EACH_TRACE_LINE            ( ( unsigned long ) ( sizeof( unsigned long ) + sizeof( unsigned long ) ) )
00197     PRIVILEGED_DATA static volatile signed char * volatile pcTraceBuffer;
00198     PRIVILEGED_DATA static signed char *pcTraceBufferStart;
00199     PRIVILEGED_DATA static signed char *pcTraceBufferEnd;
00200     PRIVILEGED_DATA static signed portBASE_TYPE xTracing = pdFALSE;
00201     static unsigned portBASE_TYPE uxPreviousTask = 255;
00202     PRIVILEGED_DATA static char pcStatusString[ 50 ];
00203 
00204 #endif
00205 
00206 /*-----------------------------------------------------------*/
00207 
00208 /*
00209  * Macro that writes a trace of scheduler activity to a buffer.  This trace
00210  * shows which task is running when and is very useful as a debugging tool.
00211  * As this macro is called each context switch it is a good idea to undefine
00212  * it if not using the facility.
00213  */
00214 #if ( configUSE_TRACE_FACILITY == 1 )
00215 
00216     #define vWriteTraceToBuffer()                                                                    \
00217     {                                                                                                \
00218         if( xTracing )                                                                                \
00219         {                                                                                            \
00220             if( uxPreviousTask != pxCurrentTCB->uxTCBNumber )                                        \
00221             {                                                                                        \
00222                 if( ( pcTraceBuffer + tskSIZE_OF_EACH_TRACE_LINE ) < pcTraceBufferEnd )                \
00223                 {                                                                                    \
00224                     uxPreviousTask = pxCurrentTCB->uxTCBNumber;                                        \
00225                     *( unsigned long * ) pcTraceBuffer = ( unsigned long ) xTickCount;        \
00226                     pcTraceBuffer += sizeof( unsigned long );                                    \
00227                     *( unsigned long * ) pcTraceBuffer = ( unsigned long ) uxPreviousTask;    \
00228                     pcTraceBuffer += sizeof( unsigned long );                                    \
00229                 }                                                                                    \
00230                 else                                                                                \
00231                 {                                                                                    \
00232                     xTracing = pdFALSE;                                                                \
00233                 }                                                                                    \
00234             }                                                                                        \
00235         }                                                                                            \
00236     }
00237 
00238 #else
00239 
00240     #define vWriteTraceToBuffer()
00241 
00242 #endif
00243 /*-----------------------------------------------------------*/
00244 
00245 /*
00246  * Place the task represented by pxTCB into the appropriate ready queue for
00247  * the task.  It is inserted at the end of the list.  One quirk of this is
00248  * that if the task being inserted is at the same priority as the currently
00249  * executing task, then it will only be rescheduled after the currently
00250  * executing task has been rescheduled.
00251  */
00252 #define prvAddTaskToReadyQueue( pxTCB )                                                                            \
00253 {                                                                                                                \
00254     if( pxTCB->uxPriority > uxTopReadyPriority )                                                                \
00255     {                                                                                                            \
00256         uxTopReadyPriority = pxTCB->uxPriority;                                                                    \
00257     }                                                                                                            \
00258     vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) );    \
00259 }
00260 /*-----------------------------------------------------------*/
00261 
00262 /*
00263  * Macro that looks at the list of tasks that are currently delayed to see if
00264  * any require waking.
00265  *
00266  * Tasks are stored in the queue in the order of their wake time - meaning
00267  * once one tasks has been found whose timer has not expired we need not look
00268  * any further down the list.
00269  */
00270 #define prvCheckDelayedTasks()                                                                                        \
00271 {                                                                                                                    \
00272 register tskTCB *pxTCB;                                                                                                \
00273                                                                                                                     \
00274     while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ) ) != NULL )                        \
00275     {                                                                                                                \
00276         if( xTickCount < listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) ) )                                    \
00277         {                                                                                                            \
00278             break;                                                                                                    \
00279         }                                                                                                            \
00280         vListRemove( &( pxTCB->xGenericListItem ) );                                                                \
00281         /* Is the task waiting on an event also? */                                                                    \
00282         if( pxTCB->xEventListItem.pvContainer )                                                                        \
00283         {                                                                                                            \
00284             vListRemove( &( pxTCB->xEventListItem ) );                                                                \
00285         }                                                                                                            \
00286         prvAddTaskToReadyQueue( pxTCB );                                                                            \
00287     }                                                                                                                \
00288 }
00289 /*-----------------------------------------------------------*/
00290 
00291 /*
00292  * Several functions take an xTaskHandle parameter that can optionally be NULL,
00293  * where NULL is used to indicate that the handle of the currently executing
00294  * task should be used in place of the parameter.  This macro simply checks to
00295  * see if the parameter is NULL and returns a pointer to the appropriate TCB.
00296  */
00297 #define prvGetTCBFromHandle( pxHandle ) ( ( pxHandle == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) pxHandle )
00298 
00299 
00300 /* File private functions. --------------------------------*/
00301 
00302 /*
00303  * Utility to ready a TCB for a given task.  Mainly just copies the parameters
00304  * into the TCB structure.
00305  */
00306 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth ) PRIVILEGED_FUNCTION;
00307 
00308 /*
00309  * Utility to ready all the lists used by the scheduler.  This is called
00310  * automatically upon the creation of the first task.
00311  */
00312 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
00313 
00314 /*
00315  * The idle task, which as all tasks is implemented as a never ending loop.
00316  * The idle task is automatically created and added to the ready lists upon
00317  * creation of the first user task.
00318  *
00319  * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
00320  * language extensions.  The equivalent prototype for this function is:
00321  *
00322  * void prvIdleTask( void *pvParameters );
00323  *
00324  */
00325 //static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
00326 #ifdef __cplusplus
00327 extern "C" {
00328 #endif
00329     static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
00330 #ifdef __cplusplus
00331 }
00332 #endif
00333 /*
00334  * Utility to free all memory allocated by the scheduler to hold a TCB,
00335  * including the stack pointed to by the TCB.
00336  *
00337  * This does not free memory allocated by the task itself (i.e. memory
00338  * allocated by calls to pvPortMalloc from within the tasks application code).
00339  */
00340 #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
00341 
00342     static void prvDeleteTCB( tskTCB *pxTCB ) PRIVILEGED_FUNCTION;
00343 
00344 #endif
00345 
00346 /*
00347  * Used only by the idle task.  This checks to see if anything has been placed
00348  * in the list of tasks waiting to be deleted.  If so the task is cleaned up
00349  * and its TCB deleted.
00350  */
00351 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
00352 
00353 /*
00354  * Allocates memory from the heap for a TCB and associated stack.  Checks the
00355  * allocation was successful.
00356  */
00357 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer ) PRIVILEGED_FUNCTION;
00358 
00359 /*
00360  * Called from vTaskList.  vListTasks details all the tasks currently under
00361  * control of the scheduler.  The tasks may be in one of a number of lists.
00362  * prvListTaskWithinSingleList accepts a list and details the tasks from
00363  * within just that list.
00364  *
00365  * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
00366  * NORMAL APPLICATION CODE.
00367  */
00368 #if ( configUSE_TRACE_FACILITY == 1 )
00369 
00370     static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus ) PRIVILEGED_FUNCTION;
00371 
00372 #endif
00373 
00374 /*
00375  * When a task is created, the stack of the task is filled with a known value.
00376  * This function determines the 'high water mark' of the task stack by
00377  * determining how much of the stack remains at the original preset value.
00378  */
00379 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
00380 
00381     static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte ) PRIVILEGED_FUNCTION;
00382 
00383 #endif
00384 
00385 
00386 /*lint +e956 */
00387 
00388 
00389 
00390 /*-----------------------------------------------------------
00391  * TASK CREATION API documented in task.h
00392  *----------------------------------------------------------*/
00393 
00394 signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const signed char * const pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions )
00395 {
00396 signed portBASE_TYPE xReturn;
00397 tskTCB * pxNewTCB;
00398 
00399     /* Allocate the memory required by the TCB and stack for the new task,
00400     checking that the allocation was successful. */
00401     pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer );
00402 
00403     if( pxNewTCB != NULL )
00404     {
00405         portSTACK_TYPE *pxTopOfStack;
00406 
00407         #if( portUSING_MPU_WRAPPERS == 1 )
00408             /* Should the task be created in privileged mode? */
00409             portBASE_TYPE xRunPrivileged;
00410             if( ( uxPriority & portPRIVILEGE_BIT ) != 0x00 )
00411             {
00412                 xRunPrivileged = pdTRUE;
00413             }
00414             else
00415             {
00416                 xRunPrivileged = pdFALSE;
00417             }
00418             uxPriority &= ~portPRIVILEGE_BIT;
00419         #endif /* portUSING_MPU_WRAPPERS == 1 */
00420 
00421         /* Calculate the top of stack address.  This depends on whether the
00422         stack grows from high memory to low (as per the 80x86) or visa versa.
00423         portSTACK_GROWTH is used to make the result positive or negative as
00424         required by the port. */
00425         #if( portSTACK_GROWTH < 0 )
00426         {
00427             pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );
00428             pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( unsigned long ) pxTopOfStack ) & ( ( unsigned long ) ~portBYTE_ALIGNMENT_MASK  ) );
00429         }
00430         #else
00431         {
00432             pxTopOfStack = pxNewTCB->pxStack;
00433 
00434             /* If we want to use stack checking on architectures that use
00435             a positive stack growth direction then we also need to store the
00436             other extreme of the stack space. */
00437             pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );
00438         }
00439         #endif
00440 
00441         /* Setup the newly allocated TCB with the initial state of the task. */
00442         prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth );
00443 
00444         /* Initialize the TCB stack to look as if the task was already running,
00445         but had been interrupted by the scheduler.  The return address is set
00446         to the start of the task function. Once the stack has been initialised
00447         the    top of stack variable is updated. */
00448         #if( portUSING_MPU_WRAPPERS == 1 )
00449         {
00450             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
00451         }
00452         #else
00453         {
00454             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
00455         }
00456         #endif
00457 
00458         /* We are going to manipulate the task queues to add this task to a
00459         ready list, so must make sure no interrupts occur. */
00460         portENTER_CRITICAL();
00461         {
00462             uxCurrentNumberOfTasks++;
00463             if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )
00464             {
00465                 /* As this is the first task it must also be the current task. */
00466                 pxCurrentTCB =  pxNewTCB;
00467 
00468                 /* This is the first task to be created so do the preliminary
00469                 initialisation required.  We will not recover if this call
00470                 fails, but we will report the failure. */
00471                 prvInitialiseTaskLists();
00472             }
00473             else
00474             {
00475                 /* If the scheduler is not already running, make this task the
00476                 current task if it is the highest priority task to be created
00477                 so far. */
00478                 if( xSchedulerRunning == pdFALSE )
00479                 {
00480                     if( pxCurrentTCB->uxPriority <= uxPriority )
00481                     {
00482                         pxCurrentTCB = pxNewTCB;
00483                     }
00484                 }
00485             }
00486 
00487             /* Remember the top priority to make context switching faster.  Use
00488             the priority in pxNewTCB as this has been capped to a valid value. */
00489             if( pxNewTCB->uxPriority > uxTopUsedPriority )
00490             {
00491                 uxTopUsedPriority = pxNewTCB->uxPriority;
00492             }
00493 
00494             #if ( configUSE_TRACE_FACILITY == 1 )
00495             {
00496                 /* Add a counter into the TCB for tracing only. */
00497                 pxNewTCB->uxTCBNumber = uxTaskNumber;
00498             }
00499             #endif
00500             uxTaskNumber++;
00501 
00502             prvAddTaskToReadyQueue( pxNewTCB );
00503 
00504             xReturn = pdPASS;
00505             traceTASK_CREATE( pxNewTCB );
00506         }
00507         portEXIT_CRITICAL();
00508     }
00509     else
00510     {
00511         xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
00512         traceTASK_CREATE_FAILED( pxNewTCB );
00513     }
00514 
00515     if( xReturn == pdPASS )
00516     {
00517         if( ( void * ) pxCreatedTask != NULL )
00518         {
00519             /* Pass the TCB out - in an anonymous way.  The calling function/
00520             task can use this as a handle to delete the task later if
00521             required.*/
00522             *pxCreatedTask = ( xTaskHandle ) pxNewTCB;
00523         }
00524 
00525         if( xSchedulerRunning != pdFALSE )
00526         {
00527             /* If the created task is of a higher priority than the current task
00528             then it should run now. */
00529             if( pxCurrentTCB->uxPriority < uxPriority )
00530             {
00531                 portYIELD_WITHIN_API();
00532             }
00533         }
00534     }
00535 
00536     return xReturn;
00537 }
00538 /*-----------------------------------------------------------*/
00539 
00540 #if ( INCLUDE_vTaskDelete == 1 )
00541 
00542     void vTaskDelete( xTaskHandle pxTaskToDelete )
00543     {
00544     tskTCB *pxTCB;
00545 
00546         portENTER_CRITICAL();
00547         {
00548             /* Ensure a yield is performed if the current task is being
00549             deleted. */
00550             if( pxTaskToDelete == pxCurrentTCB )
00551             {
00552                 pxTaskToDelete = NULL;
00553             }
00554 
00555             /* If null is passed in here then we are deleting ourselves. */
00556             pxTCB = prvGetTCBFromHandle( pxTaskToDelete );
00557 
00558             /* Remove task from the ready list and place in the    termination list.
00559             This will stop the task from be scheduled.  The idle task will check
00560             the termination list and free up any memory allocated by the
00561             scheduler for the TCB and stack. */
00562             vListRemove( &( pxTCB->xGenericListItem ) );
00563 
00564             /* Is the task waiting on an event also? */
00565             if( pxTCB->xEventListItem.pvContainer )
00566             {
00567                 vListRemove( &( pxTCB->xEventListItem ) );
00568             }
00569 
00570             vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );
00571 
00572             /* Increment the ucTasksDeleted variable so the idle task knows
00573             there is a task that has been deleted and that it should therefore
00574             check the xTasksWaitingTermination list. */
00575             ++uxTasksDeleted;
00576 
00577             /* Increment the uxTaskNumberVariable also so kernel aware debuggers
00578             can detect that the task lists need re-generating. */
00579             uxTaskNumber++;
00580 
00581             traceTASK_DELETE( pxTCB );
00582         }
00583         portEXIT_CRITICAL();
00584 
00585         /* Force a reschedule if we have just deleted the current task. */
00586         if( xSchedulerRunning != pdFALSE )
00587         {
00588             if( ( void * ) pxTaskToDelete == NULL )
00589             {
00590                 portYIELD_WITHIN_API();
00591             }
00592         }
00593     }
00594 
00595 #endif
00596 
00597 
00598 
00599 
00600 
00601 
00602 /*-----------------------------------------------------------
00603  * TASK CONTROL API documented in task.h
00604  *----------------------------------------------------------*/
00605 
00606 #if ( INCLUDE_vTaskDelayUntil == 1 )
00607 
00608     void vTaskDelayUntil( portTickType * const pxPreviousWakeTime, portTickType xTimeIncrement )
00609     {
00610     portTickType xTimeToWake;
00611     portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;
00612 
00613         vTaskSuspendAll();
00614         {
00615             /* Generate the tick time at which the task wants to wake. */
00616             xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
00617 
00618             if( xTickCount < *pxPreviousWakeTime )
00619             {
00620                 /* The tick count has overflowed since this function was
00621                 lasted called.  In this case the only time we should ever
00622                 actually delay is if the wake time has also    overflowed,
00623                 and the wake time is greater than the tick time.  When this
00624                 is the case it is as if neither time had overflowed. */
00625                 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xTickCount ) )
00626                 {
00627                     xShouldDelay = pdTRUE;
00628                 }
00629             }
00630             else
00631             {
00632                 /* The tick time has not overflowed.  In this case we will
00633                 delay if either the wake time has overflowed, and/or the
00634                 tick time is less than the wake time. */
00635                 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xTickCount ) )
00636                 {
00637                     xShouldDelay = pdTRUE;
00638                 }
00639             }
00640 
00641             /* Update the wake time ready for the next call. */
00642             *pxPreviousWakeTime = xTimeToWake;
00643 
00644             if( xShouldDelay )
00645             {
00646                 traceTASK_DELAY_UNTIL();
00647 
00648                 /* We must remove ourselves from the ready list before adding
00649                 ourselves to the blocked list as the same list item is used for
00650                 both lists. */
00651                 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
00652 
00653                 /* The list item will be inserted in wake time order. */
00654                 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
00655 
00656                 if( xTimeToWake < xTickCount )
00657                 {
00658                     /* Wake time has overflowed.  Place this item in the
00659                     overflow list. */
00660                     vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
00661                 }
00662                 else
00663                 {
00664                     /* The wake time has not overflowed, so we can use the
00665                     current block list. */
00666                     vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
00667                 }
00668             }
00669         }
00670         xAlreadyYielded = xTaskResumeAll();
00671 
00672         /* Force a reschedule if xTaskResumeAll has not already done so, we may
00673         have put ourselves to sleep. */
00674         if( !xAlreadyYielded )
00675         {
00676             portYIELD_WITHIN_API();
00677         }
00678     }
00679 
00680 #endif
00681 /*-----------------------------------------------------------*/
00682 
00683 #if ( INCLUDE_vTaskDelay == 1 )
00684 
00685     void vTaskDelay( portTickType xTicksToDelay )
00686     {
00687     portTickType xTimeToWake;
00688     signed portBASE_TYPE xAlreadyYielded = pdFALSE;
00689 
00690         /* A delay time of zero just forces a reschedule. */
00691         if( xTicksToDelay > ( portTickType ) 0 )
00692         {
00693             vTaskSuspendAll();
00694             {
00695                 traceTASK_DELAY();
00696 
00697                 /* A task that is removed from the event list while the
00698                 scheduler is suspended will not get placed in the ready
00699                 list or removed from the blocked list until the scheduler
00700                 is resumed.
00701 
00702                 This task cannot be in an event list as it is the currently
00703                 executing task. */
00704 
00705                 /* Calculate the time to wake - this may overflow but this is
00706                 not a problem. */
00707                 xTimeToWake = xTickCount + xTicksToDelay;
00708 
00709                 /* We must remove ourselves from the ready list before adding
00710                 ourselves to the blocked list as the same list item is used for
00711                 both lists. */
00712                 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
00713 
00714                 /* The list item will be inserted in wake time order. */
00715                 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
00716 
00717                 if( xTimeToWake < xTickCount )
00718                 {
00719                     /* Wake time has overflowed.  Place this item in the
00720                     overflow list. */
00721                     vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
00722                 }
00723                 else
00724                 {
00725                     /* The wake time has not overflowed, so we can use the
00726                     current block list. */
00727                     vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
00728                 }
00729             }
00730             xAlreadyYielded = xTaskResumeAll();
00731         }
00732 
00733         /* Force a reschedule if xTaskResumeAll has not already done so, we may
00734         have put ourselves to sleep. */
00735         if( !xAlreadyYielded )
00736         {
00737             portYIELD_WITHIN_API();
00738         }
00739     }
00740 
00741 #endif
00742 /*-----------------------------------------------------------*/
00743 
00744 #if ( INCLUDE_uxTaskPriorityGet == 1 )
00745 
00746     unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )
00747     {
00748     tskTCB *pxTCB;
00749     unsigned portBASE_TYPE uxReturn;
00750 
00751         portENTER_CRITICAL();
00752         {
00753             /* If null is passed in here then we are changing the
00754             priority of the calling function. */
00755             pxTCB = prvGetTCBFromHandle( pxTask );
00756             uxReturn = pxTCB->uxPriority;
00757         }
00758         portEXIT_CRITICAL();
00759 
00760         return uxReturn;
00761     }
00762 
00763 #endif
00764 /*-----------------------------------------------------------*/
00765 
00766 #if ( INCLUDE_vTaskPrioritySet == 1 )
00767 
00768     void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )
00769     {
00770     tskTCB *pxTCB;
00771     unsigned portBASE_TYPE uxCurrentPriority, xYieldRequired = pdFALSE;
00772 
00773         /* Ensure the new priority is valid. */
00774         if( uxNewPriority >= configMAX_PRIORITIES )
00775         {
00776             uxNewPriority = configMAX_PRIORITIES - 1;
00777         }
00778 
00779         portENTER_CRITICAL();
00780         {
00781             if( pxTask == pxCurrentTCB )
00782             {
00783                 pxTask = NULL;
00784             }
00785 
00786             /* If null is passed in here then we are changing the
00787             priority of the calling function. */
00788             pxTCB = prvGetTCBFromHandle( pxTask );
00789 
00790             traceTASK_PRIORITY_SET( pxTask, uxNewPriority );
00791 
00792             #if ( configUSE_MUTEXES == 1 )
00793             {
00794                 uxCurrentPriority = pxTCB->uxBasePriority;
00795             }
00796             #else
00797             {
00798                 uxCurrentPriority = pxTCB->uxPriority;
00799             }
00800             #endif
00801 
00802             if( uxCurrentPriority != uxNewPriority )
00803             {
00804                 /* The priority change may have readied a task of higher
00805                 priority than the calling task. */
00806                 if( uxNewPriority > uxCurrentPriority )
00807                 {
00808                     if( pxTask != NULL )
00809                     {
00810                         /* The priority of another task is being raised.  If we
00811                         were raising the priority of the currently running task
00812                         there would be no need to switch as it must have already
00813                         been the highest priority task. */
00814                         xYieldRequired = pdTRUE;
00815                     }
00816                 }
00817                 else if( pxTask == NULL )
00818                 {
00819                     /* Setting our own priority down means there may now be another
00820                     task of higher priority that is ready to execute. */
00821                     xYieldRequired = pdTRUE;
00822                 }
00823 
00824 
00825 
00826                 #if ( configUSE_MUTEXES == 1 )
00827                 {
00828                     /* Only change the priority being used if the task is not
00829                     currently using an inherited priority. */
00830                     if( pxTCB->uxBasePriority == pxTCB->uxPriority )
00831                     {
00832                         pxTCB->uxPriority = uxNewPriority;
00833                     }
00834 
00835                     /* The base priority gets set whatever. */
00836                     pxTCB->uxBasePriority = uxNewPriority;
00837                 }
00838                 #else
00839                 {
00840                     pxTCB->uxPriority = uxNewPriority;
00841                 }
00842                 #endif
00843 
00844                 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) );
00845 
00846                 /* If the task is in the blocked or suspended list we need do
00847                 nothing more than change it's priority variable. However, if
00848                 the task is in a ready list it needs to be removed and placed
00849                 in the queue appropriate to its new priority. */
00850                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )
00851                 {
00852                     /* The task is currently in its ready list - remove before adding
00853                     it to it's new ready list.  As we are in a critical section we
00854                     can do this even if the scheduler is suspended. */
00855                     vListRemove( &( pxTCB->xGenericListItem ) );
00856                     prvAddTaskToReadyQueue( pxTCB );
00857                 }
00858 
00859                 if( xYieldRequired == pdTRUE )
00860                 {
00861                     portYIELD_WITHIN_API();
00862                 }
00863             }
00864         }
00865         portEXIT_CRITICAL();
00866     }
00867 
00868 #endif
00869 /*-----------------------------------------------------------*/
00870 
00871 #if ( INCLUDE_vTaskSuspend == 1 )
00872 
00873     void vTaskSuspend( xTaskHandle pxTaskToSuspend )
00874     {
00875     tskTCB *pxTCB;
00876 
00877         portENTER_CRITICAL();
00878         {
00879             /* Ensure a yield is performed if the current task is being
00880             suspended. */
00881             if( pxTaskToSuspend == pxCurrentTCB )
00882             {
00883                 pxTaskToSuspend = NULL;
00884             }
00885 
00886             /* If null is passed in here then we are suspending ourselves. */
00887             pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
00888 
00889             traceTASK_SUSPEND( pxTCB );
00890 
00891             /* Remove task from the ready/delayed list and place in the    suspended list. */
00892             vListRemove( &( pxTCB->xGenericListItem ) );
00893 
00894             /* Is the task waiting on an event also? */
00895             if( pxTCB->xEventListItem.pvContainer )
00896             {
00897                 vListRemove( &( pxTCB->xEventListItem ) );
00898             }
00899 
00900             vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
00901         }
00902         portEXIT_CRITICAL();
00903 
00904         /* We may have just suspended the current task. */
00905         if( ( void * ) pxTaskToSuspend == NULL )
00906         {
00907             portYIELD_WITHIN_API();
00908         }
00909     }
00910 
00911 #endif
00912 /*-----------------------------------------------------------*/
00913 
00914 #if ( INCLUDE_vTaskSuspend == 1 )
00915 
00916     signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask )
00917     {
00918     portBASE_TYPE xReturn = pdFALSE;
00919     const tskTCB * const pxTCB = ( tskTCB * ) xTask;
00920 
00921         /* Is the task we are attempting to resume actually in the
00922         suspended list? */
00923         if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
00924         {
00925             /* Has the task already been resumed from within an ISR? */
00926             if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
00927             {
00928                 /* Is it in the suspended list because it is in the
00929                 Suspended state?  It is possible to be in the suspended
00930                 list because it is blocked on a task with no timeout
00931                 specified. */
00932                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) == pdTRUE )
00933                 {
00934                     xReturn = pdTRUE;
00935                 }
00936             }
00937         }
00938 
00939         return xReturn;
00940     }
00941 
00942 #endif
00943 /*-----------------------------------------------------------*/
00944 
00945 #if ( INCLUDE_vTaskSuspend == 1 )
00946 
00947     void vTaskResume( xTaskHandle pxTaskToResume )
00948     {
00949     tskTCB *pxTCB;
00950 
00951         /* Remove the task from whichever list it is currently in, and place
00952         it in the ready list. */
00953         pxTCB = ( tskTCB * ) pxTaskToResume;
00954 
00955         /* The parameter cannot be NULL as it is impossible to resume the
00956         currently executing task. */
00957         if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )
00958         {
00959             portENTER_CRITICAL();
00960             {
00961                 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
00962                 {
00963                     traceTASK_RESUME( pxTCB );
00964 
00965                     /* As we are in a critical section we can access the ready
00966                     lists even if the scheduler is suspended. */
00967                     vListRemove(  &( pxTCB->xGenericListItem ) );
00968                     prvAddTaskToReadyQueue( pxTCB );
00969 
00970                     /* We may have just resumed a higher priority task. */
00971                     if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
00972                     {
00973                         /* This yield may not cause the task just resumed to run, but
00974                         will leave the lists in the correct state for the next yield. */
00975                         portYIELD_WITHIN_API();
00976                     }
00977                 }
00978             }
00979             portEXIT_CRITICAL();
00980         }
00981     }
00982 
00983 #endif
00984 
00985 /*-----------------------------------------------------------*/
00986 
00987 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
00988 
00989     portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )
00990     {
00991     portBASE_TYPE xYieldRequired = pdFALSE;
00992     tskTCB *pxTCB;
00993 
00994         pxTCB = ( tskTCB * ) pxTaskToResume;
00995 
00996         if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
00997         {
00998             traceTASK_RESUME_FROM_ISR( pxTCB );
00999 
01000             if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
01001             {
01002                 xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );
01003                 vListRemove(  &( pxTCB->xGenericListItem ) );
01004                 prvAddTaskToReadyQueue( pxTCB );
01005             }
01006             else
01007             {
01008                 /* We cannot access the delayed or ready lists, so will hold this
01009                 task pending until the scheduler is resumed, at which point a
01010                 yield will be performed if necessary. */
01011                 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
01012             }
01013         }
01014 
01015         return xYieldRequired;
01016     }
01017 
01018 #endif
01019 
01020 
01021 
01022 
01023 /*-----------------------------------------------------------
01024  * PUBLIC SCHEDULER CONTROL documented in task.h
01025  *----------------------------------------------------------*/
01026 
01027 // Modified by  Kenji Arai / JH1PJL, October 30th,2010
01028 // move to port_asm.c
01029 #if 0
01030 void vTaskStartScheduler( void )
01031 {
01032 portBASE_TYPE xReturn;
01033 
01034     /* Add the idle task at the lowest priority. */
01035     xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), ( xTaskHandle * ) NULL );
01036 
01037     if( xReturn == pdPASS )
01038     {
01039         /* Interrupts are turned off here, to ensure a tick does not occur
01040         before or during the call to xPortStartScheduler().  The stacks of
01041         the created tasks contain a status word with interrupts switched on
01042         so interrupts will automatically get re-enabled when the first task
01043         starts to run.
01044 
01045         STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE
01046         DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */
01047         portDISABLE_INTERRUPTS();
01048 
01049         xSchedulerRunning = pdTRUE;
01050         xTickCount = ( portTickType ) 0;
01051 
01052         /* If configGENERATE_RUN_TIME_STATS is defined then the following
01053         macro must be defined to configure the timer/counter used to generate
01054         the run time counter time base. */
01055         portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
01056 
01057         /* Setting up the timer tick is hardware specific and thus in the
01058         portable interface. */
01059         if( xPortStartScheduler() )
01060         {
01061             /* Should not reach here as if the scheduler is running the
01062             function will not return. */
01063         }
01064         else
01065         {
01066             /* Should only reach here if a task calls xTaskEndScheduler(). */
01067         }
01068     }
01069 }
01070 /*-----------------------------------------------------------*/
01071 
01072 void vTaskEndScheduler( void )
01073 {
01074     /* Stop the scheduler interrupts and call the portable scheduler end
01075     routine so the original ISRs can be restored if necessary.  The port
01076     layer must ensure interrupts enable    bit is left in the correct state. */
01077     portDISABLE_INTERRUPTS();
01078     xSchedulerRunning = pdFALSE;
01079     vPortEndScheduler();
01080 }
01081 /*----------------------------------------------------------*/
01082 #endif
01083 
01084 void vTaskSuspendAll( void )
01085 {
01086     /* A critical section is not required as the variable is of type
01087     portBASE_TYPE. */
01088     ++uxSchedulerSuspended;
01089 }
01090 /*----------------------------------------------------------*/
01091 
01092 signed portBASE_TYPE xTaskResumeAll( void )
01093 {
01094 register tskTCB *pxTCB;
01095 signed portBASE_TYPE xAlreadyYielded = pdFALSE;
01096 
01097     /* It is possible that an ISR caused a task to be removed from an event
01098     list while the scheduler was suspended.  If this was the case then the
01099     removed task will have been added to the xPendingReadyList.  Once the
01100     scheduler has been resumed it is safe to move all the pending ready
01101     tasks from this list into their appropriate ready list. */
01102     portENTER_CRITICAL();
01103     {
01104         --uxSchedulerSuspended;
01105 
01106         if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
01107         {
01108             if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0 )
01109             {
01110                 portBASE_TYPE xYieldRequired = pdFALSE;
01111 
01112                 /* Move any readied tasks from the pending list into the
01113                 appropriate ready list. */
01114                 while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY(  ( ( xList * ) &xPendingReadyList ) ) ) != NULL )
01115                 {
01116                     vListRemove( &( pxTCB->xEventListItem ) );
01117                     vListRemove( &( pxTCB->xGenericListItem ) );
01118                     prvAddTaskToReadyQueue( pxTCB );
01119 
01120                     /* If we have moved a task that has a priority higher than
01121                     the current task then we should yield. */
01122                     if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
01123                     {
01124                         xYieldRequired = pdTRUE;
01125                     }
01126                 }
01127 
01128                 /* If any ticks occurred while the scheduler was suspended then
01129                 they should be processed now.  This ensures the tick count does not
01130                 slip, and that any delayed tasks are resumed at the correct time. */
01131                 if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
01132                 {
01133                     while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
01134                     {
01135                         vTaskIncrementTick();
01136                         --uxMissedTicks;
01137                     }
01138 
01139                     /* As we have processed some ticks it is appropriate to yield
01140                     to ensure the highest priority task that is ready to run is
01141                     the task actually running. */
01142                     #if configUSE_PREEMPTION == 1
01143                     {
01144                         xYieldRequired = pdTRUE;
01145                     }
01146                     #endif
01147                 }
01148 
01149                 if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )
01150                 {
01151                     xAlreadyYielded = pdTRUE;
01152                     xMissedYield = pdFALSE;
01153                     portYIELD_WITHIN_API();
01154                 }
01155             }
01156         }
01157     }
01158     portEXIT_CRITICAL();
01159 
01160     return xAlreadyYielded;
01161 }
01162 
01163 
01164 
01165 
01166 
01167 
01168 /*-----------------------------------------------------------
01169  * PUBLIC TASK UTILITIES documented in task.h
01170  *----------------------------------------------------------*/
01171 
01172 
01173 
01174 portTickType xTaskGetTickCount( void )
01175 {
01176 portTickType xTicks;
01177 
01178     /* Critical section required if running on a 16 bit processor. */
01179     portENTER_CRITICAL();
01180     {
01181         xTicks = xTickCount;
01182     }
01183     portEXIT_CRITICAL();
01184 
01185     return xTicks;
01186 }
01187 /*-----------------------------------------------------------*/
01188 
01189 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
01190 {
01191     /* A critical section is not required because the variables are of type
01192     portBASE_TYPE. */
01193     return uxCurrentNumberOfTasks;
01194 }
01195 /*-----------------------------------------------------------*/
01196 
01197 #if ( configUSE_TRACE_FACILITY == 1 )
01198 
01199     void vTaskList( signed char *pcWriteBuffer )
01200     {
01201     unsigned portBASE_TYPE uxQueue;
01202 
01203         /* This is a VERY costly function that should be used for debug only.
01204         It leaves interrupts disabled for a LONG time. */
01205 
01206         vTaskSuspendAll();
01207         {
01208             /* Run through all the lists that could potentially contain a TCB and
01209             report the task name, state and stack high water mark. */
01210 
01211             pcWriteBuffer[ 0 ] = ( signed char ) 0x00;
01212             strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
01213 
01214             uxQueue = uxTopUsedPriority + 1;
01215 
01216             do
01217             {
01218                 uxQueue--;
01219 
01220                 if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )
01221                 {
01222                     prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
01223                 }
01224             }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
01225 
01226             if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )
01227             {
01228                 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
01229             }
01230 
01231             if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )
01232             {
01233                 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
01234             }
01235 
01236             #if( INCLUDE_vTaskDelete == 1 )
01237             {
01238                 if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )
01239                 {
01240                     prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );
01241                 }
01242             }
01243             #endif
01244 
01245             #if ( INCLUDE_vTaskSuspend == 1 )
01246             {
01247                 if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
01248                 {
01249                     prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );
01250                 }
01251             }
01252             #endif
01253         }
01254         xTaskResumeAll();
01255     }
01256 
01257 #endif
01258 /*----------------------------------------------------------*/
01259 
01260 #if ( configGENERATE_RUN_TIME_STATS == 1 )
01261 
01262     void vTaskGetRunTimeStats( signed char *pcWriteBuffer )
01263     {
01264     unsigned portBASE_TYPE uxQueue;
01265     unsigned long ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
01266 
01267         /* This is a VERY costly function that should be used for debug only.
01268         It leaves interrupts disabled for a LONG time. */
01269 
01270         vTaskSuspendAll();
01271         {
01272             /* Run through all the lists that could potentially contain a TCB,
01273             generating a table of run timer percentages in the provided
01274             buffer. */
01275 
01276             pcWriteBuffer[ 0 ] = ( signed char ) 0x00;
01277             strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
01278 
01279             uxQueue = uxTopUsedPriority + 1;
01280 
01281             do
01282             {
01283                 uxQueue--;
01284 
01285                 if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )
01286                 {
01287                     prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );
01288                 }
01289             }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
01290 
01291             if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )
01292             {
01293                 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );
01294             }
01295 
01296             if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )
01297             {
01298                 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );
01299             }
01300 
01301             #if ( INCLUDE_vTaskDelete == 1 )
01302             {
01303                 if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )
01304                 {
01305                     prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, ulTotalRunTime );
01306                 }
01307             }
01308             #endif
01309 
01310             #if ( INCLUDE_vTaskSuspend == 1 )
01311             {
01312                 if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
01313                 {
01314                     prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, ulTotalRunTime );
01315                 }
01316             }
01317             #endif
01318         }
01319         xTaskResumeAll();
01320     }
01321 
01322 #endif
01323 /*----------------------------------------------------------*/
01324 
01325 #if ( configUSE_TRACE_FACILITY == 1 )
01326 
01327     void vTaskStartTrace( signed char * pcBuffer, unsigned long ulBufferSize )
01328     {
01329         portENTER_CRITICAL();
01330         {
01331             pcTraceBuffer = ( signed char * )pcBuffer;
01332             pcTraceBufferStart = pcBuffer;
01333             pcTraceBufferEnd = pcBuffer + ( ulBufferSize - tskSIZE_OF_EACH_TRACE_LINE );
01334             xTracing = pdTRUE;
01335         }
01336         portEXIT_CRITICAL();
01337     }
01338 
01339 #endif
01340 /*----------------------------------------------------------*/
01341 
01342 #if ( configUSE_TRACE_FACILITY == 1 )
01343 
01344     unsigned long ulTaskEndTrace( void )
01345     {
01346     unsigned long ulBufferLength;
01347 
01348         portENTER_CRITICAL();
01349             xTracing = pdFALSE;
01350         portEXIT_CRITICAL();
01351 
01352         ulBufferLength = ( unsigned long ) ( pcTraceBuffer - pcTraceBufferStart );
01353 
01354         return ulBufferLength;
01355     }
01356 
01357 #endif
01358 
01359 
01360 
01361 /*-----------------------------------------------------------
01362  * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
01363  * documented in task.h
01364  *----------------------------------------------------------*/
01365 
01366 
01367 void vTaskIncrementTick( void )
01368 {
01369     /* Called by the portable layer each time a tick interrupt occurs.
01370     Increments the tick then checks to see if the new tick value will cause any
01371     tasks to be unblocked. */
01372     if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
01373     {
01374         ++xTickCount;
01375         if( xTickCount == ( portTickType ) 0 )
01376         {
01377             xList *pxTemp;
01378 
01379             /* Tick count has overflowed so we need to swap the delay lists.
01380             If there are any items in pxDelayedTaskList here then there is
01381             an error! */
01382             pxTemp = pxDelayedTaskList;
01383             pxDelayedTaskList = pxOverflowDelayedTaskList;
01384             pxOverflowDelayedTaskList = pxTemp;
01385             xNumOfOverflows++;
01386         }
01387 
01388         /* See if this tick has made a timeout expire. */
01389         prvCheckDelayedTasks();
01390     }
01391     else
01392     {
01393         ++uxMissedTicks;
01394 
01395         /* The tick hook gets called at regular intervals, even if the
01396         scheduler is locked. */
01397         #if ( configUSE_TICK_HOOK == 1 )
01398         {
01399 //            extern void vApplicationTickHook( void );
01400 
01401             vApplicationTickHook();
01402         }
01403         #endif
01404     }
01405 
01406     #if ( configUSE_TICK_HOOK == 1 )
01407     {
01408         extern void vApplicationTickHook( void );
01409 
01410         /* Guard against the tick hook being called when the missed tick
01411         count is being unwound (when the scheduler is being unlocked. */
01412         if( uxMissedTicks == 0 )
01413         {
01414             vApplicationTickHook();
01415         }
01416     }
01417     #endif
01418 
01419     traceTASK_INCREMENT_TICK( xTickCount );
01420 }
01421 /*-----------------------------------------------------------*/
01422 
01423 #if ( ( INCLUDE_vTaskCleanUpResources == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
01424 
01425     void vTaskCleanUpResources( void )
01426     {
01427     unsigned short usQueue;
01428     volatile tskTCB *pxTCB;
01429 
01430         usQueue = ( unsigned short ) uxTopUsedPriority + ( unsigned short ) 1;
01431 
01432         /* Remove any TCB's from the ready queues. */
01433         do
01434         {
01435             usQueue--;
01436 
01437             while( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ usQueue ] ) ) )
01438             {
01439                 listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &( pxReadyTasksLists[ usQueue ] ) );
01440                 vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
01441 
01442                 prvDeleteTCB( ( tskTCB * ) pxTCB );
01443             }
01444         }while( usQueue > ( unsigned short ) tskIDLE_PRIORITY );
01445 
01446         /* Remove any TCB's from the delayed queue. */
01447         while( !listLIST_IS_EMPTY( &xDelayedTaskList1 ) )
01448         {
01449             listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList1 );
01450             vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
01451 
01452             prvDeleteTCB( ( tskTCB * ) pxTCB );
01453         }
01454 
01455         /* Remove any TCB's from the overflow delayed queue. */
01456         while( !listLIST_IS_EMPTY( &xDelayedTaskList2 ) )
01457         {
01458             listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList2 );
01459             vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
01460 
01461             prvDeleteTCB( ( tskTCB * ) pxTCB );
01462         }
01463 
01464         while( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
01465         {
01466             listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xSuspendedTaskList );
01467             vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
01468 
01469             prvDeleteTCB( ( tskTCB * ) pxTCB );
01470         }
01471     }
01472 
01473 #endif
01474 /*-----------------------------------------------------------*/
01475 
01476 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
01477 
01478     void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxTagValue )
01479     {
01480     tskTCB *xTCB;
01481 
01482         /* If xTask is NULL then we are setting our own task hook. */
01483         if( xTask == NULL )
01484         {
01485             xTCB = ( tskTCB * ) pxCurrentTCB;
01486         }
01487         else
01488         {
01489             xTCB = ( tskTCB * ) xTask;
01490         }
01491 
01492         /* Save the hook function in the TCB.  A critical section is required as
01493         the value can be accessed from an interrupt. */
01494         portENTER_CRITICAL();
01495             xTCB->pxTaskTag = pxTagValue;
01496         portEXIT_CRITICAL();
01497     }
01498 
01499 #endif
01500 /*-----------------------------------------------------------*/
01501 
01502 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
01503 
01504     pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask )
01505     {
01506     tskTCB *xTCB;
01507     pdTASK_HOOK_CODE xReturn;
01508 
01509         /* If xTask is NULL then we are setting our own task hook. */
01510         if( xTask == NULL )
01511         {
01512             xTCB = ( tskTCB * ) pxCurrentTCB;
01513         }
01514         else
01515         {
01516             xTCB = ( tskTCB * ) xTask;
01517         }
01518 
01519         /* Save the hook function in the TCB.  A critical section is required as
01520         the value can be accessed from an interrupt. */
01521         portENTER_CRITICAL();
01522             xReturn = xTCB->pxTaskTag;
01523         portEXIT_CRITICAL();
01524 
01525         return xReturn;
01526     }
01527 
01528 #endif
01529 /*-----------------------------------------------------------*/
01530 
01531 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
01532 
01533     portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter )
01534     {
01535     tskTCB *xTCB;
01536     portBASE_TYPE xReturn;
01537 
01538         /* If xTask is NULL then we are calling our own task hook. */
01539         if( xTask == NULL )
01540         {
01541             xTCB = ( tskTCB * ) pxCurrentTCB;
01542         }
01543         else
01544         {
01545             xTCB = ( tskTCB * ) xTask;
01546         }
01547 
01548         if( xTCB->pxTaskTag != NULL )
01549         {
01550             xReturn = xTCB->pxTaskTag( pvParameter );
01551         }
01552         else
01553         {
01554             xReturn = pdFAIL;
01555         }
01556 
01557         return xReturn;
01558     }
01559 
01560 #endif
01561 /*-----------------------------------------------------------*/
01562 
01563 void vTaskSwitchContext( void )
01564 {
01565     if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )
01566     {
01567         /* The scheduler is currently suspended - do not allow a context
01568         switch. */
01569         xMissedYield = pdTRUE;
01570         return;
01571     }
01572 
01573     traceTASK_SWITCHED_OUT();
01574 
01575     #if ( configGENERATE_RUN_TIME_STATS == 1 )
01576     {
01577         unsigned long ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();
01578 
01579             /* Add the amount of time the task has been running to the accumulated
01580             time so far.  The time the task started running was stored in
01581             ulTaskSwitchedInTime.  Note that there is no overflow protection here
01582             so count values are only valid until the timer overflows.  Generally
01583             this will be about 1 hour assuming a 1uS timer increment. */
01584             pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );
01585             ulTaskSwitchedInTime = ulTempCounter;
01586     }
01587     #endif
01588 
01589     taskFIRST_CHECK_FOR_STACK_OVERFLOW();
01590     taskSECOND_CHECK_FOR_STACK_OVERFLOW();
01591 
01592     /* Find the highest priority queue that contains ready tasks. */
01593     while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )
01594     {
01595         --uxTopReadyPriority;
01596     }
01597 
01598     /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the
01599     same priority get an equal share of the processor time. */
01600     listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );
01601     #if 0
01602     //#define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList )
01603     {
01604         xList * const pxConstList = &( pxReadyTasksLists[ uxTopReadyPriority ] );
01605         /* Increment the index to the next item and return the item, ensuring */
01606         /* we don't return the marker used at the end of the list.  */
01607         ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;
01608         if( ( pxConstList )->pxIndex == ( xListItem * ) &( ( pxConstList )->xListEnd ) )
01609         {
01610             ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;
01611         }
01612         pxCurrentTCB = (tskTCB *) (( pxConstList )->pxIndex->pvOwner);      // Error then added (tskTCB *)()
01613     }
01614     #endif
01615 
01616     traceTASK_SWITCHED_IN();
01617     vWriteTraceToBuffer();
01618 }
01619 /*-----------------------------------------------------------*/
01620 
01621 void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait )
01622 {
01623 portTickType xTimeToWake;
01624 
01625     /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
01626     SCHEDULER SUSPENDED. */
01627 
01628     /* Place the event list item of the TCB in the appropriate event list.
01629     This is placed in the list in priority order so the highest priority task
01630     is the first to be woken by the event. */
01631     vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
01632 
01633     /* We must remove ourselves from the ready list before adding ourselves
01634     to the blocked list as the same list item is used for both lists.  We have
01635     exclusive access to the ready lists as the scheduler is locked. */
01636     vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
01637 
01638 
01639     #if ( INCLUDE_vTaskSuspend == 1 )
01640     {
01641         if( xTicksToWait == portMAX_DELAY )
01642         {
01643             /* Add ourselves to the suspended task list instead of a delayed task
01644             list to ensure we are not woken by a timing event.  We will block
01645             indefinitely. */
01646             vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
01647         }
01648         else
01649         {
01650             /* Calculate the time at which the task should be woken if the event does
01651             not occur.  This may overflow but this doesn't matter. */
01652             xTimeToWake = xTickCount + xTicksToWait;
01653 
01654             listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
01655 
01656             if( xTimeToWake < xTickCount )
01657             {
01658                 /* Wake time has overflowed.  Place this item in the overflow list. */
01659                 vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
01660             }
01661             else
01662             {
01663                 /* The wake time has not overflowed, so we can use the current block list. */
01664                 vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
01665             }
01666         }
01667     }
01668     #else
01669     {
01670             /* Calculate the time at which the task should be woken if the event does
01671             not occur.  This may overflow but this doesn't matter. */
01672             xTimeToWake = xTickCount + xTicksToWait;
01673 
01674             listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
01675 
01676             if( xTimeToWake < xTickCount )
01677             {
01678                 /* Wake time has overflowed.  Place this item in the overflow list. */
01679                 vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
01680             }
01681             else
01682             {
01683                 /* The wake time has not overflowed, so we can use the current block list. */
01684                 vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
01685             }
01686     }
01687     #endif
01688 }
01689 /*-----------------------------------------------------------*/
01690 
01691 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )
01692 {
01693 tskTCB *pxUnblockedTCB;
01694 portBASE_TYPE xReturn;
01695 
01696     /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
01697     SCHEDULER SUSPENDED.  It can also be called from within an ISR. */
01698 
01699     /* The event list is sorted in priority order, so we can remove the
01700     first in the list, remove the TCB from the delayed list, and add
01701     it to the ready list.
01702 
01703     If an event is for a queue that is locked then this function will never
01704     get called - the lock count on the queue will get modified instead.  This
01705     means we can always expect exclusive access to the event list here. */
01706     pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
01707     vListRemove( &( pxUnblockedTCB->xEventListItem ) );
01708 
01709     if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
01710     {
01711         vListRemove( &( pxUnblockedTCB->xGenericListItem ) );
01712         prvAddTaskToReadyQueue( pxUnblockedTCB );
01713     }
01714     else
01715     {
01716         /* We cannot access the delayed or ready lists, so will hold this
01717         task pending until the scheduler is resumed. */
01718         vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
01719     }
01720 
01721     if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )
01722     {
01723         /* Return true if the task removed from the event list has
01724         a higher priority than the calling task.  This allows
01725         the calling task to know if it should force a context
01726         switch now. */
01727         xReturn = pdTRUE;
01728     }
01729     else
01730     {
01731         xReturn = pdFALSE;
01732     }
01733 
01734     return xReturn;
01735 }
01736 /*-----------------------------------------------------------*/
01737 
01738 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )
01739 {
01740     pxTimeOut->xOverflowCount = xNumOfOverflows;
01741     pxTimeOut->xTimeOnEntering = xTickCount;
01742 }
01743 /*-----------------------------------------------------------*/
01744 
01745 portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait )
01746 {
01747 portBASE_TYPE xReturn;
01748 
01749     portENTER_CRITICAL();
01750     {
01751         #if ( INCLUDE_vTaskSuspend == 1 )
01752             /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is
01753             the maximum block time then the task should block indefinitely, and
01754             therefore never time out. */
01755             if( *pxTicksToWait == portMAX_DELAY )
01756             {
01757                 xReturn = pdFALSE;
01758             }
01759             else /* We are not blocking indefinitely, perform the checks below. */
01760         #endif
01761 
01762         if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( ( portTickType ) xTickCount >= ( portTickType ) pxTimeOut->xTimeOnEntering ) )
01763         {
01764             /* The tick count is greater than the time at which vTaskSetTimeout()
01765             was called, but has also overflowed since vTaskSetTimeOut() was called.
01766             It must have wrapped all the way around and gone past us again. This
01767             passed since vTaskSetTimeout() was called. */
01768             xReturn = pdTRUE;
01769         }
01770         else if( ( ( portTickType ) ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) ) < ( portTickType ) *pxTicksToWait )
01771         {
01772             /* Not a genuine timeout. Adjust parameters for time remaining. */
01773             *pxTicksToWait -= ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering );
01774             vTaskSetTimeOutState( pxTimeOut );
01775             xReturn = pdFALSE;
01776         }
01777         else
01778         {
01779             xReturn = pdTRUE;
01780         }
01781     }
01782     portEXIT_CRITICAL();
01783 
01784     return xReturn;
01785 }
01786 /*-----------------------------------------------------------*/
01787 
01788 void vTaskMissedYield( void )
01789 {
01790     xMissedYield = pdTRUE;
01791 }
01792 
01793 /*
01794  * -----------------------------------------------------------
01795  * The Idle task.
01796  * ----------------------------------------------------------
01797  *
01798  * The portTASK_FUNCTION() macro is used to allow port/compiler specific
01799  * language extensions.  The equivalent prototype for this function is:
01800  *
01801  * void prvIdleTask( void *pvParameters );
01802  *
01803  */
01804 static portTASK_FUNCTION( prvIdleTask, pvParameters )
01805 {
01806     /* Stop warnings. */
01807     ( void ) pvParameters;
01808 
01809     for( ;; )
01810     {
01811         /* See if any tasks have been deleted. */
01812         prvCheckTasksWaitingTermination();
01813 
01814         #if ( configUSE_PREEMPTION == 0 )
01815         {
01816             /* If we are not using preemption we keep forcing a task switch to
01817             see if any other task has become available.  If we are using
01818             preemption we don't need to do this as any task becoming available
01819             will automatically get the processor anyway. */
01820             taskYIELD();
01821         }
01822         #endif
01823 
01824         #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
01825         {
01826             /* When using preemption tasks of equal priority will be
01827             timesliced.  If a task that is sharing the idle priority is ready
01828             to run then the idle task should yield before the end of the
01829             timeslice.
01830 
01831             A critical region is not required here as we are just reading from
01832             the list, and an occasional incorrect value will not matter.  If
01833             the ready list at the idle priority contains more than one task
01834             then a task other than the idle task is ready to execute. */
01835             if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( unsigned portBASE_TYPE ) 1 )
01836             {
01837                 taskYIELD();
01838             }
01839         }
01840         #endif
01841 
01842         #if ( configUSE_IDLE_HOOK == 1 )
01843         {
01844             extern void vApplicationIdleHook( void );
01845 
01846             /* Call the user defined function from within the idle task.  This
01847             allows the application designer to add background functionality
01848             without the overhead of a separate task.
01849             NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
01850             CALL A FUNCTION THAT MIGHT BLOCK. */
01851             vApplicationIdleHook();
01852         }
01853         #endif
01854     }
01855 } /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */
01856 
01857 
01858 
01859 
01860 
01861 
01862 
01863 /*-----------------------------------------------------------
01864  * File private functions documented at the top of the file.
01865  *----------------------------------------------------------*/
01866 
01867 
01868 
01869 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )
01870 {
01871     /* Store the function name in the TCB. */
01872     #if configMAX_TASK_NAME_LEN > 1
01873     {
01874         /* Don't bring strncpy into the build unnecessarily. */
01875         strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );
01876     }
01877     #endif
01878     pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = '\0';
01879 
01880     /* This is used as an array index so must ensure it's not too large.  First
01881     remove the privilege bit if one is present. */
01882     if( uxPriority >= configMAX_PRIORITIES )
01883     {
01884         uxPriority = configMAX_PRIORITIES - 1;
01885     }
01886 
01887     pxTCB->uxPriority = uxPriority;
01888     #if ( configUSE_MUTEXES == 1 )
01889     {
01890         pxTCB->uxBasePriority = uxPriority;
01891     }
01892     #endif
01893 
01894     vListInitialiseItem( &( pxTCB->xGenericListItem ) );
01895     vListInitialiseItem( &( pxTCB->xEventListItem ) );
01896 
01897     /* Set the pxTCB as a link back from the xListItem.  This is so we can get
01898     back to    the containing TCB from a generic item in a list. */
01899     listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );
01900 
01901     /* Event lists are always in priority order. */
01902     listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
01903     listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );
01904 
01905     #if ( portCRITICAL_NESTING_IN_TCB == 1 )
01906     {
01907         pxTCB->uxCriticalNesting = ( unsigned portBASE_TYPE ) 0;
01908     }
01909     #endif
01910 
01911     #if ( configUSE_APPLICATION_TASK_TAG == 1 )
01912     {
01913         pxTCB->pxTaskTag = NULL;
01914     }
01915     #endif
01916 
01917     #if ( configGENERATE_RUN_TIME_STATS == 1 )
01918     {
01919         pxTCB->ulRunTimeCounter = 0UL;
01920     }
01921     #endif
01922 
01923     #if ( portUSING_MPU_WRAPPERS == 1 )
01924     {
01925         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );
01926     }
01927     #else
01928     {
01929         ( void ) xRegions;
01930         ( void ) usStackDepth;
01931     }
01932     #endif
01933 }
01934 /*-----------------------------------------------------------*/
01935 
01936 #if ( portUSING_MPU_WRAPPERS == 1 )
01937 
01938     void vTaskAllocateMPURegions( xTaskHandle xTaskToModify, const xMemoryRegion * const xRegions )
01939     {
01940     tskTCB *pxTCB;
01941     
01942         if( xTaskToModify == pxCurrentTCB )
01943         {
01944             xTaskToModify = NULL;
01945         }
01946 
01947         /* If null is passed in here then we are deleting ourselves. */
01948         pxTCB = prvGetTCBFromHandle( xTaskToModify );
01949 
01950         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
01951     }
01952     /*-----------------------------------------------------------*/
01953 #endif
01954 
01955 static void prvInitialiseTaskLists( void )
01956 {
01957 unsigned portBASE_TYPE uxPriority;
01958 
01959     for( uxPriority = 0; uxPriority < configMAX_PRIORITIES; uxPriority++ )
01960     {
01961         vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );
01962     }
01963 
01964     vListInitialise( ( xList * ) &xDelayedTaskList1 );
01965     vListInitialise( ( xList * ) &xDelayedTaskList2 );
01966     vListInitialise( ( xList * ) &xPendingReadyList );
01967 
01968     #if ( INCLUDE_vTaskDelete == 1 )
01969     {
01970         vListInitialise( ( xList * ) &xTasksWaitingTermination );
01971     }
01972     #endif
01973 
01974     #if ( INCLUDE_vTaskSuspend == 1 )
01975     {
01976         vListInitialise( ( xList * ) &xSuspendedTaskList );
01977     }
01978     #endif
01979 
01980     /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
01981     using list2. */
01982     pxDelayedTaskList = &xDelayedTaskList1;
01983     pxOverflowDelayedTaskList = &xDelayedTaskList2;
01984 }
01985 /*-----------------------------------------------------------*/
01986 
01987 static void prvCheckTasksWaitingTermination( void )
01988 {
01989     #if ( INCLUDE_vTaskDelete == 1 )
01990     {
01991         portBASE_TYPE xListIsEmpty;
01992 
01993         /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
01994         too often in the idle task. */
01995         if( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0 )
01996         {
01997             vTaskSuspendAll();
01998                 xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
01999             xTaskResumeAll();
02000 
02001             if( !xListIsEmpty )
02002             {
02003                 tskTCB *pxTCB;
02004 
02005                 portENTER_CRITICAL();
02006                 {
02007                     pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );
02008                     vListRemove( &( pxTCB->xGenericListItem ) );
02009                     --uxCurrentNumberOfTasks;
02010                     --uxTasksDeleted;
02011                 }
02012                 portEXIT_CRITICAL();
02013 
02014                 prvDeleteTCB( pxTCB );
02015             }
02016         }
02017     }
02018     #endif
02019 }
02020 /*-----------------------------------------------------------*/
02021 
02022 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer )
02023 {
02024 tskTCB *pxNewTCB;
02025 
02026     /* Allocate space for the TCB.  Where the memory comes from depends on
02027     the implementation of the port malloc function. */
02028     pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );
02029 
02030     if( pxNewTCB != NULL )
02031     {
02032         /* Allocate space for the stack used by the task being created.
02033         The base of the stack memory stored in the TCB so the task can
02034         be deleted later if required. */
02035         pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );
02036 
02037         if( pxNewTCB->pxStack == NULL )
02038         {
02039             /* Could not allocate the stack.  Delete the allocated TCB. */
02040             vPortFree( pxNewTCB );
02041             pxNewTCB = NULL;
02042         }
02043         else
02044         {
02045             /* Just to help debugging. */
02046             memset( pxNewTCB->pxStack, tskSTACK_FILL_BYTE, usStackDepth * sizeof( portSTACK_TYPE ) );
02047         }
02048     }
02049 
02050     return pxNewTCB;
02051 }
02052 /*-----------------------------------------------------------*/
02053 
02054 #if ( configUSE_TRACE_FACILITY == 1 )
02055 
02056     static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus )
02057     {
02058     volatile tskTCB *pxNextTCB, *pxFirstTCB;
02059     unsigned short usStackRemaining;
02060 
02061         /* Write the details of all the TCB's in pxList into the buffer. */
02062         listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
02063         do
02064         {
02065             listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
02066             #if ( portSTACK_GROWTH > 0 )
02067             {
02068                 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxEndOfStack );
02069             }
02070             #else
02071             {
02072                 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );
02073             }
02074             #endif            
02075             
02076             sprintf( pcStatusString, ( char * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
02077             strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatusString );
02078 
02079         } while( pxNextTCB != pxFirstTCB );
02080     }
02081 
02082 #endif
02083 /*-----------------------------------------------------------*/
02084 
02085 #if ( configGENERATE_RUN_TIME_STATS == 1 )
02086 
02087     static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime )
02088     {
02089     volatile tskTCB *pxNextTCB, *pxFirstTCB;
02090     unsigned long ulStatsAsPercentage;
02091 
02092         /* Write the run time stats of all the TCB's in pxList into the buffer. */
02093         listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
02094         do
02095         {
02096             /* Get next TCB in from the list. */
02097             listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
02098 
02099             /* Divide by zero check. */
02100             if( ulTotalRunTime > 0UL )
02101             {
02102                 /* Has the task run at all? */
02103                 if( pxNextTCB->ulRunTimeCounter == 0 )
02104                 {
02105                     /* The task has used no CPU time at all. */
02106                     sprintf( pcStatsString, ( char * ) "%s\t\t0\t\t0%%\r\n", pxNextTCB->pcTaskName );
02107                 }
02108                 else
02109                 {
02110                     /* What percentage of the total run time as the task used?
02111                     This will always be rounded down to the nearest integer. */
02112                     ulStatsAsPercentage = ( 100UL * pxNextTCB->ulRunTimeCounter ) / ulTotalRunTime;
02113 
02114                     if( ulStatsAsPercentage > 0UL )
02115                     {
02116                         sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t%u%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );
02117                     }
02118                     else
02119                     {
02120                         /* If the percentage is zero here then the task has
02121                         consumed less than 1% of the total run time. */
02122                         sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t<1%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter );
02123                     }
02124                 }
02125 
02126                 strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatsString );
02127             }
02128 
02129         } while( pxNextTCB != pxFirstTCB );
02130     }
02131 
02132 #endif
02133 /*-----------------------------------------------------------*/
02134 
02135 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
02136 
02137     static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte )
02138     {
02139     register unsigned short usCount = 0;
02140 
02141         while( *pucStackByte == tskSTACK_FILL_BYTE )
02142         {
02143             pucStackByte -= portSTACK_GROWTH;
02144             usCount++;
02145         }
02146 
02147         usCount /= sizeof( portSTACK_TYPE );
02148 
02149         return usCount;
02150     }
02151 
02152 #endif
02153 /*-----------------------------------------------------------*/
02154 
02155 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
02156 
02157     unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask )
02158     {
02159     tskTCB *pxTCB;
02160     unsigned char *pcEndOfStack;
02161     unsigned portBASE_TYPE uxReturn;
02162 
02163         pxTCB = prvGetTCBFromHandle( xTask );
02164 
02165         #if portSTACK_GROWTH < 0
02166         {
02167             pcEndOfStack = ( unsigned char * ) pxTCB->pxStack;
02168         }
02169         #else
02170         {
02171             pcEndOfStack = ( unsigned char * ) pxTCB->pxEndOfStack;
02172         }
02173         #endif
02174 
02175         uxReturn = ( unsigned portBASE_TYPE ) usTaskCheckFreeStackSpace( pcEndOfStack );
02176 
02177         return uxReturn;
02178     }
02179 
02180 #endif
02181 /*-----------------------------------------------------------*/
02182 
02183 #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
02184 
02185     static void prvDeleteTCB( tskTCB *pxTCB )
02186     {
02187         /* Free up the memory allocated by the scheduler for the task.  It is up to
02188         the task to free any memory allocated at the application level. */
02189         vPortFreeAligned( pxTCB->pxStack );
02190         vPortFree( pxTCB );
02191     }
02192 
02193 #endif
02194 
02195 
02196 /*-----------------------------------------------------------*/
02197 
02198 #if ( INCLUDE_xTaskGetCurrentTaskHandle == 1 )
02199 
02200     xTaskHandle xTaskGetCurrentTaskHandle( void )
02201     {
02202     xTaskHandle xReturn;
02203 
02204         /* A critical section is not required as this is not called from
02205         an interrupt and the current TCB will always be the same for any
02206         individual execution thread. */
02207         xReturn = pxCurrentTCB;
02208 
02209         return xReturn;
02210     }
02211 
02212 #endif
02213 
02214 /*-----------------------------------------------------------*/
02215 
02216 #if ( INCLUDE_xTaskGetSchedulerState == 1 )
02217 
02218     portBASE_TYPE xTaskGetSchedulerState( void )
02219     {
02220     portBASE_TYPE xReturn;
02221 
02222         if( xSchedulerRunning == pdFALSE )
02223         {
02224             xReturn = taskSCHEDULER_NOT_STARTED;
02225         }
02226         else
02227         {
02228             if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
02229             {
02230                 xReturn = taskSCHEDULER_RUNNING;
02231             }
02232             else
02233             {
02234                 xReturn = taskSCHEDULER_SUSPENDED;
02235             }
02236         }
02237 
02238         return xReturn;
02239     }
02240 
02241 #endif
02242 /*-----------------------------------------------------------*/
02243 
02244 #if ( configUSE_MUTEXES == 1 )
02245 
02246     void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder )
02247     {
02248     tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
02249 
02250         if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )
02251         {
02252             /* Adjust the mutex holder state to account for its new priority. */
02253             listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority );
02254 
02255             /* If the task being modified is in the ready state it will need to
02256             be moved in to a new list. */
02257             if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) )
02258             {
02259                 vListRemove( &( pxTCB->xGenericListItem ) );
02260 
02261                 /* Inherit the priority before being moved into the new list. */
02262                 pxTCB->uxPriority = pxCurrentTCB->uxPriority;
02263                 prvAddTaskToReadyQueue( pxTCB );
02264             }
02265             else
02266             {
02267                 /* Just inherit the priority. */
02268                 pxTCB->uxPriority = pxCurrentTCB->uxPriority;
02269             }
02270         }
02271     }
02272 
02273 #endif
02274 /*-----------------------------------------------------------*/
02275 
02276 #if ( configUSE_MUTEXES == 1 )
02277 
02278     void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder )
02279     {
02280     tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
02281 
02282         if( pxMutexHolder != NULL )
02283         {
02284             if( pxTCB->uxPriority != pxTCB->uxBasePriority )
02285             {
02286                 /* We must be the running task to be able to give the mutex back.
02287                 Remove ourselves from the ready list we currently appear in. */
02288                 vListRemove( &( pxTCB->xGenericListItem ) );
02289 
02290                 /* Disinherit the priority before adding ourselves into the new
02291                 ready list. */
02292                 pxTCB->uxPriority = pxTCB->uxBasePriority;
02293                 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority );
02294                 prvAddTaskToReadyQueue( pxTCB );
02295             }
02296         }
02297     }
02298 
02299 #endif
02300 /*-----------------------------------------------------------*/
02301 
02302 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
02303 
02304     void vTaskEnterCritical( void )
02305     {
02306         portDISABLE_INTERRUPTS();
02307 
02308         if( xSchedulerRunning != pdFALSE )
02309         {
02310             pxCurrentTCB->uxCriticalNesting++;
02311         }
02312     }
02313 
02314 #endif
02315 /*-----------------------------------------------------------*/
02316 
02317 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
02318 
02319 void vTaskExitCritical( void )
02320 {
02321     if( xSchedulerRunning != pdFALSE )
02322     {
02323         if( pxCurrentTCB->uxCriticalNesting > 0 )
02324         {
02325             pxCurrentTCB->uxCriticalNesting--;
02326 
02327             if( pxCurrentTCB->uxCriticalNesting == 0 )
02328             {
02329                 portENABLE_INTERRUPTS();
02330             }
02331         }
02332     }
02333 }
02334 
02335 #endif
02336 /*-----------------------------------------------------------*/
02337 void vApplicationTickHook( void ){
02338     static unsigned long ulTicksSinceLastDisplay = 0;
02339 
02340     /* Called from every tick interrupt as described in the comments at the top    of this file.
02341     Have enough ticks passed to make it    time to perform our health status check again? */
02342     ulTicksSinceLastDisplay++;
02343     if( ulTicksSinceLastDisplay >= mainCHECK_DELAY ){
02344         /* Reset the counter so these checks run again in mainCHECK_DELAY
02345         ticks time. */
02346         ulTicksSinceLastDisplay = 0;
02347     }
02348 }
02349 
02350 /*----------------------------------------------------------------------------------------------------------*/
02351 
02352 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
02353 {
02354     /* This function will get called if a task overflows its stack. */
02355 
02356     ( void ) pxTask;
02357     ( void ) pcTaskName;
02358     for( ;; );
02359 }
02360 
02361