1 /*
2  * FreeRTOS Kernel V10.6.2
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 /*-----------------------------------------------------------
41  * Port specific definitions.
42  *
43  * The settings in this file configure FreeRTOS correctly for the
44  * given hardware and compiler.
45  *
46  * These settings should not be altered.
47  *-----------------------------------------------------------
48  */
49 
50 /* Type definitions. */
51     #define portCHAR          char
52     #define portFLOAT         float
53     #define portDOUBLE        double
54     #define portLONG          long
55     #define portSHORT         short
56     #define portSTACK_TYPE    uint32_t
57     #define portBASE_TYPE     long
58 
59     typedef portSTACK_TYPE    StackType_t;
60     typedef int32_t           BaseType_t;
61     typedef uint32_t          UBaseType_t;
62 
63     #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
64         typedef uint16_t     TickType_t;
65         #define portMAX_DELAY              ( TickType_t ) 0xffff
66     #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
67         typedef uint32_t     TickType_t;
68         #define portMAX_DELAY              ( TickType_t ) 0xffffffffUL
69 
70 /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
71  * not need to be guarded with a critical section. */
72         #define portTICK_TYPE_IS_ATOMIC    1
73     #else
74         #error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
75     #endif
76 /*-----------------------------------------------------------*/
77 
78 /* Architecture specifics. */
79     #define portSTACK_GROWTH      ( -1 )
80     #define portTICK_PERIOD_MS    ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
81     #define portBYTE_ALIGNMENT    8
82     #define portDONT_DISCARD      __attribute__( ( used ) )
83     /* We have to use PICO_DIVIDER_DISABLE_INTERRUPTS as the source of truth rathern than our config,
84      * as our FreeRTOSConfig.h header cannot be included by ASM code - which is what this affects in the SDK */
85     #define portUSE_DIVIDER_SAVE_RESTORE !PICO_DIVIDER_DISABLE_INTERRUPTS
86     #if portUSE_DIVIDER_SAVE_RESTORE
87     #define portSTACK_LIMIT_PADDING 4
88     #endif
89 
90 /*-----------------------------------------------------------*/
91 
92 
93 /* Scheduler utilities. */
94     extern void vPortYield( void );
95     #define portNVIC_INT_CTRL_REG     ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
96     #define portNVIC_PENDSVSET_BIT    ( 1UL << 28UL )
97     #define portYIELD()                                 vPortYield()
98     #define portEND_SWITCHING_ISR( xSwitchRequired )    if( xSwitchRequired ) portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT
99     #define portYIELD_FROM_ISR( x )                     portEND_SWITCHING_ISR( x )
100 
101 /*-----------------------------------------------------------*/
102 
103 /* Exception handlers */
104     #if (configUSE_DYNAMIC_EXCEPTION_HANDLERS == 0)
105         /* We only need to override the SDK's weak functions if we want to replace them at compile time */
106         #define vPortSVCHandler isr_svcall
107         #define xPortPendSVHandler isr_pendsv
108         #define xPortSysTickHandler isr_systick
109     #endif
110 
111     #define portCHECK_IF_IN_ISR() ({ \
112         uint32_t ulIPSR;                                                  \
113        __asm volatile ("mrs %0, IPSR" : "=r" (ulIPSR)::);             \
114        ((uint8_t)ulIPSR)>0;})
115 
116 /*-----------------------------------------------------------*/
117 
118 /* Critical section management. */
119     extern uint32_t ulSetInterruptMaskFromISR( void ) __attribute__( ( naked ) );
120     extern void vClearInterruptMaskFromISR( uint32_t ulMask )  __attribute__( ( naked ) );
121     #define portSET_INTERRUPT_MASK_FROM_ISR()         ulSetInterruptMaskFromISR()
122     #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x )    vClearInterruptMaskFromISR( x )
123 
124     #define portDISABLE_INTERRUPTS()                  __asm volatile ( " cpsid i " ::: "memory" )
125 
126     extern void vPortEnableInterrupts();
127     #define portENABLE_INTERRUPTS()                   vPortEnableInterrupts()
128 
129     extern void vPortEnterCritical( void );
130     extern void vPortExitCritical( void );
131     #define portENTER_CRITICAL()                      vPortEnterCritical()
132     #define portEXIT_CRITICAL()                       vPortExitCritical()
133 
134 /*-----------------------------------------------------------*/
135 
136 /* Tickless idle/low power functionality. */
137     #ifndef portSUPPRESS_TICKS_AND_SLEEP
138         extern void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
139         #define portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime )    vPortSuppressTicksAndSleep( xExpectedIdleTime )
140     #endif
141 /*-----------------------------------------------------------*/
142 
143 /* Task function macros as described on the FreeRTOS.org WEB site. */
144     #define portTASK_FUNCTION_PROTO( vFunction, pvParameters )    void vFunction( void * pvParameters )
145     #define portTASK_FUNCTION( vFunction, pvParameters )          void vFunction( void * pvParameters )
146 
147     #define portNOP()
148 
149     #define portMEMORY_BARRIER()    __asm volatile ( "" ::: "memory" )
150 
151 /* *INDENT-OFF* */
152 #ifdef __cplusplus
153     }
154 #endif
155 /* *INDENT-ON* */
156 
157 #endif /* PORTMACRO_H */
158