]> git.sur5r.net Git - freertos/blobdiff - Source/tasks.c
Add in the pcTaskGetTaskName(), xTaskGetIdleTaskHandle() and xTimerGetTimerTaskHandle...
[freertos] / Source / tasks.c
index 9bc5d46530ee5df64bbb79b445d85726924cc561..16914978cb84b29ce511f37efcae967a26d7e8b9 100644 (file)
@@ -1,41 +1,52 @@
 /*\r
-    FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
-\r
-    This file is part of the FreeRTOS distribution.\r
+    FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
+       \r
 \r
-    FreeRTOS is free software; you can redistribute it and/or modify it    under\r
-    the terms of the GNU General Public License (version 2) as published by the\r
-    Free Software Foundation and modified by the FreeRTOS exception.\r
-    **NOTE** The exception to the GPL is included to allow you to distribute a\r
-    combined work that includes FreeRTOS without being obliged to provide the\r
-    source code for proprietary components outside of the FreeRTOS kernel.\r
-    Alternative commercial license and support terms are also available upon\r
-    request.  See the licensing section of http://www.FreeRTOS.org for full\r
-    license details.\r
+       FreeRTOS supports many tools and architectures. V7.0.0 is sponsored by:\r
+       Atollic AB - Atollic provides professional embedded systems development \r
+       tools for C/C++ development, code analysis and test automation.  \r
+       See http://www.atollic.com\r
+       \r
 \r
-    FreeRTOS is distributed in the hope that it will be useful,    but WITHOUT\r
-    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
-    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
-    more details.\r
+    ***************************************************************************\r
+     *                                                                       *\r
+     *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
+     *    Complete, revised, and edited pdf reference manuals are also       *\r
+     *    available.                                                         *\r
+     *                                                                       *\r
+     *    Purchasing FreeRTOS documentation will not only help you, by       *\r
+     *    ensuring you get running as quickly as possible and with an        *\r
+     *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
+     *    the FreeRTOS project to continue with its mission of providing     *\r
+     *    professional grade, cross platform, de facto standard solutions    *\r
+     *    for microcontrollers - completely free of charge!                  *\r
+     *                                                                       *\r
+     *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
+     *                                                                       *\r
+     *    Thank you for using FreeRTOS, and thank you for your support!      *\r
+     *                                                                       *\r
+    ***************************************************************************\r
 \r
-    You should have received a copy of the GNU General Public License along\r
-    with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
-    Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
 \r
+    This file is part of the FreeRTOS distribution.\r
 \r
-    ***************************************************************************\r
-    *                                                                         *\r
-    * The FreeRTOS eBook and reference manual are available to purchase for a *\r
-    * small fee. Help yourself get started quickly while also helping the     *\r
-    * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
-    *                                                                         *\r
-    ***************************************************************************\r
+    FreeRTOS is free software; you can redistribute it and/or modify it under\r
+    the terms of the GNU General Public License (version 2) as published by the\r
+    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
+    >>>NOTE<<< The modification to the GPL is included to allow you to\r
+    distribute a combined work that includes FreeRTOS without being obliged to\r
+    provide the source code for proprietary components outside of the FreeRTOS\r
+    kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
+    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
+    more details. You should have received a copy of the GNU General Public\r
+    License and the FreeRTOS license exception along with FreeRTOS; if not it\r
+    can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
+    by writing to Richard Barry, contact details for whom are available on the\r
+    FreeRTOS WEB site.\r
 \r
     1 tab == 4 spaces!\r
 \r
-    Please ensure to read the configuration and relevant port sections of the\r
-    online documentation.\r
-\r
     http://www.FreeRTOS.org - Documentation, latest information, license and\r
     contact details.\r
 \r
@@ -58,6 +69,7 @@ task.h is included from an application file. */
 \r
 #include "FreeRTOS.h"\r
 #include "task.h"\r
+#include "timers.h"\r
 #include "StackMacros.h"\r
 \r
 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
@@ -67,8 +79,6 @@ task.h is included from an application file. */
  */\r
 #define tskIDLE_STACK_SIZE     configMINIMAL_STACK_SIZE\r
 \r
-#define tskIDLE_PRIORITY                       ( ( unsigned portBASE_TYPE ) 0 )\r
-\r
 /*\r
  * Task control block.  A task control block (TCB) is allocated to each task,\r
  * and stores the context of the task.\r
@@ -147,6 +157,12 @@ PRIVILEGED_DATA static xList xPendingReadyList;                                                    /*< Tasks that have been r
 \r
 #endif\r
 \r
+#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
+       \r
+       PRIVILEGED_DATA static xTaskHandle xIdleTaskHandle = NULL;\r
+       \r
+#endif\r
+\r
 /* File private variables. --------------------------------*/\r
 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks  = ( unsigned portBASE_TYPE ) 0;\r
 PRIVILEGED_DATA static volatile portTickType xTickCount                                                = ( portTickType ) 0;\r
@@ -158,6 +174,7 @@ PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxMissedTicks                        = ( unsi
 PRIVILEGED_DATA static volatile portBASE_TYPE xMissedYield                                             = ( portBASE_TYPE ) pdFALSE;\r
 PRIVILEGED_DATA static volatile portBASE_TYPE xNumOfOverflows                                  = ( portBASE_TYPE ) 0;\r
 PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                             = ( unsigned portBASE_TYPE ) 0;\r
+PRIVILEGED_DATA static portTickType xNextTaskUnblockTime                                               = ( portTickType ) portMAX_DELAY;\r
 \r
 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
 \r
@@ -173,7 +190,7 @@ PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                          = ( unsigned po
  * The value used to fill the stack of a task when the task is created.  This\r
  * is used purely for checking the high water mark for tasks.\r
  */\r
-#define tskSTACK_FILL_BYTE     ( 0xa5 )\r
+#define tskSTACK_FILL_BYTE     ( 0xa5U )\r
 \r
 /*\r
  * Macros used by vListTask to indicate which state a task is in.\r
@@ -193,7 +210,7 @@ PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                          = ( unsigned po
        PRIVILEGED_DATA static signed char *pcTraceBufferStart;\r
        PRIVILEGED_DATA static signed char *pcTraceBufferEnd;\r
        PRIVILEGED_DATA static signed portBASE_TYPE xTracing = pdFALSE;\r
-       static unsigned portBASE_TYPE uxPreviousTask = 255;\r
+       static unsigned portBASE_TYPE uxPreviousTask = 255U;\r
        PRIVILEGED_DATA static char pcStatusString[ 50 ];\r
 \r
 #endif\r
@@ -217,10 +234,10 @@ PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                                = ( unsigned po
                                if( ( pcTraceBuffer + tskSIZE_OF_EACH_TRACE_LINE ) < pcTraceBufferEnd )                         \\r
                                {                                                                                                                                                                       \\r
                                        uxPreviousTask = pxCurrentTCB->uxTCBNumber;                                                                             \\r
-                                       *( unsigned long * ) pcTraceBuffer = ( unsigned long ) xTickCount;              \\r
-                                       pcTraceBuffer += sizeof( unsigned long );                                                                       \\r
-                                       *( unsigned long * ) pcTraceBuffer = ( unsigned long ) uxPreviousTask;  \\r
-                                       pcTraceBuffer += sizeof( unsigned long );                                                                       \\r
+                                       *( unsigned long * ) pcTraceBuffer = ( unsigned long ) xTickCount;                              \\r
+                                       pcTraceBuffer += sizeof( unsigned long );                                                                               \\r
+                                       *( unsigned long * ) pcTraceBuffer = ( unsigned long ) uxPreviousTask;                  \\r
+                                       pcTraceBuffer += sizeof( unsigned long );                                                                               \\r
                                }                                                                                                                                                                       \\r
                                else                                                                                                                                                            \\r
                                {                                                                                                                                                                       \\r
@@ -244,14 +261,12 @@ PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                                = ( unsigned po
  * executing task, then it will only be rescheduled after the currently\r
  * executing task has been rescheduled.\r
  */\r
-#define prvAddTaskToReadyQueue( pxTCB )                                                                                                                                                        \\r
-{                                                                                                                                                                                                                              \\r
-       if( pxTCB->uxPriority > uxTopReadyPriority )                                                                                                                            \\r
-       {                                                                                                                                                                                                                       \\r
-               uxTopReadyPriority = pxTCB->uxPriority;                                                                                                                                 \\r
-       }                                                                                                                                                                                                                       \\r
-       vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) );        \\r
-}\r
+#define prvAddTaskToReadyQueue( pxTCB )                                                                                                                                                                        \\r
+       if( ( pxTCB )->uxPriority > uxTopReadyPriority )                                                                                                                                        \\r
+       {                                                                                                                                                                                                                                       \\r
+               uxTopReadyPriority = ( pxTCB )->uxPriority;                                                                                                                                             \\r
+       }                                                                                                                                                                                                                                       \\r
+       vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )\r
 /*-----------------------------------------------------------*/\r
 \r
 /*\r
@@ -262,24 +277,56 @@ PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                                = ( unsigned po
  * once one tasks has been found whose timer has not expired we need not look\r
  * any further down the list.\r
  */\r
