]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/tasks.c
Linting and MISRA checking
[freertos] / FreeRTOS / Source / tasks.c
index 0c43e63d6c8d8188c009ee02b644772483e8960e..a851af055bf05e99fa28b2eea1e83d90ded53270 100644 (file)
@@ -1,6 +1,8 @@
 /*\r
-    FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
-       \r
+    FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
+\r
+    FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
+    http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
 \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
+\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
+    kernel.\r
+\r
+    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
+    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
+    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
+    details. You should have received a copy of the GNU General Public License\r
+    and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
+    viewed here: http://www.freertos.org/a00114.html and also obtained by\r
+    writing to Real Time Engineers Ltd., contact details for whom are available\r
+    on the FreeRTOS WEB site.\r
 \r
     1 tab == 4 spaces!\r
-    \r
+\r
     ***************************************************************************\r
      *                                                                       *\r
      *    Having a problem?  Start by reading the FAQ "My application does   *\r
-     *    not run, what could be wrong?                                      *\r
+     *    not run, what could be wrong?"                                     *\r
      *                                                                       *\r
      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
      *                                                                       *\r
     ***************************************************************************\r
 \r
-    \r
-    http://www.FreeRTOS.org - Documentation, training, latest information, \r
-    license and contact details.\r
-    \r
+\r
+    http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
+    license and Real Time Engineers Ltd. contact details.\r
+\r
     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
-    including FreeRTOS+Trace - an indispensable productivity tool.\r
+    including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
+    fully thread aware and reentrant UDP/IP stack.\r
 \r
-    Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
-    the code with commercial support, indemnification, and middleware, under \r
-    the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
-    provide a safety engineered and independently SIL3 certified version under \r
-    the SafeRTOS brand: http://www.SafeRTOS.com.\r
-*/\r
+    http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
+    Integrity Systems, who sell the code with commercial support,\r
+    indemnification and middleware, under the OpenRTOS brand.\r
 \r
+    http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
+    engineered and independently SIL3 certified version for use in safety and\r
+    mission critical applications that require provable dependability.\r
+*/\r
 \r
-#include <stdio.h>\r
+/* Standard includes. */\r
 #include <stdlib.h>\r
 #include <string.h>\r
 \r
@@ -74,47 +81,68 @@ all the API functions to use the MPU wrappers.  That should only be done when
 task.h is included from an application file. */\r
 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
 \r
+/* FreeRTOS includes. */\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
+/* Lint e961 and e750 are suppressed as a MISRA exception justified because the\r
+MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the\r
+header files above, but not in this file, in order to generate the correct\r
+privileged Vs unprivileged linkage and placement. */\r
+#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */\r
+\r
+#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configINCLUDE_STATS_FORMATTING_FUNCTIONS == 1 ) )\r
+       /* At the bottom of this file are two optional functions that can be used\r
+       to generate human readable text from the raw data generated by the\r
+       xTaskGetSystemState() function.  Note the formatting functions are provided\r
+       for convenience only, and are NOT considered part of the kernel. */\r
+       #include <stdio.h>\r
+#endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configINCLUDE_STATS_FORMATTING_FUNCTIONS == 1 ) ) */\r
+\r
+/* Sanity check the configuration. */\r
+#if configUSE_TICKLESS_IDLE != 0\r
+       #if INCLUDE_vTaskSuspend != 1\r
+               #error INCLUDE_vTaskSuspend must be set to 1 if configUSE_TICKLESS_IDLE is not set to 0\r
+       #endif /* INCLUDE_vTaskSuspend */\r
+#endif /* configUSE_TICKLESS_IDLE */\r
 \r
 /*\r
- * Macro to define the amount of stack available to the idle task.\r
+ * Defines the size, in words, of the stack allocated to the idle task.\r
  */\r
 #define tskIDLE_STACK_SIZE     configMINIMAL_STACK_SIZE\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
+ * Task control block.  A task control block (TCB) is allocated for each task,\r
+ * and stores task state information, including a pointer to the task's context\r
+ * (the task's run time environment, including register values)\r
  */\r
 typedef struct tskTaskControlBlock\r
 {\r
-       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. */\r
+       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 TCB STRUCT. */\r
 \r
        #if ( portUSING_MPU_WRAPPERS == 1 )\r
-               xMPU_SETTINGS xMPUSettings;                             /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE STRUCT. */\r
-       #endif  \r
-       \r
-       xListItem                               xGenericListItem;       /*< List item used to place the TCB in ready and blocked queues. */\r
-       xListItem                               xEventListItem;         /*< List item used to place the TCB in event lists. */\r
-       unsigned portBASE_TYPE  uxPriority;                     /*< The priority of the task where 0 is the lowest priority. */\r
+               xMPU_SETTINGS xMPUSettings;                             /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */\r
+       #endif\r
+\r
+       xListItem                               xGenericListItem;       /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */\r
+       xListItem                               xEventListItem;         /*< Used to reference a task from an event list. */\r
+       unsigned portBASE_TYPE  uxPriority;                     /*< The priority of the task 0 is the lowest priority. */\r
        portSTACK_TYPE                  *pxStack;                       /*< Points to the start of the stack. */\r
        signed char                             pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */\r
 \r
        #if ( portSTACK_GROWTH > 0 )\r
-               portSTACK_TYPE *pxEndOfStack;                   /*< Used for stack overflow checking on architectures where the stack grows up from low memory. */\r
+               portSTACK_TYPE *pxEndOfStack;                   /*< Points to the end of the stack on architectures where the stack grows up from low memory. */\r
        #endif\r
 \r
        #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
-               unsigned portBASE_TYPE uxCriticalNesting;\r
+               unsigned portBASE_TYPE uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */\r
        #endif\r
 \r
        #if ( configUSE_TRACE_FACILITY == 1 )\r
-               unsigned portBASE_TYPE  uxTCBNumber;    /*< This stores a number that increments each time a TCB is created.  It allows debuggers to determine when a task has been deleted and then recreated. */\r
-               unsigned portBASE_TYPE  uxTaskNumber;   /*< This stores a number specifically for use by third party trace code. */\r
+               unsigned portBASE_TYPE  uxTCBNumber;    /*< Stores a number that increments each time a TCB is created.  It allows debuggers to determine when a task has been deleted and then recreated. */\r
+               unsigned portBASE_TYPE  uxTaskNumber;   /*< Stores a number specifically for use by third party trace code. */\r
        #endif\r
 \r
        #if ( configUSE_MUTEXES == 1 )\r
@@ -126,31 +154,43 @@ typedef struct tskTaskControlBlock
        #endif\r
 \r
        #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
-               unsigned long ulRunTimeCounter;         /*< Used for calculating how much CPU time each task is utilising. */\r
+               unsigned long ulRunTimeCounter;                 /*< Stores the amount of time the task has spent in the Running state. */\r
+       #endif\r
+\r
+       #if ( configUSE_NEWLIB_REENTRANT == 1 )\r
+               /* Allocate a Newlib reent structure that is specific to this task.\r
+               Note Newlib support has been included by popular demand, but is not\r
+               used by the FreeRTOS maintainers themselves.  FreeRTOS is not\r
+               responsible for resulting newlib operation.  User must be familiar with\r
+               newlib and must provide system-wide implementations of the necessary\r
+               stubs. Be warned that (at the time of writing) the current newlib design\r
+               implements a system-wide malloc() that must be provided with locks. */\r
+               struct _reent xNewLib_reent;\r
        #endif\r
 \r
 } tskTCB;\r
 \r
 \r
 /*\r
- * Some kernel aware debuggers require data to be viewed to be global, rather\r
- * than file scope.\r
+ * Some kernel aware debuggers require the data the debugger needs access to to\r
+ * be global, rather than file scope.\r
  */\r
 #ifdef portREMOVE_STATIC_QUALIFIER\r
        #define static\r
 #endif\r
 \r
-/*lint -e956 */\r
+/*lint -e956 A manual analysis and inspection has been used to determine which\r
+static variables must be declared volatile. */\r
+\r
 PRIVILEGED_DATA tskTCB * volatile pxCurrentTCB = NULL;\r
 \r
 /* Lists for ready and blocked tasks. --------------------*/\r
-\r
 PRIVILEGED_DATA static xList pxReadyTasksLists[ configMAX_PRIORITIES ];        /*< Prioritised ready tasks. */\r
 PRIVILEGED_DATA static xList xDelayedTaskList1;                                                        /*< Delayed tasks. */\r
 PRIVILEGED_DATA static xList xDelayedTaskList2;                                                        /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */\r
-PRIVILEGED_DATA static xList * volatile pxDelayedTaskList ;                            /*< Points to the delayed task list currently being used. */\r
+PRIVILEGED_DATA static xList * volatile pxDelayedTaskList                            /*< Points to the delayed task list currently being used. */\r
 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. */\r
-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. */\r
+PRIVILEGED_DATA static xList xPendingReadyList;                                                        /*< Tasks that have been readied while the scheduler was suspended.  They will be moved to the ready list when the scheduler is resumed. */\r
 \r
 #if ( INCLUDE_vTaskDelete == 1 )\r
 \r
@@ -166,32 +206,32 @@ PRIVILEGED_DATA static xList xPendingReadyList;                                                   /*< Tasks that have been r
 #endif\r
 \r
 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
-       \r
-       PRIVILEGED_DATA static xTaskHandle xIdleTaskHandle = NULL;\r
-       \r
+\r
+       PRIVILEGED_DATA static xTaskHandle xIdleTaskHandle = NULL;                      /*< Holds the handle of the idle task.  The idle task is created automatically when the scheduler is started. */\r
+\r
 #endif\r
 \r
-/* File private variables. --------------------------------*/\r
+/* Other file private variables. --------------------------------*/\r
 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks  = ( unsigned portBASE_TYPE ) 0U;\r
 PRIVILEGED_DATA static volatile portTickType xTickCount                                                = ( portTickType ) 0U;\r
-PRIVILEGED_DATA static unsigned portBASE_TYPE uxTopUsedPriority                                        = tskIDLE_PRIORITY;\r
 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTopReadyPriority              = tskIDLE_PRIORITY;\r
 PRIVILEGED_DATA static volatile signed portBASE_TYPE xSchedulerRunning                         = pdFALSE;\r
 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxSchedulerSuspended            = ( unsigned portBASE_TYPE ) pdFALSE;\r
-PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxMissedTicks                   = ( unsigned portBASE_TYPE ) 0U;\r
-PRIVILEGED_DATA static volatile portBASE_TYPE xMissedYield                                             = ( portBASE_TYPE ) pdFALSE;\r
+PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxPendedTicks                   = ( unsigned portBASE_TYPE ) 0U;\r
+PRIVILEGED_DATA static volatile portBASE_TYPE xYieldPending                                    = pdFALSE;\r
 PRIVILEGED_DATA static volatile portBASE_TYPE xNumOfOverflows                                  = ( portBASE_TYPE ) 0;\r
 PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                             = ( unsigned portBASE_TYPE ) 0U;\r
-PRIVILEGED_DATA static portTickType xNextTaskUnblockTime                                               = ( portTickType ) portMAX_DELAY;\r
+PRIVILEGED_DATA static volatile portTickType xNextTaskUnblockTime                              = portMAX_DELAY;\r
 \r
 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
 \r
-       PRIVILEGED_DATA static char pcStatsString[ 50 ] ;\r
        PRIVILEGED_DATA static unsigned long ulTaskSwitchedInTime = 0UL;        /*< Holds the value of a timer/counter the last time a task was switched in. */\r
-       static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime ) PRIVILEGED_FUNCTION;\r
+       PRIVILEGED_DATA static unsigned long ulTotalRunTime = 0UL;                      /*< Holds the total amount of execution time as defined by the run time counter clock. */\r
 \r
 #endif\r
 \r
+/*lint +e956 */\r
+\r
 /* Debugging and trace facilities private variables and macros. ------------*/\r
 \r
 /*\r
@@ -210,81 +250,128 @@ PRIVILEGED_DATA static portTickType xNextTaskUnblockTime                                         = ( portTickType )
 \r
 /*-----------------------------------------------------------*/\r
 \r
