OpenCores
URL https://opencores.org/ocsvn/openfire2/openfire2/trunk

Subversion Repositories openfire2

[/] [openfire2/] [trunk/] [sw/] [freertos/] [tasks.c] - Diff between revs 4 and 6

Only display areas with differences | Details | Blame | View Log

Rev 4 Rev 6
/*
/*
        FreeRTOS.org V4.2.0 - Copyright (C) 2003-2007 Richard Barry.
        FreeRTOS.org V4.2.0 - Copyright (C) 2003-2007 Richard Barry.
 
 
        This file is part of the FreeRTOS.org distribution.
        This file is part of the FreeRTOS.org distribution.
 
 
        FreeRTOS.org is free software; you can redistribute it and/or modify
        FreeRTOS.org is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.
        (at your option) any later version.
 
 
        FreeRTOS.org is distributed in the hope that it will be useful,
        FreeRTOS.org is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
        GNU General Public License for more details.
 
 
        You should have received a copy of the GNU General Public License
        You should have received a copy of the GNU General Public License
        along with FreeRTOS.org; if not, write to the Free Software
        along with FreeRTOS.org; if not, write to the Free Software
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
        A special exception to the GPL can be applied should you wish to distribute
        A special exception to the GPL can be applied should you wish to distribute
        a combined work that includes FreeRTOS.org, without being obliged to provide
        a combined work that includes FreeRTOS.org, without being obliged to provide
        the source code for any proprietary components.  See the licensing section
        the source code for any proprietary components.  See the licensing section
        of http://www.FreeRTOS.org for full details of how and when the exception
        of http://www.FreeRTOS.org for full details of how and when the exception
        can be applied.
        can be applied.
 
 
        ***************************************************************************
        ***************************************************************************
        See http://www.FreeRTOS.org for documentation, latest information, license
        See http://www.FreeRTOS.org for documentation, latest information, license
        and contact details.  Please ensure to read the configuration and relevant
        and contact details.  Please ensure to read the configuration and relevant
        port sections of the online documentation.
        port sections of the online documentation.
        ***************************************************************************
        ***************************************************************************
*/
*/
 
 
/*
/*
Changes from V1.00:
Changes from V1.00:
 
 
        + Call to portRESTORE_CONTEXT has been removed.  The first context
        + Call to portRESTORE_CONTEXT has been removed.  The first context
          switch is now performed within sPortStartScheduler().
          switch is now performed within sPortStartScheduler().
 
 
Changes from V1.01:
Changes from V1.01:
 
 
        + More use of 8bit data types.
        + More use of 8bit data types.
        + Function name prefixes changed where the data type returned has changed.
        + Function name prefixes changed where the data type returned has changed.
        + configUSE_TRACE_FACILITY is no longer defined by default.
        + configUSE_TRACE_FACILITY is no longer defined by default.
 
 
Changes from V1.2.0
Changes from V1.2.0
 
 
        + Introduced ucTopReadyPriority.  This tracks the highest priority ready
        + Introduced ucTopReadyPriority.  This tracks the highest priority ready
          queue that contains a valid TCB and thus makes the context switch
          queue that contains a valid TCB and thus makes the context switch
          slightly faster.
          slightly faster.
 
 
        + prvAddTaskToReadyQueue() has been made a macro.
        + prvAddTaskToReadyQueue() has been made a macro.
 
 
Changes from V1.2.6
Changes from V1.2.6
 
 
        + Added conditional compilation directives.
        + Added conditional compilation directives.
        + Extended API.
        + Extended API.
        + Rearranged function order.
        + Rearranged function order.
        + Creating a task now causes a context switch if the task being created
        + Creating a task now causes a context switch if the task being created
          has a higher priority than the calling task - assuming the kernel is
          has a higher priority than the calling task - assuming the kernel is
          running.
          running.
        + vTaskDelete() now only causes a context switch if the calling task is
        + vTaskDelete() now only causes a context switch if the calling task is
          the task being deleted.
          the task being deleted.
 
 
Changes from V2.0.0
Changes from V2.0.0
 
 
        + Allow the type of the tick count to be 16 or 32 bits.
        + Allow the type of the tick count to be 16 or 32 bits.
        + Introduce xPendingReadyList feature to allow the time interrupts have to
        + Introduce xPendingReadyList feature to allow the time interrupts have to
          be disabled to be minimised.
          be disabled to be minimised.
        + Remove the #if( INCLUDE_vTaskSuspendAll ) statements.  vTaskSuspendAll()
        + Remove the #if( INCLUDE_vTaskSuspendAll ) statements.  vTaskSuspendAll()
          is now always included as it is used by the scheduler itself.
          is now always included as it is used by the scheduler itself.
 
 
Changes from V2.1.0
Changes from V2.1.0
 
 
        + Bug fix - pxCurrentTCB is now initialised before the call to
        + Bug fix - pxCurrentTCB is now initialised before the call to
          prvInitializeTaskLists().  Previously pxCurrentTCB could be accessed
          prvInitializeTaskLists().  Previously pxCurrentTCB could be accessed
          while null.
          while null.
 
 
Changed from V2.1.1
Changed from V2.1.1
 
 
        + Change to where lStackSize is declared within sTaskCreate() to prevent
        + Change to where lStackSize is declared within sTaskCreate() to prevent
          compiler warnings with 8051 port.
          compiler warnings with 8051 port.
 
 
Changes from V2.2.0
Changes from V2.2.0
 
 
        + Explicit use of 'signed' qualifier on portCHAR types added.
        + Explicit use of 'signed' qualifier on portCHAR types added.
        + Changed odd calculation of initial pxTopOfStack value when
        + Changed odd calculation of initial pxTopOfStack value when
          portSTACK_GROWTH < 0.
          portSTACK_GROWTH < 0.
        + Removed pcVersionNumber definition.
        + Removed pcVersionNumber definition.
 
 
Changes from V2.5.3
Changes from V2.5.3
 
 
        + cTaskResumeAll() modified to ensure it can be called prior to the task
        + cTaskResumeAll() modified to ensure it can be called prior to the task
          lists being initialised.
          lists being initialised.
 
 
Changes from V2.5.5
Changes from V2.5.5
 
 
        + Added API function vTaskDelayUntil().
        + Added API function vTaskDelayUntil().
        + Added INCLUDE_vTaskDelay conditional compilation.
        + Added INCLUDE_vTaskDelay conditional compilation.
 
 
Changes from V2.6.0
Changes from V2.6.0
 
 
        + Updated the vWriteTraceToBuffer macro to always be 4 byte aligned so it
        + Updated the vWriteTraceToBuffer macro to always be 4 byte aligned so it
          can be used on ARM architectures.
          can be used on ARM architectures.
        + tskMAX_TASK_NAME_LEN definition replaced with the port specific
        + tskMAX_TASK_NAME_LEN definition replaced with the port specific
          configMAX_TASK_NAME_LEN definition.
          configMAX_TASK_NAME_LEN definition.
        + Removed the call to strcpy when copying across the task name into the
        + Removed the call to strcpy when copying across the task name into the
          TCB.
          TCB.
        + Added ucTasksDeleted variable to prevent vTaskSuspendAll() being called
        + Added ucTasksDeleted variable to prevent vTaskSuspendAll() being called
          too often in the idle task.
          too often in the idle task.
 
 
Changes between V3.0.0 and V2.6.1
Changes between V3.0.0 and V2.6.1
 
 
        + When resuming the scheduler a yield is performed if either a tick has
        + When resuming the scheduler a yield is performed if either a tick has
          been missed, or a task is moved from the pending ready list into a ready
          been missed, or a task is moved from the pending ready list into a ready
          list.  Previously a yield was not performed on this second condition.
          list.  Previously a yield was not performed on this second condition.
        + Introduced the type portBASE_TYPE.  This necessitates several API
        + Introduced the type portBASE_TYPE.  This necessitates several API
          changes.
          changes.
        + Removed the sUsingPreemption variable.  The constant defined in
        + Removed the sUsingPreemption variable.  The constant defined in
          portmacro.h is now used directly.
          portmacro.h is now used directly.
        + The idle task can now include an optional hook function - and no longer
        + The idle task can now include an optional hook function - and no longer
          completes its time slice if other tasks with equal priority to it are
          completes its time slice if other tasks with equal priority to it are
          ready to run.
          ready to run.
        + See the FreeRTOS.org documentation for more information on V2.x.x to
        + See the FreeRTOS.org documentation for more information on V2.x.x to
          V3.x.x modifications.
          V3.x.x modifications.
 
 
Changes from V3.1.1
Changes from V3.1.1
 
 
        + Modified vTaskPrioritySet() and vTaskResume() to allow these functions to
        + Modified vTaskPrioritySet() and vTaskResume() to allow these functions to
          be called while the scheduler is suspended.
          be called while the scheduler is suspended.
        + Corrected the task ordering within event lists.
        + Corrected the task ordering within event lists.
 
 
Changes from V3.2.0
Changes from V3.2.0
 
 
        + Added function xTaskGetCurrentTaskHandle().
        + Added function xTaskGetCurrentTaskHandle().
 
 
Changes from V3.2.4
Changes from V3.2.4
 
 
        + Changed the volatile declarations on some variables to reflect the
        + Changed the volatile declarations on some variables to reflect the
          changes to the list definitions.
          changes to the list definitions.
        + Changed the order of the TCB definition so there is commonality between
        + Changed the order of the TCB definition so there is commonality between
          the task control block and a co-routine control block.
          the task control block and a co-routine control block.
        + Allow the scheduler to be started even if no tasks other than the idle
        + Allow the scheduler to be started even if no tasks other than the idle
          task has been created.  This allows co-routines to run even when no tasks
          task has been created.  This allows co-routines to run even when no tasks
          have been created.
          have been created.
        + The need for a context switch is now signalled if a task woken by an
        + The need for a context switch is now signalled if a task woken by an
          event has a priority greater or equal to the currently running task.
          event has a priority greater or equal to the currently running task.
          Previously this was only greater than.
          Previously this was only greater than.
 
 
Changes from V4.0.0
Changes from V4.0.0
 
 
        + Added the xMissedYield handling.
        + Added the xMissedYield handling.
 
 
Changes from V4.0.1
Changes from V4.0.1
 
 
        + The function vTaskList() now suspends the scheduler rather than disabling
        + The function vTaskList() now suspends the scheduler rather than disabling
          interrupts during the creation of the task list.
          interrupts during the creation of the task list.
        + Allow a task to delete itself by passing in its own handle.  Previously
        + Allow a task to delete itself by passing in its own handle.  Previously
          this could only be done by passing in NULL.
          this could only be done by passing in NULL.
        + The tick hook function is now called only within a tick isr.  Previously
        + The tick hook function is now called only within a tick isr.  Previously
          it was also called when the tick function was called during the scheduler
          it was also called when the tick function was called during the scheduler
          unlocking process.
          unlocking process.
 
 
Changes from V4.0.3
Changes from V4.0.3
 
 
        + Extra checks have been placed in vTaskPrioritySet() to avoid unnecessary
        + Extra checks have been placed in vTaskPrioritySet() to avoid unnecessary
          yields.
          yields.
 
 
Changed from V4.0.4
Changed from V4.0.4
 
 
        + Bug fix:  The 'value' of the event list item is updated when the priority
        + Bug fix:  The 'value' of the event list item is updated when the priority
          of a task is changed.  Previously only the priority of the TCB itself was
          of a task is changed.  Previously only the priority of the TCB itself was
          changed.
          changed.
        + When resuming a task a check is first made to see if the task is actually
        + When resuming a task a check is first made to see if the task is actually
          suspended.
          suspended.
        + vTaskPrioritySet() and vTaskResume() no longer use the event list item.
        + vTaskPrioritySet() and vTaskResume() no longer use the event list item.
          This has not been necessary since V4.0.1 when the xMissedYield handling
          This has not been necessary since V4.0.1 when the xMissedYield handling
          was added.
          was added.
        + Implement xTaskResumeFromISR().
        + Implement xTaskResumeFromISR().
 
 
Changes from V4.0.5
Changes from V4.0.5
 
 
        + Added utility functions and xOverflowCount variable to facilitate the
        + Added utility functions and xOverflowCount variable to facilitate the
          queue.c changes.
          queue.c changes.
 
 
Changes from V4.1.2
Changes from V4.1.2
 
 
        + Tasks that block on events with a timeout of portMAX_DELAY are now
        + Tasks that block on events with a timeout of portMAX_DELAY are now
          blocked indefinitely if configINCLUDE_vTaskSuspend is defined.
          blocked indefinitely if configINCLUDE_vTaskSuspend is defined.
          Previously portMAX_DELAY was just the longest block time possible.
          Previously portMAX_DELAY was just the longest block time possible.
 
 
Changes from V4.1.3
Changes from V4.1.3
 
 
        + Very small change made to xTaskCheckForTimeout() as a result of the
        + Very small change made to xTaskCheckForTimeout() as a result of the
        SafeRTOS testing.  This corrects the case where the function can return an
        SafeRTOS testing.  This corrects the case where the function can return an
        invalid value - but only in an extremely unlikely scenario.
        invalid value - but only in an extremely unlikely scenario.
*/
*/
 
 
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
 
 
#include "FreeRTOS.h"
#include "FreeRTOS.h"
#include "task.h"
#include "task.h"
 
 
/*
/*
 * Macro to define the amount of stack available to the idle task.
 * Macro to define the amount of stack available to the idle task.
 */
 */
#define tskIDLE_STACK_SIZE      configMINIMAL_STACK_SIZE
#define tskIDLE_STACK_SIZE      configMINIMAL_STACK_SIZE
 
 
 
 
/*
/*
 * Default a definitions for backwards compatibility with old
 * Default a definitions for backwards compatibility with old
 * portmacro.h files.
 * portmacro.h files.
 */
 */