-#define prvCheckDelayedTasks()                                                                                                                                                                         \\r
-{                                                                                                                                                                                                                                      \\r
-register tskTCB *pxTCB;                                                                                                                                                                                                \\r
-                                                                                                                                                                                                                                       \\r
-       while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ) ) != NULL )                                              \\r
-       {                                                                                                                                                                                                                               \\r
-               if( xTickCount < listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) ) )                                                                      \\r
-               {                                                                                                                                                                                                                       \\r
-                       break;                                                                                                                                                                                                  \\r
-               }                                                                                                                                                                                                                       \\r
-               vListRemove( &( pxTCB->xGenericListItem ) );                                                                                                                            \\r
-               /* Is the task waiting on an event also? */                                                                                                                                     \\r
-               if( pxTCB->xEventListItem.pvContainer )                                                                                                                                         \\r
-               {                                                                                                                                                                                                                       \\r
-                       vListRemove( &( pxTCB->xEventListItem ) );                                                                                                                              \\r
-               }                                                                                                                                                                                                                       \\r
-               prvAddTaskToReadyQueue( pxTCB );                                                                                                                                                        \\r
-       }                                                                                                                                                                                                                               \\r
+#define prvCheckDelayedTasks()                                                                                                                 \\r
+{                                                                                                                                                                              \\r
+portTickType xItemValue;                                                                                                                               \\r
+                                                                                                                                                                               \\r
+       /* Is the tick count greater than or equal to the wake time of the first                        \\r
+       task referenced from the delayed tasks list? */                                                                         \\r
+       if( xTickCount >= xNextTaskUnblockTime )                                                                                        \\r
+       {                                                                                                                                                                       \\r
+               for( ;; )                                                                                                                                               \\r
+               {                                                                                                                                                               \\r
+                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )                                         \\r
+                       {                                                                                                                                                       \\r
+                               /* The delayed list is empty.  Set xNextTaskUnblockTime to the                  \\r
+                               maximum possible value so it is extremely unlikely that the                             \\r
+                               if( xTickCount >= xNextTaskUnblockTime ) test will pass next                    \\r
+                               time through. */                                                                                                                \\r
+                               xNextTaskUnblockTime = portMAX_DELAY;                                                                   \\r
+                               break;                                                                                                                                  \\r
+                       }                                                                                                                                                       \\r
+                       else                                                                                                                                            \\r
+                       {                                                                                                                                                       \\r
+                               /* The delayed list is not empty, get the value of the item at                  \\r
+                               the head of the delayed list.  This is the time at which the                    \\r
+                               task at the head of the delayed list should be removed from                             \\r
+                               the Blocked state. */                                                                                                   \\r
+                               pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );  \\r
+                               xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );   \\r
+                                                                                                                                                                               \\r
+                               if( xTickCount < xItemValue )                                                                                   \\r
+                               {                                                                                                                                               \\r
+                                       /* It is not time to unblock this item yet, but the item                        \\r
+                                       value is the time at which the task at the head of the                          \\r
+                                       blocked list should be removed from the Blocked state -                         \\r
+                                       so record the item value in xNextTaskUnblockTime. */                            \\r
+                                       xNextTaskUnblockTime = xItemValue;                                                                      \\r
+                                       break;                                                                                                                          \\r
+                               }                                                                                                                                               \\r
+                                                                                                                                                                               \\r
+                               /* It is time to remove the item from the Blocked state. */                             \\r
+                               vListRemove( &( pxTCB->xGenericListItem ) );                                                    \\r
+                                                                                                                                                                               \\r
+                               /* Is the task waiting on an event also? */                                                             \\r
+                               if( pxTCB->xEventListItem.pvContainer )                                                                 \\r
+                               {                                                                                                                                               \\r
+                                       vListRemove( &( pxTCB->xEventListItem ) );                                                      \\r
+                               }                                                                                                                                               \\r
+                               prvAddTaskToReadyQueue( pxTCB );                                                                                \\r
+                       }                                                                                                                                                       \\r
+               }                                                                                                                                                               \\r
+       }                                                                                                                                                                       \\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -289,9 +336,12 @@ register tskTCB *pxTCB;                                                                                                                                                                                            \
  * task should be used in place of the parameter.  This macro simply checks to\r
  * see if the parameter is NULL and returns a pointer to the appropriate TCB.\r
  */\r
-#define prvGetTCBFromHandle( pxHandle ) ( ( pxHandle == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) pxHandle )\r
-\r
+#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) ( pxHandle ) )\r
 \r
+/* Callback function prototypes. --------------------------*/\r
+extern void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );\r
+extern void vApplicationTickHook( void );\r
+               \r
 /* File private functions. --------------------------------*/\r
 \r
 /*\r
@@ -339,6 +389,12 @@ static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
  */\r
 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;\r
 \r
+/*\r
+ * The currently executing task is entering the Blocked state.  Add the task to\r
+ * either the current or the overflow delayed task list.\r
+ */\r
+static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake ) PRIVILEGED_FUNCTION;\r
+\r
 /*\r
  * Allocates memory from the heap for a TCB and associated stack.  Checks the\r
  * allocation was successful.\r
@@ -384,7 +440,9 @@ signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const signed ch
 {\r
 signed portBASE_TYPE xReturn;\r
 tskTCB * pxNewTCB;\r
-portBASE_TYPE xRunPrivileged;\r
+\r
+       configASSERT( pxTaskCode );\r
+       configASSERT( ( uxPriority < configMAX_PRIORITIES ) );\r
 \r
        /* Allocate the memory required by the TCB and stack for the new task,\r
        checking that the allocation was successful. */\r