-/*\r
- * Place the task represented by pxTCB into the appropriate ready queue for\r
- * the task.  It is inserted at the end of the list.  One quirk of this is\r
- * that if the task being inserted is at the same priority as the currently\r
- * executing task, then it will only be rescheduled after the currently\r
- * executing task has been rescheduled.\r
- */\r
-#define prvAddTaskToReadyQueue( pxTCB )                                                                                                                                                                        \\r
-       traceMOVED_TASK_TO_READY_STATE( pxTCB )                                                                                                                                                         \\r
-       if( ( pxTCB )->uxPriority > uxTopReadyPriority )                                                                                                                                        \\r
+#if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )\r
+\r
+       /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is\r
+       performed in a generic way that is not optimised to any particular\r
+       microcontroller architecture. */\r
+\r
+       /* uxTopReadyPriority holds the priority of the highest priority ready\r
+       state task. */\r
+       #define taskRECORD_READY_PRIORITY( uxPriority )                                                                                                                                         \\r
+       {                                                                                                                                                                                                                                       \\r
+               if( ( uxPriority ) > uxTopReadyPriority )                                                                                                                                               \\r
+               {                                                                                                                                                                                                                               \\r
+                       uxTopReadyPriority = ( uxPriority );                                                                                                                                            \\r
+               }                                                                                                                                                                                                                               \\r
+       } /* taskRECORD_READY_PRIORITY */\r
+\r
+       /*-----------------------------------------------------------*/\r
+\r
+       #define taskSELECT_HIGHEST_PRIORITY_TASK()                                                                                                                                                      \\r
        {                                                                                                                                                                                                                                       \\r
-               uxTopReadyPriority = ( pxTCB )->uxPriority;                                                                                                                                             \\r
-       }                                                                                                                                                                                                                                       \\r
-       vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )\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 indexes through the list, so the tasks of                                                                                \\r
+               the     same priority get an equal share of the processor time. */                                                                                                      \\r
+               listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );                                              \\r
+       } /* taskSELECT_HIGHEST_PRIORITY_TASK */\r
+\r
+       /*-----------------------------------------------------------*/\r
+\r
+       /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as\r
+       they are only required when a port optimised method of task selection is\r
+       being used. */\r
+       #define taskRESET_READY_PRIORITY( uxPriority )\r
+       #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )\r
+\r
+#else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
+\r
+       /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is\r
+       performed in a way that is tailored to the particular microcontroller\r
+       architecture being used. */\r
+\r
+       /* A port optimised version is provided.  Call the port defined macros. */\r
+       #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )\r
+\r
+       /*-----------------------------------------------------------*/\r
+\r
+       #define taskSELECT_HIGHEST_PRIORITY_TASK()                                                                                                              \\r
+       {                                                                                                                                                                                               \\r
+       unsigned portBASE_TYPE uxTopPriority;                                                                                                                   \\r
+                                                                                                                                                                                                       \\r
+               /* Find the highest priority queue that contains ready tasks. */                                                        \\r
+               portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );                                                          \\r
+               configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );         \\r
+               listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );           \\r
+       } /* taskSELECT_HIGHEST_PRIORITY_TASK() */\r
+\r
+       /*-----------------------------------------------------------*/\r
+\r
+       /* A port optimised version is provided, call it only if the TCB being reset\r
+       is being referenced from a ready list.  If it is referenced from a delayed\r
+       or suspended list then it won't be in a ready list. */\r
+       #define taskRESET_READY_PRIORITY( uxPriority )                                                                                                  \\r
+       {                                                                                                                                                                                               \\r
+               if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == 0 )                          \\r
+               {                                                                                                                                                                                       \\r
+                       portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) );                                             \\r
+               }                                                                                                                                                                                       \\r
+       }\r
+\r
+#endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick\r
+count overflows. */\r
+#define taskSWITCH_DELAYED_LISTS()                                                                                                                                     \\r
+{                                                                                                                                                                                                      \\r
+       xList *pxTemp;                                                                                                                                                                  \\r
+                                                                                                                                                                                                       \\r
+       /* The delayed tasks list should be empty when the lists are switched. */                                               \\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
 /*-----------------------------------------------------------*/\r
 \r
 /*\r
- * Macro that looks at the list of tasks that are currently delayed to see if\r
- * any require waking.\r
- *\r
- * Tasks are stored in the queue in the order of their wake time - meaning\r
- * once one tasks has been found whose timer has not expired we need not look\r
- * any further down the list.\r
+ * Place the task represented by pxTCB into the appropriate ready list for\r
+ * the task.  It is inserted at the end of the list.\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 != NULL )                                                 \\r
-                               {                                                                                                                                               \\r
-                                       vListRemove( &( pxTCB->xEventListItem ) );                                                      \\r
-                               }                                                                                                                                               \\r
-                               prvAddTaskToReadyQueue( pxTCB );                                                                                \\r
-                       }                                                                                                                                                       \\r
-               }                                                                                                                                                               \\r
-       }                                                                                                                                                                       \\r
-}\r
+#define prvAddTaskToReadyList( pxTCB )                                                                                                                                                         \\r
+       traceMOVED_TASK_TO_READY_STATE( pxTCB )                                                                                                                                                 \\r
+       taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority );                                                                                                                             \\r
+       vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )\r
 /*-----------------------------------------------------------*/\r
 \r
 /*\r
@@ -296,9 +383,9 @@ portTickType xItemValue;                                                                                                                            \
 #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 vApplicationStackOverflowHook( xTaskHandle xTask, signed char *pcTaskName );\r
 extern void vApplicationTickHook( void );\r
-               \r
+\r
 /* File private functions. --------------------------------*/\r
 \r
 /*\r
@@ -359,17 +446,16 @@ static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake ) PRIVILEGE
 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
- * Called from vTaskList.  vListTasks details all the tasks currently under\r
- * control of the scheduler.  The tasks may be in one of a number of lists.\r
- * prvListTaskWithinSingleList accepts a list and details the tasks from\r
- * within just that list.\r
+ * Fills an xTaskStatusType structure with information on each task that is\r
+ * referenced from the pxList list (which may be a ready list, a delayed list,\r
+ * a suspended list, etc.).\r
  *\r
  * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM\r
  * NORMAL APPLICATION CODE.\r
  */\r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus ) PRIVILEGED_FUNCTION;\r
+       static unsigned portBASE_TYPE prvListTaskWithinSingleList( xTaskStatusType *pxTaskStatusArray, xList *pxList, eTaskState eState ) PRIVILEGED_FUNCTION;\r
 \r
 #endif\r
 \r
@@ -380,18 +466,24 @@ static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TY
  */\r
 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
 \r
-       static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte ) PRIVILEGED_FUNCTION;\r
+       static unsigned short prvTaskCheckFreeStackSpace( const unsigned char * pucStackByte ) PRIVILEGED_FUNCTION;\r
 \r
 #endif\r
 \r
+/*\r
+ * Return the amount of time, in ticks, that will pass before the kernel will\r
+ * next move a task from the Blocked state to the Running state.\r
+ *\r
+ * This conditional compilation should use inequality to 0, not equality to 1.\r
+ * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user\r
+ * defined low power mode implementations require configUSE_TICKLESS_IDLE to be\r
+ * set to a value other than 1.\r
+ */\r
+#if ( configUSE_TICKLESS_IDLE != 0 )\r
 \r
-/*lint +e956 */\r
-\r
-\r
+       static portTickType prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;\r
 \r
-/*-----------------------------------------------------------\r
- * TASK CREATION API documented in task.h\r
- *----------------------------------------------------------*/\r
+#endif\r
 \r
 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 )\r
 {\r
@@ -430,15 +522,15 @@ tskTCB * pxNewTCB;
                #if( portSTACK_GROWTH < 0 )\r
                {\r
                        pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( unsigned short ) 1 );\r
-                       pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK  ) );\r
+                       pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK  ) ); /*lint !e923 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type. */\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
+               #else /* portSTACK_GROWTH */\r
                {\r
                        pxTopOfStack = pxNewTCB->pxStack;\r
-                       \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
@@ -447,7 +539,7 @@ tskTCB * pxNewTCB;
                        other extreme of the stack space. */\r
                        pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );\r
                }\r
-               #endif\r
+               #endif /* portSTACK_GROWTH */\r
 \r
                /* Setup the newly allocated TCB with the initial state of the task. */\r
                prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth );\r
@@ -460,14 +552,11 @@ tskTCB * pxNewTCB;
                {\r
                        pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );\r
                }\r
-               #else\r
+               #else /* portUSING_MPU_WRAPPERS */\r
                {\r
                        pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );\r
                }\r
-               #endif\r
-\r
-               /* Check the alignment of the initialised stack. */\r
-               portALIGNMENT_ASSERT_pxCurrentTCB( ( ( ( unsigned long ) pxNewTCB->pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
+               #endif /* portUSING_MPU_WRAPPERS */\r
 \r
                if( ( void * ) pxCreatedTask != NULL )\r
                {\r
@@ -476,9 +565,9 @@ tskTCB * pxNewTCB;
                        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
+\r
+               /* Ensure interrupts don't access the task lists while they are being\r
+               updated. */\r
                taskENTER_CRITICAL();\r
                {\r
                        uxCurrentNumberOfTasks++;\r
@@ -510,26 +599,20 @@ tskTCB * pxNewTCB;
                                }\r
                        }\r
 \r
-                       /* Remember the top priority to make context switching faster.  Use\r
-                       the priority in pxNewTCB as this has been capped to a valid value. */\r
-                       if( pxNewTCB->uxPriority > uxTopUsedPriority )\r
-                       {\r
-                               uxTopUsedPriority = pxNewTCB->uxPriority;\r
-                       }\r
+                       uxTaskNumber++;\r
 \r
                        #if ( configUSE_TRACE_FACILITY == 1 )\r
                        {\r
                                /* Add a counter into the TCB for tracing only. */\r
                                pxNewTCB->uxTCBNumber = uxTaskNumber;\r
                        }\r
-                       #endif\r
-                       uxTaskNumber++;\r
+                       #endif /* configUSE_TRACE_FACILITY */\r
+                       traceTASK_CREATE( pxNewTCB );\r
 \r
-                       prvAddTaskToReadyQueue( pxNewTCB );\r
+                       prvAddTaskToReadyList( pxNewTCB );\r
 \r
                        xReturn = pdPASS;\r
                        portSETUP_TCB( pxNewTCB );\r
-                       traceTASK_CREATE( pxNewTCB );\r
                }\r
                taskEXIT_CRITICAL();\r
        }\r
@@ -558,7 +641,7 @@ tskTCB * pxNewTCB;
 \r
 #if ( INCLUDE_vTaskDelete == 1 )\r
 \r
-       void vTaskDelete( xTaskHandle pxTaskToDelete )\r
+       void vTaskDelete( xTaskHandle xTaskToDelete )\r
        {\r
        tskTCB *pxTCB;\r
 \r
@@ -566,27 +649,30 @@ tskTCB * pxNewTCB;
                {\r
                        /* Ensure a yield is performed if the current task is being\r
                        deleted. */\r
-                       if( pxTaskToDelete == pxCurrentTCB )\r
+                       if( xTaskToDelete == pxCurrentTCB )\r
                        {\r
-                               pxTaskToDelete = NULL;\r
+                               xTaskToDelete = NULL;\r
                        }\r
 \r
                        /* If null is passed in here then we are deleting ourselves. */\r
-                       pxTCB = prvGetTCBFromHandle( pxTaskToDelete );\r
+                       pxTCB = prvGetTCBFromHandle( xTaskToDelete );\r
 \r
                        /* Remove task from the ready list and place in the     termination list.\r
                        This will stop the task from be scheduled.  The idle task will check\r
                        the termination list and free up any memory allocated by the\r
                        scheduler for the TCB and stack. */\r
-                       vListRemove( &( pxTCB->xGenericListItem ) );\r
+                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( unsigned portBASE_TYPE ) 0 )\r
+                       {\r
+                               taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
+                       }\r
 \r
                        /* Is the task waiting on an event also? */\r
-                       if( pxTCB->xEventListItem.pvContainer != NULL )\r
+                       if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )\r
                        {\r
-                               vListRemove( &( pxTCB->xEventListItem ) );\r
+                               ( void ) uxListRemove( &( pxTCB->xEventListItem ) );\r
                        }\r
 \r
-                       vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );\r
+                       vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );\r
 \r
                        /* Increment the ucTasksDeleted variable so the idle task knows\r
                        there is a task that has been deleted and that it should therefore\r
@@ -604,23 +690,15 @@ tskTCB * pxNewTCB;
                /* Force a reschedule if we have just deleted the current task. */\r
                if( xSchedulerRunning != pdFALSE )\r
                {\r
-                       if( ( void * ) pxTaskToDelete == NULL )\r
+                       if( ( void * ) xTaskToDelete ==  NULL ) /*lint !e961 MISRA exception as this is not a redundant cast when used with some supported compilers. */\r
                        {\r
                                portYIELD_WITHIN_API();\r
                        }\r
                }\r
        }\r
 \r