#ifndef configMAX_TASK_NAME_LEN
#ifndef configMAX_TASK_NAME_LEN
        #define configMAX_TASK_NAME_LEN 16
        #define configMAX_TASK_NAME_LEN 16
#endif
#endif
 
 
#ifndef INCLUDE_xTaskGetCurrentTaskHandle
#ifndef INCLUDE_xTaskGetCurrentTaskHandle
        #define INCLUDE_xTaskGetCurrentTaskHandle 0
        #define INCLUDE_xTaskGetCurrentTaskHandle 0
#endif
#endif
 
 
#ifndef configIDLE_SHOULD_YIELD
#ifndef configIDLE_SHOULD_YIELD
        #define configIDLE_SHOULD_YIELD         1
        #define configIDLE_SHOULD_YIELD         1
#endif
#endif
 
 
#if configMAX_TASK_NAME_LEN < 1
#if configMAX_TASK_NAME_LEN < 1
        #undef configMAX_TASK_NAME_LEN
        #undef configMAX_TASK_NAME_LEN
        #define configMAX_TASK_NAME_LEN 1
        #define configMAX_TASK_NAME_LEN 1
#endif
#endif
 
 
#ifndef INCLUDE_xTaskResumeFromISR
#ifndef INCLUDE_xTaskResumeFromISR
        #define INCLUDE_xTaskResumeFromISR 1
        #define INCLUDE_xTaskResumeFromISR 1
#endif 
#endif 
 
 
/*
/*
 * Task control block.  A task control block (TCB) is allocated to each task,
 * Task control block.  A task control block (TCB) is allocated to each task,
 * and stores the context of the task.
 * and stores the context of the task.
 */
 */
typedef struct tskTaskControlBlock
typedef struct tskTaskControlBlock
{
{
        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. */
        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. */
        xListItem                               xGenericListItem;       /*< List item used to place the TCB in ready and blocked queues. */
        xListItem                               xGenericListItem;       /*< List item used to place the TCB in ready and blocked queues. */
        xListItem                               xEventListItem;         /*< List item used to place the TCB in event lists. */
        xListItem                               xEventListItem;         /*< List item used to place the TCB in event lists. */
        unsigned portBASE_TYPE  uxPriority;                     /*< The priority of the task where 0 is the lowest priority. */
        unsigned portBASE_TYPE  uxPriority;                     /*< The priority of the task where 0 is the lowest priority. */
        portSTACK_TYPE                  *pxStack;                       /*< Points to the start of the stack. */
        portSTACK_TYPE                  *pxStack;                       /*< Points to the start of the stack. */
        unsigned portBASE_TYPE  uxTCBNumber;            /*< This is used for tracing the scheduler and making debugging easier only. */
        unsigned portBASE_TYPE  uxTCBNumber;            /*< This is used for tracing the scheduler and making debugging easier only. */
        signed portCHAR                 pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */
        signed portCHAR                 pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */
        unsigned portSHORT              usStackDepth;           /*< Total depth of the stack (when empty).  This is defined as the number of variables the stack can hold, not the number of bytes. */
        unsigned portSHORT              usStackDepth;           /*< Total depth of the stack (when empty).  This is defined as the number of variables the stack can hold, not the number of bytes. */
} tskTCB;
} tskTCB;
 
 
/*lint -e956 */
/*lint -e956 */
 
 
tskTCB * volatile pxCurrentTCB = NULL;
tskTCB * volatile pxCurrentTCB = NULL;
 
 
/* Lists for ready and blocked tasks. --------------------*/
/* Lists for ready and blocked tasks. --------------------*/
 
 
static xList pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
static xList pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
static xList xDelayedTaskList1;                                                 /*< Delayed tasks. */
static xList xDelayedTaskList1;                                                 /*< Delayed tasks. */
static xList xDelayedTaskList2;                                                 /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
static xList xDelayedTaskList2;                                                 /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
static xList * volatile pxDelayedTaskList;                              /*< Points to the delayed task list currently being used. */
static xList * volatile pxDelayedTaskList;                              /*< Points to the delayed task list currently being used. */
static xList * volatile pxOverflowDelayedTaskList;              /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
static xList * volatile pxOverflowDelayedTaskList;              /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
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. */
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. */
 
 
#if ( INCLUDE_vTaskDelete == 1 )
#if ( INCLUDE_vTaskDelete == 1 )
 
 
        static volatile xList xTasksWaitingTermination;         /*< Tasks that have been deleted - but the their memory not yet freed. */
        static volatile xList xTasksWaitingTermination;         /*< Tasks that have been deleted - but the their memory not yet freed. */
        static volatile unsigned portBASE_TYPE uxTasksDeleted = ( unsigned portBASE_TYPE ) 0;
        static volatile unsigned portBASE_TYPE uxTasksDeleted = ( unsigned portBASE_TYPE ) 0;
 
 
#endif
#endif
 
 
#if ( INCLUDE_vTaskSuspend == 1 )
#if ( INCLUDE_vTaskSuspend == 1 )
 
 
        static xList xSuspendedTaskList;                                        /*< Tasks that are currently suspended. */
        static xList xSuspendedTaskList;                                        /*< Tasks that are currently suspended. */
 
 
#endif
#endif
 
 
/* File private variables. --------------------------------*/
/* File private variables. --------------------------------*/
static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks   = ( unsigned portBASE_TYPE ) 0;
static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks   = ( unsigned portBASE_TYPE ) 0;
static volatile portTickType xTickCount                                                 = ( portTickType ) 0;
static volatile portTickType xTickCount                                                 = ( portTickType ) 0;
static unsigned portBASE_TYPE uxTopUsedPriority                                 = tskIDLE_PRIORITY;
static unsigned portBASE_TYPE uxTopUsedPriority                                 = tskIDLE_PRIORITY;
static volatile unsigned portBASE_TYPE uxTopReadyPriority               = tskIDLE_PRIORITY;
static volatile unsigned portBASE_TYPE uxTopReadyPriority               = tskIDLE_PRIORITY;
static volatile signed portBASE_TYPE xSchedulerRunning                  = pdFALSE;
static volatile signed portBASE_TYPE xSchedulerRunning                  = pdFALSE;
static volatile unsigned portBASE_TYPE uxSchedulerSuspended             = ( unsigned portBASE_TYPE ) pdFALSE;
static volatile unsigned portBASE_TYPE uxSchedulerSuspended             = ( unsigned portBASE_TYPE ) pdFALSE;
static volatile unsigned portBASE_TYPE uxMissedTicks                    = ( unsigned portBASE_TYPE ) 0;
static volatile unsigned portBASE_TYPE uxMissedTicks                    = ( unsigned portBASE_TYPE ) 0;
static volatile portBASE_TYPE xMissedYield                                              = ( portBASE_TYPE ) pdFALSE;
static volatile portBASE_TYPE xMissedYield                                              = ( portBASE_TYPE ) pdFALSE;
static volatile portBASE_TYPE xNumOfOverflows                                   = ( portBASE_TYPE ) 0;
static volatile portBASE_TYPE xNumOfOverflows                                   = ( portBASE_TYPE ) 0;
/* Debugging and trace facilities private variables and macros. ------------*/
/* Debugging and trace facilities private variables and macros. ------------*/
 
 
/*
/*
 * The value used to fill the stack of a task when the task is created.  This
 * The value used to fill the stack of a task when the task is created.  This
 * is used purely for checking the high water mark for tasks.
 * is used purely for checking the high water mark for tasks.
 */
 */
#define tskSTACK_FILL_BYTE      ( 0xa5 )
#define tskSTACK_FILL_BYTE      ( 0xa5 )
 
 
/*
/*
 * Macros used by vListTask to indicate which state a task is in.
 * Macros used by vListTask to indicate which state a task is in.
 */
 */
#define tskBLOCKED_CHAR         ( ( signed portCHAR ) 'B' )
#define tskBLOCKED_CHAR         ( ( signed portCHAR ) 'B' )
#define tskREADY_CHAR           ( ( signed portCHAR ) 'R' )
#define tskREADY_CHAR           ( ( signed portCHAR ) 'R' )
#define tskDELETED_CHAR         ( ( signed portCHAR ) 'D' )
#define tskDELETED_CHAR         ( ( signed portCHAR ) 'D' )
#define tskSUSPENDED_CHAR       ( ( signed portCHAR ) 'S' )
#define tskSUSPENDED_CHAR       ( ( signed portCHAR ) 'S' )
 
 
/*
/*
 * Macros and private variables used by the trace facility.
 * Macros and private variables used by the trace facility.
 */
 */
#if ( configUSE_TRACE_FACILITY == 1 )
#if ( configUSE_TRACE_FACILITY == 1 )
 
 
        #define tskSIZE_OF_EACH_TRACE_LINE                      ( ( unsigned portLONG ) ( sizeof( unsigned portLONG ) + sizeof( unsigned portLONG ) ) )
        #define tskSIZE_OF_EACH_TRACE_LINE                      ( ( unsigned portLONG ) ( sizeof( unsigned portLONG ) + sizeof( unsigned portLONG ) ) )
        static volatile signed portCHAR * volatile pcTraceBuffer;
        static volatile signed portCHAR * volatile pcTraceBuffer;
        static signed portCHAR *pcTraceBufferStart;
        static signed portCHAR *pcTraceBufferStart;
        static signed portCHAR *pcTraceBufferEnd;
        static signed portCHAR *pcTraceBufferEnd;
        static signed portBASE_TYPE xTracing = pdFALSE;
        static signed portBASE_TYPE xTracing = pdFALSE;
 
 
#endif
#endif
 
 
/*
/*
 * Macro that writes a trace of scheduler activity to a buffer.  This trace
 * Macro that writes a trace of scheduler activity to a buffer.  This trace
 * shows which task is running when and is very useful as a debugging tool.
 * shows which task is running when and is very useful as a debugging tool.
 * As this macro is called each context switch it is a good idea to undefine
 * As this macro is called each context switch it is a good idea to undefine
 * it if not using the facility.
 * it if not using the facility.
 */
 */
#if ( configUSE_TRACE_FACILITY == 1 )
#if ( configUSE_TRACE_FACILITY == 1 )
 
 
        #define vWriteTraceToBuffer()                                                                                                                                   \
        #define vWriteTraceToBuffer()                                                                                                                                   \
        {                                                                                                                                                                                               \
        {                                                                                                                                                                                               \
                if( xTracing )                                                                                                                                                          \
                if( xTracing )                                                                                                                                                          \
                {                                                                                                                                                                                       \
                {                                                                                                                                                                                       \
                        static unsigned portBASE_TYPE uxPreviousTask = 255;                                                                             \
                        static unsigned portBASE_TYPE uxPreviousTask = 255;                                                                             \
                                                                                                                                                                                                        \
                                                                                                                                                                                                        \
                        if( uxPreviousTask != pxCurrentTCB->uxTCBNumber )                                                                               \
                        if( uxPreviousTask != pxCurrentTCB->uxTCBNumber )                                                                               \
                        {                                                                                                                                                                               \
                        {                                                                                                                                                                               \
                                if( ( pcTraceBuffer + tskSIZE_OF_EACH_TRACE_LINE ) < pcTraceBufferEnd )                         \
                                if( ( pcTraceBuffer + tskSIZE_OF_EACH_TRACE_LINE ) < pcTraceBufferEnd )                         \
                                {                                                                                                                                                                       \
                                {                                                                                                                                                                       \
                                        uxPreviousTask = pxCurrentTCB->uxTCBNumber;                                                                             \
                                        uxPreviousTask = pxCurrentTCB->uxTCBNumber;                                                                             \
                                        *( unsigned portLONG * ) pcTraceBuffer = ( unsigned portLONG ) xTickCount;              \
                                        *( unsigned portLONG * ) pcTraceBuffer = ( unsigned portLONG ) xTickCount;              \
                                        pcTraceBuffer += sizeof( unsigned portLONG );                                                                   \
                                        pcTraceBuffer += sizeof( unsigned portLONG );                                                                   \
                                        *( unsigned portLONG * ) pcTraceBuffer = ( unsigned portLONG ) uxPreviousTask;  \
                                        *( unsigned portLONG * ) pcTraceBuffer = ( unsigned portLONG ) uxPreviousTask;  \
                                        pcTraceBuffer += sizeof( unsigned portLONG );                                                                   \
                                        pcTraceBuffer += sizeof( unsigned portLONG );                                                                   \
                                }                                                                                                                                                                       \
                                }                                                                                                                                                                       \
                                else                                                                                                                                                            \
                                else                                                                                                                                                            \
                                {                                                                                                                                                                       \
                                {                                                                                                                                                                       \
                                        xTracing = pdFALSE;                                                                                                                             \
                                        xTracing = pdFALSE;                                                                                                                             \
                                }                                                                                                                                                                       \
                                }                                                                                                                                                                       \
                        }                                                                                                                                                                               \
                        }                                                                                                                                                                               \
                }                                                                                                                                                                                       \
                }                                                                                                                                                                                       \
        }
        }
 
 
#else
#else
 
 
        #define vWriteTraceToBuffer()
        #define vWriteTraceToBuffer()
 
 
#endif
#endif
 
 
 
 
/*
/*
 * Place the task represented by pxTCB into the appropriate ready queue for
 * Place the task represented by pxTCB into the appropriate ready queue for
 * the task.  It is inserted at the end of the list.  One quirk of this is
 * the task.  It is inserted at the end of the list.  One quirk of this is
 * that if the task being inserted is at the same priority as the currently
 * that if the task being inserted is at the same priority as the currently
 * executing task, then it will only be rescheduled after the currently
 * executing task, then it will only be rescheduled after the currently
 * executing task has been rescheduled.
 * executing task has been rescheduled.
 */
 */