@@ -394,16 +452,19 @@ portBASE_TYPE xRunPrivileged;
        {\r
                portSTACK_TYPE *pxTopOfStack;\r
 \r
-               /* Should the task be created in privileged mode? */\r
-               if( ( uxPriority & portPRIVILEGE_BIT ) != 0x00 )\r
-               {\r
-                       xRunPrivileged = pdTRUE;\r
-               }\r
-               else\r
-               {\r
-                       xRunPrivileged = pdFALSE;\r
-               }\r
-               uxPriority &= ~portPRIVILEGE_BIT;\r
+               #if( portUSING_MPU_WRAPPERS == 1 )\r
+                       /* Should the task be created in privileged mode? */\r
+                       portBASE_TYPE xRunPrivileged;\r
+                       if( ( uxPriority & portPRIVILEGE_BIT ) != 0x00 )\r
+                       {\r
+                               xRunPrivileged = pdTRUE;\r
+                       }\r
+                       else\r
+                       {\r
+                               xRunPrivileged = pdFALSE;\r
+                       }\r
+                       uxPriority &= ~portPRIVILEGE_BIT;\r
+               #endif /* portUSING_MPU_WRAPPERS == 1 */\r
 \r
                /* Calculate the top of stack address.  This depends on whether the\r
                stack grows from high memory to low (as per the 80x86) or visa versa.\r
@@ -411,12 +472,18 @@ portBASE_TYPE xRunPrivileged;
                required by the port. */\r
                #if( portSTACK_GROWTH < 0 )\r
                {\r
-                       pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );\r
+                       pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( unsigned short ) 1 );\r
                        pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( unsigned long ) pxTopOfStack ) & ( ( unsigned long ) ~portBYTE_ALIGNMENT_MASK  ) );\r
+\r
+                       /* Check the alignment of the calculated top of stack is correct. */\r
+                       configASSERT( ( ( ( unsigned long ) pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
                }\r
                #else\r
                {\r
                        pxTopOfStack = pxNewTCB->pxStack;\r
+                       \r
+                       /* Check the alignment of the stack buffer is correct. */\r
+                       configASSERT( ( ( ( unsigned long ) pxNewTCB->pxStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
 \r
                        /* If we want to use stack checking on architectures that use\r
                        a positive stack growth direction then we also need to store the\r
@@ -439,24 +506,38 @@ portBASE_TYPE xRunPrivileged;
                #else\r
                {\r
                        pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );\r
-                       ( void ) xRunPrivileged;\r
                }\r
                #endif\r
 \r
+               /* Check the alignment of the initialised stack. */\r
+               configASSERT( ( ( ( unsigned long ) pxNewTCB->pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
+\r
+               if( ( void * ) pxCreatedTask != NULL )\r
+               {\r
+                       /* Pass the TCB out - in an anonymous way.  The calling function/\r
+                       task can use this as a handle to delete the task later if\r
+                       required.*/\r
+                       *pxCreatedTask = ( xTaskHandle ) pxNewTCB;\r
+               }\r
+               \r
                /* We are going to manipulate the task queues to add this task to a\r
                ready list, so must make sure no interrupts occur. */\r
-               portENTER_CRITICAL();\r
+               taskENTER_CRITICAL();\r
                {\r
                        uxCurrentNumberOfTasks++;\r
-                       if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )\r
+                       if( pxCurrentTCB == NULL )\r
                        {\r
-                               /* As this is the first task it must also be the current task. */\r
+                               /* There are no other tasks, or all the other tasks are in\r
+                               the suspended state - make this the current task. */\r
                                pxCurrentTCB =  pxNewTCB;\r
 \r
-                               /* This is the first task to be created so do the preliminary\r
-                               initialisation required.  We will not recover if this call\r
-                               fails, but we will report the failure. */\r
-                               prvInitialiseTaskLists();\r
+                               if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )\r
+                               {\r
+                                       /* This is the first task to be created so do the preliminary\r
+                                       initialisation required.  We will not recover if this call\r
+                                       fails, but we will report the failure. */\r
+                                       prvInitialiseTaskLists();\r
+                               }\r
                        }\r
                        else\r
                        {\r
@@ -492,24 +573,16 @@ portBASE_TYPE xRunPrivileged;
                        xReturn = pdPASS;\r
                        traceTASK_CREATE( pxNewTCB );\r
                }\r
-               portEXIT_CRITICAL();\r
+               taskEXIT_CRITICAL();\r
        }\r
        else\r
        {\r
                xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;\r
-               traceTASK_CREATE_FAILED( pxNewTCB );\r
+               traceTASK_CREATE_FAILED();\r
        }\r
 \r
        if( xReturn == pdPASS )\r
        {\r
-               if( ( void * ) pxCreatedTask != NULL )\r
-               {\r
-                       /* Pass the TCB out - in an anonymous way.  The calling function/\r
-                       task can use this as a handle to delete the task later if\r
-                       required.*/\r
-                       *pxCreatedTask = ( xTaskHandle ) pxNewTCB;\r
-               }\r
-\r
                if( xSchedulerRunning != pdFALSE )\r
                {\r
                        /* If the created task is of a higher priority than the current task\r
@@ -531,7 +604,7 @@ portBASE_TYPE xRunPrivileged;
        {\r
        tskTCB *pxTCB;\r
 \r
-               portENTER_CRITICAL();\r
+               taskENTER_CRITICAL();\r
                {\r
                        /* Ensure a yield is performed if the current task is being\r
                        deleted. */\r
@@ -568,7 +641,7 @@ portBASE_TYPE xRunPrivileged;
 \r
                        traceTASK_DELETE( pxTCB );\r
                }\r
-               portEXIT_CRITICAL();\r
+               taskEXIT_CRITICAL();\r
 \r
                /* Force a reschedule if we have just deleted the current task. */\r
                if( xSchedulerRunning != pdFALSE )\r
@@ -598,6 +671,9 @@ portBASE_TYPE xRunPrivileged;
        portTickType xTimeToWake;\r
        portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;\r
 \r
+               configASSERT( pxPreviousWakeTime );\r
+               configASSERT( ( xTimeIncrement > 0 ) );\r
+\r
                vTaskSuspendAll();\r
                {\r
                        /* Generate the tick time at which the task wants to wake. */\r
@@ -629,7 +705,7 @@ portBASE_TYPE xRunPrivileged;
                        /* Update the wake time ready for the next call. */\r
                        *pxPreviousWakeTime = xTimeToWake;\r
 \r
-                       if( xShouldDelay )\r
+                       if( xShouldDelay != pdFALSE )\r
                        {\r
                                traceTASK_DELAY_UNTIL();\r
 \r
@@ -637,22 +713,7 @@ portBASE_TYPE xRunPrivileged;
                                ourselves to the blocked list as the same list item is used for\r
                                both lists. */\r
                                vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-\r
-                               /* The list item will be inserted in wake time order. */\r
-                               listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );\r
-\r
-                               if( xTimeToWake < xTickCount )\r
-                               {\r
-                                       /* Wake time has overflowed.  Place this item in the\r
-                                       overflow list. */\r
-                                       vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-                               }\r
-                               else\r
-                               {\r
-                                       /* The wake time has not overflowed, so we can use the\r
-                                       current block list. */\r
-                                       vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-                               }\r
+                               prvAddCurrentTaskToDelayedList( xTimeToWake );\r
                        }\r
                }\r
                xAlreadyYielded = xTaskResumeAll();\r
@@ -698,22 +759,7 @@ portBASE_TYPE xRunPrivileged;
                                ourselves to the blocked list as the same list item is used for\r
                                both lists. */\r
                                vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-\r
-                               /* The list item will be inserted in wake time order. */\r
-                               listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );\r
-\r
-                               if( xTimeToWake < xTickCount )\r
-                               {\r
-                                       /* Wake time has overflowed.  Place this item in the\r
-                                       overflow list. */\r
-                                       vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-                               }\r
-                               else\r
-                               {\r
-                                       /* The wake time has not overflowed, so we can use the\r
-                                       current block list. */\r
-                                       vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-                               }\r
+                               prvAddCurrentTaskToDelayedList( xTimeToWake );\r
                        }\r
                        xAlreadyYielded = xTaskResumeAll();\r
                }\r
@@ -736,14 +782,14 @@ portBASE_TYPE xRunPrivileged;
        tskTCB *pxTCB;\r
        unsigned portBASE_TYPE uxReturn;\r
 \r
-               portENTER_CRITICAL();\r
+               taskENTER_CRITICAL();\r
                {\r
                        /* If null is passed in here then we are changing the\r
                        priority of the calling function. */\r
                        pxTCB = prvGetTCBFromHandle( pxTask );\r
                        uxReturn = pxTCB->uxPriority;\r
                }\r
-               portEXIT_CRITICAL();\r
+               taskEXIT_CRITICAL();\r
 \r
                return uxReturn;\r
        }\r
@@ -756,15 +802,18 @@ portBASE_TYPE xRunPrivileged;
        void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )\r
        {\r
        tskTCB *pxTCB;\r
-       unsigned portBASE_TYPE uxCurrentPriority, xYieldRequired = pdFALSE;\r
+       unsigned portBASE_TYPE uxCurrentPriority;\r
+       portBASE_TYPE xYieldRequired = pdFALSE;\r
+\r
+               configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );\r
 \r
                /* Ensure the new priority is valid. */\r
                if( uxNewPriority >= configMAX_PRIORITIES )\r
                {\r
-                       uxNewPriority = configMAX_PRIORITIES - 1;\r
+                       uxNewPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
                }\r
 \r
-               portENTER_CRITICAL();\r
+               taskENTER_CRITICAL();\r
                {\r
                        if( pxTask == pxCurrentTCB )\r
                        {\r
@@ -850,7 +899,7 @@ portBASE_TYPE xRunPrivileged;
                                }\r
                        }\r
                }\r
-               portEXIT_CRITICAL();\r
+               taskEXIT_CRITICAL();\r
        }\r
 \r
 #endif\r
