portmacro.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2015-2019 Cadence Design Systems, Inc.
  4. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * SPDX-License-Identifier: MIT
  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. #ifndef PORTMACRO_H
  30. #define PORTMACRO_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #ifndef __ASSEMBLER__
  35. #include <stdint.h>
  36. #include <xtensa/tie/xt_core.h>
  37. #include <xtensa/hal.h>
  38. #include <xtensa/config/core.h>
  39. #include <xtensa/config/system.h> /* required for XSHAL_CLIB */
  40. #include <xtensa/xtruntime.h>
  41. //#include "xtensa_context.h"
  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. /* Type definitions. */
  52. #define portCHAR int8_t
  53. #define portFLOAT float
  54. #define portDOUBLE double
  55. #define portLONG int32_t
  56. #define portSHORT int16_t
  57. #define portSTACK_TYPE uint32_t
  58. #define portBASE_TYPE int
  59. typedef portSTACK_TYPE StackType_t;
  60. typedef portBASE_TYPE BaseType_t;
  61. typedef unsigned portBASE_TYPE UBaseType_t;
  62. #if( configUSE_16_BIT_TICKS == 1 )
  63. typedef uint16_t TickType_t;
  64. #define portMAX_DELAY ( TickType_t ) 0xffff
  65. #else
  66. typedef uint32_t TickType_t;
  67. #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
  68. #endif
  69. /*-----------------------------------------------------------*/
  70. // portbenchmark
  71. #include "portbenchmark.h"
  72. /* Critical section management. NW-TODO: replace XTOS_SET_INTLEVEL with more efficient version, if any? */
  73. // These cannot be nested. They should be used with a lot of care and cannot be called from interrupt level.
  74. #define portDISABLE_INTERRUPTS() do { XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); portbenchmarkINTERRUPT_DISABLE(); } while (0)
  75. #define portENABLE_INTERRUPTS() do { portbenchmarkINTERRUPT_RESTORE(0); XTOS_SET_INTLEVEL(0); } while (0)
  76. // These can be nested
  77. #define portCRITICAL_NESTING_IN_TCB 1 // For now, let FreeRTOS' (tasks.c) manage critical nesting
  78. void vTaskEnterCritical(void);
  79. void vTaskExitCritical(void);
  80. #define portENTER_CRITICAL() vTaskEnterCritical()
  81. #define portEXIT_CRITICAL() vTaskExitCritical()
  82. // Cleaner and preferred solution allows nested interrupts disabling and restoring via local registers or stack.
  83. // They can be called from interrupts too.
  84. static inline unsigned portENTER_CRITICAL_NESTED() { unsigned state = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); portbenchmarkINTERRUPT_DISABLE(); return state; }
  85. #define portEXIT_CRITICAL_NESTED(state) do { portbenchmarkINTERRUPT_RESTORE(state); XTOS_RESTORE_JUST_INTLEVEL(state); } while (0)
  86. // These FreeRTOS versions are similar to the nested versions above
  87. #define portSET_INTERRUPT_MASK_FROM_ISR() portENTER_CRITICAL_NESTED()
  88. #define portCLEAR_INTERRUPT_MASK_FROM_ISR(state) portEXIT_CRITICAL_NESTED(state)
  89. /*-----------------------------------------------------------*/
  90. /* Architecture specifics. */
  91. #define portSTACK_GROWTH ( -1 )
  92. #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
  93. #define portBYTE_ALIGNMENT 4
  94. #define portNOP() XT_NOP()
  95. /*-----------------------------------------------------------*/
  96. /* Fine resolution time */
  97. #define portGET_RUN_TIME_COUNTER_VALUE() xthal_get_ccount()
  98. /* Kernel utilities. */
  99. void vPortYield( void );
  100. void _frxt_setup_switch( void );
  101. #define portYIELD() vPortYield()
  102. #define portYIELD_FROM_ISR( xHigherPriorityTaskWoken ) \
  103. if ( ( xHigherPriorityTaskWoken ) != 0 ) { \
  104. _frxt_setup_switch(); \
  105. }
  106. /*-----------------------------------------------------------*/
  107. /* Task function macros as described on the FreeRTOS.org WEB site. */
  108. #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )
  109. #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )
  110. // When coprocessors are defined, we to maintain a pointer to coprocessors area.
  111. // We currently use a hack: redefine field xMPU_SETTINGS in TCB block as a structure that can hold:
  112. // MPU wrappers, coprocessor area pointer, trace code structure, and more if needed.
  113. // The field is normally used for memory protection. FreeRTOS should create another general purpose field.
  114. typedef struct {
  115. #if XCHAL_CP_NUM > 0
  116. volatile StackType_t* coproc_area; // Pointer to coprocessor save area; MUST BE FIRST
  117. #endif
  118. #if portUSING_MPU_WRAPPERS
  119. // Define here mpu_settings, which is port dependent
  120. int mpu_setting; // Just a dummy example here; MPU not ported to Xtensa yet
  121. #endif
  122. #if configUSE_TRACE_FACILITY_2
  123. struct {
  124. // Cf. porttraceStamp()
  125. int taskstamp; /* Stamp from inside task to see where we are */
  126. int taskstampcount; /* A counter usually incremented when we restart the task's loop */
  127. } porttrace;
  128. #endif
  129. } xMPU_SETTINGS;
  130. // 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)
  131. #if (XCHAL_CP_NUM > 0 || configUSE_TRACE_FACILITY_2) && !portUSING_MPU_WRAPPERS // If MPU wrappers not used, we still need to allocate coproc area
  132. #undef portUSING_MPU_WRAPPERS
  133. #define portUSING_MPU_WRAPPERS 1 // Enable it to allocate coproc area
  134. #define MPU_WRAPPERS_H // Override mpu_wrapper.h to disable unwanted code
  135. #define PRIVILEGED_FUNCTION
  136. #define PRIVILEGED_DATA
  137. #endif
  138. // porttrace
  139. #if configUSE_TRACE_FACILITY_2
  140. #include "porttrace.h"
  141. #endif
  142. // configASSERT_2 if requested
  143. #if configASSERT_2
  144. #include <stdio.h>
  145. void exit(int);
  146. #define configASSERT( x ) if (!(x)) { porttracePrint(-1); printf("\nAssertion failed in %s:%d\n", __FILE__, __LINE__); exit(-1); }
  147. #endif
  148. /* C library support -- only XCLIB and NEWLIB are supported. */
  149. /* To enable thread-safe C library support, XT_USE_THREAD_SAFE_CLIB must be
  150. defined to be > 0 somewhere above or on the command line. */
  151. #if (XT_USE_THREAD_SAFE_CLIB > 0u) && (XSHAL_CLIB == XTHAL_CLIB_XCLIB)
  152. extern void vPortClibInit(void);
  153. #endif // XCLIB support
  154. #if (XT_USE_THREAD_SAFE_CLIB > 0u) && (XSHAL_CLIB == XTHAL_CLIB_NEWLIB)
  155. extern void vPortClibInit(void);
  156. // This C library cleanup is not currently done by FreeRTOS when deleting a task
  157. #include <stdio.h>
  158. #define portCLEAN_UP_TCB(pxTCB) vPortCleanUpTcbClib(&((pxTCB)->xNewLib_reent))
  159. static inline void vPortCleanUpTcbClib(struct _reent *ptr)
  160. {
  161. FILE * fp = &(ptr->__sf[0]);
  162. int i;
  163. for (i = 0; i < 3; ++i, ++fp) {
  164. fp->_close = NULL;
  165. }
  166. }
  167. #endif // NEWLIB support
  168. #endif // __ASSEMBLER__
  169. #ifdef __cplusplus
  170. }
  171. #endif
  172. #endif /* PORTMACRO_H */