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