@@ -862,7 +911,7 @@ portBASE_TYPE xRunPrivileged;
        {\r
        tskTCB *pxTCB;\r
 \r
-               portENTER_CRITICAL();\r
+               taskENTER_CRITICAL();\r
                {\r
                        /* Ensure a yield is performed if the current task is being\r
                        suspended. */\r
@@ -887,12 +936,33 @@ portBASE_TYPE xRunPrivileged;
 \r
                        vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );\r
                }\r
-               portEXIT_CRITICAL();\r
+               taskEXIT_CRITICAL();\r
 \r
-               /* We may have just suspended the current task. */\r
                if( ( void * ) pxTaskToSuspend == NULL )\r
                {\r
-                       portYIELD_WITHIN_API();\r
+                       if( xSchedulerRunning != pdFALSE )\r
+                       {\r
+                               /* We have just suspended the current task. */\r
+                               portYIELD_WITHIN_API();\r
+                       }\r
+                       else\r
+                       {\r
+                               /* The scheduler is not running, but the task that was pointed\r
+                               to by pxCurrentTCB has just been suspended and pxCurrentTCB\r
+                               must be adjusted to point to a different task. */\r
+                               if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) \r
+                               {\r
+                                       /* No other tasks are ready, so set pxCurrentTCB back to\r
+                                       NULL so when the next task is created pxCurrentTCB will\r
+                                       be set to point to it no matter what its relative priority\r
+                                       is. */\r
+                                       pxCurrentTCB = NULL;\r
+                               }\r
+                               else\r
+                               {\r
+                                       vTaskSwitchContext();\r
+                               }\r
+                       }\r
                }\r
        }\r
 \r
@@ -906,6 +976,9 @@ portBASE_TYPE xRunPrivileged;
        portBASE_TYPE xReturn = pdFALSE;\r
        const tskTCB * const pxTCB = ( tskTCB * ) xTask;\r
 \r
+               /* It does not make sense to check if the calling task is suspended. */\r
+               configASSERT( xTask );\r
+\r
                /* Is the task we are attempting to resume actually in the\r
                suspended list? */\r
                if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
@@ -936,6 +1009,9 @@ portBASE_TYPE xRunPrivileged;
        {\r
        tskTCB *pxTCB;\r
 \r
+               /* It does not make sense to resume the calling task. */\r
+               configASSERT( pxTaskToResume );\r
+\r
                /* Remove the task from whichever list it is currently in, and place\r
                it in the ready list. */\r
                pxTCB = ( tskTCB * ) pxTaskToResume;\r
@@ -944,7 +1020,7 @@ portBASE_TYPE xRunPrivileged;
                currently executing task. */\r
                if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )\r
                {\r
-                       portENTER_CRITICAL();\r
+                       taskENTER_CRITICAL();\r
                        {\r
                                if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
                                {\r
@@ -964,7 +1040,7 @@ portBASE_TYPE xRunPrivileged;
                                        }\r
                                }\r
                        }\r
-                       portEXIT_CRITICAL();\r
+                       taskEXIT_CRITICAL();\r
                }\r
        }\r
 \r
@@ -979,6 +1055,8 @@ portBASE_TYPE xRunPrivileged;
        portBASE_TYPE xYieldRequired = pdFALSE;\r
        tskTCB *pxTCB;\r
 \r
+               configASSERT( pxTaskToResume );\r
+\r
                pxTCB = ( tskTCB * ) pxTaskToResume;\r
 \r
                if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
@@ -1018,7 +1096,27 @@ void vTaskStartScheduler( void )
 portBASE_TYPE xReturn;\r
 \r
        /* Add the idle task at the lowest priority. */\r
-       xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), ( xTaskHandle * ) NULL );\r
+       #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
+       {\r
+               /* Create the idle task, storing its handle in xIdleTaskHandle so it can\r
+               be returned by the xTaskGetIdleTaskHandle() function. */\r
+               xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle );\r
+       }\r
+       #else\r
+       {\r
+               /* Create the idle task without storing its handle. */\r
+               xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL );\r
+       }\r
+       #endif\r
+\r
+       #if ( configUSE_TIMERS == 1 )\r
+       {\r
+               if( xReturn == pdPASS )\r
+               {\r
+                       xReturn = xTimerCreateTimerTask();\r
+               }\r
+       }\r
+       #endif\r
 \r
        if( xReturn == pdPASS )\r
        {\r
@@ -1039,7 +1137,7 @@ portBASE_TYPE xReturn;
                macro must be defined to configure the timer/counter used to generate\r
                the run time counter time base. */\r
                portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();\r
-\r
+               \r
                /* Setting up the timer tick is hardware specific and thus in the\r
                portable interface. */\r
                if( xPortStartScheduler() )\r
@@ -1052,6 +1150,9 @@ portBASE_TYPE xReturn;
                        /* Should only reach here if a task calls xTaskEndScheduler(). */\r
                }\r
        }\r
