1 /*
2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 * Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
5 *
6 * SPDX-License-Identifier: MIT AND BSD-3-Clause
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9 * this software and associated documentation files (the "Software"), to deal in
10 * the Software without restriction, including without limitation the rights to
11 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12 * the Software, and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * https://www.FreeRTOS.org
26 * https://github.com/FreeRTOS
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 #include "pico.h"
40 #include "hardware/sync.h"
41
42 /*-----------------------------------------------------------
43 * Port specific definitions.
44 *
45 * The settings in this file configure FreeRTOS correctly for the
46 * given hardware and compiler.
47 *
48 * These settings should not be altered.
49 *-----------------------------------------------------------
50 */
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 int32_t BaseType_t;
63 typedef uint32_t 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
80 /* Architecture specifics. */
81 #define portSTACK_GROWTH ( -1 )
82 #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
83 #define portBYTE_ALIGNMENT 8
84 #define portDONT_DISCARD __attribute__( ( used ) )
85
86 /* We have to use PICO_DIVIDER_DISABLE_INTERRUPTS as the source of truth rather than our config,
87 * as our FreeRTOSConfig.h header cannot be included by ASM code - which is what this affects in the SDK */
88 #define portUSE_DIVIDER_SAVE_RESTORE !PICO_DIVIDER_DISABLE_INTERRUPTS
89 #if portUSE_DIVIDER_SAVE_RESTORE
90 #define portSTACK_LIMIT_PADDING 4
91 #endif
92
93 /*-----------------------------------------------------------*/
94
95
96 /* Scheduler utilities. */
97 extern void vPortYield( void );
98 #define portNVIC_INT_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
99 #define portNVIC_PENDSVSET_BIT ( 1UL << 28UL )
100 #define portYIELD() vPortYield()
101 #define portEND_SWITCHING_ISR( xSwitchRequired ) \
102 do \
103 { \
104 if( xSwitchRequired ) \
105 { \
106 traceISR_EXIT_TO_SCHEDULER(); \
107 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \
108 } \
109 else \
110 { \
111 traceISR_EXIT(); \
112 } \
113 } while( 0 )
114 #define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )
115
116 /*-----------------------------------------------------------*/
117
118 /* Exception handlers */
119 #if ( configUSE_DYNAMIC_EXCEPTION_HANDLERS == 0 )
120 /* We only need to override the SDK's weak functions if we want to replace them at compile time */
121 #define vPortSVCHandler isr_svcall
122 #define xPortPendSVHandler isr_pendsv
123 #define xPortSysTickHandler isr_systick
124 #endif
125
126 /*-----------------------------------------------------------*/
127
128 /* Multi-core */
129 #define portMAX_CORE_COUNT 2
130
131 /* Check validity of number of cores specified in config */
132 #if ( configNUMBER_OF_CORES < 1 || portMAX_CORE_COUNT < configNUMBER_OF_CORES )
133 #error "Invalid number of cores specified in config!"
134 #endif
135
136 #if ( configTICK_CORE < 0 || configTICK_CORE > configNUMBER_OF_CORES )
137 #error "Invalid tick core specified in config!"
138 #endif
139 /* FreeRTOS core id is always zero based, so always 0 if we're running on only one core */
140 #if configNUMBER_OF_CORES == portMAX_CORE_COUNT
141 #define portGET_CORE_ID() get_core_num()
142 #else
143 #define portGET_CORE_ID() 0
144 #endif
145
146 #define portCHECK_IF_IN_ISR() \
147 ( { \
148 uint32_t ulIPSR; \
149 __asm volatile ( "mrs %0, IPSR" : "=r" ( ulIPSR )::); \
150 ( ( uint8_t ) ulIPSR ) > 0; } )
151
152 void vYieldCore( int xCoreID );
153 #define portYIELD_CORE( a ) vYieldCore( a )
154
155 /*-----------------------------------------------------------*/
156
157 /* Critical nesting count management. */
158 #define portCRITICAL_NESTING_IN_TCB 0
159
160 extern UBaseType_t uxCriticalNestings[ configNUMBER_OF_CORES ];
161 #define portGET_CRITICAL_NESTING_COUNT( xCoreID ) ( uxCriticalNestings[ ( xCoreID ) ] )
162 #define portSET_CRITICAL_NESTING_COUNT( xCoreID, x ) ( uxCriticalNestings[ ( xCoreID ) ] = ( x ) )
163 #define portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( uxCriticalNestings[ ( xCoreID ) ]++ )
164 #define portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( uxCriticalNestings[ ( xCoreID ) ]-- )
165
166 /*-----------------------------------------------------------*/
167
168 /* Critical section management. */
169
170 #define portSET_INTERRUPT_MASK() \
171 ( { \
172 uint32_t ulState; \
173 __asm volatile ( "mrs %0, PRIMASK" : "=r" ( ulState )::); \
174 __asm volatile ( " cpsid i " ::: "memory" ); \
175 ulState; } )
176
177 #define portCLEAR_INTERRUPT_MASK( ulState ) __asm volatile ( "msr PRIMASK,%0" ::"r" ( ulState ) : )
178
179 extern uint32_t ulSetInterruptMaskFromISR( void ) __attribute__( ( naked ) );
180 extern void vClearInterruptMaskFromISR( uint32_t ulMask ) __attribute__( ( naked ) );
181 #define portSET_INTERRUPT_MASK_FROM_ISR() ulSetInterruptMaskFromISR()
182 #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x ) vClearInterruptMaskFromISR( x )
183
184 #define portDISABLE_INTERRUPTS() __asm volatile ( " cpsid i " ::: "memory" )
185 #define portENABLE_INTERRUPTS() __asm volatile ( " cpsie i " ::: "memory" )
186
187 #if ( configNUMBER_OF_CORES == 1 )
188 extern void vPortEnterCritical( void );
189 extern void vPortExitCritical( void );
190 #define portENTER_CRITICAL() vPortEnterCritical()
191 #define portEXIT_CRITICAL() vPortExitCritical()
192 #else
193 extern void vTaskEnterCritical( void );
194 extern void vTaskExitCritical( void );
195 extern UBaseType_t vTaskEnterCriticalFromISR( void );
196 extern void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus );
197 #define portENTER_CRITICAL() vTaskEnterCritical()
198 #define portEXIT_CRITICAL() vTaskExitCritical()
199 #define portENTER_CRITICAL_FROM_ISR() vTaskEnterCriticalFromISR()
200 #define portEXIT_CRITICAL_FROM_ISR( x ) vTaskExitCriticalFromISR( x )
201 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
202
203 #define portRTOS_SPINLOCK_COUNT 2
204
205 #if PICO_SDK_VERSION_MAJOR < 2
spin_try_lock_unsafe(spin_lock_t * lock)206 __force_inline static bool spin_try_lock_unsafe(spin_lock_t *lock) {
207 return *lock;
208 }
209 #endif
210
211 /* Note this is a single method with uxAcquire parameter since we have
212 * static vars, the method is always called with a compile time constant for
213 * uxAcquire, and the compiler should do the right thing! */
vPortRecursiveLock(BaseType_t xCoreID,uint32_t ulLockNum,spin_lock_t * pxSpinLock,BaseType_t uxAcquire)214 static inline void vPortRecursiveLock( BaseType_t xCoreID,
215 uint32_t ulLockNum,
216 spin_lock_t * pxSpinLock,
217 BaseType_t uxAcquire )
218 {
219 static volatile uint8_t ucOwnedByCore[ portMAX_CORE_COUNT ][portRTOS_SPINLOCK_COUNT];
220 static volatile uint8_t ucRecursionCountByLock[ portRTOS_SPINLOCK_COUNT ];
221
222 configASSERT( ulLockNum < portRTOS_SPINLOCK_COUNT );
223
224 if( uxAcquire )
225 {
226 if (!spin_try_lock_unsafe(pxSpinLock)) {
227 if( ucOwnedByCore[ xCoreID ][ ulLockNum ] )
228 {
229 configASSERT( ucRecursionCountByLock[ ulLockNum ] != 255u );
230 ucRecursionCountByLock[ ulLockNum ]++;
231 return;
232 }
233 spin_lock_unsafe_blocking(pxSpinLock);
234 }
235 configASSERT( ucRecursionCountByLock[ ulLockNum ] == 0 );
236 ucRecursionCountByLock[ ulLockNum ] = 1;
237 ucOwnedByCore[ xCoreID ][ ulLockNum ] = 1;
238 }
239 else
240 {
241 configASSERT( ( ucOwnedByCore[ xCoreID ] [ulLockNum ] ) != 0 );
242 configASSERT( ucRecursionCountByLock[ ulLockNum ] != 0 );
243
244 if( !--ucRecursionCountByLock[ ulLockNum ] )
245 {
246 ucOwnedByCore[ xCoreID ] [ ulLockNum ] = 0;
247 spin_unlock_unsafe(pxSpinLock);
248 }
249 }
250 }
251
252 #if ( configNUMBER_OF_CORES == 1 )
253 #define portGET_ISR_LOCK( xCoreID )
254 #define portRELEASE_ISR_LOCK( xCoreID )
255 #define portGET_TASK_LOCK( xCoreID )
256 #define portRELEASE_TASK_LOCK( xCoreID )
257 #else
258 #define portGET_ISR_LOCK( xCoreID ) vPortRecursiveLock( ( xCoreID ), 0, spin_lock_instance( configSMP_SPINLOCK_0 ), pdTRUE )
259 #define portRELEASE_ISR_LOCK( xCoreID ) vPortRecursiveLock( ( xCoreID ), 0, spin_lock_instance( configSMP_SPINLOCK_0 ), pdFALSE )
260 #define portGET_TASK_LOCK( xCoreID ) vPortRecursiveLock( ( xCoreID ), 1, spin_lock_instance( configSMP_SPINLOCK_1 ), pdTRUE )
261 #define portRELEASE_TASK_LOCK( xCoreID ) vPortRecursiveLock( ( xCoreID ), 1, spin_lock_instance( configSMP_SPINLOCK_1 ), pdFALSE )
262 #endif
263
264 /*-----------------------------------------------------------*/
265
266 /* Tickless idle/low power functionality. */
267 #ifndef portSUPPRESS_TICKS_AND_SLEEP
268 extern void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
269 #define portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime ) vPortSuppressTicksAndSleep( xExpectedIdleTime )
270 #endif
271 /*-----------------------------------------------------------*/
272
273 /* Task function macros as described on the FreeRTOS.org WEB site. */
274 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void * pvParameters )
275 #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void * pvParameters )
276
277 #define portNOP() __asm volatile ( "nop" )
278
279 #define portMEMORY_BARRIER() __asm volatile ( "" ::: "memory" )
280
281 /* *INDENT-OFF* */
282 #ifdef __cplusplus
283 }
284 #endif
285 /* *INDENT-ON* */
286
287 #endif /* PORTMACRO_H */
288