#define prvAddTaskToReadyQueue( pxTCB )                                                                                                                                                 \
#define prvAddTaskToReadyQueue( pxTCB )                                                                                                                                                 \
{                                                                                                                                                                                                                               \
{                                                                                                                                                                                                                               \
        if( pxTCB->uxPriority > uxTopReadyPriority )                                                                                                                            \
        if( pxTCB->uxPriority > uxTopReadyPriority )                                                                                                                            \
        {                                                                                                                                                                                                                       \
        {                                                                                                                                                                                                                       \
                uxTopReadyPriority = pxTCB->uxPriority;                                                                                                                                 \
                uxTopReadyPriority = pxTCB->uxPriority;                                                                                                                                 \
        }                                                                                                                                                                                                                       \
        }                                                                                                                                                                                                                       \
        vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) );        \
        vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) );        \
}
}
 
 
/*
/*
 * Macro that looks at the list of tasks that are currently delayed to see if
 * Macro that looks at the list of tasks that are currently delayed to see if
 * any require waking.
 * any require waking.
 *
 *
 * Tasks are stored in the queue in the order of their wake time - meaning
 * Tasks are stored in the queue in the order of their wake time - meaning
 * once one tasks has been found whose timer has not expired we need not look
 * once one tasks has been found whose timer has not expired we need not look
 * any further down the list.
 * any further down the list.
 */
 */
#define prvCheckDelayedTasks()                                                                                                                                                                          \
#define prvCheckDelayedTasks()                                                                                                                                                                          \
{                                                                                                                                                                                                                                       \
{                                                                                                                                                                                                                                       \
register tskTCB *pxTCB;                                                                                                                                                                                         \
register tskTCB *pxTCB;                                                                                                                                                                                         \
                                                                                                                                                                                                                                        \
                                                                                                                                                                                                                                        \
        while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ) ) != NULL )                                              \
        while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ) ) != NULL )                                              \
        {                                                                                                                                                                                                                               \
        {                                                                                                                                                                                                                               \
                if( xTickCount < listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) ) )                                                                      \
                if( xTickCount < listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) ) )                                                                      \
                {                                                                                                                                                                                                                       \
                {                                                                                                                                                                                                                       \
                        break;                                                                                                                                                                                                  \
                        break;                                                                                                                                                                                                  \
                }                                                                                                                                                                                                                       \
                }                                                                                                                                                                                                                       \
                vListRemove( &( pxTCB->xGenericListItem ) );                                                                                                                            \
                vListRemove( &( pxTCB->xGenericListItem ) );                                                                                                                            \
                /* Is the task waiting on an event also? */                                                                                                                                     \
                /* Is the task waiting on an event also? */                                                                                                                                     \
                if( pxTCB->xEventListItem.pvContainer )                                                                                                                                         \
                if( pxTCB->xEventListItem.pvContainer )                                                                                                                                         \
                {                                                                                                                                                                                                                       \
                {                                                                                                                                                                                                                       \
                        vListRemove( &( pxTCB->xEventListItem ) );                                                                                                                              \
                        vListRemove( &( pxTCB->xEventListItem ) );                                                                                                                              \
                }                                                                                                                                                                                                                       \
                }                                                                                                                                                                                                                       \
                prvAddTaskToReadyQueue( pxTCB );                                                                                                                \
                prvAddTaskToReadyQueue( pxTCB );                                                                                                                \
        }                                                                                                                                                                                                                               \
        }                                                                                                                                                                                                                               \
}
}
 
 
/*
/*
 * Several functions take an xTaskHandle parameter that can optionally be NULL,
 * Several functions take an xTaskHandle parameter that can optionally be NULL,
 * where NULL is used to indicate that the handle of the currently executing
 * where NULL is used to indicate that the handle of the currently executing
 * task should be used in place of the parameter.  This macro simply checks to
 * task should be used in place of the parameter.  This macro simply checks to
 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
 */
 */
#define prvGetTCBFromHandle( pxHandle ) ( ( pxHandle == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) pxHandle )
#define prvGetTCBFromHandle( pxHandle ) ( ( pxHandle == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) pxHandle )
 
 
 
 
/* File private functions. --------------------------------*/
/* File private functions. --------------------------------*/
 
 
/*
/*
 * Utility to ready a TCB for a given task.  Mainly just copies the parameters
 * Utility to ready a TCB for a given task.  Mainly just copies the parameters
 * into the TCB structure.
 * into the TCB structure.
 */
 */
static void prvInitialiseTCBVariables( tskTCB *pxTCB, unsigned portSHORT usStackDepth, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority );
static void prvInitialiseTCBVariables( tskTCB *pxTCB, unsigned portSHORT usStackDepth, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority );
 
 
/*
/*
 * Utility to ready all the lists used by the scheduler.  This is called
 * Utility to ready all the lists used by the scheduler.  This is called
 * automatically upon the creation of the first task.
 * automatically upon the creation of the first task.
 */
 */
static void prvInitialiseTaskLists( void );
static void prvInitialiseTaskLists( void );
 
 
/*
/*
 * The idle task, which as all tasks is implemented as a never ending loop.
 * The idle task, which as all tasks is implemented as a never ending loop.
 * The idle task is automatically created and added to the ready lists upon
 * The idle task is automatically created and added to the ready lists upon
 * creation of the first user task.
 * creation of the first user task.
 *
 *
 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
 * language extensions.  The equivalent prototype for this function is:
 * language extensions.  The equivalent prototype for this function is:
 *
 *
 * void prvIdleTask( void *pvParameters );
 * void prvIdleTask( void *pvParameters );
 *
 *
 */
 */
static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
 
 
/*
/*
 * Utility to free all memory allocated by the scheduler to hold a TCB,
 * Utility to free all memory allocated by the scheduler to hold a TCB,
 * including the stack pointed to by the TCB.
 * including the stack pointed to by the TCB.
 *
 *
 * This does not free memory allocated by the task itself (i.e. memory
 * This does not free memory allocated by the task itself (i.e. memory
 * allocated by calls to pvPortMalloc from within the tasks application code).
 * allocated by calls to pvPortMalloc from within the tasks application code).
 */
 */
#if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
#if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
        static void prvDeleteTCB( tskTCB *pxTCB );
        static void prvDeleteTCB( tskTCB *pxTCB );
#endif
#endif
 
 
/*
/*
 * Used only by the idle task.  This checks to see if anything has been placed
 * Used only by the idle task.  This checks to see if anything has been placed
 * in the list of tasks waiting to be deleted.  If so the task is cleaned up
 * in the list of tasks waiting to be deleted.  If so the task is cleaned up
 * and its TCB deleted.
 * and its TCB deleted.
 */
 */
static void prvCheckTasksWaitingTermination( void );
static void prvCheckTasksWaitingTermination( void );
 
 
/*
/*
 * Allocates memory from the heap for a TCB and associated stack.  Checks the
 * Allocates memory from the heap for a TCB and associated stack.  Checks the
 * allocation was successful.
 * allocation was successful.
 */
 */
static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth );
static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth );
 
 
/*
/*
 * Called from vTaskList.  vListTasks details all the tasks currently under
 * Called from vTaskList.  vListTasks details all the tasks currently under
 * control of the scheduler.  The tasks may be in one of a number of lists.
 * control of the scheduler.  The tasks may be in one of a number of lists.
 * prvListTaskWithinSingleList accepts a list and details the tasks from
 * prvListTaskWithinSingleList accepts a list and details the tasks from
 * within just that list.
 * within just that list.
 *
 *
 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
 * NORMAL APPLICATION CODE.
 * NORMAL APPLICATION CODE.
 */
 */
#if ( configUSE_TRACE_FACILITY == 1 )
#if ( configUSE_TRACE_FACILITY == 1 )
 
 
        static void prvListTaskWithinSingleList( signed portCHAR *pcWriteBuffer, xList *pxList, signed portCHAR cStatus );
        static void prvListTaskWithinSingleList( signed portCHAR *pcWriteBuffer, xList *pxList, signed portCHAR cStatus );
 
 
#endif
#endif
 
 
/*
/*
 * When a task is created, the stack of the task is filled with a known value.
 * When a task is created, the stack of the task is filled with a known value.
 * This function determines the 'high water mark' of the task stack by
 * This function determines the 'high water mark' of the task stack by
 * determining how much of the stack remains at the original preset value.
 * determining how much of the stack remains at the original preset value.
 */
 */
#if ( configUSE_TRACE_FACILITY == 1 )
#if ( configUSE_TRACE_FACILITY == 1 )
 
 
        unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR *pucStackByte );
        unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR *pucStackByte );
 
 
#endif
#endif
 
 
/*lint +e956 */
/*lint +e956 */
 
 
 
 
 
 
 
 
 
 
/*-----------------------------------------------------------
/*-----------------------------------------------------------
 * TASK CREATION API documented in task.h
 * TASK CREATION API documented in task.h
 *----------------------------------------------------------*/
 *----------------------------------------------------------*/
 
 