+\r
+       /* This line will only be reached if the kernel could not be started. */\r
+       configASSERT( xReturn );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1079,12 +1180,16 @@ signed portBASE_TYPE xTaskResumeAll( void )
 register tskTCB *pxTCB;\r
 signed portBASE_TYPE xAlreadyYielded = pdFALSE;\r
 \r
+       /* If uxSchedulerSuspended is zero then this function does not match a\r
+       previous call to vTaskSuspendAll(). */\r
+       configASSERT( uxSchedulerSuspended );\r
+\r
        /* It is possible that an ISR caused a task to be removed from an event\r
        list while the scheduler was suspended.  If this was the case then the\r
        removed task will have been added to the xPendingReadyList.  Once the\r
        scheduler has been resumed it is safe to move all the pending ready\r
        tasks from this list into their appropriate ready list. */\r
-       portENTER_CRITICAL();\r
+       taskENTER_CRITICAL();\r
        {\r
                --uxSchedulerSuspended;\r
 \r
@@ -1096,8 +1201,9 @@ signed portBASE_TYPE xAlreadyYielded = pdFALSE;
 \r
                                /* Move any readied tasks from the pending list into the\r
                                appropriate ready list. */\r
-                               while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY(  ( ( xList * ) &xPendingReadyList ) ) ) != NULL )\r
+                               while( listLIST_IS_EMPTY( ( xList * ) &xPendingReadyList ) == pdFALSE )\r
                                {\r
+                                       pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY(  ( ( xList * ) &xPendingReadyList ) );\r
                                        vListRemove( &( pxTCB->xEventListItem ) );\r
                                        vListRemove( &( pxTCB->xGenericListItem ) );\r
                                        prvAddTaskToReadyQueue( pxTCB );\r
@@ -1140,7 +1246,7 @@ signed portBASE_TYPE xAlreadyYielded = pdFALSE;
                        }\r
                }\r
        }\r
-       portEXIT_CRITICAL();\r
+       taskEXIT_CRITICAL();\r
 \r
        return xAlreadyYielded;\r
 }\r
@@ -1161,16 +1267,29 @@ portTickType xTaskGetTickCount( void )
 portTickType xTicks;\r
 \r
        /* Critical section required if running on a 16 bit processor. */\r
-       portENTER_CRITICAL();\r
+       taskENTER_CRITICAL();\r
        {\r
                xTicks = xTickCount;\r
        }\r
-       portEXIT_CRITICAL();\r
+       taskEXIT_CRITICAL();\r
 \r
        return xTicks;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+portTickType xTaskGetTickCountFromISR( void )\r
+{\r
+portTickType xReturn;\r
+unsigned portBASE_TYPE uxSavedInterruptStatus;\r
+\r
+       uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
+       xReturn = xTickCount;\r
+       portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
+\r
+       return xReturn;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )\r
 {\r
        /* A critical section is not required because the variables are of type\r
@@ -1179,6 +1298,17 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+signed char *pcTaskGetTaskName( xTaskHandle xTaskToQuery )\r
+{\r
+tskTCB *pxTCB;\r
+\r
+       /* If null is passed in here then the name of the calling task is being queried. */\r
+       pxTCB = prvGetTCBFromHandle( xTaskToQuery );\r
+       configASSERT( pxTCB );\r
+       return &( pxTCB->pcTaskName[ 0 ] );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
        void vTaskList( signed char *pcWriteBuffer )\r
@@ -1193,34 +1323,34 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
                        /* Run through all the lists that could potentially contain a TCB and\r
                        report the task name, state and stack high water mark. */\r
 \r
-                       pcWriteBuffer[ 0 ] = ( signed char ) 0x00;\r
+                       *pcWriteBuffer = ( signed char ) 0x00;\r
                        strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );\r
 \r
-                       uxQueue = uxTopUsedPriority + 1;\r
+                       uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;\r
 \r
                        do\r
                        {\r
                                uxQueue--;\r
 \r
-                               if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )\r
+                               if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )\r
                                {\r
                                        prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );\r
                                }\r
                        }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );\r
 \r
-                       if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )\r
+                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )\r
                        {\r
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );\r
                        }\r
 \r
-                       if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )\r
+                       if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )\r
                        {\r
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );\r
                        }\r
 \r
                        #if( INCLUDE_vTaskDelete == 1 )\r
                        {\r
-                               if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )\r
+                               if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )\r
                                {\r
                                        prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );\r
                                }\r
@@ -1229,7 +1359,7 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
 \r
                        #if ( INCLUDE_vTaskSuspend == 1 )\r
                        {\r
-                               if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )\r
+                               if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
                                {\r
                                        prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );\r
                                }\r
@@ -1247,45 +1377,55 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
        void vTaskGetRunTimeStats( signed char *pcWriteBuffer )\r
        {\r
        unsigned portBASE_TYPE uxQueue;\r
-       unsigned long ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();\r
+       unsigned long ulTotalRunTime;\r
 \r
                /* This is a VERY costly function that should be used for debug only.\r
                It leaves interrupts disabled for a LONG time. */\r
 \r
                vTaskSuspendAll();\r
                {\r
+                       #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE\r
+                               portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );\r
+                       #else\r
+                               ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();\r
+                       #endif\r
+\r
+                       /* Divide ulTotalRunTime by 100 to make the percentage caluclations\r
+                       simpler in the prvGenerateRunTimeStatsForTasksInList() function. */\r
+                       ulTotalRunTime /= 100UL;\r
+                       \r
                        /* Run through all the lists that could potentially contain a TCB,\r
                        generating a table of run timer percentages in the provided\r
                        buffer. */\r
 \r
-                       pcWriteBuffer[ 0 ] = ( signed char ) 0x00;\r
+                       *pcWriteBuffer = ( signed char ) 0x00;\r
                        strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );\r
 \r
-                       uxQueue = uxTopUsedPriority + 1;\r
+                       uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;\r
 \r
                        do\r
                        {\r
                                uxQueue--;\r
 \r
-                               if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )\r
+                               if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )\r
                                {\r
                                        prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );\r
                                }\r
                        }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );\r
 \r
-                       if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )\r
+                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )\r
                        {\r
                                prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );\r
                        }\r
 \r
-                       if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )\r
+                       if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )\r
                        {\r
                                prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );\r
                        }\r
 \r
                        #if ( INCLUDE_vTaskDelete == 1 )\r
                        {\r
-                               if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )\r
+                               if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )\r
                                {\r
                                        prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, ulTotalRunTime );\r
                                }\r
@@ -1294,7 +1434,7 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
 \r
                        #if ( INCLUDE_vTaskSuspend == 1 )\r
                        {\r
-                               if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )\r
+                               if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
                                {\r
                                        prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, ulTotalRunTime );\r
                                }\r