-#endif\r
-\r
-\r
-\r
-\r
-\r
-\r
-/*-----------------------------------------------------------\r
- * TASK CONTROL API documented in task.h\r
- *----------------------------------------------------------*/\r
+#endif /* INCLUDE_vTaskDelete */\r
+/*-----------------------------------------------------------*/\r
 \r
 #if ( INCLUDE_vTaskDelayUntil == 1 )\r
 \r
@@ -634,17 +712,21 @@ tskTCB * pxNewTCB;
 \r
                vTaskSuspendAll();\r
                {\r
+                       /* Minor optimisation.  The tick count cannot change in this\r
+                       block. */\r
+                       const portTickType xConstTickCount = xTickCount;\r
+\r
                        /* Generate the tick time at which the task wants to wake. */\r
                        xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;\r
 \r
-                       if( xTickCount < *pxPreviousWakeTime )\r
+                       if( xConstTickCount < *pxPreviousWakeTime )\r
                        {\r
                                /* The tick count has overflowed since this function was\r
                                lasted called.  In this case the only time we should ever\r
                                actually delay is if the wake time has also     overflowed,\r
                                and the wake time is greater than the tick time.  When this\r
                                is the case it is as if neither time had overflowed. */\r
-                               if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xTickCount ) )\r
+                               if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )\r
                                {\r
                                        xShouldDelay = pdTRUE;\r
                                }\r
@@ -654,7 +736,7 @@ tskTCB * pxNewTCB;
                                /* The tick time has not overflowed.  In this case we will\r
                                delay if either the wake time has overflowed, and/or the\r
                                tick time is less than the wake time. */\r
-                               if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xTickCount ) )\r
+                               if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )\r
                                {\r
                                        xShouldDelay = pdTRUE;\r
                                }\r
@@ -670,7 +752,14 @@ tskTCB * pxNewTCB;
                                /* We must remove ourselves from the ready list before adding\r
                                ourselves to the blocked list as the same list item is used for\r
                                both lists. */\r
-                               vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
+                               if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( unsigned portBASE_TYPE ) 0 )\r
+                               {\r
+                                       /* The current task must be in a ready list, so there is\r
+                                       no need to check, and the port reset macro can be called\r
+                                       directly. */\r
+                                       portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
+                               }\r
+\r
                                prvAddCurrentTaskToDelayedList( xTimeToWake );\r
                        }\r
                }\r
@@ -684,7 +773,7 @@ tskTCB * pxNewTCB;
                }\r
        }\r
 \r
-#endif\r
+#endif /* INCLUDE_vTaskDelayUntil */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( INCLUDE_vTaskDelay == 1 )\r
@@ -716,7 +805,13 @@ tskTCB * pxNewTCB;
                                /* We must remove ourselves from the ready list before adding\r
                                ourselves to the blocked list as the same list item is used for\r
                                both lists. */\r
-                               vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
+                               if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( unsigned portBASE_TYPE ) 0 )\r
+                               {\r
+                                       /* The current task must be in a ready list, so there is\r
+                                       no need to check, and the port reset macro can be called\r
+                                       directly. */\r
+                                       portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
+                               }\r
                                prvAddCurrentTaskToDelayedList( xTimeToWake );\r
                        }\r
                        xAlreadyYielded = xTaskResumeAll();\r
@@ -730,12 +825,72 @@ tskTCB * pxNewTCB;
                }\r
        }\r
 \r
-#endif\r
+#endif /* INCLUDE_vTaskDelay */\r
+/*-----------------------------------------------------------*/\r
+\r
+#if ( INCLUDE_eTaskGetState == 1 )\r
+\r
+       eTaskState eTaskGetState( xTaskHandle xTask )\r
+       {\r
+       eTaskState eReturn;\r
+       xList *pxStateList;\r
+       const tskTCB * const pxTCB = ( tskTCB * ) xTask;\r
+\r
+               if( pxTCB == pxCurrentTCB )\r
+               {\r
+                       /* The task calling this function is querying its own state. */\r
+                       eReturn = eRunning;\r
+               }\r
+               else\r
+               {\r
+                       taskENTER_CRITICAL();\r
+                       {\r
+                               pxStateList = ( xList * ) listLIST_ITEM_CONTAINER( &( pxTCB->xGenericListItem ) );\r
+                       }\r
+                       taskEXIT_CRITICAL();\r
+\r
+                       if( ( pxStateList == pxDelayedTaskList ) || ( pxStateList == pxOverflowDelayedTaskList ) )\r
+                       {\r
+                               /* The task being queried is referenced from one of the Blocked\r
+                               lists. */\r
+                               eReturn = eBlocked;\r
+                       }\r
+\r
+                       #if ( INCLUDE_vTaskSuspend == 1 )\r
+                               else if( pxStateList == &xSuspendedTaskList )\r
+                               {\r
+                                       /* The task being queried is referenced from the suspended\r
+                                       list. */\r
+                                       eReturn = eSuspended;\r
+                               }\r
+                       #endif\r
+\r
+                       #if ( INCLUDE_vTaskDelete == 1 )\r
+                               else if( pxStateList == &xTasksWaitingTermination )\r
+                               {\r
+                                       /* The task being queried is referenced from the deleted\r
+                                       tasks list. */\r
+                                       eReturn = eDeleted;\r
+                               }\r
+                       #endif\r
+\r
+                       else\r
+                       {\r
+                               /* If the task is not in any other state, it must be in the\r
+                               Ready (including pending ready) state. */\r
+                               eReturn = eReady;\r
+                       }\r
+               }\r
+\r
+               return eReturn;\r
+       }\r
+\r
+#endif /* INCLUDE_eTaskGetState */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( INCLUDE_uxTaskPriorityGet == 1 )\r
 \r
-       unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )\r
+       unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle xTask )\r
        {\r
        tskTCB *pxTCB;\r
        unsigned portBASE_TYPE uxReturn;\r
@@ -744,7 +899,7 @@ tskTCB * pxNewTCB;
                {\r
                        /* If null is passed in here then we are changing the\r
                        priority of the calling function. */\r
-                       pxTCB = prvGetTCBFromHandle( pxTask );\r
+                       pxTCB = prvGetTCBFromHandle( xTask );\r
                        uxReturn = pxTCB->uxPriority;\r
                }\r
                taskEXIT_CRITICAL();\r
@@ -752,35 +907,35 @@ tskTCB * pxNewTCB;
                return uxReturn;\r
        }\r
 \r
-#endif\r
+#endif /* INCLUDE_uxTaskPriorityGet */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( INCLUDE_vTaskPrioritySet == 1 )\r
 \r
-       void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )\r
+       void vTaskPrioritySet( xTaskHandle xTask, unsigned portBASE_TYPE uxNewPriority )\r
        {\r
        tskTCB *pxTCB;\r
-       unsigned portBASE_TYPE uxCurrentPriority;\r
+       unsigned portBASE_TYPE uxCurrentPriority, uxPriorityUsedOnEntry;\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
+               if( uxNewPriority >= ( unsigned portBASE_TYPE ) configMAX_PRIORITIES )\r
                {\r
-                       uxNewPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
+                       uxNewPriority = ( unsigned portBASE_TYPE ) configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
                }\r
 \r
                taskENTER_CRITICAL();\r
                {\r
-                       if( pxTask == pxCurrentTCB )\r
+                       if( xTask == ( xTaskHandle ) pxCurrentTCB )\r
                        {\r
-                               pxTask = NULL;\r
+                               xTask = NULL;\r
                        }\r
 \r
                        /* If null is passed in here then we are changing the\r
                        priority of the calling function. */\r
-                       pxTCB = prvGetTCBFromHandle( pxTask );\r
+                       pxTCB = prvGetTCBFromHandle( xTask );\r
 \r
                        traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );\r
 \r
@@ -800,7 +955,7 @@ tskTCB * pxNewTCB;
                                priority than the calling task. */\r
                                if( uxNewPriority > uxCurrentPriority )\r
                                {\r
-                                       if( pxTask != NULL )\r
+                                       if( xTask != NULL )\r
                                        {\r
                                                /* The priority of another task is being raised.  If we\r
                                                were raising the priority of the currently running task\r
@@ -809,14 +964,21 @@ tskTCB * pxNewTCB;
                                                xYieldRequired = pdTRUE;\r
                                        }\r
                                }\r
-                               else if( pxTask == NULL )\r
+                               else if( xTask == NULL )\r
                                {\r
                                        /* Setting our own priority down means there may now be another\r
                                        task of higher priority that is ready to execute. */\r
                                        xYieldRequired = pdTRUE;\r
                                }\r
+                               else\r
+                               {\r
+                                       /* Yield not required. */\r
+                               }\r
 \r
-\r
+                               /* Remember the ready list the task might be referenced from\r
+                               before its uxPriority member is changed so the\r
+                               taskRESET_READY_PRIORITY() macro can function correctly. */\r
+                               uxPriorityUsedOnEntry = pxTCB->uxPriority;\r
 \r
                                #if ( configUSE_MUTEXES == 1 )\r
                                {\r
@@ -836,36 +998,43 @@ tskTCB * pxNewTCB;
                                }\r
                                #endif\r
 \r
-                               listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) );\r
+                               listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( portTickType ) configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
 \r
                                /* If the task is in the blocked or suspended list we need do\r
                                nothing more than change it's priority variable. However, if\r
                                the task is in a ready list it needs to be removed and placed\r
                                in the queue appropriate to its new priority. */\r
-                               if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )\r
+                               if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
                                {\r
                                        /* The task is currently in its ready list - remove before adding\r
                                        it to it's new ready list.  As we are in a critical section we\r
                                        can do this even if the scheduler is suspended. */\r
-                                       vListRemove( &( pxTCB->xGenericListItem ) );\r
-                                       prvAddTaskToReadyQueue( pxTCB );\r
+                                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( unsigned portBASE_TYPE ) 0 )\r
+                                       {\r
+                                               taskRESET_READY_PRIORITY( uxPriorityUsedOnEntry );\r
+                                       }\r
+                                       prvAddTaskToReadyList( pxTCB );\r
                                }\r
 \r
                                if( xYieldRequired == pdTRUE )\r
                                {\r
                                        portYIELD_WITHIN_API();\r
                                }\r
+\r
+                               /* Remove compiler warning about unused variables when the port\r
+                               optimised task selection is not being used. */\r
+                               ( void ) uxPriorityUsedOnEntry;\r
                        }\r
                }\r
                taskEXIT_CRITICAL();\r
        }\r
 \r
-#endif\r
+#endif /* INCLUDE_vTaskPrioritySet */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( INCLUDE_vTaskSuspend == 1 )\r
 \r
-       void vTaskSuspend( xTaskHandle pxTaskToSuspend )\r
+       void vTaskSuspend( xTaskHandle xTaskToSuspend )\r
        {\r
        tskTCB *pxTCB;\r
 \r
@@ -873,30 +1042,33 @@ tskTCB * pxNewTCB;
                {\r
                        /* Ensure a yield is performed if the current task is being\r
                        suspended. */\r
-                       if( pxTaskToSuspend == pxCurrentTCB )\r
+                       if( xTaskToSuspend == ( xTaskHandle ) pxCurrentTCB )\r
                        {\r
-                               pxTaskToSuspend = NULL;\r
+                               xTaskToSuspend = NULL;\r
                        }\r
 \r
                        /* If null is passed in here then we are suspending ourselves. */\r
-                       pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );\r
+                       pxTCB = prvGetTCBFromHandle( xTaskToSuspend );\r
 \r
                        traceTASK_SUSPEND( pxTCB );\r
 \r
                        /* Remove task from the ready/delayed list and place in the     suspended list. */\r
-                       vListRemove( &( pxTCB->xGenericListItem ) );\r
+                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( unsigned portBASE_TYPE ) 0 )\r
+                       {\r
+                               taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
+                       }\r
 \r
                        /* Is the task waiting on an event also? */\r
-                       if( pxTCB->xEventListItem.pvContainer != NULL )\r
+                       if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )\r
                        {\r
-                               vListRemove( &( pxTCB->xEventListItem ) );\r
+                               ( void ) uxListRemove( &( pxTCB->xEventListItem ) );\r
                        }\r
 \r
