]> git.sur5r.net Git - freertos/commitdiff
First version that includes the FreeRTOS-MPU implementation.
authorrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Wed, 30 Sep 2009 20:16:26 +0000 (20:16 +0000)
committerrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Wed, 30 Sep 2009 20:16:26 +0000 (20:16 +0000)
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@886 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

Source/tasks.c

index 88a839559539e4236916b4ac9479273cf20aa305..748b4621a8362260d46365cf2470a5dc106da685 100644 (file)
 \r
 \r
        ***************************************************************************\r
-       *                                                                         *\r
-       * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
-       * See http://www.FreeRTOS.org/Documentation for details                   *\r
-       *                                                                         *\r
+       *                                                                                                                                                *\r
+       * Looking for a quick start?  Then check out the FreeRTOS eBook!                  *\r
+       * See http://www.FreeRTOS.org/Documentation for details                            *\r
+       *                                                                                                                                                *\r
        ***************************************************************************\r
 \r
        1 tab == 4 spaces!\r
 #include <stdlib.h>\r
 #include <string.h>\r
 \r
+/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
+all the API functions to use the MPU wrappers.  That should only be done when\r
+task.h is included from an application file. */\r
+#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
+\r
 #include "FreeRTOS.h"\r
 #include "task.h"\r
 #include "StackMacros.h"\r
 \r
+#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
+\r
 /*\r
  * Macro to define the amount of stack available to the idle task.\r
  */\r
 #define tskIDLE_STACK_SIZE     configMINIMAL_STACK_SIZE\r
 \r
+#define tskIDLE_PRIORITY                       ( ( unsigned portBASE_TYPE ) 0 )\r
+\r
 /*\r
  * Task control block.  A task control block (TCB) is allocated to each task,\r
  * and stores the context of the task.\r
 typedef struct tskTaskControlBlock\r
 {\r
        volatile portSTACK_TYPE *pxTopOfStack;          /*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE STRUCT. */\r
+\r
+       #if ( portUSING_MPU_WRAPPERS == 1 )\r
+               xMPU_SETTINGS xMPUSettings;                             /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE STRUCT. */\r
+       #endif  \r
+       \r
        xListItem                               xGenericListItem;       /*< List item used to place the TCB in ready and blocked queues. */\r
        xListItem                               xEventListItem;         /*< List item used to place the TCB in event lists. */\r
        unsigned portBASE_TYPE  uxPriority;                     /*< The priority of the task where 0 is the lowest priority. */\r
@@ -93,11 +107,12 @@ typedef struct tskTaskControlBlock
        #endif\r
 \r
        #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
-               unsigned portLONG ulRunTimeCounter;                     /*< Used for calculating how much CPU time each task is utilising. */\r
+               unsigned portLONG ulRunTimeCounter;             /*< Used for calculating how much CPU time each task is utilising. */\r
        #endif\r
 \r
 } tskTCB;\r
 \r
+\r
 /*\r
  * Some kernel aware debuggers require data to be viewed to be global, rather\r
  * than file scope.\r
@@ -107,48 +122,47 @@ typedef struct tskTaskControlBlock
 #endif\r
 \r
 /*lint -e956 */\r
-\r
-tskTCB * volatile pxCurrentTCB = NULL;\r
+PRIVILEGED_DATA tskTCB * volatile pxCurrentTCB = NULL;\r
 \r
 /* Lists for ready and blocked tasks. --------------------*/\r
 \r