@@ -1311,14 +1451,17 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
 \r
        void vTaskStartTrace( signed char * pcBuffer, unsigned long ulBufferSize )\r
        {\r
-               portENTER_CRITICAL();\r
+               configASSERT( pcBuffer );\r
+               configASSERT( ulBufferSize );\r
+\r
+               taskENTER_CRITICAL();\r
                {\r
                        pcTraceBuffer = ( signed char * )pcBuffer;\r
                        pcTraceBufferStart = pcBuffer;\r
                        pcTraceBufferEnd = pcBuffer + ( ulBufferSize - tskSIZE_OF_EACH_TRACE_LINE );\r
                        xTracing = pdTRUE;\r
                }\r
-               portEXIT_CRITICAL();\r
+               taskEXIT_CRITICAL();\r
        }\r
 \r
 #endif\r
@@ -1330,9 +1473,9 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
        {\r
        unsigned long ulBufferLength;\r
 \r
-               portENTER_CRITICAL();\r
+               taskENTER_CRITICAL();\r
                        xTracing = pdFALSE;\r
-               portEXIT_CRITICAL();\r
+               taskEXIT_CRITICAL();\r
 \r
                ulBufferLength = ( unsigned long ) ( pcTraceBuffer - pcTraceBufferStart );\r
 \r
@@ -1340,8 +1483,19 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
        }\r
 \r
 #endif\r
+/*----------------------------------------------------------*/\r
 \r
+#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
 \r
+       xTaskHandle xTaskGetIdleTaskHandle( void )\r
+       {\r
+               /* If xTaskGetIdleTaskHandle() is called before the scheduler has been\r
+               started, then xIdleTaskHandle will be NULL. */\r
+               configASSERT( ( xIdleTaskHandle != NULL ) );\r
+               return xIdleTaskHandle;\r
+       }\r
+       \r
+#endif\r
 \r
 /*-----------------------------------------------------------\r
  * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES\r
@@ -1351,6 +1505,8 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
 \r
 void vTaskIncrementTick( void )\r
 {\r
+tskTCB * pxTCB;\r
+\r
        /* Called by the portable layer each time a tick interrupt occurs.\r
        Increments the tick then checks to see if the new tick value will cause any\r
        tasks to be unblocked. */\r
@@ -1364,10 +1520,31 @@ void vTaskIncrementTick( void )
                        /* Tick count has overflowed so we need to swap the delay lists.\r
                        If there are any items in pxDelayedTaskList here then there is\r
                        an error! */\r
+                       configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );\r
+                       \r
                        pxTemp = pxDelayedTaskList;\r
                        pxDelayedTaskList = pxOverflowDelayedTaskList;\r
                        pxOverflowDelayedTaskList = pxTemp;\r
                        xNumOfOverflows++;\r
+       \r
+                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )\r
+                       {\r
+                               /* The new current delayed list is empty.  Set \r
+                               xNextTaskUnblockTime to the maximum possible value so it is \r
+                               extremely unlikely that the     \r
+                               if( xTickCount >= xNextTaskUnblockTime ) test will pass until \r
+                               there is an item in the delayed list. */\r
+                               xNextTaskUnblockTime = portMAX_DELAY;\r
+                       }\r
+                       else\r
+                       {\r
+                               /* The new current delayed list is not empty, get the value of \r
+                               the item at the head of the delayed list.  This is the time at \r
+                               which the task at the head of the delayed list should be removed \r
+                               from the Blocked state. */\r
+                               pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );\r
+                               xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );\r
+                       }\r
                }\r
 \r
                /* See if this tick has made a timeout expire. */\r
@@ -1381,8 +1558,6 @@ void vTaskIncrementTick( void )
                scheduler is locked. */\r
                #if ( configUSE_TICK_HOOK == 1 )\r
                {\r
-                       extern void vApplicationTickHook( void );\r
-\r
                        vApplicationTickHook();\r
                }\r
                #endif\r
@@ -1390,11 +1565,9 @@ void vTaskIncrementTick( void )
 \r
        #if ( configUSE_TICK_HOOK == 1 )\r
        {\r
-               extern void vApplicationTickHook( void );\r
-\r
                /* Guard against the tick hook being called when the missed tick\r
                count is being unwound (when the scheduler is being unlocked. */\r
-               if( uxMissedTicks == 0 )\r
+               if( uxMissedTicks == ( unsigned portBASE_TYPE ) 0U )\r
                {\r
                        vApplicationTickHook();\r
                }\r
@@ -1419,7 +1592,7 @@ void vTaskIncrementTick( void )
                {\r
                        usQueue--;\r
 \r
-                       while( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ usQueue ] ) ) )\r
+                       while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ usQueue ] ) ) == pdFALSE )\r
                        {\r
                                listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &( pxReadyTasksLists[ usQueue ] ) );\r
                                vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );\r
@@ -1429,7 +1602,7 @@ void vTaskIncrementTick( void )
                }while( usQueue > ( unsigned short ) tskIDLE_PRIORITY );\r
 \r
                /* Remove any TCB's from the delayed queue. */\r
-               while( !listLIST_IS_EMPTY( &xDelayedTaskList1 ) )\r
+               while( listLIST_IS_EMPTY( &xDelayedTaskList1 ) == pdFALSE )\r
                {\r
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList1 );\r
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );\r
@@ -1438,7 +1611,7 @@ void vTaskIncrementTick( void )
                }\r
 \r
                /* Remove any TCB's from the overflow delayed queue. */\r
-               while( !listLIST_IS_EMPTY( &xDelayedTaskList2 ) )\r
+               while( listLIST_IS_EMPTY( &xDelayedTaskList2 ) == pdFALSE )\r
                {\r
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList2 );\r
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );\r
@@ -1446,7 +1619,7 @@ void vTaskIncrementTick( void )
                        prvDeleteTCB( ( tskTCB * ) pxTCB );\r
                }\r
 \r
-               while( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )\r
+               while( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
                {\r
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xSuspendedTaskList );\r
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );\r
@@ -1460,7 +1633,7 @@ void vTaskIncrementTick( void )
 \r
 #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
 \r
-       void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxTagValue )\r
+       void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction )\r
        {\r
        tskTCB *xTCB;\r
 \r
@@ -1476,9 +1649,9 @@ void vTaskIncrementTick( void )
 \r
                /* Save the hook function in the TCB.  A critical section is required as\r
                the value can be accessed from an interrupt. */\r
-               portENTER_CRITICAL();\r
-                       xTCB->pxTaskTag = pxTagValue;\r
-               portEXIT_CRITICAL();\r
+               taskENTER_CRITICAL();\r
+                       xTCB->pxTaskTag = pxHookFunction;\r
+               taskEXIT_CRITICAL();\r
        }\r
 \r
 #endif\r
@@ -1503,9 +1676,9 @@ void vTaskIncrementTick( void )
 \r
                /* Save the hook function in the TCB.  A critical section is required as\r
                the value can be accessed from an interrupt. */\r
-               portENTER_CRITICAL();\r
+               taskENTER_CRITICAL();\r
                        xReturn = xTCB->pxTaskTag;\r
-               portEXIT_CRITICAL();\r
+               taskEXIT_CRITICAL();\r
 \r
                return xReturn;\r
        }\r
@@ -1552,40 +1725,48 @@ void vTaskSwitchContext( void )
                /* The scheduler is currently suspended - do not allow a context\r
                switch. */\r
                xMissedYield = pdTRUE;\r