-                       vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );\r
+                       vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );\r
                }\r
                taskEXIT_CRITICAL();\r
 \r
-               if( ( void * ) pxTaskToSuspend == NULL )\r
+               if( ( void * ) xTaskToSuspend == NULL ) /*lint !e961 MISRA exception justified because it is not a redundant cast for some supported compilers. */\r
                {\r
                        if( xSchedulerRunning != pdFALSE )\r
                        {\r
@@ -924,7 +1096,7 @@ tskTCB * pxNewTCB;
                }\r
        }\r
 \r
-#endif\r
+#endif /* INCLUDE_vTaskSuspend */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( INCLUDE_vTaskSuspend == 1 )\r
@@ -942,13 +1114,13 @@ tskTCB * pxNewTCB;
                if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
                {\r
                        /* Has the task already been resumed from within an ISR? */\r
-                       if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )\r
+                       if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )\r
                        {\r
                                /* Is it in the suspended list because it is in the\r
                                Suspended state?  It is possible to be in the suspended\r
                                list because it is blocked on a task with no timeout\r
                                specified. */\r
-                               if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) == pdTRUE )\r
+                               if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )\r
                                {\r
                                        xReturn = pdTRUE;\r
                                }\r
@@ -956,23 +1128,19 @@ tskTCB * pxNewTCB;
                }\r
 \r
                return xReturn;\r
-       }\r
+       } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */\r
 \r
-#endif\r
+#endif /* INCLUDE_vTaskSuspend */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( INCLUDE_vTaskSuspend == 1 )\r
 \r
-       void vTaskResume( xTaskHandle pxTaskToResume )\r
+       void vTaskResume( xTaskHandle xTaskToResume )\r
        {\r
-       tskTCB *pxTCB;\r
+       tskTCB * const pxTCB = ( tskTCB * ) xTaskToResume;\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
+               configASSERT( xTaskToResume );\r
 \r
                /* The parameter cannot be NULL as it is impossible to resume the\r
                currently executing task. */\r
@@ -986,8 +1154,8 @@ tskTCB * pxNewTCB;
 \r
                                        /* As we are in a critical section we can access the ready\r
                                        lists even if the scheduler is suspended. */\r
-                                       vListRemove(  &( pxTCB->xGenericListItem ) );\r
-                                       prvAddTaskToReadyQueue( pxTCB );\r
+                                       ( void ) uxListRemove(  &( pxTCB->xGenericListItem ) );\r
+                                       prvAddTaskToReadyList( pxTCB );\r
 \r
                                        /* We may have just resumed a higher priority task. */\r
                                        if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
@@ -1002,21 +1170,37 @@ tskTCB * pxNewTCB;
                }\r
        }\r
 \r
-#endif\r
+#endif /* INCLUDE_vTaskSuspend */\r
 \r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )\r
 \r
-       portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )\r
+       portBASE_TYPE xTaskResumeFromISR( xTaskHandle xTaskToResume )\r
        {\r
        portBASE_TYPE xYieldRequired = pdFALSE;\r
-       tskTCB *pxTCB;\r
+       tskTCB * const pxTCB = ( tskTCB * ) xTaskToResume;\r
        unsigned portBASE_TYPE uxSavedInterruptStatus;\r
 \r
-               configASSERT( pxTaskToResume );\r
-\r
-               pxTCB = ( tskTCB * ) pxTaskToResume;\r
+               configASSERT( xTaskToResume );\r
+\r
+               /* RTOS ports that support interrupt nesting have the concept of a\r
+               maximum system call (or maximum API call) interrupt priority.\r
+               Interrupts that are     above the maximum system call priority are keep\r
+               permanently enabled, even when the RTOS kernel is in a critical section,\r
+               but cannot make any calls to FreeRTOS API functions.  If configASSERT()\r
+               is defined in FreeRTOSConfig.h then\r
+               portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
+               failure if a FreeRTOS API function is called from an interrupt that has\r
+               been assigned a priority above the configured maximum system call\r
+               priority.  Only FreeRTOS functions that end in FromISR can be called\r
+               from interrupts that have been assigned a priority at or (logically)\r
+               below the maximum system call interrupt priority.  FreeRTOS maintains a\r
+               separate interrupt safe API to ensure interrupt entry is as fast and as\r
+               simple as possible.  More information (albeit Cortex-M specific) is\r
+               provided on the following link:\r
+               http://www.freertos.org/RTOS-Cortex-M3-M4.html */\r
+               portASSERT_IF_INTERRUPT_PRIORITY_INVALID();\r
 \r
                uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
                {\r
@@ -1027,15 +1211,15 @@ tskTCB * pxNewTCB;
                                if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
                                {\r
                                        xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );\r
-                                       vListRemove(  &( pxTCB->xGenericListItem ) );\r
-                                       prvAddTaskToReadyQueue( pxTCB );\r
+                                       ( void ) uxListRemove(  &( pxTCB->xGenericListItem ) );\r
+                                       prvAddTaskToReadyList( pxTCB );\r
                                }\r
                                else\r
                                {\r
                                        /* We cannot access the delayed or ready lists, so will hold this\r
                                        task pending until the scheduler is resumed, at which point a\r
                                        yield will be performed if necessary. */\r
-                                       vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );\r
+                                       vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );\r
                                }\r
                        }\r
                }\r
@@ -1044,15 +1228,8 @@ tskTCB * pxNewTCB;
                return xYieldRequired;\r
        }\r
 \r
-#endif\r
-\r
-\r
-\r
-\r
-/*-----------------------------------------------------------\r
- * PUBLIC SCHEDULER CONTROL documented in task.h\r
- *----------------------------------------------------------*/\r
-\r
+#endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */\r
+/*-----------------------------------------------------------*/\r
 \r
 void vTaskStartScheduler( void )\r
 {\r
@@ -1063,14 +1240,14 @@ portBASE_TYPE xReturn;
        {\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
+               xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */\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
+               xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL );  /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */\r
        }\r
-       #endif\r
+       #endif /* INCLUDE_xTaskGetIdleTaskHandle */\r
 \r
        #if ( configUSE_TIMERS == 1 )\r
        {\r
@@ -1079,7 +1256,7 @@ portBASE_TYPE xReturn;
                        xReturn = xTimerCreateTimerTask();\r
                }\r
        }\r
-       #endif\r
+       #endif /* configUSE_TIMERS */\r
 \r
        if( xReturn == pdPASS )\r
        {\r
@@ -1100,7 +1277,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() != pdFALSE )\r
@@ -1113,9 +1290,13 @@ 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
+       else\r
+       {\r
+               /* This line will only be reached if the kernel could not be started,\r
+               because there was not enough FreeRTOS heap to create the idle task\r
+               or the timer task. */\r
+               configASSERT( xReturn );\r
+       }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1138,10 +1319,39 @@ void vTaskSuspendAll( void )
 }\r
 /*----------------------------------------------------------*/\r
 \r
+#if ( configUSE_TICKLESS_IDLE != 0 )\r
+\r
+       static portTickType prvGetExpectedIdleTime( void )\r
+       {\r
+       portTickType xReturn;\r
+\r
+               if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )\r
+               {\r
+                       xReturn = 0;\r
+               }\r
+               else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )\r
+               {\r
+                       /* There are other idle priority tasks in the ready state.  If\r
+                       time slicing is used then the very next tick interrupt must be\r
+                       processed. */\r
+                       xReturn = 0;\r
+               }\r
+               else\r
+               {\r
+                       xReturn = xNextTaskUnblockTime - xTickCount;\r
+               }\r
+\r
+               return xReturn;\r
+       }\r
+\r
+#endif /* configUSE_TICKLESS_IDLE */\r
+/*----------------------------------------------------------*/\r
+\r
 signed portBASE_TYPE xTaskResumeAll( void )\r
 {\r
-register tskTCB *pxTCB;\r
-signed portBASE_TYPE xAlreadyYielded = pdFALSE;\r
+tskTCB *pxTCB;\r
+portBASE_TYPE xAlreadyYielded = pdFALSE;\r
+portBASE_TYPE xYieldRequired = pdFALSE;\r
 \r
        /* If uxSchedulerSuspended is zero then this function does not match a\r
        previous call to vTaskSuspendAll(). */\r
@@ -1160,16 +1370,14 @@ signed portBASE_TYPE xAlreadyYielded = pdFALSE;
                {\r
                        if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0U )\r
                        {\r
-                               portBASE_TYPE xYieldRequired = pdFALSE;\r
-\r
                                /* Move any readied tasks from the pending list into the\r
                                appropriate ready list. */\r
-                               while( listLIST_IS_EMPTY( ( xList * ) &xPendingReadyList ) == pdFALSE )\r
+                               while( listLIST_IS_EMPTY( &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
+                                       pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );\r
+                                       ( void ) uxListRemove( &( pxTCB->xEventListItem ) );\r
+                                       ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );\r
+                                       prvAddTaskToReadyList( pxTCB );\r
 \r
                                        /* If we have moved a task that has a priority higher than\r
                                        the current task then we should yield. */\r
@@ -1182,28 +1390,22 @@ signed portBASE_TYPE xAlreadyYielded = pdFALSE;
                                /* If any ticks occurred while the scheduler was suspended then\r
                                they should be processed now.  This ensures the tick count does not\r
                                slip, and that any delayed tasks are resumed at the correct time. */\r
-                               if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )\r
+                               if( uxPendedTicks > ( unsigned portBASE_TYPE ) 0U )\r
                                {\r
-                                       while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )\r
+                                       while( uxPendedTicks > ( unsigned portBASE_TYPE ) 0U )\r
                                        {\r
-                                               vTaskIncrementTick();\r
-                                               --uxMissedTicks;\r
-                                       }\r
-\r
-                                       /* As we have processed some ticks it is appropriate to yield\r
-                                       to ensure the highest priority task that is ready to run is\r
-                                       the task actually running. */\r
-                                       #if configUSE_PREEMPTION == 1\r
-                                       {\r
-                                               xYieldRequired = pdTRUE;\r
+                                               if( xTaskIncrementTick() != pdFALSE )\r
+                                               {\r
+                                                       xYieldRequired = pdTRUE;\r
+                                               }\r
+                                               --uxPendedTicks;\r
                                        }\r
-                                       #endif\r
                                }\r
 \r
-                               if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )\r
+                               if( ( xYieldRequired == pdTRUE ) || ( xYieldPending == pdTRUE ) )\r
                                {\r
                                        xAlreadyYielded = pdTRUE;\r
-                                       xMissedYield = pdFALSE;\r
+                                       xYieldPending = pdFALSE;\r
                                        portYIELD_WITHIN_API();\r
                                }\r
                        }\r
@@ -1213,17 +1415,7 @@ signed portBASE_TYPE xAlreadyYielded = pdFALSE;
 \r
        return xAlreadyYielded;\r
 }\r
-\r
-\r
-\r
-\r
-\r
-\r
-/*-----------------------------------------------------------\r
- * PUBLIC TASK UTILITIES documented in task.h\r
- *----------------------------------------------------------*/\r
-\r
-\r
+/*-----------------------------------------------------------*/\r
 \r
 portTickType xTaskGetTickCount( void )\r
 {\r
@@ -1245,6 +1437,22 @@ portTickType xTaskGetTickCountFromISR( void )
 portTickType xReturn;\r
 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
 \r
+       /* RTOS ports that support interrupt nesting have the concept of a maximum\r
+       system call (or maximum API call) interrupt priority.  Interrupts that are\r
+       above the maximum system call priority are keep permanently enabled, even\r
+       when the RTOS kernel is in a critical section, but cannot make any calls to\r
+       FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h\r
+       then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
+       failure if a FreeRTOS API function is called from an interrupt that has been\r
+       assigned a priority above the configured maximum system call priority.\r
+       Only FreeRTOS functions that end in FromISR can be called from interrupts\r
+       that have been assigned a priority at or (logically) below the maximum\r
+       system call     interrupt priority.  FreeRTOS maintains a separate interrupt\r
+       safe API to ensure interrupt entry is as fast and as simple as possible.\r
+       More information (albeit Cortex-M specific) is provided on the following\r
+       link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */\r
+       portASSERT_IF_INTERRUPT_PRIORITY_INVALID();\r
+\r
        uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
        xReturn = xTickCount;\r
        portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
@@ -1273,214 +1481,216 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
                return &( pxTCB->pcTaskName[ 0 ] );\r
        }\r
 \r