-static xList pxReadyTasksLists[ configMAX_PRIORITIES ];        /*< Prioritised ready tasks. */\r
-static xList xDelayedTaskList1;                                                        /*< Delayed tasks. */\r
-static xList xDelayedTaskList2;                                                        /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */\r
-static xList * volatile pxDelayedTaskList;                             /*< Points to the delayed task list currently being used. */\r
-static xList * volatile pxOverflowDelayedTaskList;             /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */\r
-static xList xPendingReadyList;                                                        /*< Tasks that have been readied while the scheduler was suspended.  They will be moved to the ready queue when the scheduler is resumed. */\r
+PRIVILEGED_DATA static xList pxReadyTasksLists[ configMAX_PRIORITIES ];        /*< Prioritised ready tasks. */\r
+PRIVILEGED_DATA static xList xDelayedTaskList1;                                                        /*< Delayed tasks. */\r
+PRIVILEGED_DATA static xList xDelayedTaskList2;                                                        /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */\r
+PRIVILEGED_DATA static xList * volatile pxDelayedTaskList ;                            /*< Points to the delayed task list currently being used. */\r
+PRIVILEGED_DATA static xList * volatile pxOverflowDelayedTaskList;             /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */\r
+PRIVILEGED_DATA static xList xPendingReadyList;                                                        /*< Tasks that have been readied while the scheduler was suspended.  They will be moved to the ready queue when the scheduler is resumed. */\r
 \r
 #if ( INCLUDE_vTaskDelete == 1 )\r
 \r
-       static volatile xList xTasksWaitingTermination;         /*< Tasks that have been deleted - but the their memory not yet freed. */\r
-       static volatile unsigned portBASE_TYPE uxTasksDeleted = ( unsigned portBASE_TYPE ) 0;\r
+       PRIVILEGED_DATA static volatile xList xTasksWaitingTermination;         /*< Tasks that have been deleted - but the their memory not yet freed. */\r
+       PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTasksDeleted = ( unsigned portBASE_TYPE ) 0;\r
 \r
 #endif\r
 \r
 #if ( INCLUDE_vTaskSuspend == 1 )\r
 \r
-       static xList xSuspendedTaskList;                                        /*< Tasks that are currently suspended. */\r
+       PRIVILEGED_DATA static xList xSuspendedTaskList;                                        /*< Tasks that are currently suspended. */\r
 \r
 #endif\r
 \r
 /* File private variables. --------------------------------*/\r
-static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks  = ( unsigned portBASE_TYPE ) 0;\r
-static volatile portTickType xTickCount                                                        = ( portTickType ) 0;\r
-static unsigned portBASE_TYPE uxTopUsedPriority                                        = tskIDLE_PRIORITY;\r
-static volatile unsigned portBASE_TYPE uxTopReadyPriority              = tskIDLE_PRIORITY;\r
-static volatile signed portBASE_TYPE xSchedulerRunning                 = pdFALSE;\r
-static volatile unsigned portBASE_TYPE uxSchedulerSuspended            = ( unsigned portBASE_TYPE ) pdFALSE;\r
-static volatile unsigned portBASE_TYPE uxMissedTicks                   = ( unsigned portBASE_TYPE ) 0;\r
-static volatile portBASE_TYPE xMissedYield                                             = ( portBASE_TYPE ) pdFALSE;\r
-static volatile portBASE_TYPE xNumOfOverflows                                  = ( portBASE_TYPE ) 0;\r
-static unsigned portBASE_TYPE uxTaskNumber                                             = ( unsigned portBASE_TYPE ) 0;\r
+PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks  = ( unsigned portBASE_TYPE ) 0;\r
+PRIVILEGED_DATA static volatile portTickType xTickCount                                                = ( portTickType ) 0;\r
+PRIVILEGED_DATA static unsigned portBASE_TYPE uxTopUsedPriority                                        = tskIDLE_PRIORITY;\r
+PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTopReadyPriority              = tskIDLE_PRIORITY;\r
+PRIVILEGED_DATA static volatile signed portBASE_TYPE xSchedulerRunning                         = pdFALSE;\r
+PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxSchedulerSuspended            = ( unsigned portBASE_TYPE ) pdFALSE;\r
+PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxMissedTicks                   = ( unsigned portBASE_TYPE ) 0;\r
+PRIVILEGED_DATA static volatile portBASE_TYPE xMissedYield                                             = ( portBASE_TYPE ) pdFALSE;\r
+PRIVILEGED_DATA static volatile portBASE_TYPE xNumOfOverflows                                  = ( portBASE_TYPE ) 0;\r
+PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                             = ( unsigned portBASE_TYPE ) 0;\r
 \r
 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
 \r
