2 * FreeRTOS Kernel V10.1.1
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /*-----------------------------------------------------------
\r
29 * Implementation of functions defined in portable.h for the ARM CM4F port.
\r
30 *----------------------------------------------------------*/
\r
33 #include <intrinsics.h>
\r
35 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
36 all the API functions to use the MPU wrappers. That should only be done when
\r
37 task.h is included from an application file. */
\r
38 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
40 /* Scheduler includes. */
\r
41 #include "FreeRTOS.h"
\r
44 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
46 #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
49 #error This port can only be used when the project options are configured to enable hardware floating point support.
\r
52 #if( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 )
\r
53 #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
\r
56 #ifndef configSYSTICK_CLOCK_HZ
\r
57 #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
\r
58 /* Ensure the SysTick is clocked at the same frequency as the core. */
\r
59 #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
\r
61 /* The way the SysTick is clocked is not modified in case it is not the same
\r
63 #define portNVIC_SYSTICK_CLK_BIT ( 0 )
\r
66 /* Constants required to manipulate the core. Registers first... */
\r
67 #define portNVIC_SYSTICK_CTRL_REG ( * ( ( volatile uint32_t * ) 0xe000e010 ) )
\r
68 #define portNVIC_SYSTICK_LOAD_REG ( * ( ( volatile uint32_t * ) 0xe000e014 ) )
\r
69 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( * ( ( volatile uint32_t * ) 0xe000e018 ) )
\r
70 #define portNVIC_SYSPRI2_REG ( * ( ( volatile uint32_t * ) 0xe000ed20 ) )
\r
71 #define portNVIC_SYSPRI1_REG ( * ( ( volatile uint32_t * ) 0xe000ed1c ) )
\r
72 #define portNVIC_SYS_CTRL_STATE_REG ( * ( ( volatile uint32_t * ) 0xe000ed24 ) )
\r
73 #define portNVIC_MEM_FAULT_ENABLE ( 1UL << 16UL )
\r
75 /* Constants required to access and manipulate the MPU. */
\r
76 #define portMPU_TYPE_REG ( * ( ( volatile uint32_t * ) 0xe000ed90 ) )
\r
77 #define portMPU_REGION_BASE_ADDRESS_REG ( * ( ( volatile uint32_t * ) 0xe000ed9C ) )
\r
78 #define portMPU_REGION_ATTRIBUTE_REG ( * ( ( volatile uint32_t * ) 0xe000edA0 ) )
\r
79 #define portMPU_CTRL_REG ( * ( ( volatile uint32_t * ) 0xe000ed94 ) )
\r
80 #define portEXPECTED_MPU_TYPE_VALUE ( 8UL << 8UL ) /* 8 regions, unified. */
\r
81 #define portMPU_ENABLE ( 0x01UL )
\r
82 #define portMPU_BACKGROUND_ENABLE ( 1UL << 2UL )
\r
83 #define portPRIVILEGED_EXECUTION_START_ADDRESS ( 0UL )
\r
84 #define portMPU_REGION_VALID ( 0x10UL )
\r
85 #define portMPU_REGION_ENABLE ( 0x01UL )
\r
86 #define portPERIPHERALS_START_ADDRESS 0x40000000UL
\r
87 #define portPERIPHERALS_END_ADDRESS 0x5FFFFFFFUL
\r
89 /* ...then bits in the registers. */
\r
90 #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
\r
91 #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
\r
92 #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
\r
93 #define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL )
\r
94 #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
\r
96 /* Constants used to detect a Cortex-M7 r0p1 core, which should use the ARM_CM7
\r
98 #define portCPUID ( * ( ( volatile uint32_t * ) 0xE000ed00 ) )
\r
99 #define portCORTEX_M7_r0p1_ID ( 0x410FC271UL )
\r
100 #define portCORTEX_M7_r0p0_ID ( 0x410FC270UL )
\r
102 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )
\r
103 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )
\r
104 #define portNVIC_SVC_PRI ( ( ( uint32_t ) configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ) << 24UL )
\r
106 /* Constants required to check the validity of an interrupt priority. */
\r
107 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
\r
108 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
\r
109 #define portAIRCR_REG ( * ( ( volatile uint32_t * ) 0xE000ED0C ) )
\r
110 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
\r
111 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
\r
112 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
\r
113 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
\r
114 #define portPRIGROUP_SHIFT ( 8UL )
\r
116 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
\r
117 #define portVECTACTIVE_MASK ( 0xFFUL )
\r
119 /* Constants required to manipulate the VFP. */
\r
120 #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
\r
121 #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
\r
123 /* Constants required to set up the initial stack. */
\r
124 #define portINITIAL_XPSR ( 0x01000000 )
\r
125 #define portINITIAL_EXC_RETURN ( 0xfffffffd )
\r
126 #define portINITIAL_CONTROL_IF_UNPRIVILEGED ( 0x03 )
\r
127 #define portINITIAL_CONTROL_IF_PRIVILEGED ( 0x02 )
\r
129 /* Offsets in the stack to the parameters when inside the SVC handler. */
\r
130 #define portOFFSET_TO_PC ( 6 )
\r
132 /* The systick is a 24-bit counter. */
\r
133 #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
\r
135 /* A fiddle factor to estimate the number of SysTick counts that would have
\r
136 occurred while the SysTick counter is stopped during tickless idle
\r
138 #define portMISSED_COUNTS_FACTOR ( 45UL )
\r
140 /* For strict compliance with the Cortex-M spec the task start address should
\r
141 have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
\r
142 #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
\r
145 * Configure a number of standard MPU regions that are used by all tasks.
\r
147 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
\r
150 * Return the smallest MPU region size that a given number of bytes will fit
\r
151 * into. The region size is returned as the value that should be programmed
\r
152 * into the region attribute register for that region.
\r
154 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;
\r
157 * Checks to see if being called from the context of an unprivileged task, and
\r
158 * if so raises the privilege level and returns false - otherwise does nothing
\r
159 * other than return true.
\r
161 extern BaseType_t xPortRaisePrivilege( void );
\r
164 * Setup the timer to generate the tick interrupts. The implementation in this
\r
165 * file is weak to allow application writers to change the timer used to
\r
166 * generate the tick interrupt.
\r
168 void vPortSetupTimerInterrupt( void );
\r
171 * Exception handlers.
\r
173 void xPortSysTickHandler( void ) PRIVILEGED_FUNCTION;
\r
176 * Start first task is a separate function so it can be tested in isolation.
\r
178 extern void vPortStartFirstTask( void ) PRIVILEGED_FUNCTION;
\r
183 extern void vPortEnableVFP( void );
\r
186 * The C portion of the SVC handler.
\r
188 void vPortSVCHandler_C( uint32_t *pulParam );
\r
191 * Called from the SVC handler used to start the scheduler.
\r
193 extern void vPortRestoreContextOfFirstTask( void ) PRIVILEGED_FUNCTION;
\r
195 /*-----------------------------------------------------------*/
\r
197 /* Each task maintains its own interrupt status in the critical nesting
\r
199 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
\r
202 * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
\r
203 * FreeRTOS API functions are not called from interrupts that have been assigned
\r
204 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
206 #if( configASSERT_DEFINED == 1 )
\r
207 static uint8_t ucMaxSysCallPriority = 0;
\r
208 static uint32_t ulMaxPRIGROUPValue = 0;
\r
209 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
\r
210 #endif /* configASSERT_DEFINED */
\r
212 /*-----------------------------------------------------------*/
\r
215 * See header file for description.
\r
217 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters, BaseType_t xRunPrivileged )
\r
219 /* Simulate the stack frame as it would be created by a context switch
\r
222 /* Offset added to account for the way the MCU uses the stack on entry/exit
\r
223 of interrupts, and to ensure alignment. */
\r
226 *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
\r
228 *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */
\r
230 *pxTopOfStack = ( StackType_t ) 0; /* LR */
\r
232 /* Save code space by skipping register initialisation. */
\r
233 pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
\r
234 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
\r
236 /* A save method is being used that requires each task to maintain its
\r
237 own exec return value. */
\r
239 *pxTopOfStack = portINITIAL_EXC_RETURN;
\r
241 pxTopOfStack -= 9; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
\r
243 if( xRunPrivileged == pdTRUE )
\r
245 *pxTopOfStack = portINITIAL_CONTROL_IF_PRIVILEGED;
\r
249 *pxTopOfStack = portINITIAL_CONTROL_IF_UNPRIVILEGED;
\r
252 return pxTopOfStack;
\r
254 /*-----------------------------------------------------------*/
\r
256 void vPortSVCHandler_C( uint32_t *pulParam )
\r
258 uint8_t ucSVCNumber;
\r
260 /* The stack contains: r0, r1, r2, r3, r12, r14, the return address and
\r
261 xPSR. The first argument (r0) is pulParam[ 0 ]. */
\r
262 ucSVCNumber = ( ( uint8_t * ) pulParam[ portOFFSET_TO_PC ] )[ -2 ];
\r
263 switch( ucSVCNumber )
\r
265 case portSVC_START_SCHEDULER : portNVIC_SYSPRI1_REG |= portNVIC_SVC_PRI;
\r
266 vPortRestoreContextOfFirstTask();
\r
269 case portSVC_YIELD : portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
270 /* Barriers are normally not required
\r
271 but do ensure the code is completely
\r
272 within the specified behaviour for the
\r
274 __asm volatile( "dsb" ::: "memory" );
\r
275 __asm volatile( "isb" );
\r
279 case portSVC_RAISE_PRIVILEGE : __asm volatile
\r
281 " mrs r1, control \n" /* Obtain current control value. */
\r
282 " bic r1, r1, #1 \n" /* Set privilege bit. */
\r
283 " msr control, r1 \n" /* Write back new control value. */
\r
288 default : /* Unknown SVC call. */
\r
292 /*-----------------------------------------------------------*/
\r
295 * See header file for description.
\r
297 BaseType_t xPortStartScheduler( void )
\r
299 /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.
\r
300 See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
\r
301 configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY );
\r
303 /* This port can be used on all revisions of the Cortex-M7 core other than
\r
304 the r0p1 parts. r0p1 parts should use the port from the
\r
305 /source/portable/GCC/ARM_CM7/r0p1 directory. */
\r
306 configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );
\r
307 configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );
\r
309 #if( configASSERT_DEFINED == 1 )
\r
311 volatile uint32_t ulOriginalPriority;
\r
312 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
\r
313 volatile uint8_t ucMaxPriorityValue;
\r
315 /* Determine the maximum priority from which ISR safe FreeRTOS API
\r
316 functions can be called. ISR safe functions are those that end in
\r
317 "FromISR". FreeRTOS maintains separate thread and ISR API functions to
\r
318 ensure interrupt entry is as fast and simple as possible.
\r
320 Save the interrupt priority value that is about to be clobbered. */
\r
321 ulOriginalPriority = *pucFirstUserPriorityRegister;
\r
323 /* Determine the number of priority bits available. First write to all
\r
325 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
\r
327 /* Read the value back to see how many bits stuck. */
\r
328 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
\r
330 /* Use the same mask on the maximum system call priority. */
\r
331 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
\r
333 /* Calculate the maximum acceptable priority group value for the number
\r
334 of bits read back. */
\r
335 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;
\r
336 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
\r
338 ulMaxPRIGROUPValue--;
\r
339 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
\r
342 #ifdef __NVIC_PRIO_BITS
\r
344 /* Check the CMSIS configuration that defines the number of
\r
345 priority bits matches the number of priority bits actually queried
\r
346 from the hardware. */
\r
347 configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );
\r
351 #ifdef configPRIO_BITS
\r
353 /* Check the FreeRTOS configuration that defines the number of
\r
354 priority bits matches the number of priority bits actually queried
\r
355 from the hardware. */
\r
356 configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );
\r
360 /* Shift the priority group value back to its position within the AIRCR
\r
362 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
\r
363 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
\r
365 /* Restore the clobbered interrupt priority register to its original
\r
367 *pucFirstUserPriorityRegister = ulOriginalPriority;
\r
369 #endif /* conifgASSERT_DEFINED */
\r
371 /* Make PendSV and SysTick the lowest priority interrupts. */
\r
372 portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI;
\r
373 portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI;
\r
375 /* Configure the regions in the MPU that are common to all tasks. */
\r
378 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
380 vPortSetupTimerInterrupt();
\r
382 /* Initialise the critical nesting count ready for the first task. */
\r
383 uxCriticalNesting = 0;
\r
385 /* Ensure the VFP is enabled - it should be anyway. */
\r
388 /* Lazy save always. */
\r
389 *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
\r
391 /* Start the first task. */
\r
392 vPortStartFirstTask();
\r
394 /* Should not get here! */
\r
397 /*-----------------------------------------------------------*/
\r
399 void vPortEndScheduler( void )
\r
401 /* Not implemented in ports where there is nothing to return to.
\r
402 Artificially force an assert. */
\r
403 configASSERT( uxCriticalNesting == 1000UL );
\r
405 /*-----------------------------------------------------------*/
\r
407 void vPortEnterCritical( void )
\r
409 BaseType_t xRunningPrivileged = xPortRaisePrivilege();
\r
411 portDISABLE_INTERRUPTS();
\r
412 uxCriticalNesting++;
\r
414 vPortResetPrivilege( xRunningPrivileged );
\r
416 /* This is not the interrupt safe version of the enter critical function so
\r
417 assert() if it is being called from an interrupt context. Only API
\r
418 functions that end in "FromISR" can be used in an interrupt. Only assert if
\r
419 the critical nesting count is 1 to protect against recursive calls if the
\r
420 assert function also uses a critical section. */
\r
421 if( uxCriticalNesting == 1 )
\r
423 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
\r
426 /*-----------------------------------------------------------*/
\r
428 void vPortExitCritical( void )
\r
430 BaseType_t xRunningPrivileged = xPortRaisePrivilege();
\r
432 configASSERT( uxCriticalNesting );
\r
434 uxCriticalNesting--;
\r
435 if( uxCriticalNesting == 0 )
\r
437 portENABLE_INTERRUPTS();
\r
440 vPortResetPrivilege( xRunningPrivileged );
\r
442 /*-----------------------------------------------------------*/
\r
444 void xPortSysTickHandler( void )
\r
446 /* The SysTick runs at the lowest interrupt priority, so when this interrupt
\r
447 executes all interrupts must be unmasked. There is therefore no need to
\r
448 save and then restore the interrupt mask value as its value is already
\r
450 portDISABLE_INTERRUPTS();
\r
452 /* Increment the RTOS tick. */
\r
453 if( xTaskIncrementTick() != pdFALSE )
\r
455 /* A context switch is required. Context switching is performed in
\r
456 the PendSV interrupt. Pend the PendSV interrupt. */
\r
457 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
460 portENABLE_INTERRUPTS();
\r
462 /*-----------------------------------------------------------*/
\r
465 * Setup the systick timer to generate the tick interrupts at the required
\r
468 __weak void vPortSetupTimerInterrupt( void )
\r
470 /* Stop and clear the SysTick. */
\r
471 portNVIC_SYSTICK_CTRL_REG = 0UL;
\r
472 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
\r
474 /* Configure SysTick to interrupt at the requested rate. */
\r
475 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
\r
476 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
\r
478 /*-----------------------------------------------------------*/
\r
480 static void prvSetupMPU( void )
\r
482 extern uint32_t __privileged_functions_end__[];
\r
483 extern uint32_t __FLASH_segment_start__[];
\r
484 extern uint32_t __FLASH_segment_end__[];
\r
485 extern uint32_t __privileged_data_start__[];
\r
486 extern uint32_t __privileged_data_end__[];
\r
488 /* Check the expected MPU is present. */
\r
489 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
\r
491 /* First setup the entire flash for unprivileged read only access. */
\r
492 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
\r
493 ( portMPU_REGION_VALID ) |
\r
494 ( portUNPRIVILEGED_FLASH_REGION );
\r
496 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
\r
497 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
498 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
\r
499 ( portMPU_REGION_ENABLE );
\r
501 /* Setup the first 16K for privileged only access (even though less
\r
502 than 10K is actually being used). This is where the kernel code is
\r
504 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
\r
505 ( portMPU_REGION_VALID ) |
\r
506 ( portPRIVILEGED_FLASH_REGION );
\r
508 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
\r
509 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
510 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
\r
511 ( portMPU_REGION_ENABLE );
\r
513 /* Setup the privileged data RAM region. This is where the kernel data
\r
515 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
\r
516 ( portMPU_REGION_VALID ) |
\r
517 ( portPRIVILEGED_RAM_REGION );
\r
519 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
\r
520 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
521 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
\r
522 ( portMPU_REGION_ENABLE );
\r
524 /* By default allow everything to access the general peripherals. The
\r
525 system peripherals and registers are protected. */
\r
526 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
\r
527 ( portMPU_REGION_VALID ) |
\r
528 ( portGENERAL_PERIPHERALS_REGION );
\r
530 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
\r
531 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
\r
532 ( portMPU_REGION_ENABLE );
\r
534 /* Enable the memory fault exception. */
\r
535 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
\r
537 /* Enable the MPU with the background region configured. */
\r
538 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
\r
541 /*-----------------------------------------------------------*/
\r
543 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
\r
545 uint32_t ulRegionSize, ulReturnValue = 4;
\r
547 /* 32 is the smallest region size, 31 is the largest valid value for
\r
549 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
\r
551 if( ulActualSizeInBytes <= ulRegionSize )
\r
561 /* Shift the code by one before returning so it can be written directly
\r
562 into the the correct bit position of the attribute register. */
\r
563 return ( ulReturnValue << 1UL );
\r
565 /*-----------------------------------------------------------*/
\r
567 void vPortResetPrivilege( BaseType_t xRunningPrivileged )
\r
569 if( xRunningPrivileged != pdTRUE )
\r
571 __asm volatile ( " mrs r0, control \n" \
\r
572 " orr r0, r0, #1 \n" \
\r
573 " msr control, r0 \n" \
\r
574 :::"r0", "memory" );
\r
577 /*-----------------------------------------------------------*/
\r
579 void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMORY_REGION * const xRegions, StackType_t *pxBottomOfStack, uint32_t ulStackDepth )
\r
581 extern uint32_t __SRAM_segment_start__[];
\r
582 extern uint32_t __SRAM_segment_end__[];
\r
583 extern uint32_t __privileged_data_start__[];
\r
584 extern uint32_t __privileged_data_end__[];
\r
588 if( xRegions == NULL )
\r
590 /* No MPU regions are specified so allow access to all RAM. */
\r
591 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
\r
592 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
\r
593 ( portMPU_REGION_VALID ) |
\r
594 ( portSTACK_REGION );
\r
596 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
\r
597 ( portMPU_REGION_READ_WRITE ) |
\r
598 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
599 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
\r
600 ( portMPU_REGION_ENABLE );
\r
602 /* Re-instate the privileged only RAM region as xRegion[ 0 ] will have
\r
603 just removed the privileged only parameters. */
\r
604 xMPUSettings->xRegion[ 1 ].ulRegionBaseAddress =
\r
605 ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
\r
606 ( portMPU_REGION_VALID ) |
\r
607 ( portSTACK_REGION + 1 );
\r
609 xMPUSettings->xRegion[ 1 ].ulRegionAttribute =
\r
610 ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
\r
611 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
612 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
\r
613 ( portMPU_REGION_ENABLE );
\r
615 /* Invalidate all other regions. */
\r
616 for( ul = 2; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
\r
618 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;
\r
619 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
\r
624 /* This function is called automatically when the task is created - in
\r
625 which case the stack region parameters will be valid. At all other
\r
626 times the stack parameters will not be valid and it is assumed that the
\r
627 stack region has already been configured. */
\r
628 if( ulStackDepth > 0 )
\r
630 /* Define the region that allows access to the stack. */
\r
631 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
\r
632 ( ( uint32_t ) pxBottomOfStack ) |
\r
633 ( portMPU_REGION_VALID ) |
\r
634 ( portSTACK_REGION ); /* Region number. */
\r
636 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
\r
637 ( portMPU_REGION_READ_WRITE ) | /* Read and write. */
\r
638 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
\r
639 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
640 ( portMPU_REGION_ENABLE );
\r
645 for( ul = 1; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
\r
647 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
\r
649 /* Translate the generic region definition contained in
\r
650 xRegions into the CM3 specific MPU settings that are then
\r
651 stored in xMPUSettings. */
\r
652 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
\r
653 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
\r
654 ( portMPU_REGION_VALID ) |
\r
655 ( portSTACK_REGION + ul ); /* Region number. */
\r
657 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
\r
658 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
\r
659 ( xRegions[ lIndex ].ulParameters ) |
\r
660 ( portMPU_REGION_ENABLE );
\r
664 /* Invalidate the region. */
\r
665 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;
\r
666 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
\r
673 /*-----------------------------------------------------------*/
\r
675 #if( configASSERT_DEFINED == 1 )
\r
677 void vPortValidateInterruptPriority( void )
\r
679 uint32_t ulCurrentInterrupt;
\r
680 uint8_t ucCurrentPriority;
\r
682 /* Obtain the number of the currently executing interrupt. */
\r
683 __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) :: "memory" );
\r
685 /* Is the interrupt number a user defined interrupt? */
\r
686 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
\r
688 /* Look up the interrupt's priority. */
\r
689 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
\r
691 /* The following assertion will fail if a service routine (ISR) for
\r
692 an interrupt that has been assigned a priority above
\r
693 configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
\r
694 function. ISR safe FreeRTOS API functions must *only* be called
\r
695 from interrupts that have been assigned a priority at or below
\r
696 configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
698 Numerically low interrupt priority numbers represent logically high
\r
699 interrupt priorities, therefore the priority of the interrupt must
\r
700 be set to a value equal to or numerically *higher* than
\r
701 configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
703 Interrupts that use the FreeRTOS API must not be left at their
\r
704 default priority of zero as that is the highest possible priority,
\r
705 which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
\r
706 and therefore also guaranteed to be invalid.
\r
708 FreeRTOS maintains separate thread and ISR API functions to ensure
\r
709 interrupt entry is as fast and simple as possible.
\r
711 The following links provide detailed information:
\r
712 http://www.freertos.org/RTOS-Cortex-M3-M4.html
\r
713 http://www.freertos.org/FAQHelp.html */
\r
714 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
\r
717 /* Priority grouping: The interrupt controller (NVIC) allows the bits
\r
718 that define each interrupt's priority to be split between bits that
\r
719 define the interrupt's pre-emption priority bits and bits that define
\r
720 the interrupt's sub-priority. For simplicity all bits must be defined
\r
721 to be pre-emption priority bits. The following assertion will fail if
\r
722 this is not the case (if some bits represent a sub-priority).
\r
724 If the application only uses CMSIS libraries for interrupt
\r
725 configuration then the correct setting can be achieved on all Cortex-M
\r
726 devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
\r
727 scheduler. Note however that some vendor specific peripheral libraries
\r
728 assume a non-zero priority group setting, in which cases using a value
\r
729 of zero will result in unpredictable behaviour. */
\r
730 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
\r
733 #endif /* configASSERT_DEFINED */
\r