-#endif\r
+#endif /* INCLUDE_pcTaskGetTaskName */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       void vTaskList( signed char *pcWriteBuffer )\r
+       unsigned portBASE_TYPE xTaskGetSystemState( xTaskStatusType *pxTaskStatusArray, unsigned portBASE_TYPE uxArraySize, unsigned long *pulTotalRunTime )\r
        {\r
-       unsigned portBASE_TYPE uxQueue;\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
+       unsigned portBASE_TYPE uxTask = 0, uxQueue = configMAX_PRIORITIES;\r
 \r
                vTaskSuspendAll();\r
                {\r
-                       /* 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 = ( signed char ) 0x00;\r
-                       strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );\r
+                       /* Is there a space in the array for each task in the system? */\r
+                       if( uxArraySize >= uxCurrentNumberOfTasks )\r
+                       {\r
+                               /* Fill in an xTaskStatusType structure with information on each\r
+                               task in the Ready state. */\r
+                               do\r
+                               {\r
+                                       uxQueue--;\r
+                                       uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );\r
 \r
-                       uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;\r
+                               } while( uxQueue > ( unsigned portBASE_TYPE ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
 \r
-                       do\r
-                       {\r
-                               uxQueue--;\r
+                               /* Fill in an xTaskStatusType structure with information on each\r
+                               task in the Blocked state. */\r
+                               uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( xList * ) pxDelayedTaskList, eBlocked );\r
+                               uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( xList * ) pxOverflowDelayedTaskList, eBlocked );\r
 \r
-                               if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )\r
+                               #if( INCLUDE_vTaskDelete == 1 )\r
                                {\r
-                                       prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );\r
+                                       /* Fill in an xTaskStatusType structure with information on\r
+                                       each task that has been deleted but not yet cleaned up. */\r
+                                       uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );\r
                                }\r
-                       }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );\r
-\r
-                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )\r
-                       {\r
-                               prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );\r
-                       }\r
-\r
-                       if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )\r
-                       {\r
-                               prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );\r
-                       }\r
+                               #endif\r
 \r
-                       #if( INCLUDE_vTaskDelete == 1 )\r
-                       {\r
-                               if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )\r
+                               #if ( INCLUDE_vTaskSuspend == 1 )\r
                                {\r
-                                       prvListTaskWithinSingleList( pcWriteBuffer, &xTasksWaitingTermination, tskDELETED_CHAR );\r
+                                       /* Fill in an xTaskStatusType structure with information on\r
+                                       each task in the Suspended state. */\r
+                                       uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );\r
                                }\r
-                       }\r
-                       #endif\r
+                               #endif\r
 \r
-                       #if ( INCLUDE_vTaskSuspend == 1 )\r
-                       {\r
-                               if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
+                               #if ( configGENERATE_RUN_TIME_STATS == 1)\r
+                               {\r
+                                       if( pulTotalRunTime != NULL )\r
+                                       {\r
+                                               *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();\r
+                                       }\r
+                               }\r
+                               #else\r
                                {\r
-                                       prvListTaskWithinSingleList( pcWriteBuffer, &xSuspendedTaskList, tskSUSPENDED_CHAR );\r
+                                       if( pulTotalRunTime != NULL )\r
+                                       {\r
+                                               *pulTotalRunTime = 0;\r
+                                       }\r
                                }\r
+                               #endif\r
                        }\r
-                       #endif\r
                }\r
-               xTaskResumeAll();\r
+               ( void ) xTaskResumeAll();\r
+\r
+               return uxTask;\r
        }\r
 \r
-#endif\r
+#endif /* configUSE_TRACE_FACILITY */\r
 /*----------------------------------------------------------*/\r
 \r
-#if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
 \r
-       void vTaskGetRunTimeStats( signed char *pcWriteBuffer )\r
+       xTaskHandle xTaskGetIdleTaskHandle( void )\r
        {\r
-       unsigned portBASE_TYPE uxQueue;\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
+               /* 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
-               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
+#endif /* INCLUDE_xTaskGetIdleTaskHandle */\r
+/*----------------------------------------------------------*/\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
+/* This conditional compilation should use inequality to 0, not equality to 1.\r
+This is to ensure vTaskStepTick() is available when user defined low power mode\r
+implementations require configUSE_TICKLESS_IDLE to be set to a value other than\r
+1. */\r
+#if ( configUSE_TICKLESS_IDLE != 0 )\r
 \r
-                       *pcWriteBuffer = ( signed char ) 0x00;\r
-                       strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );\r
+       void vTaskStepTick( portTickType xTicksToJump )\r
+       {\r
+               /* Correct the tick count value after a period during which the tick\r
+               was suppressed.  Note this does *not* call the tick hook function for\r
+               each stepped tick. */\r
+               configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );\r
+               xTickCount += xTicksToJump;\r
+       }\r
 \r
-                       uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;\r
+#endif /* configUSE_TICKLESS_IDLE */\r
+/*----------------------------------------------------------*/\r
 \r
-                       do\r
-                       {\r
-                               uxQueue--;\r
+portBASE_TYPE xTaskIncrementTick( void )\r
+{\r
+tskTCB * pxTCB;\r
+portTickType xItemValue;\r
+portBASE_TYPE xSwitchRequired = pdFALSE;\r
 \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
+       /* 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
+       traceTASK_INCREMENT_TICK( xTickCount );\r
+       if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
+       {\r
+               /* Increment the RTOS tick, switching the delayed and overflowed\r
+               delayed lists if it wraps to 0. */\r
+               ++xTickCount;\r
 \r
-                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )\r
-                       {\r
-                               prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );\r
-                       }\r
+               {\r
+                       /* Minor optimisation.  The tick count cannot change in this\r
+                       block. */\r
+                       const portTickType xConstTickCount = xTickCount;\r
 \r
-                       if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )\r
+                       if( xConstTickCount == ( portTickType ) 0U )\r
                        {\r
-                               prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );\r
+                               taskSWITCH_DELAYED_LISTS();\r
                        }\r
 \r
-                       #if ( INCLUDE_vTaskDelete == 1 )\r
+                       /* See if this tick has made a timeout expire.  Tasks are stored in the\r
+                       queue in the order of their wake time - meaning once one tasks has been\r
+                       found whose block time has not expired there is no need not look any\r
+                       further down the list. */\r
+                       if( xConstTickCount >= xNextTaskUnblockTime )\r
                        {\r
-                               if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )\r
+                               for( ;; )\r
                                {\r
-                                       prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xTasksWaitingTermination, ulTotalRunTime );\r
-                               }\r
-                       }\r
-                       #endif\r
-\r
-                       #if ( INCLUDE_vTaskSuspend == 1 )\r
-                       {\r
-                               if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
-                               {\r
-                                       prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xSuspendedTaskList, ulTotalRunTime );\r
-                               }\r
-                       }\r
-                       #endif\r
-               }\r
-               xTaskResumeAll();\r
-       }\r
-\r
-#endif\r
-/*----------------------------------------------------------*/\r
-\r
-#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
+                                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )\r
+                                       {\r
+                                               /* The delayed list is empty.  Set xNextTaskUnblockTime to\r
+                                               the     maximum possible value so it is extremely unlikely that\r
+                                               the if( xTickCount >= xNextTaskUnblockTime ) test will pass\r
+                                               next 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\r
+                                               at the head of the delayed list.  This is the time at which\r
+                                               the task at the head of the delayed list must be removed\r
+                                               from the Blocked state. */\r
+                                               pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );\r
+                                               xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );\r
+\r
+                                               if( xConstTickCount < 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 must be removed from the Blocked state -\r
+                                                       so record the item value in xNextTaskUnblockTime. */\r
+                                                       xNextTaskUnblockTime = xItemValue;\r
+                                                       break;\r
+                                               }\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
+                                               /* It is time to remove the item from the Blocked state. */\r
+                                               ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );\r
 \r
-/*-----------------------------------------------------------\r
- * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES\r
- * documented in task.h\r
- *----------------------------------------------------------*/\r
+                                               /* Is the task waiting on an event also?  If so remove it\r
+                                               from the event list. */\r
+                                               if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )\r
+                                               {\r
+                                                       ( void ) uxListRemove( &( pxTCB->xEventListItem ) );\r
+                                               }\r
 \r
-void vTaskIncrementTick( void )\r
-{\r
-tskTCB * pxTCB;\r
+                                               /* Place the unblocked task into the appropriate ready\r
+                                               list. */\r
+                                               prvAddTaskToReadyList( 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
-       if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
-       {\r
-               ++xTickCount;\r
-               if( xTickCount == ( portTickType ) 0U )\r
-               {\r
-                       xList *pxTemp;\r
-\r
-                       /* 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
+                                               /* A task being unblocked cannot cause an immediate context\r
+                                               switch if preemption is turned off. */\r
+                                               #if (  configUSE_PREEMPTION == 1 )\r
+                                               {\r
+                                                       /* Preemption is on, but a context switch should only\r
+                                                       be performed if the unblocked task has a priority that\r
+                                                       is equal to or higher than the currently executing\r
+                                                       task. */\r
+                                                       if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
+                                                       {\r
+                                                               xSwitchRequired = pdTRUE;\r
+                                                       }\r
+                                               }\r
+                                               #endif /* configUSE_PREEMPTION */\r
+                                       }\r
+                               }\r
                        }\r
-                       else\r
+               }\r
+\r
+               /* Tasks of equal priority to the currently running task will share\r
+               processing time (time slice) if preemption is on, and the application\r
+               writer has not explicitly turned time slicing off. */\r
+               #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )\r
+               {\r
+                       if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( unsigned portBASE_TYPE ) 1 )\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
+                               xSwitchRequired = pdTRUE;\r
                        }\r
                }\r
-\r
-               /* See if this tick has made a timeout expire. */\r
-               prvCheckDelayedTasks();\r
+               #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */\r
        }\r
        else\r
        {\r
-               ++uxMissedTicks;\r
+               ++uxPendedTicks;\r
 \r
                /* The tick hook gets called at regular intervals, even if the\r
                scheduler is locked. */\r
@@ -1494,15 +1704,15 @@ tskTCB * pxTCB;
        #if ( configUSE_TICK_HOOK == 1 )\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 == ( unsigned portBASE_TYPE ) 0U )\r
+               count is being unwound (when the scheduler is being unlocked). */\r
+               if( uxPendedTicks == ( unsigned portBASE_TYPE ) 0U )\r
                {\r
                        vApplicationTickHook();\r
                }\r
        }\r
-       #endif\r
+       #endif /* configUSE_TICK_HOOK */\r
 \r
-       traceTASK_INCREMENT_TICK( xTickCount );\r
+       return xSwitchRequired;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1529,7 +1739,7 @@ tskTCB * pxTCB;
                taskEXIT_CRITICAL();\r
        }\r
 \r
-#endif\r
+#endif /* configUSE_APPLICATION_TASK_TAG */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
@@ -1558,7 +1768,7 @@ tskTCB * pxTCB;
                return xReturn;\r
        }\r
 \r
-#endif\r
+#endif /* configUSE_APPLICATION_TASK_TAG */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
@@ -1590,7 +1800,7 @@ tskTCB * pxTCB;
                return xReturn;\r
        }\r
 \r
-#endif\r
+#endif /* configUSE_APPLICATION_TASK_TAG */\r
 /*-----------------------------------------------------------*/\r
 \r
 void vTaskSwitchContext( void )\r
@@ -1599,52 +1809,54 @@ void vTaskSwitchContext( void )
        {\r
                /* The scheduler is currently suspended - do not allow a context\r
                switch. */\r
-               xMissedYield = pdTRUE;\r
+               xYieldPending = pdTRUE;\r
        }\r
        else\r
        {\r
                traceTASK_SWITCHED_OUT();\r
-       \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
+                                       portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );\r
                                #else\r
-                                       ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();\r
+                                       ulTotalRunTime = 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
+                               /* Add the amount of time the task has been running to the\r
+                               accumulated     time so far.  The time the task started running was\r
+                               stored in ulTaskSwitchedInTime.  Note that there is no overflow\r
+                               protection here so count values are only valid until the timer\r
+                               overflows.  The guard against negative values is to protect\r
+                               against suspect run time stat counter implementations - which\r
+                               are provided by the application, not the kernel. */\r
+                               if( ulTotalRunTime > ulTaskSwitchedInTime )\r
+                               {\r
+                                       pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );\r
+                               }\r
+                               ulTaskSwitchedInTime = ulTotalRunTime;\r
                }\r