-       static portCHAR pcStatsString[ 50 ];\r
-       static unsigned portLONG ulTaskSwitchedInTime = 0UL;    /*< Holds the value of a timer/counter the last time a task was switched in. */\r
-       static void prvGenerateRunTimeStatsForTasksInList( const signed portCHAR *pcWriteBuffer, xList *pxList, unsigned portLONG ulTotalRunTime );\r
+       PRIVILEGED_DATA static portCHAR pcStatsString[ 50 ] ;\r
+       PRIVILEGED_DATA static unsigned portLONG ulTaskSwitchedInTime = 0UL;    /*< Holds the value of a timer/counter the last time a task was switched in. */\r
+       static void prvGenerateRunTimeStatsForTasksInList( const signed portCHAR *pcWriteBuffer, xList *pxList, unsigned portLONG ulTotalRunTime ) PRIVILEGED_FUNCTION;\r
 \r
 #endif\r
 \r
@@ -174,12 +188,12 @@ static unsigned portBASE_TYPE uxTaskNumber                                                = ( unsigned portBASE_TYPE ) 0;
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
        #define tskSIZE_OF_EACH_TRACE_LINE                      ( ( unsigned portLONG ) ( sizeof( unsigned portLONG ) + sizeof( unsigned portLONG ) ) )\r
-       static volatile signed portCHAR * volatile pcTraceBuffer;\r
-       static signed portCHAR *pcTraceBufferStart;\r
-       static signed portCHAR *pcTraceBufferEnd;\r
-       static signed portBASE_TYPE xTracing = pdFALSE;\r
-       static unsigned portBASE_TYPE uxPreviousTask = 255;\r
-       static portCHAR pcStatusString[ 50 ];\r
+       PRIVILEGED_DATA static volatile signed portCHAR * volatile pcTraceBuffer;\r
+       PRIVILEGED_DATA static signed portCHAR *pcTraceBufferStart;\r
+       PRIVILEGED_DATA static signed portCHAR *pcTraceBufferEnd;\r
+       PRIVILEGED_DATA static signed portBASE_TYPE xTracing = pdFALSE;\r
+       PRIVILEGED_DATA static unsigned portBASE_TYPE uxPreviousTask = 255;\r
+       PRIVILEGED_DATA static portCHAR pcStatusString[ 50 ];\r
 \r
 #endif\r
 \r
@@ -283,13 +297,13 @@ register tskTCB *pxTCB;                                                                                                                                                                                           \
  * Utility to ready a TCB for a given task.  Mainly just copies the parameters\r
  * into the TCB structure.\r
  */\r
-static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority );\r
+static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned portSHORT usStackDepth ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
  * Utility to ready all the lists used by the scheduler.  This is called\r
  * automatically upon the creation of the first task.\r
  */\r
-static void prvInitialiseTaskLists( void );\r
+static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
  * The idle task, which as all tasks is implemented as a never ending loop.\r
@@ -313,7 +327,7 @@ static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
  */\r
 #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )\r
 \r
-       static void prvDeleteTCB( tskTCB *pxTCB );\r
+       static void prvDeleteTCB( tskTCB *pxTCB ) PRIVILEGED_FUNCTION;\r
 \r
 #endif\r
 \r
@@ -322,13 +336,13 @@ static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
  * in the list of tasks waiting to be deleted.  If so the task is cleaned up\r
  * and its TCB deleted.\r
  */\r
-static void prvCheckTasksWaitingTermination( void );\r
+static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
  * Allocates memory from the heap for a TCB and associated stack.  Checks the\r
  * allocation was successful.\r
  */\r
-static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth );\r
+static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth, portSTACK_TYPE *puxStackBuffer ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
  * Called from vTaskList.  vListTasks details all the tasks currently under\r
@@ -341,7 +355,7 @@ static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth );
  */\r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       static void prvListTaskWithinSingleList( const signed portCHAR *pcWriteBuffer, xList *pxList, signed portCHAR cStatus );\r
+       static void prvListTaskWithinSingleList( const signed portCHAR *pcWriteBuffer, xList *pxList, signed portCHAR cStatus ) PRIVILEGED_FUNCTION;\r
 \r
 #endif\r
 \r
@@ -352,7 +366,7 @@ static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth );
  */\r
 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
 \r