-               return;\r
        }\r
-\r
-       traceTASK_SWITCHED_OUT();\r
-\r
-       #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
-       {\r
-               unsigned long ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();\r
-\r
-                       /* Add the amount of time the task has been running to the accumulated\r
-                       time so far.  The time the task started running was stored in\r
-                       ulTaskSwitchedInTime.  Note that there is no overflow protection here\r
-                       so count values are only valid until the timer overflows.  Generally\r
-                       this will be about 1 hour assuming a 1uS timer increment. */\r
-                       pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );\r
-                       ulTaskSwitchedInTime = ulTempCounter;\r
-       }\r
-       #endif\r
-\r
-       taskFIRST_CHECK_FOR_STACK_OVERFLOW();\r
-       taskSECOND_CHECK_FOR_STACK_OVERFLOW();\r
-\r
-       /* Find the highest priority queue that contains ready tasks. */\r
-       while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )\r
+       else\r
        {\r
-               --uxTopReadyPriority;\r
+               traceTASK_SWITCHED_OUT();\r
+       \r
+               #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+               {\r
+                       unsigned long ulTempCounter;\r
+                       \r
+                               #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE\r
+                                       portALT_GET_RUN_TIME_COUNTER_VALUE( ulTempCounter );\r
+                               #else\r
+                                       ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();\r
+                               #endif\r
+       \r
+                               /* Add the amount of time the task has been running to the accumulated\r
+                               time so far.  The time the task started running was stored in\r
+                               ulTaskSwitchedInTime.  Note that there is no overflow protection here\r
+                               so count values are only valid until the timer overflows.  Generally\r
+                               this will be about 1 hour assuming a 1uS timer increment. */\r
+                               pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );\r
+                               ulTaskSwitchedInTime = ulTempCounter;\r
+               }\r
+               #endif\r
+       \r
+               taskFIRST_CHECK_FOR_STACK_OVERFLOW();\r
+               taskSECOND_CHECK_FOR_STACK_OVERFLOW();\r
+       \r
+               /* Find the highest priority queue that contains ready tasks. */\r
+               while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )\r
+               {\r
+                       configASSERT( uxTopReadyPriority );\r
+                       --uxTopReadyPriority;\r
+               }\r
+       \r
+               /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the\r
+               same priority get an equal share of the processor time. */\r
+               listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );\r
+       \r
+               traceTASK_SWITCHED_IN();\r
+               vWriteTraceToBuffer();\r
        }\r
-\r
-       /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the\r
-       same priority get an equal share of the processor time. */\r
-       listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );\r
-\r
-       traceTASK_SWITCHED_IN();\r
-       vWriteTraceToBuffer();\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1593,6 +1774,8 @@ void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicks
 {\r
 portTickType xTimeToWake;\r
 \r
+       configASSERT( pxEventList );\r
+\r
        /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE\r
        SCHEDULER SUSPENDED. */\r
 \r
@@ -1621,19 +1804,7 @@ portTickType xTimeToWake;
                        /* Calculate the time at which the task should be woken if the event does\r
                        not occur.  This may overflow but this doesn't matter. */\r
                        xTimeToWake = xTickCount + xTicksToWait;\r
-\r
-                       listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );\r
-\r
-                       if( xTimeToWake < xTickCount )\r
-                       {\r
-                               /* Wake time has overflowed.  Place this item in the overflow list. */\r
-                               vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-                       }\r
-                       else\r
-                       {\r
-                               /* The wake time has not overflowed, so we can use the current block list. */\r
-                               vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-                       }\r
+                       prvAddCurrentTaskToDelayedList( xTimeToWake );\r
                }\r
        }\r
        #else\r
@@ -1641,24 +1812,46 @@ portTickType xTimeToWake;
                        /* Calculate the time at which the task should be woken if the event does\r
                        not occur.  This may overflow but this doesn't matter. */\r
                        xTimeToWake = xTickCount + xTicksToWait;\r
-\r
-                       listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );\r
-\r
-                       if( xTimeToWake < xTickCount )\r
-                       {\r
-                               /* Wake time has overflowed.  Place this item in the overflow list. */\r
-                               vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-                       }\r
-                       else\r
-                       {\r
-                               /* The wake time has not overflowed, so we can use the current block list. */\r
-                               vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-                       }\r
+                       prvAddCurrentTaskToDelayedList( xTimeToWake );\r
        }\r
        #endif\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+#if configUSE_TIMERS == 1\r
+\r
+       void vTaskPlaceOnEventListRestricted( const xList * const pxEventList, portTickType xTicksToWait )\r
+       {\r
+       portTickType xTimeToWake;\r
+\r
+               configASSERT( pxEventList );\r
+\r
+               /* This function should not be called by application code hence the\r
+               'Restricted' in its name.  It is not part of the public API.  It is\r
+               designed for use by kernel code, and has special calling requirements -\r
+               it should be called from a critical section. */\r
+\r
+       \r
+               /* Place the event list item of the TCB in the appropriate event list.\r
+               In this case it is assume that this is the only task that is going to\r
+               be waiting on this event list, so the faster vListInsertEnd() function\r
+               can be used in place of vListInsert. */\r
+               vListInsertEnd( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );\r
+\r
+               /* We must remove this task from the ready list before adding it to the\r
+               blocked list as the same list item is used for both lists.  This\r
+               function is called form a critical section. */\r
+               vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
+\r
+               /* Calculate the time at which the task should be woken if the event does\r
+               not occur.  This may overflow but this doesn't matter. */\r
+               xTimeToWake = xTickCount + xTicksToWait;\r
+               prvAddCurrentTaskToDelayedList( xTimeToWake );\r
+       }\r
+       \r
+#endif /* configUSE_TIMERS */\r
+/*-----------------------------------------------------------*/\r
+\r
 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )\r
 {\r
 tskTCB *pxUnblockedTCB;\r
@@ -1673,8 +1866,12 @@ portBASE_TYPE xReturn;
 \r
        If an event is for a queue that is locked then this function will never\r
        get called - the lock count on the queue will get modified instead.  This\r
-       means we can always expect exclusive access to the event list here. */\r
+       means we can always expect exclusive access to the event list here.\r
+       \r
+       This function assumes that a check has already been made to ensure that\r
+       pxEventList is not empty. */\r
        pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );\r
+       configASSERT( pxUnblockedTCB );\r
        vListRemove( &( pxUnblockedTCB->xEventListItem ) );\r
 \r
        if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
@@ -1708,6 +1905,7 @@ portBASE_TYPE xReturn;
 \r
 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )\r
 {\r
+       configASSERT( pxTimeOut );\r
        pxTimeOut->xOverflowCount = xNumOfOverflows;\r
        pxTimeOut->xTimeOnEntering = xTickCount;\r
 }\r
@@ -1717,7 +1915,10 @@ portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType
 {\r
 portBASE_TYPE xReturn;\r
 \r
-       portENTER_CRITICAL();\r
+       configASSERT( pxTimeOut );\r
+       configASSERT( pxTicksToWait );\r
+\r
+       taskENTER_CRITICAL();\r
        {\r
                #if ( INCLUDE_vTaskSuspend == 1 )\r
                        /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is\r
@@ -1738,7 +1939,7 @@ portBASE_TYPE xReturn;
                        passed since vTaskSetTimeout() was called. */\r
                        xReturn = pdTRUE;\r
                }\r
-               else if( ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) < ( portTickType ) *pxTicksToWait )\r
+               else if( ( ( portTickType ) ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) ) < ( portTickType ) *pxTicksToWait )\r
                {\r
                        /* Not a genuine timeout. Adjust parameters for time remaining. */\r
                        *pxTicksToWait -= ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering );\r