-               #endif\r
-       \r
+               #endif /* configGENERATE_RUN_TIME_STATS */\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
+\r
+               taskSELECT_HIGHEST_PRIORITY_TASK();\r
+\r
                traceTASK_SWITCHED_IN();\r
+\r
+               #if ( configUSE_NEWLIB_REENTRANT == 1 )\r
+               {\r
+                       /* Switch Newlib's _impure_ptr variable to point to the _reent\r
+                       structure specific to this task. */\r
+                       _impure_ptr = &( pxCurrentTCB->xNewLib_reent );\r
+               }\r
+               #endif /* configUSE_NEWLIB_REENTRANT */\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait )\r
+void vTaskPlaceOnEventList( xList * const pxEventList, portTickType xTicksToWait )\r
 {\r
 portTickType xTimeToWake;\r
 \r
@@ -1656,13 +1868,17 @@ portTickType xTimeToWake;
        /* Place the event list item of the TCB in the appropriate event list.\r
        This is placed in the list in priority order so the highest priority task\r
        is the first to be woken by the event. */\r
-       vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );\r
+       vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );\r
 \r
        /* We must remove ourselves from the ready list before adding ourselves\r
        to the blocked list as the same list item is used for both lists.  We have\r
        exclusive access to the ready lists as the scheduler is locked. */\r
-       vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
-\r
+       if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( unsigned portBASE_TYPE ) 0 )\r
+       {\r
+               /* The current task must be in a ready list, so there is no need to\r
+               check, and the port reset macro can be called directly. */\r
+               portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
+       }\r
 \r
        #if ( INCLUDE_vTaskSuspend == 1 )\r
        {\r
@@ -1671,7 +1887,7 @@ portTickType xTimeToWake;
                        /* Add ourselves to the suspended task list instead of a delayed task\r
                        list to ensure we are not woken by a timing event.  We will block\r
                        indefinitely. */\r
-                       vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
+                       vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );\r
                }\r
                else\r
                {\r
@@ -1681,20 +1897,20 @@ portTickType xTimeToWake;
                        prvAddCurrentTaskToDelayedList( xTimeToWake );\r
                }\r
        }\r
-       #else\r
+       #else /* INCLUDE_vTaskSuspend */\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
-       #endif\r
+       #endif /* INCLUDE_vTaskSuspend */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 #if configUSE_TIMERS == 1\r
 \r
-       void vTaskPlaceOnEventListRestricted( const xList * const pxEventList, portTickType xTicksToWait )\r
+       void vTaskPlaceOnEventListRestricted( xList * const pxEventList, portTickType xTicksToWait )\r
        {\r
        portTickType xTimeToWake;\r
 \r
@@ -1705,24 +1921,31 @@ portTickType xTimeToWake;
                designed for use by kernel code, and has special calling requirements -\r
                it should be called from a critical section. */\r
 \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
+               vListInsertEnd( pxEventList, &( 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
+               if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( unsigned portBASE_TYPE ) 0 )\r
+               {\r
+                       /* The current task must be in a ready list, so there is no need to\r
+                       check, and the port reset macro can be called directly. */\r
+                       portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
+               }\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
+\r
+               traceTASK_DELAY_UNTIL();\r
                prvAddCurrentTaskToDelayedList( xTimeToWake );\r
        }\r
-       \r
+\r
 #endif /* configUSE_TIMERS */\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1741,23 +1964,23 @@ portBASE_TYPE xReturn;
        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
-       \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
+       ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );\r
 \r
        if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
        {\r
-               vListRemove( &( pxUnblockedTCB->xGenericListItem ) );\r
-               prvAddTaskToReadyQueue( pxUnblockedTCB );\r
+               ( void ) uxListRemove( &( pxUnblockedTCB->xGenericListItem ) );\r
+               prvAddTaskToReadyList( pxUnblockedTCB );\r
        }\r
        else\r
        {\r
                /* We cannot access the delayed or ready lists, so will hold this\r
                task pending until the scheduler is resumed. */\r
-               vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );\r
+               vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );\r
        }\r
 \r
        if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
@@ -1794,6 +2017,9 @@ portBASE_TYPE xReturn;
 \r
        taskENTER_CRITICAL();\r
        {\r
+               /* Minor optimisation.  The tick count cannot change in this block. */\r
+               const portTickType xConstTickCount = xTickCount;\r
+\r
                #if ( INCLUDE_vTaskSuspend == 1 )\r
                        /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is\r
                        the maximum block time then the task should block indefinitely, and\r
@@ -1805,7 +2031,7 @@ portBASE_TYPE xReturn;
                        else /* We are not blocking indefinitely, perform the checks below. */\r
                #endif\r
 \r
-               if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( ( portTickType ) xTickCount >= ( portTickType ) pxTimeOut->xTimeOnEntering ) )\r
+               if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */\r
                {\r
                        /* The tick count is greater than the time at which vTaskSetTimeout()\r
                        was called, but has also overflowed since vTaskSetTimeOut() was called.\r
@@ -1813,10 +2039,10 @@ portBASE_TYPE xReturn;
                        passed since vTaskSetTimeout() was called. */\r
                        xReturn = pdTRUE;\r
                }\r
-               else if( ( ( portTickType ) ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) ) < ( portTickType ) *pxTicksToWait )\r
+               else if( ( xConstTickCount - pxTimeOut->xTimeOnEntering ) < *pxTicksToWait )\r
                {\r
                        /* Not a genuine timeout. Adjust parameters for time remaining. */\r
-                       *pxTicksToWait -= ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering );\r
+                       *pxTicksToWait -= ( xConstTickCount -  pxTimeOut->xTimeOnEntering );\r
                        vTaskSetTimeOutState( pxTimeOut );\r
                        xReturn = pdFALSE;\r
                }\r
@@ -1833,16 +2059,17 @@ portBASE_TYPE xReturn;
 \r
 void vTaskMissedYield( void )\r
 {\r
-       xMissedYield = pdTRUE;\r
+       xYieldPending = pdTRUE;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
+\r
        unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask )\r
        {\r
        unsigned portBASE_TYPE uxReturn;\r
        tskTCB *pxTCB;\r
-       \r
+\r
                if( xTask != NULL )\r
                {\r
                        pxTCB = ( tskTCB * ) xTask;\r
@@ -1852,25 +2079,27 @@ void vTaskMissedYield( void )
                {\r
                        uxReturn = 0U;\r
                }\r
-               \r
+\r
                return uxReturn;\r
        }\r
-#endif\r
+\r
+#endif /* configUSE_TRACE_FACILITY */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
+\r
        void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle )\r
        {\r
        tskTCB *pxTCB;\r
-       \r
+\r
                if( xTask != NULL )\r
                {\r
                        pxTCB = ( tskTCB * ) xTask;\r
                        pxTCB->uxTaskNumber = uxHandle;\r
                }\r
        }\r
-#endif\r
 \r
+#endif /* configUSE_TRACE_FACILITY */\r
 \r
 /*\r
  * -----------------------------------------------------------\r
@@ -1901,7 +2130,7 @@ static portTASK_FUNCTION( prvIdleTask, pvParameters )
                        will automatically get the processor anyway. */\r
                        taskYIELD();\r
                }\r
-               #endif\r
+               #endif /* configUSE_PREEMPTION */\r
 \r
                #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )\r
                {\r
@@ -1919,7 +2148,7 @@ static portTASK_FUNCTION( prvIdleTask, pvParameters )
                                taskYIELD();\r
                        }\r
                }\r
-               #endif\r
+               #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */\r
 \r
                #if ( configUSE_IDLE_HOOK == 1 )\r
                {\r
@@ -1932,38 +2161,113 @@ static portTASK_FUNCTION( prvIdleTask, pvParameters )
                        CALL A FUNCTION THAT MIGHT BLOCK. */\r
                        vApplicationIdleHook();\r
                }\r
-               #endif\r
-       }\r
-} /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */\r
-\r
+               #endif /* configUSE_IDLE_HOOK */\r
 \r
+               /* This conditional compilation should use inequality to 0, not equality\r
+               to 1.  This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when\r
+               user defined low power mode     implementations require\r
+               configUSE_TICKLESS_IDLE to be set to a value other than 1. */\r
+               #if ( configUSE_TICKLESS_IDLE != 0 )\r
+               {\r
+               portTickType xExpectedIdleTime;\r
 \r
+                       /* It is not desirable to suspend then resume the scheduler on\r
+                       each iteration of the idle task.  Therefore, a preliminary\r
+                       test of the expected idle time is performed without the\r
+                       scheduler suspended.  The result here is not necessarily\r
+                       valid. */\r
+                       xExpectedIdleTime = prvGetExpectedIdleTime();\r
 \r
+                       if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )\r
+                       {\r
+                               vTaskSuspendAll();\r
+                               {\r
+                                       /* Now the scheduler is suspended, the expected idle\r
+                                       time can be sampled again, and this time its value can\r
+                                       be used. */\r
+                                       configASSERT( xNextTaskUnblockTime >= xTickCount );\r
+                                       xExpectedIdleTime = prvGetExpectedIdleTime();\r
 \r
+                                       if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )\r
+                                       {\r
+                                               portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );\r
+                                       }\r
+                               }\r
+                               xTaskResumeAll();\r
+                       }\r
+               }\r
+               #endif /* configUSE_TICKLESS_IDLE */\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
 \r
+#if configUSE_TICKLESS_IDLE != 0\r
 \r
-/*-----------------------------------------------------------\r
- * File private functions documented at the top of the file.\r
- *----------------------------------------------------------*/\r
+       eSleepModeStatus eTaskConfirmSleepModeStatus( void )\r
+       {\r
+       eSleepModeStatus eReturn = eStandardSleep;\r
 \r
+               if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )\r
+               {\r
+                       /* A task was made ready while the scheduler was suspended. */\r
+                       eReturn = eAbortSleep;\r
+               }\r
+               else if( xYieldPending != pdFALSE )\r
+               {\r
+                       /* A yield was pended while the scheduler was suspended. */\r
+                       eReturn = eAbortSleep;\r
+               }\r
+               else\r
+               {\r
+                       #if configUSE_TIMERS == 0\r
+                       {\r
+                               /* The idle task exists in addition to the application tasks. */\r
+                               const unsigned portBASE_TYPE uxNonApplicationTasks = 1;\r
+\r
+                               /* If timers are not being used and all the tasks are in the\r
+                               suspended list (which might mean they have an infinite block\r
+                               time rather than actually being suspended) then it is safe to\r
+                               turn all clocks off and just wait for external interrupts. */\r
+                               if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )\r
+                               {\r
+                                       eReturn = eNoTasksWaitingTimeout;\r
+                               }\r
+                       }\r
+                       #endif /* configUSE_TIMERS */\r
+               }\r
 \r
+               return eReturn;\r
+       }\r
+#endif /* configUSE_TICKLESS_IDLE */\r
+/*-----------------------------------------------------------*/\r
 \r
 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )\r
 {\r
-       /* Store the function name in the TCB. */\r
-       #if configMAX_TASK_NAME_LEN > 1\r
+unsigned portBASE_TYPE x;\r
+\r
+       /* Store the task name in the TCB. */\r
+       for( x = ( unsigned portBASE_TYPE ) 0; x < ( unsigned portBASE_TYPE ) configMAX_TASK_NAME_LEN; x++ )\r
        {\r
-               /* Don't bring strncpy into the build unnecessarily. */\r
-               strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );\r
+               pxTCB->pcTaskName[ x ] = pcName[ x ];\r
+\r
+               /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than\r
+               configMAX_TASK_NAME_LEN characters just in case the memory after the\r
+               string is not accessible (extremely unlikely). */\r
+               if( pcName[ x ] == 0x00 )\r
+               {\r
+                       break;\r
+               }\r
        }\r
-       #endif\r
-       pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = ( signed char ) '\0';\r
+\r
+       /* Ensure the name string is terminated in the case that the string length\r
+       was greater or equal to configMAX_TASK_NAME_LEN. */\r
+       pxTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 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
+       if( uxPriority >= ( unsigned portBASE_TYPE ) configMAX_PRIORITIES )\r
        {\r
-               uxPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
+               uxPriority = ( unsigned portBASE_TYPE ) configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
        }\r
 \r
        pxTCB->uxPriority = uxPriority;\r