-       unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR * pucStackByte );\r
+       static unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR * pucStackByte ) PRIVILEGED_FUNCTION;\r
 \r
 #endif\r
 \r
@@ -365,27 +379,36 @@ static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth );
  * TASK CREATION API documented in task.h\r
  *----------------------------------------------------------*/\r
 \r
-signed portBASE_TYPE xTaskCreate( pdTASK_CODE pvTaskCode, const signed portCHAR * const pcName, unsigned portSHORT usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask )\r
+signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const signed portCHAR * const pcName, unsigned portSHORT usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions )\r
 {\r
 signed portBASE_TYPE xReturn;\r
 tskTCB * pxNewTCB;\r
+portBASE_TYPE xRunPrivileged;\r
 \r
-       /* Allocate the memory required by the TCB and stack for the new task.\r
+       /* Allocate the memory required by the TCB and stack for the new task,\r
        checking that the allocation was successful. */\r
-       pxNewTCB = prvAllocateTCBAndStack( usStackDepth );\r
+       pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer );\r
 \r
        if( pxNewTCB != NULL )\r
        {\r
                portSTACK_TYPE *pxTopOfStack;\r
 \r
-               /* Setup the newly allocated TCB with the initial state of the task. */\r
-               prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority );\r
+               /* Should the task be created in privileged mode? */\r
+               if( ( uxPriority & portPRIVILEGE_BIT ) != 0x00 )\r
+               {\r
+                       xRunPrivileged = pdTRUE;\r
+               }\r
+               else\r
+               {\r
+                       xRunPrivileged = pdFALSE;\r
+               }\r
+               uxPriority &= ~portPRIVILEGE_BIT;\r
 \r
                /* Calculate the top of stack address.  This depends on whether the\r
                stack grows from high memory to low (as per the 80x86) or visa versa.\r
                portSTACK_GROWTH is used to make the result positive or negative as\r
                required by the port. */\r
-               #if portSTACK_GROWTH < 0\r
+               #if( portSTACK_GROWTH < 0 )\r
                {\r
                        pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 ) - ( ( usStackDepth - 1 ) % portBYTE_ALIGNMENT );\r
                }\r
@@ -400,11 +423,23 @@ tskTCB * pxNewTCB;
                }\r
                #endif\r
 \r
+               /* Setup the newly allocated TCB with the initial state of the task. */\r
+               prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth );\r
+\r
                /* Initialize the TCB stack to look as if the task was already running,\r
                but had been interrupted by the scheduler.  The return address is set\r
                to the start of the task function. Once the stack has been initialised\r
                the     top of stack variable is updated. */\r
-               pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pvTaskCode, pvParameters );\r
+               #if( portUSING_MPU_WRAPPERS == 1 )\r
+               {\r
+                       pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );\r
+               }\r
+               #else\r
+               {\r
+                       pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );\r
+                       ( void ) xRunPrivileged;\r
+               }\r
+               #endif\r
 \r
                /* We are going to manipulate the task queues to add this task to a\r
                ready list, so must make sure no interrupts occur. */\r
@@ -479,7 +514,7 @@ tskTCB * pxNewTCB;
                        then it should run now. */\r
                        if( pxCurrentTCB->uxPriority < uxPriority )\r
                        {\r
-                               taskYIELD();\r
+                               portYIELD_WITHIN_API();\r
                        }\r
                }\r
        }\r
@@ -494,7 +529,7 @@ tskTCB * pxNewTCB;
        {\r
        tskTCB *pxTCB;\r
 \r
-               taskENTER_CRITICAL();\r
+               portENTER_CRITICAL();\r
                {\r
                        /* Ensure a yield is performed if the current task is being\r
                        deleted. */\r
@@ -531,14 +566,14 @@ tskTCB * pxNewTCB;
 \r
                        traceTASK_DELETE( pxTCB );\r
                }\r
-               taskEXIT_CRITICAL();\r
+               portEXIT_CRITICAL();\r
 \r
                /* Force a reschedule if we have just deleted the current task. */\r
                if( xSchedulerRunning != pdFALSE )\r
                {\r
                        if( ( void * ) pxTaskToDelete == NULL )\r
                        {\r
-                               taskYIELD();\r
+                               portYIELD_WITHIN_API();\r
                        }\r
                }\r
        }\r