signed portBASE_TYPE xTaskCreate( pdTASK_CODE pvTaskCode, const signed portCHAR * const pcName, unsigned portSHORT usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask )
signed portBASE_TYPE xTaskCreate( pdTASK_CODE pvTaskCode, const signed portCHAR * const pcName, unsigned portSHORT usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask )
{
{
signed portBASE_TYPE xReturn;
signed portBASE_TYPE xReturn;
tskTCB * pxNewTCB;
tskTCB * pxNewTCB;
static unsigned portBASE_TYPE uxTaskNumber = 0; /*lint !e956 Static is deliberate - this is guarded before use. */
static unsigned portBASE_TYPE uxTaskNumber = 0; /*lint !e956 Static is deliberate - this is guarded before use. */
 
 
        /* Allocate the memory required by the TCB and stack for the new task.
        /* Allocate the memory required by the TCB and stack for the new task.
        checking that the allocation was successful. */
        checking that the allocation was successful. */
        pxNewTCB = prvAllocateTCBAndStack( usStackDepth );
        pxNewTCB = prvAllocateTCBAndStack( usStackDepth );
 
 
        if( pxNewTCB != NULL )
        if( pxNewTCB != NULL )
        {
        {
                portSTACK_TYPE *pxTopOfStack;
                portSTACK_TYPE *pxTopOfStack;
 
 
                /* Setup the newly allocated TCB with the initial state of the task. */
                /* Setup the newly allocated TCB with the initial state of the task. */
                prvInitialiseTCBVariables( pxNewTCB, usStackDepth, pcName, uxPriority );
                prvInitialiseTCBVariables( pxNewTCB, usStackDepth, pcName, uxPriority );
 
 
                /* Calculate the top of stack address.  This depends on whether the
                /* Calculate the top of stack address.  This depends on whether the
                stack grows from high memory to low (as per the 80x86) or visa versa.
                stack grows from high memory to low (as per the 80x86) or visa versa.
                portSTACK_GROWTH is used to make the result positive or negative as
                portSTACK_GROWTH is used to make the result positive or negative as
                required by the port. */
                required by the port. */
                #if portSTACK_GROWTH < 0
                #if portSTACK_GROWTH < 0
                {
                {
                        pxTopOfStack = pxNewTCB->pxStack + ( pxNewTCB->usStackDepth - 1 );
                        pxTopOfStack = pxNewTCB->pxStack + ( pxNewTCB->usStackDepth - 1 );
                }
                }
                #else
                #else
                {
                {
                        pxTopOfStack = pxNewTCB->pxStack;
                        pxTopOfStack = pxNewTCB->pxStack;
                }
                }
                #endif
                #endif
 
 
                /* Initialize the TCB stack to look as if the task was already running,
                /* Initialize the TCB stack to look as if the task was already running,
                but had been interrupted by the scheduler.  The return address is set
                but had been interrupted by the scheduler.  The return address is set
                to the start of the task function. Once the stack has been initialised
                to the start of the task function. Once the stack has been initialised
                the     top of stack variable is updated. */
                the     top of stack variable is updated. */
                pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pvTaskCode, pvParameters );
                pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pvTaskCode, pvParameters );
 
 
                /* We are going to manipulate the task queues to add this task to a
                /* We are going to manipulate the task queues to add this task to a
                ready list, so must make sure no interrupts occur. */
                ready list, so must make sure no interrupts occur. */
                portENTER_CRITICAL();
                portENTER_CRITICAL();
                {
                {
                        uxCurrentNumberOfTasks++;
                        uxCurrentNumberOfTasks++;
                        if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )
                        if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )
                        {
                        {
                                /* As this is the first task it must also be the current task. */
                                /* As this is the first task it must also be the current task. */
                                pxCurrentTCB =  pxNewTCB;
                                pxCurrentTCB =  pxNewTCB;
 
 
                                /* This is the first task to be created so do the preliminary
                                /* This is the first task to be created so do the preliminary
                                initialisation required.  We will not recover if this call
                                initialisation required.  We will not recover if this call
                                fails, but we will report the failure. */
                                fails, but we will report the failure. */
                                prvInitialiseTaskLists();
                                prvInitialiseTaskLists();
                        }
                        }
                        else
                        else
                        {
                        {
                                /* If the scheduler is not already running, make this task the
                                /* If the scheduler is not already running, make this task the
                                current task if it is the highest priority task to be created
                                current task if it is the highest priority task to be created
                                so far. */
                                so far. */
                                if( xSchedulerRunning == pdFALSE )
                                if( xSchedulerRunning == pdFALSE )
                                {
                                {
                                        if( pxCurrentTCB->uxPriority <= uxPriority )
                                        if( pxCurrentTCB->uxPriority <= uxPriority )
                                        {
                                        {
                                                pxCurrentTCB = pxNewTCB;
                                                pxCurrentTCB = pxNewTCB;
                                        }
                                        }
                                }
                                }
                        }
                        }
 
 
                        /* Remember the top priority to make context switching faster.  Use
                        /* Remember the top priority to make context switching faster.  Use
                        the priority in pxNewTCB as this has been capped to a valid value. */
                        the priority in pxNewTCB as this has been capped to a valid value. */
                        if( pxNewTCB->uxPriority > uxTopUsedPriority )
                        if( pxNewTCB->uxPriority > uxTopUsedPriority )
                        {
                        {
                                uxTopUsedPriority = pxNewTCB->uxPriority;
                                uxTopUsedPriority = pxNewTCB->uxPriority;
                        }
                        }
 
 
                        /* Add a counter into the TCB for tracing only. */
                        /* Add a counter into the TCB for tracing only. */
                        pxNewTCB->uxTCBNumber = uxTaskNumber;
                        pxNewTCB->uxTCBNumber = uxTaskNumber;
                        uxTaskNumber++;
                        uxTaskNumber++;
 
 
                        prvAddTaskToReadyQueue( pxNewTCB );
                        prvAddTaskToReadyQueue( pxNewTCB );
 
 
                        xReturn = pdPASS;
                        xReturn = pdPASS;
                }
                }
                portEXIT_CRITICAL();
                portEXIT_CRITICAL();
        }
        }
        else
        else
        {
        {
                xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
                xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
        }
        }
 
 
        if( xReturn == pdPASS )
        if( xReturn == pdPASS )
        {
        {
                if( ( void * ) pxCreatedTask != NULL )
                if( ( void * ) pxCreatedTask != NULL )
                {
                {
                        /* Pass the TCB out - in an anonymous way.  The calling function/
                        /* Pass the TCB out - in an anonymous way.  The calling function/
                        task can use this as a handle to delete the task later if
                        task can use this as a handle to delete the task later if
                        required.*/
                        required.*/
                        *pxCreatedTask = ( xTaskHandle ) pxNewTCB;
                        *pxCreatedTask = ( xTaskHandle ) pxNewTCB;
                }
                }
 
 
                if( xSchedulerRunning != pdFALSE )
                if( xSchedulerRunning != pdFALSE )
                {
                {
                        /* If the created task is of a higher priority than the current task
                        /* If the created task is of a higher priority than the current task
                        then it should run now. */
                        then it should run now. */
                        if( pxCurrentTCB->uxPriority < uxPriority )
                        if( pxCurrentTCB->uxPriority < uxPriority )
                        {
                        {
                                taskYIELD();
                                taskYIELD();
                        }
                        }
                }
                }
        }
        }
 
 
        return xReturn;
        return xReturn;
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( INCLUDE_vTaskDelete == 1 )
#if ( INCLUDE_vTaskDelete == 1 )
 
 
        void vTaskDelete( xTaskHandle pxTaskToDelete )
        void vTaskDelete( xTaskHandle pxTaskToDelete )
        {
        {
        tskTCB *pxTCB;
        tskTCB *pxTCB;
 
 
                taskENTER_CRITICAL();
                taskENTER_CRITICAL();
                {
                {
                        /* Ensure a yield is performed if the current task is being
                        /* Ensure a yield is performed if the current task is being
                        deleted. */
                        deleted. */
                        if( pxTaskToDelete == pxCurrentTCB )
                        if( pxTaskToDelete == pxCurrentTCB )
                        {
                        {
                                pxTaskToDelete = NULL;
                                pxTaskToDelete = NULL;
                        }
                        }
 
 
                        /* If null is passed in here then we are deleting ourselves. */
                        /* If null is passed in here then we are deleting ourselves. */
                        pxTCB = prvGetTCBFromHandle( pxTaskToDelete );
                        pxTCB = prvGetTCBFromHandle( pxTaskToDelete );
 
 
                        /* Remove task from the ready list and place in the     termination list.
                        /* Remove task from the ready list and place in the     termination list.
                        This will stop the task from be scheduled.  The idle task will check
                        This will stop the task from be scheduled.  The idle task will check
                        the termination list and free up any memory allocated by the
                        the termination list and free up any memory allocated by the
                        scheduler for the TCB and stack. */
                        scheduler for the TCB and stack. */
                        vListRemove( &( pxTCB->xGenericListItem ) );
                        vListRemove( &( pxTCB->xGenericListItem ) );
 
 
                        /* Is the task waiting on an event also? */
                        /* Is the task waiting on an event also? */
                        if( pxTCB->xEventListItem.pvContainer )
                        if( pxTCB->xEventListItem.pvContainer )
                        {
                        {
                                vListRemove( &( pxTCB->xEventListItem ) );
                                vListRemove( &( pxTCB->xEventListItem ) );
                        }
                        }
 
 
                        vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );
                        vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );
 
 
                        /* Increment the ucTasksDeleted variable so the idle task knows
                        /* Increment the ucTasksDeleted variable so the idle task knows
                        there is a task that has been deleted and that it should therefore
                        there is a task that has been deleted and that it should therefore
                        check the xTasksWaitingTermination list. */
                        check the xTasksWaitingTermination list. */
                        ++uxTasksDeleted;
                        ++uxTasksDeleted;
                }
                }
                taskEXIT_CRITICAL();
                taskEXIT_CRITICAL();
 
 
                /* Force a reschedule if we have just deleted the current task. */
                /* Force a reschedule if we have just deleted the current task. */
                if( xSchedulerRunning != pdFALSE )
                if( xSchedulerRunning != pdFALSE )
                {
                {
                        if( ( void * ) pxTaskToDelete == NULL )
                        if( ( void * ) pxTaskToDelete == NULL )
                        {
                        {
                                taskYIELD();
                                taskYIELD();
                        }
                        }
                }
                }
        }
        }
 
 
#endif
#endif
 
 
 
 
 
 
 
 
 
 
 
 
/*-----------------------------------------------------------
/*-----------------------------------------------------------
 * TASK CONTROL API documented in task.h
 * TASK CONTROL API documented in task.h
 *----------------------------------------------------------*/
 *----------------------------------------------------------*/
 
 
#if ( INCLUDE_vTaskDelayUntil == 1 )
#if ( INCLUDE_vTaskDelayUntil == 1 )
 
 
        void vTaskDelayUntil( portTickType *pxPreviousWakeTime, portTickType xTimeIncrement )
        void vTaskDelayUntil( portTickType *pxPreviousWakeTime, portTickType xTimeIncrement )
        {
        {
        portTickType xTimeToWake;
        portTickType xTimeToWake;
        portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;
        portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;
 
 
                vTaskSuspendAll();
                vTaskSuspendAll();
                {
                {
                        /* Generate the tick time at which the task wants to wake. */
                        /* Generate the tick time at which the task wants to wake. */
                        xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
                        xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
 
 
                        if( xTickCount < *pxPreviousWakeTime )
                        if( xTickCount < *pxPreviousWakeTime )
                        {
                        {
                                /* The tick count has overflowed since this function was
                                /* The tick count has overflowed since this function was
                                lasted called.  In this case the only time we should ever
                                lasted called.  In this case the only time we should ever
                                actually delay is if the wake time has also     overflowed,
                                actually delay is if the wake time has also     overflowed,
                                and the wake time is greater than the tick time.  When this
                                and the wake time is greater than the tick time.  When this
                                is the case it is as if neither time had overflowed. */
                                is the case it is as if neither time had overflowed. */
                                if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xTickCount ) )
                                if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xTickCount ) )
                                {
                                {
                                        xShouldDelay = pdTRUE;
                                        xShouldDelay = pdTRUE;
                                }
                                }
                        }
                        }
                        else
                        else
                        {
                        {
                                /* The tick time has not overflowed.  In this case we will
                                /* The tick time has not overflowed.  In this case we will
                                delay if either the wake time has overflowed, and/or the
                                delay if either the wake time has overflowed, and/or the
                                tick time is less than the wake time. */
                                tick time is less than the wake time. */
                                if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xTickCount ) )
                                if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xTickCount ) )
                                {
                                {
                                        xShouldDelay = pdTRUE;
                                        xShouldDelay = pdTRUE;
                                }
                                }
                        }
                        }
 
 
                        /* Update the wake time ready for the next call. */
                        /* Update the wake time ready for the next call. */
                        *pxPreviousWakeTime = xTimeToWake;
                        *pxPreviousWakeTime = xTimeToWake;
 
 
                        if( xShouldDelay )
                        if( xShouldDelay )
                        {
                        {
                                /* We must remove ourselves from the ready list before adding
                                /* We must remove ourselves from the ready list before adding
                                ourselves to the blocked list as the same list item is used for
                                ourselves to the blocked list as the same list item is used for
                                both lists. */
                                both lists. */
                                vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
 
 
                                /* The list item will be inserted in wake time order. */
                                /* The list item will be inserted in wake time order. */
                                listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
                                listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
 
 
                                if( xTimeToWake < xTickCount )
                                if( xTimeToWake < xTickCount )
                                {
                                {
                                        /* Wake time has overflowed.  Place this item in the
                                        /* Wake time has overflowed.  Place this item in the
                                        overflow list. */
                                        overflow list. */
                                        vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                        vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                }
                                }
                                else
                                else
                                {
                                {
                                        /* The wake time has not overflowed, so we can use the
                                        /* The wake time has not overflowed, so we can use the
                                        current block list. */
                                        current block list. */
                                        vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                        vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                }
                                }
                        }
                        }
                }
                }
                xAlreadyYielded = xTaskResumeAll();
                xAlreadyYielded = xTaskResumeAll();
 
 
                /* Force a reschedule if xTaskResumeAll has not already done so, we may
                /* Force a reschedule if xTaskResumeAll has not already done so, we may
                have put ourselves to sleep. */
                have put ourselves to sleep. */
                if( !xAlreadyYielded )
                if( !xAlreadyYielded )
                {
                {
                        taskYIELD();
                        taskYIELD();
                }
                }
        }
        }
 
 
#endif
#endif
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( INCLUDE_vTaskDelay == 1 )
#if ( INCLUDE_vTaskDelay == 1 )
 
 
        void vTaskDelay( portTickType xTicksToDelay )
        void vTaskDelay( portTickType xTicksToDelay )
        {
        {
        portTickType xTimeToWake;
        portTickType xTimeToWake;
        signed portBASE_TYPE xAlreadyYielded = pdFALSE;
        signed portBASE_TYPE xAlreadyYielded = pdFALSE;
 
 
                /* A delay time of zero just forces a reschedule. */
                /* A delay time of zero just forces a reschedule. */
                if( xTicksToDelay > ( portTickType ) 0 )
                if( xTicksToDelay > ( portTickType ) 0 )
                {
                {
                        vTaskSuspendAll();
                        vTaskSuspendAll();
                        {
                        {
                                /* A task that is removed from the event list while the
                                /* A task that is removed from the event list while the
                                scheduler is suspended will not get placed in the ready
                                scheduler is suspended will not get placed in the ready
                                list or removed from the blocked list until the scheduler
                                list or removed from the blocked list until the scheduler
                                is resumed.
                                is resumed.
 
 
                                This task cannot be in an event list as it is the currently
                                This task cannot be in an event list as it is the currently
                                executing task. */
                                executing task. */
 
 
                                /* Calculate the time to wake - this may overflow but this is
                                /* Calculate the time to wake - this may overflow but this is
                                not a problem. */
                                not a problem. */
                                xTimeToWake = xTickCount + xTicksToDelay;
                                xTimeToWake = xTickCount + xTicksToDelay;
 
 
                                /* We must remove ourselves from the ready list before adding
                                /* We must remove ourselves from the ready list before adding
                                ourselves to the blocked list as the same list item is used for
                                ourselves to the blocked list as the same list item is used for
                                both lists. */
                                both lists. */
                                vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
 
 
                                /* The list item will be inserted in wake time order. */
                                /* The list item will be inserted in wake time order. */
                                listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
                                listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
 
 
                                if( xTimeToWake < xTickCount )
                                if( xTimeToWake < xTickCount )
                                {
                                {
                                        /* Wake time has overflowed.  Place this item in the
                                        /* Wake time has overflowed.  Place this item in the
                                        overflow list. */
                                        overflow list. */
                                        vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                        vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                }
                                }
                                else
                                else
                                {
                                {
                                        /* The wake time has not overflowed, so we can use the
                                        /* The wake time has not overflowed, so we can use the
                                        current block list. */
                                        current block list. */
                                        vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                        vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                }
                                }
                        }
                        }
                        xAlreadyYielded = xTaskResumeAll();
                        xAlreadyYielded = xTaskResumeAll();
                }
                }
 
 
                /* Force a reschedule if xTaskResumeAll has not already done so, we may
                /* Force a reschedule if xTaskResumeAll has not already done so, we may
                have put ourselves to sleep. */
                have put ourselves to sleep. */
                if( !xAlreadyYielded )
                if( !xAlreadyYielded )
                {
                {
                        taskYIELD();
                        taskYIELD();
                }
                }
        }
        }
 
 
#endif
#endif
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( INCLUDE_uxTaskPriorityGet == 1 )
#if ( INCLUDE_uxTaskPriorityGet == 1 )
 
 
        unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )
        unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )
        {
        {
        tskTCB *pxTCB;
        tskTCB *pxTCB;
        unsigned portBASE_TYPE uxReturn;
        unsigned portBASE_TYPE uxReturn;
 
 
                taskENTER_CRITICAL();
                taskENTER_CRITICAL();
                {
                {
                        /* If null is passed in here then we are changing the
                        /* If null is passed in here then we are changing the
                        priority of the calling function. */
                        priority of the calling function. */
                        pxTCB = prvGetTCBFromHandle( pxTask );
                        pxTCB = prvGetTCBFromHandle( pxTask );
                        uxReturn = pxTCB->uxPriority;
                        uxReturn = pxTCB->uxPriority;
                }
                }
                taskEXIT_CRITICAL();
                taskEXIT_CRITICAL();
 
 
                return uxReturn;
                return uxReturn;
        }
        }
 
 