@@ -1971,7 +2275,7 @@ static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const
        {\r
                pxTCB->uxBasePriority = uxPriority;\r
        }\r
-       #endif\r
+       #endif /* configUSE_MUTEXES */\r
 \r
        vListInitialiseItem( &( pxTCB->xGenericListItem ) );\r
        vListInitialiseItem( &( pxTCB->xEventListItem ) );\r
@@ -1981,37 +2285,44 @@ static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const
        listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );\r
 \r
        /* Event lists are always in priority order. */\r
-       listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );\r
+       listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( portTickType ) configMAX_PRIORITIES - ( portTickType ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
        listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );\r
 \r
        #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
        {\r
                pxTCB->uxCriticalNesting = ( unsigned portBASE_TYPE ) 0U;\r
        }\r
-       #endif\r
+       #endif /* portCRITICAL_NESTING_IN_TCB */\r
 \r
        #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
        {\r
                pxTCB->pxTaskTag = NULL;\r
        }\r
-       #endif\r
+       #endif /* configUSE_APPLICATION_TASK_TAG */\r
 \r
        #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
        {\r
                pxTCB->ulRunTimeCounter = 0UL;\r
        }\r
-       #endif\r
+       #endif /* configGENERATE_RUN_TIME_STATS */\r
 \r
        #if ( portUSING_MPU_WRAPPERS == 1 )\r
        {\r
                vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );\r
        }\r
-       #else\r
+       #else /* portUSING_MPU_WRAPPERS */\r
        {\r
                ( void ) xRegions;\r
                ( void ) usStackDepth;\r
        }\r
-       #endif\r
+       #endif /* portUSING_MPU_WRAPPERS */\r
+\r
+       #if ( configUSE_NEWLIB_REENTRANT == 1 )\r
+       {\r
+               /* Initialise this task's Newlib reent structure. */\r
+               _REENT_INIT_PTR( ( &( pxTCB->xNewLib_reent ) ) );\r
+       }\r
+       #endif /* configUSE_NEWLIB_REENTRANT */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -2020,7 +2331,7 @@ static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const
        void vTaskAllocateMPURegions( xTaskHandle xTaskToModify, const xMemoryRegion * const xRegions )\r
        {\r
        tskTCB *pxTCB;\r
-       \r
+\r
                if( xTaskToModify == pxCurrentTCB )\r
                {\r
                        xTaskToModify = NULL;\r
@@ -2031,33 +2342,34 @@ static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const
 \r
         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );\r
        }\r
-       /*-----------------------------------------------------------*/\r
-#endif\r
+\r
+#endif /* portUSING_MPU_WRAPPERS */\r
+/*-----------------------------------------------------------*/\r
 \r
 static void prvInitialiseTaskLists( void )\r
 {\r
 unsigned portBASE_TYPE uxPriority;\r
 \r
-       for( uxPriority = ( unsigned portBASE_TYPE ) 0U; uxPriority < configMAX_PRIORITIES; uxPriority++ )\r
+       for( uxPriority = ( unsigned portBASE_TYPE ) 0U; uxPriority < ( unsigned portBASE_TYPE ) configMAX_PRIORITIES; uxPriority++ )\r
        {\r
-               vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );\r
+               vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );\r
        }\r
 \r
-       vListInitialise( ( xList * ) &xDelayedTaskList1 );\r
-       vListInitialise( ( xList * ) &xDelayedTaskList2 );\r
-       vListInitialise( ( xList * ) &xPendingReadyList );\r
+       vListInitialise( &xDelayedTaskList1 );\r
+       vListInitialise( &xDelayedTaskList2 );\r
+       vListInitialise( &xPendingReadyList );\r
 \r
        #if ( INCLUDE_vTaskDelete == 1 )\r
        {\r
-               vListInitialise( ( xList * ) &xTasksWaitingTermination );\r
+               vListInitialise( &xTasksWaitingTermination );\r
        }\r
-       #endif\r
+       #endif /* INCLUDE_vTaskDelete */\r
 \r
        #if ( INCLUDE_vTaskSuspend == 1 )\r
        {\r
-               vListInitialise( ( xList * ) &xSuspendedTaskList );\r
+               vListInitialise( &xSuspendedTaskList );\r
        }\r
-       #endif\r
+       #endif /* INCLUDE_vTaskSuspend */\r
 \r
        /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList\r
        using list2. */\r
@@ -2074,11 +2386,11 @@ static void prvCheckTasksWaitingTermination( void )
 \r
                /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called\r
                too often in the idle task. */\r
-               if( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0U )\r
+               while( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0U )\r
                {\r
                        vTaskSuspendAll();\r
                                xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );\r
-                       xTaskResumeAll();\r
+                       ( void ) xTaskResumeAll();\r
 \r
                        if( xListIsEmpty == pdFALSE )\r
                        {\r
@@ -2086,8 +2398,8 @@ static void prvCheckTasksWaitingTermination( void )
 \r
                                taskENTER_CRITICAL();\r
                                {\r
-                                       pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );\r
-                                       vListRemove( &( pxTCB->xGenericListItem ) );\r
+                                       pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );\r
+                                       ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );\r
                                        --uxCurrentNumberOfTasks;\r
                                        --uxTasksDeleted;\r
                                }\r
@@ -2097,7 +2409,7 @@ static void prvCheckTasksWaitingTermination( void )
                        }\r
                }\r
        }\r
-       #endif\r
+       #endif /* vTaskDelete */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -2109,12 +2421,12 @@ static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake )
        if( xTimeToWake < xTickCount )\r
        {\r
                /* Wake time has overflowed.  Place this item in the overflow list. */\r
-               vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
+               vListInsert( pxOverflowDelayedTaskList, &( 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
+               vListInsert( pxDelayedTaskList, &( 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
@@ -2140,7 +2452,7 @@ tskTCB *pxNewTCB;
                /* Allocate space for the stack used by the task being created.\r
                The base of the stack memory stored in the TCB so the task can\r
                be deleted later if required. */\r
-               pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );\r
+               pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t ) usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
 \r
                if( pxNewTCB->pxStack == NULL )\r
                {\r
@@ -2151,7 +2463,7 @@ tskTCB *pxNewTCB;
                else\r
                {\r
                        /* Just to help debugging. */\r
-                       memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( portSTACK_TYPE ) );\r
+                       ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( portSTACK_TYPE ) );\r
                }\r
        }\r
 \r
@@ -2161,110 +2473,73 @@ tskTCB *pxNewTCB;
 \r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus )\r
+       static unsigned portBASE_TYPE prvListTaskWithinSingleList( xTaskStatusType *pxTaskStatusArray, xList *pxList, eTaskState eState )\r
        {\r
        volatile tskTCB *pxNextTCB, *pxFirstTCB;\r
-       unsigned short usStackRemaining;\r
-       PRIVILEGED_DATA static char pcStatusString[ configMAX_TASK_NAME_LEN + 30 ];\r
+       unsigned portBASE_TYPE uxTask = 0;\r
 \r
-               /* Write the details of all the TCB's in pxList into the buffer. */\r
-               listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );\r
-               do\r
+               if( listCURRENT_LIST_LENGTH( pxList ) > ( unsigned portBASE_TYPE ) 0 )\r
                {\r
-                       listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\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
-               } while( pxNextTCB != pxFirstTCB );\r
-       }\r
+                       listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );\r
 \r
-#endif\r
-/*-----------------------------------------------------------*/\r
-\r
-#if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+                       /* Populate an xTaskStatusType structure within the\r
+                       pxTaskStatusArray array for each task that is referenced from\r
+                       pxList.  See the definition of xTaskStatusType in task.h for the\r
+                       meaning of each xTaskStatusType structure member. */\r
+                       do\r
+                       {\r
+                               listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
 \r
-       static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime )\r
-       {\r
-       volatile tskTCB *pxNextTCB, *pxFirstTCB;\r
-       unsigned long ulStatsAsPercentage;\r
+                               pxTaskStatusArray[ uxTask ].xHandle = ( xTaskHandle ) pxNextTCB;\r
+                               pxTaskStatusArray[ uxTask ].pcTaskName = ( const signed char * ) &( pxNextTCB->pcTaskName [ 0 ] );\r
+                               pxTaskStatusArray[ uxTask ].xTaskNumber = pxNextTCB->uxTCBNumber;\r
+                               pxTaskStatusArray[ uxTask ].eCurrentState = eState;\r
+                               pxTaskStatusArray[ uxTask ].uxCurrentPriority = pxNextTCB->uxPriority;\r
 \r
-               /* Write the run time stats of all the TCB's in pxList into the buffer. */\r
-               listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );\r
-               do\r
-               {\r
-                       /* Get next TCB in from the list. */\r
-                       listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
+                               #if ( configUSE_MUTEXES == 1 )\r
+                               {\r
+                                       pxTaskStatusArray[ uxTask ].uxBasePriority = pxNextTCB->uxBasePriority;\r
+                               }\r
+                               #else\r
+                               {\r
+                                       pxTaskStatusArray[ uxTask ].uxBasePriority = 0;\r
+                               }\r
+                               #endif\r
 \r
-                       /* Divide by zero check. */\r
-                       if( ulTotalRunTime > 0UL )\r
-                       {\r
-                               /* Has the task run at all? */\r
-                               if( pxNextTCB->ulRunTimeCounter == 0UL )\r
+                               #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
                                {\r
-                                       /* The task has used no CPU time at all. */\r
-                                       sprintf( pcStatsString, ( char * ) "%s\t\t0\t\t0%%\r\n", pxNextTCB->pcTaskName );\r
+                                       pxTaskStatusArray[ uxTask ].ulRunTimeCounter = pxNextTCB->ulRunTimeCounter;\r
                                }\r
-                               else\r
+                               #else\r
                                {\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
+                                       pxTaskStatusArray[ uxTask ].ulRunTimeCounter = 0;\r
+                               }\r
+                               #endif\r
 \r
-                                       if( ulStatsAsPercentage > 0UL )\r
-                                       {\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
-                                               #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
+                               #if ( portSTACK_GROWTH > 0 )\r
+                               {\r
+                                       ppxTaskStatusArray[ uxTask ].usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxEndOfStack );\r
                                }\r
+                               #else\r
+                               {\r
+                                       pxTaskStatusArray[ uxTask ].usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );\r
+                               }\r
+                               #endif\r
 \r
-                               strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatsString );\r
-                       }\r
+                               uxTask++;\r
+\r
+                       } while( pxNextTCB != pxFirstTCB );\r
+               }\r
 \r
-               } while( pxNextTCB != pxFirstTCB );\r
+               return uxTask;\r
        }\r
 \r
-#endif\r
+#endif /* configUSE_TRACE_FACILITY */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
 \r
-       static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte )\r
+       static unsigned short prvTaskCheckFreeStackSpace( const unsigned char * pucStackByte )\r
        {\r
        register unsigned short usCount = 0U;\r
 \r
@@ -2279,7 +2554,7 @@ tskTCB *pxNewTCB;
                return usCount;\r
        }\r
 \r
-#endif\r
+#endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )\r
@@ -2302,12 +2577,12 @@ tskTCB *pxNewTCB;
                }\r
                #endif\r
 \r
-               uxReturn = ( unsigned portBASE_TYPE ) usTaskCheckFreeStackSpace( pcEndOfStack );\r
+               uxReturn = ( unsigned portBASE_TYPE ) prvTaskCheckFreeStackSpace( pcEndOfStack );\r
 \r
                return uxReturn;\r
        }\r
 \r
-#endif\r
+#endif /* INCLUDE_uxTaskGetStackHighWaterMark */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( INCLUDE_vTaskDelete == 1 )\r
@@ -2325,9 +2600,7 @@ tskTCB *pxNewTCB;
                vPortFree( pxTCB );\r
        }\r
 \r
-#endif\r
-\r
-\r
+#endif /* INCLUDE_vTaskDelete */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )\r
@@ -2344,8 +2617,7 @@ tskTCB *pxNewTCB;
                return xReturn;\r
        }\r
 \r
-#endif\r
-\r
+#endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
@@ -2373,48 +2645,54 @@ tskTCB *pxNewTCB;
                return xReturn;\r
        }\r
 \r
-#endif\r
+#endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_MUTEXES == 1 )\r
 \r
