2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
29 /*-----------------------------------------------------------
\r
30 * Implementation of functions defined in portable.h for the ARM CM4F MPU port.
\r
31 *----------------------------------------------------------*/
\r
34 #include <intrinsics.h>
\r
36 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
37 * all the API functions to use the MPU wrappers. That should only be done when
\r
38 * task.h is included from an application file. */
\r
39 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
41 /* Scheduler includes. */
\r
42 #include "FreeRTOS.h"
\r
45 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
48 #error This port can only be used when the project options are configured to enable hardware floating point support.
\r
51 #if ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 )
\r
52 #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http: /*www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
\r
55 #ifndef configSYSTICK_CLOCK_HZ
\r
56 #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
\r
57 /* Ensure the SysTick is clocked at the same frequency as the core. */
\r
58 #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_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
\r
71 #define portNVIC_SHPR2_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 ( portTOTAL_NUM_REGIONS << 8UL )
\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 * Setup the timer to generate the tick interrupts. The implementation in this
\r
158 * file is weak to allow application writers to change the timer used to
\r
159 * generate the tick interrupt.
\r
161 void vPortSetupTimerInterrupt( void );
\r
164 * Exception handlers.
\r
166 void xPortSysTickHandler( void ) PRIVILEGED_FUNCTION;
\r
169 * Start first task is a separate function so it can be tested in isolation.
\r
171 extern void vPortStartFirstTask( void ) PRIVILEGED_FUNCTION;
\r
176 extern void vPortEnableVFP( void );
\r
179 * The C portion of the SVC handler.
\r
181 void vPortSVCHandler_C( uint32_t * pulParam );
\r
184 * Called from the SVC handler used to start the scheduler.
\r
186 extern void vPortRestoreContextOfFirstTask( void ) PRIVILEGED_FUNCTION;
\r
189 * @brief Calls the port specific code to raise the privilege.
\r
191 * @return pdFALSE if privilege was raised, pdTRUE otherwise.
\r
193 extern BaseType_t xPortRaisePrivilege( void );
\r
196 * @brief If xRunningPrivileged is not pdTRUE, calls the port specific
\r
197 * code to reset the privilege, otherwise does nothing.
\r
199 extern void vPortResetPrivilege( BaseType_t xRunningPrivileged );
\r
200 /*-----------------------------------------------------------*/
\r
202 /* Each task maintains its own interrupt status in the critical nesting
\r
204 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
\r
207 * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
\r
208 * FreeRTOS API functions are not called from interrupts that have been assigned
\r
209 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
211 #if ( configASSERT_DEFINED == 1 )
\r
212 static uint8_t ucMaxSysCallPriority = 0;
\r
213 static uint32_t ulMaxPRIGROUPValue = 0;
\r
214 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
\r
215 #endif /* configASSERT_DEFINED */
\r
217 /*-----------------------------------------------------------*/
\r
220 * See header file for description.
\r
222 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
\r
223 TaskFunction_t pxCode,
\r
224 void * pvParameters,
\r
225 BaseType_t xRunPrivileged )
\r
227 /* Simulate the stack frame as it would be created by a context switch
\r
230 /* Offset added to account for the way the MCU uses the stack on entry/exit
\r
231 * of interrupts, and to ensure alignment. */
\r
234 *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
\r
236 *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */
\r
238 *pxTopOfStack = ( StackType_t ) 0; /* LR */
\r
240 /* Save code space by skipping register initialisation. */
\r
241 pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
\r
242 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
\r
244 /* A save method is being used that requires each task to maintain its
\r
245 * own exec return value. */
\r
247 *pxTopOfStack = portINITIAL_EXC_RETURN;
\r
249 pxTopOfStack -= 9; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
\r
251 if( xRunPrivileged == pdTRUE )
\r
253 *pxTopOfStack = portINITIAL_CONTROL_IF_PRIVILEGED;
\r
257 *pxTopOfStack = portINITIAL_CONTROL_IF_UNPRIVILEGED;
\r
260 return pxTopOfStack;
\r
262 /*-----------------------------------------------------------*/
\r
264 void vPortSVCHandler_C( uint32_t * pulParam )
\r
266 uint8_t ucSVCNumber;
\r
269 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
\r
270 extern uint32_t __syscalls_flash_start__[];
\r
271 extern uint32_t __syscalls_flash_end__[];
\r
272 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
\r
274 /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first
\r
275 * argument (r0) is pulParam[ 0 ]. */
\r
276 ulPC = pulParam[ portOFFSET_TO_PC ];
\r
277 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
\r
279 switch( ucSVCNumber )
\r
281 case portSVC_START_SCHEDULER:
\r
282 portNVIC_SHPR2_REG |= portNVIC_SVC_PRI;
\r
283 vPortRestoreContextOfFirstTask();
\r
286 case portSVC_YIELD:
\r
287 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
289 /* Barriers are normally not required
\r
290 * but do ensure the code is completely
\r
291 * within the specified behaviour for the
\r
293 __asm volatile ( "dsb" ::: "memory" );
\r
294 __asm volatile ( "isb" );
\r
298 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
\r
299 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the
\r
300 * svc was raised from any of the
\r
303 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
\r
304 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
\r
308 " mrs r1, control \n"/* Obtain current control value. */
\r
309 " bic r1, r1, #1 \n"/* Set privilege bit. */
\r
310 " msr control, r1 \n"/* Write back new control value. */
\r
316 #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
\r
317 case portSVC_RAISE_PRIVILEGE:
\r
320 " mrs r1, control \n"/* Obtain current control value. */
\r
321 " bic r1, r1, #1 \n"/* Set privilege bit. */
\r
322 " msr control, r1 \n"/* Write back new control value. */
\r
326 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
\r
328 default: /* Unknown SVC call. */
\r
332 /*-----------------------------------------------------------*/
\r
335 * See header file for description.
\r
337 BaseType_t xPortStartScheduler( void )
\r
339 /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.
\r
340 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
\r
341 configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY );
\r
343 /* This port can be used on all revisions of the Cortex-M7 core other than
\r
344 * the r0p1 parts. r0p1 parts should use the port from the
\r
345 * /source/portable/GCC/ARM_CM7/r0p1 directory. */
\r
346 configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );
\r
347 configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );
\r
349 #if ( configASSERT_DEFINED == 1 )
\r
351 volatile uint32_t ulOriginalPriority;
\r
352 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
\r
353 volatile uint8_t ucMaxPriorityValue;
\r
355 /* Determine the maximum priority from which ISR safe FreeRTOS API
\r
356 * functions can be called. ISR safe functions are those that end in
\r
357 * "FromISR". FreeRTOS maintains separate thread and ISR API functions to
\r
358 * ensure interrupt entry is as fast and simple as possible.
\r
360 * Save the interrupt priority value that is about to be clobbered. */
\r
361 ulOriginalPriority = *pucFirstUserPriorityRegister;
\r
363 /* Determine the number of priority bits available. First write to all
\r
364 * possible bits. */
\r
365 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
\r
367 /* Read the value back to see how many bits stuck. */
\r
368 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
\r
370 /* Use the same mask on the maximum system call priority. */
\r
371 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
\r
373 /* Calculate the maximum acceptable priority group value for the number
\r
374 * of bits read back. */
\r
375 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;
\r
377 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
\r
379 ulMaxPRIGROUPValue--;
\r
380 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
\r
383 #ifdef __NVIC_PRIO_BITS
\r
385 /* Check the CMSIS configuration that defines the number of
\r
386 * priority bits matches the number of priority bits actually queried
\r
387 * from the hardware. */
\r
388 configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );
\r
392 #ifdef configPRIO_BITS
\r
394 /* Check the FreeRTOS configuration that defines the number of
\r
395 * priority bits matches the number of priority bits actually queried
\r
396 * from the hardware. */
\r
397 configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );
\r
401 /* Shift the priority group value back to its position within the AIRCR
\r
403 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
\r
404 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
\r
406 /* Restore the clobbered interrupt priority register to its original
\r
408 *pucFirstUserPriorityRegister = ulOriginalPriority;
\r
410 #endif /* conifgASSERT_DEFINED */
\r
412 /* Make PendSV and SysTick the lowest priority interrupts. */
\r
413 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
\r
414 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
\r
416 /* Configure the regions in the MPU that are common to all tasks. */
\r
419 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
421 vPortSetupTimerInterrupt();
\r
423 /* Initialise the critical nesting count ready for the first task. */
\r
424 uxCriticalNesting = 0;
\r
426 /* Ensure the VFP is enabled - it should be anyway. */
\r
429 /* Lazy save always. */
\r
430 *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
\r
432 /* Start the first task. */
\r
433 vPortStartFirstTask();
\r
435 /* Should not get here! */
\r
438 /*-----------------------------------------------------------*/
\r
440 void vPortEndScheduler( void )
\r
442 /* Not implemented in ports where there is nothing to return to.
\r
443 * Artificially force an assert. */
\r
444 configASSERT( uxCriticalNesting == 1000UL );
\r
446 /*-----------------------------------------------------------*/
\r
448 void vPortEnterCritical( void )
\r
450 BaseType_t xRunningPrivileged = xPortRaisePrivilege();
\r
452 portDISABLE_INTERRUPTS();
\r
453 uxCriticalNesting++;
\r
455 vPortResetPrivilege( xRunningPrivileged );
\r
457 /* This is not the interrupt safe version of the enter critical function so
\r
458 * assert() if it is being called from an interrupt context. Only API
\r
459 * functions that end in "FromISR" can be used in an interrupt. Only assert if
\r
460 * the critical nesting count is 1 to protect against recursive calls if the
\r
461 * assert function also uses a critical section. */
\r
462 if( uxCriticalNesting == 1 )
\r
464 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
\r
467 /*-----------------------------------------------------------*/
\r
469 void vPortExitCritical( void )
\r
471 BaseType_t xRunningPrivileged = xPortRaisePrivilege();
\r
473 configASSERT( uxCriticalNesting );
\r
475 uxCriticalNesting--;
\r
477 if( uxCriticalNesting == 0 )
\r
479 portENABLE_INTERRUPTS();
\r
482 vPortResetPrivilege( xRunningPrivileged );
\r
484 /*-----------------------------------------------------------*/
\r
486 void xPortSysTickHandler( void )
\r
488 /* The SysTick runs at the lowest interrupt priority, so when this interrupt
\r
489 * executes all interrupts must be unmasked. There is therefore no need to
\r
490 * save and then restore the interrupt mask value as its value is already
\r
492 portDISABLE_INTERRUPTS();
\r
494 /* Increment the RTOS tick. */
\r
495 if( xTaskIncrementTick() != pdFALSE )
\r
497 /* A context switch is required. Context switching is performed in
\r
498 * the PendSV interrupt. Pend the PendSV interrupt. */
\r
499 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
502 portENABLE_INTERRUPTS();
\r
504 /*-----------------------------------------------------------*/
\r
507 * Setup the systick timer to generate the tick interrupts at the required
\r
510 __weak void vPortSetupTimerInterrupt( void )
\r
512 /* Stop and clear the SysTick. */
\r
513 portNVIC_SYSTICK_CTRL_REG = 0UL;
\r
514 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
\r
516 /* Configure SysTick to interrupt at the requested rate. */
\r
517 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
\r
518 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
\r
520 /*-----------------------------------------------------------*/
\r
522 static void prvSetupMPU( void )
\r
524 extern uint32_t __privileged_functions_start__[];
\r
525 extern uint32_t __privileged_functions_end__[];
\r
526 extern uint32_t __FLASH_segment_start__[];
\r
527 extern uint32_t __FLASH_segment_end__[];
\r
528 extern uint32_t __privileged_data_start__[];
\r
529 extern uint32_t __privileged_data_end__[];
\r
531 /* The only permitted number of regions are 8 or 16. */
\r
532 configASSERT( ( portTOTAL_NUM_REGIONS == 8 ) || ( portTOTAL_NUM_REGIONS == 16 ) );
\r
534 /* Ensure that the configTOTAL_MPU_REGIONS is configured correctly. */
\r
535 configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );
\r
537 /* Check the expected MPU is present. */
\r
538 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
\r
540 /* First setup the unprivileged flash for unprivileged read only access. */
\r
541 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
\r
542 ( portMPU_REGION_VALID ) |
\r
543 ( portUNPRIVILEGED_FLASH_REGION );
\r
545 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
\r
546 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
\r
547 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
\r
548 ( portMPU_REGION_ENABLE );
\r
550 /* Setup the privileged flash for privileged only access. This is where
\r
551 * the kernel code is placed. */
\r
552 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
\r
553 ( portMPU_REGION_VALID ) |
\r
554 ( portPRIVILEGED_FLASH_REGION );
\r
556 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
\r
557 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
\r
558 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
\r
559 ( portMPU_REGION_ENABLE );
\r
561 /* Setup the privileged data RAM region. This is where the kernel data
\r
563 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
\r
564 ( portMPU_REGION_VALID ) |
\r
565 ( portPRIVILEGED_RAM_REGION );
\r
567 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
\r
568 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
\r
569 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
\r
570 ( portMPU_REGION_ENABLE );
\r
572 /* By default allow everything to access the general peripherals. The
\r
573 * system peripherals and registers are protected. */
\r
574 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
\r
575 ( portMPU_REGION_VALID ) |
\r
576 ( portGENERAL_PERIPHERALS_REGION );
\r
578 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
\r
579 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
\r
580 ( portMPU_REGION_ENABLE );
\r
582 /* Enable the memory fault exception. */
\r
583 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
\r
585 /* Enable the MPU with the background region configured. */
\r
586 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
\r
589 /*-----------------------------------------------------------*/
\r
591 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
\r
593 uint32_t ulRegionSize, ulReturnValue = 4;
\r
595 /* 32 is the smallest region size, 31 is the largest valid value for
\r
596 * ulReturnValue. */
\r
597 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
\r
599 if( ulActualSizeInBytes <= ulRegionSize )
\r
609 /* Shift the code by one before returning so it can be written directly
\r
610 * into the the correct bit position of the attribute register. */
\r
611 return( ulReturnValue << 1UL );
\r
613 /*-----------------------------------------------------------*/
\r
615 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
\r
616 const struct xMEMORY_REGION * const xRegions,
\r
617 StackType_t * pxBottomOfStack,
\r
618 uint32_t ulStackDepth )
\r
620 extern uint32_t __SRAM_segment_start__[];
\r
621 extern uint32_t __SRAM_segment_end__[];
\r
622 extern uint32_t __privileged_data_start__[];
\r
623 extern uint32_t __privileged_data_end__[];
\r
627 if( xRegions == NULL )
\r
629 /* No MPU regions are specified so allow access to all RAM. */
\r
630 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
\r
631 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
\r
632 ( portMPU_REGION_VALID ) |
\r
633 ( portSTACK_REGION );
\r
635 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
\r
636 ( portMPU_REGION_READ_WRITE ) |
\r
637 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
\r
638 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
\r
639 ( portMPU_REGION_ENABLE );
\r
641 /* Re-instate the privileged only RAM region as xRegion[ 0 ] will have
\r
642 * just removed the privileged only parameters. */
\r
643 xMPUSettings->xRegion[ 1 ].ulRegionBaseAddress =
\r
644 ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
\r
645 ( portMPU_REGION_VALID ) |
\r
646 ( portSTACK_REGION + 1 );
\r
648 xMPUSettings->xRegion[ 1 ].ulRegionAttribute =
\r
649 ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
\r
650 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
\r
651 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
\r
652 ( portMPU_REGION_ENABLE );
\r
654 /* Invalidate all other regions. */
\r
655 for( ul = 2; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
\r
657 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;
\r
658 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
\r
663 /* This function is called automatically when the task is created - in
\r
664 * which case the stack region parameters will be valid. At all other
\r
665 * times the stack parameters will not be valid and it is assumed that the
\r
666 * stack region has already been configured. */
\r
667 if( ulStackDepth > 0 )
\r
669 /* Define the region that allows access to the stack. */
\r
670 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
\r
671 ( ( uint32_t ) pxBottomOfStack ) |
\r
672 ( portMPU_REGION_VALID ) |
\r
673 ( portSTACK_REGION ); /* Region number. */
\r
675 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
\r
676 ( portMPU_REGION_READ_WRITE ) | /* Read and write. */
\r
677 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
\r
678 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
\r
679 ( portMPU_REGION_ENABLE );
\r
684 for( ul = 1; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
\r
686 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
\r
688 /* Translate the generic region definition contained in
\r
689 * xRegions into the CM4 specific MPU settings that are then
\r
690 * stored in xMPUSettings. */
\r
691 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
\r
692 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
\r
693 ( portMPU_REGION_VALID ) |
\r
694 ( portSTACK_REGION + ul ); /* Region number. */
\r
696 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
\r
697 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
\r
698 ( xRegions[ lIndex ].ulParameters ) |
\r
699 ( portMPU_REGION_ENABLE );
\r
703 /* Invalidate the region. */
\r
704 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;
\r
705 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
\r
712 /*-----------------------------------------------------------*/
\r
714 #if ( configASSERT_DEFINED == 1 )
\r
716 void vPortValidateInterruptPriority( void )
\r
718 uint32_t ulCurrentInterrupt;
\r
719 uint8_t ucCurrentPriority;
\r
721 /* Obtain the number of the currently executing interrupt. */
\r
722 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
\r
724 /* Is the interrupt number a user defined interrupt? */
\r
725 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
\r
727 /* Look up the interrupt's priority. */
\r
728 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
\r
730 /* The following assertion will fail if a service routine (ISR) for
\r
731 * an interrupt that has been assigned a priority above
\r
732 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
\r
733 * function. ISR safe FreeRTOS API functions must *only* be called
\r
734 * from interrupts that have been assigned a priority at or below
\r
735 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
737 * Numerically low interrupt priority numbers represent logically high
\r
738 * interrupt priorities, therefore the priority of the interrupt must
\r
739 * be set to a value equal to or numerically *higher* than
\r
740 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
742 * Interrupts that use the FreeRTOS API must not be left at their
\r
743 * default priority of zero as that is the highest possible priority,
\r
744 * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
\r
745 * and therefore also guaranteed to be invalid.
\r
747 * FreeRTOS maintains separate thread and ISR API functions to ensure
\r
748 * interrupt entry is as fast and simple as possible.
\r
750 * The following links provide detailed information:
\r
751 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
\r
752 * https://www.FreeRTOS.org/FAQHelp.html */
\r
753 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
\r
756 /* Priority grouping: The interrupt controller (NVIC) allows the bits
\r
757 * that define each interrupt's priority to be split between bits that
\r
758 * define the interrupt's pre-emption priority bits and bits that define
\r
759 * the interrupt's sub-priority. For simplicity all bits must be defined
\r
760 * to be pre-emption priority bits. The following assertion will fail if
\r
761 * this is not the case (if some bits represent a sub-priority).
\r
763 * If the application only uses CMSIS libraries for interrupt
\r
764 * configuration then the correct setting can be achieved on all Cortex-M
\r
765 * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
\r
766 * scheduler. Note however that some vendor specific peripheral libraries
\r
767 * assume a non-zero priority group setting, in which cases using a value
\r
768 * of zero will result in unpredictable behaviour. */
\r
769 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
\r
772 #endif /* configASSERT_DEFINED */
\r
773 /*-----------------------------------------------------------*/
\r