#endif
#endif
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( INCLUDE_vTaskPrioritySet == 1 )
#if ( INCLUDE_vTaskPrioritySet == 1 )
 
 
        void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )
        void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )
        {
        {
        tskTCB *pxTCB;
        tskTCB *pxTCB;
        unsigned portBASE_TYPE uxCurrentPriority, xYieldRequired = pdFALSE;
        unsigned portBASE_TYPE uxCurrentPriority, xYieldRequired = pdFALSE;
 
 
                /* Ensure the new priority is valid. */
                /* Ensure the new priority is valid. */
                if( uxNewPriority >= configMAX_PRIORITIES )
                if( uxNewPriority >= configMAX_PRIORITIES )
                {
                {
                        uxNewPriority = configMAX_PRIORITIES - 1;
                        uxNewPriority = configMAX_PRIORITIES - 1;
                }
                }
 
 
                taskENTER_CRITICAL();
                taskENTER_CRITICAL();
                {
                {
                        /* If null is passed in here then we are changing the
                        /* If null is passed in here then we are changing the
                        priority of the calling function. */
                        priority of the calling function. */
                        pxTCB = prvGetTCBFromHandle( pxTask );
                        pxTCB = prvGetTCBFromHandle( pxTask );
                        uxCurrentPriority = pxTCB->uxPriority;
                        uxCurrentPriority = pxTCB->uxPriority;
 
 
                        if( uxCurrentPriority != uxNewPriority )
                        if( uxCurrentPriority != uxNewPriority )
                        {
                        {
                                /* The priority change may have readied a task of higher
                                /* The priority change may have readied a task of higher
                                priority than the calling task. */
                                priority than the calling task. */
                                if( uxNewPriority > pxCurrentTCB->uxPriority )
                                if( uxNewPriority > pxCurrentTCB->uxPriority )
                                {
                                {
                                        if( pxTask != NULL )
                                        if( pxTask != NULL )
                                        {
                                        {
                                                /* The priority of another task is being raised.  If we
                                                /* The priority of another task is being raised.  If we
                                                were raising the priority of the currently running task
                                                were raising the priority of the currently running task
                                                there would be no need to switch as it must have already
                                                there would be no need to switch as it must have already
                                                been the highest priority task. */
                                                been the highest priority task. */
                                                xYieldRequired = pdTRUE;
                                                xYieldRequired = pdTRUE;
                                        }
                                        }
                                }
                                }
                                else if( pxTask == NULL )
                                else if( pxTask == NULL )
                                {
                                {
                                        /* Setting our own priority down means there may now be another
                                        /* Setting our own priority down means there may now be another
                                        task of higher priority that is ready to execute. */
                                        task of higher priority that is ready to execute. */
                                        xYieldRequired = pdTRUE;
                                        xYieldRequired = pdTRUE;
                                }
                                }
 
 
                                pxTCB->uxPriority = uxNewPriority;
                                pxTCB->uxPriority = uxNewPriority;
                                listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxNewPriority );
                                listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxNewPriority );
 
 
                                /* If the task is in the blocked or suspended list we need do
                                /* If the task is in the blocked or suspended list we need do
                                nothing more than change it's priority variable. However, if
                                nothing more than change it's priority variable. However, if
                                the task is in a ready list it needs to be removed and placed
                                the task is in a ready list it needs to be removed and placed
                                in the queue appropriate to its new priority. */
                                in the queue appropriate to its new priority. */
                                if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )
                                if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )
                                {
                                {
                                        /* The task is currently in its ready list - remove before adding
                                        /* The task is currently in its ready list - remove before adding
                                        it to it's new ready list.  As we are in a critical section we
                                        it to it's new ready list.  As we are in a critical section we
                                        can do this even if the scheduler is suspended. */
                                        can do this even if the scheduler is suspended. */
                                        vListRemove( &( pxTCB->xGenericListItem ) );
                                        vListRemove( &( pxTCB->xGenericListItem ) );
                                        prvAddTaskToReadyQueue( pxTCB );
                                        prvAddTaskToReadyQueue( pxTCB );
                                }
                                }
 
 
                                if( xYieldRequired == pdTRUE )
                                if( xYieldRequired == pdTRUE )
                                {
                                {
                                        taskYIELD();
                                        taskYIELD();
                                }
                                }
                        }
                        }
                }
                }
                taskEXIT_CRITICAL();
                taskEXIT_CRITICAL();
        }
        }
 
 
#endif
#endif
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( INCLUDE_vTaskSuspend == 1 )
#if ( INCLUDE_vTaskSuspend == 1 )
 
 
        void vTaskSuspend( xTaskHandle pxTaskToSuspend )
        void vTaskSuspend( xTaskHandle pxTaskToSuspend )
        {
        {
        tskTCB *pxTCB;
        tskTCB *pxTCB;
 
 
                taskENTER_CRITICAL();
                taskENTER_CRITICAL();
                {
                {
                        /* Ensure a yield is performed if the current task is being
                        /* Ensure a yield is performed if the current task is being
                        suspended. */
                        suspended. */
                        if( pxTaskToSuspend == pxCurrentTCB )
                        if( pxTaskToSuspend == pxCurrentTCB )
                        {
                        {
                                pxTaskToSuspend = NULL;
                                pxTaskToSuspend = NULL;
                        }
                        }
 
 
                        /* If null is passed in here then we are suspending ourselves. */
                        /* If null is passed in here then we are suspending ourselves. */
                        pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
                        pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
 
 
                        /* Remove task from the ready/delayed list and place in the     suspended list. */
                        /* Remove task from the ready/delayed list and place in the     suspended list. */
                        vListRemove( &( pxTCB->xGenericListItem ) );
                        vListRemove( &( pxTCB->xGenericListItem ) );
 
 
                        /* Is the task waiting on an event also? */
                        /* Is the task waiting on an event also? */
                        if( pxTCB->xEventListItem.pvContainer )
                        if( pxTCB->xEventListItem.pvContainer )
                        {
                        {
                                vListRemove( &( pxTCB->xEventListItem ) );
                                vListRemove( &( pxTCB->xEventListItem ) );
                        }
                        }
 
 
                        vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
                        vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
                }
                }
                taskEXIT_CRITICAL();
                taskEXIT_CRITICAL();
 
 
                /* We may have just suspended the current task. */
                /* We may have just suspended the current task. */
                if( ( void * ) pxTaskToSuspend == NULL )
                if( ( void * ) pxTaskToSuspend == NULL )
                {
                {
                        taskYIELD();
                        taskYIELD();
                }
                }
        }
        }
 
 
#endif
#endif
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( INCLUDE_vTaskSuspend == 1 )
#if ( INCLUDE_vTaskSuspend == 1 )
 
 
        void vTaskResume( xTaskHandle pxTaskToResume )
        void vTaskResume( xTaskHandle pxTaskToResume )
        {
        {
        tskTCB *pxTCB;
        tskTCB *pxTCB;
 
 
                /* Remove the task from whichever list it is currently in, and place
                /* Remove the task from whichever list it is currently in, and place
                it in the ready list. */
                it in the ready list. */
                pxTCB = ( tskTCB * ) pxTaskToResume;
                pxTCB = ( tskTCB * ) pxTaskToResume;
 
 
                /* The parameter cannot be NULL as it is impossible to resume the
                /* The parameter cannot be NULL as it is impossible to resume the
                currently executing task. */
                currently executing task. */
                if( pxTCB != NULL )
                if( pxTCB != NULL )
                {
                {
                        taskENTER_CRITICAL();
                        taskENTER_CRITICAL();
                        {
                        {
                                /* Is the task we are attempting to resume actually suspended? */
                                /* Is the task we are attempting to resume actually suspended? */
                                if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
                                if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
                                {
                                {
                                        /* Has the task already been resumed from within an ISR? */
                                        /* Has the task already been resumed from within an ISR? */
                                        if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
                                        if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
                                        {
                                        {
                                                /* As we are in a critical section we can access the ready
                                                /* As we are in a critical section we can access the ready
                                                lists even if the scheduler is suspended. */
                                                lists even if the scheduler is suspended. */
                                                vListRemove(  &( pxTCB->xGenericListItem ) );
                                                vListRemove(  &( pxTCB->xGenericListItem ) );
                                                prvAddTaskToReadyQueue( pxTCB );
                                                prvAddTaskToReadyQueue( pxTCB );
 
 
                                                /* We may have just resumed a higher priority task. */
                                                /* We may have just resumed a higher priority task. */
                                                if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
                                                if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
                                                {
                                                {
                                                        /* This yield may not cause the task just resumed to run, but
                                                        /* This yield may not cause the task just resumed to run, but
                                                        will leave the lists in the correct state for the next yield. */
                                                        will leave the lists in the correct state for the next yield. */
                                                        taskYIELD();
                                                        taskYIELD();
                                                }
                                                }
                                        }
                                        }
                                }
                                }
                        }
                        }
                        taskEXIT_CRITICAL();
                        taskEXIT_CRITICAL();
                }
                }
        }
        }
 
 
#endif
#endif
 
 
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
#if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
 
 
        portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )
        portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )
        {
        {
        portBASE_TYPE xYieldRequired = pdFALSE;
        portBASE_TYPE xYieldRequired = pdFALSE;
        tskTCB *pxTCB;
        tskTCB *pxTCB;
 
 
                pxTCB = ( tskTCB * ) pxTaskToResume;
                pxTCB = ( tskTCB * ) pxTaskToResume;
 
 
                /* Is the task we are attempting to resume actually suspended? */
                /* Is the task we are attempting to resume actually suspended? */
                if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
                if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
                {
                {
                        /* Has the task already been resumed from within an ISR? */
                        /* Has the task already been resumed from within an ISR? */
                        if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
                        if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
                        {
                        {
                                if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
                                if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
                                {
                                {
                                        xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );
                                        xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );
                                        vListRemove(  &( pxTCB->xGenericListItem ) );
                                        vListRemove(  &( pxTCB->xGenericListItem ) );
                                        prvAddTaskToReadyQueue( pxTCB );
                                        prvAddTaskToReadyQueue( pxTCB );
                                }
                                }
                                else
                                else
                                {
                                {
                                        /* We cannot access the delayed or ready lists, so will hold this
                                        /* We cannot access the delayed or ready lists, so will hold this
                                        task pending until the scheduler is resumed, at which point a
                                        task pending until the scheduler is resumed, at which point a
                                        yield will be preformed if necessary. */
                                        yield will be preformed if necessary. */
                                        vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
                                        vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
                                }
                                }
                        }
                        }
                }
                }
 
 
                return xYieldRequired;
                return xYieldRequired;
        }
        }
 
 
#endif
#endif
 
 
 
 
 
 
 
 
/*-----------------------------------------------------------
/*-----------------------------------------------------------
 * PUBLIC SCHEDULER CONTROL documented in task.h
 * PUBLIC SCHEDULER CONTROL documented in task.h
 *----------------------------------------------------------*/
 *----------------------------------------------------------*/
 
 
 
 
