1 /*
2 * FreeRTOS Kernel V10.4.3
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
24 *
25 * 1 tab == 4 spaces!
26 */
27
28 #ifndef PORTMACRO_H
29 #define PORTMACRO_H
30
31 #ifndef __ASSEMBLER__
32
33 #include "sdkconfig.h"
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <stdarg.h>
38 #include <xtensa/config/core.h>
39 #include <xtensa/hal.h> /* required for xthal_get_ccount. [refactor-todo] use cpu_hal instead */
40 #include <xtensa/xtruntime.h> /* required for XTOS_SET_INTLEVEL. [refactor-todo] add common intr functions to esp_hw_support */
41 #include "xt_instr_macros.h"
42 #include "soc/spinlock.h"
43 #include "hal/cpu_hal.h"
44 #include "esp_private/crosscore_int.h"
45 #include "esp_attr.h"
46 #include "esp_timer.h" /* required for esp_timer_get_time. [refactor-todo] make this common between archs */
47 #include "esp_newlib.h" /* required for esp_reent_init() in tasks.c */
48 #include "esp_heap_caps.h"
49 #include "esp_rom_sys.h"
50 #include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */
51 #include "portbenchmark.h"
52
53 /* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */
54 #include <limits.h>
55 #include <xtensa/config/system.h>
56 #include <xtensa/xtensa_api.h>
57 #include "soc/cpu.h"
58 #ifdef CONFIG_LEGACY_INCLUDE_COMMON_HEADERS
59 #include "soc/soc_memory_layout.h"
60 #endif
61
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65
66
67
68 /* --------------------------------------------------- Port Types ------------------------------------------------------
69 * - Port specific types.
70 * - The settings in this file configure FreeRTOS correctly for the given hardware and compiler.
71 * - These settings should not be altered.
72 * - The port types must come before first as they are used further down the file
73 * ------------------------------------------------------------------------------------------------------------------ */
74
75 #define portCHAR int8_t
76 #define portFLOAT float
77 #define portDOUBLE double
78 #define portLONG int32_t
79 #define portSHORT int16_t
80 #define portSTACK_TYPE uint8_t
81 #define portBASE_TYPE int
82
83 typedef portSTACK_TYPE StackType_t;
84 typedef portBASE_TYPE BaseType_t;
85 typedef unsigned portBASE_TYPE UBaseType_t;
86
87 #if( configUSE_16_BIT_TICKS == 1 )
88 typedef uint16_t TickType_t;
89 #define portMAX_DELAY ( TickType_t ) 0xffff
90 #else
91 typedef uint32_t TickType_t;
92 #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
93 #endif
94
95 /* Task function macros as described on the FreeRTOS.org WEB site. */
96 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )
97 #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )
98
99
100
101 /* ----------------------------------------------- Port Configurations -------------------------------------------------
102 * - Configurations values supplied by each port
103 * - Required by FreeRTOS
104 * ------------------------------------------------------------------------------------------------------------------ */
105
106 #define portCRITICAL_NESTING_IN_TCB 0
107 #define portSTACK_GROWTH ( -1 )
108 #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
109 #define portBYTE_ALIGNMENT 4
110 #define portNOP() XT_NOP()
111
112
113
114 /* ---------------------------------------------- Forward Declarations -------------------------------------------------
115 * - Forward declarations of all the port functions and macros need to implement the FreeRTOS porting interface
116 * - These must come before definition/declaration of the FreeRTOS porting interface
117 * ------------------------------------------------------------------------------------------------------------------ */
118
119 // --------------------- Interrupts ------------------------
120
121 /**
122 * @brief Checks if the current core is in an ISR context
123 *
124 * - ISR context consist of Low/Mid priority ISR, or time tick ISR
125 * - High priority ISRs aren't detected here, but they normally cannot call C code, so that should not be an issue anyway.
126 *
127 * @note [refactor-todo] Check if this should be inlined
128 * @return
129 * - pdTRUE if in ISR
130 * - pdFALSE otherwise
131 */
132 BaseType_t xPortInIsrContext(void);
133
134 /**
135 * @brief Asserts if in ISR context
136 *
137 * - Asserts on xPortInIsrContext() internally
138 *
139 * @note [refactor-todo] Check if this API is still required
140 * @note [refactor-todo] Check if this should be inlined
141 */
142 void vPortAssertIfInISR(void);
143
144 /**
145 * @brief Check if in ISR context from High priority ISRs
146 *
147 * - Called from High priority ISR
148 * - Checks if the previous context (before high priority interrupt) was in ISR context (meaning low/med priority)
149 *
150 * @note [refactor-todo] Check if this should be inlined
151 * @return
152 * - pdTRUE if in previous in ISR context
153 * - pdFALSE otherwise
154 */
155 BaseType_t xPortInterruptedFromISRContext(void);
156
157 /**
158 * @brief Disable interrupts in a nested manner (meant to be called from ISRs)
159 *
160 * @warning Only applies to current CPU.
161 * @return UBaseType_t Previous interrupt level
162 */
163 static inline UBaseType_t xPortSetInterruptMaskFromISR(void);
164
165 /**
166 * @brief Reenable interrupts in a nested manner (meant to be called from ISRs)
167 *
168 * @warning Only applies to current CPU.
169 * @param prev_level Previous interrupt level
170 */
171 static inline void vPortClearInterruptMaskFromISR(UBaseType_t prev_level);
172
173 /* ---------------------- Spinlocks ------------------------
174 * - Modifications made to critical sections to support SMP
175 * - See "Critical Sections & Disabling Interrupts" in docs/api-guides/freertos-smp.rst for more details
176 * - Remark: For the ESP32, portENTER_CRITICAL and portENTER_CRITICAL_ISR both alias vPortEnterCritical, meaning that
177 * either function can be called both from ISR as well as task context. This is not standard FreeRTOS
178 * behaviorr; please keep this in mind if you need any compatibility with other FreeRTOS implementations.
179 * @note [refactor-todo] Check if these comments are still true
180 * ------------------------------------------------------ */
181
182 typedef spinlock_t portMUX_TYPE; /**< Spinlock type used by FreeRTOS critical sections */
183 #define portMUX_INITIALIZER_UNLOCKED SPINLOCK_INITIALIZER /**< Spinlock initializer */
184 #define portMUX_FREE_VAL SPINLOCK_FREE /**< Spinlock is free. [refactor-todo] check if this is still required */
185 #define portMUX_NO_TIMEOUT SPINLOCK_WAIT_FOREVER /**< When passed for 'timeout_cycles', spin forever if necessary. [refactor-todo] check if this is still required */
186 #define portMUX_TRY_LOCK SPINLOCK_NO_WAIT /**< Try to acquire the spinlock a single time only. [refactor-todo] check if this is still required */
187 #define portMUX_INITIALIZE(mux) spinlock_initialize(mux) /*< Initialize a spinlock to its unlocked state */
188
189 // ------------------ Critical Sections --------------------
190
191 /**
192 * @brief Enter a SMP critical section with a timeout
193 *
194 * This function enters an SMP critical section by disabling interrupts then
195 * taking a spinlock with a specified timeout.
196 *
197 * This function can be called in a nested manner.
198 *
199 * @note This function is made non-inline on purpose to reduce code size
200 * @param mux Spinlock
201 * @param timeout Timeout to wait for spinlock in number of CPU cycles.
202 * Use portMUX_NO_TIMEOUT to wait indefinitely
203 * Use portMUX_TRY_LOCK to only getting the spinlock a single time
204 * @retval pdPASS Critical section entered (spinlock taken)
205 * @retval pdFAIL If timed out waiting for spinlock (will not occur if using portMUX_NO_TIMEOUT)
206 */
207 BaseType_t xPortEnterCriticalTimeout(portMUX_TYPE *mux, BaseType_t timeout);
208
209 /**
210 * @brief Enter a SMP critical section
211 *
212 * This function enters an SMP critical section by disabling interrupts then
213 * taking a spinlock with an unlimited timeout.
214 *
215 * This function can be called in a nested manner
216 *
217 * @param[in] mux Spinlock
218 */
219 static inline void __attribute__((always_inline)) vPortEnterCritical(portMUX_TYPE *mux);
220
221 /**
222 * @brief Exit a SMP critical section
223 *
224 * This function can be called in a nested manner. On the outer most level of nesting, this function will:
225 *
226 * - Release the spinlock
227 * - Restore the previous interrupt level before the critical section was entered
228 *
229 * If still nesting, this function simply decrements a critical nesting count
230 *
231 * @note This function is made non-inline on purpose to reduce code size
232 * @param[in] mux Spinlock
233 */
234 void vPortExitCritical(portMUX_TYPE *mux);
235
236 /**
237 * @brief FreeRTOS Compliant version of xPortEnterCriticalTimeout()
238 *
239 * Compliant version of xPortEnterCriticalTimeout() will ensure that this is
240 * called from a task context only. An abort is called otherwise.
241 *
242 * @note This function is made non-inline on purpose to reduce code size
243 *
244 * @param mux Spinlock
245 * @param timeout Timeout
246 * @return BaseType_t
247 */
248 BaseType_t xPortEnterCriticalTimeoutCompliance(portMUX_TYPE *mux, BaseType_t timeout);
249
250 /**
251 * @brief FreeRTOS compliant version of vPortEnterCritical()
252 *
253 * Compliant version of vPortEnterCritical() will ensure that this is
254 * called from a task context only. An abort is called otherwise.
255 *
256 * @param[in] mux Spinlock
257 */
258 static inline void __attribute__((always_inline)) vPortEnterCriticalCompliance(portMUX_TYPE *mux);
259
260 /**
261 * @brief FreeRTOS compliant version of vPortExitCritical()
262 *
263 * Compliant version of vPortExitCritical() will ensure that this is
264 * called from a task context only. An abort is called otherwise.
265 *
266 * @note This function is made non-inline on purpose to reduce code size
267 * @param[in] mux Spinlock
268 */
269 void vPortExitCriticalCompliance(portMUX_TYPE *mux);
270
271 /**
272 * @brief Safe version of enter critical timeout
273 *
274 * Safe version of enter critical will automatically select between
275 * portTRY_ENTER_CRITICAL() and portTRY_ENTER_CRITICAL_ISR()
276 *
277 * @param mux Spinlock
278 * @param timeout Timeout
279 * @return BaseType_t
280 */
281 static inline BaseType_t __attribute__((always_inline)) xPortEnterCriticalTimeoutSafe(portMUX_TYPE *mux, BaseType_t timeout);
282
283 /**
284 * @brief Safe version of enter critical
285 *
286 * Safe version of enter critical will automatically select between
287 * portENTER_CRITICAL() and portENTER_CRITICAL_ISR()
288 *
289 * @param[in] mux Spinlock
290 */
291 static inline void __attribute__((always_inline)) vPortEnterCriticalSafe(portMUX_TYPE *mux);
292
293 /**
294 * @brief Safe version of exit critical
295 *
296 * Safe version of enter critical will automatically select between
297 * portEXIT_CRITICAL() and portEXIT_CRITICAL_ISR()
298 *
299 * @param[in] mux Spinlock
300 */
301 static inline void __attribute__((always_inline)) vPortExitCriticalSafe(portMUX_TYPE *mux);
302
303 // ---------------------- Yielding -------------------------
304
305 /**
306 * @brief Perform a solicited context switch
307 *
308 * - Defined in portasm.S
309 *
310 * @note [refactor-todo] The rest of ESP-IDF should call taskYield() instead
311 */
312 void vPortYield( void );
313
314 /**
315 * @brief
316 *
317 * @note [refactor-todo] Refactor this to avoid va_args
318 * @param argc
319 * @param ... Variable arguments to allow for IDF prototype without arguments, and vanilla version WITH argument
320 */
321 void vPortEvaluateYieldFromISR(int argc, ...);
322
323 /**
324 * @brief Yields the other core
325 *
326 * - Send an interrupt to another core in order to make the task running on it yield for a higher-priority task.
327 * - Can be used to yield current core as well
328 *
329 * @note [refactor-todo] Put this into private macros as its only called from task.c and is not public API
330 * @param coreid ID of core to yield
331 */
332 void vPortYieldOtherCore(BaseType_t coreid);
333
334 /**
335 * @brief Checks if the current core can yield
336 *
337 * - A core cannot yield if its in an ISR or in a critical section
338 *
339 * @note [refactor-todo] See if this can be separated from port macro
340 * @return true Core can yield
341 * @return false Core cannot yield
342 */
343 static inline bool IRAM_ATTR xPortCanYield(void);
344
345 // ------------------- Hook Functions ----------------------
346
347 extern void esp_vApplicationIdleHook(void); /* Required by tasks.c */
348 extern void esp_vApplicationTickHook(void); /* Required by tasks.c */
349
350 /**
351 * @brief Hook function called on entry to tickless idle
352 *
353 * - Implemented in pm_impl.c
354 *
355 * @param xExpectedIdleTime Expected idle time
356 */
357 void vApplicationSleep(TickType_t xExpectedIdleTime);
358
359 // ----------------------- System --------------------------
360
361 /**
362 * @brief Get the tick rate per second
363 *
364 * @note [refactor-todo] make this inline
365 * @return uint32_t Tick rate in Hz
366 */
367 uint32_t xPortGetTickRateHz(void);
368
369 /**
370 * @brief Set a watchpoint to watch the last 32 bytes of the stack
371 *
372 * Callback to set a watchpoint on the end of the stack. Called every context switch to change the stack watchpoint
373 * around.
374 *
375 * @param pxStackStart Pointer to the start of the stack
376 */
377 void vPortSetStackWatchpoint( void *pxStackStart );
378
379 /**
380 * @brief Get the current core's ID
381 *
382 * @note [refactor-todo] IDF should call a FreeRTOS like macro instead of port function directly
383 * @return BaseType_t Core ID
384 */
385 static inline BaseType_t IRAM_ATTR xPortGetCoreID(void);
386
387 /**
388 * @brief Wrapper for atomic compare-and-set instruction
389 *
390 * This subroutine will atomically compare *addr to 'compare'. If *addr == compare, *addr is set to *set. *set is
391 * updated with the previous value of *addr (either 'compare' or some other value.)
392 *
393 * @warning From the ISA docs: in some (unspecified) cases, the s32c1i instruction may return the "bitwise inverse" of
394 * the old mem if the mem wasn't written. This doesn't seem to happen on the ESP32 (portMUX assertions would
395 * fail).
396 *
397 * @note [refactor-todo] Check if this can be deprecated
398 * @note [refactor-todo] Check if this function should be renamed (due to void return type)
399 *
400 * @param[inout] addr Pointer to target address
401 * @param[in] compare Compare value
402 * @param[inout] set Pointer to set value
403 */
404 static inline void __attribute__((always_inline)) uxPortCompareSet(volatile uint32_t *addr, uint32_t compare, uint32_t *set);
405
406 /**
407 * @brief Wrapper for atomic compare-and-set instruction in external RAM
408 *
409 * Atomic compare-and-set but the target address is placed in external RAM
410 *
411 * @note [refactor-todo] Check if this can be deprecated
412 *
413 * @param[inout] addr Pointer to target address
414 * @param[in] compare Compare value
415 * @param[inout] set Pointer to set value
416 */
417 static inline void __attribute__((always_inline)) uxPortCompareSetExtram(volatile uint32_t *addr, uint32_t compare, uint32_t *set);
418
419
420
421 /* ------------------------------------------- FreeRTOS Porting Interface ----------------------------------------------
422 * - Contains all the mappings of the macros required by FreeRTOS
423 * - Most come after forward declare as porting macros map to declared functions
424 * - Maps to forward declared functions
425 * ------------------------------------------------------------------------------------------------------------------ */
426
427 // ----------------------- Memory --------------------------
428
429 /**
430 * @brief Task memory allocation macros
431 *
432 * @note Because the ROM routines don't necessarily handle a stack in external RAM correctly, we force the stack
433 * memory to always be internal.
434 * @note [refactor-todo] Update portable.h to match v10.4.3 to use new malloc prototypes
435 */
436 #define portTcbMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
437 #define portStackMemoryCaps (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
438 #define pvPortMallocTcbMem(size) heap_caps_malloc(size, portTcbMemoryCaps)
439 #define pvPortMallocStackMem(size) heap_caps_malloc(size, portStackMemoryCaps)
440
441 // --------------------- Interrupts ------------------------
442
443 /**
444 * - Only applies to current core
445 * - These cannot be nested. They should be used with a lot of care and cannot be called from interrupt level.
446 *
447 * @note [refactor-todo] replace XTOS_SET_INTLEVEL with more efficient version, if any?
448 */
449 #define portDISABLE_INTERRUPTS() do { XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); portbenchmarkINTERRUPT_DISABLE(); } while (0)
450 #define portENABLE_INTERRUPTS() do { portbenchmarkINTERRUPT_RESTORE(0); XTOS_SET_INTLEVEL(0); } while (0)
451
452 /**
453 * ISR versions to enable/disable interrupts
454 */
455 #define portSET_INTERRUPT_MASK_FROM_ISR() xPortSetInterruptMaskFromISR()
456 #define portCLEAR_INTERRUPT_MASK_FROM_ISR(prev_level) vPortClearInterruptMaskFromISR(prev_level)
457
458 #define portASSERT_IF_IN_ISR() vPortAssertIfInISR()
459
460 // ------------------ Critical Sections --------------------
461
462 /**
463 * @brief FreeRTOS critical section macros
464 *
465 * - Added a spinlock argument for SMP
466 * - Can be nested
467 * - Compliance versions will assert if regular critical section API is used in ISR context
468 * - Safe versions can be called from either contexts
469 */
470 #ifdef CONFIG_FREERTOS_CHECK_PORT_CRITICAL_COMPLIANCE
471 #define portTRY_ENTER_CRITICAL(mux, timeout) xPortEnterCriticalTimeoutCompliance(mux, timeout)
472 #define portENTER_CRITICAL(mux) vPortEnterCriticalCompliance(mux)
473 #define portEXIT_CRITICAL(mux) vPortExitCriticalCompliance(mux)
474 #else
475 #define portTRY_ENTER_CRITICAL(mux, timeout) xPortEnterCriticalTimeout(mux, timeout)
476 #define portENTER_CRITICAL(mux) vPortEnterCritical(mux)
477 #define portEXIT_CRITICAL(mux) vPortExitCritical(mux)
478 #endif /* CONFIG_FREERTOS_CHECK_PORT_CRITICAL_COMPLIANCE */
479
480 #define portTRY_ENTER_CRITICAL_ISR(mux, timeout) xPortEnterCriticalTimeout(mux, timeout)
481 #define portENTER_CRITICAL_ISR(mux) vPortEnterCritical(mux)
482 #define portEXIT_CRITICAL_ISR(mux) vPortExitCritical(mux)
483
484 #define portTRY_ENTER_CRITICAL_SAFE(mux, timeout) xPortEnterCriticalTimeoutSafe(mux)
485 #define portENTER_CRITICAL_SAFE(mux) vPortEnterCriticalSafe(mux)
486 #define portEXIT_CRITICAL_SAFE(mux) vPortExitCriticalSafe(mux)
487
488 // ---------------------- Yielding -------------------------
489
490 #define portYIELD() vPortYield()
491
492 /**
493 * @note The macro below could be used when passing a single argument, or without any argument,
494 * it was developed to support both usages of portYIELD inside of an ISR. Any other usage form
495 * might result in undesired behavior
496 *
497 * @note [refactor-todo] Refactor this to avoid va_args
498 */
499 #if defined(__cplusplus) && (__cplusplus > 201703L)
500 #define portYIELD_FROM_ISR(...) vPortEvaluateYieldFromISR(portGET_ARGUMENT_COUNT(__VA_ARGS__) __VA_OPT__(,) __VA_ARGS__)
501 #else
502 #define portYIELD_FROM_ISR(...) vPortEvaluateYieldFromISR(portGET_ARGUMENT_COUNT(__VA_ARGS__), ##__VA_ARGS__)
503 #endif
504
505 /* Yielding within an API call (when interrupts are off), means the yield should be delayed
506 until interrupts are re-enabled.
507
508 To do this, we use the "cross-core" interrupt as a trigger to yield on this core when interrupts are re-enabled.This
509 is the same interrupt & code path which is used to trigger a yield between CPUs, although in this case the yield is
510 happening on the same CPU.
511 */
512 #define portYIELD_WITHIN_API() esp_crosscore_int_send_yield(xPortGetCoreID())
513
514 // ------------------- Hook Functions ----------------------
515
516 #ifndef CONFIG_FREERTOS_LEGACY_HOOKS
517 #define vApplicationIdleHook esp_vApplicationIdleHook
518 #define vApplicationTickHook esp_vApplicationTickHook
519 #endif /* !CONFIG_FREERTOS_LEGACY_HOOKS */
520
521 #define portSUPPRESS_TICKS_AND_SLEEP(idleTime) vApplicationSleep(idleTime)
522
523 // ------------------- Run Time Stats ----------------------
524
525 #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()
526
527 /**
528 * - Fine resolution uses ccount
529 * - ALT is coarse and uses esp_timer
530 * @note [refactor-todo] Make fine and alt timers mutually exclusive
531 */
532 #define portGET_RUN_TIME_COUNTER_VALUE() xthal_get_ccount()
533 #ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
534 #define portALT_GET_RUN_TIME_COUNTER_VALUE(x) do {x = (uint32_t)esp_timer_get_time();} while(0)
535 #endif
536
537 // -------------- Optimized Task Selection -----------------
538
539 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
540 /* Check the configuration. */
541 #if( configMAX_PRIORITIES > 32 )
542 #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 different priorities as tasks that share a priority will time slice.
543 #endif
544
545 /* Store/clear the ready priorities in a bit map. */
546 #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
547 #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
548 #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) uxTopPriority = ( 31 - __builtin_clz( ( uxReadyPriorities ) ) )
549 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
550
551
552
553 /* --------------------------------------------- Inline Implementations ------------------------------------------------
554 * - Implementation of inline functions of the forward declares
555 * - Should come after forward declare and FreeRTOS Porting interface, as implementation may use both.
556 * - For implementation of non-inlined functions, see port.c
557 * ------------------------------------------------------------------------------------------------------------------ */
558
559 // --------------------- Interrupts ------------------------
560
xPortSetInterruptMaskFromISR(void)561 static inline UBaseType_t __attribute__((always_inline)) xPortSetInterruptMaskFromISR(void)
562 {
563 UBaseType_t prev_int_level = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL);
564 portbenchmarkINTERRUPT_DISABLE();
565 return prev_int_level;
566 }
567
vPortClearInterruptMaskFromISR(UBaseType_t prev_level)568 static inline void __attribute__((always_inline)) vPortClearInterruptMaskFromISR(UBaseType_t prev_level)
569 {
570 portbenchmarkINTERRUPT_RESTORE(prev_level);
571 XTOS_RESTORE_JUST_INTLEVEL(prev_level);
572 }
573
574 // ------------------ Critical Sections --------------------
575
vPortEnterCritical(portMUX_TYPE * mux)576 static inline void __attribute__((always_inline)) vPortEnterCritical(portMUX_TYPE *mux)
577 {
578 xPortEnterCriticalTimeout(mux, portMUX_NO_TIMEOUT);
579 }
580
vPortEnterCriticalCompliance(portMUX_TYPE * mux)581 static inline void __attribute__((always_inline)) vPortEnterCriticalCompliance(portMUX_TYPE *mux)
582 {
583 xPortEnterCriticalTimeoutCompliance(mux, portMUX_NO_TIMEOUT);
584 }
585
xPortEnterCriticalTimeoutSafe(portMUX_TYPE * mux,BaseType_t timeout)586 static inline BaseType_t __attribute__((always_inline)) xPortEnterCriticalTimeoutSafe(portMUX_TYPE *mux, BaseType_t timeout)
587 {
588 BaseType_t ret;
589 if (xPortInIsrContext()) {
590 ret = portTRY_ENTER_CRITICAL_ISR(mux, timeout);
591 } else {
592 ret = portTRY_ENTER_CRITICAL(mux, timeout);
593 }
594 return ret;
595 }
596
vPortEnterCriticalSafe(portMUX_TYPE * mux)597 static inline void __attribute__((always_inline)) vPortEnterCriticalSafe(portMUX_TYPE *mux)
598 {
599 xPortEnterCriticalTimeoutSafe(mux, portMUX_NO_TIMEOUT);
600 }
601
vPortExitCriticalSafe(portMUX_TYPE * mux)602 static inline void __attribute__((always_inline)) vPortExitCriticalSafe(portMUX_TYPE *mux)
603 {
604 if (xPortInIsrContext()) {
605 portEXIT_CRITICAL_ISR(mux);
606 } else {
607 portEXIT_CRITICAL(mux);
608 }
609 }
610
611 // ---------------------- Yielding -------------------------
612
xPortCanYield(void)613 static inline bool IRAM_ATTR xPortCanYield(void)
614 {
615 uint32_t ps_reg = 0;
616
617 //Get the current value of PS (processor status) register
618 RSR(PS, ps_reg);
619
620 /*
621 * intlevel = (ps_reg & 0xf);
622 * excm = (ps_reg >> 4) & 0x1;
623 * CINTLEVEL is max(excm * EXCMLEVEL, INTLEVEL), where EXCMLEVEL is 3.
624 * However, just return true, only intlevel is zero.
625 */
626
627 return ((ps_reg & PS_INTLEVEL_MASK) == 0);
628 }
629
630 // ----------------------- System --------------------------
631
xPortGetCoreID(void)632 static inline BaseType_t IRAM_ATTR xPortGetCoreID(void)
633 {
634 return (uint32_t) cpu_hal_get_core_id();
635 }
636
uxPortCompareSet(volatile uint32_t * addr,uint32_t compare,uint32_t * set)637 static inline void __attribute__((always_inline)) uxPortCompareSet(volatile uint32_t *addr, uint32_t compare, uint32_t *set)
638 {
639 compare_and_set_native(addr, compare, set);
640 }
641
uxPortCompareSetExtram(volatile uint32_t * addr,uint32_t compare,uint32_t * set)642 static inline void __attribute__((always_inline)) uxPortCompareSetExtram(volatile uint32_t *addr, uint32_t compare, uint32_t *set)
643 {
644 #ifdef CONFIG_SPIRAM
645 compare_and_set_extram(addr, compare, set);
646 #endif
647 }
648
649
650
651 /* ------------------------------------------------------ Misc ---------------------------------------------------------
652 * - Miscellaneous porting macros
653 * - These are not port of the FreeRTOS porting interface, but are used by other FreeRTOS dependent components
654 * - [refactor-todo] Remove dependency on MPU wrappers by modifying TCB
655 * ------------------------------------------------------------------------------------------------------------------ */
656
657 // -------------------- Co-Processor -----------------------
658
659 // When coprocessors are defined, we maintain a pointer to coprocessors area.
660 // We currently use a hack: redefine field xMPU_SETTINGS in TCB block as a structure that can hold:
661 // MPU wrappers, coprocessor area pointer, trace code structure, and more if needed.
662 // The field is normally used for memory protection. FreeRTOS should create another general purpose field.
663 typedef struct {
664 #if XCHAL_CP_NUM > 0
665 volatile StackType_t *coproc_area; // Pointer to coprocessor save area; MUST BE FIRST
666 #endif
667
668 #if portUSING_MPU_WRAPPERS
669 // Define here mpu_settings, which is port dependent
670 int mpu_setting; // Just a dummy example here; MPU not ported to Xtensa yet
671 #endif
672 } xMPU_SETTINGS;
673
674 // Main hack to use MPU_wrappers even when no MPU is defined (warning: mpu_setting should not be accessed; otherwise move this above xMPU_SETTINGS)
675 #if (XCHAL_CP_NUM > 0) && !portUSING_MPU_WRAPPERS // If MPU wrappers not used, we still need to allocate coproc area
676 #undef portUSING_MPU_WRAPPERS
677 #define portUSING_MPU_WRAPPERS 1 // Enable it to allocate coproc area
678 #define MPU_WRAPPERS_H // Override mpu_wrapper.h to disable unwanted code
679 #define PRIVILEGED_FUNCTION
680 #define PRIVILEGED_DATA
681 #endif
682
683 void _xt_coproc_release(volatile void *coproc_sa_base);
684
685 /*
686 * The structures and methods of manipulating the MPU are contained within the
687 * port layer.
688 *
689 * Fills the xMPUSettings structure with the memory region information
690 * contained in xRegions.
691 */
692 #if( portUSING_MPU_WRAPPERS == 1 )
693 struct xMEMORY_REGION;
694 void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMORY_REGION *const xRegions, StackType_t *pxBottomOfStack, uint32_t usStackDepth ) PRIVILEGED_FUNCTION;
695 void vPortReleaseTaskMPUSettings( xMPU_SETTINGS *xMPUSettings );
696 #endif
697
698 // -------------------- VA_ARGS Yield ----------------------
699
700 /**
701 * Macro to count number of arguments of a __VA_ARGS__ used to support portYIELD_FROM_ISR with,
702 * or without arguments. The macro counts only 0 or 1 arguments.
703 *
704 * In the future, we want to switch to C++20. We also want to become compatible with clang.
705 * Hence, we provide two versions of the following macros which are using variadic arguments.
706 * The first one is using the GNU extension ##__VA_ARGS__. The second one is using the C++20 feature __VA_OPT__(,).
707 * This allows users to compile their code with standard C++20 enabled instead of the GNU extension.
708 * Below C++20, we haven't found any good alternative to using ##__VA_ARGS__.
709 */
710 #if defined(__cplusplus) && (__cplusplus > 201703L)
711 #define portGET_ARGUMENT_COUNT(...) portGET_ARGUMENT_COUNT_INNER(0 __VA_OPT__(,) __VA_ARGS__,1,0)
712 #else
713 #define portGET_ARGUMENT_COUNT(...) portGET_ARGUMENT_COUNT_INNER(0, ##__VA_ARGS__,1,0)
714 #endif
715 #define portGET_ARGUMENT_COUNT_INNER(zero, one, count, ...) count
716
717 _Static_assert(portGET_ARGUMENT_COUNT() == 0, "portGET_ARGUMENT_COUNT() result does not match for 0 arguments");
718 _Static_assert(portGET_ARGUMENT_COUNT(1) == 1, "portGET_ARGUMENT_COUNT() result does not match for 1 argument");
719
720 // -------------------- Heap Related -----------------------
721
722 /**
723 * @brief Checks if a given piece of memory can be used to store a task's TCB
724 *
725 * - Defined in port_common.c
726 *
727 * @param ptr Pointer to memory
728 * @return true Memory can be used to store a TCB
729 * @return false Otherwise
730 */
731 bool xPortCheckValidTCBMem(const void *ptr);
732
733 /**
734 * @brief Checks if a given piece of memory can be used to store a task's stack
735 *
736 * - Defined in port_common.c
737 *
738 * @param ptr Pointer to memory
739 * @return true Memory can be used to store a task stack
740 * @return false Otherwise
741 */
742 bool xPortcheckValidStackMem(const void *ptr);
743
744 #define portVALID_TCB_MEM(ptr) xPortCheckValidTCBMem(ptr)
745 #define portVALID_STACK_MEM(ptr) xPortcheckValidStackMem(ptr)
746
747
748
749 /* ---------------------------------------------------- Deprecate ------------------------------------------------------
750 * - Pull in header containing deprecated macros here
751 * ------------------------------------------------------------------------------------------------------------------ */
752
753 #include "portmacro_deprecated.h"
754
755 #ifdef __cplusplus
756 }
757 #endif
758
759 #endif // __ASSEMBLER__
760
761 #endif /* PORTMACRO_H */
762