1 /*
2  * FreeRTOS Kernel V11.1.0
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 /* Type definitions. */
50 #if __riscv_xlen == 64
51     #define portSTACK_TYPE           uint64_t
52     #define portBASE_TYPE            int64_t
53     #define portUBASE_TYPE           uint64_t
54     #define portMAX_DELAY            ( TickType_t ) 0xffffffffffffffffUL
55     #define portPOINTER_SIZE_TYPE    uint64_t
56 #elif __riscv_xlen == 32
57     #define portSTACK_TYPE           uint32_t
58     #define portBASE_TYPE            int32_t
59     #define portUBASE_TYPE           uint32_t
60     #define portMAX_DELAY            ( TickType_t ) 0xffffffffUL
61 #else /* if __riscv_xlen == 64 */
62     #error "Assembler did not define __riscv_xlen"
63 #endif /* if __riscv_xlen == 64 */
64 
65 typedef portSTACK_TYPE   StackType_t;
66 typedef portBASE_TYPE    BaseType_t;
67 typedef portUBASE_TYPE   UBaseType_t;
68 typedef portUBASE_TYPE   TickType_t;
69 
70 /* Legacy type definitions. */
71 #define portCHAR                   char
72 #define portFLOAT                  float
73 #define portDOUBLE                 double
74 #define portLONG                   long
75 #define portSHORT                  short
76 
77 /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
78  * not need to be guarded with a critical section. */
79 #define portTICK_TYPE_IS_ATOMIC    1
80 /*-----------------------------------------------------------*/
81 
82 /* Architecture specifics. */
83 #define portSTACK_GROWTH          ( -1 )
84 #define portTICK_PERIOD_MS        ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
85 #ifdef __riscv_32e
86     #define portBYTE_ALIGNMENT    8     /* RV32E uses RISC-V EABI with reduced stack alignment requirements */
87 #else
88     #define portBYTE_ALIGNMENT    16
89 #endif
90 /*-----------------------------------------------------------*/
91 
92 /* Scheduler utilities. */
93 extern void vTaskSwitchContext( void );
94 #define portYIELD()                __asm volatile ( "ecall" );
95 #define portEND_SWITCHING_ISR( xSwitchRequired ) \
96     do                                           \
97     {                                            \
98         if( xSwitchRequired != pdFALSE )         \
99         {                                        \
100             traceISR_EXIT_TO_SCHEDULER();        \
101             vTaskSwitchContext();                \
102         }                                        \
103         else                                     \
104         {                                        \
105             traceISR_EXIT();                     \
106         }                                        \
107     } while( 0 )
108 #define portYIELD_FROM_ISR( x )    portEND_SWITCHING_ISR( x )
109 /*-----------------------------------------------------------*/
110 
111 /* Critical section management. */
112 #define portCRITICAL_NESTING_IN_TCB    0
113 
114 #define portDISABLE_INTERRUPTS()                                   __asm volatile ( "csrc mstatus, 8" )
115 #define portENABLE_INTERRUPTS()                                    __asm volatile ( "csrs mstatus, 8" )
116 
117 extern size_t xCriticalNesting;
118 #define portENTER_CRITICAL()      \
119     {                             \
120         portDISABLE_INTERRUPTS(); \
121         xCriticalNesting++;       \
122     }
123 
124 #define portEXIT_CRITICAL()          \
125     {                                \
126         xCriticalNesting--;          \
127         if( xCriticalNesting == 0 )  \
128         {                            \
129             portENABLE_INTERRUPTS(); \
130         }                            \
131     }
132 
133 /*-----------------------------------------------------------*/
134 
135 /* Architecture specific optimisations. */
136 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
137     #define configUSE_PORT_OPTIMISED_TASK_SELECTION    1
138 #endif
139 
140 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 1 )
141 
142 /* Check the configuration. */
143     #if ( configMAX_PRIORITIES > 32 )
144         #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."
145     #endif
146 
147 /* Store/clear the ready priorities in a bit map. */
148     #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities )    ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
149     #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities )     ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
150 
151 /*-----------------------------------------------------------*/
152 
153     #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    uxTopPriority = ( 31UL - __builtin_clz( uxReadyPriorities ) )
154 
155 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
156 
157 
158 /*-----------------------------------------------------------*/
159 
160 /* Task function macros as described on the FreeRTOS.org WEB site. These are
161  * not necessary for to use this port.  They are defined so the common demo
162  * files (which build with all the ports) will build. */
163 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters )    void vFunction( void * pvParameters )
164 #define portTASK_FUNCTION( vFunction, pvParameters )          void vFunction( void * pvParameters )
165 
166 /*-----------------------------------------------------------*/
167 
168 #define portNOP()    __asm volatile ( " nop " )
169 #define portINLINE              __inline
170 
171 #ifndef portFORCE_INLINE
172     #define portFORCE_INLINE    inline __attribute__( ( always_inline ) )
173 #endif
174 
175 #define portMEMORY_BARRIER()    __asm volatile ( "" ::: "memory" )
176 /*-----------------------------------------------------------*/
177 
178 /* configCLINT_BASE_ADDRESS is a legacy definition that was replaced by the
179  * configMTIME_BASE_ADDRESS and configMTIMECMP_BASE_ADDRESS definitions.  For
180  * backward compatibility derive the newer definitions from the old if the old
181  * definition is found. */
182 #if defined( configCLINT_BASE_ADDRESS ) && !defined( configMTIME_BASE_ADDRESS ) && ( configCLINT_BASE_ADDRESS == 0 )
183 
184 /* Legacy case where configCLINT_BASE_ADDRESS was defined as 0 to indicate
185  * there was no CLINT.  Equivalent now is to set the MTIME and MTIMECMP
186  * addresses to 0. */
187     #define configMTIME_BASE_ADDRESS       ( 0 )
188     #define configMTIMECMP_BASE_ADDRESS    ( 0 )
189 #elif defined( configCLINT_BASE_ADDRESS ) && !defined( configMTIME_BASE_ADDRESS )
190 
191 /* Legacy case where configCLINT_BASE_ADDRESS was set to the base address of
192  * the CLINT.  Equivalent now is to derive the MTIME and MTIMECMP addresses
193  * from the CLINT address. */
194     #define configMTIME_BASE_ADDRESS       ( ( configCLINT_BASE_ADDRESS ) + 0xBFF8UL )
195     #define configMTIMECMP_BASE_ADDRESS    ( ( configCLINT_BASE_ADDRESS ) + 0x4000UL )
196 #elif !defined( configMTIME_BASE_ADDRESS ) || !defined( configMTIMECMP_BASE_ADDRESS )
197     #error "configMTIME_BASE_ADDRESS and configMTIMECMP_BASE_ADDRESS must be defined in FreeRTOSConfig.h.  Set them to zero if there is no MTIME (machine time) clock.  See www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html"
198 #endif /* if defined( configCLINT_BASE_ADDRESS ) && !defined( configMTIME_BASE_ADDRESS ) && ( configCLINT_BASE_ADDRESS == 0 ) */
199 
200 /* *INDENT-OFF* */
201 #ifdef __cplusplus
202     }
203 #endif
204 /* *INDENT-ON* */
205 
206 #endif /* PORTMACRO_H */
207