void vTaskStartScheduler( void )
void vTaskStartScheduler( void )
{
{
portBASE_TYPE xReturn;
portBASE_TYPE xReturn;
 
 
        /* Add the idle task at the lowest priority. */
        /* Add the idle task at the lowest priority. */
        xReturn = xTaskCreate( prvIdleTask, ( signed portCHAR * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
        xReturn = xTaskCreate( prvIdleTask, ( signed portCHAR * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
 
 
        if( xReturn == pdPASS )
        if( xReturn == pdPASS )
        {
        {
                /* Interrupts are turned off here, to ensure a tick does not occur
                /* Interrupts are turned off here, to ensure a tick does not occur
                before or during the call to xPortStartScheduler().  The stacks of
                before or during the call to xPortStartScheduler().  The stacks of
                the created tasks contain a status word with interrupts switched on
                the created tasks contain a status word with interrupts switched on
                so interrupts will automatically get re-enabled when the first task
                so interrupts will automatically get re-enabled when the first task
                starts to run.
                starts to run.
 
 
                STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE
                STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE
                DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */
                DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */
                portDISABLE_INTERRUPTS();
                portDISABLE_INTERRUPTS();
 
 
                xSchedulerRunning = pdTRUE;
                xSchedulerRunning = pdTRUE;
                xTickCount = ( portTickType ) 0;
                xTickCount = ( portTickType ) 0;
 
 
                /* Setting up the timer tick is hardware specific and thus in the
                /* Setting up the timer tick is hardware specific and thus in the
                portable interface. */
                portable interface. */
                if( xPortStartScheduler() )
                if( xPortStartScheduler() )
                {
                {
                        /* Should not reach here as if the scheduler is running the
                        /* Should not reach here as if the scheduler is running the
                        function will not return. */
                        function will not return. */
                }
                }
                else
                else
                {
                {
                        /* Should only reach here if a task calls xTaskEndScheduler(). */
                        /* Should only reach here if a task calls xTaskEndScheduler(). */
                }
                }
        }
        }
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
void vTaskEndScheduler( void )
void vTaskEndScheduler( void )
{
{
        /* Stop the scheduler interrupts and call the portable scheduler end
        /* Stop the scheduler interrupts and call the portable scheduler end
        routine so the original ISRs can be restored if necessary.  The port
        routine so the original ISRs can be restored if necessary.  The port
        layer must ensure interrupts enable     bit is left in the correct state. */
        layer must ensure interrupts enable     bit is left in the correct state. */
        portDISABLE_INTERRUPTS();
        portDISABLE_INTERRUPTS();
        xSchedulerRunning = pdFALSE;
        xSchedulerRunning = pdFALSE;
        vPortEndScheduler();
        vPortEndScheduler();
}
}
/*----------------------------------------------------------*/
/*----------------------------------------------------------*/
 
 
void vTaskSuspendAll( void )
void vTaskSuspendAll( void )
{
{
        portENTER_CRITICAL();
        portENTER_CRITICAL();
                ++uxSchedulerSuspended;
                ++uxSchedulerSuspended;
        portEXIT_CRITICAL();
        portEXIT_CRITICAL();
}
}
/*----------------------------------------------------------*/
/*----------------------------------------------------------*/
 
 
signed portBASE_TYPE xTaskResumeAll( void )
signed portBASE_TYPE xTaskResumeAll( void )
{
{
register tskTCB *pxTCB;
register tskTCB *pxTCB;
signed portBASE_TYPE xAlreadyYielded = pdFALSE;
signed portBASE_TYPE xAlreadyYielded = pdFALSE;
 
 
        /* It is possible that an ISR caused a task to be removed from an event
        /* It is possible that an ISR caused a task to be removed from an event
        list while the scheduler was suspended.  If this was the case then the
        list while the scheduler was suspended.  If this was the case then the
        removed task will have been added to the xPendingReadyList.  Once the
        removed task will have been added to the xPendingReadyList.  Once the
        scheduler has been resumed it is safe to move all the pending ready
        scheduler has been resumed it is safe to move all the pending ready
        tasks from this list into their appropriate ready list. */
        tasks from this list into their appropriate ready list. */
        portENTER_CRITICAL();
        portENTER_CRITICAL();
        {
        {
                --uxSchedulerSuspended;
                --uxSchedulerSuspended;
 
 
                if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
                if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
                {
                {
                        if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0 )
                        if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0 )
                        {
                        {
                                portBASE_TYPE xYieldRequired = pdFALSE;
                                portBASE_TYPE xYieldRequired = pdFALSE;
 
 
                                /* Move any readied tasks from the pending list into the
                                /* Move any readied tasks from the pending list into the
                                appropriate ready list. */
                                appropriate ready list. */
                                while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY(  ( ( xList * ) &xPendingReadyList ) ) ) != NULL )
                                while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY(  ( ( xList * ) &xPendingReadyList ) ) ) != NULL )
                                {
                                {
                                        vListRemove( &( pxTCB->xEventListItem ) );
                                        vListRemove( &( pxTCB->xEventListItem ) );
                                        vListRemove( &( pxTCB->xGenericListItem ) );
                                        vListRemove( &( pxTCB->xGenericListItem ) );
                                        prvAddTaskToReadyQueue( pxTCB );
                                        prvAddTaskToReadyQueue( pxTCB );
 
 
                                        /* If we have moved a task that has a priority higher than
                                        /* If we have moved a task that has a priority higher than
                                        the current task then we should yield. */
                                        the current task then we should yield. */
                                        if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
                                        if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
                                        {
                                        {
                                                xYieldRequired = pdTRUE;
                                                xYieldRequired = pdTRUE;
                                        }
                                        }
                                }
                                }
 
 
                                /* If any ticks occurred while the scheduler was suspended then
                                /* If any ticks occurred while the scheduler was suspended then
                                they should be processed now.  This ensures the tick count does not
                                they should be processed now.  This ensures the tick count does not
                                slip, and that any delayed tasks are resumed at the correct time. */
                                slip, and that any delayed tasks are resumed at the correct time. */
                                if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
                                if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
                                {
                                {
                                        while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
                                        while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
                                        {
                                        {
                                                vTaskIncrementTick();
                                                vTaskIncrementTick();
                                                --uxMissedTicks;
                                                --uxMissedTicks;
                                        }
                                        }
 
 
                                        /* As we have processed some ticks it is appropriate to yield
                                        /* As we have processed some ticks it is appropriate to yield
                                        to ensure the highest priority task that is ready to run is
                                        to ensure the highest priority task that is ready to run is
                                        the task actually running. */
                                        the task actually running. */
                                        xYieldRequired = pdTRUE;
                                        xYieldRequired = pdTRUE;
                                }
                                }
 
 
                                if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )
                                if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )
                                {
                                {
                                        xAlreadyYielded = pdTRUE;
                                        xAlreadyYielded = pdTRUE;
                                        xMissedYield = pdFALSE;
                                        xMissedYield = pdFALSE;
                                        taskYIELD();
                                        taskYIELD();
                                }
                                }
                        }
                        }
                }
                }
        }
        }
        portEXIT_CRITICAL();
        portEXIT_CRITICAL();
 
 
        return xAlreadyYielded;
        return xAlreadyYielded;
}
}
 
 
 
 
 
 
 
 
 
 
 
 
/*-----------------------------------------------------------
/*-----------------------------------------------------------
 * PUBLIC TASK UTILITIES documented in task.h
 * PUBLIC TASK UTILITIES documented in task.h
 *----------------------------------------------------------*/
 *----------------------------------------------------------*/
 
 
 
 
 
 
portTickType xTaskGetTickCount( void )
portTickType xTaskGetTickCount( void )
{
{
portTickType xTicks;
portTickType xTicks;
 
 
        /* Critical section required if running on a 16 bit processor. */
        /* Critical section required if running on a 16 bit processor. */
        taskENTER_CRITICAL();
        taskENTER_CRITICAL();
        {
        {
                xTicks = xTickCount;
                xTicks = xTickCount;
        }
        }
        taskEXIT_CRITICAL();
        taskEXIT_CRITICAL();
 
 
        return xTicks;
        return xTicks;
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
{
{
unsigned portBASE_TYPE uxNumberOfTasks;
unsigned portBASE_TYPE uxNumberOfTasks;
 
 
        taskENTER_CRITICAL();
        taskENTER_CRITICAL();
                uxNumberOfTasks = uxCurrentNumberOfTasks;
                uxNumberOfTasks = uxCurrentNumberOfTasks;
        taskEXIT_CRITICAL();
        taskEXIT_CRITICAL();
 
 
        return uxNumberOfTasks;
        return uxNumberOfTasks;
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_vTaskDelete == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_vTaskDelete == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
 
 
        void vTaskList( signed portCHAR *pcWriteBuffer )
        void vTaskList( signed portCHAR *pcWriteBuffer )
        {
        {
        unsigned portBASE_TYPE uxQueue;
        unsigned portBASE_TYPE uxQueue;
 
 
                /* This is a VERY costly function that should be used for debug only.
                /* This is a VERY costly function that should be used for debug only.
                It leaves interrupts disabled for a LONG time. */
                It leaves interrupts disabled for a LONG time. */
 
 
        vTaskSuspendAll();
        vTaskSuspendAll();
                {
                {
                        /* Run through all the lists that could potentially contain a TCB and
                        /* Run through all the lists that could potentially contain a TCB and
                        report the task name, state and stack high water mark. */
                        report the task name, state and stack high water mark. */
 
 
                        pcWriteBuffer[ 0 ] = ( signed portCHAR ) 0x00;
                        pcWriteBuffer[ 0 ] = ( signed portCHAR ) 0x00;
                        strcat( ( portCHAR * ) pcWriteBuffer, ( const portCHAR * ) "\r\n" );
                        strcat( ( portCHAR * ) pcWriteBuffer, ( const portCHAR * ) "\r\n" );
 
 
                        uxQueue = uxTopUsedPriority + 1;
                        uxQueue = uxTopUsedPriority + 1;
 
 
                        do
                        do
                        {
                        {
                                uxQueue--;
                                uxQueue--;
 
 
                                if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )
                                if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )
                                {
                                {
                                        prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
                                        prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
                                }
                                }
                        }while( uxQueue > ( unsigned portSHORT ) tskIDLE_PRIORITY );
                        }while( uxQueue > ( unsigned portSHORT ) tskIDLE_PRIORITY );
 
 
                        if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )
                        if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )
                        {
                        {
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
                        }
                        }
 
 
                        if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )
                        if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )
                        {
                        {
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
                        }
                        }
 
 
                        if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )
                        if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )
                        {
                        {
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );
                        }
                        }
 
 
                        if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
                        if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
                        {
                        {
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );
                        }
                        }
                }
                }
        xTaskResumeAll();
        xTaskResumeAll();
        }
        }
 
 
#endif
#endif
/*----------------------------------------------------------*/
/*----------------------------------------------------------*/
 
 
#if ( configUSE_TRACE_FACILITY == 1 )
#if ( configUSE_TRACE_FACILITY == 1 )
 
 
        void vTaskStartTrace( signed portCHAR * pcBuffer, unsigned portLONG ulBufferSize )
        void vTaskStartTrace( signed portCHAR * pcBuffer, unsigned portLONG ulBufferSize )
        {
        {
                portENTER_CRITICAL();
                portENTER_CRITICAL();
                {
                {
                        pcTraceBuffer = ( volatile signed portCHAR * volatile )pcBuffer;
                        pcTraceBuffer = ( volatile signed portCHAR * volatile )pcBuffer;
                        pcTraceBufferStart = pcBuffer;
                        pcTraceBufferStart = pcBuffer;
                        pcTraceBufferEnd = pcBuffer + ( ulBufferSize - tskSIZE_OF_EACH_TRACE_LINE );
                        pcTraceBufferEnd = pcBuffer + ( ulBufferSize - tskSIZE_OF_EACH_TRACE_LINE );
                        xTracing = pdTRUE;
                        xTracing = pdTRUE;
                }
                }
                portEXIT_CRITICAL();
                portEXIT_CRITICAL();
        }
        }
 
 
#endif
#endif
/*----------------------------------------------------------*/
/*----------------------------------------------------------*/
 
 
#if ( configUSE_TRACE_FACILITY == 1 )
#if ( configUSE_TRACE_FACILITY == 1 )
 
 
        unsigned portLONG ulTaskEndTrace( void )
        unsigned portLONG ulTaskEndTrace( void )
        {
        {
        unsigned portLONG ulBufferLength;
        unsigned portLONG ulBufferLength;
 
 
                portENTER_CRITICAL();
                portENTER_CRITICAL();
                        xTracing = pdFALSE;
                        xTracing = pdFALSE;
                portEXIT_CRITICAL();
                portEXIT_CRITICAL();
 
 
                ulBufferLength = ( unsigned portLONG ) ( pcTraceBuffer - pcTraceBufferStart );
                ulBufferLength = ( unsigned portLONG ) ( pcTraceBuffer - pcTraceBufferStart );
 
 
                return ulBufferLength;
                return ulBufferLength;
        }
        }
 
 
#endif
#endif
 
 
 
 
 
 
/*-----------------------------------------------------------
/*-----------------------------------------------------------
 * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
 * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
 * documented in task.h
 * documented in task.h
 *----------------------------------------------------------*/
 *----------------------------------------------------------*/
 
 
 
 
