1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
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.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28 
29 
30 #ifndef PORTMACRO_H
31 #define PORTMACRO_H
32 
33 /* *INDENT-OFF* */
34 #ifdef __cplusplus
35     extern "C" {
36 #endif
37 /* *INDENT-ON* */
38 
39 /*-----------------------------------------------------------
40  * Port specific definitions.
41  *
42  * The settings in this file configure FreeRTOS correctly for the
43  * given hardware and compiler.
44  *
45  * These settings should not be altered.
46  *-----------------------------------------------------------
47  */
48 
49 /* IAR includes. */
50 #include <intrinsics.h>
51 
52 /* Type definitions. */
53 #define portCHAR          char
54 #define portFLOAT         float
55 #define portDOUBLE        double
56 #define portLONG          long
57 #define portSHORT         short
58 #define portSTACK_TYPE    uint32_t
59 #define portBASE_TYPE     long
60 
61 typedef portSTACK_TYPE   StackType_t;
62 typedef long             BaseType_t;
63 typedef unsigned long    UBaseType_t;
64 
65 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
66     typedef uint16_t     TickType_t;
67     #define portMAX_DELAY              ( TickType_t ) 0xffff
68 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
69     typedef uint32_t     TickType_t;
70     #define portMAX_DELAY              ( TickType_t ) 0xffffffffUL
71 
72 /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
73  * not need to be guarded with a critical section. */
74     #define portTICK_TYPE_IS_ATOMIC    1
75 #else
76     #error "configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width."
77 #endif
78 
79 /* Errata 837070 workaround must be enabled on Cortex-M7 r0p0
80  * and r0p1 cores. */
81 #ifndef configENABLE_ERRATA_837070_WORKAROUND
82     #define configENABLE_ERRATA_837070_WORKAROUND    0
83 #endif
84 /*-----------------------------------------------------------*/
85 
86 /* MPU specific constants. */
87 #define portUSING_MPU_WRAPPERS                                   1
88 #define portPRIVILEGE_BIT                                        ( 0x80000000UL )
89 
90 #define portMPU_REGION_READ_WRITE                                ( 0x03UL << 24UL )
91 #define portMPU_REGION_PRIVILEGED_READ_ONLY                      ( 0x05UL << 24UL )
92 #define portMPU_REGION_READ_ONLY                                 ( 0x06UL << 24UL )
93 #define portMPU_REGION_PRIVILEGED_READ_WRITE                     ( 0x01UL << 24UL )
94 #define portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY    ( 0x02UL << 24UL )
95 #define portMPU_REGION_CACHEABLE_BUFFERABLE                      ( 0x07UL << 16UL )
96 #define portMPU_REGION_EXECUTE_NEVER                             ( 0x01UL << 28UL )
97 
98 /* Location of the TEX,S,C,B bits in the MPU Region Attribute and Size
99  * Register (RASR). */
100 #define portMPU_RASR_TEX_S_C_B_LOCATION                          ( 16UL )
101 #define portMPU_RASR_TEX_S_C_B_MASK                              ( 0x3FUL )
102 
103 /* MPU settings that can be overridden in FreeRTOSConfig.h. */
104 #ifndef configTOTAL_MPU_REGIONS
105     /* Define to 8 for backward compatibility. */
106     #define configTOTAL_MPU_REGIONS    ( 8UL )
107 #endif
108 
109 /*
110  * The TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits define the
111  * memory type, and where necessary the cacheable and shareable properties
112  * of the memory region.
113  *
114  * The TEX, C, and B bits together indicate the memory type of the region,
115  * and:
116  * - For Normal memory, the cacheable properties of the region.
117  * - For Device memory, whether the region is shareable.
118  *
119  * For Normal memory regions, the S bit indicates whether the region is
120  * shareable. For Strongly-ordered and Device memory, the S bit is ignored.
121  *
122  * See the following two tables for setting TEX, S, C and B bits for
123  * unprivileged flash, privileged flash and privileged RAM regions.
124  *
125  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
126  | TEX | C | B | Memory type            |  Description or Normal region cacheability             |  Shareable?             |
127  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
128  | 000 | 0 | 0 | Strongly-ordered       |  Strongly ordered                                      |  Shareable              |
129  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
130  | 000 | 0 | 1 | Device                 |  Shared device                                         |  Shareable              |
131  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
132  | 000 | 1 | 0 | Normal                 |  Outer and inner   write-through; no write allocate    |  S bit                  |
133  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
134  | 000 | 1 | 1 | Normal                 |  Outer and inner   write-back; no write allocate       |  S bit                  |
135  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
136  | 001 | 0 | 0 | Normal                 |  Outer and inner   Non-cacheable                       |  S bit                  |
137  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
138  | 001 | 0 | 1 | Reserved               |  Reserved                                              |  Reserved               |
139  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
140  | 001 | 1 | 0 | IMPLEMENTATION DEFINED |  IMPLEMENTATION DEFINED                                |  IMPLEMENTATION DEFINED |
141  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
142  | 001 | 1 | 1 | Normal                 |  Outer and inner   write-back; write and read allocate |  S bit                  |
143  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
144  | 010 | 0 | 0 | Device                 |  Non-shared device                                     |  Not shareable          |
145  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
146  | 010 | 0 | 1 | Reserved               |  Reserved                                              |  Reserved               |
147  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
148  | 010 | 1 | X | Reserved               |  Reserved                                              |  Reserved               |
149  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
150  | 011 | X | X | Reserved               |  Reserved                                              |  Reserved               |
151  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
152  | 1BB | A | A | Normal                 | Cached memory, with AA and BB indicating the inner and |  Reserved               |
153  |     |   |   |                        | outer cacheability rules that must be exported on the  |                         |
154  |     |   |   |                        | bus. See the table below for the cacheability policy   |                         |
155  |     |   |   |                        | encoding. memory, BB=Outer policy, AA=Inner policy.    |                         |
156  +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
157  |
158  +-----------------------------------------+----------------------------------------+
159  | AA or BB subfield of {TEX,C,B} encoding |  Cacheability policy                   |
160  +-----------------------------------------+----------------------------------------+
161  | 00                                      |  Non-cacheable                         |
162  +-----------------------------------------+----------------------------------------+
163  | 01                                      |  Write-back, write and   read allocate |
164  +-----------------------------------------+----------------------------------------+
165  | 10                                      |  Write-through, no write   allocate    |
166  +-----------------------------------------+----------------------------------------+
167  | 11                                      |  Write-back, no write   allocate       |
168  +-----------------------------------------+----------------------------------------+
169  */
170 
171 /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for flash
172  * region. */
173 #ifndef configTEX_S_C_B_FLASH
174     /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */
175     #define configTEX_S_C_B_FLASH    ( 0x07UL )
176 #endif
177 
178 /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for RAM
179  * region. */
180 #ifndef configTEX_S_C_B_SRAM
181     /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */
182     #define configTEX_S_C_B_SRAM          ( 0x07UL )
183 #endif
184 
185 #define portSTACK_REGION                  ( configTOTAL_MPU_REGIONS - 5UL )
186 #define portGENERAL_PERIPHERALS_REGION    ( configTOTAL_MPU_REGIONS - 4UL )
187 #define portUNPRIVILEGED_FLASH_REGION     ( configTOTAL_MPU_REGIONS - 3UL )
188 #define portPRIVILEGED_FLASH_REGION       ( configTOTAL_MPU_REGIONS - 2UL )
189 #define portPRIVILEGED_RAM_REGION         ( configTOTAL_MPU_REGIONS - 1UL )
190 #define portFIRST_CONFIGURABLE_REGION     ( 0UL )
191 #define portLAST_CONFIGURABLE_REGION      ( configTOTAL_MPU_REGIONS - 6UL )
192 #define portNUM_CONFIGURABLE_REGIONS      ( configTOTAL_MPU_REGIONS - 5UL )
193 #define portTOTAL_NUM_REGIONS_IN_TCB      ( portNUM_CONFIGURABLE_REGIONS + 1 ) /* Plus 1 to create space for the stack region. */
194 
195 typedef struct MPU_REGION_REGISTERS
196 {
197     uint32_t ulRegionBaseAddress;
198     uint32_t ulRegionAttribute;
199 } xMPU_REGION_REGISTERS;
200 
201 typedef struct MPU_REGION_SETTINGS
202 {
203     uint32_t ulRegionStartAddress;
204     uint32_t ulRegionEndAddress;
205     uint32_t ulRegionPermissions;
206 } xMPU_REGION_SETTINGS;
207 
208 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
209 
210     #ifndef configSYSTEM_CALL_STACK_SIZE
211         #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     #endif
213 
214     typedef struct SYSTEM_CALL_STACK_INFO
215     {
216         uint32_t ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE ];
217         uint32_t * pulSystemCallStack;
218         uint32_t * pulTaskStack;
219         uint32_t ulLinkRegisterAtSystemCallEntry;
220     } xSYSTEM_CALL_STACK_INFO;
221 
222 #endif /* configUSE_MPU_WRAPPERS_V1 == 0 */
223 
224 /*
225  * +---------+---------------+-----------------+-----------------+-----+
226  * | s16-s31 | s0-s15, FPSCR | CONTROL, r4-r11 | PSP, r0-r3, r12 |     |
227  * |         |               | EXC_RETURN      | LR, PC, xPSR    |     |
228  * +---------+---------------+-----------------+-----------------+-----+
229  *
230  * <--------><---------------><----------------><----------------><---->
231  *     16           17               10                 9           1
232  */
233 #define MAX_CONTEXT_SIZE                    ( 53 )
234 
235 /* Size of an Access Control List (ACL) entry in bits. */
236 #define portACL_ENTRY_SIZE_BITS             ( 32U )
237 
238 /* Flags used for xMPU_SETTINGS.ulTaskFlags member. */
239 #define portSTACK_FRAME_HAS_PADDING_FLAG    ( 1UL << 0UL )
240 #define portTASK_IS_PRIVILEGED_FLAG         ( 1UL << 1UL )
241 
242 typedef struct MPU_SETTINGS
243 {
244     xMPU_REGION_REGISTERS xRegion[ portTOTAL_NUM_REGIONS_IN_TCB ];
245     xMPU_REGION_SETTINGS xRegionSettings[ portTOTAL_NUM_REGIONS_IN_TCB ];
246     uint32_t ulContext[ MAX_CONTEXT_SIZE ];
247     uint32_t ulTaskFlags;
248 
249     #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
250         xSYSTEM_CALL_STACK_INFO xSystemCallStackInfo;
251         #if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
252             uint32_t ulAccessControlList[ ( configPROTECTED_KERNEL_OBJECT_POOL_SIZE / portACL_ENTRY_SIZE_BITS ) + 1 ];
253         #endif
254     #endif
255 } xMPU_SETTINGS;
256 
257 /* Architecture specifics. */
258 #define portSTACK_GROWTH      ( -1 )
259 #define portTICK_PERIOD_MS    ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
260 #define portBYTE_ALIGNMENT    8
261 /*-----------------------------------------------------------*/
262 
263 /* SVC numbers for various services. */
264 #define portSVC_START_SCHEDULER        100
265 #define portSVC_YIELD                  101
266 #define portSVC_RAISE_PRIVILEGE        102
267 #define portSVC_SYSTEM_CALL_EXIT       103
268 
269 /* Scheduler utilities. */
270 
271 #define portYIELD()    __asm volatile ( "   SVC %0  \n" ::"i" ( portSVC_YIELD ) : "memory" )
272 #define portYIELD_WITHIN_API()                          \
273     {                                                   \
274         /* Set a PendSV to request a context switch. */ \
275         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \
276         __DSB();                                        \
277         __ISB();                                        \
278     }
279 
280 #define portNVIC_INT_CTRL_REG     ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
281 #define portNVIC_PENDSVSET_BIT    ( 1UL << 28UL )
282 #define portEND_SWITCHING_ISR( xSwitchRequired ) \
283     do                                           \
284     {                                            \
285         if( xSwitchRequired != pdFALSE )         \
286         {                                        \
287             traceISR_EXIT_TO_SCHEDULER();        \
288             portYIELD_WITHIN_API();              \
289         }                                        \
290         else                                     \
291         {                                        \
292             traceISR_EXIT();                     \
293         }                                        \
294     } while( 0 )
295 #define portYIELD_FROM_ISR( x )    portEND_SWITCHING_ISR( x )
296 /*-----------------------------------------------------------*/
297 
298 /* Architecture specific optimisations. */
299 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
300     #define configUSE_PORT_OPTIMISED_TASK_SELECTION    1
301 #endif
302 
303 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 1 )
304 
305 /* Check the configuration. */
306     #if ( configMAX_PRIORITIES > 32 )
307         #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."
308     #endif
309 
310 /* Store/clear the ready priorities in a bit map. */
311     #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities )    ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
312     #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities )     ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
313 
314 /*-----------------------------------------------------------*/
315 
316     #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    uxTopPriority = ( 31UL - ( ( uint32_t ) __CLZ( ( uxReadyPriorities ) ) ) )
317 
318 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
319 /*-----------------------------------------------------------*/
320 
321 /* Critical section management. */
322 extern void vPortEnterCritical( void );
323 extern void vPortExitCritical( void );
324 
325 #if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
326     #define portDISABLE_INTERRUPTS()                           \
327     {                                                          \
328         __disable_interrupt();                                 \
329         __set_BASEPRI( configMAX_SYSCALL_INTERRUPT_PRIORITY ); \
330         __DSB();                                               \
331         __ISB();                                               \
332         __enable_interrupt();                                  \
333     }
334 #else
335     #define portDISABLE_INTERRUPTS()                           \
336     {                                                          \
337         __set_BASEPRI( configMAX_SYSCALL_INTERRUPT_PRIORITY ); \
338         __DSB();                                               \
339         __ISB();                                               \
340     }
341 #endif /* if ( configENABLE_ERRATA_837070_WORKAROUND == 1 ) */
342 
343 #define portENABLE_INTERRUPTS()                   __set_BASEPRI( 0 )
344 #define portENTER_CRITICAL()                      vPortEnterCritical()
345 #define portEXIT_CRITICAL()                       vPortExitCritical()
346 #define portSET_INTERRUPT_MASK_FROM_ISR()         __get_BASEPRI(); portDISABLE_INTERRUPTS()
347 #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x )    __set_BASEPRI( x )
348 /*-----------------------------------------------------------*/
349 
350 /* Task function macros as described on the FreeRTOS.org WEB site.  These are
351  * not necessary for to use this port.  They are defined so the common demo files
352  * (which build with all the ports) will build. */
353 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters )    void vFunction( void * pvParameters )
354 #define portTASK_FUNCTION( vFunction, pvParameters )          void vFunction( void * pvParameters )
355 /*-----------------------------------------------------------*/
356 
357 #if ( configASSERT_DEFINED == 1 )
358     void vPortValidateInterruptPriority( void );
359     #define portASSERT_IF_INTERRUPT_PRIORITY_INVALID()    vPortValidateInterruptPriority()
360 #endif
361 
362 /* portNOP() is not required by this port. */
363 #define portNOP()
364 
365 #define portINLINE              __inline
366 
367 #ifndef portFORCE_INLINE
368     #define portFORCE_INLINE    inline __attribute__( ( always_inline ) )
369 #endif
370 
371 /*-----------------------------------------------------------*/
372 
xPortIsInsideInterrupt(void)373 portFORCE_INLINE static BaseType_t xPortIsInsideInterrupt( void )
374 {
375     uint32_t ulCurrentInterrupt;
376     BaseType_t xReturn;
377 
378     /* Obtain the number of the currently executing interrupt. */
379     __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
380 
381     if( ulCurrentInterrupt == 0 )
382     {
383         xReturn = pdFALSE;
384     }
385     else
386     {
387         xReturn = pdTRUE;
388     }
389 
390     return xReturn;
391 }
392 
393 
394 /*-----------------------------------------------------------*/
395 
396 extern BaseType_t xIsPrivileged( void );
397 extern void vResetPrivilege( void );
398 extern void vPortSwitchToUserMode( void );
399 
400 /**
401  * @brief Checks whether or not the processor is privileged.
402  *
403  * @return 1 if the processor is already privileged, 0 otherwise.
404  */
405 #define portIS_PRIVILEGED()          xIsPrivileged()
406 
407 /**
408  * @brief Raise an SVC request to raise privilege.
409  */
410 #define portRAISE_PRIVILEGE()        __asm volatile ( "svc %0 \n" ::"i" ( portSVC_RAISE_PRIVILEGE ) : "memory" );
411 
412 /**
413  * @brief Lowers the privilege level by setting the bit 0 of the CONTROL
414  * register.
415  */
416 #define portRESET_PRIVILEGE()        vResetPrivilege()
417 
418 /**
419  * @brief Make a task unprivileged.
420  *
421  * It must be called from privileged tasks only. Calling it from unprivileged
422  * task will result in a memory protection fault.
423  */
424 #define portSWITCH_TO_USER_MODE()    vPortSwitchToUserMode()
425 /*-----------------------------------------------------------*/
426 
427 extern BaseType_t xPortIsTaskPrivileged( void );
428 
429 /**
430  * @brief Checks whether or not the calling task is privileged.
431  *
432  * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
433  */
434 #define portIS_TASK_PRIVILEGED()    xPortIsTaskPrivileged()
435 /*-----------------------------------------------------------*/
436 
437 #ifndef configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY
438     #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"
439     #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY    0
440 #endif
441 /*-----------------------------------------------------------*/
442 
443 /* Suppress warnings that are generated by the IAR tools, but cannot be fixed in
444  * the source code because to do so would cause other compilers to generate
445  * warnings. */
446 #pragma diag_suppress=Pe191
447 #pragma diag_suppress=Pa082
448 #pragma diag_suppress=Be006
449 /*-----------------------------------------------------------*/
450 
451 /* *INDENT-OFF* */
452 #ifdef __cplusplus
453     }
454 #endif
455 /* *INDENT-ON* */
456 
457 #endif /* PORTMACRO_H */
458