@@ -624,7 +659,7 @@ tskTCB * pxNewTCB;
                have put ourselves to sleep. */\r
                if( !xAlreadyYielded )\r
                {\r
-                       taskYIELD();\r
+                       portYIELD_WITHIN_API();\r
                }\r
        }\r
 \r
@@ -685,7 +720,7 @@ tskTCB * pxNewTCB;
                have put ourselves to sleep. */\r
                if( !xAlreadyYielded )\r
                {\r
-                       taskYIELD();\r
+                       portYIELD_WITHIN_API();\r
                }\r
        }\r
 \r
@@ -699,14 +734,14 @@ tskTCB * pxNewTCB;
        tskTCB *pxTCB;\r
        unsigned portBASE_TYPE uxReturn;\r
 \r
-               taskENTER_CRITICAL();\r
+               portENTER_CRITICAL();\r
                {\r
                        /* If null is passed in here then we are changing the\r
                        priority of the calling function. */\r
                        pxTCB = prvGetTCBFromHandle( pxTask );\r
                        uxReturn = pxTCB->uxPriority;\r
                }\r
-               taskEXIT_CRITICAL();\r
+               portEXIT_CRITICAL();\r
 \r
                return uxReturn;\r
        }\r
@@ -727,7 +762,7 @@ tskTCB * pxNewTCB;
                        uxNewPriority = configMAX_PRIORITIES - 1;\r
                }\r
 \r
-               taskENTER_CRITICAL();\r
+               portENTER_CRITICAL();\r
                {\r
                        if( pxTask == pxCurrentTCB )\r
                        {\r
@@ -809,11 +844,11 @@ tskTCB * pxNewTCB;
 \r
                                if( xYieldRequired == pdTRUE )\r
                                {\r
-                                       taskYIELD();\r
+                                       portYIELD_WITHIN_API();\r
                                }\r
                        }\r
                }\r
-               taskEXIT_CRITICAL();\r
+               portEXIT_CRITICAL();\r
        }\r
 \r
 #endif\r
@@ -825,7 +860,7 @@ tskTCB * pxNewTCB;
        {\r
        tskTCB *pxTCB;\r
 \r
-               taskENTER_CRITICAL();\r
+               portENTER_CRITICAL();\r
                {\r
                        /* Ensure a yield is performed if the current task is being\r
                        suspended. */\r
@@ -850,12 +885,12 @@ tskTCB * pxNewTCB;
 \r
                        vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );\r
                }\r
-               taskEXIT_CRITICAL();\r
+               portEXIT_CRITICAL();\r
 \r
                /* We may have just suspended the current task. */\r
                if( ( void * ) pxTaskToSuspend == NULL )\r
                {\r
-                       taskYIELD();\r
+                       portYIELD_WITHIN_API();\r
                }\r
        }\r
 \r
@@ -907,7 +942,7 @@ tskTCB * pxNewTCB;
                currently executing task. */\r
                if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )\r
                {\r
-                       taskENTER_CRITICAL();\r
+                       portENTER_CRITICAL();\r
                        {\r
                                if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
                                {\r
@@ -923,11 +958,11 @@ tskTCB * pxNewTCB;
                                        {\r
                                                /* This yield may not cause the task just resumed to run, but\r
                                                will leave the lists in the correct state for the next yield. */\r
-                                               taskYIELD();\r
+                                               portYIELD_WITHIN_API();\r
                                        }\r
                                }\r
                        }\r
-                       taskEXIT_CRITICAL();\r
+                       portEXIT_CRITICAL();\r
                }\r
        }\r
 \r
@@ -981,7 +1016,7 @@ void vTaskStartScheduler( void )
 portBASE_TYPE xReturn;\r
 \r
        /* Add the idle task at the lowest priority. */\r
