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 #ifndef PORTMACRO_H
30 #define PORTMACRO_H
31 
32 /* System include files */
33 #include <xc.h>
34 
35 /* *INDENT-OFF* */
36 #ifdef __cplusplus
37     extern "C" {
38 #endif
39 /* *INDENT-ON* */
40 
41 /*-----------------------------------------------------------
42  * Port specific definitions.
43  *
44  * The settings in this file configure FreeRTOS correctly for the
45  * given hardware and compiler.
46  *
47  * These settings should not be altered.
48  *-----------------------------------------------------------
49  */
50 
51 /* Type definitions. */
52 #define portCHAR        char
53 #define portFLOAT       float
54 #define portDOUBLE      double
55 #define portLONG        long
56 #define portSHORT       short
57 #define portSTACK_TYPE  uint32_t
58 #define portBASE_TYPE   long
59 
60 typedef portSTACK_TYPE StackType_t;
61 typedef long BaseType_t;
62 typedef unsigned long UBaseType_t;
63 
64 #if( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
65     typedef uint16_t TickType_t;
66     #define portMAX_DELAY ( TickType_t ) 0xffff
67 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
68     typedef uint32_t TickType_t;
69     #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
70 
71     /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
72     not need to be guarded with a critical section. */
73     #define portTICK_TYPE_IS_ATOMIC 1
74 #else
75     #error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
76 #endif
77 /*-----------------------------------------------------------*/
78 
79 /* Hardware specifics. */
80 #define portBYTE_ALIGNMENT          8
81 #define portSTACK_GROWTH            -1
82 #define portTICK_PERIOD_MS          ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
83 /*-----------------------------------------------------------*/
84 
85 /* Critical section management. */
86 #define portIPL_SHIFT               ( 10UL )
87 #define portALL_IPL_BITS            ( 0x3fUL << portIPL_SHIFT )
88 #define portSW0_BIT                 ( 0x01 << 8 )
89 
90 /* This clears the IPL bits, then sets them to
91 configMAX_SYSCALL_INTERRUPT_PRIORITY.  An extra check is performed if
92 configASSERT() is defined to ensure an assertion handler does not inadvertently
93 attempt to lower the IPL when the call to assert was triggered because the IPL
94 value was found to be above configMAX_SYSCALL_INTERRUPT_PRIORITY when an ISR
95 safe FreeRTOS API function was executed.  ISR safe FreeRTOS API functions are
96 those that end in FromISR.  FreeRTOS maintains a separate interrupt API to
97 ensure API function and interrupt entry is as fast and as simple as possible. */
98 #ifdef configASSERT
99     #define portDISABLE_INTERRUPTS()                                            \
100     {                                                                           \
101     uint32_t ulStatus;                                                      \
102                                                                                 \
103         /* Mask interrupts at and below the kernel interrupt priority. */       \
104         ulStatus = _CP0_GET_STATUS();                                           \
105                                                                                 \
106         /* Is the current IPL below configMAX_SYSCALL_INTERRUPT_PRIORITY? */    \
107         if( ( ( ulStatus & portALL_IPL_BITS ) >> portIPL_SHIFT ) < configMAX_SYSCALL_INTERRUPT_PRIORITY ) \
108         {                                                                       \
109             ulStatus &= ~portALL_IPL_BITS;                                      \
110             _CP0_SET_STATUS( ( ulStatus | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) ) ); \
111         }                                                                       \
112     }
113 #else /* configASSERT */
114     #define portDISABLE_INTERRUPTS()                                        \
115     {                                                                       \
116     uint32_t ulStatus;                                                  \
117                                                                             \
118         /* Mask interrupts at and below the kernel interrupt priority. */   \
119         ulStatus = _CP0_GET_STATUS();                                       \
120         ulStatus &= ~portALL_IPL_BITS;                                      \
121         _CP0_SET_STATUS( ( ulStatus | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) ) ); \
122     }
123 #endif /* configASSERT */
124 
125 #define portENABLE_INTERRUPTS()                                         \
126 {                                                                       \
127 uint32_t ulStatus;                                                  \
128                                                                         \
129     /* Unmask all interrupts. */                                        \
130     ulStatus = _CP0_GET_STATUS();                                       \
131     ulStatus &= ~portALL_IPL_BITS;                                      \
132     _CP0_SET_STATUS( ulStatus );                                        \
133 }
134 
135 
136 extern void vTaskEnterCritical( void );
137 extern void vTaskExitCritical( void );
138 #define portCRITICAL_NESTING_IN_TCB 1
139 #define portENTER_CRITICAL()        vTaskEnterCritical()
140 #define portEXIT_CRITICAL()         vTaskExitCritical()
141 
142 extern UBaseType_t uxPortSetInterruptMaskFromISR();
143 extern void vPortClearInterruptMaskFromISR( UBaseType_t );
144 #define portSET_INTERRUPT_MASK_FROM_ISR() uxPortSetInterruptMaskFromISR()
145 #define portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedStatusRegister ) vPortClearInterruptMaskFromISR( uxSavedStatusRegister )
146 
147 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
148     #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
149 #endif
150 
151 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
152 
153     /* Check the configuration. */
154     #if( configMAX_PRIORITIES > 32 )
155         #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.
156     #endif
157 
158     /* Store/clear the ready priorities in a bit map. */
159     #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
160     #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
161 
162     /*-----------------------------------------------------------*/
163 
164     #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) uxTopPriority = ( 31UL - _clz( ( uxReadyPriorities ) ) )
165 
166 #endif /* taskRECORD_READY_PRIORITY */
167 
168 /*-----------------------------------------------------------*/
169 
170 /* Task utilities. */
171 
172 #define portYIELD()                             \
173 {                                               \
174 uint32_t ulCause;                           \
175                                                 \
176     /* Trigger software interrupt. */           \
177     ulCause = _CP0_GET_CAUSE();                 \
178     ulCause |= portSW0_BIT;                     \
179     _CP0_SET_CAUSE( ulCause );                  \
180 }
181 
182 extern volatile UBaseType_t uxInterruptNesting;
183 #define portASSERT_IF_IN_ISR() configASSERT( uxInterruptNesting == 0 )
184 
185 #define portNOP()   __asm volatile ( "nop" )
186 
187 /*-----------------------------------------------------------*/
188 
189 /* Task function macros as described on the FreeRTOS.org WEB site. */
190 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters ) __attribute__((noreturn))
191 #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )
192 /*-----------------------------------------------------------*/
193 
194 #define portEND_SWITCHING_ISR( xSwitchRequired ) \
195     do                                           \
196     {                                            \
197         if( xSwitchRequired != pdFALSE )         \
198         {                                        \
199             traceISR_EXIT_TO_SCHEDULER();        \
200             portYIELD();                         \
201         }                                        \
202         else                                     \
203         {                                        \
204             traceISR_EXIT();                     \
205         }                                        \
206     } while( 0 )
207 
208 /* Required by the kernel aware debugger. */
209 #ifdef __DEBUG
210     #define portREMOVE_STATIC_QUALIFIER
211 #endif
212 
213 /* *INDENT-OFF* */
214 #ifdef __cplusplus
215     }
216 #endif
217 /* *INDENT-ON* */
218 
219 #endif /* PORTMACRO_H */
220