-       void vTaskPriorityInherit( xTaskHandle const pxMutexHolder )\r
+       void vTaskPriorityInherit( xTaskHandle const pxMutexHolder )\r
        {\r
        tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;\r
 \r
-               configASSERT( pxMutexHolder );\r
-\r
-               if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )\r
+               /* If the mutex was given back by an interrupt while the queue was\r
+               locked then the mutex holder might now be NULL. */\r
+               if( pxMutexHolder != NULL )\r
                {\r
-                       /* Adjust the mutex holder state to account for its new priority. */\r
-                       listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority );\r
-\r
-                       /* If the task being modified is in the ready state it will need to\r
-                       be moved in to a new list. */\r
-                       if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
+                       if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )\r
                        {\r
-                               vListRemove( &( pxTCB->xGenericListItem ) );\r
+                               /* Adjust the mutex holder state to account for its new priority. */\r
+                               listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( portTickType ) configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
 \r
-                               /* Inherit the priority before being moved into the new list. */\r
-                               pxTCB->uxPriority = pxCurrentTCB->uxPriority;\r
-                               prvAddTaskToReadyQueue( pxTCB );\r
-                       }\r
-                       else\r
-                       {\r
-                               /* Just inherit the priority. */\r
-                               pxTCB->uxPriority = pxCurrentTCB->uxPriority;\r
-                       }\r
+                               /* If the task being modified is in the ready state it will need to\r
+                               be moved into a new list. */\r
+                               if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
+                               {\r
+                                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( unsigned portBASE_TYPE ) 0 )\r
+                                       {\r
+                                               taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
+                                       }\r
+\r
+                                       /* Inherit the priority before being moved into the new list. */\r
+                                       pxTCB->uxPriority = pxCurrentTCB->uxPriority;\r
+                                       prvAddTaskToReadyList( pxTCB );\r
+                               }\r
+                               else\r
+                               {\r
+                                       /* Just inherit the priority. */\r
+                                       pxTCB->uxPriority = pxCurrentTCB->uxPriority;\r
+                               }\r
 \r
-                       traceTASK_PRIORITY_INHERIT( pxTCB, pxCurrentTCB->uxPriority );\r
+                               traceTASK_PRIORITY_INHERIT( pxTCB, pxCurrentTCB->uxPriority );\r
+                       }\r
                }\r
        }\r
 \r
-#endif\r
+#endif /* configUSE_MUTEXES */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_MUTEXES == 1 )\r
 \r
-       void vTaskPriorityDisinherit( xTaskHandle const pxMutexHolder )\r
+       void vTaskPriorityDisinherit( xTaskHandle const pxMutexHolder )\r
        {\r
        tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;\r
 \r
@@ -2424,19 +2702,22 @@ tskTCB *pxNewTCB;
                        {\r
                                /* We must be the running task to be able to give the mutex back.\r
                                Remove ourselves from the ready list we currently appear in. */\r
-                               vListRemove( &( pxTCB->xGenericListItem ) );\r
+                               if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( unsigned portBASE_TYPE ) 0 )\r
+                               {\r
+                                       taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
+                               }\r
 \r
                                /* Disinherit the priority before adding the task into the new\r
                                ready list. */\r
                                traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );\r
                                pxTCB->uxPriority = pxTCB->uxBasePriority;\r
-                               listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority );\r
-                               prvAddTaskToReadyQueue( pxTCB );\r
+                               listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( portTickType ) configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+                               prvAddTaskToReadyList( pxTCB );\r
                        }\r
                }\r
        }\r
 \r
-#endif\r
+#endif /* configUSE_MUTEXES */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
@@ -2451,30 +2732,216 @@ tskTCB *pxNewTCB;
                }\r
        }\r
 \r
-#endif\r
+#endif /* portCRITICAL_NESTING_IN_TCB */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
 \r
-void vTaskExitCritical( void )\r
-{\r
-       if( xSchedulerRunning != pdFALSE )\r
+       void vTaskExitCritical( void )\r
        {\r
-               if( pxCurrentTCB->uxCriticalNesting > 0U )\r
+               if( xSchedulerRunning != pdFALSE )\r
                {\r
-                       ( pxCurrentTCB->uxCriticalNesting )--;\r
-\r
-                       if( pxCurrentTCB->uxCriticalNesting == 0U )\r
+                       if( pxCurrentTCB->uxCriticalNesting > 0U )\r
                        {\r
-                               portENABLE_INTERRUPTS();\r
+                               ( pxCurrentTCB->uxCriticalNesting )--;\r
+\r
+                               if( pxCurrentTCB->uxCriticalNesting == 0U )\r
+                               {\r
+                                       portENABLE_INTERRUPTS();\r
+                               }\r
                        }\r
                }\r
        }\r
-}\r
 \r
-#endif\r
+#endif /* portCRITICAL_NESTING_IN_TCB */\r
 /*-----------------------------------------------------------*/\r
 \r
+#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configINCLUDE_STATS_FORMATTING_FUNCTIONS == 1 ) )\r
+\r
+       void vTaskList( signed char *pcWriteBuffer )\r
+       {\r
+       xTaskStatusType *pxTaskStatusArray;\r
+       volatile unsigned portBASE_TYPE uxArraySize, x;\r
+       char cStatus;\r
+\r
+               /*\r
+                * PLEASE NOTE:\r
+                *\r
+                * This function is provided for convenience only, and is used by many\r
+                * of the demo applications.  Do not consider it to be part of the\r
+                * scheduler.\r
+                *\r
+                * vTaskList() calls xTaskGetSystemState(), then formats part of the\r
+                * xTaskGetSystemState() output into a human readable table that\r
+                * displays task names, states and stack usage.\r
+                *\r
+                * vTaskList() has a dependency on the sprintf() C library function that\r
+                * might bloat the code size, use a lot of stack, and provide different\r
+                * results on different platforms.  An alternative, tiny, third party,\r
+                * and limited functionality implementation of sprintf() is provided in\r
+                * many of the FreeRTOS/Demo sub-directories in a file called\r
+                * printf-stdarg.c (note printf-stdarg.c does not provide a full\r
+                * snprintf() implementation!).\r
+                *\r
+                * It is recommended that production systems call xTaskGetSystemState()\r
+                * directly to get access to raw stats data, rather than indirectly\r
+                * through a call to vTaskList().\r
+                */\r
+\r
+\r
+               /* Make sure the write buffer does not contain a string. */\r
+               *pcWriteBuffer = 0x00;\r
+\r
+               /* Take a snapshot of the number of tasks in case it changes while this\r
+               function is executing. */\r
+               uxArraySize = uxCurrentNumberOfTasks;\r
+\r
+               /* Allocate an array index for each task. */\r
+               pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( xTaskStatusType ) );\r
+\r
+               if( pxTaskStatusArray != NULL )\r
+               {\r
+                       /* Generate the (binary) data. */\r
+                       uxArraySize = xTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );\r
+\r
+                       /* Create a human readable table from the binary data. */\r
+                       for( x = 0; x < uxArraySize; x++ )\r
+                       {\r
+                               switch( pxTaskStatusArray[ x ].eCurrentState )\r
+                               {\r
+                               case eReady:            cStatus = tskREADY_CHAR;\r
+                                                                       break;\r
+\r
+                               case eBlocked:          cStatus = tskBLOCKED_CHAR;\r
+                                                                       break;\r
+\r
+                               case eSuspended:        cStatus = tskSUSPENDED_CHAR;\r
+                                                                       break;\r
+\r
+                               case eDeleted:          cStatus = tskDELETED_CHAR;\r
+                                                                       break;\r
+\r
+                               default:                        /* Should not get here, but it is included\r
+                                                                       to prevent static checking errors. */\r
+                                                                       cStatus = 0x00;\r
+                                                                       break;\r
+                               }\r
+\r
+                               sprintf( ( char * ) pcWriteBuffer, ( char * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxTaskStatusArray[ x ].pcTaskName, cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );\r
+                               pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );\r
+                       }\r
+\r
+                       /* Free the array again. */\r
+                       vPortFree( pxTaskStatusArray );\r
+               }\r
+       }\r
+\r
+#endif /* configUSE_TRACE_FACILITY */\r
+/*----------------------------------------------------------*/\r
+\r
+#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configINCLUDE_STATS_FORMATTING_FUNCTIONS == 1 ) )\r
+\r
+       void vTaskGetRunTimeStats( signed char *pcWriteBuffer )\r
+       {\r
+       xTaskStatusType *pxTaskStatusArray;\r
+       volatile unsigned portBASE_TYPE uxArraySize, x;\r
+       unsigned long ulTotalTime, ulStatsAsPercentage;\r
+\r
+               /*\r
+                * PLEASE NOTE:\r
+                *\r
+                * This function is provided for convenience only, and is used by many\r
+                * of the demo applications.  Do not consider it to be part of the\r
+                * scheduler.\r
+                *\r
+                * vTaskGetRunTimeStats() calls xTaskGetSystemState(), then formats part\r
+                * of the xTaskGetSystemState() output into a human readable table that\r
+                * displays the amount of time each task has spent in the Running state\r
+                * in both absolute and percentage terms.\r
+                *\r
+                * vTaskGetRunTimeStats() has a dependency on the sprintf() C library\r
+                * function that might bloat the code size, use a lot of stack, and\r
+                * provide different results on different platforms.  An alternative,\r
+                * tiny, third party, and limited functionality implementation of\r
+                * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in\r
+                * a file called printf-stdarg.c (note printf-stdarg.c does not provide\r
+                * a full snprintf() implementation!).\r
+                *\r
+                * It is recommended that production systems call xTaskGetSystemState()\r
+                * directly to get access to raw stats data, rather than indirectly\r
+                * through a call to vTaskGetRunTimeStats().\r
+                */\r
+\r
+               /* Make sure the write buffer does not contain a string. */\r
+               *pcWriteBuffer = 0x00;\r
+\r
+               /* Take a snapshot of the number of tasks in case it changes while this\r
+               function is executing. */\r
+               uxArraySize = uxCurrentNumberOfTasks;\r
+\r
+               /* Allocate an array index for each task. */\r
+               pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( xTaskStatusType ) );\r
+\r
+               if( pxTaskStatusArray != NULL )\r
+               {\r
+                       /* Generate the (binary) data. */\r
+                       uxArraySize = xTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );\r
+\r
+                       /* For percentage calculations. */\r
+                       ulTotalTime /= 100UL;\r
+\r
+                       /* Avoid divide by zero errors. */\r
+                       if( ulTotalTime > 0 )\r
+                       {\r
+                               /* Create a human readable table from the binary data. */\r
+                               for( x = 0; x < uxArraySize; x++ )\r
+                               {\r
+                                       /* What percentage of the total run time has the task used?\r
+                                       This will always be rounded down to the nearest integer.\r
+                                       ulTotalRunTimeDiv100 has already been divided by 100. */\r
+                                       ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;\r
+\r
+                                       if( ulStatsAsPercentage > 0UL )\r
+                                       {\r
+                                               #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
+                                               {\r
+                                                       sprintf( ( char * ) pcWriteBuffer, ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].pcTaskName, pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );\r
+                                               }\r
+                                               #else\r
+                                               {\r
+                                                       /* sizeof( int ) == sizeof( long ) so a smaller\r
+                                                       printf() library can be used. */\r
+                                                       sprintf( ( char * ) pcWriteBuffer, ( char * ) "%s\t\t%u\t\t%u%%\r\n", pxTaskStatusArray[ x ].pcTaskName, ( unsigned int ) pxTaskStatusArray[ x ].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
+                                               #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
+                                               {\r
+                                                       sprintf( ( char * ) pcWriteBuffer, ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].pcTaskName, pxTaskStatusArray[ x ].ulRunTimeCounter );\r
+                                               }\r
+                                               #else\r
+                                               {\r
+                                                       /* sizeof( int ) == sizeof( long ) so a smaller\r
+                                                       printf() library can be used. */\r
+                                                       sprintf( ( char * ) pcWriteBuffer, ( char * ) "%s\t\t%u\t\t<1%%\r\n", pxTaskStatusArray[ x ].pcTaskName, ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );\r
+                                               }\r
+                                               #endif\r
+                                       }\r
+\r
+                                       pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );\r
+                               }\r
+                       }\r
+\r
+                       /* Free the array again. */\r
+                       vPortFree( pxTaskStatusArray );\r
+               }\r
+       }\r
+\r
+#endif /* configGENERATE_RUN_TIME_STATS */\r
 \r
 \r
 \r