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. */
39 #include "mpu_wrappers.h"
40 #include "mpu_syscall_numbers.h"
42 /* Portasm includes. */
45 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
47 /*-----------------------------------------------------------*/
50 * @brief Prototype of all Interrupt Service Routines (ISRs).
52 typedef void ( * portISR_t )( void );
54 /*-----------------------------------------------------------*/
57 * @brief Constants required to manipulate the NVIC.
59 #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
60 #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
61 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
62 #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
63 #define portNVIC_SHPR2_REG ( *( ( volatile uint32_t * ) 0xe000ed1c ) )
64 #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
65 #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
66 #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
67 #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
68 #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
69 #define portNVIC_PEND_SYSTICK_SET_BIT ( 1UL << 26UL )
70 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
71 #define portNVIC_PENDSV_PRI ( portMIN_INTERRUPT_PRIORITY << 16UL )
72 #define portNVIC_SYSTICK_PRI ( portMIN_INTERRUPT_PRIORITY << 24UL )
74 /*-----------------------------------------------------------*/
77 * @brief Constants required to manipulate the SCB.
79 #define portSCB_VTOR_REG ( *( ( portISR_t ** ) 0xe000ed08 ) )
80 #define portSCB_SYS_HANDLER_CTRL_STATE_REG ( *( ( volatile uint32_t * ) 0xe000ed24 ) )
81 #define portSCB_MEM_FAULT_ENABLE_BIT ( 1UL << 16UL )
83 /*-----------------------------------------------------------*/
86 * @brief Constants used to check the installation of the FreeRTOS interrupt handlers.
88 #define portVECTOR_INDEX_SVC ( 11 )
89 #define portVECTOR_INDEX_PENDSV ( 14 )
91 /*-----------------------------------------------------------*/
94 * @brief Constants used during system call enter and exit.
96 #define portPSR_STACK_PADDING_MASK ( 1UL << 9UL )
97 #define portEXC_RETURN_STACK_FRAME_TYPE_MASK ( 1UL << 4UL )
99 /*-----------------------------------------------------------*/
102 * @brief Offsets in the stack to the parameters when inside the SVC handler.
104 #define portOFFSET_TO_LR ( 5 )
105 #define portOFFSET_TO_PC ( 6 )
106 #define portOFFSET_TO_PSR ( 7 )
108 /*-----------------------------------------------------------*/
111 * @brief Constants required to manipulate the MPU.
113 #define portMPU_TYPE_REG ( *( ( volatile uint32_t * ) 0xe000ed90 ) )
114 #define portMPU_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed94 ) )
116 #define portMPU_RBAR_REG ( *( ( volatile uint32_t * ) 0xe000ed9c ) )
117 #define portMPU_RASR_REG ( *( ( volatile uint32_t * ) 0xe000eda0 ) )
119 /* MPU Region Attribute and Size Register (RASR) bitmasks. */
120 #define portMPU_RASR_AP_BITMASK ( 0x7UL << 24UL )
121 #define portMPU_RASR_S_C_B_BITMASK ( 0x7UL )
122 #define portMPU_RASR_S_C_B_LOCATION ( 16UL )
123 #define portMPU_RASR_SIZE_BITMASK ( 0x1FUL << 1UL )
124 #define portMPU_RASR_REGION_ENABLE_BITMASK ( 0x1UL )
126 /* MPU Region Base Address Register (RBAR) bitmasks. */
127 #define portMPU_RBAR_ADDRESS_BITMASK ( 0xFFFFFF00UL )
128 #define portMPU_RBAR_REGION_NUMBER_VALID_BITMASK ( 0x1UL << 4UL )
129 #define portMPU_RBAR_REGION_NUMBER_BITMASK ( 0x0000000FUL )
131 /* MPU Control Register (MPU_CTRL) bitmasks. */
132 #define portMPU_CTRL_ENABLE_BITMASK ( 0x1UL )
133 #define portMPU_CTRL_PRIV_BACKGROUND_ENABLE_BITMASK ( 0x1UL << 2UL ) /* PRIVDEFENA bit. */
135 /* Expected value of the portMPU_TYPE register. */
136 #define portEXPECTED_MPU_TYPE_VALUE ( 0x8UL << 8UL ) /* 8 DREGION unified. */
138 /* Extract first address of the MPU region as encoded in the
139 * RBAR (Region Base Address Register) value. */
140 #define portEXTRACT_FIRST_ADDRESS_FROM_RBAR( rbar ) \
141 ( ( rbar ) & portMPU_RBAR_ADDRESS_BITMASK )
143 /* Extract size of the MPU region as encoded in the
144 * RASR (Region Attribute and Size Register) value. */
145 #define portEXTRACT_REGION_SIZE_FROM_RASR( rasr ) \
146 ( 1 << ( ( ( ( rasr ) & portMPU_RASR_SIZE_BITMASK ) >> 1 )+ 1 ) )
148 /* Does addr lies within [start, end] address range? */
149 #define portIS_ADDRESS_WITHIN_RANGE( addr, start, end ) \
150 ( ( ( addr ) >= ( start ) ) && ( ( addr ) <= ( end ) ) )
152 /* Is the access request satisfied by the available permissions? */
153 #define portIS_AUTHORIZED( accessRequest, permissions ) \
154 ( ( ( permissions ) & ( accessRequest ) ) == accessRequest )
156 /* Max value that fits in a uint32_t type. */
157 #define portUINT32_MAX ( ~( ( uint32_t ) 0 ) )
159 /* Check if adding a and b will result in overflow. */
160 #define portADD_UINT32_WILL_OVERFLOW( a, b ) ( ( a ) > ( portUINT32_MAX - ( b ) ) )
162 /*-----------------------------------------------------------*/
165 * @brief The maximum 24-bit number.
167 * It is needed because the systick is a 24-bit counter.
169 #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
172 * @brief A fiddle factor to estimate the number of SysTick counts that would
173 * have occurred while the SysTick counter is stopped during tickless idle
176 #define portMISSED_COUNTS_FACTOR ( 94UL )
178 /*-----------------------------------------------------------*/
181 * @brief Constants required to set up the initial stack.
183 #define portINITIAL_XPSR ( 0x01000000 )
186 * @brief Initial EXC_RETURN value.
189 * 1111 1111 1111 1111 1111 1111 1111 1101
191 * Bit[3] - 1 --> Return to the Thread mode.
192 * Bit[2] - 1 --> Restore registers from the process stack.
193 * Bit[1] - 0 --> Reserved, 0.
194 * Bit[0] - 0 --> Reserved, 1.
196 #define portINITIAL_EXC_RETURN ( 0xfffffffdUL )
199 * @brief CONTROL register privileged bit mask.
201 * Bit[0] in CONTROL register tells the privilege:
202 * Bit[0] = 0 ==> The task is privileged.
203 * Bit[0] = 1 ==> The task is not privileged.
205 #define portCONTROL_PRIVILEGED_MASK ( 1UL << 0UL )
208 * @brief Initial CONTROL register values.
210 #define portINITIAL_CONTROL_UNPRIVILEGED ( 0x3 )
211 #define portINITIAL_CONTROL_PRIVILEGED ( 0x2 )
214 * @brief Let the user override the default SysTick clock rate. If defined by the
215 * user, this symbol must equal the SysTick clock rate when the CLK bit is 0 in the
216 * configuration register.
218 #ifndef configSYSTICK_CLOCK_HZ
219 #define configSYSTICK_CLOCK_HZ ( configCPU_CLOCK_HZ )
220 /* Ensure the SysTick is clocked at the same frequency as the core. */
221 #define portNVIC_SYSTICK_CLK_BIT_CONFIG ( portNVIC_SYSTICK_CLK_BIT )
223 /* Select the option to clock SysTick not at the same frequency as the core. */
224 #define portNVIC_SYSTICK_CLK_BIT_CONFIG ( 0 )
228 * @brief Let the user override the pre-loading of the initial LR with the
229 * address of prvTaskExitError() in case it messes up unwinding of the stack
232 #ifdef configTASK_RETURN_ADDRESS
233 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
235 #define portTASK_RETURN_ADDRESS prvTaskExitError
239 * @brief If portPRELOAD_REGISTERS then registers will be given an initial value
240 * when a task is created. This helps in debugging at the cost of code size.
242 #define portPRELOAD_REGISTERS 1
244 /*-----------------------------------------------------------*/
247 * @brief Used to catch tasks that attempt to return from their implementing
250 static void prvTaskExitError( void );
252 #if ( configENABLE_MPU == 1 )
255 * @brief Setup the Memory Protection Unit (MPU).
257 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
259 #endif /* configENABLE_MPU */
262 * @brief Setup the timer to generate the tick interrupts.
264 * The implementation in this file is weak to allow application writers to
265 * change the timer used to generate the tick interrupt.
267 void vPortSetupTimerInterrupt( void ) PRIVILEGED_FUNCTION;
270 * @brief Checks whether the current execution context is interrupt.
272 * @return pdTRUE if the current execution context is interrupt, pdFALSE
275 BaseType_t xPortIsInsideInterrupt( void );
278 * @brief Yield the processor.
280 void vPortYield( void ) PRIVILEGED_FUNCTION;
283 * @brief Enter critical section.
285 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
288 * @brief Exit from critical section.
290 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
293 * @brief SysTick handler.
295 void SysTick_Handler( void ) PRIVILEGED_FUNCTION;
298 * @brief C part of SVC handler.
300 portDONT_DISCARD void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) PRIVILEGED_FUNCTION;
302 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
305 * @brief Sets up the system call stack so that upon returning from
306 * SVC, the system call stack is used.
308 * @param pulTaskStack The current SP when the SVC was raised.
309 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
310 * @param ucSystemCallNumber The system call number of the system call.
312 void vSystemCallEnter( uint32_t * pulTaskStack,
314 uint8_t ucSystemCallNumber ) PRIVILEGED_FUNCTION;
316 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
318 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
321 * @brief Raise SVC for exiting from a system call.
323 void vRequestSystemCallExit( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
325 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
327 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
330 * @brief Sets up the task stack so that upon returning from
331 * SVC, the task stack is used again.
333 * @param pulSystemCallStack The current SP when the SVC was raised.
334 * @param ulLR The value of Link Register (EXC_RETURN) in the SVC handler.
336 void vSystemCallExit( uint32_t * pulSystemCallStack,
337 uint32_t ulLR ) PRIVILEGED_FUNCTION;
339 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
341 #if ( configENABLE_MPU == 1 )
344 * @brief Checks whether or not the calling task is privileged.
346 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
348 BaseType_t xPortIsTaskPrivileged( void ) PRIVILEGED_FUNCTION;
350 #endif /* configENABLE_MPU == 1 */
352 /*-----------------------------------------------------------*/
354 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
357 * @brief This variable is set to pdTRUE when the scheduler is started.
359 PRIVILEGED_DATA static BaseType_t xSchedulerRunning = pdFALSE;
364 * @brief Each task maintains its own interrupt status in the critical nesting
367 PRIVILEGED_DATA static volatile uint32_t ulCriticalNesting = 0xaaaaaaaaUL;
369 #if ( configUSE_TICKLESS_IDLE == 1 )
372 * @brief The number of SysTick increments that make up one tick period.
374 PRIVILEGED_DATA static uint32_t ulTimerCountsForOneTick = 0;
377 * @brief The maximum number of tick periods that can be suppressed is
378 * limited by the 24 bit resolution of the SysTick timer.
380 PRIVILEGED_DATA static uint32_t xMaximumPossibleSuppressedTicks = 0;
383 * @brief Compensate for the CPU cycles that pass while the SysTick is
384 * stopped (low power functionality only).
386 PRIVILEGED_DATA static uint32_t ulStoppedTimerCompensation = 0;
388 #endif /* configUSE_TICKLESS_IDLE */
390 /*-----------------------------------------------------------*/
392 #if ( configUSE_TICKLESS_IDLE == 1 )
394 __attribute__( ( weak ) ) void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
396 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements, ulSysTickDecrementsLeft;
397 TickType_t xModifiableIdleTime;
399 /* Make sure the SysTick reload value does not overflow the counter. */
400 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
402 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
405 /* Enter a critical section but don't use the taskENTER_CRITICAL()
406 * method as that will mask interrupts that should exit sleep mode. */
407 __asm volatile ( "cpsid i" ::: "memory" );
408 __asm volatile ( "dsb" );
409 __asm volatile ( "isb" );
411 /* If a context switch is pending or a task is waiting for the scheduler
412 * to be unsuspended then abandon the low power entry. */
413 if( eTaskConfirmSleepModeStatus() == eAbortSleep )
415 /* Re-enable interrupts - see comments above the cpsid instruction
417 __asm volatile ( "cpsie i" ::: "memory" );
421 /* Stop the SysTick momentarily. The time the SysTick is stopped for
422 * is accounted for as best it can be, but using the tickless mode will
423 * inevitably result in some tiny drift of the time maintained by the
424 * kernel with respect to calendar time. */
425 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT );
427 /* Use the SysTick current-value register to determine the number of
428 * SysTick decrements remaining until the next tick interrupt. If the
429 * current-value register is zero, then there are actually
430 * ulTimerCountsForOneTick decrements remaining, not zero, because the
431 * SysTick requests the interrupt when decrementing from 1 to 0. */
432 ulSysTickDecrementsLeft = portNVIC_SYSTICK_CURRENT_VALUE_REG;
434 if( ulSysTickDecrementsLeft == 0 )
436 ulSysTickDecrementsLeft = ulTimerCountsForOneTick;
439 /* Calculate the reload value required to wait xExpectedIdleTime
440 * tick periods. -1 is used because this code normally executes part
441 * way through the first tick period. But if the SysTick IRQ is now
442 * pending, then clear the IRQ, suppressing the first tick, and correct
443 * the reload value to reflect that the second tick period is already
444 * underway. The expected idle time is always at least two ticks. */
445 ulReloadValue = ulSysTickDecrementsLeft + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
447 if( ( portNVIC_INT_CTRL_REG & portNVIC_PEND_SYSTICK_SET_BIT ) != 0 )
449 portNVIC_INT_CTRL_REG = portNVIC_PEND_SYSTICK_CLEAR_BIT;
450 ulReloadValue -= ulTimerCountsForOneTick;
453 if( ulReloadValue > ulStoppedTimerCompensation )
455 ulReloadValue -= ulStoppedTimerCompensation;
458 /* Set the new reload value. */
459 portNVIC_SYSTICK_LOAD_REG = ulReloadValue;
461 /* Clear the SysTick count flag and set the count value back to
463 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
465 /* Restart SysTick. */
466 portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
468 /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can
469 * set its parameter to 0 to indicate that its implementation contains
470 * its own wait for interrupt or wait for event instruction, and so wfi
471 * should not be executed again. However, the original expected idle
472 * time variable must remain unmodified, so a copy is taken. */
473 xModifiableIdleTime = xExpectedIdleTime;
474 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
476 if( xModifiableIdleTime > 0 )
478 __asm volatile ( "dsb" ::: "memory" );
479 __asm volatile ( "wfi" );
480 __asm volatile ( "isb" );
483 configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
485 /* Re-enable interrupts to allow the interrupt that brought the MCU
486 * out of sleep mode to execute immediately. See comments above
487 * the cpsid instruction above. */
488 __asm volatile ( "cpsie i" ::: "memory" );
489 __asm volatile ( "dsb" );
490 __asm volatile ( "isb" );
492 /* Disable interrupts again because the clock is about to be stopped
493 * and interrupts that execute while the clock is stopped will increase
494 * any slippage between the time maintained by the RTOS and calendar
496 __asm volatile ( "cpsid i" ::: "memory" );
497 __asm volatile ( "dsb" );
498 __asm volatile ( "isb" );
500 /* Disable the SysTick clock without reading the
501 * portNVIC_SYSTICK_CTRL_REG register to ensure the
502 * portNVIC_SYSTICK_COUNT_FLAG_BIT is not cleared if it is set. Again,
503 * the time the SysTick is stopped for is accounted for as best it can
504 * be, but using the tickless mode will inevitably result in some tiny
505 * drift of the time maintained by the kernel with respect to calendar
507 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT );
509 /* Determine whether the SysTick has already counted to zero. */
510 if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
512 uint32_t ulCalculatedLoadValue;
514 /* The tick interrupt ended the sleep (or is now pending), and
515 * a new tick period has started. Reset portNVIC_SYSTICK_LOAD_REG
516 * with whatever remains of the new tick period. */
517 ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );
519 /* Don't allow a tiny value, or values that have somehow
520 * underflowed because the post sleep hook did something
521 * that took too long or because the SysTick current-value register
523 if( ( ulCalculatedLoadValue <= ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )
525 ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );
528 portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;
530 /* As the pending tick will be processed as soon as this
531 * function exits, the tick value maintained by the tick is stepped
532 * forward by one less than the time spent waiting. */
533 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
537 /* Something other than the tick interrupt ended the sleep. */
539 /* Use the SysTick current-value register to determine the
540 * number of SysTick decrements remaining until the expected idle
541 * time would have ended. */
542 ulSysTickDecrementsLeft = portNVIC_SYSTICK_CURRENT_VALUE_REG;
543 #if ( portNVIC_SYSTICK_CLK_BIT_CONFIG != portNVIC_SYSTICK_CLK_BIT )
545 /* If the SysTick is not using the core clock, the current-
546 * value register might still be zero here. In that case, the
547 * SysTick didn't load from the reload register, and there are
548 * ulReloadValue decrements remaining in the expected idle
550 if( ulSysTickDecrementsLeft == 0 )
552 ulSysTickDecrementsLeft = ulReloadValue;
555 #endif /* portNVIC_SYSTICK_CLK_BIT_CONFIG */
557 /* Work out how long the sleep lasted rounded to complete tick
558 * periods (not the ulReload value which accounted for part
560 ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - ulSysTickDecrementsLeft;
562 /* How many complete tick periods passed while the processor
564 ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
566 /* The reload value is set to whatever fraction of a single tick
568 portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;
571 /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG again,
572 * then set portNVIC_SYSTICK_LOAD_REG back to its standard value. If
573 * the SysTick is not using the core clock, temporarily configure it to
574 * use the core clock. This configuration forces the SysTick to load
575 * from portNVIC_SYSTICK_LOAD_REG immediately instead of at the next
576 * cycle of the other clock. Then portNVIC_SYSTICK_LOAD_REG is ready
577 * to receive the standard value immediately. */
578 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
579 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
580 #if ( portNVIC_SYSTICK_CLK_BIT_CONFIG == portNVIC_SYSTICK_CLK_BIT )
582 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
586 /* The temporary usage of the core clock has served its purpose,
587 * as described above. Resume usage of the other clock. */
588 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT;
590 if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
592 /* The partial tick period already ended. Be sure the SysTick
593 * counts it only once. */
594 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0;
597 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
598 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
600 #endif /* portNVIC_SYSTICK_CLK_BIT_CONFIG */
602 /* Step the tick to account for any tick periods that elapsed. */
603 vTaskStepTick( ulCompleteTickPeriods );
605 /* Exit with interrupts enabled. */
606 __asm volatile ( "cpsie i" ::: "memory" );
610 #endif /* configUSE_TICKLESS_IDLE */
612 /*-----------------------------------------------------------*/
614 __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void ) /* PRIVILEGED_FUNCTION */
616 /* Calculate the constants required to configure the tick interrupt. */
617 #if ( configUSE_TICKLESS_IDLE == 1 )
619 ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
620 xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
621 ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
623 #endif /* configUSE_TICKLESS_IDLE */
625 /* Stop and reset SysTick.
627 * QEMU versions older than 7.0.0 contain a bug which causes an error if we
628 * enable SysTick without first selecting a valid clock source. We trigger
629 * the bug if we change clock sources from a clock with a zero clock period
630 * to one with a nonzero clock period and enable Systick at the same time.
631 * So we configure the CLKSOURCE bit here, prior to setting the ENABLE bit.
632 * This workaround avoids the bug in QEMU versions older than 7.0.0. */
633 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT_CONFIG;
634 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
636 /* Configure SysTick to interrupt at the requested rate. */
637 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
638 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
641 /*-----------------------------------------------------------*/
643 static void prvTaskExitError( void )
645 volatile uint32_t ulDummy = 0UL;
647 /* A function that implements a task must not exit or attempt to return to
648 * its caller as there is nothing to return to. If a task wants to exit it
649 * should instead call vTaskDelete( NULL ). Artificially force an assert()
650 * to be triggered if configASSERT() is defined, then stop here so
651 * application writers can catch the error. */
652 configASSERT( ulCriticalNesting == ~0UL );
653 portDISABLE_INTERRUPTS();
655 while( ulDummy == 0 )
657 /* This file calls prvTaskExitError() after the scheduler has been
658 * started to remove a compiler warning about the function being
659 * defined but never called. ulDummy is used purely to quieten other
660 * warnings about code appearing after this function is called - making
661 * ulDummy volatile makes the compiler think the function could return
662 * and therefore not output an 'unreachable code' warning for code that
663 * appears after it. */
667 /*-----------------------------------------------------------*/
669 #if ( configENABLE_MPU == 1 )
671 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
673 uint32_t ulRegionSize, ulReturnValue = 7UL;
675 /* 256 is the smallest region size, 31 is the largest valid value for
677 for( ulRegionSize = 256UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
679 if( ulActualSizeInBytes <= ulRegionSize )
689 /* Shift the code by one before returning so it can be written directly
690 * into the the correct bit position of the attribute register. */
691 return( ulReturnValue << 1UL );
694 #endif /* configENABLE_MPU */
696 /*-----------------------------------------------------------*/
698 #if ( configENABLE_MPU == 1 )
700 static void prvSetupMPU( void ) /* PRIVILEGED_FUNCTION */
702 #if defined( __ARMCC_VERSION )
704 /* Declaration when these variable are defined in code instead of being
705 * exported from linker scripts. */
706 extern uint32_t * __privileged_functions_start__;
707 extern uint32_t * __privileged_functions_end__;
708 extern uint32_t * __FLASH_segment_start__;
709 extern uint32_t * __FLASH_segment_end__;
710 extern uint32_t * __privileged_sram_start__;
711 extern uint32_t * __privileged_sram_end__;
713 #else /* if defined( __ARMCC_VERSION ) */
715 /* Declaration when these variable are exported from linker scripts. */
716 extern uint32_t __privileged_functions_start__[];
717 extern uint32_t __privileged_functions_end__[];
718 extern uint32_t __FLASH_segment_start__[];
719 extern uint32_t __FLASH_segment_end__[];
720 extern uint32_t __privileged_sram_start__[];
721 extern uint32_t __privileged_sram_end__[];
723 #endif /* defined( __ARMCC_VERSION ) */
725 /* Ensure that the MPU is present. */
726 configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );
728 /* Check that the MPU is present. */
729 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
731 /* Setup privileged flash as Read Only so that privileged tasks can
732 * read it but not modify. */
733 portMPU_RBAR_REG = ( ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
734 ( portMPU_RBAR_REGION_NUMBER_VALID_BITMASK ) |
735 ( portPRIVILEGED_FLASH_REGION ) );
737 portMPU_RASR_REG = ( ( portMPU_REGION_PRIV_RO_UNPRIV_NA ) |
738 ( ( configS_C_B_FLASH & portMPU_RASR_S_C_B_BITMASK ) << portMPU_RASR_S_C_B_LOCATION ) |
739 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
740 ( portMPU_RASR_REGION_ENABLE_BITMASK ) );
742 /* Setup unprivileged flash as Read Only by both privileged and
743 * unprivileged tasks. All tasks can read it but no-one can modify. */
744 portMPU_RBAR_REG = ( ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
745 ( portMPU_RBAR_REGION_NUMBER_VALID_BITMASK ) |
746 ( portUNPRIVILEGED_FLASH_REGION ) );
748 portMPU_RASR_REG = ( ( portMPU_REGION_PRIV_RO_UNPRIV_RO ) |
749 ( ( configS_C_B_FLASH & portMPU_RASR_S_C_B_BITMASK ) << portMPU_RASR_S_C_B_LOCATION ) |
750 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
751 ( portMPU_RASR_REGION_ENABLE_BITMASK ) );
753 /* Setup RAM containing kernel data for privileged access only. */
754 portMPU_RBAR_REG = ( ( uint32_t ) __privileged_sram_start__ ) | /* Base address. */
755 ( portMPU_RBAR_REGION_NUMBER_VALID_BITMASK ) |
756 ( portPRIVILEGED_RAM_REGION );
758 portMPU_RASR_REG = ( ( portMPU_REGION_PRIV_RW_UNPRIV_NA ) |
759 ( portMPU_REGION_EXECUTE_NEVER ) |
760 ( ( configS_C_B_SRAM & portMPU_RASR_S_C_B_BITMASK ) << portMPU_RASR_S_C_B_LOCATION ) |
761 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_sram_end__ - ( uint32_t ) __privileged_sram_start__ ) |
762 ( portMPU_RASR_REGION_ENABLE_BITMASK ) );
764 /* Enable MPU with privileged background access i.e. unmapped
765 * regions have privileged access. */
766 portMPU_CTRL_REG |= ( portMPU_CTRL_PRIV_BACKGROUND_ENABLE_BITMASK |
767 portMPU_CTRL_ENABLE_BITMASK );
771 #endif /* configENABLE_MPU */
773 /*-----------------------------------------------------------*/
775 void vPortYield( void ) /* PRIVILEGED_FUNCTION */
777 /* Set a PendSV to request a context switch. */
778 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
780 /* Barriers are normally not required but do ensure the code is
781 * completely within the specified behaviour for the architecture. */
782 __asm volatile ( "dsb" ::: "memory" );
783 __asm volatile ( "isb" );
786 /*-----------------------------------------------------------*/
788 void vPortEnterCritical( void ) /* PRIVILEGED_FUNCTION */
790 portDISABLE_INTERRUPTS();
793 /* Barriers are normally not required but do ensure the code is
794 * completely within the specified behaviour for the architecture. */
795 __asm volatile ( "dsb" ::: "memory" );
796 __asm volatile ( "isb" );
799 /*-----------------------------------------------------------*/
801 void vPortExitCritical( void ) /* PRIVILEGED_FUNCTION */
803 configASSERT( ulCriticalNesting );
806 if( ulCriticalNesting == 0 )
808 portENABLE_INTERRUPTS();
812 /*-----------------------------------------------------------*/
814 void SysTick_Handler( void ) /* PRIVILEGED_FUNCTION */
816 uint32_t ulPreviousMask;
818 ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();
822 /* Increment the RTOS tick. */
823 if( xTaskIncrementTick() != pdFALSE )
825 traceISR_EXIT_TO_SCHEDULER();
826 /* Pend a context switch. */
827 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
835 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );
838 /*-----------------------------------------------------------*/
840 void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTION portDONT_DISCARD */
842 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 1 ) )
844 #if defined( __ARMCC_VERSION )
846 /* Declaration when these variable are defined in code instead of being
847 * exported from linker scripts. */
848 extern uint32_t * __syscalls_flash_start__;
849 extern uint32_t * __syscalls_flash_end__;
853 /* Declaration when these variable are exported from linker scripts. */
854 extern uint32_t __syscalls_flash_start__[];
855 extern uint32_t __syscalls_flash_end__[];
857 #endif /* defined( __ARMCC_VERSION ) */
859 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 1 ) */
864 /* Register are stored on the stack in the following order - R0, R1, R2, R3,
865 * R12, LR, PC, xPSR. */
866 ulPC = pulCallerStackAddress[ portOFFSET_TO_PC ];
867 ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
869 switch( ucSVCNumber )
871 case portSVC_START_SCHEDULER:
872 /* Setup the context of the first task so that the first task starts
874 vRestoreContextOfFirstTask();
877 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 1 ) )
879 case portSVC_RAISE_PRIVILEGE:
880 /* Only raise the privilege, if the svc was raised from any of
881 * the system calls. */
882 if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
883 ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
889 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 1 ) */
891 #if ( configENABLE_MPU == 1 )
897 #endif /* configENABLE_MPU == 1 */
900 /* Incorrect SVC call. */
901 configASSERT( pdFALSE );
905 /*-----------------------------------------------------------*/
907 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
909 void vSystemCallEnter( uint32_t * pulTaskStack,
911 uint8_t ucSystemCallNumber ) /* PRIVILEGED_FUNCTION */
913 extern TaskHandle_t pxCurrentTCB;
914 extern UBaseType_t uxSystemCallImplementations[ NUM_SYSTEM_CALLS ];
915 xMPU_SETTINGS * pxMpuSettings;
916 uint32_t * pulSystemCallStack;
917 uint32_t ulSystemCallLocation, i;
918 const uint32_t ulStackFrameSize = 8;
920 #if defined( __ARMCC_VERSION )
922 /* Declaration when these variable are defined in code instead of being
923 * exported from linker scripts. */
924 extern uint32_t * __syscalls_flash_start__;
925 extern uint32_t * __syscalls_flash_end__;
929 /* Declaration when these variable are exported from linker scripts. */
930 extern uint32_t __syscalls_flash_start__[];
931 extern uint32_t __syscalls_flash_end__[];
933 #endif /* #if defined( __ARMCC_VERSION ) */
935 ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
936 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
939 * 1. SVC is raised from the system call section (i.e. application is
940 * not raising SVC directly).
941 * 2. pxMpuSettings->xSystemCallStackInfo.pulTaskStack must be NULL as
942 * it is non-NULL only during the execution of a system call (i.e.
943 * between system call enter and exit).
944 * 3. System call is not for a kernel API disabled by the configuration
945 * in FreeRTOSConfig.h.
946 * 4. We do not need to check that ucSystemCallNumber is within range
947 * because the assembly SVC handler checks that before calling
950 if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
951 ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) &&
952 ( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL ) &&
953 ( uxSystemCallImplementations[ ucSystemCallNumber ] != ( UBaseType_t ) 0 ) )
955 pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
957 /* Make space on the system call stack for the stack frame. */
958 pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
960 /* Copy the stack frame. */
961 for( i = 0; i < ulStackFrameSize; i++ )
963 pulSystemCallStack[ i ] = pulTaskStack[ i ];
966 /* Store the value of the Link Register before the SVC was raised.
967 * It contains the address of the caller of the System Call entry
968 * point (i.e. the caller of the MPU_<API>). We need to restore it
969 * when we exit from the system call. */
970 pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
972 /* Use the pulSystemCallStack in thread mode. */
973 __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
975 /* Start executing the system call upon returning from this handler. */
976 pulSystemCallStack[ portOFFSET_TO_PC ] = uxSystemCallImplementations[ ucSystemCallNumber ];
978 /* Raise a request to exit from the system call upon finishing the
980 pulSystemCallStack[ portOFFSET_TO_LR ] = ( uint32_t ) vRequestSystemCallExit;
982 /* Remember the location where we should copy the stack frame when we exit from
983 * the system call. */
984 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
986 /* Record if the hardware used padding to force the stack pointer
987 * to be double word aligned. */
988 if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
990 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
994 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
997 /* We ensure in pxPortInitialiseStack that the system call stack is
998 * double word aligned and therefore, there is no need of padding.
999 * Clear the bit[9] of stacked xPSR. */
1000 pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
1002 /* Raise the privilege for the duration of the system call. */
1005 " .syntax unified \n"
1006 " mrs r0, control \n" /* Obtain current control value. */
1007 " movs r1, #1 \n" /* r1 = 1. */
1008 " bics r0, r1 \n" /* Clear nPRIV bit. */
1009 " msr control, r0 \n" /* Write back new control value. */
1010 ::: "r0", "r1", "memory"
1015 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1017 /*-----------------------------------------------------------*/
1019 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
1021 void vRequestSystemCallExit( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
1023 __asm volatile ( "svc %0 \n" ::"i" ( portSVC_SYSTEM_CALL_EXIT ) : "memory" );
1026 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1028 /*-----------------------------------------------------------*/
1030 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
1032 void vSystemCallExit( uint32_t * pulSystemCallStack,
1033 uint32_t ulLR ) /* PRIVILEGED_FUNCTION */
1035 extern TaskHandle_t pxCurrentTCB;
1036 xMPU_SETTINGS * pxMpuSettings;
1037 uint32_t * pulTaskStack;
1038 uint32_t ulSystemCallLocation, i;
1039 const uint32_t ulStackFrameSize = 8;
1041 #if defined( __ARMCC_VERSION )
1043 /* Declaration when these variable are defined in code instead of being
1044 * exported from linker scripts. */
1045 extern uint32_t * __privileged_functions_start__;
1046 extern uint32_t * __privileged_functions_end__;
1050 /* Declaration when these variable are exported from linker scripts. */
1051 extern uint32_t __privileged_functions_start__[];
1052 extern uint32_t __privileged_functions_end__[];
1054 #endif /* #if defined( __ARMCC_VERSION ) */
1056 ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
1057 pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
1060 * 1. SVC is raised from the privileged code (i.e. application is not
1061 * raising SVC directly). This SVC is only raised from
1062 * vRequestSystemCallExit which is in the privileged code section.
1063 * 2. pxMpuSettings->xSystemCallStackInfo.pulTaskStack must not be NULL -
1064 * this means that we previously entered a system call and the
1065 * application is not attempting to exit without entering a system
1068 if( ( ulSystemCallLocation >= ( uint32_t ) __privileged_functions_start__ ) &&
1069 ( ulSystemCallLocation <= ( uint32_t ) __privileged_functions_end__ ) &&
1070 ( pxMpuSettings->xSystemCallStackInfo.pulTaskStack != NULL ) )
1072 pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
1074 /* Make space on the task stack for the stack frame. */
1075 pulTaskStack = pulTaskStack - ulStackFrameSize;
1077 /* Copy the stack frame. */
1078 for( i = 0; i < ulStackFrameSize; i++ )
1080 pulTaskStack[ i ] = pulSystemCallStack[ i ];
1083 /* Use the pulTaskStack in thread mode. */
1084 __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
1086 /* Return to the caller of the System Call entry point (i.e. the
1087 * caller of the MPU_<API>). */
1088 pulTaskStack[ portOFFSET_TO_PC ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
1090 /* Ensure that LR has a valid value.*/
1091 pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
1093 /* If the hardware used padding to force the stack pointer
1094 * to be double word aligned, set the stacked xPSR bit[9],
1095 * otherwise clear it. */
1096 if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
1098 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
1102 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
1105 /* This is not NULL only for the duration of the system call. */
1106 pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
1108 /* Drop the privilege before returning to the thread mode. */
1111 " .syntax unified \n"
1112 " mrs r0, control \n" /* Obtain current control value. */
1113 " movs r1, #1 \n" /* r1 = 1. */
1114 " orrs r0, r1 \n" /* Set nPRIV bit. */
1115 " msr control, r0 \n" /* Write back new control value. */
1116 ::: "r0", "r1", "memory"
1121 #endif /* ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
1123 /*-----------------------------------------------------------*/
1125 #if ( configENABLE_MPU == 1 )
1127 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
1129 BaseType_t xTaskIsPrivileged = pdFALSE;
1130 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1132 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1134 xTaskIsPrivileged = pdTRUE;
1137 return xTaskIsPrivileged;
1140 #endif /* configENABLE_MPU == 1 */
1142 /*-----------------------------------------------------------*/
1144 #if ( configENABLE_MPU == 1 )
1146 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
1147 TaskFunction_t pxCode,
1148 void * pvParameters,
1149 BaseType_t xRunPrivileged,
1150 xMPU_SETTINGS * xMPUSettings ) /* PRIVILEGED_FUNCTION */
1152 xMPUSettings->ulContext[ 0 ] = 0x04040404; /* r4. */
1153 xMPUSettings->ulContext[ 1 ] = 0x05050505; /* r5. */
1154 xMPUSettings->ulContext[ 2 ] = 0x06060606; /* r6. */
1155 xMPUSettings->ulContext[ 3 ] = 0x07070707; /* r7. */
1156 xMPUSettings->ulContext[ 4 ] = 0x08080808; /* r8. */
1157 xMPUSettings->ulContext[ 5 ] = 0x09090909; /* r9. */
1158 xMPUSettings->ulContext[ 6 ] = 0x10101010; /* r10. */
1159 xMPUSettings->ulContext[ 7 ] = 0x11111111; /* r11. */
1161 xMPUSettings->ulContext[ 8 ] = ( uint32_t ) pvParameters; /* r0. */
1162 xMPUSettings->ulContext[ 9 ] = 0x01010101; /* r1. */
1163 xMPUSettings->ulContext[ 10 ] = 0x02020202; /* r2. */
1164 xMPUSettings->ulContext[ 11 ] = 0x03030303; /* r3. */
1165 xMPUSettings->ulContext[ 12 ] = 0x12121212; /* r12. */
1166 xMPUSettings->ulContext[ 13 ] = ( uint32_t ) portTASK_RETURN_ADDRESS; /* LR. */
1167 xMPUSettings->ulContext[ 14 ] = ( uint32_t ) pxCode; /* PC. */
1168 xMPUSettings->ulContext[ 15 ] = portINITIAL_XPSR; /* xPSR. */
1170 xMPUSettings->ulContext[ 16 ] = ( uint32_t ) ( pxTopOfStack - 8 ); /* PSP with the hardware saved stack. */
1171 if( xRunPrivileged == pdTRUE )
1173 xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
1174 xMPUSettings->ulContext[ 17 ] = ( uint32_t ) portINITIAL_CONTROL_PRIVILEGED; /* CONTROL. */
1178 xMPUSettings->ulTaskFlags &= ( ~portTASK_IS_PRIVILEGED_FLAG );
1179 xMPUSettings->ulContext[ 17 ] = ( uint32_t ) portINITIAL_CONTROL_UNPRIVILEGED; /* CONTROL. */
1181 xMPUSettings->ulContext[ 18 ] = portINITIAL_EXC_RETURN; /* LR (EXC_RETURN). */
1183 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
1185 /* Ensure that the system call stack is double word aligned. */
1186 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
1187 xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
1188 ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
1190 /* This is not NULL only for the duration of a system call. */
1191 xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
1193 #endif /* configUSE_MPU_WRAPPERS_V1 == 0 */
1195 return &( xMPUSettings->ulContext[ 19 ] );
1198 #else /* configENABLE_MPU */
1200 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
1201 TaskFunction_t pxCode,
1202 void * pvParameters ) /* PRIVILEGED_FUNCTION */
1204 /* Simulate the stack frame as it would be created by a context switch
1206 #if ( portPRELOAD_REGISTERS == 0 )
1208 pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
1209 *pxTopOfStack = portINITIAL_XPSR; /* xPSR. */
1211 *pxTopOfStack = ( StackType_t ) pxCode; /* PC. */
1213 *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR. */
1214 pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
1215 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0. */
1216 pxTopOfStack -= 9; /* R11..R4, EXC_RETURN. */
1217 *pxTopOfStack = portINITIAL_EXC_RETURN;
1219 #else /* portPRELOAD_REGISTERS */
1221 pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
1222 *pxTopOfStack = portINITIAL_XPSR; /* xPSR. */
1224 *pxTopOfStack = ( StackType_t ) pxCode; /* PC. */
1226 *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR. */
1228 *pxTopOfStack = ( StackType_t ) 0x12121212UL; /* R12. */
1230 *pxTopOfStack = ( StackType_t ) 0x03030303UL; /* R3. */
1232 *pxTopOfStack = ( StackType_t ) 0x02020202UL; /* R2. */
1234 *pxTopOfStack = ( StackType_t ) 0x01010101UL; /* R1. */
1236 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0. */
1238 *pxTopOfStack = ( StackType_t ) 0x11111111UL; /* R11. */
1240 *pxTopOfStack = ( StackType_t ) 0x10101010UL; /* R10. */
1242 *pxTopOfStack = ( StackType_t ) 0x09090909UL; /* R09. */
1244 *pxTopOfStack = ( StackType_t ) 0x08080808UL; /* R08. */
1246 *pxTopOfStack = ( StackType_t ) 0x07070707UL; /* R07. */
1248 *pxTopOfStack = ( StackType_t ) 0x06060606UL; /* R06. */
1250 *pxTopOfStack = ( StackType_t ) 0x05050505UL; /* R05. */
1252 *pxTopOfStack = ( StackType_t ) 0x04040404UL; /* R04. */
1254 *pxTopOfStack = portINITIAL_EXC_RETURN; /* EXC_RETURN. */
1256 #endif /* portPRELOAD_REGISTERS */
1258 return pxTopOfStack;
1261 #endif /* configENABLE_MPU */
1263 /*-----------------------------------------------------------*/
1265 BaseType_t xPortStartScheduler( void ) /* PRIVILEGED_FUNCTION */
1267 /* An application can install FreeRTOS interrupt handlers in one of the
1269 * 1. Direct Routing - Install the functions SVC_Handler and PendSV_Handler
1270 * for SVCall and PendSV interrupts respectively.
1271 * 2. Indirect Routing - Install separate handlers for SVCall and PendSV
1272 * interrupts and route program control from those handlers to
1273 * SVC_Handler and PendSV_Handler functions.
1275 * Applications that use Indirect Routing must set
1276 * configCHECK_HANDLER_INSTALLATION to 0 in their FreeRTOSConfig.h. Direct
1277 * routing, which is validated here when configCHECK_HANDLER_INSTALLATION
1278 * is 1, should be preferred when possible. */
1279 #if ( configCHECK_HANDLER_INSTALLATION == 1 )
1281 const portISR_t * const pxVectorTable = portSCB_VTOR_REG;
1283 /* Validate that the application has correctly installed the FreeRTOS
1284 * handlers for SVCall and PendSV interrupts. We do not check the
1285 * installation of the SysTick handler because the application may
1286 * choose to drive the RTOS tick using a timer other than the SysTick
1287 * timer by overriding the weak function vPortSetupTimerInterrupt().
1289 * Assertion failures here indicate incorrect installation of the
1290 * FreeRTOS handlers. For help installing the FreeRTOS handlers, see
1291 * https://www.freertos.org/Why-FreeRTOS/FAQs.
1293 * Systems with a configurable address for the interrupt vector table
1294 * can also encounter assertion failures or even system faults here if
1295 * VTOR is not set correctly to point to the application's vector table. */
1296 configASSERT( pxVectorTable[ portVECTOR_INDEX_SVC ] == SVC_Handler );
1297 configASSERT( pxVectorTable[ portVECTOR_INDEX_PENDSV ] == PendSV_Handler );
1299 #endif /* configCHECK_HANDLER_INSTALLATION */
1301 /* Make PendSV and SysTick the lowest priority interrupts, and make SVCall
1302 * the highest priority. */
1303 portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
1304 portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
1305 portNVIC_SHPR2_REG = 0;
1307 #if ( configENABLE_MPU == 1 )
1309 /* Setup the Memory Protection Unit (MPU). */
1312 #endif /* configENABLE_MPU */
1314 /* Start the timer that generates the tick ISR. Interrupts are disabled
1316 vPortSetupTimerInterrupt();
1318 /* Initialize the critical nesting count ready for the first task. */
1319 ulCriticalNesting = 0;
1321 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
1323 xSchedulerRunning = pdTRUE;
1327 /* Start the first task. */
1330 /* Should never get here as the tasks will now be executing. Call the task
1331 * exit error function to prevent compiler warnings about a static function
1332 * not being called in the case that the application writer overrides this
1333 * functionality by defining configTASK_RETURN_ADDRESS. Call
1334 * vTaskSwitchContext() so link time optimization does not remove the
1336 vTaskSwitchContext();
1339 /* Should not get here. */
1343 /*-----------------------------------------------------------*/
1345 void vPortEndScheduler( void ) /* PRIVILEGED_FUNCTION */
1347 /* Not implemented in ports where there is nothing to return to.
1348 * Artificially force an assert. */
1349 configASSERT( ulCriticalNesting == 1000UL );
1352 /*-----------------------------------------------------------*/
1354 #if ( configENABLE_MPU == 1 )
1356 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1357 const struct xMEMORY_REGION * const xRegions,
1358 StackType_t * pxBottomOfStack,
1359 configSTACK_DEPTH_TYPE uxStackDepth )
1361 #if defined( __ARMCC_VERSION )
1363 /* Declaration when these variable are defined in code instead of being
1364 * exported from linker scripts. */
1365 extern uint32_t * __SRAM_segment_start__;
1366 extern uint32_t * __SRAM_segment_end__;
1367 extern uint32_t * __privileged_sram_start__;
1368 extern uint32_t * __privileged_sram_end__;
1371 /* Declaration when these variable are exported from linker scripts. */
1372 extern uint32_t __SRAM_segment_start__[];
1373 extern uint32_t __SRAM_segment_end__[];
1374 extern uint32_t __privileged_sram_start__[];
1375 extern uint32_t __privileged_sram_end__[];
1377 #endif /* defined( __ARMCC_VERSION ) */
1382 if( xRegions == NULL )
1384 /* No MPU regions are specified so allow access to all RAM. */
1385 xMPUSettings->xRegionsSettings[ 0 ].ulRBAR =
1386 ( ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
1387 ( portMPU_RBAR_REGION_NUMBER_VALID_BITMASK ) |
1388 ( portSTACK_REGION ) ); /* Region number. */
1390 xMPUSettings->xRegionsSettings[ 0 ].ulRASR =
1391 ( ( portMPU_REGION_PRIV_RW_UNPRIV_RW ) |
1392 ( portMPU_REGION_EXECUTE_NEVER ) |
1393 ( ( configS_C_B_SRAM & portMPU_RASR_S_C_B_BITMASK ) << portMPU_RASR_S_C_B_LOCATION ) |
1394 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
1395 ( portMPU_RASR_REGION_ENABLE_BITMASK ) );
1398 /* Invalidate user configurable regions. */
1399 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1401 xMPUSettings->xRegionsSettings[ ul ].ulRBAR = ( ( ul - 1UL ) | portMPU_RBAR_REGION_NUMBER_VALID_BITMASK );
1402 xMPUSettings->xRegionsSettings[ ul ].ulRASR = 0UL;
1407 /* This function is called automatically when the task is created - in
1408 * which case the stack region parameters will be valid. At all other
1409 * times the stack parameters will not be valid and it is assumed that the
1410 * stack region has already been configured. */
1411 if( uxStackDepth > 0 )
1413 /* Define the region that allows access to the stack. */
1414 xMPUSettings->xRegionsSettings[ 0 ].ulRBAR =
1415 ( ( ( uint32_t ) pxBottomOfStack ) |
1416 ( portMPU_RBAR_REGION_NUMBER_VALID_BITMASK ) |
1417 ( portSTACK_REGION ) ); /* Region number. */
1419 xMPUSettings->xRegionsSettings[ 0 ].ulRASR =
1420 ( ( portMPU_REGION_PRIV_RW_UNPRIV_RW ) |
1421 ( portMPU_REGION_EXECUTE_NEVER ) |
1422 ( prvGetMPURegionSizeSetting( uxStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
1423 ( ( configS_C_B_SRAM & portMPU_RASR_S_C_B_BITMASK ) << portMPU_RASR_S_C_B_LOCATION ) |
1424 ( portMPU_RASR_REGION_ENABLE_BITMASK ) );
1429 for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1431 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
1433 /* Translate the generic region definition contained in
1434 * xRegions into the CM0+ specific MPU settings that are then
1435 * stored in xMPUSettings. */
1436 xMPUSettings->xRegionsSettings[ ul ].ulRBAR =
1437 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
1438 ( portMPU_RBAR_REGION_NUMBER_VALID_BITMASK ) |
1439 ( ul - 1UL ); /* Region number. */
1441 xMPUSettings->xRegionsSettings[ ul ].ulRASR =
1442 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
1443 ( xRegions[ lIndex ].ulParameters ) |
1444 ( portMPU_RASR_REGION_ENABLE_BITMASK );
1448 /* Invalidate the region. */
1449 xMPUSettings->xRegionsSettings[ ul ].ulRBAR = ( ( ul - 1UL ) | portMPU_RBAR_REGION_NUMBER_VALID_BITMASK );
1450 xMPUSettings->xRegionsSettings[ ul ].ulRASR = 0UL;
1458 #endif /* configENABLE_MPU */
1460 /*-----------------------------------------------------------*/
1462 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
1464 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1465 uint32_t ulBufferLength,
1466 uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1469 uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1470 uint32_t ulRegionStart, ulRegionSize, ulRegionEnd;
1471 uint32_t ulMPURegionAccessPermissions;
1472 BaseType_t xAccessGranted = pdFALSE;
1473 const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1475 if( xSchedulerRunning == pdFALSE )
1477 /* Grant access to all the kernel objects before the scheduler
1478 * is started. It is necessary because there is no task running
1479 * yet and therefore, we cannot use the permissions of any
1481 xAccessGranted = pdTRUE;
1483 else if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1485 xAccessGranted = pdTRUE;
1489 if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1491 ulBufferStartAddress = ( uint32_t ) pvBuffer;
1492 ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1494 for( i = 0; i < portTOTAL_NUM_REGIONS; i++ )
1496 /* Is the MPU region enabled? */
1497 if( ( xTaskMpuSettings->xRegionsSettings[ i ].ulRASR &
1498 portMPU_RASR_REGION_ENABLE_BITMASK ) == portMPU_RASR_REGION_ENABLE_BITMASK )
1500 ulRegionStart = portEXTRACT_FIRST_ADDRESS_FROM_RBAR( xTaskMpuSettings->xRegionsSettings[ i ].ulRBAR );
1501 ulRegionSize = portEXTRACT_REGION_SIZE_FROM_RASR( xTaskMpuSettings->xRegionsSettings[ i ].ulRASR );
1502 ulRegionEnd = ulRegionStart + ulRegionSize;
1504 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1507 portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1511 ulMPURegionAccessPermissions = xTaskMpuSettings->xRegionsSettings[ i ].ulRASR &
1512 portMPU_RASR_AP_BITMASK;
1514 if( ulAccessRequested == tskMPU_READ_PERMISSION ) /* RO. */
1516 if( ( ulMPURegionAccessPermissions == portMPU_REGION_PRIV_RW_UNPRIV_RO ) ||
1517 ( ulMPURegionAccessPermissions == portMPU_REGION_PRIV_RO_UNPRIV_RO ) ||
1518 ( ulMPURegionAccessPermissions == portMPU_REGION_PRIV_RW_UNPRIV_RW ) )
1520 xAccessGranted = pdTRUE;
1524 else if( ( ulAccessRequested & tskMPU_WRITE_PERMISSION ) != 0UL ) /* W or RW. */
1526 if( ulMPURegionAccessPermissions == portMPU_REGION_PRIV_RW_UNPRIV_RW )
1528 xAccessGranted = pdTRUE;
1538 return xAccessGranted;
1541 #endif /* #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) ) */
1543 /*-----------------------------------------------------------*/
1545 BaseType_t xPortIsInsideInterrupt( void )
1547 uint32_t ulCurrentInterrupt;
1550 /* Obtain the number of the currently executing interrupt. Interrupt Program
1551 * Status Register (IPSR) holds the exception number of the currently-executing
1552 * exception or zero for Thread mode.*/
1553 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1555 if( ulCurrentInterrupt == 0 )
1567 /*-----------------------------------------------------------*/
1569 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1571 void vPortGrantAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1572 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1574 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1575 xMPU_SETTINGS * xTaskMpuSettings;
1577 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1578 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1580 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1582 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] |= ( 1U << ulAccessControlListEntryBit );
1585 #endif /* #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1587 /*-----------------------------------------------------------*/
1589 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
1591 void vPortRevokeAccessToKernelObject( TaskHandle_t xInternalTaskHandle,
1592 int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1594 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1595 xMPU_SETTINGS * xTaskMpuSettings;
1597 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1598 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1600 xTaskMpuSettings = xTaskGetMPUSettings( xInternalTaskHandle );
1602 xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] &= ~( 1U << ulAccessControlListEntryBit );
1605 #endif /* #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
1607 /*-----------------------------------------------------------*/
1609 #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
1611 #if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
1613 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1615 uint32_t ulAccessControlListEntryIndex, ulAccessControlListEntryBit;
1616 BaseType_t xAccessGranted = pdFALSE;
1617 const xMPU_SETTINGS * xTaskMpuSettings;
1619 if( xSchedulerRunning == pdFALSE )
1621 /* Grant access to all the kernel objects before the scheduler
1622 * is started. It is necessary because there is no task running
1623 * yet and therefore, we cannot use the permissions of any
1625 xAccessGranted = pdTRUE;
1629 xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1631 ulAccessControlListEntryIndex = ( ( uint32_t ) lInternalIndexOfKernelObject / portACL_ENTRY_SIZE_BITS );
1632 ulAccessControlListEntryBit = ( ( uint32_t ) lInternalIndexOfKernelObject % portACL_ENTRY_SIZE_BITS );
1634 if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1636 xAccessGranted = pdTRUE;
1640 if( ( xTaskMpuSettings->ulAccessControlList[ ulAccessControlListEntryIndex ] & ( 1U << ulAccessControlListEntryBit ) ) != 0 )
1642 xAccessGranted = pdTRUE;
1647 return xAccessGranted;
1650 #else /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1652 BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) /* PRIVILEGED_FUNCTION */
1654 ( void ) lInternalIndexOfKernelObject;
1656 /* If Access Control List feature is not used, all the tasks have
1657 * access to all the kernel objects. */
1661 #endif /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
1663 #endif /* #if ( ( configENABLE_MPU == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) ) */
1665 /*-----------------------------------------------------------*/