-       xReturn = xTaskCreate( prvIdleTask, ( signed portCHAR * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
+       xReturn = xTaskCreate( prvIdleTask, ( signed portCHAR * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), ( xTaskHandle * ) NULL );\r
 \r
        if( xReturn == pdPASS )\r
        {\r
@@ -1087,7 +1122,7 @@ signed portBASE_TYPE xAlreadyYielded = pdFALSE;
                                        /* As we have processed some ticks it is appropriate to yield\r
                                        to ensure the highest priority task that is ready to run is\r
                                        the task actually running. */\r
-                                       #if configUSE_PREEMPTION == 1\r
+                                       #if configUSE_PREEMPTION == 1\r
                                        {\r
                                                xYieldRequired = pdTRUE;\r
                                        }\r
@@ -1098,7 +1133,7 @@ signed portBASE_TYPE xAlreadyYielded = pdFALSE;
                                {\r
                                        xAlreadyYielded = pdTRUE;\r
                                        xMissedYield = pdFALSE;\r
-                                       taskYIELD();\r
+                                       portYIELD_WITHIN_API();\r
                                }\r
                        }\r
                }\r
@@ -1124,11 +1159,11 @@ portTickType xTaskGetTickCount( void )
 portTickType xTicks;\r
 \r
        /* Critical section required if running on a 16 bit processor. */\r
-       taskENTER_CRITICAL();\r
+       portENTER_CRITICAL();\r
        {\r
                xTicks = xTickCount;\r
        }\r
-       taskEXIT_CRITICAL();\r
+       portEXIT_CRITICAL();\r
 \r
        return xTicks;\r
 }\r
@@ -1151,7 +1186,7 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
                /* This is a VERY costly function that should be used for debug only.\r
                It leaves interrupts disabled for a LONG time. */\r
 \r
-        vTaskSuspendAll();\r
+               vTaskSuspendAll();\r
                {\r
                        /* Run through all the lists that could potentially contain a TCB and\r
                        report the task name, state and stack high water mark. */\r
@@ -1199,7 +1234,7 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
                        }\r
                        #endif\r
                }\r
-        xTaskResumeAll();\r
+               xTaskResumeAll();\r
        }\r
 \r
 #endif\r
@@ -1215,7 +1250,7 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
                /* This is a VERY costly function that should be used for debug only.\r
                It leaves interrupts disabled for a LONG time. */\r
 \r
-        vTaskSuspendAll();\r
+               vTaskSuspendAll();\r
                {\r
                        /* Run through all the lists that could potentially contain a TCB,\r
                        generating a table of run timer percentages in the provided\r
@@ -1264,7 +1299,7 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
                        }\r
                        #endif\r
                }\r
-        xTaskResumeAll();\r
+               xTaskResumeAll();\r
        }\r
 \r
 #endif\r
@@ -1330,7 +1365,7 @@ void vTaskIncrementTick( void )
                        pxTemp = pxDelayedTaskList;\r
                        pxDelayedTaskList = pxOverflowDelayedTaskList;\r
                        pxOverflowDelayedTaskList = pxTemp;\r
-            xNumOfOverflows++;\r
+                       xNumOfOverflows++;\r
                }\r
 \r
                /* See if this tick has made a timeout expire. */\r
@@ -1671,8 +1706,8 @@ portBASE_TYPE xReturn;
 \r
 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )\r
 {\r
-    pxTimeOut->xOverflowCount = xNumOfOverflows;\r
-    pxTimeOut->xTimeOnEntering = xTickCount;\r
+       pxTimeOut->xOverflowCount = xNumOfOverflows;\r
+       pxTimeOut->xTimeOnEntering = xTickCount;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1715,7 +1750,7 @@ portBASE_TYPE xReturn;
        }\r
        portEXIT_CRITICAL();\r
 \r
-    return xReturn;\r
+       return xReturn;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1800,7 +1835,7 @@ static portTASK_FUNCTION( prvIdleTask, pvParameters )
 \r
 \r
 \r