inline void vTaskIncrementTick( void )
inline void vTaskIncrementTick( void )
{
{
        /* Called by the portable layer each time a tick interrupt occurs.
        /* Called by the portable layer each time a tick interrupt occurs.
        Increments the tick then checks to see if the new tick value will cause any
        Increments the tick then checks to see if the new tick value will cause any
        tasks to be unblocked. */
        tasks to be unblocked. */
        if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
        if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
        {
        {
                ++xTickCount;
                ++xTickCount;
                if( xTickCount == ( portTickType ) 0 )
                if( xTickCount == ( portTickType ) 0 )
                {
                {
                        xList *pxTemp;
                        xList *pxTemp;
 
 
                        /* Tick count has overflowed so we need to swap the delay lists.
                        /* Tick count has overflowed so we need to swap the delay lists.
                        If there are any items in pxDelayedTaskList here then there is
                        If there are any items in pxDelayedTaskList here then there is
                        an error! */
                        an error! */
                        pxTemp = pxDelayedTaskList;
                        pxTemp = pxDelayedTaskList;
                        pxDelayedTaskList = pxOverflowDelayedTaskList;
                        pxDelayedTaskList = pxOverflowDelayedTaskList;
                        pxOverflowDelayedTaskList = pxTemp;
                        pxOverflowDelayedTaskList = pxTemp;
            xNumOfOverflows++;
            xNumOfOverflows++;
                }
                }
 
 
                /* See if this tick has made a timeout expire. */
                /* See if this tick has made a timeout expire. */
                prvCheckDelayedTasks();
                prvCheckDelayedTasks();
        }
        }
        else
        else
        {
        {
                ++uxMissedTicks;
                ++uxMissedTicks;
 
 
                /* The tick hook gets called at regular intervals, even if the
                /* The tick hook gets called at regular intervals, even if the
                scheduler is locked. */
                scheduler is locked. */
                #if ( configUSE_TICK_HOOK == 1 )
                #if ( configUSE_TICK_HOOK == 1 )
                {
                {
                        extern void vApplicationTickHook( void );
                        extern void vApplicationTickHook( void );
 
 
                        vApplicationTickHook();
                        vApplicationTickHook();
                }
                }
                #endif
                #endif
        }
        }
 
 
        #if ( configUSE_TICK_HOOK == 1 )
        #if ( configUSE_TICK_HOOK == 1 )
        {
        {
                extern void vApplicationTickHook( void );
                extern void vApplicationTickHook( void );
 
 
                /* Guard against the tick hook being called when the missed tick
                /* Guard against the tick hook being called when the missed tick
                count is being unwound (when the scheduler is being unlocked. */
                count is being unwound (when the scheduler is being unlocked. */
                if( uxMissedTicks == 0 )
                if( uxMissedTicks == 0 )
                {
                {
                        vApplicationTickHook();
                        vApplicationTickHook();
                }
                }
        }
        }
        #endif
        #endif
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( ( INCLUDE_vTaskCleanUpResources == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
#if ( ( INCLUDE_vTaskCleanUpResources == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
 
 
        void vTaskCleanUpResources( void )
        void vTaskCleanUpResources( void )
        {
        {
        unsigned portSHORT usQueue;
        unsigned portSHORT usQueue;
        volatile tskTCB *pxTCB;
        volatile tskTCB *pxTCB;
 
 
                usQueue = ( unsigned portSHORT ) uxTopUsedPriority + ( unsigned portSHORT ) 1;
                usQueue = ( unsigned portSHORT ) uxTopUsedPriority + ( unsigned portSHORT ) 1;
 
 
                /* Remove any TCB's from the ready queues. */
                /* Remove any TCB's from the ready queues. */
                do
                do
                {
                {
                        usQueue--;
                        usQueue--;
 
 
                        while( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ usQueue ] ) ) )
                        while( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ usQueue ] ) ) )
                        {
                        {
                                listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &( pxReadyTasksLists[ usQueue ] ) );
                                listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &( pxReadyTasksLists[ usQueue ] ) );
                                vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
                                vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
 
 
                                prvDeleteTCB( ( tskTCB * ) pxTCB );
                                prvDeleteTCB( ( tskTCB * ) pxTCB );
                        }
                        }
                }while( usQueue > ( unsigned portSHORT ) tskIDLE_PRIORITY );
                }while( usQueue > ( unsigned portSHORT ) tskIDLE_PRIORITY );
 
 
                /* Remove any TCB's from the delayed queue. */
                /* Remove any TCB's from the delayed queue. */
                while( !listLIST_IS_EMPTY( &xDelayedTaskList1 ) )
                while( !listLIST_IS_EMPTY( &xDelayedTaskList1 ) )
                {
                {
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList1 );
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList1 );
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
 
 
                        prvDeleteTCB( ( tskTCB * ) pxTCB );
                        prvDeleteTCB( ( tskTCB * ) pxTCB );
                }
                }
 
 
                /* Remove any TCB's from the overflow delayed queue. */
                /* Remove any TCB's from the overflow delayed queue. */
                while( !listLIST_IS_EMPTY( &xDelayedTaskList2 ) )
                while( !listLIST_IS_EMPTY( &xDelayedTaskList2 ) )
                {
                {
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList2 );
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList2 );
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
 
 
                        prvDeleteTCB( ( tskTCB * ) pxTCB );
                        prvDeleteTCB( ( tskTCB * ) pxTCB );
                }
                }
 
 
                while( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
                while( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
                {
                {
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xSuspendedTaskList );
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xSuspendedTaskList );
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
 
 
                        prvDeleteTCB( ( tskTCB * ) pxTCB );
                        prvDeleteTCB( ( tskTCB * ) pxTCB );
                }
                }
 
 
                while( !listLIST_IS_EMPTY( &xPendingReadyList ) )
                while( !listLIST_IS_EMPTY( &xPendingReadyList ) )
                {
                {
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xPendingReadyList );
                        listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xPendingReadyList );
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
                        vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
 
 
                        prvDeleteTCB( ( tskTCB * ) pxTCB );
                        prvDeleteTCB( ( tskTCB * ) pxTCB );
                }
                }
        }
        }
 
 
#endif
#endif
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
void vTaskSwitchContext( void )
void vTaskSwitchContext( void )
{
{
        if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )
        if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )
        {
        {
                /* The scheduler is currently suspended - do not allow a context
                /* The scheduler is currently suspended - do not allow a context
                switch. */
                switch. */
                xMissedYield = pdTRUE;
                xMissedYield = pdTRUE;
                return;
                return;
        }
        }
 
 
        /* Find the highest priority queue that contains ready tasks. */
        /* Find the highest priority queue that contains ready tasks. */
        while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )
        while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )
        {
        {
                --uxTopReadyPriority;
                --uxTopReadyPriority;
        }
        }
 
 
        /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the
        /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the
        same priority get an equal share of the processor time. */
        same priority get an equal share of the processor time. */
        listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );
        listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );
        vWriteTraceToBuffer();
        vWriteTraceToBuffer();
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
void vTaskPlaceOnEventList( xList *pxEventList, portTickType xTicksToWait )
void vTaskPlaceOnEventList( xList *pxEventList, portTickType xTicksToWait )
{
{
portTickType xTimeToWake;
portTickType xTimeToWake;
 
 
        /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
        /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
        SCHEDULER SUSPENDED. */
        SCHEDULER SUSPENDED. */
 
 
        /* Place the event list item of the TCB in the appropriate event list.
        /* Place the event list item of the TCB in the appropriate event list.
        This is placed in the list in priority order so the highest priority task
        This is placed in the list in priority order so the highest priority task
        is the first to be woken by the event. */
        is the first to be woken by the event. */
        vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
        vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
 
 
        /* We must remove ourselves from the ready list before adding ourselves
        /* We must remove ourselves from the ready list before adding ourselves
        to the blocked list as the same list item is used for both lists.  We have
        to the blocked list as the same list item is used for both lists.  We have
        exclusive access to the ready lists as the scheduler is locked. */
        exclusive access to the ready lists as the scheduler is locked. */
        vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
        vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
 
 
 
 
        #if ( INCLUDE_vTaskSuspend == 1 )
        #if ( INCLUDE_vTaskSuspend == 1 )
        {
        {
                if( xTicksToWait == portMAX_DELAY )
                if( xTicksToWait == portMAX_DELAY )
                {
                {
                        /* Add ourselves to the suspended task list instead of a delayed task
                        /* Add ourselves to the suspended task list instead of a delayed task
                        list to ensure we are not woken by a timing event.  We will block
                        list to ensure we are not woken by a timing event.  We will block
                        indefinitely. */
                        indefinitely. */
                        vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                        vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                }
                }
                else
                else
                {
                {
                        /* Calculate the time at which the task should be woken if the event does
                        /* Calculate the time at which the task should be woken if the event does
                        not occur.  This may overflow but this doesn't matter. */
                        not occur.  This may overflow but this doesn't matter. */
                        xTimeToWake = xTickCount + xTicksToWait;
                        xTimeToWake = xTickCount + xTicksToWait;
 
 
                        listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
                        listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
 
 
                        if( xTimeToWake < xTickCount )
                        if( xTimeToWake < xTickCount )
                        {
                        {
                                /* Wake time has overflowed.  Place this item in the overflow list. */
                                /* Wake time has overflowed.  Place this item in the overflow list. */
                                vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                        }
                        }
                        else
                        else
                        {
                        {
                                /* The wake time has not overflowed, so we can use the current block list. */
                                /* The wake time has not overflowed, so we can use the current block list. */
                                vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                        }
                        }
                }
                }
        }
        }
        #else
        #else
        {
        {
                        /* Calculate the time at which the task should be woken if the event does
                        /* Calculate the time at which the task should be woken if the event does
                        not occur.  This may overflow but this doesn't matter. */
                        not occur.  This may overflow but this doesn't matter. */
                        xTimeToWake = xTickCount + xTicksToWait;
                        xTimeToWake = xTickCount + xTicksToWait;
 
 
                        listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
                        listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
 
 
                        if( xTimeToWake < xTickCount )
                        if( xTimeToWake < xTickCount )
                        {
                        {
                                /* Wake time has overflowed.  Place this item in the overflow list. */
                                /* Wake time has overflowed.  Place this item in the overflow list. */
                                vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                        }
                        }
                        else
                        else
                        {
                        {
                                /* The wake time has not overflowed, so we can use the current block list. */
                                /* The wake time has not overflowed, so we can use the current block list. */
                                vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                                vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
                        }
                        }
        }
        }
        #endif
        #endif
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
signed portBASE_TYPE xTaskRemoveFromEventList( const xList *pxEventList )
signed portBASE_TYPE xTaskRemoveFromEventList( const xList *pxEventList )
{
{
tskTCB *pxUnblockedTCB;
tskTCB *pxUnblockedTCB;
portBASE_TYPE xReturn;
portBASE_TYPE xReturn;
 
 
        /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
        /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
        SCHEDULER SUSPENDED.  It can also be called from within an ISR. */
        SCHEDULER SUSPENDED.  It can also be called from within an ISR. */
 
 
        /* The event list is sorted in priority order, so we can remove the
        /* The event list is sorted in priority order, so we can remove the
        first in the list, remove the TCB from the delayed list, and add
        first in the list, remove the TCB from the delayed list, and add
        it to the ready list.
        it to the ready list.
 
 
        If an event is for a queue that is locked then this function will never
        If an event is for a queue that is locked then this function will never
        get called - the lock count on the queue will get modified instead.  This
        get called - the lock count on the queue will get modified instead.  This
        means we can always expect exclusive access to the event list here. */
        means we can always expect exclusive access to the event list here. */
        pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
        pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
        vListRemove( &( pxUnblockedTCB->xEventListItem ) );
        vListRemove( &( pxUnblockedTCB->xEventListItem ) );
 
 
        if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
        if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
        {
        {
                vListRemove( &( pxUnblockedTCB->xGenericListItem ) );
                vListRemove( &( pxUnblockedTCB->xGenericListItem ) );
                prvAddTaskToReadyQueue( pxUnblockedTCB );
                prvAddTaskToReadyQueue( pxUnblockedTCB );
        }
        }
        else
        else
        {
        {
                /* We cannot access the delayed or ready lists, so will hold this
                /* We cannot access the delayed or ready lists, so will hold this
                task pending until the scheduler is resumed. */
                task pending until the scheduler is resumed. */
                vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
                vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
        }
        }
 
 
        if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )
        if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )
        {
        {
                /* Return true if the task removed from the event list has
                /* Return true if the task removed from the event list has
                a higher priority than the calling task.  This allows
                a higher priority than the calling task.  This allows
                the calling task to know if it should force a context
                the calling task to know if it should force a context
                switch now. */
                switch now. */
                xReturn = pdTRUE;
                xReturn = pdTRUE;
        }
        }
        else
        else
        {
        {
                xReturn = pdFALSE;
                xReturn = pdFALSE;
        }
        }
 
 
        return xReturn;
        return xReturn;
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
void vTaskSetTimeOutState( xTimeOutType *pxTimeOut )
void vTaskSetTimeOutState( xTimeOutType *pxTimeOut )
{
{
    pxTimeOut->xOverflowCount = xNumOfOverflows;
    pxTimeOut->xOverflowCount = xNumOfOverflows;
    pxTimeOut->xTimeOnEntering = xTickCount;
    pxTimeOut->xTimeOnEntering = xTickCount;
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType *pxTimeOut, portTickType *pxTicksToWait )
portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType *pxTimeOut, portTickType *pxTicksToWait )
{
{
portBASE_TYPE xReturn;
portBASE_TYPE xReturn;
 
 
    if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xTickCount >= pxTimeOut->xTimeOnEntering ) )
    if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xTickCount >= pxTimeOut->xTimeOnEntering ) )
    {
    {
        /* The tick count is greater than the time at which vTaskSetTimeout()
        /* The tick count is greater than the time at which vTaskSetTimeout()
                was called, but has also overflowed since vTaskSetTimeOut() was called.
                was called, but has also overflowed since vTaskSetTimeOut() was called.
        It must have wrapped all the way around and gone past us again. This
        It must have wrapped all the way around and gone past us again. This
        passed since vTaskSetTimeout() was called. */
        passed since vTaskSetTimeout() was called. */
        xReturn = pdTRUE;
        xReturn = pdTRUE;
    }
    }
    else if( ( xTickCount - pxTimeOut->xTimeOnEntering ) < *pxTicksToWait )
    else if( ( xTickCount - pxTimeOut->xTimeOnEntering ) < *pxTicksToWait )
    {
    {
        /* Not a genuine timeout. Adjust parameters for time remaining. */
        /* Not a genuine timeout. Adjust parameters for time remaining. */
        *pxTicksToWait -= ( xTickCount - pxTimeOut->xTimeOnEntering );
        *pxTicksToWait -= ( xTickCount - pxTimeOut->xTimeOnEntering );
        vTaskSetTimeOutState( pxTimeOut );
        vTaskSetTimeOutState( pxTimeOut );
        xReturn = pdFALSE;
        xReturn = pdFALSE;
    }
    }
    else
    else
    {
    {
        xReturn = pdTRUE;
        xReturn = pdTRUE;
    }
    }
 
 
    return xReturn;
    return xReturn;
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
void vTaskMissedYield( void )
void vTaskMissedYield( void )
{
{
        xMissedYield = pdTRUE;
        xMissedYield = pdTRUE;
}
}
 
 
/*
/*
 * -----------------------------------------------------------
 * -----------------------------------------------------------
 * The Idle task.
 * The Idle task.
 * ----------------------------------------------------------
 * ----------------------------------------------------------
 *
 *
 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
 * language extensions.  The equivalent prototype for this function is:
 * language extensions.  The equivalent prototype for this function is:
 *
 *
 * void prvIdleTask( void *pvParameters );
 * void prvIdleTask( void *pvParameters );
 *
 *
 */
 */
