2 * FreeRTOS Kernel V11.2.0
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
40 /*-----------------------------------------------------------
41 * Port specific definitions.
43 * The settings in this file configure FreeRTOS correctly for the
44 * given hardware and compiler.
46 * These settings should not be altered.
47 *-----------------------------------------------------------
50 /* Type definitions. */
52 #define portFLOAT float
53 #define portDOUBLE double
55 #define portSHORT short
56 #define portSTACK_TYPE uint32_t
57 #define portBASE_TYPE long
59 typedef portSTACK_TYPE StackType_t;
60 typedef long BaseType_t;
61 typedef unsigned long UBaseType_t;
63 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
64 typedef uint16_t TickType_t;
65 #define portMAX_DELAY ( TickType_t ) 0xffff
66 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
67 typedef uint32_t TickType_t;
68 #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
70 /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
71 * not need to be guarded with a critical section. */
72 #define portTICK_TYPE_IS_ATOMIC 1
74 #error "configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width."
77 /* Errata 837070 workaround must be enabled on Cortex-M7 r0p0
79 #ifndef configENABLE_ERRATA_837070_WORKAROUND
80 #define configENABLE_ERRATA_837070_WORKAROUND 0
82 /*-----------------------------------------------------------*/
84 /* MPU specific constants. */
85 #define portUSING_MPU_WRAPPERS 1
86 #define portPRIVILEGE_BIT ( 0x80000000UL )
88 #define portMPU_REGION_READ_WRITE ( 0x03UL << 24UL )
89 #define portMPU_REGION_PRIVILEGED_READ_ONLY ( 0x05UL << 24UL )
90 #define portMPU_REGION_READ_ONLY ( 0x06UL << 24UL )
91 #define portMPU_REGION_PRIVILEGED_READ_WRITE ( 0x01UL << 24UL )
92 #define portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ( 0x02UL << 24UL )
93 #define portMPU_REGION_CACHEABLE_BUFFERABLE ( 0x07UL << 16UL )
94 #define portMPU_REGION_EXECUTE_NEVER ( 0x01UL << 28UL )
96 /* Location of the TEX,S,C,B bits in the MPU Region Attribute and Size
98 #define portMPU_RASR_TEX_S_C_B_LOCATION ( 16UL )
99 #define portMPU_RASR_TEX_S_C_B_MASK ( 0x3FUL )
101 /* MPU settings that can be overridden in FreeRTOSConfig.h. */
102 #ifndef configTOTAL_MPU_REGIONS
103 /* Define to 8 for backward compatibility. */
104 #define configTOTAL_MPU_REGIONS ( 8UL )
108 * The TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits define the
109 * memory type, and where necessary the cacheable and shareable properties
110 * of the memory region.
112 * The TEX, C, and B bits together indicate the memory type of the region,
114 * - For Normal memory, the cacheable properties of the region.
115 * - For Device memory, whether the region is shareable.
117 * For Normal memory regions, the S bit indicates whether the region is
118 * shareable. For Strongly-ordered and Device memory, the S bit is ignored.
120 * See the following two tables for setting TEX, S, C and B bits for
121 * unprivileged flash, privileged flash and privileged RAM regions.
123 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
124 | TEX | C | B | Memory type | Description or Normal region cacheability | Shareable? |
125 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
126 | 000 | 0 | 0 | Strongly-ordered | Strongly ordered | Shareable |
127 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
128 | 000 | 0 | 1 | Device | Shared device | Shareable |
129 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
130 | 000 | 1 | 0 | Normal | Outer and inner write-through; no write allocate | S bit |
131 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
132 | 000 | 1 | 1 | Normal | Outer and inner write-back; no write allocate | S bit |
133 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
134 | 001 | 0 | 0 | Normal | Outer and inner Non-cacheable | S bit |
135 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
136 | 001 | 0 | 1 | Reserved | Reserved | Reserved |
137 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
138 | 001 | 1 | 0 | IMPLEMENTATION DEFINED | IMPLEMENTATION DEFINED | IMPLEMENTATION DEFINED |
139 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
140 | 001 | 1 | 1 | Normal | Outer and inner write-back; write and read allocate | S bit |
141 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
142 | 010 | 0 | 0 | Device | Non-shared device | Not shareable |
143 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
144 | 010 | 0 | 1 | Reserved | Reserved | Reserved |
145 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
146 | 010 | 1 | X | Reserved | Reserved | Reserved |
147 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
148 | 011 | X | X | Reserved | Reserved | Reserved |
149 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
150 | 1BB | A | A | Normal | Cached memory, with AA and BB indicating the inner and | Reserved |
151 | | | | | outer cacheability rules that must be exported on the | |
152 | | | | | bus. See the table below for the cacheability policy | |
153 | | | | | encoding. memory, BB=Outer policy, AA=Inner policy. | |
154 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
156 +-----------------------------------------+----------------------------------------+
157 | AA or BB subfield of {TEX,C,B} encoding | Cacheability policy |
158 +-----------------------------------------+----------------------------------------+
159 | 00 | Non-cacheable |
160 +-----------------------------------------+----------------------------------------+
161 | 01 | Write-back, write and read allocate |
162 +-----------------------------------------+----------------------------------------+
163 | 10 | Write-through, no write allocate |
164 +-----------------------------------------+----------------------------------------+
165 | 11 | Write-back, no write allocate |
166 +-----------------------------------------+----------------------------------------+
169 /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for flash
171 #ifndef configTEX_S_C_B_FLASH
172 /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */
173 #define configTEX_S_C_B_FLASH ( 0x07UL )
176 /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for RAM
178 #ifndef configTEX_S_C_B_SRAM
179 /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */
180 #define configTEX_S_C_B_SRAM ( 0x07UL )
183 #define portSTACK_REGION ( configTOTAL_MPU_REGIONS - 5UL )
184 #define portGENERAL_PERIPHERALS_REGION ( configTOTAL_MPU_REGIONS - 4UL )
185 #define portUNPRIVILEGED_FLASH_REGION ( configTOTAL_MPU_REGIONS - 3UL )
186 #define portPRIVILEGED_FLASH_REGION ( configTOTAL_MPU_REGIONS - 2UL )
187 #define portPRIVILEGED_RAM_REGION ( configTOTAL_MPU_REGIONS - 1UL )
188 #define portFIRST_CONFIGURABLE_REGION ( 0UL )
189 #define portLAST_CONFIGURABLE_REGION ( configTOTAL_MPU_REGIONS - 6UL )
190 #define portNUM_CONFIGURABLE_REGIONS ( configTOTAL_MPU_REGIONS - 5UL )
191 #define portTOTAL_NUM_REGIONS_IN_TCB ( portNUM_CONFIGURABLE_REGIONS + 1 ) /* Plus 1 to create space for the stack region. */
193 typedef struct MPU_REGION_REGISTERS
195 uint32_t ulRegionBaseAddress;
196 uint32_t ulRegionAttribute;
197 } xMPU_REGION_REGISTERS;
199 typedef struct MPU_REGION_SETTINGS
201 uint32_t ulRegionStartAddress;
202 uint32_t ulRegionEndAddress;
203 uint32_t ulRegionPermissions;
204 } xMPU_REGION_SETTINGS;
206 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
208 #ifndef configSYSTEM_CALL_STACK_SIZE
209 #error "configSYSTEM_CALL_STACK_SIZE must be defined to the desired size of the system call stack in words for using MPU wrappers v2."
212 typedef struct SYSTEM_CALL_STACK_INFO
214 uint32_t ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE ];
215 uint32_t * pulSystemCallStack;
216 uint32_t * pulTaskStack;
217 uint32_t ulLinkRegisterAtSystemCallEntry;
218 } xSYSTEM_CALL_STACK_INFO;
220 #endif /* configUSE_MPU_WRAPPERS_V1 == 0 */
223 * +---------+---------------+-----------------+-----------------+-----+
224 * | s16-s31 | s0-s15, FPSCR | CONTROL, r4-r11 | PSP, r0-r3, r12 | |
225 * | | | EXC_RETURN | LR, PC, xPSR | |
226 * +---------+---------------+-----------------+-----------------+-----+
228 * <--------><---------------><----------------><----------------><---->
231 #define MAX_CONTEXT_SIZE ( 53 )
233 /* Size of an Access Control List (ACL) entry in bits. */
234 #define portACL_ENTRY_SIZE_BITS ( 32U )
236 /* Flags used for xMPU_SETTINGS.ulTaskFlags member. */
237 #define portSTACK_FRAME_HAS_PADDING_FLAG ( 1UL << 0UL )
238 #define portTASK_IS_PRIVILEGED_FLAG ( 1UL << 1UL )
240 typedef struct MPU_SETTINGS
242 xMPU_REGION_REGISTERS xRegion[ portTOTAL_NUM_REGIONS_IN_TCB ];
243 xMPU_REGION_SETTINGS xRegionSettings[ portTOTAL_NUM_REGIONS_IN_TCB ];
244 uint32_t ulContext[ MAX_CONTEXT_SIZE ];
245 uint32_t ulTaskFlags;
247 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
248 xSYSTEM_CALL_STACK_INFO xSystemCallStackInfo;
249 #if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
250 uint32_t ulAccessControlList[ ( configPROTECTED_KERNEL_OBJECT_POOL_SIZE / portACL_ENTRY_SIZE_BITS ) + 1 ];
255 /* Architecture specifics. */
256 #define portSTACK_GROWTH ( -1 )
257 #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
258 #define portBYTE_ALIGNMENT 8
259 #define portDONT_DISCARD __attribute__( ( used ) )
260 /*-----------------------------------------------------------*/
262 /* SVC numbers for various services. */
263 #define portSVC_START_SCHEDULER 100
264 #define portSVC_YIELD 101
265 #define portSVC_RAISE_PRIVILEGE 102
266 #define portSVC_SYSTEM_CALL_EXIT 103
268 /* Scheduler utilities. */
270 #define portYIELD() __asm volatile ( " SVC %0 \n" ::"i" ( portSVC_YIELD ) : "memory" )
271 #define portYIELD_WITHIN_API() \
273 /* Set a PendSV to request a context switch. */ \
274 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \
276 /* Barriers are normally not required but do ensure the code is completely \
277 * within the specified behaviour for the architecture. */ \
278 __asm volatile ( "dsb" ::: "memory" ); \
279 __asm volatile ( "isb" ); \
282 #define portNVIC_INT_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
283 #define portNVIC_PENDSVSET_BIT ( 1UL << 28UL )
284 #define portEND_SWITCHING_ISR( xSwitchRequired ) \
287 if( xSwitchRequired ) \
289 traceISR_EXIT_TO_SCHEDULER(); \
290 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \
297 #define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )
298 /*-----------------------------------------------------------*/
300 /* Critical section management. */
301 extern void vPortEnterCritical( void );
302 extern void vPortExitCritical( void );
303 #define portSET_INTERRUPT_MASK_FROM_ISR() ulPortRaiseBASEPRI()
304 #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x ) vPortSetBASEPRI( x )
305 #define portDISABLE_INTERRUPTS() vPortRaiseBASEPRI()
306 #define portENABLE_INTERRUPTS() vPortSetBASEPRI( 0 )
307 #define portENTER_CRITICAL() vPortEnterCritical()
308 #define portEXIT_CRITICAL() vPortExitCritical()
310 /*-----------------------------------------------------------*/
312 /* Task function macros as described on the FreeRTOS.org WEB site. These are
313 * not necessary for to use this port. They are defined so the common demo files
314 * (which build with all the ports) will build. */
315 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void * pvParameters )
316 #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void * pvParameters )
317 /*-----------------------------------------------------------*/
319 /* Architecture specific optimisations. */
320 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
321 #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
324 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
326 /* Generic helper function. */
327 __attribute__( ( always_inline ) ) static inline uint8_t ucPortCountLeadingZeros( uint32_t ulBitmap )
331 __asm volatile ( "clz %0, %1" : "=r" ( ucReturn ) : "r" ( ulBitmap ) : "memory" );
336 /* Check the configuration. */
337 #if ( configMAX_PRIORITIES > 32 )
338 #error "configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32. It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice."
341 /* Store/clear the ready priorities in a bit map. */
342 #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
343 #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
345 /*-----------------------------------------------------------*/
347 #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) uxTopPriority = ( 31UL - ( uint32_t ) ucPortCountLeadingZeros( ( uxReadyPriorities ) ) )
349 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
351 /*-----------------------------------------------------------*/
353 #if ( configASSERT_DEFINED == 1 )
354 void vPortValidateInterruptPriority( void );
355 #define portASSERT_IF_INTERRUPT_PRIORITY_INVALID() vPortValidateInterruptPriority()
358 /* portNOP() is not required by this port. */
361 #define portINLINE __inline
363 #ifndef portFORCE_INLINE
364 #define portFORCE_INLINE inline __attribute__( ( always_inline ) )
366 /*-----------------------------------------------------------*/
368 extern BaseType_t xIsPrivileged( void );
369 extern void vResetPrivilege( void );
370 extern void vPortSwitchToUserMode( void );
373 * @brief Checks whether or not the processor is privileged.
375 * @return 1 if the processor is already privileged, 0 otherwise.
377 #define portIS_PRIVILEGED() xIsPrivileged()
380 * @brief Raise an SVC request to raise privilege.
382 #define portRAISE_PRIVILEGE() __asm volatile ( "svc %0 \n" ::"i" ( portSVC_RAISE_PRIVILEGE ) : "memory" );
385 * @brief Lowers the privilege level by setting the bit 0 of the CONTROL
388 #define portRESET_PRIVILEGE() vResetPrivilege()
391 * @brief Make a task unprivileged.
393 * It must be called from privileged tasks only. Calling it from unprivileged
394 * task will result in a memory protection fault.
396 #define portSWITCH_TO_USER_MODE() vPortSwitchToUserMode()
397 /*-----------------------------------------------------------*/
399 extern BaseType_t xPortIsTaskPrivileged( void );
402 * @brief Checks whether or not the calling task is privileged.
404 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
406 #define portIS_TASK_PRIVILEGED() xPortIsTaskPrivileged()
407 /*-----------------------------------------------------------*/
409 portFORCE_INLINE static BaseType_t xPortIsInsideInterrupt( void )
411 uint32_t ulCurrentInterrupt;
414 /* Obtain the number of the currently executing interrupt. */
415 __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
417 if( ulCurrentInterrupt == 0 )
429 /*-----------------------------------------------------------*/
431 portFORCE_INLINE static void vPortRaiseBASEPRI( void )
433 uint32_t ulNewBASEPRI;
438 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
439 " cpsid i \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
441 " msr basepri, %0 \n"
444 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
445 " cpsie i \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
447 : "=r" ( ulNewBASEPRI ) : "i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory"
451 /*-----------------------------------------------------------*/
453 portFORCE_INLINE static uint32_t ulPortRaiseBASEPRI( void )
455 uint32_t ulOriginalBASEPRI, ulNewBASEPRI;
459 " mrs %0, basepri \n"
461 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
462 " cpsid i \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
464 " msr basepri, %1 \n"
467 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
468 " cpsie i \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
470 : "=r" ( ulOriginalBASEPRI ), "=r" ( ulNewBASEPRI ) : "i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory"
473 /* This return will not be reached but is necessary to prevent compiler
475 return ulOriginalBASEPRI;
477 /*-----------------------------------------------------------*/
479 portFORCE_INLINE static void vPortSetBASEPRI( uint32_t ulNewMaskValue )
483 " msr basepri, %0 " ::"r" ( ulNewMaskValue ) : "memory"
486 /*-----------------------------------------------------------*/
488 #define portMEMORY_BARRIER() __asm volatile ( "" ::: "memory" )
490 #ifndef configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY
491 #warning "configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY is not defined. We recommend defining it to 1 in FreeRTOSConfig.h for better security. www.FreeRTOS.org/FreeRTOS-V10.3.x.html"
492 #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY 0
494 /*-----------------------------------------------------------*/
502 #endif /* PORTMACRO_H */