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.
6 * SPDX-License-Identifier: MIT AND BSD-3-Clause
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:
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
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.
25 * https://www.FreeRTOS.org
26 * https://github.com/FreeRTOS
40 #include "hardware/sync.h"
42 /*-----------------------------------------------------------
43 * Port specific definitions.
45 * The settings in this file configure FreeRTOS correctly for the
46 * given hardware and compiler.
48 * These settings should not be altered.
49 *-----------------------------------------------------------
52 /* Type definitions. */
54 #define portFLOAT float
55 #define portDOUBLE double
57 #define portSHORT short
58 #define portSTACK_TYPE uint32_t
59 #define portBASE_TYPE long
61 typedef portSTACK_TYPE StackType_t;
62 typedef int32_t BaseType_t;
63 typedef uint32_t UBaseType_t;
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
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
76 #error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
78 /*-----------------------------------------------------------*/
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 ) )
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
93 /*-----------------------------------------------------------*/
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 ) \
104 if( xSwitchRequired ) \
106 traceISR_EXIT_TO_SCHEDULER(); \
107 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \
114 #define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )
116 /*-----------------------------------------------------------*/
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
126 /*-----------------------------------------------------------*/
129 #define portMAX_CORE_COUNT 2
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!"
136 #if ( configTICK_CORE < 0 || configTICK_CORE > configNUMBER_OF_CORES )
137 #error "Invalid tick core specified in config!"
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()
143 #define portGET_CORE_ID() 0
146 #define portCHECK_IF_IN_ISR() \
149 __asm volatile ( "mrs %0, IPSR" : "=r" ( ulIPSR )::); \
150 ( ( uint8_t ) ulIPSR ) > 0; } )
152 void vYieldCore( int xCoreID );
153 #define portYIELD_CORE( a ) vYieldCore( a )
155 /*-----------------------------------------------------------*/
157 /* Critical nesting count management. */
158 #define portCRITICAL_NESTING_IN_TCB 0
160 extern UBaseType_t uxCriticalNestings[ configNUMBER_OF_CORES ];
161 #define portGET_CRITICAL_NESTING_COUNT() ( uxCriticalNestings[ portGET_CORE_ID() ] )
162 #define portSET_CRITICAL_NESTING_COUNT( x ) ( uxCriticalNestings[ portGET_CORE_ID() ] = ( x ) )
163 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( uxCriticalNestings[ portGET_CORE_ID() ]++ )
164 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( uxCriticalNestings[ portGET_CORE_ID() ]-- )
166 /*-----------------------------------------------------------*/
168 /* Critical section management. */
170 #define portSET_INTERRUPT_MASK() \
173 __asm volatile ( "mrs %0, PRIMASK" : "=r" ( ulState )::); \
174 __asm volatile ( " cpsid i " ::: "memory" ); \
177 #define portCLEAR_INTERRUPT_MASK( ulState ) __asm volatile ( "msr PRIMASK,%0" ::"r" ( ulState ) : )
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 )
184 #define portDISABLE_INTERRUPTS() __asm volatile ( " cpsid i " ::: "memory" )
185 #define portENABLE_INTERRUPTS() __asm volatile ( " cpsie i " ::: "memory" )
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()
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 ) */
203 #define portRTOS_SPINLOCK_COUNT 2
205 #if PICO_SDK_VERSION_MAJOR < 2
206 __force_inline static bool spin_try_lock_unsafe(spin_lock_t *lock) {
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 dothe right thing! */
214 static inline void vPortRecursiveLock( uint32_t ulLockNum,
215 spin_lock_t * pxSpinLock,
216 BaseType_t uxAcquire )
218 static volatile uint8_t ucOwnedByCore[ portMAX_CORE_COUNT ][portRTOS_SPINLOCK_COUNT];
219 static volatile uint8_t ucRecursionCountByLock[ portRTOS_SPINLOCK_COUNT ];
221 configASSERT( ulLockNum < portRTOS_SPINLOCK_COUNT );
222 uint32_t ulCoreNum = get_core_num();
226 if (!spin_try_lock_unsafe(pxSpinLock)) {
227 if( ucOwnedByCore[ ulCoreNum ][ ulLockNum ] )
229 configASSERT( ucRecursionCountByLock[ ulLockNum ] != 255u );
230 ucRecursionCountByLock[ ulLockNum ]++;
233 spin_lock_unsafe_blocking(pxSpinLock);
235 configASSERT( ucRecursionCountByLock[ ulLockNum ] == 0 );
236 ucRecursionCountByLock[ ulLockNum ] = 1;
237 ucOwnedByCore[ ulCoreNum ][ ulLockNum ] = 1;
241 configASSERT( ( ucOwnedByCore[ ulCoreNum ] [ulLockNum ] ) != 0 );
242 configASSERT( ucRecursionCountByLock[ ulLockNum ] != 0 );
244 if( !--ucRecursionCountByLock[ ulLockNum ] )
246 ucOwnedByCore[ ulCoreNum ] [ ulLockNum ] = 0;
247 spin_unlock_unsafe(pxSpinLock);
252 #if ( configNUMBER_OF_CORES == 1 )
253 #define portGET_ISR_LOCK()
254 #define portRELEASE_ISR_LOCK()
255 #define portGET_TASK_LOCK()
256 #define portRELEASE_TASK_LOCK()
258 #define portGET_ISR_LOCK() vPortRecursiveLock( 0, spin_lock_instance( configSMP_SPINLOCK_0 ), pdTRUE )
259 #define portRELEASE_ISR_LOCK() vPortRecursiveLock( 0, spin_lock_instance( configSMP_SPINLOCK_0 ), pdFALSE )
260 #define portGET_TASK_LOCK() vPortRecursiveLock( 1, spin_lock_instance( configSMP_SPINLOCK_1 ), pdTRUE )
261 #define portRELEASE_TASK_LOCK() vPortRecursiveLock( 1, spin_lock_instance( configSMP_SPINLOCK_1 ), pdFALSE )
264 /*-----------------------------------------------------------*/
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 )
271 /*-----------------------------------------------------------*/
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 )
277 #define portNOP() __asm volatile ( "nop" )
279 #define portMEMORY_BARRIER() __asm volatile ( "" ::: "memory" )
287 #endif /* PORTMACRO_H */