static portTASK_FUNCTION( prvIdleTask, pvParameters )
static portTASK_FUNCTION( prvIdleTask, pvParameters )
{
{
        /* Stop warnings. */
        /* Stop warnings. */
        ( void ) pvParameters;
        ( void ) pvParameters;
 
 
        for( ;; )
        for( ;; )
        {
        {
                /* See if any tasks have been deleted. */
                /* See if any tasks have been deleted. */
                prvCheckTasksWaitingTermination();
                prvCheckTasksWaitingTermination();
 
 
                #if ( configUSE_PREEMPTION == 0 )
                #if ( configUSE_PREEMPTION == 0 )
                {
                {
                        /* If we are not using preemption we keep forcing a task switch to
                        /* If we are not using preemption we keep forcing a task switch to
                        see if any other task has become available.  If we are using
                        see if any other task has become available.  If we are using
                        preemption we don't need to do this as any task becoming available
                        preemption we don't need to do this as any task becoming available
                        will automatically get the processor anyway. */
                        will automatically get the processor anyway. */
                        taskYIELD();
                        taskYIELD();
                }
                }
                #endif
                #endif
 
 
                #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
                #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
                {
                {
                        /* When using preemption tasks of equal priority will be
                        /* When using preemption tasks of equal priority will be
                        timesliced.  If a task that is sharing the idle priority is ready
                        timesliced.  If a task that is sharing the idle priority is ready
                        to run then the idle task should yield before the end of the
                        to run then the idle task should yield before the end of the
                        timeslice.
                        timeslice.
 
 
                        A critical region is not required here as we are just reading from
                        A critical region is not required here as we are just reading from
                        the list, and an occasional incorrect value will not matter.  If
                        the list, and an occasional incorrect value will not matter.  If
                        the ready list at the idle priority contains more than one task
                        the ready list at the idle priority contains more than one task
                        then a task other than the idle task is ready to execute. */
                        then a task other than the idle task is ready to execute. */
                        if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( unsigned portBASE_TYPE ) 1 )
                        if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( unsigned portBASE_TYPE ) 1 )
                        {
                        {
                                taskYIELD();
                                taskYIELD();
                        }
                        }
                }
                }
                #endif
                #endif
 
 
                #if ( configUSE_IDLE_HOOK == 1 )
                #if ( configUSE_IDLE_HOOK == 1 )
                {
                {
                        extern void vApplicationIdleHook( void );
                        extern void vApplicationIdleHook( void );
 
 
                        /* Call the user defined function from within the idle task.  This
                        /* Call the user defined function from within the idle task.  This
                        allows the application designer to add background functionality
                        allows the application designer to add background functionality
                        without the overhead of a separate task.
                        without the overhead of a separate task.
                        NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
                        NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
                        CALL A FUNCTION THAT MIGHT BLOCK. */
                        CALL A FUNCTION THAT MIGHT BLOCK. */
                        vApplicationIdleHook();
                        vApplicationIdleHook();
                }
                }
                #endif
                #endif
        }
        }
} /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */
} /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/*-----------------------------------------------------------
/*-----------------------------------------------------------
 * File private functions documented at the top of the file.
 * File private functions documented at the top of the file.
 *----------------------------------------------------------*/
 *----------------------------------------------------------*/
 
 
 
 
 
 
static void prvInitialiseTCBVariables( tskTCB *pxTCB, unsigned portSHORT usStackDepth, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority )
static void prvInitialiseTCBVariables( tskTCB *pxTCB, unsigned portSHORT usStackDepth, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority )
{
{
        pxTCB->usStackDepth = usStackDepth;
        pxTCB->usStackDepth = usStackDepth;
 
 
        /* Store the function name in the TCB. */
        /* Store the function name in the TCB. */
        strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned portSHORT ) configMAX_TASK_NAME_LEN );
        strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned portSHORT ) configMAX_TASK_NAME_LEN );
        pxTCB->pcTaskName[ ( unsigned portSHORT ) configMAX_TASK_NAME_LEN - ( unsigned portSHORT ) 1 ] = '\0';
        pxTCB->pcTaskName[ ( unsigned portSHORT ) configMAX_TASK_NAME_LEN - ( unsigned portSHORT ) 1 ] = '\0';
 
 
        /* This is used as an array index so must ensure it's not too large. */
        /* This is used as an array index so must ensure it's not too large. */
        if( uxPriority >= configMAX_PRIORITIES )
        if( uxPriority >= configMAX_PRIORITIES )
        {
        {
                uxPriority = configMAX_PRIORITIES - 1;
                uxPriority = configMAX_PRIORITIES - 1;
        }
        }
 
 
        pxTCB->uxPriority = uxPriority;
        pxTCB->uxPriority = uxPriority;
 
 
        vListInitialiseItem( &( pxTCB->xGenericListItem ) );
        vListInitialiseItem( &( pxTCB->xGenericListItem ) );
        vListInitialiseItem( &( pxTCB->xEventListItem ) );
        vListInitialiseItem( &( pxTCB->xEventListItem ) );
 
 
        /* Set the pxTCB as a link back from the xListItem.  This is so we can get
        /* Set the pxTCB as a link back from the xListItem.  This is so we can get
        back to the containing TCB from a generic item in a list. */
        back to the containing TCB from a generic item in a list. */
        listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );
        listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );
 
 
        /* Event lists are always in priority order. */
        /* Event lists are always in priority order. */
        listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
        listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
        listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );
        listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
static void prvInitialiseTaskLists( void )
static void prvInitialiseTaskLists( void )
{
{
unsigned portBASE_TYPE uxPriority;
unsigned portBASE_TYPE uxPriority;
 
 
        for( uxPriority = 0; uxPriority < configMAX_PRIORITIES; uxPriority++ )
        for( uxPriority = 0; uxPriority < configMAX_PRIORITIES; uxPriority++ )
        {
        {
                vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );
                vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );
        }
        }
 
 
        vListInitialise( ( xList * ) &xDelayedTaskList1 );
        vListInitialise( ( xList * ) &xDelayedTaskList1 );
        vListInitialise( ( xList * ) &xDelayedTaskList2 );
        vListInitialise( ( xList * ) &xDelayedTaskList2 );
        vListInitialise( ( xList * ) &xPendingReadyList );
        vListInitialise( ( xList * ) &xPendingReadyList );
 
 
        #if ( INCLUDE_vTaskDelete == 1 )
        #if ( INCLUDE_vTaskDelete == 1 )
        {
        {
                vListInitialise( ( xList * ) &xTasksWaitingTermination );
                vListInitialise( ( xList * ) &xTasksWaitingTermination );
        }
        }
        #endif
        #endif
 
 
        #if ( INCLUDE_vTaskSuspend == 1 )
        #if ( INCLUDE_vTaskSuspend == 1 )
        {
        {
                vListInitialise( ( xList * ) &xSuspendedTaskList );
                vListInitialise( ( xList * ) &xSuspendedTaskList );
        }
        }
        #endif
        #endif
 
 
        /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
        /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
        using list2. */
        using list2. */
        pxDelayedTaskList = &xDelayedTaskList1;
        pxDelayedTaskList = &xDelayedTaskList1;
        pxOverflowDelayedTaskList = &xDelayedTaskList2;
        pxOverflowDelayedTaskList = &xDelayedTaskList2;
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
static void prvCheckTasksWaitingTermination( void )
static void prvCheckTasksWaitingTermination( void )
{
{
        #if ( INCLUDE_vTaskDelete == 1 )
        #if ( INCLUDE_vTaskDelete == 1 )
        {
        {
                portBASE_TYPE xListIsEmpty;
                portBASE_TYPE xListIsEmpty;
 
 
                /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
                /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
                too often in the idle task. */
                too often in the idle task. */
                if( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0 )
                if( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0 )
                {
                {
                        vTaskSuspendAll();
                        vTaskSuspendAll();
                                xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
                                xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
                        xTaskResumeAll();
                        xTaskResumeAll();
 
 
                        if( !xListIsEmpty )
                        if( !xListIsEmpty )
                        {
                        {
                                tskTCB *pxTCB;
                                tskTCB *pxTCB;
 
 
                                portENTER_CRITICAL();
                                portENTER_CRITICAL();
                                {
                                {
                                        pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );
                                        pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );
                                        vListRemove( &( pxTCB->xGenericListItem ) );
                                        vListRemove( &( pxTCB->xGenericListItem ) );
                                        --uxCurrentNumberOfTasks;
                                        --uxCurrentNumberOfTasks;
                                        --uxTasksDeleted;
                                        --uxTasksDeleted;
                                }
                                }
                                portEXIT_CRITICAL();
                                portEXIT_CRITICAL();
 
 
                                prvDeleteTCB( pxTCB );
                                prvDeleteTCB( pxTCB );
                        }
                        }
                }
                }
        }
        }
        #endif
        #endif
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth )
static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth )
{
{
tskTCB *pxNewTCB;
tskTCB *pxNewTCB;
 
 
        /* Allocate space for the TCB.  Where the memory comes from depends on
        /* Allocate space for the TCB.  Where the memory comes from depends on
        the implementation of the port malloc function. */
        the implementation of the port malloc function. */
        pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );
        pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );
 
 
        if( pxNewTCB != NULL )
        if( pxNewTCB != NULL )
        {
        {
                /* Allocate space for the stack used by the task being created.
                /* Allocate space for the stack used by the task being created.
                The base of the stack memory stored in the TCB so the task can
                The base of the stack memory stored in the TCB so the task can
                be deleted later if required. */
                be deleted later if required. */
                pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMalloc( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) );
                pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMalloc( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) );
 
 
                if( pxNewTCB->pxStack == NULL )
                if( pxNewTCB->pxStack == NULL )
                {
                {
                        /* Could not allocate the stack.  Delete the allocated TCB. */
                        /* Could not allocate the stack.  Delete the allocated TCB. */
                        vPortFree( pxNewTCB );
                        vPortFree( pxNewTCB );
                        pxNewTCB = NULL;
                        pxNewTCB = NULL;
                }
                }
                else
                else
                {
                {
                        /* Just to help debugging. */
                        /* Just to help debugging. */
                        memset( pxNewTCB->pxStack, tskSTACK_FILL_BYTE, usStackDepth * sizeof( portSTACK_TYPE ) );
                        memset( pxNewTCB->pxStack, tskSTACK_FILL_BYTE, usStackDepth * sizeof( portSTACK_TYPE ) );
                }
                }
        }
        }
 
 
        return pxNewTCB;
        return pxNewTCB;
}
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( configUSE_TRACE_FACILITY == 1 )
#if ( configUSE_TRACE_FACILITY == 1 )
 
 
        static void prvListTaskWithinSingleList( signed portCHAR *pcWriteBuffer, xList *pxList, signed portCHAR cStatus )
        static void prvListTaskWithinSingleList( signed portCHAR *pcWriteBuffer, xList *pxList, signed portCHAR cStatus )
        {
        {
        volatile tskTCB *pxNextTCB, *pxFirstTCB;
        volatile tskTCB *pxNextTCB, *pxFirstTCB;
        static portCHAR pcStatusString[ 50 ];
        static portCHAR pcStatusString[ 50 ];
        unsigned portSHORT usStackRemaining;
        unsigned portSHORT usStackRemaining;
 
 
                /* Write the details of all the TCB's in pxList into the buffer. */
                /* Write the details of all the TCB's in pxList into the buffer. */
                listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
                listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
                do
                do
                {
                {
                        listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
                        listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
                        usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned portCHAR * ) pxNextTCB->pxStack );
                        usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned portCHAR * ) pxNextTCB->pxStack );
                        sprintf( pcStatusString, ( portCHAR * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
                        sprintf( pcStatusString, ( portCHAR * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
                        strcat( ( portCHAR * ) pcWriteBuffer, ( portCHAR * ) pcStatusString );
                        strcat( ( portCHAR * ) pcWriteBuffer, ( portCHAR * ) pcStatusString );
 
 
                } while( pxNextTCB != pxFirstTCB );
                } while( pxNextTCB != pxFirstTCB );
        }
        }
 
 
#endif
#endif
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( configUSE_TRACE_FACILITY == 1 )
#if ( configUSE_TRACE_FACILITY == 1 )
        unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR *pucStackByte )
        unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR *pucStackByte )
        {
        {
        register unsigned portSHORT usCount = 0;
        register unsigned portSHORT usCount = 0;
 
 
                while( *pucStackByte == tskSTACK_FILL_BYTE )
                while( *pucStackByte == tskSTACK_FILL_BYTE )
                {
                {
                        pucStackByte -= portSTACK_GROWTH;
                        pucStackByte -= portSTACK_GROWTH;
                        usCount++;
                        usCount++;
                }
                }
 
 
                usCount /= sizeof( portSTACK_TYPE );
                usCount /= sizeof( portSTACK_TYPE );
 
 
                return usCount;
                return usCount;
        }
        }
#endif
#endif
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
 
 
 
 
#if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
#if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
 
 
        static void prvDeleteTCB( tskTCB *pxTCB )
        static void prvDeleteTCB( tskTCB *pxTCB )
        {
        {
                /* Free up the memory allocated by the scheduler for the task.  It is up to
                /* Free up the memory allocated by the scheduler for the task.  It is up to
                the task to free any memory allocated at the application level. */
                the task to free any memory allocated at the application level. */
                vPortFree( pxTCB->pxStack );
                vPortFree( pxTCB->pxStack );
                vPortFree( pxTCB );
                vPortFree( pxTCB );
        }
        }
 
 
#endif
#endif
 
 
 
 
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
 
 
#if ( INCLUDE_xTaskGetCurrentTaskHandle == 1 )
#if ( INCLUDE_xTaskGetCurrentTaskHandle == 1 )
 
 
        xTaskHandle xTaskGetCurrentTaskHandle( void )
        xTaskHandle xTaskGetCurrentTaskHandle( void )
        {
        {
        xTaskHandle xReturn;
        xTaskHandle xReturn;
 
 
                portENTER_CRITICAL();
                portENTER_CRITICAL();
                {
                {
                        xReturn = ( xTaskHandle ) pxCurrentTCB;
                        xReturn = ( xTaskHandle ) pxCurrentTCB;
                }
                }
                portEXIT_CRITICAL();
                portEXIT_CRITICAL();
 
 
                return xReturn;
                return xReturn;
        }
        }
 
 
#endif
#endif
 
 
 
 
 
 
 
 
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.