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
39 /*-----------------------------------------------------------
40 * Port specific definitions.
42 * The settings in this file configure FreeRTOS correctly for the
43 * given hardware and compiler.
45 * These settings should not be altered.
46 *-----------------------------------------------------------
49 /* Type definitions. */
51 #define portFLOAT float
52 #define portDOUBLE double
54 #define portSHORT short
55 #define portSTACK_TYPE uint32_t
56 #define portBASE_TYPE long
58 typedef portSTACK_TYPE StackType_t;
59 typedef long BaseType_t;
60 typedef unsigned long UBaseType_t;
62 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
63 typedef uint16_t TickType_t;
64 #define portMAX_DELAY ( TickType_t ) 0xffff
65 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
66 typedef uint32_t TickType_t;
67 #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
69 /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
70 * not need to be guarded with a critical section. */
71 #define portTICK_TYPE_IS_ATOMIC 1
73 #error "configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width."
76 /* Errata 837070 workaround must be enabled on Cortex-M7 r0p0
78 #ifndef configENABLE_ERRATA_837070_WORKAROUND
79 #define configENABLE_ERRATA_837070_WORKAROUND 0
81 /*-----------------------------------------------------------*/
83 /* MPU specific constants. */
84 #define portUSING_MPU_WRAPPERS 1
85 #define portPRIVILEGE_BIT ( 0x80000000UL )
87 #define portMPU_REGION_READ_WRITE ( 0x03UL << 24UL )
88 #define portMPU_REGION_PRIVILEGED_READ_ONLY ( 0x05UL << 24UL )
89 #define portMPU_REGION_READ_ONLY ( 0x06UL << 24UL )
90 #define portMPU_REGION_PRIVILEGED_READ_WRITE ( 0x01UL << 24UL )
91 #define portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ( 0x02UL << 24UL )
92 #define portMPU_REGION_CACHEABLE_BUFFERABLE ( 0x07UL << 16UL )
93 #define portMPU_REGION_EXECUTE_NEVER ( 0x01UL << 28UL )
95 /* Location of the TEX,S,C,B bits in the MPU Region Attribute and Size
97 #define portMPU_RASR_TEX_S_C_B_LOCATION ( 16UL )
98 #define portMPU_RASR_TEX_S_C_B_MASK ( 0x3FUL )
100 /* MPU settings that can be overridden in FreeRTOSConfig.h. */
101 #ifndef configTOTAL_MPU_REGIONS
102 /* Define to 8 for backward compatibility. */
103 #define configTOTAL_MPU_REGIONS ( 8UL )
107 * The TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits define the
108 * memory type, and where necessary the cacheable and shareable properties
109 * of the memory region.
111 * The TEX, C, and B bits together indicate the memory type of the region,
113 * - For Normal memory, the cacheable properties of the region.
114 * - For Device memory, whether the region is shareable.
116 * For Normal memory regions, the S bit indicates whether the region is
117 * shareable. For Strongly-ordered and Device memory, the S bit is ignored.
119 * See the following two tables for setting TEX, S, C and B bits for
120 * unprivileged flash, privileged flash and privileged RAM regions.
122 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
123 | TEX | C | B | Memory type | Description or Normal region cacheability | Shareable? |
124 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
125 | 000 | 0 | 0 | Strongly-ordered | Strongly ordered | Shareable |
126 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
127 | 000 | 0 | 1 | Device | Shared device | Shareable |
128 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
129 | 000 | 1 | 0 | Normal | Outer and inner write-through; no write allocate | S bit |
130 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
131 | 000 | 1 | 1 | Normal | Outer and inner write-back; no write allocate | S bit |
132 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
133 | 001 | 0 | 0 | Normal | Outer and inner Non-cacheable | S bit |
134 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
135 | 001 | 0 | 1 | Reserved | Reserved | Reserved |
136 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
137 | 001 | 1 | 0 | IMPLEMENTATION DEFINED | IMPLEMENTATION DEFINED | IMPLEMENTATION DEFINED |
138 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
139 | 001 | 1 | 1 | Normal | Outer and inner write-back; write and read allocate | S bit |
140 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
141 | 010 | 0 | 0 | Device | Non-shared device | Not shareable |
142 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
143 | 010 | 0 | 1 | Reserved | Reserved | Reserved |
144 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
145 | 010 | 1 | X | Reserved | Reserved | Reserved |
146 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
147 | 011 | X | X | Reserved | Reserved | Reserved |
148 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
149 | 1BB | A | A | Normal | Cached memory, with AA and BB indicating the inner and | Reserved |
150 | | | | | outer cacheability rules that must be exported on the | |
151 | | | | | bus. See the table below for the cacheability policy | |
152 | | | | | encoding. memory, BB=Outer policy, AA=Inner policy. | |
153 +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
155 +-----------------------------------------+----------------------------------------+
156 | AA or BB subfield of {TEX,C,B} encoding | Cacheability policy |
157 +-----------------------------------------+----------------------------------------+
158 | 00 | Non-cacheable |
159 +-----------------------------------------+----------------------------------------+
160 | 01 | Write-back, write and read allocate |
161 +-----------------------------------------+----------------------------------------+
162 | 10 | Write-through, no write allocate |
163 +-----------------------------------------+----------------------------------------+
164 | 11 | Write-back, no write allocate |
165 +-----------------------------------------+----------------------------------------+
168 /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for Flash
170 #ifndef configTEX_S_C_B_FLASH
171 /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */
172 #define configTEX_S_C_B_FLASH ( 0x07UL )
175 /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for SRAM
177 #ifndef configTEX_S_C_B_SRAM
178 /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */
179 #define configTEX_S_C_B_SRAM ( 0x07UL )
182 #define portSTACK_REGION ( configTOTAL_MPU_REGIONS - 5UL )
183 #define portGENERAL_PERIPHERALS_REGION ( configTOTAL_MPU_REGIONS - 4UL )
184 #define portUNPRIVILEGED_FLASH_REGION ( configTOTAL_MPU_REGIONS - 3UL )
185 #define portPRIVILEGED_FLASH_REGION ( configTOTAL_MPU_REGIONS - 2UL )
186 #define portPRIVILEGED_RAM_REGION ( configTOTAL_MPU_REGIONS - 1UL )
187 #define portFIRST_CONFIGURABLE_REGION ( 0UL )
188 #define portLAST_CONFIGURABLE_REGION ( configTOTAL_MPU_REGIONS - 6UL )
189 #define portNUM_CONFIGURABLE_REGIONS ( configTOTAL_MPU_REGIONS - 5UL )
190 #define portTOTAL_NUM_REGIONS_IN_TCB ( portNUM_CONFIGURABLE_REGIONS + 1 ) /* Plus 1 to create space for the stack region. */
192 typedef struct MPU_REGION_REGISTERS
194 uint32_t ulRegionBaseAddress;
195 uint32_t ulRegionAttribute;
196 } xMPU_REGION_REGISTERS;
198 typedef struct MPU_REGION_SETTINGS
200 uint32_t ulRegionStartAddress;
201 uint32_t ulRegionEndAddress;
202 uint32_t ulRegionPermissions;
203 } xMPU_REGION_SETTINGS;
205 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
207 #ifndef configSYSTEM_CALL_STACK_SIZE
208 #error "configSYSTEM_CALL_STACK_SIZE must be defined to the desired size of the system call stack in words for using MPU wrappers v2."
211 typedef struct SYSTEM_CALL_STACK_INFO
213 uint32_t ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE ];
214 uint32_t * pulSystemCallStack;
215 uint32_t * pulTaskStack;
216 uint32_t ulLinkRegisterAtSystemCallEntry;
217 } xSYSTEM_CALL_STACK_INFO;
219 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
222 * +---------+---------------+-----------------+-----------------+-----+
223 * | s16-s31 | s0-s15, FPSCR | CONTROL, r4-r11 | PSP, r0-r3, r12 | |
224 * | | | EXC_RETURN | LR, PC, xPSR | |
225 * +---------+---------------+-----------------+-----------------+-----+
227 * <--------><---------------><----------------><----------------><---->
230 #define MAX_CONTEXT_SIZE ( 53 )
232 /* Size of an Access Control List (ACL) entry in bits. */
233 #define portACL_ENTRY_SIZE_BITS ( 32U )
235 /* Flags used for xMPU_SETTINGS.ulTaskFlags member. */
236 #define portSTACK_FRAME_HAS_PADDING_FLAG ( 1UL << 0UL )
237 #define portTASK_IS_PRIVILEGED_FLAG ( 1UL << 1UL )
239 typedef struct MPU_SETTINGS
241 xMPU_REGION_REGISTERS xRegion[ portTOTAL_NUM_REGIONS_IN_TCB ];
242 xMPU_REGION_SETTINGS xRegionSettings[ portTOTAL_NUM_REGIONS_IN_TCB ];
243 uint32_t ulContext[ MAX_CONTEXT_SIZE ];
244 uint32_t ulTaskFlags;
246 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
247 xSYSTEM_CALL_STACK_INFO xSystemCallStackInfo;
248 #if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
249 uint32_t ulAccessControlList[ ( configPROTECTED_KERNEL_OBJECT_POOL_SIZE / portACL_ENTRY_SIZE_BITS ) + 1 ];
254 /* Architecture specifics. */
255 #define portSTACK_GROWTH ( -1 )
256 #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
257 #define portBYTE_ALIGNMENT 8
259 /* Constants used with memory barrier intrinsics. */
260 #define portSY_FULL_READ_WRITE ( 15 )
262 /*-----------------------------------------------------------*/
264 /* SVC numbers for various services. */
265 #define portSVC_START_SCHEDULER 100
266 #define portSVC_YIELD 101
267 #define portSVC_RAISE_PRIVILEGE 102
268 #define portSVC_SYSTEM_CALL_EXIT 103
270 /* Scheduler utilities. */
272 #define portYIELD() __asm { SVC portSVC_YIELD }
273 #define portYIELD_WITHIN_API() \
275 /* Set a PendSV to request a context switch. */ \
276 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \
278 /* Barriers are normally not required but do ensure the code is completely \
279 * within the specified behaviour for the architecture. */ \
280 __dsb( portSY_FULL_READ_WRITE ); \
281 __isb( portSY_FULL_READ_WRITE ); \
283 /*-----------------------------------------------------------*/
285 #define portNVIC_INT_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
286 #define portNVIC_PENDSVSET_BIT ( 1UL << 28UL )
287 #define portEND_SWITCHING_ISR( xSwitchRequired ) \
290 if( xSwitchRequired ) \
292 traceISR_EXIT_TO_SCHEDULER(); \
293 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \
300 #define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )
301 /*-----------------------------------------------------------*/
303 /* Critical section management. */
304 extern void vPortEnterCritical( void );
305 extern void vPortExitCritical( void );
307 #define portDISABLE_INTERRUPTS() vPortRaiseBASEPRI()
308 #define portENABLE_INTERRUPTS() vPortSetBASEPRI( 0 )
309 #define portENTER_CRITICAL() vPortEnterCritical()
310 #define portEXIT_CRITICAL() vPortExitCritical()
311 #define portSET_INTERRUPT_MASK_FROM_ISR() ulPortRaiseBASEPRI()
312 #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x ) vPortSetBASEPRI( x )
314 /*-----------------------------------------------------------*/
316 /* Architecture specific optimisations. */
317 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
318 #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
321 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
323 /* Check the configuration. */
324 #if ( configMAX_PRIORITIES > 32 )
325 #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."
328 /* Store/clear the ready priorities in a bit map. */
329 #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
330 #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
332 /*-----------------------------------------------------------*/
334 #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) uxTopPriority = ( 31UL - ( uint32_t ) __clz( ( uxReadyPriorities ) ) )
336 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
337 /*-----------------------------------------------------------*/
339 /* Task function macros as described on the FreeRTOS.org WEB site. These are
340 * not necessary for to use this port. They are defined so the common demo files
341 * (which build with all the ports) will build. */
342 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void * pvParameters )
343 #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void * pvParameters )
344 /*-----------------------------------------------------------*/
346 #if ( configASSERT_DEFINED == 1 )
347 void vPortValidateInterruptPriority( void );
348 #define portASSERT_IF_INTERRUPT_PRIORITY_INVALID() vPortValidateInterruptPriority()
351 /* portNOP() is not required by this port. */
354 #define portINLINE __inline
356 #ifndef portFORCE_INLINE
357 #define portFORCE_INLINE __forceinline
359 /*-----------------------------------------------------------*/
361 extern BaseType_t xIsPrivileged( void );
362 extern void vResetPrivilege( void );
363 extern void vPortSwitchToUserMode( void );
366 * @brief Checks whether or not the processor is privileged.
368 * @return 1 if the processor is already privileged, 0 otherwise.
370 #define portIS_PRIVILEGED() xIsPrivileged()
373 * @brief Raise an SVC request to raise privilege.
375 #define portRAISE_PRIVILEGE() __asm { svc portSVC_RAISE_PRIVILEGE }
378 * @brief Lowers the privilege level by setting the bit 0 of the CONTROL
381 #define portRESET_PRIVILEGE() vResetPrivilege()
384 * @brief Make a task unprivileged.
386 * It must be called from privileged tasks only. Calling it from unprivileged
387 * task will result in a memory protection fault.
389 #define portSWITCH_TO_USER_MODE() vPortSwitchToUserMode()
390 /*-----------------------------------------------------------*/
392 extern BaseType_t xPortIsTaskPrivileged( void );
395 * @brief Checks whether or not the calling task is privileged.
397 * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
399 #define portIS_TASK_PRIVILEGED() xPortIsTaskPrivileged()
400 /*-----------------------------------------------------------*/
402 static portFORCE_INLINE void vPortSetBASEPRI( uint32_t ulBASEPRI )
406 /* Barrier instructions are not used as this function is only used to
407 * lower the BASEPRI value. */
409 msr basepri, ulBASEPRI
413 /*-----------------------------------------------------------*/
415 static portFORCE_INLINE void vPortRaiseBASEPRI( void )
417 uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
421 /* Set BASEPRI to the max syscall priority to effect a critical
424 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
427 msr basepri, ulNewBASEPRI
430 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
436 /*-----------------------------------------------------------*/
438 static portFORCE_INLINE void vPortClearBASEPRIFromISR( void )
442 /* Set BASEPRI to 0 so no interrupts are masked. This function is only
443 * used to lower the mask in an interrupt, so memory barriers are not
450 /*-----------------------------------------------------------*/
452 static portFORCE_INLINE uint32_t ulPortRaiseBASEPRI( void )
454 uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
458 /* Set BASEPRI to the max syscall priority to effect a critical
461 mrs ulReturn, basepri
462 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
465 msr basepri, ulNewBASEPRI
468 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
476 /*-----------------------------------------------------------*/
478 static portFORCE_INLINE BaseType_t xPortIsInsideInterrupt( void )
480 uint32_t ulCurrentInterrupt;
483 /* Obtain the number of the currently executing interrupt. */
486 mrs ulCurrentInterrupt, ipsr
489 if( ulCurrentInterrupt == 0 )
500 /*-----------------------------------------------------------*/
502 #ifndef configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY
503 #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"
504 #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY 0
506 /*-----------------------------------------------------------*/
514 #endif /* PORTMACRO_H */