]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/portable/IAR/ARM_CM4F_MPU/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CM4F_MPU / port.c
diff --git a/FreeRTOS/Source/portable/IAR/ARM_CM4F_MPU/port.c b/FreeRTOS/Source/portable/IAR/ARM_CM4F_MPU/port.c
new file mode 100644 (file)
index 0000000..906f124
--- /dev/null
@@ -0,0 +1,755 @@
+/*\r
+ * FreeRTOS Kernel V10.0.0\r
+ * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software. If you wish to use our Amazon\r
+ * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ * http://www.FreeRTOS.org\r
+ * http://aws.amazon.com/freertos\r
+ *\r
+ * 1 tab == 4 spaces!\r
+ */\r
+\r
+/*-----------------------------------------------------------\r
+ * Implementation of functions defined in portable.h for the ARM CM4F port.\r
+ *----------------------------------------------------------*/\r
+\r
+/* IAR includes. */\r
+#include <intrinsics.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
+/* Scheduler includes. */\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+\r
+#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
+\r
+#warning This is not yet a documented port as it has not been fully tested, so no demo projects that use this port are provided.\r
+\r
+#ifndef __ARMVFP__\r
+       #error This port can only be used when the project options are configured to enable hardware floating point support.\r
+#endif\r
+\r
+#if( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 )\r
+       #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.  See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html\r
+#endif\r
+\r
+#ifndef configSYSTICK_CLOCK_HZ\r
+       #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ\r
+       /* Ensure the SysTick is clocked at the same frequency as the core. */\r
+       #define portNVIC_SYSTICK_CLK_BIT        ( 1UL << 2UL )\r
+#else\r
+       /* The way the SysTick is clocked is not modified in case it is not the same\r
+       as the core. */\r
+       #define portNVIC_SYSTICK_CLK_BIT        ( 0 )\r
+#endif\r
+\r
+/* Constants required to manipulate the core.  Registers first... */\r
+#define portNVIC_SYSTICK_CTRL_REG                              ( * ( ( volatile uint32_t * ) 0xe000e010 ) )\r
+#define portNVIC_SYSTICK_LOAD_REG                              ( * ( ( volatile uint32_t * ) 0xe000e014 ) )\r
+#define portNVIC_SYSTICK_CURRENT_VALUE_REG             ( * ( ( volatile uint32_t * ) 0xe000e018 ) )\r
+#define portNVIC_SYSPRI2_REG                                   ( * ( ( volatile uint32_t * ) 0xe000ed20 ) )\r
+#define portNVIC_SYSPRI1_REG                                   ( * ( ( volatile uint32_t * ) 0xe000ed1c ) )\r
+#define portNVIC_SYS_CTRL_STATE_REG                            ( * ( ( volatile uint32_t * ) 0xe000ed24 ) )\r
+#define portNVIC_MEM_FAULT_ENABLE                              ( 1UL << 16UL )\r
+\r
+/* Constants required to access and manipulate the MPU. */\r
+#define portMPU_TYPE_REG                                               ( * ( ( volatile uint32_t * ) 0xe000ed90 ) )\r
+#define portMPU_REGION_BASE_ADDRESS_REG                        ( * ( ( volatile uint32_t * ) 0xe000ed9C ) )\r
+#define portMPU_REGION_ATTRIBUTE_REG                   ( * ( ( volatile uint32_t * ) 0xe000edA0 ) )\r
+#define portMPU_CTRL_REG                                               ( * ( ( volatile uint32_t * ) 0xe000ed94 ) )\r
+#define portEXPECTED_MPU_TYPE_VALUE                            ( 8UL << 8UL ) /* 8 regions, unified. */\r
+#define portMPU_ENABLE                                                 ( 0x01UL )\r
+#define portMPU_BACKGROUND_ENABLE                              ( 1UL << 2UL )\r
+#define portPRIVILEGED_EXECUTION_START_ADDRESS ( 0UL )\r
+#define portMPU_REGION_VALID                                   ( 0x10UL )\r
+#define portMPU_REGION_ENABLE                                  ( 0x01UL )\r
+#define portPERIPHERALS_START_ADDRESS                  0x40000000UL\r
+#define portPERIPHERALS_END_ADDRESS                            0x5FFFFFFFUL\r
+\r
+/* ...then bits in the registers. */\r
+#define portNVIC_SYSTICK_INT_BIT                       ( 1UL << 1UL )\r
+#define portNVIC_SYSTICK_ENABLE_BIT                    ( 1UL << 0UL )\r
+#define portNVIC_SYSTICK_COUNT_FLAG_BIT                ( 1UL << 16UL )\r
+#define portNVIC_PENDSVCLEAR_BIT                       ( 1UL << 27UL )\r
+#define portNVIC_PEND_SYSTICK_CLEAR_BIT                ( 1UL << 25UL )\r
+\r
+/* Constants used to detect a Cortex-M7 r0p1 core, which should use the ARM_CM7\r
+r0p1 port. */\r
+#define portCPUID                                                      ( * ( ( volatile uint32_t * ) 0xE000ed00 ) )\r
+#define portCORTEX_M7_r0p1_ID                          ( 0x410FC271UL )\r
+#define portCORTEX_M7_r0p0_ID                          ( 0x410FC270UL )\r
+\r
+#define portNVIC_PENDSV_PRI                                    ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )\r
+#define portNVIC_SYSTICK_PRI                           ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )\r
+#define portNVIC_SVC_PRI                                       ( ( ( uint32_t ) configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ) << 24UL )\r
+\r
+/* Constants required to check the validity of an interrupt priority. */\r
+#define portFIRST_USER_INTERRUPT_NUMBER                ( 16 )\r
+#define portNVIC_IP_REGISTERS_OFFSET_16        ( 0xE000E3F0 )\r
+#define portAIRCR_REG                                          ( * ( ( volatile uint32_t * ) 0xE000ED0C ) )\r
+#define portMAX_8_BIT_VALUE                                    ( ( uint8_t ) 0xff )\r
+#define portTOP_BIT_OF_BYTE                                    ( ( uint8_t ) 0x80 )\r
+#define portMAX_PRIGROUP_BITS                          ( ( uint8_t ) 7 )\r
+#define portPRIORITY_GROUP_MASK                                ( 0x07UL << 8UL )\r
+#define portPRIGROUP_SHIFT                                     ( 8UL )\r
+\r
+/* Masks off all bits but the VECTACTIVE bits in the ICSR register. */\r
+#define portVECTACTIVE_MASK                                    ( 0xFFUL )\r
+\r
+/* Constants required to manipulate the VFP. */\r
+#define portFPCCR                                                      ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */\r
+#define portASPEN_AND_LSPEN_BITS                       ( 0x3UL << 30UL )\r
+\r
+/* Constants required to set up the initial stack. */\r
+#define portINITIAL_XPSR                                       ( 0x01000000 )\r
+#define portINITIAL_EXC_RETURN                         ( 0xfffffffd )\r
+#define portINITIAL_CONTROL_IF_UNPRIVILEGED    ( 0x03 )\r
+#define portINITIAL_CONTROL_IF_PRIVILEGED      ( 0x02 )\r
+\r
+/* Offsets in the stack to the parameters when inside the SVC handler. */\r
+#define portOFFSET_TO_PC                                               ( 6 )\r
+\r
+/* The systick is a 24-bit counter. */\r
+#define portMAX_24_BIT_NUMBER                          ( 0xffffffUL )\r
+\r
+/* A fiddle factor to estimate the number of SysTick counts that would have\r
+occurred while the SysTick counter is stopped during tickless idle\r
+calculations. */\r
+#define portMISSED_COUNTS_FACTOR                       ( 45UL )\r
+\r
+/* For strict compliance with the Cortex-M spec the task start address should\r
+have bit-0 clear, as it is loaded into the PC on exit from an ISR. */\r
+#define portSTART_ADDRESS_MASK                         ( ( StackType_t ) 0xfffffffeUL )\r
+\r
+/*\r
+ * Configure a number of standard MPU regions that are used by all tasks.\r
+ */\r
+static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;\r
+\r
+/*\r
+ * Return the smallest MPU region size that a given number of bytes will fit\r
+ * into.  The region size is returned as the value that should be programmed\r
+ * into the region attribute register for that region.\r
+ */\r
+static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;\r
+\r
+/*\r
+ * Checks to see if being called from the context of an unprivileged task, and\r
+ * if so raises the privilege level and returns false - otherwise does nothing\r
+ * other than return true.\r
+ */\r
+extern BaseType_t xPortRaisePrivilege( void );\r
+\r
+/*\r
+ * Setup the timer to generate the tick interrupts.  The implementation in this\r
+ * file is weak to allow application writers to change the timer used to\r
+ * generate the tick interrupt.\r
+ */\r
+void vPortSetupTimerInterrupt( void );\r
+\r
+/*\r
+ * Exception handlers.\r
+ */\r
+void xPortSysTickHandler( void ) PRIVILEGED_FUNCTION;\r
+\r
+/*\r
+ * Start first task is a separate function so it can be tested in isolation.\r
+ */\r
+extern void vPortStartFirstTask( void ) PRIVILEGED_FUNCTION;\r
+\r
+/*\r
+ * Turn the VFP on.\r
+ */\r
+extern void vPortEnableVFP( void );\r
+\r
+/*\r
+ * The C portion of the SVC handler.\r
+ */\r
+void vPortSVCHandler_C( uint32_t *pulParam );\r
+\r
+/*\r
+ * Called from the SVC handler used to start the scheduler.\r
+ */\r
+extern void vPortRestoreContextOfFirstTask( void ) PRIVILEGED_FUNCTION;\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/* Each task maintains its own interrupt status in the critical nesting\r
+variable. */\r
+static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
+\r
+/*\r
+ * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure\r
+ * FreeRTOS API functions are not called from interrupts that have been assigned\r
+ * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
+ */\r
+#if( configASSERT_DEFINED == 1 )\r
+        static uint8_t ucMaxSysCallPriority = 0;\r
+        static uint32_t ulMaxPRIGROUPValue = 0;\r
+        static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;\r
+#endif /* configASSERT_DEFINED */\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/*\r
+ * See header file for description.\r
+ */\r
+StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters, BaseType_t xRunPrivileged )\r
+{\r
+       /* Simulate the stack frame as it would be created by a context switch\r
+       interrupt. */\r
+\r
+       /* Offset added to account for the way the MCU uses the stack on entry/exit\r
+       of interrupts, and to ensure alignment. */\r
+       pxTopOfStack--;\r
+\r
+       *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
+       pxTopOfStack--;\r
+       *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK;    /* PC */\r
+       pxTopOfStack--;\r
+       *pxTopOfStack = ( StackType_t ) 0;      /* LR */\r
+\r
+       /* Save code space by skipping register initialisation. */\r
+       pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
+       *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
+\r
+       /* A save method is being used that requires each task to maintain its\r
+       own exec return value. */\r
+       pxTopOfStack--;\r
+       *pxTopOfStack = portINITIAL_EXC_RETURN;\r
+\r
+       pxTopOfStack -= 9;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
+\r
+       if( xRunPrivileged == pdTRUE )\r
+       {\r
+               *pxTopOfStack = portINITIAL_CONTROL_IF_PRIVILEGED;\r
+       }\r
+       else\r
+       {\r
+               *pxTopOfStack = portINITIAL_CONTROL_IF_UNPRIVILEGED;\r
+       }\r
+\r
+       return pxTopOfStack;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortSVCHandler_C( uint32_t *pulParam )\r
+{\r
+uint8_t ucSVCNumber;\r
+\r
+       /* The stack contains: r0, r1, r2, r3, r12, r14, the return address and\r
+       xPSR.  The first argument (r0) is pulParam[ 0 ]. */\r
+       ucSVCNumber = ( ( uint8_t * ) pulParam[ portOFFSET_TO_PC ] )[ -2 ];\r
+       switch( ucSVCNumber )\r
+       {\r
+               case portSVC_START_SCHEDULER    :       portNVIC_SYSPRI1_REG |= portNVIC_SVC_PRI;\r
+                                                                                       vPortRestoreContextOfFirstTask();\r
+                                                                                       break;\r
+\r
+               case portSVC_YIELD                              :       portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
+                                                                                       /* Barriers are normally not required\r
+                                                                                       but do ensure the code is completely\r
+                                                                                       within the specified behaviour for the\r
+                                                                                       architecture. */\r
+                                                                                       __asm volatile( "dsb" ::: "memory" );\r
+                                                                                       __asm volatile( "isb" );\r
+\r
+                                                                                       break;\r
+\r
+               case portSVC_RAISE_PRIVILEGE    :       __asm volatile\r
+                                                                                       (\r
+                                                                                               "       mrs r1, control         \n" /* Obtain current control value. */\r
+                                                                                               "       bic r1, r1, #1          \n" /* Set privilege bit. */\r
+                                                                                               "       msr control, r1         \n" /* Write back new control value. */\r
+                                                                                               ::: "r1", "memory"\r
+                                                                                       );\r
+                                                                                       break;\r
+\r
+               default                                                 :       /* Unknown SVC call. */\r
+                                                                                       break;\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/*\r
+ * See header file for description.\r
+ */\r
+BaseType_t xPortStartScheduler( void )\r
+{\r
+       /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.\r
+       See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
+       configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
+\r
+       /* This port can be used on all revisions of the Cortex-M7 core other than\r
+       the r0p1 parts.  r0p1 parts should use the port from the\r
+       /source/portable/GCC/ARM_CM7/r0p1 directory. */\r
+       configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );\r
+       configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );\r
+\r
+       #if( configASSERT_DEFINED == 1 )\r
+       {\r
+               volatile uint32_t ulOriginalPriority;\r
+               volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );\r
+               volatile uint8_t ucMaxPriorityValue;\r
+\r
+               /* Determine the maximum priority from which ISR safe FreeRTOS API\r
+               functions can be called.  ISR safe functions are those that end in\r
+               "FromISR".  FreeRTOS maintains separate thread and ISR API functions to\r
+               ensure interrupt entry is as fast and simple as possible.\r
+\r
+               Save the interrupt priority value that is about to be clobbered. */\r
+               ulOriginalPriority = *pucFirstUserPriorityRegister;\r
+\r
+               /* Determine the number of priority bits available.  First write to all\r
+               possible bits. */\r
+               *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;\r
+\r
+               /* Read the value back to see how many bits stuck. */\r
+               ucMaxPriorityValue = *pucFirstUserPriorityRegister;\r
+\r
+               /* Use the same mask on the maximum system call priority. */\r
+               ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;\r
+\r
+               /* Calculate the maximum acceptable priority group value for the number\r
+               of bits read back. */\r
+               ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;\r
+               while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )\r
+               {\r
+                       ulMaxPRIGROUPValue--;\r
+                       ucMaxPriorityValue <<= ( uint8_t ) 0x01;\r
+               }\r
+\r
+               #ifdef __NVIC_PRIO_BITS\r
+               {\r
+                       /* Check the CMSIS configuration that defines the number of\r
+                       priority bits matches the number of priority bits actually queried\r
+                       from the hardware. */\r
+                       configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );\r
+               }\r
+               #endif\r
+\r
+               #ifdef configPRIO_BITS\r
+               {\r
+                       /* Check the FreeRTOS configuration that defines the number of\r
+                       priority bits matches the number of priority bits actually queried\r
+                       from the hardware. */\r
+                       configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );\r
+               }\r
+               #endif\r
+\r
+               /* Shift the priority group value back to its position within the AIRCR\r
+               register. */\r
+               ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;\r
+               ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;\r
+\r
+               /* Restore the clobbered interrupt priority register to its original\r
+               value. */\r
+               *pucFirstUserPriorityRegister = ulOriginalPriority;\r
+       }\r
+       #endif /* conifgASSERT_DEFINED */\r
+\r
+       /* Make PendSV and SysTick the lowest priority interrupts. */\r
+       portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI;\r
+       portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI;\r
+\r
+       /* Configure the regions in the MPU that are common to all tasks. */\r
+       prvSetupMPU();\r
+\r
+       /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
+       here already. */\r
+       vPortSetupTimerInterrupt();\r
+\r
+       /* Initialise the critical nesting count ready for the first task. */\r
+       uxCriticalNesting = 0;\r
+\r
+       /* Ensure the VFP is enabled - it should be anyway. */\r
+       vPortEnableVFP();\r
+\r
+       /* Lazy save always. */\r
+       *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\r
+\r
+       /* Start the first task. */\r
+       vPortStartFirstTask();\r
+\r
+       /* Should not get here! */\r
+       return 0;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortEndScheduler( void )\r
+{\r
+       /* Not implemented in ports where there is nothing to return to.\r
+       Artificially force an assert. */\r
+       configASSERT( uxCriticalNesting == 1000UL );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortEnterCritical( void )\r
+{\r
+ BaseType_t xRunningPrivileged = xPortRaisePrivilege();\r
+\r
+       portDISABLE_INTERRUPTS();\r
+       uxCriticalNesting++;\r
+\r
+       vPortResetPrivilege( xRunningPrivileged );\r
+\r
+       /* This is not the interrupt safe version of the enter critical function so\r
+       assert() if it is being called from an interrupt context.  Only API\r
+       functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
+       the critical nesting count is 1 to protect against recursive calls if the\r
+       assert function also uses a critical section. */\r
+       if( uxCriticalNesting == 1 )\r
+       {\r
+               configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortExitCritical( void )\r
+{\r
+BaseType_t xRunningPrivileged = xPortRaisePrivilege();\r
+\r
+       configASSERT( uxCriticalNesting );\r
+\r
+       uxCriticalNesting--;\r
+       if( uxCriticalNesting == 0 )\r
+       {\r
+               portENABLE_INTERRUPTS();\r
+       }\r
+\r
+       vPortResetPrivilege( xRunningPrivileged );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void xPortSysTickHandler( void )\r
+{\r
+       /* The SysTick runs at the lowest interrupt priority, so when this interrupt\r
+       executes all interrupts must be unmasked.  There is therefore no need to\r
+       save and then restore the interrupt mask value as its value is already\r
+       known. */\r
+       portDISABLE_INTERRUPTS();\r
+       {\r
+               /* Increment the RTOS tick. */\r
+               if( xTaskIncrementTick() != pdFALSE )\r
+               {\r
+                       /* A context switch is required.  Context switching is performed in\r
+                       the PendSV interrupt.  Pend the PendSV interrupt. */\r
+                       portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
+               }\r
+       }\r
+       portENABLE_INTERRUPTS();\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/*\r
+ * Setup the systick timer to generate the tick interrupts at the required\r
+ * frequency.\r
+ */\r
+__weak void vPortSetupTimerInterrupt( void )\r
+{\r
+       /* Stop and clear the SysTick. */\r
+       portNVIC_SYSTICK_CTRL_REG = 0UL;\r
+       portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
+\r
+       /* Configure SysTick to interrupt at the requested rate. */\r
+       portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
+       portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSetupMPU( void )\r
+{\r
+extern uint32_t __privileged_functions_end__[];\r
+extern uint32_t __FLASH_segment_start__[];\r
+extern uint32_t __FLASH_segment_end__[];\r
+extern uint32_t __privileged_data_start__[];\r
+extern uint32_t __privileged_data_end__[];\r
+\r
+       /* Check the expected MPU is present. */\r
+       if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )\r
+       {\r
+               /* First setup the entire flash for unprivileged read only access. */\r
+               portMPU_REGION_BASE_ADDRESS_REG =       ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */\r
+                                                                                       ( portMPU_REGION_VALID ) |\r
+                                                                                       ( portUNPRIVILEGED_FLASH_REGION );\r
+\r
+               portMPU_REGION_ATTRIBUTE_REG =  ( portMPU_REGION_READ_ONLY ) |\r
+                                                                               ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
+                                                                               ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |\r
+                                                                               ( portMPU_REGION_ENABLE );\r
+\r
+               /* Setup the first 16K for privileged only access (even though less\r
+               than 10K is actually being used).  This is where the kernel code is\r
+               placed. */\r
+               portMPU_REGION_BASE_ADDRESS_REG =       ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */\r
+                                                                                       ( portMPU_REGION_VALID ) |\r
+                                                                                       ( portPRIVILEGED_FLASH_REGION );\r
+\r
+               portMPU_REGION_ATTRIBUTE_REG =  ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |\r
+                                                                               ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
+                                                                               ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |\r
+                                                                               ( portMPU_REGION_ENABLE );\r
+\r
+               /* Setup the privileged data RAM region.  This is where the kernel data\r
+               is placed. */\r
+               portMPU_REGION_BASE_ADDRESS_REG =       ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */\r
+                                                                                       ( portMPU_REGION_VALID ) |\r
+                                                                                       ( portPRIVILEGED_RAM_REGION );\r
+\r
+               portMPU_REGION_ATTRIBUTE_REG =  ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |\r
+                                                                               ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
+                                                                               prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |\r
+                                                                               ( portMPU_REGION_ENABLE );\r
+\r
+               /* By default allow everything to access the general peripherals.  The\r
+               system peripherals and registers are protected. */\r
+               portMPU_REGION_BASE_ADDRESS_REG =       ( portPERIPHERALS_START_ADDRESS ) |\r
+                                                                                       ( portMPU_REGION_VALID ) |\r
+                                                                                       ( portGENERAL_PERIPHERALS_REGION );\r
+\r
+               portMPU_REGION_ATTRIBUTE_REG =  ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |\r
+                                                                               ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |\r
+                                                                               ( portMPU_REGION_ENABLE );\r
+\r
+               /* Enable the memory fault exception. */\r
+               portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;\r
+\r
+               /* Enable the MPU with the background region configured. */\r
+               portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )\r
+{\r
+uint32_t ulRegionSize, ulReturnValue = 4;\r
+\r
+       /* 32 is the smallest region size, 31 is the largest valid value for\r
+       ulReturnValue. */\r
+       for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )\r
+       {\r
+               if( ulActualSizeInBytes <= ulRegionSize )\r
+               {\r
+                       break;\r
+               }\r
+               else\r
+               {\r
+                       ulReturnValue++;\r
+               }\r
+       }\r
+\r
+       /* Shift the code by one before returning so it can be written directly\r
+       into the the correct bit position of the attribute register. */\r
+       return ( ulReturnValue << 1UL );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortResetPrivilege( BaseType_t xRunningPrivileged )\r
+{\r
+       if( xRunningPrivileged != pdTRUE )\r
+       {\r
+               __asm volatile ( " mrs r0, control      \n" \\r
+                                                " orr r0, r0, #1       \n" \\r
+                                                " msr control, r0      \n"     \\r
+                                                :::"r0", "memory" );\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMORY_REGION * const xRegions, StackType_t *pxBottomOfStack, uint32_t ulStackDepth )\r
+{\r
+extern uint32_t __SRAM_segment_start__[];\r
+extern uint32_t __SRAM_segment_end__[];\r
+extern uint32_t __privileged_data_start__[];\r
+extern uint32_t __privileged_data_end__[];\r
+int32_t lIndex;\r
+uint32_t ul;\r
+\r
+       if( xRegions == NULL )\r
+       {\r
+               /* No MPU regions are specified so allow access to all RAM. */\r
+               xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =\r
+                               ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */\r
+                               ( portMPU_REGION_VALID ) |\r
+                               ( portSTACK_REGION );\r
+\r
+               xMPUSettings->xRegion[ 0 ].ulRegionAttribute =\r
+                               ( portMPU_REGION_READ_WRITE ) |\r
+                               ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
+                               ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |\r
+                               ( portMPU_REGION_ENABLE );\r
+\r
+               /* Re-instate the privileged only RAM region as xRegion[ 0 ] will have\r
+               just removed the privileged only parameters. */\r
+               xMPUSettings->xRegion[ 1 ].ulRegionBaseAddress =\r
+                               ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */\r
+                               ( portMPU_REGION_VALID ) |\r
+                               ( portSTACK_REGION + 1 );\r
+\r
+               xMPUSettings->xRegion[ 1 ].ulRegionAttribute =\r
+                               ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |\r
+                               ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
+                               prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |\r
+                               ( portMPU_REGION_ENABLE );\r
+\r
+               /* Invalidate all other regions. */\r
+               for( ul = 2; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )\r
+               {\r
+                       xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;\r
+                       xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;\r
+               }\r
+       }\r
+       else\r
+       {\r
+               /* This function is called automatically when the task is created - in\r
+               which case the stack region parameters will be valid.  At all other\r
+               times the stack parameters will not be valid and it is assumed that the\r
+               stack region has already been configured. */\r
+               if( ulStackDepth > 0 )\r
+               {\r
+                       /* Define the region that allows access to the stack. */\r
+                       xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =\r
+                                       ( ( uint32_t ) pxBottomOfStack ) |\r
+                                       ( portMPU_REGION_VALID ) |\r
+                                       ( portSTACK_REGION ); /* Region number. */\r
+\r
+                       xMPUSettings->xRegion[ 0 ].ulRegionAttribute =\r
+                                       ( portMPU_REGION_READ_WRITE ) | /* Read and write. */\r
+                                       ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |\r
+                                       ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
+                                       ( portMPU_REGION_ENABLE );\r
+               }\r
+\r
+               lIndex = 0;\r
+\r
+               for( ul = 1; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )\r
+               {\r
+                       if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )\r
+                       {\r
+                               /* Translate the generic region definition contained in\r
+                               xRegions into the CM3 specific MPU settings that are then\r
+                               stored in xMPUSettings. */\r
+                               xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =\r
+                                               ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |\r
+                                               ( portMPU_REGION_VALID ) |\r
+                                               ( portSTACK_REGION + ul ); /* Region number. */\r
+\r
+                               xMPUSettings->xRegion[ ul ].ulRegionAttribute =\r
+                                               ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |\r
+                                               ( xRegions[ lIndex ].ulParameters ) |\r
+                                               ( portMPU_REGION_ENABLE );\r
+                       }\r
+                       else\r
+                       {\r
+                               /* Invalidate the region. */\r
+                               xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;\r
+                               xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;\r
+                       }\r
+\r
+                       lIndex++;\r
+               }\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+#if( configASSERT_DEFINED == 1 )\r
+\r
+       void vPortValidateInterruptPriority( void )\r
+       {\r
+       uint32_t ulCurrentInterrupt;\r
+       uint8_t ucCurrentPriority;\r
+\r
+               /* Obtain the number of the currently executing interrupt. */\r
+               __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) :: "memory" );\r
+\r
+               /* Is the interrupt number a user defined interrupt? */\r
+               if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )\r
+               {\r
+                       /* Look up the interrupt's priority. */\r
+                       ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];\r
+\r
+                       /* The following assertion will fail if a service routine (ISR) for\r
+                       an interrupt that has been assigned a priority above\r
+                       configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API\r
+                       function.  ISR safe FreeRTOS API functions must *only* be called\r
+                       from interrupts that have been assigned a priority at or below\r
+                       configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
+\r
+                       Numerically low interrupt priority numbers represent logically high\r
+                       interrupt priorities, therefore the priority of the interrupt must\r
+                       be set to a value equal to or numerically *higher* than\r
+                       configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
+\r
+                       Interrupts that use the FreeRTOS API must not be left at their\r
+                       default priority of     zero as that is the highest possible priority,\r
+                       which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,\r
+                       and     therefore also guaranteed to be invalid.\r
+\r
+                       FreeRTOS maintains separate thread and ISR API functions to ensure\r
+                       interrupt entry is as fast and simple as possible.\r
+\r
+                       The following links provide detailed information:\r
+                       http://www.freertos.org/RTOS-Cortex-M3-M4.html\r
+                       http://www.freertos.org/FAQHelp.html */\r
+                       configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );\r
+               }\r
+\r
+               /* Priority grouping:  The interrupt controller (NVIC) allows the bits\r
+               that define each interrupt's priority to be split between bits that\r
+               define the interrupt's pre-emption priority bits and bits that define\r
+               the interrupt's sub-priority.  For simplicity all bits must be defined\r
+               to be pre-emption priority bits.  The following assertion will fail if\r
+               this is not the case (if some bits represent a sub-priority).\r
+\r
+               If the application only uses CMSIS libraries for interrupt\r
+               configuration then the correct setting can be achieved on all Cortex-M\r
+               devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the\r
+               scheduler.  Note however that some vendor specific peripheral libraries\r
+               assume a non-zero priority group setting, in which cases using a value\r
+               of zero will result in unpredictable behaviour. */\r
+               configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );\r
+       }\r
+\r
+#endif /* configASSERT_DEFINED */\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r