@@ -1750,7 +1951,7 @@ portBASE_TYPE xReturn;
                        xReturn = pdTRUE;\r
                }\r
        }\r
-       portEXIT_CRITICAL();\r
+       taskEXIT_CRITICAL();\r
 \r
        return xReturn;\r
 }\r
@@ -1846,13 +2047,13 @@ static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const
                strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );\r
        }\r
        #endif\r
-       pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = '\0';\r
+       pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = ( signed char ) '\0';\r
 \r
        /* This is used as an array index so must ensure it's not too large.  First\r
        remove the privilege bit if one is present. */\r
        if( uxPriority >= configMAX_PRIORITIES )\r
        {\r
-               uxPriority = configMAX_PRIORITIES - 1;\r
+               uxPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
        }\r
 \r
        pxTCB->uxPriority = uxPriority;\r
@@ -1927,7 +2128,7 @@ static void prvInitialiseTaskLists( void )
 {\r
 unsigned portBASE_TYPE uxPriority;\r
 \r
-       for( uxPriority = 0; uxPriority < configMAX_PRIORITIES; uxPriority++ )\r
+       for( uxPriority = ( unsigned portBASE_TYPE ) 0U; uxPriority < configMAX_PRIORITIES; uxPriority++ )\r
        {\r
                vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );\r
        }\r
@@ -1969,18 +2170,18 @@ static void prvCheckTasksWaitingTermination( void )
                                xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );\r
                        xTaskResumeAll();\r
 \r
-                       if( !xListIsEmpty )\r
+                       if( xListIsEmpty == pdFALSE )\r
                        {\r
                                tskTCB *pxTCB;\r
 \r
-                               portENTER_CRITICAL();\r
+                               taskENTER_CRITICAL();\r
                                {\r
                                        pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );\r
                                        vListRemove( &( pxTCB->xGenericListItem ) );\r
                                        --uxCurrentNumberOfTasks;\r
                                        --uxTasksDeleted;\r
                                }\r
-                               portEXIT_CRITICAL();\r
+                               taskEXIT_CRITICAL();\r
 \r
                                prvDeleteTCB( pxTCB );\r
                        }\r
@@ -1990,6 +2191,32 @@ static void prvCheckTasksWaitingTermination( void )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake )\r
+{\r
+       /* The list item will be inserted in wake time order. */\r
+       listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );\r
+\r
+       if( xTimeToWake < xTickCount )\r
+       {\r
+               /* Wake time has overflowed.  Place this item in the overflow list. */\r
+               vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
+       }\r
+       else\r
+       {\r
+               /* The wake time has not overflowed, so we can use the current block list. */\r
+               vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
+\r
+               /* If the task entering the blocked state was placed at the head of the\r
+               list of blocked tasks then xNextTaskUnblockTime needs to be updated\r
+               too. */\r
+               if( xTimeToWake < xNextTaskUnblockTime )\r
+               {\r
+                       xNextTaskUnblockTime = xTimeToWake;\r
+               }\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer )\r
 {\r
 tskTCB *pxNewTCB;\r
@@ -2034,7 +2261,16 @@ tskTCB *pxNewTCB;
                do\r
                {\r
                        listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
-                       usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );\r
+                       #if ( portSTACK_GROWTH > 0 )\r
+                       {\r
+                               usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxEndOfStack );\r
+                       }\r
+                       #else\r
+                       {\r
+                               usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );\r
+                       }\r
+                       #endif                  \r
+                       \r
                        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 );\r
                        strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatusString );\r
 \r
@@ -2069,19 +2305,40 @@ tskTCB *pxNewTCB;
                                }\r
                                else\r
                                {\r
-                                       /* What percentage of the total run time as the task used?\r
-                                       This will always be rounded down to the nearest integer. */\r
-                                       ulStatsAsPercentage = ( 100UL * pxNextTCB->ulRunTimeCounter ) / ulTotalRunTime;\r
+                                       /* What percentage of the total run time has the task used?\r
+                                       This will always be rounded down to the nearest integer.\r
+                                       ulTotalRunTime has already been divided by 100. */\r
+                                       ulStatsAsPercentage = pxNextTCB->ulRunTimeCounter / ulTotalRunTime;\r
 \r
                                        if( ulStatsAsPercentage > 0UL )\r
                                        {\r
-                                               sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t%u%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );\r
+                                               #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
+                                               {\r
+                                                       sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );                                                 \r
+                                               }\r
+                                               #else\r
+                                               {\r
+                                                       /* sizeof( int ) == sizeof( long ) so a smaller\r
+                                                       printf() library can be used. */\r
+                                                       sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t%u%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );\r
+                                               }\r
+                                               #endif\r
                                        }\r
                                        else\r
                                        {\r
                                                /* If the percentage is zero here then the task has\r
                                                consumed less than 1% of the total run time. */\r
-                                               sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t<1%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter );\r
+                                               #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
+                                               {\r
+                                                       sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );                                                       \r
+                                               }\r
+                                               #else\r
+                                               {\r
+                                                       /* sizeof( int ) == sizeof( long ) so a smaller\r
+                                                       printf() library can be used. */\r
+                                                       sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t<1%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter );\r
+                                               }\r
+                                               #endif\r
                                        }\r
                                }\r
 \r
@@ -2157,7 +2414,7 @@ tskTCB *pxNewTCB;
 \r
 /*-----------------------------------------------------------*/\r
 \r
-#if ( INCLUDE_xTaskGetCurrentTaskHandle == 1 )\r
+#if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )\r
 \r
        xTaskHandle xTaskGetCurrentTaskHandle( void )\r
        {\r
@@ -2175,7 +2432,7 @@ tskTCB *pxNewTCB;
 \r
 /*-----------------------------------------------------------*/\r
 \r
-#if ( INCLUDE_xTaskGetSchedulerState == 1 )\r
+#if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
 \r
        portBASE_TYPE xTaskGetSchedulerState( void )\r
        {\r
@@ -2209,6 +2466,8 @@ tskTCB *pxNewTCB;
        {\r
        tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;\r
 \r
+               configASSERT( pxMutexHolder );\r
+\r
                if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )\r
                {\r
                        /* Adjust the mutex holder state to account for its new priority. */\r
@@ -2269,7 +2528,7 @@ tskTCB *pxNewTCB;
 \r
                if( xSchedulerRunning != pdFALSE )\r
                {\r
-                       pxCurrentTCB->uxCriticalNesting++;\r
+                       ( pxCurrentTCB->uxCriticalNesting )++;\r
                }\r
        }\r
 \r
@@ -2284,7 +2543,7 @@ void vTaskExitCritical( void )
        {\r
                if( pxCurrentTCB->uxCriticalNesting > 0 )\r
                {\r
-                       pxCurrentTCB->uxCriticalNesting--;\r
+                       ( pxCurrentTCB->uxCriticalNesting )--;\r
 \r
                        if( pxCurrentTCB->uxCriticalNesting == 0 )\r
                        {\r