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 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
30 * all the API functions to use the MPU wrappers. That should only be done when
31 * task.h is included from an application file. */
32 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
34 /* Scheduler includes. */
38 /* MPU wrappers includes. */
39 #include "mpu_wrappers.h"
41 /* Portasm includes. */
44 #if ( configENABLE_TRUSTZONE == 1 )
45 /* Secure components includes. */
46 #include "secure_context.h"
47 #include "secure_init.h"
48 #endif /* configENABLE_TRUSTZONE */
50 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
53 * The FreeRTOS Cortex M33 port can be configured to run on the Secure Side only
54 * i.e. the processor boots as secure and never jumps to the non-secure side.
55 * The Trust Zone support in the port must be disabled in order to run FreeRTOS
56 * on the secure side. The following are the valid configuration seetings:
58 * 1. Run FreeRTOS on the Secure Side:
59 * configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0
61 * 2. Run FreeRTOS on the Non-Secure Side with Secure Side function call support:
62 * configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1
64 * 3. Run FreeRTOS on the Non-Secure Side only i.e. no Secure Side function call support:
65 * configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0
67 #if ( ( configRUN_FREERTOS_SECURE_ONLY == 1 ) && ( configENABLE_TRUSTZONE == 1 ) )
68 #error TrustZone needs to be disabled in order to run FreeRTOS on the Secure Side.
70 /*-----------------------------------------------------------*/
73 * @brief Constants required to manipulate the NVIC.
75 #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
76 #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
77 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
78 #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
79 #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
80 #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
81 #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
82 #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
83 #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
84 #define portNVIC_PEND_SYSTICK_SET_BIT ( 1UL << 26UL )
85 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
86 #define portNVIC_PENDSV_PRI ( portMIN_INTERRUPT_PRIORITY << 16UL )
87 #define portNVIC_SYSTICK_PRI ( portMIN_INTERRUPT_PRIORITY << 24UL )
88 /*-----------------------------------------------------------*/
91 * @brief Constants required to manipulate the SCB.
93 #define portSCB_SYS_HANDLER_CTRL_STATE_REG ( *( volatile uint32_t * ) 0xe000ed24 )
94 #define portSCB_MEM_FAULT_ENABLE_BIT ( 1UL << 16UL )
95 /*-----------------------------------------------------------*/
98 * @brief Constants required to check the validity of an interrupt priority.
100 #define portNVIC_SHPR2_REG ( *( ( volatile uint32_t * ) 0xE000ED1C ) )
101 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
102 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
103 #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
104 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
105 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
106 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
107 #define portPRIGROUP_SHIFT ( 8UL )
108 /*-----------------------------------------------------------*/
111 * @brief Constants used during system call enter and exit.
113 #define portPSR_STACK_PADDING_MASK ( 1UL << 9UL )
114 #define portEXC_RETURN_STACK_FRAME_TYPE_MASK ( 1UL << 4UL )
115 /*-----------------------------------------------------------*/
118 * @brief Constants required to manipulate the FPU.
120 #define portCPACR ( ( volatile uint32_t * ) 0xe000ed88 ) /* Coprocessor Access Control Register. */
121 #define portCPACR_CP10_VALUE ( 3UL )
122 #define portCPACR_CP11_VALUE portCPACR_CP10_VALUE
123 #define portCPACR_CP10_POS ( 20UL )
124 #define portCPACR_CP11_POS ( 22UL )
126 #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating Point Context Control Register. */
127 #define portFPCCR_ASPEN_POS ( 31UL )
128 #define portFPCCR_ASPEN_MASK ( 1UL << portFPCCR_ASPEN_POS )
129 #define portFPCCR_LSPEN_POS ( 30UL )
130 #define portFPCCR_LSPEN_MASK ( 1UL << portFPCCR_LSPEN_POS )
131 /*-----------------------------------------------------------*/
134 * @brief Offsets in the stack to the parameters when inside the SVC handler.
136 #define portOFFSET_TO_LR ( 5 )
137 #define portOFFSET_TO_PC ( 6 )
138 #define portOFFSET_TO_PSR ( 7 )
139 /*-----------------------------------------------------------*/
142 * @brief Constants required to manipulate the MPU.
144 #define portMPU_TYPE_REG ( *( ( volatile uint32_t * ) 0xe000ed90 ) )
145 #define portMPU_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed94 ) )
146 #define portMPU_RNR_REG ( *( ( volatile uint32_t * ) 0xe000ed98 ) )
148 #define portMPU_RBAR_REG ( *( ( volatile uint32_t * ) 0xe000ed9c ) )
149 #define portMPU_RLAR_REG ( *( ( volatile uint32_t * ) 0xe000eda0 ) )
151 #define portMPU_RBAR_A1_REG ( *( ( volatile uint32_t * ) 0xe000eda4 ) )
152 #define portMPU_RLAR_A1_REG ( *( ( volatile uint32_t * ) 0xe000eda8 ) )
154 #define portMPU_RBAR_A2_REG ( *( ( volatile uint32_t * ) 0xe000edac ) )
155 #define portMPU_RLAR_A2_REG ( *( ( volatile uint32_t * ) 0xe000edb0 ) )
157 #define portMPU_RBAR_A3_REG ( *( ( volatile uint32_t * ) 0xe000edb4 ) )
158 #define portMPU_RLAR_A3_REG ( *( ( volatile uint32_t * ) 0xe000edb8 ) )
160 #define portMPU_MAIR0_REG ( *( ( volatile uint32_t * ) 0xe000edc0 ) )
161 #define portMPU_MAIR1_REG ( *( ( volatile uint32_t * ) 0xe000edc4 ) )
163 #define portMPU_RBAR_ADDRESS_MASK ( 0xffffffe0 ) /* Must be 32-byte aligned. */
164 #define portMPU_RLAR_ADDRESS_MASK ( 0xffffffe0 ) /* Must be 32-byte aligned. */
166 #define portMPU_RBAR_ACCESS_PERMISSIONS_MASK ( 3UL << 1UL )
168 #define portMPU_MAIR_ATTR0_POS ( 0UL )
169 #define portMPU_MAIR_ATTR0_MASK ( 0x000000ff )
171 #define portMPU_MAIR_ATTR1_POS ( 8UL )
172 #define portMPU_MAIR_ATTR1_MASK ( 0x0000ff00 )
174 #define portMPU_MAIR_ATTR2_POS ( 16UL )
175 #define portMPU_MAIR_ATTR2_MASK ( 0x00ff0000 )
177 #define portMPU_MAIR_ATTR3_POS ( 24UL )
178 #define portMPU_MAIR_ATTR3_MASK ( 0xff000000 )
180 #define portMPU_MAIR_ATTR4_POS ( 0UL )
181 #define portMPU_MAIR_ATTR4_MASK ( 0x000000ff )
183 #define portMPU_MAIR_ATTR5_POS ( 8UL )
184 #define portMPU_MAIR_ATTR5_MASK ( 0x0000ff00 )
186 #define portMPU_MAIR_ATTR6_POS ( 16UL )
187 #define portMPU_MAIR_ATTR6_MASK ( 0x00ff0000 )
189 #define portMPU_MAIR_ATTR7_POS ( 24UL )
190 #define portMPU_MAIR_ATTR7_MASK ( 0xff000000 )
192 #define portMPU_RLAR_ATTR_INDEX0 ( 0UL << 1UL )
193 #define portMPU_RLAR_ATTR_INDEX1 ( 1UL << 1UL )
194 #define portMPU_RLAR_ATTR_INDEX2 ( 2UL << 1UL )
195 #define portMPU_RLAR_ATTR_INDEX3 ( 3UL << 1UL )
196 #define portMPU_RLAR_ATTR_INDEX4 ( 4UL << 1UL )
197 #define portMPU_RLAR_ATTR_INDEX5 ( 5UL << 1UL )
198 #define portMPU_RLAR_ATTR_INDEX6 ( 6UL << 1UL )
199 #define portMPU_RLAR_ATTR_INDEX7 ( 7UL << 1UL )
201 #define portMPU_RLAR_REGION_ENABLE ( 1UL )
203 /* Enable privileged access to unmapped region. */
204 #define portMPU_PRIV_BACKGROUND_ENABLE_BIT ( 1UL << 2UL )
207 #define portMPU_ENABLE_BIT ( 1UL << 0UL )
209 /* Expected value of the portMPU_TYPE register. */
210 #define portEXPECTED_MPU_TYPE_VALUE ( configTOTAL_MPU_REGIONS << 8UL )
212 /* Extract first address of the MPU region as encoded in the
213 * RBAR (Region Base Address Register) value. */
214 #define portEXTRACT_FIRST_ADDRESS_FROM_RBAR( rbar ) \
215 ( ( rbar ) & portMPU_RBAR_ADDRESS_MASK )
217 /* Extract last address of the MPU region as encoded in the
218 * RLAR (Region Limit Address Register) value. */
219 #define portEXTRACT_LAST_ADDRESS_FROM_RLAR( rlar ) \
220 ( ( ( rlar ) & portMPU_RLAR_ADDRESS_MASK ) | ~portMPU_RLAR_ADDRESS_MASK )
222 /* Does addr lies within [start, end] address range? */
223 #define portIS_ADDRESS_WITHIN_RANGE( addr, start, end ) \
224 ( ( ( addr ) >= ( start ) ) && ( ( addr ) <= ( end ) ) )
226 /* Is the access request satisfied by the available permissions? */
227 #define portIS_AUTHORIZED( accessRequest, permissions ) \
228 ( ( ( permissions ) & ( accessRequest ) ) == accessRequest )
230 /* Max value that fits in a uint32_t type. */
231 #define portUINT32_MAX ( ~( ( uint32_t ) 0 ) )
233 /* Check if adding a and b will result in overflow. */
234 #define portADD_UINT32_WILL_OVERFLOW( a, b ) ( ( a ) > ( portUINT32_MAX - ( b ) ) )
235 /*-----------------------------------------------------------*/
238 * @brief The maximum 24-bit number.
240 * It is needed because the systick is a 24-bit counter.
242 #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
245 * @brief A fiddle factor to estimate the number of SysTick counts that would
246 * have occurred while the SysTick counter is stopped during tickless idle
249 #define portMISSED_COUNTS_FACTOR ( 94UL )
250 /*-----------------------------------------------------------*/
253 * @brief Constants required to set up the initial stack.
255 #define portINITIAL_XPSR ( 0x01000000 )
257 #if ( configRUN_FREERTOS_SECURE_ONLY == 1 )
260 * @brief Initial EXC_RETURN value.
263 * 1111 1111 1111 1111 1111 1111 1111 1101
265 * Bit[6] - 1 --> The exception was taken from the Secure state.
266 * Bit[5] - 1 --> Do not skip stacking of additional state context.
267 * Bit[4] - 1 --> The PE did not allocate space on the stack for FP context.
268 * Bit[3] - 1 --> Return to the Thread mode.
269 * Bit[2] - 1 --> Restore registers from the process stack.
270 * Bit[1] - 0 --> Reserved, 0.
271 * Bit[0] - 1 --> The exception was taken to the Secure state.
273 #define portINITIAL_EXC_RETURN ( 0xfffffffd )
277 * @brief Initial EXC_RETURN value.
280 * 1111 1111 1111 1111 1111 1111 1011 1100
282 * Bit[6] - 0 --> The exception was taken from the Non-Secure state.
283 * Bit[5] - 1 --> Do not skip stacking of additional state context.
284 * Bit[4] - 1 --> The PE did not allocate space on the stack for FP context.
285 * Bit[3] - 1 --> Return to the Thread mode.
286 * Bit[2] - 1 --> Restore registers from the process stack.
287 * Bit[1] - 0 --> Reserved, 0.
288 * Bit[0] - 0 --> The exception was taken to the Non-Secure state.
290 #define portINITIAL_EXC_RETURN ( 0xffffffbc )
291 #endif /* configRUN_FREERTOS_SECURE_ONLY */
294 * @brief CONTROL register privileged bit mask.
296 * Bit[0] in CONTROL register tells the privilege:
297 * Bit[0] = 0 ==> The task is privileged.
298 * Bit[0] = 1 ==> The task is not privileged.
300 #define portCONTROL_PRIVILEGED_MASK ( 1UL << 0UL )
303 * @brief Initial CONTROL register values.
305 #define portINITIAL_CONTROL_UNPRIVILEGED ( 0x3 )
306 #define portINITIAL_CONTROL_PRIVILEGED ( 0x2 )
309 * @brief Let the user override the default SysTick clock rate. If defined by the
310 * user, this symbol must equal the SysTick clock rate when the CLK bit is 0 in the
311 * configuration register.
313 #ifndef configSYSTICK_CLOCK_HZ
314 #define configSYSTICK_CLOCK_HZ ( configCPU_CLOCK_HZ )
315 /* Ensure the SysTick is clocked at the same frequency as the core. */
316 #define portNVIC_SYSTICK_CLK_BIT_CONFIG ( portNVIC_SYSTICK_CLK_BIT )
318 /* Select the option to clock SysTick not at the same frequency as the core. */
319 #define portNVIC_SYSTICK_CLK_BIT_CONFIG ( 0 )
323 * @brief Let the user override the pre-loading of the initial LR with the
324 * address of prvTaskExitError() in case it messes up unwinding of the stack
327 #ifdef configTASK_RETURN_ADDRESS
328 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
330 #define portTASK_RETURN_ADDRESS prvTaskExitError
334 * @brief If portPRELOAD_REGISTERS then registers will be given an initial value
335 * when a task is created. This helps in debugging at the cost of code size.
337 #define portPRELOAD_REGISTERS 1
340 * @brief A task is created without a secure context, and must call
341 * portALLOCATE_SECURE_CONTEXT() to give itself a secure context before it makes
344 #define portNO_SECURE_CONTEXT 0
345 /*-----------------------------------------------------------*/
348 * @brief Used to catch tasks that attempt to return from their implementing
351 static void prvTaskExitError( void );
353 #if ( configENABLE_MPU == 1 )
356 * @brief Extract MPU region's access permissions from the Region Base Address
357 * Register (RBAR) value.
359 * @param ulRBARValue RBAR value for the MPU region.
361 * @return uint32_t Access permissions.
363 static uint32_t prvGetRegionAccessPermissions( uint32_t ulRBARValue ) PRIVILEGED_FUNCTION;
364 #endif /* configENABLE_MPU */
366 #if ( configENABLE_MPU == 1 )
369 * @brief Setup the Memory Protection Unit (MPU).
371 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
372 #endif /* configENABLE_MPU */
374 #if ( configENABLE_FPU == 1 )
377 * @brief Setup the Floating Point Unit (FPU).
379 static void prvSetupFPU( void ) PRIVILEGED_FUNCTION;
380 #endif /* configENABLE_FPU */
383 * @brief Setup the timer to generate the tick interrupts.
385 * The implementation in this file is weak to allow application writers to
386 * change the timer used to generate the tick interrupt.
388 void vPortSetupTimerInterrupt( void ) PRIVILEGED_FUNCTION;
391 * @brief Checks whether the current execution context is interrupt.
393 * @return pdTRUE if the current execution context is interrupt, pdFALSE
396 BaseType_t xPortIsInsideInterrupt( void );
399 * @brief Yield the processor.
401 void vPortYield( void ) PRIVILEGED_FUNCTION;
404 * @brief Enter critical section.
406 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
409 * @brief Exit from critical section.
411 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
414 * @brief SysTick handler.
416 void SysTick_Handler( void ) PRIVILEGED_FUNCTION;
419 * @brief C part of SVC handler.
421 portDONT_DISCARD void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) PRIVILEGED_FUNCTION;
423 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
426 * @brief Sets up the system call stack so that upon returning from
427 * SVC, the system call stack is used.
429 * It is used for the system calls with up to 4 parameters.
431 * @param pulTaskStack The current SP when the SVC was raised.
432 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
434 void vSystemCallEnter( uint32_t * pulTaskStack, uint32_t ulLR ) PRIVILEGED_FUNCTION;
436 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
438 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
441 * @brief Sets up the system call stack so that upon returning from
442 * SVC, the system call stack is used.
444 * It is used for the system calls with 5 parameters.
446 * @param pulTaskStack The current SP when the SVC was raised.
447 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
449 void vSystemCallEnter_1( uint32_t * pulTaskStack, uint32_t ulLR ) PRIVILEGED_FUNCTION;
451 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
453 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
456 * @brief Sets up the task stack so that upon returning from
457 * SVC, the task stack is used again.
459 * @param pulSystemCallStack The current SP when the SVC was raised.
460 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
462 void vSystemCallExit( uint32_t * pulSystemCallStack, uint32_t ulLR ) PRIVILEGED_FUNCTION;
464 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
466 #if ( configENABLE_MPU == 1 )
469 * @brief Checks whether or not the calling task is privileged.
471 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
473 BaseType_t xPortIsTaskPrivileged( void ) PRIVILEGED_FUNCTION;
475 #endif /* configENABLE_MPU == 1 */
476 /*-----------------------------------------------------------*/
479 * @brief Each task maintains its own interrupt status in the critical nesting
482 PRIVILEGED_DATA static volatile uint32_t ulCriticalNesting = 0xaaaaaaaaUL;
484 #if ( configENABLE_TRUSTZONE == 1 )
487 * @brief Saved as part of the task context to indicate which context the
488 * task is using on the secure side.
490 PRIVILEGED_DATA portDONT_DISCARD volatile SecureContextHandle_t xSecureContext = portNO_SECURE_CONTEXT;
491 #endif /* configENABLE_TRUSTZONE */
494 * @brief Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
495 * FreeRTOS API functions are not called from interrupts that have been assigned
496 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
498 #if ( ( configASSERT_DEFINED == 1 ) && ( portHAS_ARMV8M_MAIN_EXTENSION == 1 ) )
500 static uint8_t ucMaxSysCallPriority = 0;
501 static uint32_t ulMaxPRIGROUPValue = 0;
502 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * ) portNVIC_IP_REGISTERS_OFFSET_16;
504 #endif /* #if ( ( configASSERT_DEFINED == 1 ) && ( portHAS_ARMV8M_MAIN_EXTENSION == 1 ) ) */
506 #if ( configUSE_TICKLESS_IDLE == 1 )
509 * @brief The number of SysTick increments that make up one tick period.
511 PRIVILEGED_DATA static uint32_t ulTimerCountsForOneTick = 0;
514 * @brief The maximum number of tick periods that can be suppressed is
515 * limited by the 24 bit resolution of the SysTick timer.
517 PRIVILEGED_DATA static uint32_t xMaximumPossibleSuppressedTicks = 0;
520 * @brief Compensate for the CPU cycles that pass while the SysTick is
521 * stopped (low power functionality only).
523 PRIVILEGED_DATA static uint32_t ulStoppedTimerCompensation = 0;
524 #endif /* configUSE_TICKLESS_IDLE */
525 /*-----------------------------------------------------------*/
527 #if ( configUSE_TICKLESS_IDLE == 1 )
528 __attribute__( ( weak ) ) void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
530 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements, ulSysTickDecrementsLeft;
531 TickType_t xModifiableIdleTime;
533 /* Make sure the SysTick reload value does not overflow the counter. */
534 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
536 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
539 /* Enter a critical section but don't use the taskENTER_CRITICAL()
540 * method as that will mask interrupts that should exit sleep mode. */
541 __asm volatile ( "cpsid i" ::: "memory" );
542 __asm volatile ( "dsb" );
543 __asm volatile ( "isb" );
545 /* If a context switch is pending or a task is waiting for the scheduler
546 * to be unsuspended then abandon the low power entry. */
547 if( eTaskConfirmSleepModeStatus() == eAbortSleep )
549 /* Re-enable interrupts - see comments above the cpsid instruction
551 __asm volatile ( "cpsie i" ::: "memory" );
555 /* Stop the SysTick momentarily. The time the SysTick is stopped for
556 * is accounted for as best it can be, but using the tickless mode will
557 * inevitably result in some tiny drift of the time maintained by the
558 * kernel with respect to calendar time. */
559 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT );
561 /* Use the SysTick current-value register to determine the number of
562 * SysTick decrements remaining until the next tick interrupt. If the
563 * current-value register is zero, then there are actually
564 * ulTimerCountsForOneTick decrements remaining, not zero, because the
565 * SysTick requests the interrupt when decrementing from 1 to 0. */
566 ulSysTickDecrementsLeft = portNVIC_SYSTICK_CURRENT_VALUE_REG;
568 if( ulSysTickDecrementsLeft == 0 )
570 ulSysTickDecrementsLeft = ulTimerCountsForOneTick;
573 /* Calculate the reload value required to wait xExpectedIdleTime
574 * tick periods. -1 is used because this code normally executes part
575 * way through the first tick period. But if the SysTick IRQ is now
576 * pending, then clear the IRQ, suppressing the first tick, and correct
577 * the reload value to reflect that the second tick period is already
578 * underway. The expected idle time is always at least two ticks. */
579 ulReloadValue = ulSysTickDecrementsLeft + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
581 if( ( portNVIC_INT_CTRL_REG & portNVIC_PEND_SYSTICK_SET_BIT ) != 0 )
583 portNVIC_INT_CTRL_REG = portNVIC_PEND_SYSTICK_CLEAR_BIT;
584 ulReloadValue -= ulTimerCountsForOneTick;
587 if( ulReloadValue > ulStoppedTimerCompensation )
589 ulReloadValue -= ulStoppedTimerCompensation;
592 /* Set the new reload value. */
593 portNVIC_SYSTICK_LOAD_REG = ulReloadValue;
595 /* Clear the SysTick count flag and set the count value back to
597 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
599 /* Restart SysTick. */
600 portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
602 /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can
603 * set its parameter to 0 to indicate that its implementation contains
604 * its own wait for interrupt or wait for event instruction, and so wfi
605 * should not be executed again. However, the original expected idle
606 * time variable must remain unmodified, so a copy is taken. */
607 xModifiableIdleTime = xExpectedIdleTime;
608 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
610 if( xModifiableIdleTime > 0 )
612 __asm volatile ( "dsb" ::: "memory" );
613 __asm volatile ( "wfi" );
614 __asm volatile ( "isb" );
617 configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
619 /* Re-enable interrupts to allow the interrupt that brought the MCU
620 * out of sleep mode to execute immediately. See comments above
621 * the cpsid instruction above. */
622 __asm volatile ( "cpsie i" ::: "memory" );
623 __asm volatile ( "dsb" );
624 __asm volatile ( "isb" );
626 /* Disable interrupts again because the clock is about to be stopped
627 * and interrupts that execute while the clock is stopped will increase
628 * any slippage between the time maintained by the RTOS and calendar
630 __asm volatile ( "cpsid i" ::: "memory" );
631 __asm volatile ( "dsb" );
632 __asm volatile ( "isb" );
634 /* Disable the SysTick clock without reading the
635 * portNVIC_SYSTICK_CTRL_REG register to ensure the
636 * portNVIC_SYSTICK_COUNT_FLAG_BIT is not cleared if it is set. Again,
637 * the time the SysTick is stopped for is accounted for as best it can
638 * be, but using the tickless mode will inevitably result in some tiny
639 * drift of the time maintained by the kernel with respect to calendar
641 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT );
643 /* Determine whether the SysTick has already counted to zero. */
644 if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
646 uint32_t ulCalculatedLoadValue;
648 /* The tick interrupt ended the sleep (or is now pending), and
649 * a new tick period has started. Reset portNVIC_SYSTICK_LOAD_REG
650 * with whatever remains of the new tick period. */
651 ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );
653 /* Don't allow a tiny value, or values that have somehow
654 * underflowed because the post sleep hook did something
655 * that took too long or because the SysTick current-value register
657 if( ( ulCalculatedLoadValue <= ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )
659 ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );
662 portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;
664 /* As the pending tick will be processed as soon as this
665 * function exits, the tick value maintained by the tick is stepped
666 * forward by one less than the time spent waiting. */
667 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
671 /* Something other than the tick interrupt ended the sleep. */
673 /* Use the SysTick current-value register to determine the
674 * number of SysTick decrements remaining until the expected idle
675 * time would have ended. */
676 ulSysTickDecrementsLeft = portNVIC_SYSTICK_CURRENT_VALUE_REG;
677 #if ( portNVIC_SYSTICK_CLK_BIT_CONFIG != portNVIC_SYSTICK_CLK_BIT )
679 /* If the SysTick is not using the core clock, the current-
680 * value register might still be zero here. In that case, the
681 * SysTick didn't load from the reload register, and there are
682 * ulReloadValue decrements remaining in the expected idle
684 if( ulSysTickDecrementsLeft == 0 )
686 ulSysTickDecrementsLeft = ulReloadValue;
689 #endif /* portNVIC_SYSTICK_CLK_BIT_CONFIG */
691 /* Work out how long the sleep lasted rounded to complete tick
692 * periods (not the ulReload value which accounted for part
694 ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - ulSysTickDecrementsLeft;
696 /* How many complete tick periods passed while the processor
698 ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
700 /* The reload value is set to whatever fraction of a single tick
702 portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;
705 /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG again,
706 * then set portNVIC_SYSTICK_LOAD_REG back to its standard value. If
707 * the SysTick is not using the core clock, temporarily configure it to
708 * use the core clock. This configuration forces the SysTick to load
709 * from portNVIC_SYSTICK_LOAD_REG immediately instead of at the next
710 * cycle of the other clock. Then portNVIC_SYSTICK_LOAD_REG is ready
711 * to receive the standard value immediately. */
712 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
713 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
714 #if ( portNVIC_SYSTICK_CLK_BIT_CONFIG == portNVIC_SYSTICK_CLK_BIT )
716 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
720 /* The temporary usage of the core clock has served its purpose,
721 * as described above. Resume usage of the other clock. */
722 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT;
724 if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
726 /* The partial tick period already ended. Be sure the SysTick
727 * counts it only once. */
728 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0;
731 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
732 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
734 #endif /* portNVIC_SYSTICK_CLK_BIT_CONFIG */
736 /* Step the tick to account for any tick periods that elapsed. */
737 vTaskStepTick( ulCompleteTickPeriods );
739 /* Exit with interrupts enabled. */
740 __asm volatile ( "cpsie i" ::: "memory" );
743 #endif /* configUSE_TICKLESS_IDLE */
744 /*-----------------------------------------------------------*/
746 __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void ) /* PRIVILEGED_FUNCTION */
748 /* Calculate the constants required to configure the tick interrupt. */
749 #if ( configUSE_TICKLESS_IDLE == 1 )
751 ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
752 xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
753 ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
755 #endif /* configUSE_TICKLESS_IDLE */
757 /* Stop and reset SysTick.
759 * QEMU versions older than 7.0.0 contain a bug which causes an error if we
760 * enable SysTick without first selecting a valid clock source. We trigger
761 * the bug if we change clock sources from a clock with a zero clock period
762 * to one with a nonzero clock period and enable Systick at the same time.
763 * So we configure the CLKSOURCE bit here, prior to setting the ENABLE bit.
764 * This workaround avoids the bug in QEMU versions older than 7.0.0. */
765 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT_CONFIG;
766 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
768 /* Configure SysTick to interrupt at the requested rate. */
769 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
770 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
772 /*-----------------------------------------------------------*/
774 static void prvTaskExitError( void )
776 volatile uint32_t ulDummy = 0UL;
778 /* A function that implements a task must not exit or attempt to return to
779 * its caller as there is nothing to return to. If a task wants to exit it
780 * should instead call vTaskDelete( NULL ). Artificially force an assert()
781 * to be triggered if configASSERT() is defined, then stop here so
782 * application writers can catch the error. */
783 configASSERT( ulCriticalNesting == ~0UL );
784 portDISABLE_INTERRUPTS();
786 while( ulDummy == 0 )
788 /* This file calls prvTaskExitError() after the scheduler has been
789 * started to remove a compiler warning about the function being
790 * defined but never called. ulDummy is used purely to quieten other
791 * warnings about code appearing after this function is called - making
792 * ulDummy volatile makes the compiler think the function could return
793 * and therefore not output an 'unreachable code' warning for code that
794 * appears after it. */
797 /*-----------------------------------------------------------*/
799 #if ( configENABLE_MPU == 1 )
800 static uint32_t prvGetRegionAccessPermissions( uint32_t ulRBARValue ) /* PRIVILEGED_FUNCTION */
802 uint32_t ulAccessPermissions = 0;
804 if( ( ulRBARValue & portMPU_RBAR_ACCESS_PERMISSIONS_MASK ) == portMPU_REGION_READ_ONLY )
806 ulAccessPermissions = tskMPU_READ_PERMISSION;
809 if( ( ulRBARValue & portMPU_RBAR_ACCESS_PERMISSIONS_MASK ) == portMPU_REGION_READ_WRITE )
811 ulAccessPermissions = ( tskMPU_READ_PERMISSION | tskMPU_WRITE_PERMISSION );
814 return ulAccessPermissions;
816 #endif /* configENABLE_MPU */
817 /*-----------------------------------------------------------*/
819 #if ( configENABLE_MPU == 1 )
820 static void prvSetupMPU( void ) /* PRIVILEGED_FUNCTION */
822 #if defined( __ARMCC_VERSION )
824 /* Declaration when these variable are defined in code instead of being
825 * exported from linker scripts. */
826 extern uint32_t * __privileged_functions_start__;
827 extern uint32_t * __privileged_functions_end__;
828 extern uint32_t * __syscalls_flash_start__;
829 extern uint32_t * __syscalls_flash_end__;
830 extern uint32_t * __unprivileged_flash_start__;
831 extern uint32_t * __unprivileged_flash_end__;
832 extern uint32_t * __privileged_sram_start__;
833 extern uint32_t * __privileged_sram_end__;
834 #else /* if defined( __ARMCC_VERSION ) */
835 /* Declaration when these variable are exported from linker scripts. */
836 extern uint32_t __privileged_functions_start__[];
837 extern uint32_t __privileged_functions_end__[];
838 extern uint32_t __syscalls_flash_start__[];
839 extern uint32_t __syscalls_flash_end__[];
840 extern uint32_t __unprivileged_flash_start__[];
841 extern uint32_t __unprivileged_flash_end__[];
842 extern uint32_t __privileged_sram_start__[];
843 extern uint32_t __privileged_sram_end__[];
844 #endif /* defined( __ARMCC_VERSION ) */
846 /* The only permitted number of regions are 8 or 16. */
847 configASSERT( ( configTOTAL_MPU_REGIONS == 8 ) || ( configTOTAL_MPU_REGIONS == 16 ) );
849 /* Ensure that the configTOTAL_MPU_REGIONS is configured correctly. */
850 configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );
852 /* Check that the MPU is present. */
853 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
855 /* MAIR0 - Index 0. */
856 portMPU_MAIR0_REG |= ( ( portMPU_NORMAL_MEMORY_BUFFERABLE_CACHEABLE << portMPU_MAIR_ATTR0_POS ) & portMPU_MAIR_ATTR0_MASK );
857 /* MAIR0 - Index 1. */
858 portMPU_MAIR0_REG |= ( ( portMPU_DEVICE_MEMORY_nGnRE << portMPU_MAIR_ATTR1_POS ) & portMPU_MAIR_ATTR1_MASK );
860 /* Setup privileged flash as Read Only so that privileged tasks can
861 * read it but not modify. */
862 portMPU_RNR_REG = portPRIVILEGED_FLASH_REGION;
863 portMPU_RBAR_REG = ( ( ( uint32_t ) __privileged_functions_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |
864 ( portMPU_REGION_NON_SHAREABLE ) |
865 ( portMPU_REGION_PRIVILEGED_READ_ONLY );
866 portMPU_RLAR_REG = ( ( ( uint32_t ) __privileged_functions_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |
867 ( portMPU_RLAR_ATTR_INDEX0 ) |
868 ( portMPU_RLAR_REGION_ENABLE );
870 /* Setup unprivileged flash as Read Only by both privileged and
871 * unprivileged tasks. All tasks can read it but no-one can modify. */
872 portMPU_RNR_REG = portUNPRIVILEGED_FLASH_REGION;
873 portMPU_RBAR_REG = ( ( ( uint32_t ) __unprivileged_flash_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |
874 ( portMPU_REGION_NON_SHAREABLE ) |
875 ( portMPU_REGION_READ_ONLY );
876 portMPU_RLAR_REG = ( ( ( uint32_t ) __unprivileged_flash_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |
877 ( portMPU_RLAR_ATTR_INDEX0 ) |
878 ( portMPU_RLAR_REGION_ENABLE );
880 /* Setup unprivileged syscalls flash as Read Only by both privileged
881 * and unprivileged tasks. All tasks can read it but no-one can modify. */
882 portMPU_RNR_REG = portUNPRIVILEGED_SYSCALLS_REGION;
883 portMPU_RBAR_REG = ( ( ( uint32_t ) __syscalls_flash_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |
884 ( portMPU_REGION_NON_SHAREABLE ) |
885 ( portMPU_REGION_READ_ONLY );
886 portMPU_RLAR_REG = ( ( ( uint32_t ) __syscalls_flash_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |
887 ( portMPU_RLAR_ATTR_INDEX0 ) |
888 ( portMPU_RLAR_REGION_ENABLE );
890 /* Setup RAM containing kernel data for privileged access only. */
891 portMPU_RNR_REG = portPRIVILEGED_RAM_REGION;
892 portMPU_RBAR_REG = ( ( ( uint32_t ) __privileged_sram_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |
893 ( portMPU_REGION_NON_SHAREABLE ) |
894 ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
895 ( portMPU_REGION_EXECUTE_NEVER );
896 portMPU_RLAR_REG = ( ( ( uint32_t ) __privileged_sram_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |
897 ( portMPU_RLAR_ATTR_INDEX0 ) |
898 ( portMPU_RLAR_REGION_ENABLE );
900 /* Enable mem fault. */
901 portSCB_SYS_HANDLER_CTRL_STATE_REG |= portSCB_MEM_FAULT_ENABLE_BIT;
903 /* Enable MPU with privileged background access i.e. unmapped
904 * regions have privileged access. */
905 portMPU_CTRL_REG |= ( portMPU_PRIV_BACKGROUND_ENABLE_BIT | portMPU_ENABLE_BIT );
908 #endif /* configENABLE_MPU */
909 /*-----------------------------------------------------------*/
911 #if ( configENABLE_FPU == 1 )
912 static void prvSetupFPU( void ) /* PRIVILEGED_FUNCTION */
914 #if ( configENABLE_TRUSTZONE == 1 )
916 /* Enable non-secure access to the FPU. */
917 SecureInit_EnableNSFPUAccess();
919 #endif /* configENABLE_TRUSTZONE */
921 /* CP10 = 11 ==> Full access to FPU i.e. both privileged and
922 * unprivileged code should be able to access FPU. CP11 should be
923 * programmed to the same value as CP10. */
924 *( portCPACR ) |= ( ( portCPACR_CP10_VALUE << portCPACR_CP10_POS ) |
925 ( portCPACR_CP11_VALUE << portCPACR_CP11_POS )
928 /* ASPEN = 1 ==> Hardware should automatically preserve floating point
929 * context on exception entry and restore on exception return.
930 * LSPEN = 1 ==> Enable lazy context save of FP state. */
931 *( portFPCCR ) |= ( portFPCCR_ASPEN_MASK | portFPCCR_LSPEN_MASK );
933 #endif /* configENABLE_FPU */
934 /*-----------------------------------------------------------*/
936 void vPortYield( void ) /* PRIVILEGED_FUNCTION */
938 /* Set a PendSV to request a context switch. */
939 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
941 /* Barriers are normally not required but do ensure the code is
942 * completely within the specified behaviour for the architecture. */
943 __asm volatile ( "dsb" ::: "memory" );
944 __asm volatile ( "isb" );
946 /*-----------------------------------------------------------*/
948 void vPortEnterCritical( void ) /* PRIVILEGED_FUNCTION */
950 portDISABLE_INTERRUPTS();
953 /* Barriers are normally not required but do ensure the code is
954 * completely within the specified behaviour for the architecture. */
955 __asm volatile ( "dsb" ::: "memory" );
956 __asm volatile ( "isb" );
958 /*-----------------------------------------------------------*/
960 void vPortExitCritical( void ) /* PRIVILEGED_FUNCTION */
962 configASSERT( ulCriticalNesting );
965 if( ulCriticalNesting == 0 )
967 portENABLE_INTERRUPTS();
970 /*-----------------------------------------------------------*/
972 void SysTick_Handler( void ) /* PRIVILEGED_FUNCTION */
974 uint32_t ulPreviousMask;
976 ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();
978 /* Increment the RTOS tick. */
979 if( xTaskIncrementTick() != pdFALSE )
981 /* Pend a context switch. */
982 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
985 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );
987 /*-----------------------------------------------------------*/
989 void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTION portDONT_DISCARD */
991 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 1 ) )
992 #if defined( __ARMCC_VERSION )
994 /* Declaration when these variable are defined in code instead of being
995 * exported from linker scripts. */
996 extern uint32_t * __syscalls_flash_start__;
997 extern uint32_t * __syscalls_flash_end__;
999 /* Declaration when these variable are exported from linker scripts. */
1000 extern uint32_t __syscalls_flash_start__[];
1001 extern uint32_t __syscalls_flash_end__[];
1002 #endif /* defined( __ARMCC_VERSION ) */
1003 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 1 ) */
1007 #if ( configENABLE_TRUSTZONE == 1 )
1008 uint32_t ulR0, ulR1;
1009 extern TaskHandle_t pxCurrentTCB;
1010 #if ( configENABLE_MPU == 1 )
1011 uint32_t ulControl, ulIsTaskPrivileged;
1012 #endif /* configENABLE_MPU */
1013 #endif /* configENABLE_TRUSTZONE */
1014 uint8_t ucSVCNumber;
1016 /* Register are stored on the stack in the following order - R0, R1, R2, R3,
1017 * R12, LR, PC, xPSR. */
1018 ulPC = pulCallerStackAddress[ portOFFSET_TO_PC ];
1019 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
1021 switch( ucSVCNumber )
1023 #if ( configENABLE_TRUSTZONE == 1 )
1024 case portSVC_ALLOCATE_SECURE_CONTEXT:
1026 /* R0 contains the stack size passed as parameter to the
1027 * vPortAllocateSecureContext function. */
1028 ulR0 = pulCallerStackAddress[ 0 ];
1030 #if ( configENABLE_MPU == 1 )
1032 /* Read the CONTROL register value. */
1033 __asm volatile ( "mrs %0, control" : "=r" ( ulControl ) );
1035 /* The task that raised the SVC is privileged if Bit[0]
1036 * in the CONTROL register is 0. */
1037 ulIsTaskPrivileged = ( ( ulControl & portCONTROL_PRIVILEGED_MASK ) == 0 );
1039 /* Allocate and load a context for the secure task. */
1040 xSecureContext = SecureContext_AllocateContext( ulR0, ulIsTaskPrivileged, pxCurrentTCB );
1042 #else /* if ( configENABLE_MPU == 1 ) */
1044 /* Allocate and load a context for the secure task. */
1045 xSecureContext = SecureContext_AllocateContext( ulR0, pxCurrentTCB );
1047 #endif /* configENABLE_MPU */
1049 configASSERT( xSecureContext != securecontextINVALID_CONTEXT_ID );
1050 SecureContext_LoadContext( xSecureContext, pxCurrentTCB );
1053 case portSVC_FREE_SECURE_CONTEXT:
1055 /* R0 contains TCB being freed and R1 contains the secure
1056 * context handle to be freed. */
1057 ulR0 = pulCallerStackAddress[ 0 ];
1058 ulR1 = pulCallerStackAddress[ 1 ];
1060 /* Free the secure context. */
1061 SecureContext_FreeContext( ( SecureContextHandle_t ) ulR1, ( void * ) ulR0 );
1063 #endif /* configENABLE_TRUSTZONE */
1065 case portSVC_START_SCHEDULER:
1066 #if ( configENABLE_TRUSTZONE == 1 )
1068 /* De-prioritize the non-secure exceptions so that the
1069 * non-secure pendSV runs at the lowest priority. */
1070 SecureInit_DePrioritizeNSExceptions();
1072 /* Initialize the secure context management system. */
1073 SecureContext_Init();
1075 #endif /* configENABLE_TRUSTZONE */
1077 #if ( configENABLE_FPU == 1 )
1079 /* Setup the Floating Point Unit (FPU). */
1082 #endif /* configENABLE_FPU */
1084 /* Setup the context of the first task so that the first task starts
1086 vRestoreContextOfFirstTask();
1089 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 1 ) )
1090 case portSVC_RAISE_PRIVILEGE:
1092 /* Only raise the privilege, if the svc was raised from any of
1093 * the system calls. */
1094 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
1095 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
1100 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 1 ) */
1103 /* Incorrect SVC call. */
1104 configASSERT( pdFALSE );
1107 /*-----------------------------------------------------------*/
1109 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
1111 void vSystemCallEnter( uint32_t * pulTaskStack, uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
1113 extern TaskHandle_t pxCurrentTCB;
1114 xMPU_SETTINGS * pxMpuSettings;
1115 uint32_t * pulSystemCallStack;
1116 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
1117 #if defined( __ARMCC_VERSION )
1118 /* Declaration when these variable are defined in code instead of being
1119 * exported from linker scripts. */
1120 extern uint32_t * __syscalls_flash_start__;
1121 extern uint32_t * __syscalls_flash_end__;
1123 /* Declaration when these variable are exported from linker scripts. */
1124 extern uint32_t __syscalls_flash_start__[];
1125 extern uint32_t __syscalls_flash_end__[];
1126 #endif /* #if defined( __ARMCC_VERSION ) */
1128 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
1130 /* If the request did not come from the system call section, do nothing. */
1131 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
1132 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
1134 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
1135 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
1137 /* This is not NULL only for the duration of the system call. */
1138 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
1140 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
1142 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
1144 /* Extended frame i.e. FPU in use. */
1145 ulStackFrameSize = 26;
1147 " vpush {s0} \n" /* Trigger lazy stacking. */
1148 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
1154 /* Standard frame i.e. FPU not in use. */
1155 ulStackFrameSize = 8;
1160 ulStackFrameSize = 8;
1162 #endif /* configENABLE_FPU || configENABLE_MVE */
1164 /* Make space on the system call stack for the stack frame. */
1165 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
1167 /* Copy the stack frame. */
1168 for( i = 0; i < ulStackFrameSize; i++ )
1170 pulSystemCallStack[ i ] = pulTaskStack[ i ];
1173 /* Store the value of the LR and PSPLIM registers before the SVC was raised. We need to
1174 * restore it when we exit from the system call. */
1175 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
1176 __asm volatile ( "mrs %0, psplim" : "=r" ( pxMpuSettings->xSystemCallStackInfo.ulStackLimitRegisterAtSystemCallEntry ) );
1178 /* Use the pulSystemCallStack in thread mode. */
1179 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
1180 __asm volatile ( "msr psplim, %0" : : "r" ( pxMpuSettings->xSystemCallStackInfo.pulSystemCallStackLimit ) );
1182 /* Remember the location where we should copy the stack frame when we exit from
1183 * the system call. */
1184 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
1186 /* Record if the hardware used padding to force the stack pointer
1187 * to be double word aligned. */
1188 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
1190 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
1194 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
1197 /* We ensure in pxPortInitialiseStack that the system call stack is
1198 * double word aligned and therefore, there is no need of padding.
1199 * Clear the bit[9] of stacked xPSR. */
1200 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
1202 /* Raise the privilege for the duration of the system call. */
1204 " mrs r0, control \n" /* Obtain current control value. */
1205 " movs r1, #1 \n" /* r1 = 1. */
1206 " bics r0, r1 \n" /* Clear nPRIV bit. */
1207 " msr control, r0 \n" /* Write back new control value. */
1208 ::: "r0", "r1", "memory"
1213 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1214 /*-----------------------------------------------------------*/
1216 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
1218 void vSystemCallEnter_1( uint32_t * pulTaskStack, uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
1220 extern TaskHandle_t pxCurrentTCB;
1221 xMPU_SETTINGS * pxMpuSettings;
1222 uint32_t * pulSystemCallStack;
1223 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
1224 #if defined( __ARMCC_VERSION )
1225 /* Declaration when these variable are defined in code instead of being
1226 * exported from linker scripts. */
1227 extern uint32_t * __syscalls_flash_start__;
1228 extern uint32_t * __syscalls_flash_end__;
1230 /* Declaration when these variable are exported from linker scripts. */
1231 extern uint32_t __syscalls_flash_start__[];
1232 extern uint32_t __syscalls_flash_end__[];
1233 #endif /* #if defined( __ARMCC_VERSION ) */
1235 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
1237 /* If the request did not come from the system call section, do nothing. */
1238 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
1239 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
1241 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
1242 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
1244 /* This is not NULL only for the duration of the system call. */
1245 configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
1247 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
1249 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
1251 /* Extended frame i.e. FPU in use. */
1252 ulStackFrameSize = 26;
1254 " vpush {s0} \n" /* Trigger lazy stacking. */
1255 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
1261 /* Standard frame i.e. FPU not in use. */
1262 ulStackFrameSize = 8;
1267 ulStackFrameSize = 8;
1269 #endif /* configENABLE_FPU || configENABLE_MVE */
1271 /* Make space on the system call stack for the stack frame and
1272 * the parameter passed on the stack. We only need to copy one
1273 * parameter but we still reserve 2 spaces to keep the stack
1274 * double word aligned. */
1275 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize - 2UL;
1277 /* Copy the stack frame. */
1278 for( i = 0; i < ulStackFrameSize; i++ )
1280 pulSystemCallStack[ i ] = pulTaskStack[ i ];
1283 /* Copy the parameter which is passed the stack. */
1284 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
1286 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize + 1 ];
1287 /* Record if the hardware used padding to force the stack pointer
1288 * to be double word aligned. */
1289 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
1293 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize ];
1294 /* Record if the hardware used padding to force the stack pointer
1295 * to be double word aligned. */
1296 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
1299 /* Store the value of the LR and PSPLIM registers before the SVC was raised.
1300 * We need to restore it when we exit from the system call. */
1301 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
1302 __asm volatile ( "mrs %0, psplim" : "=r" ( pxMpuSettings->xSystemCallStackInfo.ulStackLimitRegisterAtSystemCallEntry ) );
1304 /* Use the pulSystemCallStack in thread mode. */
1305 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
1306 __asm volatile ( "msr psplim, %0" : : "r" ( pxMpuSettings->xSystemCallStackInfo.pulSystemCallStackLimit ) );
1308 /* Remember the location where we should copy the stack frame when we exit from
1309 * the system call. */
1310 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
1312 /* We ensure in pxPortInitialiseStack that the system call stack is
1313 * double word aligned and therefore, there is no need of padding.
1314 * Clear the bit[9] of stacked xPSR. */
1315 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
1317 /* Raise the privilege for the duration of the system call. */
1319 " mrs r0, control \n" /* Obtain current control value. */
1320 " movs r1, #1 \n" /* r1 = 1. */
1321 " bics r0, r1 \n" /* Clear nPRIV bit. */
1322 " msr control, r0 \n" /* Write back new control value. */
1323 ::: "r0", "r1", "memory"
1328 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1329 /*-----------------------------------------------------------*/
1331 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
1333 void vSystemCallExit( uint32_t * pulSystemCallStack, uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
1335 extern TaskHandle_t pxCurrentTCB;
1336 xMPU_SETTINGS * pxMpuSettings;
1337 uint32_t * pulTaskStack;
1338 uint32_t ulStackFrameSize, ulSystemCallLocation, i;
1339 #if defined( __ARMCC_VERSION )
1340 /* Declaration when these variable are defined in code instead of being
1341 * exported from linker scripts. */
1342 extern uint32_t * __syscalls_flash_start__;
1343 extern uint32_t * __syscalls_flash_end__;
1345 /* Declaration when these variable are exported from linker scripts. */
1346 extern uint32_t __syscalls_flash_start__[];
1347 extern uint32_t __syscalls_flash_end__[];
1348 #endif /* #if defined( __ARMCC_VERSION ) */
1350 ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
1352 /* If the request did not come from the system call section, do nothing. */
1353 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
1354 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
1356 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
1357 pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
1359 #if ( ( configENABLE_FPU == 1 ) || ( configENABLE_MVE == 1 ) )
1361 if( ( ulLR & portEXC_RETURN_STACK_FRAME_TYPE_MASK ) == 0UL )
1363 /* Extended frame i.e. FPU in use. */
1364 ulStackFrameSize = 26;
1366 " vpush {s0} \n" /* Trigger lazy stacking. */
1367 " vpop {s0} \n" /* Nullify the affect of the above instruction. */
1373 /* Standard frame i.e. FPU not in use. */
1374 ulStackFrameSize = 8;
1379 ulStackFrameSize = 8;
1381 #endif /* configENABLE_FPU || configENABLE_MVE */
1383 /* Make space on the task stack for the stack frame. */
1384 pulTaskStack = pulTaskStack - ulStackFrameSize;
1386 /* Copy the stack frame. */
1387 for( i = 0; i < ulStackFrameSize; i++ )
1389 pulTaskStack[ i ] = pulSystemCallStack[ i ];
1392 /* Use the pulTaskStack in thread mode. */
1393 __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
1395 /* Restore the LR and PSPLIM to what they were at the time of
1396 * system call entry. */
1397 pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
1398 __asm volatile ( "msr psplim, %0" : : "r" ( pxMpuSettings->xSystemCallStackInfo.ulStackLimitRegisterAtSystemCallEntry ) );
1400 /* If the hardware used padding to force the stack pointer
1401 * to be double word aligned, set the stacked xPSR bit[9],
1402 * otherwise clear it. */
1403 if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
1405 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
1409 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
1412 /* This is not NULL only for the duration of the system call. */
1413 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
1415 /* Drop the privilege before returning to the thread mode. */
1417 " mrs r0, control \n" /* Obtain current control value. */
1418 " movs r1, #1 \n" /* r1 = 1. */
1419 " orrs r0, r1 \n" /* Set nPRIV bit. */
1420 " msr control, r0 \n" /* Write back new control value. */
1421 ::: "r0", "r1", "memory"
1426 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1427 /*-----------------------------------------------------------*/
1429 #if ( configENABLE_MPU == 1 )
1431 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
1433 BaseType_t xTaskIsPrivileged = pdFALSE;
1434 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1436 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1438 xTaskIsPrivileged = pdTRUE;
1441 return xTaskIsPrivileged;
1444 #endif /* configENABLE_MPU == 1 */
1445 /*-----------------------------------------------------------*/
1447 #if( configENABLE_MPU == 1 )
1449 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
1450 StackType_t * pxEndOfStack,
1451 TaskFunction_t pxCode,
1452 void * pvParameters,
1453 BaseType_t xRunPrivileged,
1454 xMPU_SETTINGS * xMPUSettings ) /* PRIVILEGED_FUNCTION */
1456 uint32_t ulIndex = 0;
1458 xMPUSettings->ulContext[ ulIndex ] = 0x04040404; /* r4. */
1460 xMPUSettings->ulContext[ ulIndex ] = 0x05050505; /* r5. */
1462 xMPUSettings->ulContext[ ulIndex ] = 0x06060606; /* r6. */
1464 xMPUSettings->ulContext[ ulIndex ] = 0x07070707; /* r7. */
1466 xMPUSettings->ulContext[ ulIndex ] = 0x08080808; /* r8. */
1468 xMPUSettings->ulContext[ ulIndex ] = 0x09090909; /* r9. */
1470 xMPUSettings->ulContext[ ulIndex ] = 0x10101010; /* r10. */
1472 xMPUSettings->ulContext[ ulIndex ] = 0x11111111; /* r11. */
1475 xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) pvParameters; /* r0. */
1477 xMPUSettings->ulContext[ ulIndex ] = 0x01010101; /* r1. */
1479 xMPUSettings->ulContext[ ulIndex ] = 0x02020202; /* r2. */
1481 xMPUSettings->ulContext[ ulIndex ] = 0x03030303; /* r3. */
1483 xMPUSettings->ulContext[ ulIndex ] = 0x12121212; /* r12. */
1485 xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) portTASK_RETURN_ADDRESS; /* LR. */
1487 xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) pxCode; /* PC. */
1489 xMPUSettings->ulContext[ ulIndex ] = portINITIAL_XPSR; /* xPSR. */
1492 #if ( configENABLE_TRUSTZONE == 1 )
1494 xMPUSettings->ulContext[ ulIndex ] = portNO_SECURE_CONTEXT; /* xSecureContext. */
1497 #endif /* configENABLE_TRUSTZONE */
1498 xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) ( pxTopOfStack - 8 ); /* PSP with the hardware saved stack. */
1500 xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) pxEndOfStack; /* PSPLIM. */
1502 if( xRunPrivileged == pdTRUE )
1504 xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
1505 xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) portINITIAL_CONTROL_PRIVILEGED; /* CONTROL. */
1510 xMPUSettings->ulTaskFlags &= ( ~portTASK_IS_PRIVILEGED_FLAG );
1511 xMPUSettings->ulContext[ ulIndex ] = ( uint32_t ) portINITIAL_CONTROL_UNPRIVILEGED; /* CONTROL. */
1514 xMPUSettings->ulContext[ ulIndex ] = portINITIAL_EXC_RETURN; /* LR (EXC_RETURN). */
1517 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
1519 /* Ensure that the system call stack is double word aligned. */
1520 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
1521 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
1522 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
1524 xMPUSettings->xSystemCallStackInfo.pulSystemCallStackLimit = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ 0 ] );
1525 xMPUSettings->xSystemCallStackInfo.pulSystemCallStackLimit = ( uint32_t * ) ( ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStackLimit ) +
1526 ( uint32_t ) ( portBYTE_ALIGNMENT - 1 ) ) &
1527 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
1529 /* This is not NULL only for the duration of a system call. */
1530 xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
1532 #endif /* configUSE_MPU_WRAPPERS_V1 == 0 */
1534 return &( xMPUSettings->ulContext[ ulIndex ] );
1537 #else /* configENABLE_MPU */
1539 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
1540 StackType_t * pxEndOfStack,
1541 TaskFunction_t pxCode,
1542 void * pvParameters ) /* PRIVILEGED_FUNCTION */
1544 /* Simulate the stack frame as it would be created by a context switch
1546 #if ( portPRELOAD_REGISTERS == 0 )
1548 pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
1549 *pxTopOfStack = portINITIAL_XPSR; /* xPSR. */
1551 *pxTopOfStack = ( StackType_t ) pxCode; /* PC. */
1553 *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR. */
1554 pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
1555 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0. */
1556 pxTopOfStack -= 9; /* R11..R4, EXC_RETURN. */
1557 *pxTopOfStack = portINITIAL_EXC_RETURN;
1559 *pxTopOfStack = ( StackType_t ) pxEndOfStack; /* Slot used to hold this task's PSPLIM value. */
1561 #if ( configENABLE_TRUSTZONE == 1 )
1564 *pxTopOfStack = portNO_SECURE_CONTEXT; /* Slot used to hold this task's xSecureContext value. */
1566 #endif /* configENABLE_TRUSTZONE */
1568 #else /* portPRELOAD_REGISTERS */
1570 pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
1571 *pxTopOfStack = portINITIAL_XPSR; /* xPSR. */
1573 *pxTopOfStack = ( StackType_t ) pxCode; /* PC. */
1575 *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR. */
1577 *pxTopOfStack = ( StackType_t ) 0x12121212UL; /* R12. */
1579 *pxTopOfStack = ( StackType_t ) 0x03030303UL; /* R3. */
1581 *pxTopOfStack = ( StackType_t ) 0x02020202UL; /* R2. */
1583 *pxTopOfStack = ( StackType_t ) 0x01010101UL; /* R1. */
1585 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0. */
1587 *pxTopOfStack = ( StackType_t ) 0x11111111UL; /* R11. */
1589 *pxTopOfStack = ( StackType_t ) 0x10101010UL; /* R10. */
1591 *pxTopOfStack = ( StackType_t ) 0x09090909UL; /* R09. */
1593 *pxTopOfStack = ( StackType_t ) 0x08080808UL; /* R08. */
1595 *pxTopOfStack = ( StackType_t ) 0x07070707UL; /* R07. */
1597 *pxTopOfStack = ( StackType_t ) 0x06060606UL; /* R06. */
1599 *pxTopOfStack = ( StackType_t ) 0x05050505UL; /* R05. */
1601 *pxTopOfStack = ( StackType_t ) 0x04040404UL; /* R04. */
1603 *pxTopOfStack = portINITIAL_EXC_RETURN; /* EXC_RETURN. */
1605 *pxTopOfStack = ( StackType_t ) pxEndOfStack; /* Slot used to hold this task's PSPLIM value. */
1607 #if ( configENABLE_TRUSTZONE == 1 )
1610 *pxTopOfStack = portNO_SECURE_CONTEXT; /* Slot used to hold this task's xSecureContext value. */
1612 #endif /* configENABLE_TRUSTZONE */
1614 #endif /* portPRELOAD_REGISTERS */
1616 return pxTopOfStack;
1619 #endif /* configENABLE_MPU */
1620 /*-----------------------------------------------------------*/
1622 BaseType_t xPortStartScheduler( void ) /* PRIVILEGED_FUNCTION */
1624 #if ( ( configASSERT_DEFINED == 1 ) && ( portHAS_ARMV8M_MAIN_EXTENSION == 1 ) )
1626 volatile uint32_t ulOriginalPriority;
1627 volatile uint32_t ulImplementedPrioBits = 0;
1628 volatile uint8_t ucMaxPriorityValue;
1630 /* Determine the maximum priority from which ISR safe FreeRTOS API
1631 * functions can be called. ISR safe functions are those that end in
1632 * "FromISR". FreeRTOS maintains separate thread and ISR API functions to
1633 * ensure interrupt entry is as fast and simple as possible.
1635 * Save the interrupt priority value that is about to be clobbered. */
1636 ulOriginalPriority = portNVIC_SHPR2_REG;
1638 /* Determine the number of priority bits available. First write to all
1640 portNVIC_SHPR2_REG = 0xFF000000;
1642 /* Read the value back to see how many bits stuck. */
1643 ucMaxPriorityValue = ( uint8_t ) ( ( portNVIC_SHPR2_REG & 0xFF000000 ) >> 24 );
1645 /* Use the same mask on the maximum system call priority. */
1646 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
1648 /* Check that the maximum system call priority is nonzero after
1649 * accounting for the number of priority bits supported by the
1650 * hardware. A priority of 0 is invalid because setting the BASEPRI
1651 * register to 0 unmasks all interrupts, and interrupts with priority 0
1652 * cannot be masked using BASEPRI.
1653 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1654 configASSERT( ucMaxSysCallPriority );
1656 /* Check that the bits not implemented in hardware are zero in
1657 * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
1658 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( uint8_t ) ( ~( uint32_t ) ucMaxPriorityValue ) ) == 0U );
1660 /* Calculate the maximum acceptable priority group value for the number
1661 * of bits read back. */
1663 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
1665 ulImplementedPrioBits++;
1666 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
1669 if( ulImplementedPrioBits == 8 )
1671 /* When the hardware implements 8 priority bits, there is no way for
1672 * the software to configure PRIGROUP to not have sub-priorities. As
1673 * a result, the least significant bit is always used for sub-priority
1674 * and there are 128 preemption priorities and 2 sub-priorities.
1676 * This may cause some confusion in some cases - for example, if
1677 * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
1678 * priority interrupts will be masked in Critical Sections as those
1679 * are at the same preemption priority. This may appear confusing as
1680 * 4 is higher (numerically lower) priority than
1681 * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
1682 * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
1683 * to 4, this confusion does not happen and the behaviour remains the same.
1685 * The following assert ensures that the sub-priority bit in the
1686 * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
1688 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
1689 ulMaxPRIGROUPValue = 0;
1693 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
1696 /* Shift the priority group value back to its position within the AIRCR
1698 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
1699 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
1701 /* Restore the clobbered interrupt priority register to its original
1703 portNVIC_SHPR2_REG = ulOriginalPriority;
1705 #endif /* #if ( ( configASSERT_DEFINED == 1 ) && ( portHAS_ARMV8M_MAIN_EXTENSION == 1 ) ) */
1707 /* Make PendSV, CallSV and SysTick the same priority as the kernel. */
1708 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
1709 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
1711 #if ( configENABLE_MPU == 1 )
1713 /* Setup the Memory Protection Unit (MPU). */
1716 #endif /* configENABLE_MPU */
1718 /* Start the timer that generates the tick ISR. Interrupts are disabled
1720 vPortSetupTimerInterrupt();
1722 /* Initialize the critical nesting count ready for the first task. */
1723 ulCriticalNesting = 0;
1725 /* Start the first task. */
1728 /* Should never get here as the tasks will now be executing. Call the task
1729 * exit error function to prevent compiler warnings about a static function
1730 * not being called in the case that the application writer overrides this
1731 * functionality by defining configTASK_RETURN_ADDRESS. Call
1732 * vTaskSwitchContext() so link time optimization does not remove the
1734 vTaskSwitchContext();
1737 /* Should not get here. */
1740 /*-----------------------------------------------------------*/
1742 void vPortEndScheduler( void ) /* PRIVILEGED_FUNCTION */
1744 /* Not implemented in ports where there is nothing to return to.
1745 * Artificially force an assert. */
1746 configASSERT( ulCriticalNesting == 1000UL );
1748 /*-----------------------------------------------------------*/
1750 #if ( configENABLE_MPU == 1 )
1751 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1752 const struct xMEMORY_REGION * const xRegions,
1753 StackType_t * pxBottomOfStack,
1754 uint32_t ulStackDepth )
1756 uint32_t ulRegionStartAddress, ulRegionEndAddress, ulRegionNumber;
1759 #if defined( __ARMCC_VERSION )
1761 /* Declaration when these variable are defined in code instead of being
1762 * exported from linker scripts. */
1763 extern uint32_t * __privileged_sram_start__;
1764 extern uint32_t * __privileged_sram_end__;
1766 /* Declaration when these variable are exported from linker scripts. */
1767 extern uint32_t __privileged_sram_start__[];
1768 extern uint32_t __privileged_sram_end__[];
1769 #endif /* defined( __ARMCC_VERSION ) */
1772 xMPUSettings->ulMAIR0 = ( ( portMPU_NORMAL_MEMORY_BUFFERABLE_CACHEABLE << portMPU_MAIR_ATTR0_POS ) & portMPU_MAIR_ATTR0_MASK );
1773 xMPUSettings->ulMAIR0 |= ( ( portMPU_DEVICE_MEMORY_nGnRE << portMPU_MAIR_ATTR1_POS ) & portMPU_MAIR_ATTR1_MASK );
1775 /* This function is called automatically when the task is created - in
1776 * which case the stack region parameters will be valid. At all other
1777 * times the stack parameters will not be valid and it is assumed that
1778 * the stack region has already been configured. */
1779 if( ulStackDepth > 0 )
1781 ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;
1782 ulRegionEndAddress = ( uint32_t ) pxBottomOfStack + ( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) - 1;
1784 /* If the stack is within the privileged SRAM, do not protect it
1785 * using a separate MPU region. This is needed because privileged
1786 * SRAM is already protected using an MPU region and ARMv8-M does
1787 * not allow overlapping MPU regions. */
1788 if( ( ulRegionStartAddress >= ( uint32_t ) __privileged_sram_start__ ) &&
1789 ( ulRegionEndAddress <= ( uint32_t ) __privileged_sram_end__ ) )
1791 xMPUSettings->xRegionsSettings[ 0 ].ulRBAR = 0;
1792 xMPUSettings->xRegionsSettings[ 0 ].ulRLAR = 0;
1796 /* Define the region that allows access to the stack. */
1797 ulRegionStartAddress &= portMPU_RBAR_ADDRESS_MASK;
1798 ulRegionEndAddress &= portMPU_RLAR_ADDRESS_MASK;
1800 xMPUSettings->xRegionsSettings[ 0 ].ulRBAR = ( ulRegionStartAddress ) |
1801 ( portMPU_REGION_NON_SHAREABLE ) |
1802 ( portMPU_REGION_READ_WRITE ) |
1803 ( portMPU_REGION_EXECUTE_NEVER );
1805 xMPUSettings->xRegionsSettings[ 0 ].ulRLAR = ( ulRegionEndAddress ) |
1806 ( portMPU_RLAR_ATTR_INDEX0 ) |
1807 ( portMPU_RLAR_REGION_ENABLE );
1811 /* User supplied configurable regions. */
1812 for( ulRegionNumber = 1; ulRegionNumber <= portNUM_CONFIGURABLE_REGIONS; ulRegionNumber++ )
1814 /* If xRegions is NULL i.e. the task has not specified any MPU
1815 * region, the else part ensures that all the configurable MPU
1816 * regions are invalidated. */
1817 if( ( xRegions != NULL ) && ( xRegions[ lIndex ].ulLengthInBytes > 0UL ) )
1819 /* Translate the generic region definition contained in xRegions
1820 * into the ARMv8 specific MPU settings that are then stored in
1822 ulRegionStartAddress = ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) & portMPU_RBAR_ADDRESS_MASK;
1823 ulRegionEndAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1;
1824 ulRegionEndAddress &= portMPU_RLAR_ADDRESS_MASK;
1826 /* Start address. */
1827 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR = ( ulRegionStartAddress ) |
1828 ( portMPU_REGION_NON_SHAREABLE );
1831 if( ( xRegions[ lIndex ].ulParameters & tskMPU_REGION_READ_ONLY ) != 0 )
1833 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR |= ( portMPU_REGION_READ_ONLY );
1837 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR |= ( portMPU_REGION_READ_WRITE );
1841 if( ( xRegions[ lIndex ].ulParameters & tskMPU_REGION_EXECUTE_NEVER ) != 0 )
1843 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR |= ( portMPU_REGION_EXECUTE_NEVER );
1847 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR = ( ulRegionEndAddress ) |
1848 ( portMPU_RLAR_REGION_ENABLE );
1850 /* Normal memory/ Device memory. */
1851 if( ( xRegions[ lIndex ].ulParameters & tskMPU_REGION_DEVICE_MEMORY ) != 0 )
1853 /* Attr1 in MAIR0 is configured as device memory. */
1854 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR |= portMPU_RLAR_ATTR_INDEX1;
1858 /* Attr0 in MAIR0 is configured as normal memory. */
1859 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR |= portMPU_RLAR_ATTR_INDEX0;
1864 /* Invalidate the region. */
1865 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR = 0UL;
1866 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR = 0UL;
1872 #endif /* configENABLE_MPU */
1873 /*-----------------------------------------------------------*/
1875 #if ( configENABLE_MPU == 1 )
1876 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1877 uint32_t ulBufferLength,
1878 uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1881 uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1882 BaseType_t xAccessGranted = pdFALSE;
1883 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1885 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1887 xAccessGranted = pdTRUE;
1891 if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1893 ulBufferStartAddress = ( uint32_t ) pvBuffer;
1894 ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1896 for( i = 0; i < portTOTAL_NUM_REGIONS; i++ )
1898 /* Is the MPU region enabled? */
1899 if( ( xTaskMpuSettings->xRegionsSettings[ i ].ulRLAR & portMPU_RLAR_REGION_ENABLE ) == portMPU_RLAR_REGION_ENABLE )
1901 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1902 portEXTRACT_FIRST_ADDRESS_FROM_RBAR( xTaskMpuSettings->xRegionsSettings[ i ].ulRBAR ),
1903 portEXTRACT_LAST_ADDRESS_FROM_RLAR( xTaskMpuSettings->xRegionsSettings[ i ].ulRLAR ) ) &&
1904 portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1905 portEXTRACT_FIRST_ADDRESS_FROM_RBAR( xTaskMpuSettings->xRegionsSettings[ i ].ulRBAR ),
1906 portEXTRACT_LAST_ADDRESS_FROM_RLAR( xTaskMpuSettings->xRegionsSettings[ i ].ulRLAR ) ) &&
1907 portIS_AUTHORIZED( ulAccessRequested,
1908 prvGetRegionAccessPermissions( xTaskMpuSettings->xRegionsSettings[ i ].ulRBAR ) ) )
1910 xAccessGranted = pdTRUE;
1918 return xAccessGranted;
1920 #endif /* configENABLE_MPU */
1921 /*-----------------------------------------------------------*/
1923 BaseType_t xPortIsInsideInterrupt( void )
1925 uint32_t ulCurrentInterrupt;
1928 /* Obtain the number of the currently executing interrupt. Interrupt Program
1929 * Status Register (IPSR) holds the exception number of the currently-executing
1930 * exception or zero for Thread mode.*/
1931 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1933 if( ulCurrentInterrupt == 0 )
1944 /*-----------------------------------------------------------*/
1946 #if ( ( configASSERT_DEFINED == 1 ) && ( portHAS_ARMV8M_MAIN_EXTENSION == 1 ) )
1948 void vPortValidateInterruptPriority( void )
1950 uint32_t ulCurrentInterrupt;
1951 uint8_t ucCurrentPriority;
1953 /* Obtain the number of the currently executing interrupt. */
1954 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1956 /* Is the interrupt number a user defined interrupt? */
1957 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
1959 /* Look up the interrupt's priority. */
1960 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
1962 /* The following assertion will fail if a service routine (ISR) for
1963 * an interrupt that has been assigned a priority above
1964 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
1965 * function. ISR safe FreeRTOS API functions must *only* be called
1966 * from interrupts that have been assigned a priority at or below
1967 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1969 * Numerically low interrupt priority numbers represent logically high
1970 * interrupt priorities, therefore the priority of the interrupt must
1971 * be set to a value equal to or numerically *higher* than
1972 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1974 * Interrupts that use the FreeRTOS API must not be left at their
1975 * default priority of zero as that is the highest possible priority,
1976 * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
1977 * and therefore also guaranteed to be invalid.
1979 * FreeRTOS maintains separate thread and ISR API functions to ensure
1980 * interrupt entry is as fast and simple as possible.
1982 * The following links provide detailed information:
1983 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
1984 * https://www.FreeRTOS.org/FAQHelp.html */
1985 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
1988 /* Priority grouping: The interrupt controller (NVIC) allows the bits
1989 * that define each interrupt's priority to be split between bits that
1990 * define the interrupt's pre-emption priority bits and bits that define
1991 * the interrupt's sub-priority. For simplicity all bits must be defined
1992 * to be pre-emption priority bits. The following assertion will fail if
1993 * this is not the case (if some bits represent a sub-priority).
1995 * If the application only uses CMSIS libraries for interrupt
1996 * configuration then the correct setting can be achieved on all Cortex-M
1997 * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
1998 * scheduler. Note however that some vendor specific peripheral libraries
1999 * assume a non-zero priority group setting, in which cases using a value
2000 * of zero will result in unpredictable behaviour. */
2001 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
2004 #endif /* #if ( ( configASSERT_DEFINED == 1 ) && ( portHAS_ARMV8M_MAIN_EXTENSION == 1 ) ) */
2005 /*-----------------------------------------------------------*/