-static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority )\r
+static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned portSHORT usStackDepth )\r
 {\r
        /* Store the function name in the TCB. */\r
        #if configMAX_TASK_NAME_LEN > 1\r
@@ -1811,7 +1846,8 @@ static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed portCHAR * co
        #endif\r
        pxTCB->pcTaskName[ ( unsigned portSHORT ) configMAX_TASK_NAME_LEN - ( unsigned portSHORT ) 1 ] = '\0';\r
 \r
-       /* This is used as an array index so must ensure it's not too large. */\r
+       /* This is used as an array index so must ensure it's not too large.  First\r
+       remove the privilege bit if one is present. */\r
        if( uxPriority >= configMAX_PRIORITIES )\r
        {\r
                uxPriority = configMAX_PRIORITIES - 1;\r
@@ -1852,9 +1888,39 @@ static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed portCHAR * co
                pxTCB->ulRunTimeCounter = 0UL;\r
        }\r
        #endif\r
+\r
+       #if ( portUSING_MPU_WRAPPERS == 1 )\r
+       {\r
+               vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );\r
+       }\r
+       #else\r
+       {\r
+               ( void ) xRegions;\r
+               ( void ) usStackDepth;\r
+       }\r
+       #endif\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+#if ( portUSING_MPU_WRAPPERS == 1 )\r
+\r
+       void vTaskAllocateMPURegions( xTaskHandle xTaskToModify, const xMemoryRegion * const xRegions )\r
+       {\r
+       tskTCB *pxTCB;\r
+       \r
+               if( xTaskToModify == pxCurrentTCB )\r
+               {\r
+                       xTaskToModify = NULL;\r
+               }\r
+\r
+               /* If null is passed in here then we are deleting ourselves. */\r
+               pxTCB = prvGetTCBFromHandle( xTaskToModify );\r
+\r
+        vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );\r
+       }\r
+       /*-----------------------------------------------------------*/\r
+#endif\r
+\r
 static void prvInitialiseTaskLists( void )\r
 {\r
 unsigned portBASE_TYPE uxPriority;\r
@@ -1922,7 +1988,7 @@ static void prvCheckTasksWaitingTermination( void )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth )\r
+static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth, portSTACK_TYPE *puxStackBuffer )\r
 {\r
 tskTCB *pxNewTCB;\r
 \r
@@ -1935,7 +2001,7 @@ tskTCB *pxNewTCB;
                /* Allocate space for the stack used by the task being created.\r
                The base of the stack memory stored in the TCB so the task can\r
                be deleted later if required. */\r
-               pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMalloc( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) );\r
+               pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );\r
 \r
                if( pxNewTCB->pxStack == NULL )\r
                {\r
@@ -2028,7 +2094,7 @@ tskTCB *pxNewTCB;
 \r
 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
 \r
-       unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR * pucStackByte )\r
+       static unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR * pucStackByte )\r
        {\r
        register unsigned portSHORT usCount = 0;\r
 \r
@@ -2052,6 +2118,7 @@ tskTCB *pxNewTCB;
        {\r
        tskTCB *pxTCB;\r
        unsigned portCHAR *pcEndOfStack;\r
+       unsigned portBASE_TYPE uxReturn;\r
 \r
                pxTCB = prvGetTCBFromHandle( xTask );\r
 \r
@@ -2065,7 +2132,9 @@ tskTCB *pxNewTCB;
                }\r
                #endif\r
 \r
-               return usTaskCheckFreeStackSpace( pcEndOfStack );\r
+               uxReturn = ( unsigned portBASE_TYPE ) usTaskCheckFreeStackSpace( pcEndOfStack );\r
+\r
+               return uxReturn;\r
        }\r
 \r
 #endif\r
@@ -2077,7 +2146,7 @@ tskTCB *pxNewTCB;
        {\r
                /* Free up the memory allocated by the scheduler for the task.  It is up to\r
                the task to free any memory allocated at the application level. */\r
-               vPortFree( pxTCB->pxStack );\r
+               vPortFreeAligned( pxTCB->pxStack );\r
                vPortFree( pxTCB );\r
        }\r
 \r
@@ -2090,10 +2159,14 @@ tskTCB *pxNewTCB;
 \r
        xTaskHandle xTaskGetCurrentTaskHandle( void )\r
        {\r
+       xTaskHandle xReturn;\r
+\r
                /* A critical section is not required as this is not called from\r
                an interrupt and the current TCB will always be the same for any\r
                individual execution thread. */\r
-               return pxCurrentTCB;\r
+               xReturn = pxCurrentTCB;\r
+\r
+               return xReturn;\r
        }\r
 \r
 #endif\r