2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the ARM CM4F MPU port.
31 *----------------------------------------------------------*/
34 #include <intrinsics.h>
36 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
37 * all the API functions to use the MPU wrappers. That should only be done when
38 * task.h is included from an application file. */
39 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
41 /* Scheduler includes. */
45 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
48 #error This port can only be used when the project options are configured to enable hardware floating point support.
51 #if ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 )
52 #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http: /*www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
55 #ifndef configSYSTICK_CLOCK_HZ
56 #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
57 /* Ensure the SysTick is clocked at the same frequency as the core. */
58 #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
61 /* The way the SysTick is clocked is not modified in case it is not the same
63 #define portNVIC_SYSTICK_CLK_BIT ( 0 )
66 #ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
67 #warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
68 #define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
71 /* Constants required to manipulate the core. Registers first... */
72 #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
73 #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
74 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
75 #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
76 #define portNVIC_SHPR2_REG ( *( ( volatile uint32_t * ) 0xe000ed1c ) )
77 #define portNVIC_SYS_CTRL_STATE_REG ( *( ( volatile uint32_t * ) 0xe000ed24 ) )
78 #define portNVIC_MEM_FAULT_ENABLE ( 1UL << 16UL )
80 /* Constants required to access and manipulate the MPU. */
81 #define portMPU_TYPE_REG ( *( ( volatile uint32_t * ) 0xe000ed90 ) )
82 #define portMPU_REGION_BASE_ADDRESS_REG ( *( ( volatile uint32_t * ) 0xe000ed9C ) )
83 #define portMPU_REGION_ATTRIBUTE_REG ( *( ( volatile uint32_t * ) 0xe000edA0 ) )
84 #define portMPU_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed94 ) )
85 #define portEXPECTED_MPU_TYPE_VALUE ( configTOTAL_MPU_REGIONS << 8UL )
86 #define portMPU_ENABLE ( 0x01UL )
87 #define portMPU_BACKGROUND_ENABLE ( 1UL << 2UL )
88 #define portPRIVILEGED_EXECUTION_START_ADDRESS ( 0UL )
89 #define portMPU_REGION_VALID ( 0x10UL )
90 #define portMPU_REGION_ENABLE ( 0x01UL )
91 #define portPERIPHERALS_START_ADDRESS 0x40000000UL
92 #define portPERIPHERALS_END_ADDRESS 0x5FFFFFFFUL
94 /* ...then bits in the registers. */
95 #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
96 #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
97 #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
98 #define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL )
99 #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
101 /* Constants used to detect Cortex-M7 r0p0 and r0p1 cores, and ensure
102 * that a work around is active for errata 837070. */
103 #define portCPUID ( *( ( volatile uint32_t * ) 0xE000ed00 ) )
104 #define portCORTEX_M7_r0p1_ID ( 0x410FC271UL )
105 #define portCORTEX_M7_r0p0_ID ( 0x410FC270UL )
107 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
108 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
109 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
110 #define portNVIC_SVC_PRI ( ( ( uint32_t ) configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ) << 24UL )
112 /* Constants required to check the validity of an interrupt priority. */
113 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
114 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
115 #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
116 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
117 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
118 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
119 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
120 #define portPRIGROUP_SHIFT ( 8UL )
122 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
123 #define portVECTACTIVE_MASK ( 0xFFUL )
125 /* Constants required to manipulate the VFP. */
126 #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
127 #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
129 /* Constants required to set up the initial stack. */
130 #define portINITIAL_XPSR ( 0x01000000 )
131 #define portINITIAL_EXC_RETURN ( 0xfffffffd )
132 #define portINITIAL_CONTROL_IF_UNPRIVILEGED ( 0x03 )
133 #define portINITIAL_CONTROL_IF_PRIVILEGED ( 0x02 )
135 /* Constants used during system call enter and exit. */
136 #define portPSR_STACK_PADDING_MASK ( 1UL << 9UL )
137 #define portEXC_RETURN_STACK_FRAME_TYPE_MASK ( 1UL << 4UL )
139 /* Offsets in the stack to the parameters when inside the SVC handler. */
140 #define portOFFSET_TO_LR ( 5 )
141 #define portOFFSET_TO_PC ( 6 )
142 #define portOFFSET_TO_PSR ( 7 )
144 /* The systick is a 24-bit counter. */
145 #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
147 /* A fiddle factor to estimate the number of SysTick counts that would have
148 * occurred while the SysTick counter is stopped during tickless idle
150 #define portMISSED_COUNTS_FACTOR ( 45UL )
152 /* For strict compliance with the Cortex-M spec the task start address should
153 * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
154 #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
156 /* Does addr lie within [start, end] address range? */
157 #define portIS_ADDRESS_WITHIN_RANGE( addr, start, end ) \
158 ( ( ( addr ) >= ( start ) ) && ( ( addr ) <= ( end ) ) )
160 /* Is the access request satisfied by the available permissions? */
161 #define portIS_AUTHORIZED( accessRequest, permissions ) \
162 ( ( ( permissions ) & ( accessRequest ) ) == accessRequest )
164 /* Max value that fits in a uint32_t type. */
165 #define portUINT32_MAX ( ~( ( uint32_t ) 0 ) )
167 /* Check if adding a and b will result in overflow. */
168 #define portADD_UINT32_WILL_OVERFLOW( a, b ) ( ( a ) > ( portUINT32_MAX - ( b ) ) )
169 /*-----------------------------------------------------------*/
172 * Configure a number of standard MPU regions that are used by all tasks.
174 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
177 * Return the smallest MPU region size that a given number of bytes will fit
178 * into. The region size is returned as the value that should be programmed
179 * into the region attribute register for that region.
181 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;
184 * Setup the timer to generate the tick interrupts. The implementation in this
185 * file is weak to allow application writers to change the timer used to
186 * generate the tick interrupt.
188 void vPortSetupTimerInterrupt( void );
191 * Exception handlers.
193 void xPortSysTickHandler( void ) PRIVILEGED_FUNCTION;
196 * Start first task is a separate function so it can be tested in isolation.
198 extern void vPortStartFirstTask( void ) PRIVILEGED_FUNCTION;
203 extern void vPortEnableVFP( void );
206 * The C portion of the SVC handler.
208 void vPortSVCHandler_C( uint32_t * pulParam ) PRIVILEGED_FUNCTION;
211 * Called from the SVC handler used to start the scheduler.
213 extern void vPortRestoreContextOfFirstTask( void ) PRIVILEGED_FUNCTION;
216 * @brief Enter critical section.
218 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
219 void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
221 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
225 * @brief Exit from critical section.
227 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
228 void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
230 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
233 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
236 * @brief Sets up the system call stack so that upon returning from
237 * SVC, the system call stack is used.
239 * It is used for the system calls with up to 4 parameters.
241 * @param pulTaskStack The current SP when the SVC was raised.
242 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
244 void vSystemCallEnter( uint32_t * pulTaskStack,
245 uint32_t ulLR ) PRIVILEGED_FUNCTION;
247 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
249 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
252 * @brief Sets up the system call stack so that upon returning from
253 * SVC, the system call stack is used.
255 * It is used for the system calls with 5 parameters.
257 * @param pulTaskStack The current SP when the SVC was raised.
258 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
260 void vSystemCallEnter_1( uint32_t * pulTaskStack,
261 uint32_t ulLR ) PRIVILEGED_FUNCTION;
263 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
265 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
268 * @brief Sets up the task stack so that upon returning from
269 * SVC, the task stack is used again.
271 * @param pulSystemCallStack The current SP when the SVC was raised.
272 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
274 void vSystemCallExit( uint32_t * pulSystemCallStack,
275 uint32_t ulLR ) PRIVILEGED_FUNCTION;
277 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
280 * @brief Checks whether or not the calling task is privileged.
282 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
284 BaseType_t xPortIsTaskPrivileged( void ) PRIVILEGED_FUNCTION;
286 /*-----------------------------------------------------------*/
288 /* Each task maintains its own interrupt status in the critical nesting
290 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
293 * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
294 * FreeRTOS API functions are not called from interrupts that have been assigned
295 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
297 #if ( configASSERT_DEFINED == 1 )
298 static uint8_t ucMaxSysCallPriority = 0;
299 static uint32_t ulMaxPRIGROUPValue = 0;
300 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
301 #endif /* configASSERT_DEFINED */
303 /*-----------------------------------------------------------*/
306 * See header file for description.
308 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
309 TaskFunction_t pxCode,
311 BaseType_t xRunPrivileged,
312 xMPU_SETTINGS * xMPUSettings )
314 if( xRunPrivileged == pdTRUE )
316 xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
317 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_PRIVILEGED;
321 xMPUSettings->ulTaskFlags &= ( ~portTASK_IS_PRIVILEGED_FLAG );
322 xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_UNPRIVILEGED;
325 xMPUSettings->ulContext[ 1 ] = 0x04040404; /* r4. */
326 xMPUSettings->ulContext[ 2 ] = 0x05050505; /* r5. */
327 xMPUSettings->ulContext[ 3 ] = 0x06060606; /* r6. */
328 xMPUSettings->ulContext[ 4 ] = 0x07070707; /* r7. */
329 xMPUSettings->ulContext[ 5 ] = 0x08080808; /* r8. */
330 xMPUSettings->ulContext[ 6 ] = 0x09090909; /* r9. */
331 xMPUSettings->ulContext[ 7 ] = 0x10101010; /* r10. */
332 xMPUSettings->ulContext[ 8 ] = 0x11111111; /* r11. */
333 xMPUSettings->ulContext[ 9 ] = portINITIAL_EXC_RETURN; /* EXC_RETURN. */
335 xMPUSettings->ulContext[ 10 ] = ( uint32_t ) ( pxTopOfStack - 8 ); /* PSP with the hardware saved stack. */
336 xMPUSettings->ulContext[ 11 ] = ( uint32_t ) pvParameters; /* r0. */
337 xMPUSettings->ulContext[ 12 ] = 0x01010101; /* r1. */
338 xMPUSettings->ulContext[ 13 ] = 0x02020202; /* r2. */
339 xMPUSettings->ulContext[ 14 ] = 0x03030303; /* r3. */
340 xMPUSettings->ulContext[ 15 ] = 0x12121212; /* r12. */
341 xMPUSettings->ulContext[ 16 ] = 0; /* LR. */
342 xMPUSettings->ulContext[ 17 ] = ( ( uint32_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC. */
343 xMPUSettings->ulContext[ 18 ] = portINITIAL_XPSR; /* xPSR. */
345 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
347 /* Ensure that the system call stack is double word aligned. */
348 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
349 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
350 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
352 /* This is not NULL only for the duration of a system call. */
353 xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
355 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
357 return &( xMPUSettings->ulContext[ 19 ] );
359 /*-----------------------------------------------------------*/
361 void vPortSVCHandler_C( uint32_t * pulParam ) /* PRIVILEGED_FUNCTION */
366 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
367 extern uint32_t __syscalls_flash_start__[];
368 extern uint32_t __syscalls_flash_end__[];
369 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
371 /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first
372 * argument (r0) is pulParam[ 0 ]. */
373 ulPC = pulParam[ portOFFSET_TO_PC ];
374 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
376 switch( ucSVCNumber )
378 case portSVC_START_SCHEDULER:
379 portNVIC_SHPR2_REG |= portNVIC_SVC_PRI;
380 vPortRestoreContextOfFirstTask();
384 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
386 /* Barriers are normally not required
387 * but do ensure the code is completely
388 * within the specified behaviour for the
390 __asm volatile ( "dsb" ::: "memory" );
391 __asm volatile ( "isb" );
395 #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
396 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the
397 * svc was raised from any of the
400 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
401 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
405 " mrs r1, control \n" /* Obtain current control value. */
406 " bic r1, r1, #1 \n" /* Set privilege bit. */
407 " msr control, r1 \n" /* Write back new control value. */
413 #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
414 case portSVC_RAISE_PRIVILEGE:
417 " mrs r1, control \n" /* Obtain current control value. */
418 " bic r1, r1, #1 \n" /* Set privilege bit. */
419 " msr control, r1 \n" /* Write back new control value. */
423 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
425 default: /* Unknown SVC call. */
429 /*-----------------------------------------------------------*/
431 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
433 void vSystemCallEnter( uint32_t * pulTaskStack,
434 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
436 extern TaskHandle_t pxCurrentTCB;
437 xMPU_SETTINGS * pxMpuSettings;
438 uint32_t * pulSystemCallStack;
439 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
441 #if defined( __ARMCC_VERSION )
443 /* Declaration when these variable are defined in code instead of being
444 * exported from linker scripts. */
445 extern uint32_t * __syscalls_flash_start__;
446 extern uint32_t * __syscalls_flash_end__;
448 /* Declaration when these variable are exported from linker scripts. */
449 extern uint32_t __syscalls_flash_start__[];
450 extern uint32_t __syscalls_flash_end__[];
451 #endif /* #if defined( __ARMCC_VERSION ) */
453 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
455 /* If the request did not come from the system call section, do nothing. */
456 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
457 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
459 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
460 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
462 /* This is not NULL only for the duration of the system call. */
463 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
465 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
467 /* Extended frame i.e. FPU in use. */
468 ulStackFrameSize = 26;
470 " vpush {s0} \n" /* Trigger lazy stacking. */
471 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
477 /* Standard frame i.e. FPU not in use. */
478 ulStackFrameSize = 8;
481 /* Make space on the system call stack for the stack frame. */
482 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
484 /* Copy the stack frame. */
485 for( i = 0; i < ulStackFrameSize; i++ )
487 pulSystemCallStack[ i ] = pulTaskStack[ i ];
490 /* Use the pulSystemCallStack in thread mode. */
491 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
493 /* Raise the privilege for the duration of the system call. */
495 " mrs r1, control \n" /* Obtain current control value. */
496 " bic r1, #1 \n" /* Clear nPRIV bit. */
497 " msr control, r1 \n" /* Write back new control value. */
501 /* Remember the location where we should copy the stack frame when we exit from
502 * the system call. */
503 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
505 /* Store the value of the Link Register before the SVC was raised. We need to
506 * restore it when we exit from the system call. */
507 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
509 /* Record if the hardware used padding to force the stack pointer
510 * to be double word aligned. */
511 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
513 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
517 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
520 /* We ensure in pxPortInitialiseStack that the system call stack is
521 * double word aligned and therefore, there is no need of padding.
522 * Clear the bit[9] of stacked xPSR. */
523 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
527 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
528 /*-----------------------------------------------------------*/
530 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
532 void vSystemCallEnter_1( uint32_t * pulTaskStack,
533 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
535 extern TaskHandle_t pxCurrentTCB;
536 xMPU_SETTINGS * pxMpuSettings;
537 uint32_t * pulSystemCallStack;
538 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
540 #if defined( __ARMCC_VERSION )
542 /* Declaration when these variable are defined in code instead of being
543 * exported from linker scripts. */
544 extern uint32_t * __syscalls_flash_start__;
545 extern uint32_t * __syscalls_flash_end__;
547 /* Declaration when these variable are exported from linker scripts. */
548 extern uint32_t __syscalls_flash_start__[];
549 extern uint32_t __syscalls_flash_end__[];
550 #endif /* #if defined( __ARMCC_VERSION ) */
552 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
554 /* If the request did not come from the system call section, do nothing. */
555 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
556 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
558 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
559 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
561 /* This is not NULL only for the duration of the system call. */
562 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
564 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
566 /* Extended frame i.e. FPU in use. */
567 ulStackFrameSize = 26;
569 " vpush {s0} \n" /* Trigger lazy stacking. */
570 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
576 /* Standard frame i.e. FPU not in use. */
577 ulStackFrameSize = 8;
580 /* Make space on the system call stack for the stack frame and
581 * the parameter passed on the stack. We only need to copy one
582 * parameter but we still reserve 2 spaces to keep the stack
583 * double word aligned. */
584 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize - 2UL;
586 /* Copy the stack frame. */
587 for( i = 0; i < ulStackFrameSize; i++ )
589 pulSystemCallStack[ i ] = pulTaskStack[ i ];
592 /* Copy the parameter which is passed the stack. */
593 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
595 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize + 1 ];
597 /* Record if the hardware used padding to force the stack pointer
598 * to be double word aligned. */
599 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
603 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize ];
605 /* Record if the hardware used padding to force the stack pointer
606 * to be double word aligned. */
607 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
610 /* Use the pulSystemCallStack in thread mode. */
611 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
613 /* Raise the privilege for the duration of the system call. */
615 " mrs r1, control \n" /* Obtain current control value. */
616 " bic r1, #1 \n" /* Clear nPRIV bit. */
617 " msr control, r1 \n" /* Write back new control value. */
621 /* Remember the location where we should copy the stack frame when we exit from
622 * the system call. */
623 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
625 /* Store the value of the Link Register before the SVC was raised. We need to
626 * restore it when we exit from the system call. */
627 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
629 /* We ensure in pxPortInitialiseStack that the system call stack is
630 * double word aligned and therefore, there is no need of padding.
631 * Clear the bit[9] of stacked xPSR. */
632 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
636 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
637 /*-----------------------------------------------------------*/
639 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
641 void vSystemCallExit( uint32_t * pulSystemCallStack,
642 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
644 extern TaskHandle_t pxCurrentTCB;
645 xMPU_SETTINGS * pxMpuSettings;
646 uint32_t * pulTaskStack;
647 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
649 #if defined( __ARMCC_VERSION )
651 /* Declaration when these variable are defined in code instead of being
652 * exported from linker scripts. */
653 extern uint32_t * __syscalls_flash_start__;
654 extern uint32_t * __syscalls_flash_end__;
656 /* Declaration when these variable are exported from linker scripts. */
657 extern uint32_t __syscalls_flash_start__[];
658 extern uint32_t __syscalls_flash_end__[];
659 #endif /* #if defined( __ARMCC_VERSION ) */
661 ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
663 /* If the request did not come from the system call section, do nothing. */
664 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
665 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
667 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
668 pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
670 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
672 /* Extended frame i.e. FPU in use. */
673 ulStackFrameSize = 26;
675 " vpush {s0} \n" /* Trigger lazy stacking. */
676 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
682 /* Standard frame i.e. FPU not in use. */
683 ulStackFrameSize = 8;
686 /* Make space on the task stack for the stack frame. */
687 pulTaskStack = pulTaskStack - ulStackFrameSize;
689 /* Copy the stack frame. */
690 for( i = 0; i < ulStackFrameSize; i++ )
692 pulTaskStack[ i ] = pulSystemCallStack[ i ];
695 /* Use the pulTaskStack in thread mode. */
696 __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
698 /* Drop the privilege before returning to the thread mode. */
700 " mrs r1, control \n" /* Obtain current control value. */
701 " orr r1, #1 \n" /* Set nPRIV bit. */
702 " msr control, r1 \n" /* Write back new control value. */
706 /* Restore the stacked link register to what it was at the time of
707 * system call entry. */
708 pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
710 /* If the hardware used padding to force the stack pointer
711 * to be double word aligned, set the stacked xPSR bit[9],
712 * otherwise clear it. */
713 if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
715 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
719 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
722 /* This is not NULL only for the duration of the system call. */
723 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
727 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
728 /*-----------------------------------------------------------*/
730 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
732 BaseType_t xTaskIsPrivileged = pdFALSE;
733 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
735 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
737 xTaskIsPrivileged = pdTRUE;
740 return xTaskIsPrivileged;
742 /*-----------------------------------------------------------*/
745 * See header file for description.
747 BaseType_t xPortStartScheduler( void )
749 /* Errata 837070 workaround must only be enabled on Cortex-M7 r0p0
751 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
752 configASSERT( ( portCPUID == portCORTEX_M7_r0p1_ID ) || ( portCPUID == portCORTEX_M7_r0p0_ID ) );
755 /* When using this port on a Cortex-M7 r0p0 or r0p1 core, define
756 * configENABLE_ERRATA_837070_WORKAROUND to 1 in your
757 * FreeRTOSConfig.h. */
758 configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );
759 configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );
762 #if ( configASSERT_DEFINED == 1 )
764 volatile uint8_t ucOriginalPriority;
765 volatile uint32_t ulImplementedPrioBits = 0;
766 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
767 volatile uint8_t ucMaxPriorityValue;
769 /* Determine the maximum priority from which ISR safe FreeRTOS API
770 * functions can be called. ISR safe functions are those that end in
771 * "FromISR". FreeRTOS maintains separate thread and ISR API functions to
772 * ensure interrupt entry is as fast and simple as possible.
774 * Save the interrupt priority value that is about to be clobbered. */
775 ucOriginalPriority = *pucFirstUserPriorityRegister;
777 /* Determine the number of priority bits available. First write to all
779 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
781 /* Read the value back to see how many bits stuck. */
782 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
784 /* Use the same mask on the maximum system call priority. */
785 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
787 /* Check that the maximum system call priority is nonzero after
788 * accounting for the number of priority bits supported by the
789 * hardware. A priority of 0 is invalid because setting the BASEPRI
790 * register to 0 unmasks all interrupts, and interrupts with priority 0
791 * cannot be masked using BASEPRI.
792 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
793 configASSERT( ucMaxSysCallPriority );
795 /* Check that the bits not implemented in hardware are zero in
796 * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
797 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( uint8_t ) ( ~( uint32_t ) ucMaxPriorityValue ) ) == 0U );
799 /* Calculate the maximum acceptable priority group value for the number
800 * of bits read back. */
802 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
804 ulImplementedPrioBits++;
805 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
808 if( ulImplementedPrioBits == 8 )
810 /* When the hardware implements 8 priority bits, there is no way for
811 * the software to configure PRIGROUP to not have sub-priorities. As
812 * a result, the least significant bit is always used for sub-priority
813 * and there are 128 preemption priorities and 2 sub-priorities.
815 * This may cause some confusion in some cases - for example, if
816 * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
817 * priority interrupts will be masked in Critical Sections as those
818 * are at the same preemption priority. This may appear confusing as
819 * 4 is higher (numerically lower) priority than
820 * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
821 * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
822 * to 4, this confusion does not happen and the behaviour remains the same.
824 * The following assert ensures that the sub-priority bit in the
825 * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
827 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
828 ulMaxPRIGROUPValue = 0;
832 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
835 /* Shift the priority group value back to its position within the AIRCR
837 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
838 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
840 /* Restore the clobbered interrupt priority register to its original
842 *pucFirstUserPriorityRegister = ucOriginalPriority;
844 #endif /* configASSERT_DEFINED */
846 /* Make PendSV and SysTick the lowest priority interrupts. */
847 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
848 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
850 /* Configure the regions in the MPU that are common to all tasks. */
853 /* Start the timer that generates the tick ISR. Interrupts are disabled
855 vPortSetupTimerInterrupt();
857 /* Initialise the critical nesting count ready for the first task. */
858 uxCriticalNesting = 0;
860 /* Ensure the VFP is enabled - it should be anyway. */
863 /* Lazy save always. */
864 *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
866 /* Start the first task. */
867 vPortStartFirstTask();
869 /* Should not get here! */
872 /*-----------------------------------------------------------*/
874 void vPortEndScheduler( void )
876 /* Not implemented in ports where there is nothing to return to.
877 * Artificially force an assert. */
878 configASSERT( uxCriticalNesting == 1000UL );
880 /*-----------------------------------------------------------*/
882 void vPortEnterCritical( void )
884 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
885 if( portIS_PRIVILEGED() == pdFALSE )
887 portRAISE_PRIVILEGE();
888 portMEMORY_BARRIER();
890 portDISABLE_INTERRUPTS();
893 /* This is not the interrupt safe version of the enter critical function so
894 * assert() if it is being called from an interrupt context. Only API
895 * functions that end in "FromISR" can be used in an interrupt. Only assert if
896 * the critical nesting count is 1 to protect against recursive calls if the
897 * assert function also uses a critical section. */
898 if( uxCriticalNesting == 1 )
900 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
903 portMEMORY_BARRIER();
905 portRESET_PRIVILEGE();
906 portMEMORY_BARRIER();
910 portDISABLE_INTERRUPTS();
913 /* This is not the interrupt safe version of the enter critical function so
914 * assert() if it is being called from an interrupt context. Only API
915 * functions that end in "FromISR" can be used in an interrupt. Only assert if
916 * the critical nesting count is 1 to protect against recursive calls if the
917 * assert function also uses a critical section. */
918 if( uxCriticalNesting == 1 )
920 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
923 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
924 portDISABLE_INTERRUPTS();
927 /* This is not the interrupt safe version of the enter critical function so
928 * assert() if it is being called from an interrupt context. Only API
929 * functions that end in "FromISR" can be used in an interrupt. Only assert if
930 * the critical nesting count is 1 to protect against recursive calls if the
931 * assert function also uses a critical section. */
932 if( uxCriticalNesting == 1 )
934 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
936 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
938 /*-----------------------------------------------------------*/
940 void vPortExitCritical( void )
942 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
943 if( portIS_PRIVILEGED() == pdFALSE )
945 portRAISE_PRIVILEGE();
946 portMEMORY_BARRIER();
948 configASSERT( uxCriticalNesting );
951 if( uxCriticalNesting == 0 )
953 portENABLE_INTERRUPTS();
956 portMEMORY_BARRIER();
958 portRESET_PRIVILEGE();
959 portMEMORY_BARRIER();
963 configASSERT( uxCriticalNesting );
966 if( uxCriticalNesting == 0 )
968 portENABLE_INTERRUPTS();
971 #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
972 configASSERT( uxCriticalNesting );
975 if( uxCriticalNesting == 0 )
977 portENABLE_INTERRUPTS();
979 #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
981 /*-----------------------------------------------------------*/
983 void xPortSysTickHandler( void )
985 /* The SysTick runs at the lowest interrupt priority, so when this interrupt
986 * executes all interrupts must be unmasked. There is therefore no need to
987 * save and then restore the interrupt mask value as its value is already
989 portDISABLE_INTERRUPTS();
991 /* Increment the RTOS tick. */
992 if( xTaskIncrementTick() != pdFALSE )
994 /* A context switch is required. Context switching is performed in
995 * the PendSV interrupt. Pend the PendSV interrupt. */
996 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
999 portENABLE_INTERRUPTS();
1001 /*-----------------------------------------------------------*/
1004 * Setup the systick timer to generate the tick interrupts at the required
1007 __weak void vPortSetupTimerInterrupt( void )
1009 /* Stop and clear the SysTick. */
1010 portNVIC_SYSTICK_CTRL_REG = 0UL;
1011 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
1013 /* Configure SysTick to interrupt at the requested rate. */
1014 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
1015 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
1017 /*-----------------------------------------------------------*/
1019 static void prvSetupMPU( void )
1021 extern uint32_t __privileged_functions_start__[];
1022 extern uint32_t __privileged_functions_end__[];
1023 extern uint32_t __FLASH_segment_start__[];
1024 extern uint32_t __FLASH_segment_end__[];
1025 extern uint32_t __privileged_data_start__[];
1026 extern uint32_t __privileged_data_end__[];
1028 /* The only permitted number of regions are 8 or 16. */
1029 configASSERT( ( configTOTAL_MPU_REGIONS == 8 ) || ( configTOTAL_MPU_REGIONS == 16 ) );
1031 /* Ensure that the configTOTAL_MPU_REGIONS is configured correctly. */
1032 configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );
1034 /* Check the expected MPU is present. */
1035 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
1037 /* First setup the unprivileged flash for unprivileged read only access. */
1038 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
1039 ( portMPU_REGION_VALID ) |
1040 ( portUNPRIVILEGED_FLASH_REGION );
1042 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
1043 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1044 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
1045 ( portMPU_REGION_ENABLE );
1047 /* Setup the privileged flash for privileged only access. This is where
1048 * the kernel code is placed. */
1049 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
1050 ( portMPU_REGION_VALID ) |
1051 ( portPRIVILEGED_FLASH_REGION );
1053 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
1054 ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1055 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
1056 ( portMPU_REGION_ENABLE );
1058 /* Setup the privileged data RAM region. This is where the kernel data
1060 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
1061 ( portMPU_REGION_VALID ) |
1062 ( portPRIVILEGED_RAM_REGION );
1064 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
1065 ( portMPU_REGION_EXECUTE_NEVER ) |
1066 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1067 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
1068 ( portMPU_REGION_ENABLE );
1070 /* By default allow everything to access the general peripherals. The
1071 * system peripherals and registers are protected. */
1072 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
1073 ( portMPU_REGION_VALID ) |
1074 ( portGENERAL_PERIPHERALS_REGION );
1076 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
1077 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
1078 ( portMPU_REGION_ENABLE );
1080 /* Enable the memory fault exception. */
1081 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
1083 /* Enable the MPU with the background region configured. */
1084 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
1087 /*-----------------------------------------------------------*/
1089 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
1091 uint32_t ulRegionSize, ulReturnValue = 4;
1093 /* 32 is the smallest region size, 31 is the largest valid value for
1095 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
1097 if( ulActualSizeInBytes <= ulRegionSize )
1107 /* Shift the code by one before returning so it can be written directly
1108 * into the the correct bit position of the attribute register. */
1109 return( ulReturnValue << 1UL );
1111 /*-----------------------------------------------------------*/
1113 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1114 const struct xMEMORY_REGION * const xRegions,
1115 StackType_t * pxBottomOfStack,
1116 uint32_t ulStackDepth )
1118 extern uint32_t __SRAM_segment_start__[];
1119 extern uint32_t __SRAM_segment_end__[];
1120 extern uint32_t __privileged_data_start__[];
1121 extern uint32_t __privileged_data_end__[];
1125 if( xRegions == NULL )
1127 /* No MPU regions are specified so allow access to all RAM. */
1128 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1129 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
1130 ( portMPU_REGION_VALID ) |
1131 ( portSTACK_REGION ); /* Region number. */
1133 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1134 ( portMPU_REGION_READ_WRITE ) |
1135 ( portMPU_REGION_EXECUTE_NEVER ) |
1136 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1137 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
1138 ( portMPU_REGION_ENABLE );
1140 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) __SRAM_segment_start__;
1141 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) __SRAM_segment_end__;
1142 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1143 tskMPU_WRITE_PERMISSION );
1145 /* Invalidate user configurable regions. */
1146 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1148 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1149 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1150 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1151 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1152 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1157 /* This function is called automatically when the task is created - in
1158 * which case the stack region parameters will be valid. At all other
1159 * times the stack parameters will not be valid and it is assumed that the
1160 * stack region has already been configured. */
1161 if( ulStackDepth > 0 )
1163 /* Define the region that allows access to the stack. */
1164 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1165 ( ( uint32_t ) pxBottomOfStack ) |
1166 ( portMPU_REGION_VALID ) |
1167 ( portSTACK_REGION ); /* Region number. */
1169 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1170 ( portMPU_REGION_READ_WRITE ) |
1171 ( portMPU_REGION_EXECUTE_NEVER ) |
1172 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
1173 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |
1174 ( portMPU_REGION_ENABLE );
1176 xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;
1177 xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) ( pxBottomOfStack ) +
1178 ( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) - 1UL );
1179 xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1180 tskMPU_WRITE_PERMISSION );
1185 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1187 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
1189 /* Translate the generic region definition contained in
1190 * xRegions into the CM4 specific MPU settings that are then
1191 * stored in xMPUSettings. */
1192 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
1193 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
1194 ( portMPU_REGION_VALID ) |
1195 ( ul - 1UL ); /* Region number. */
1197 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
1198 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
1199 ( xRegions[ lIndex ].ulParameters ) |
1200 ( portMPU_REGION_ENABLE );
1202 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress;
1203 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1UL );
1204 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1206 if( ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_ONLY ) == portMPU_REGION_READ_ONLY ) ||
1207 ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) == portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) )
1209 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = tskMPU_READ_PERMISSION;
1212 if( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_WRITE ) == portMPU_REGION_READ_WRITE )
1214 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = ( tskMPU_READ_PERMISSION | tskMPU_WRITE_PERMISSION );
1219 /* Invalidate the region. */
1220 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1221 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1222 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1223 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1224 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1231 /*-----------------------------------------------------------*/
1233 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1234 uint32_t ulBufferLength,
1235 uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1238 uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1239 BaseType_t xAccessGranted = pdFALSE;
1240 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1242 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1244 xAccessGranted = pdTRUE;
1248 if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1250 ulBufferStartAddress = ( uint32_t ) pvBuffer;
1251 ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1253 for( i = 0; i < portTOTAL_NUM_REGIONS_IN_TCB; i++ )
1255 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1256 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1257 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1258 portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1259 xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1260 xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1261 portIS_AUTHORIZED( ulAccessRequested, xTaskMpuSettings->xRegionSettings[ i ].ulRegionPermissions ) )
1263 xAccessGranted = pdTRUE;
1270 return xAccessGranted;
1272 /*-----------------------------------------------------------*/
1275 #if ( configASSERT_DEFINED == 1 )
1277 void vPortValidateInterruptPriority( void )
1279 uint32_t ulCurrentInterrupt;
1280 uint8_t ucCurrentPriority;
1282 /* Obtain the number of the currently executing interrupt. */
1283 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1285 /* Is the interrupt number a user defined interrupt? */
1286 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
1288 /* Look up the interrupt's priority. */
1289 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
1291 /* The following assertion will fail if a service routine (ISR) for
1292 * an interrupt that has been assigned a priority above
1293 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
1294 * function. ISR safe FreeRTOS API functions must *only* be called
1295 * from interrupts that have been assigned a priority at or below
1296 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1298 * Numerically low interrupt priority numbers represent logically high
1299 * interrupt priorities, therefore the priority of the interrupt must
1300 * be set to a value equal to or numerically *higher* than
1301 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1303 * Interrupts that use the FreeRTOS API must not be left at their
1304 * default priority of zero as that is the highest possible priority,
1305 * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
1306 * and therefore also guaranteed to be invalid.
1308 * FreeRTOS maintains separate thread and ISR API functions to ensure
1309 * interrupt entry is as fast and simple as possible.
1311 * The following links provide detailed information:
1312 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
1313 * https://www.FreeRTOS.org/FAQHelp.html */
1314 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
1317 /* Priority grouping: The interrupt controller (NVIC) allows the bits
1318 * that define each interrupt's priority to be split between bits that
1319 * define the interrupt's pre-emption priority bits and bits that define
1320 * the interrupt's sub-priority. For simplicity all bits must be defined
1321 * to be pre-emption priority bits. The following assertion will fail if
1322 * this is not the case (if some bits represent a sub-priority).
1324 * If the application only uses CMSIS libraries for interrupt
1325 * configuration then the correct setting can be achieved on all Cortex-M
1326 * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
1327 * scheduler. Note however that some vendor specific peripheral libraries
1328 * assume a non-zero priority group setting, in which cases using a value
1329 * of zero will result in unpredictable behaviour. */
1330 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
1333 #endif /* configASSERT_DEFINED */
1334 /*-----------------------------------------------------------*/