]> begriffs open source - freertos/blob - portable/RVDS/ARM_CM4_MPU/portmacro.h
Normalize files with mixed line endings (introduced in commit 3a413d1)
[freertos] / portable / RVDS / ARM_CM4_MPU / portmacro.h
1 /*\r
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT\r
6  *\r
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
8  * this software and associated documentation files (the "Software"), to deal in\r
9  * the Software without restriction, including without limitation the rights to\r
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
11  * the Software, and to permit persons to whom the Software is furnished to do so,\r
12  * subject to the following conditions:\r
13  *\r
14  * The above copyright notice and this permission notice shall be included in all\r
15  * copies or substantial portions of the Software.\r
16  *\r
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\r
27  */\r
28 \r
29 \r
30 #ifndef PORTMACRO_H\r
31 #define PORTMACRO_H\r
32 \r
33 /* *INDENT-OFF* */\r
34 #ifdef __cplusplus\r
35     extern "C" {\r
36 #endif\r
37 /* *INDENT-ON* */\r
38 \r
39 /*-----------------------------------------------------------\r
40  * Port specific definitions.\r
41  *\r
42  * The settings in this file configure FreeRTOS correctly for the\r
43  * given hardware and compiler.\r
44  *\r
45  * These settings should not be altered.\r
46  *-----------------------------------------------------------\r
47  */\r
48 \r
49 /* Type definitions. */\r
50 #define portCHAR          char\r
51 #define portFLOAT         float\r
52 #define portDOUBLE        double\r
53 #define portLONG          long\r
54 #define portSHORT         short\r
55 #define portSTACK_TYPE    uint32_t\r
56 #define portBASE_TYPE     long\r
57 \r
58 typedef portSTACK_TYPE   StackType_t;\r
59 typedef long             BaseType_t;\r
60 typedef unsigned long    UBaseType_t;\r
61 \r
62 #if ( configUSE_16_BIT_TICKS == 1 )\r
63     typedef uint16_t     TickType_t;\r
64     #define portMAX_DELAY              ( TickType_t ) 0xffff\r
65 #else\r
66     typedef uint32_t     TickType_t;\r
67     #define portMAX_DELAY              ( TickType_t ) 0xffffffffUL\r
68 \r
69 /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do\r
70  * not need to be guarded with a critical section. */\r
71     #define portTICK_TYPE_IS_ATOMIC    1\r
72 #endif\r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* MPU specific constants. */\r
76 #define portUSING_MPU_WRAPPERS                                   1\r
77 #define portPRIVILEGE_BIT                                        ( 0x80000000UL )\r
78 \r
79 #define portMPU_REGION_READ_WRITE                                ( 0x03UL << 24UL )\r
80 #define portMPU_REGION_PRIVILEGED_READ_ONLY                      ( 0x05UL << 24UL )\r
81 #define portMPU_REGION_READ_ONLY                                 ( 0x06UL << 24UL )\r
82 #define portMPU_REGION_PRIVILEGED_READ_WRITE                     ( 0x01UL << 24UL )\r
83 #define portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY    ( 0x02UL << 24UL )\r
84 #define portMPU_REGION_CACHEABLE_BUFFERABLE                      ( 0x07UL << 16UL )\r
85 #define portMPU_REGION_EXECUTE_NEVER                             ( 0x01UL << 28UL )\r
86 \r
87 /* Location of the TEX,S,C,B bits in the MPU Region Attribute and Size\r
88  * Register (RASR). */\r
89 #define portMPU_RASR_TEX_S_C_B_LOCATION                          ( 16UL )\r
90 #define portMPU_RASR_TEX_S_C_B_MASK                              ( 0x3FUL )\r
91 \r
92 /* MPU settings that can be overriden in FreeRTOSConfig.h. */\r
93 #ifndef configTOTAL_MPU_REGIONS\r
94     /* Define to 8 for backward compatibility. */\r
95     #define configTOTAL_MPU_REGIONS    ( 8UL )\r
96 #endif\r
97 \r
98 /*\r
99  * The TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits define the\r
100  * memory type, and where necessary the cacheable and shareable properties\r
101  * of the memory region.\r
102  *\r
103  * The TEX, C, and B bits together indicate the memory type of the region,\r
104  * and:\r
105  * - For Normal memory, the cacheable properties of the region.\r
106  * - For Device memory, whether the region is shareable.\r
107  *\r
108  * For Normal memory regions, the S bit indicates whether the region is\r
109  * shareable. For Strongly-ordered and Device memory, the S bit is ignored.\r
110  *\r
111  * See the following two tables for setting TEX, S, C and B bits for\r
112  * unprivileged flash, privileged flash and privileged RAM regions.\r
113  *\r
114  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
115  | TEX | C | B | Memory type            |  Description or Normal region cacheability             |  Shareable?             |\r
116  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
117  | 000 | 0 | 0 | Strongly-ordered       |  Strongly ordered                                      |  Shareable              |\r
118  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
119  | 000 | 0 | 1 | Device                 |  Shared device                                         |  Shareable              |\r
120  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
121  | 000 | 1 | 0 | Normal                 |  Outer and inner   write-through; no write allocate    |  S bit                  |\r
122  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
123  | 000 | 1 | 1 | Normal                 |  Outer and inner   write-back; no write allocate       |  S bit                  |\r
124  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
125  | 001 | 0 | 0 | Normal                 |  Outer and inner   Non-cacheable                       |  S bit                  |\r
126  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
127  | 001 | 0 | 1 | Reserved               |  Reserved                                              |  Reserved               |\r
128  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
129  | 001 | 1 | 0 | IMPLEMENTATION DEFINED |  IMPLEMENTATION DEFINED                                |  IMPLEMENTATION DEFINED |\r
130  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
131  | 001 | 1 | 1 | Normal                 |  Outer and inner   write-back; write and read allocate |  S bit                  |\r
132  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
133  | 010 | 0 | 0 | Device                 |  Non-shared device                                     |  Not shareable          |\r
134  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
135  | 010 | 0 | 1 | Reserved               |  Reserved                                              |  Reserved               |\r
136  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
137  | 010 | 1 | X | Reserved               |  Reserved                                              |  Reserved               |\r
138  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
139  | 011 | X | X | Reserved               |  Reserved                                              |  Reserved               |\r
140  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
141  | 1BB | A | A | Normal                 | Cached memory, with AA and BB indicating the inner and |  Reserved               |\r
142  |     |   |   |                        | outer cacheability rules that must be exported on the  |                         |\r
143  |     |   |   |                        | bus. See the table below for the cacheability policy   |                         |\r
144  |     |   |   |                        | encoding. memory, BB=Outer policy, AA=Inner policy.    |                         |\r
145  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+\r
146  |\r
147  +-----------------------------------------+----------------------------------------+\r
148  | AA or BB subfield of {TEX,C,B} encoding |  Cacheability policy                   |\r
149  +-----------------------------------------+----------------------------------------+\r
150  | 00                                      |  Non-cacheable                         |\r
151  +-----------------------------------------+----------------------------------------+\r
152  | 01                                      |  Write-back, write and   read allocate |\r
153  +-----------------------------------------+----------------------------------------+\r
154  | 10                                      |  Write-through, no write   allocate    |\r
155  +-----------------------------------------+----------------------------------------+\r
156  | 11                                      |  Write-back, no write   allocate       |\r
157  +-----------------------------------------+----------------------------------------+\r
158  */\r
159 \r
160 /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for Flash\r
161  * region. */\r
162 #ifndef configTEX_S_C_B_FLASH\r
163     /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */\r
164     #define configTEX_S_C_B_FLASH    ( 0x07UL )\r
165 #endif\r
166 \r
167 /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for SRAM\r
168  * region. */\r
169 #ifndef configTEX_S_C_B_SRAM\r
170     /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */\r
171     #define configTEX_S_C_B_SRAM          ( 0x07UL )\r
172 #endif\r
173 \r
174 #define portUNPRIVILEGED_FLASH_REGION     ( 0UL )\r
175 #define portPRIVILEGED_FLASH_REGION       ( 1UL )\r
176 #define portPRIVILEGED_RAM_REGION         ( 2UL )\r
177 #define portGENERAL_PERIPHERALS_REGION    ( 3UL )\r
178 #define portSTACK_REGION                  ( 4UL )\r
179 #define portFIRST_CONFIGURABLE_REGION     ( 5UL )\r
180 #define portTOTAL_NUM_REGIONS             ( configTOTAL_MPU_REGIONS )\r
181 #define portNUM_CONFIGURABLE_REGIONS      ( portTOTAL_NUM_REGIONS - portFIRST_CONFIGURABLE_REGION )\r
182 #define portLAST_CONFIGURABLE_REGION      ( portTOTAL_NUM_REGIONS - 1 )\r
183 \r
184 void vPortSwitchToUserMode( void );\r
185 #define portSWITCH_TO_USER_MODE()    vPortSwitchToUserMode()\r
186 \r
187 typedef struct MPU_REGION_REGISTERS\r
188 {\r
189     uint32_t ulRegionBaseAddress;\r
190     uint32_t ulRegionAttribute;\r
191 } xMPU_REGION_REGISTERS;\r
192 \r
193 /* Plus 1 to create space for the stack region. */\r
194 typedef struct MPU_SETTINGS\r
195 {\r
196     xMPU_REGION_REGISTERS xRegion[ portTOTAL_NUM_REGIONS ];\r
197 } xMPU_SETTINGS;\r
198 \r
199 /* Architecture specifics. */\r
200 #define portSTACK_GROWTH          ( -1 )\r
201 #define portTICK_PERIOD_MS        ( ( TickType_t ) 1000 / configTICK_RATE_HZ )\r
202 #define portBYTE_ALIGNMENT        8\r
203 \r
204 /* Constants used with memory barrier intrinsics. */\r
205 #define portSY_FULL_READ_WRITE    ( 15 )\r
206 \r
207 /*-----------------------------------------------------------*/\r
208 \r
209 /* SVC numbers for various services. */\r
210 #define portSVC_START_SCHEDULER    0\r
211 #define portSVC_YIELD              1\r
212 #define portSVC_RAISE_PRIVILEGE    2\r
213 \r
214 /* Scheduler utilities. */\r
215 \r
216 #define portYIELD()    __asm{ SVC portSVC_YIELD }\r
217 #define portYIELD_WITHIN_API()                          \\r
218     {                                                   \\r
219         /* Set a PendSV to request a context switch. */ \\r
220         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \\r
221                                                         \\r
222         /* Barriers are normally not required but do ensure the code is completely \\r
223          * within the specified behaviour for the architecture. */ \\r
224         __dsb( portSY_FULL_READ_WRITE );                           \\r
225         __isb( portSY_FULL_READ_WRITE );                           \\r
226     }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 #define portNVIC_INT_CTRL_REG     ( *( ( volatile uint32_t * ) 0xe000ed04 ) )\r
230 #define portNVIC_PENDSVSET_BIT    ( 1UL << 28UL )\r
231 #define portEND_SWITCHING_ISR( xSwitchRequired )    do { if( xSwitchRequired ) portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; } while( 0 )\r
232 #define portYIELD_FROM_ISR( x )                     portEND_SWITCHING_ISR( x )\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 /* Critical section management. */\r
236 extern void vPortEnterCritical( void );\r
237 extern void vPortExitCritical( void );\r
238 \r
239 #define portDISABLE_INTERRUPTS()                  vPortRaiseBASEPRI()\r
240 #define portENABLE_INTERRUPTS()                   vPortSetBASEPRI( 0 )\r
241 #define portENTER_CRITICAL()                      vPortEnterCritical()\r
242 #define portEXIT_CRITICAL()                       vPortExitCritical()\r
243 #define portSET_INTERRUPT_MASK_FROM_ISR()         ulPortRaiseBASEPRI()\r
244 #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x )    vPortSetBASEPRI( x )\r
245 \r
246 /*-----------------------------------------------------------*/\r
247 \r
248 /* Architecture specific optimisations. */\r
249 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION\r
250     #define configUSE_PORT_OPTIMISED_TASK_SELECTION    1\r
251 #endif\r
252 \r
253 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
254 \r
255 /* Check the configuration. */\r
256     #if ( configMAX_PRIORITIES > 32 )\r
257         #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.\r
258     #endif\r
259 \r
260 /* Store/clear the ready priorities in a bit map. */\r
261     #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities )    ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )\r
262     #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities )     ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )\r
263 \r
264 /*-----------------------------------------------------------*/\r
265 \r
266     #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    uxTopPriority = ( 31UL - ( uint32_t ) __clz( ( uxReadyPriorities ) ) )\r
267 \r
268 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 /* Task function macros as described on the FreeRTOS.org WEB site.  These are\r
272  * not necessary for to use this port.  They are defined so the common demo files\r
273  * (which build with all the ports) will build. */\r
274 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters )    void vFunction( void * pvParameters )\r
275 #define portTASK_FUNCTION( vFunction, pvParameters )          void vFunction( void * pvParameters )\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 #ifdef configASSERT\r
279     void vPortValidateInterruptPriority( void );\r
280     #define portASSERT_IF_INTERRUPT_PRIORITY_INVALID()    vPortValidateInterruptPriority()\r
281 #endif\r
282 \r
283 /* portNOP() is not required by this port. */\r
284 #define portNOP()\r
285 \r
286 #define portINLINE              __inline\r
287 \r
288 #ifndef portFORCE_INLINE\r
289     #define portFORCE_INLINE    __forceinline\r
290 #endif\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 extern BaseType_t xIsPrivileged( void );\r
294 extern void vResetPrivilege( void );\r
295 \r
296 /**\r
297  * @brief Checks whether or not the processor is privileged.\r
298  *\r
299  * @return 1 if the processor is already privileged, 0 otherwise.\r
300  */\r
301 #define portIS_PRIVILEGED()      xIsPrivileged()\r
302 \r
303 /**\r
304  * @brief Raise an SVC request to raise privilege.\r
305  */\r
306 #define portRAISE_PRIVILEGE()    __asm { svc portSVC_RAISE_PRIVILEGE }\r
307 \r
308 /**\r
309  * @brief Lowers the privilege level by setting the bit 0 of the CONTROL\r
310  * register.\r
311  */\r
312 #define portRESET_PRIVILEGE()    vResetPrivilege()\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static portFORCE_INLINE void vPortSetBASEPRI( uint32_t ulBASEPRI )\r
316 {\r
317     __asm\r
318     {\r
319         /* Barrier instructions are not used as this function is only used to\r
320          * lower the BASEPRI value. */\r
321 /* *INDENT-OFF* */\r
322         msr basepri, ulBASEPRI\r
323 /* *INDENT-ON* */\r
324     }\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 static portFORCE_INLINE void vPortRaiseBASEPRI( void )\r
329 {\r
330     uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;\r
331 \r
332     __asm\r
333     {\r
334         /* Set BASEPRI to the max syscall priority to effect a critical\r
335          * section. */\r
336 /* *INDENT-OFF* */\r
337         msr basepri, ulNewBASEPRI\r
338         dsb\r
339         isb\r
340 /* *INDENT-ON* */\r
341     }\r
342 }\r
343 /*-----------------------------------------------------------*/\r
344 \r
345 static portFORCE_INLINE void vPortClearBASEPRIFromISR( void )\r
346 {\r
347     __asm\r
348     {\r
349         /* Set BASEPRI to 0 so no interrupts are masked.  This function is only\r
350          * used to lower the mask in an interrupt, so memory barriers are not\r
351          * used. */\r
352 /* *INDENT-OFF* */\r
353         msr basepri, # 0\r
354 /* *INDENT-ON* */\r
355     }\r
356 }\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 static portFORCE_INLINE uint32_t ulPortRaiseBASEPRI( void )\r
360 {\r
361     uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;\r
362 \r
363     __asm\r
364     {\r
365         /* Set BASEPRI to the max syscall priority to effect a critical\r
366          * section. */\r
367 /* *INDENT-OFF* */\r
368         mrs ulReturn, basepri\r
369         msr basepri, ulNewBASEPRI\r
370         dsb\r
371         isb\r
372 /* *INDENT-ON* */\r
373     }\r
374 \r
375     return ulReturn;\r
376 }\r
377 /*-----------------------------------------------------------*/\r
378 \r
379 static portFORCE_INLINE BaseType_t xPortIsInsideInterrupt( void )\r
380 {\r
381     uint32_t ulCurrentInterrupt;\r
382     BaseType_t xReturn;\r
383 \r
384     /* Obtain the number of the currently executing interrupt. */\r
385     __asm\r
386     {\r
387         mrs ulCurrentInterrupt, ipsr\r
388     }\r
389 \r
390     if( ulCurrentInterrupt == 0 )\r
391     {\r
392         xReturn = pdFALSE;\r
393     }\r
394     else\r
395     {\r
396         xReturn = pdTRUE;\r
397     }\r
398 \r
399     return xReturn;\r
400 }\r
401 /*-----------------------------------------------------------*/\r
402 \r
403 #ifndef configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY\r
404     #warning "configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY is not defined. We recommend defining it to 1 in FreeRTOSConfig.h for better security. https://www.FreeRTOS.org/FreeRTOS-V10.3.x.html"\r
405     #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY    0\r
406 #endif\r
407 /*-----------------------------------------------------------*/\r
408 \r
409 /* *INDENT-OFF* */\r
410 #ifdef __cplusplus\r
411     }\r
412 #endif\r
413 /* *INDENT-ON* */\r
414 \r
415 #endif /* PORTMACRO_H */\r