xtensa_timer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /*
  30. * XTENSA INFORMATION FOR RTOS TICK TIMER AND CLOCK FREQUENCY
  31. *
  32. * This header contains definitions and macros for use primarily by Xtensa
  33. * RTOS assembly coded source files. It includes and uses the Xtensa hardware
  34. * abstraction layer (HAL) to deal with config specifics. It may also be
  35. * included in C source files.
  36. *
  37. * Edit this file to modify timer selection and to specify clock frequency and
  38. * tick duration to match timer interrupt to the real-time tick duration.
  39. *
  40. * If the RTOS has no timer interrupt, then there is no tick timer and the
  41. * clock frequency is irrelevant, so all of these macros are left undefined
  42. * and the Xtensa core configuration need not have a timer.
  43. */
  44. #ifndef XTENSA_TIMER_H
  45. #define XTENSA_TIMER_H
  46. #ifdef __ASSEMBLER__
  47. #include <xtensa/coreasm.h>
  48. #endif
  49. #include <xtensa/corebits.h>
  50. #include <xtensa/config/system.h>
  51. #include "xtensa_rtos.h" /* in case this wasn't included directly */
  52. #include <FreeRTOSConfig.h>
  53. /*
  54. Select timer to use for periodic tick, and determine its interrupt number
  55. and priority. User may specify a timer by defining XT_TIMER_INDEX with -D,
  56. in which case its validity is checked (it must exist in this core and must
  57. not be on a high priority interrupt - an error will be reported in invalid).
  58. Otherwise select the first low or medium priority interrupt timer available.
  59. */
  60. #if XCHAL_NUM_TIMERS == 0
  61. #error "This Xtensa configuration is unsupported, it has no timers."
  62. #else
  63. #ifndef XT_TIMER_INDEX
  64. #if XCHAL_TIMER3_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
  65. #if XCHAL_INT_LEVEL(XCHAL_TIMER3_INTERRUPT) <= XCHAL_EXCM_LEVEL
  66. #undef XT_TIMER_INDEX
  67. #define XT_TIMER_INDEX 3
  68. #endif
  69. #endif
  70. #if XCHAL_TIMER2_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
  71. #if XCHAL_INT_LEVEL(XCHAL_TIMER2_INTERRUPT) <= XCHAL_EXCM_LEVEL
  72. #undef XT_TIMER_INDEX
  73. #define XT_TIMER_INDEX 2
  74. #endif
  75. #endif
  76. #if XCHAL_TIMER1_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
  77. #if XCHAL_INT_LEVEL(XCHAL_TIMER1_INTERRUPT) <= XCHAL_EXCM_LEVEL
  78. #undef XT_TIMER_INDEX
  79. #define XT_TIMER_INDEX 1
  80. #endif
  81. #endif
  82. #if XCHAL_TIMER0_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
  83. #if XCHAL_INT_LEVEL(XCHAL_TIMER0_INTERRUPT) <= XCHAL_EXCM_LEVEL
  84. #undef XT_TIMER_INDEX
  85. #define XT_TIMER_INDEX 0
  86. #endif
  87. #endif
  88. #endif
  89. #ifndef XT_TIMER_INDEX
  90. #error "There is no suitable timer in this Xtensa configuration."
  91. #endif
  92. #define XT_CCOMPARE (CCOMPARE + XT_TIMER_INDEX)
  93. #define XT_TIMER_INTNUM XCHAL_TIMER_INTERRUPT(XT_TIMER_INDEX)
  94. #define XT_TIMER_INTPRI XCHAL_INT_LEVEL(XT_TIMER_INTNUM)
  95. #define XT_TIMER_INTEN (1 << XT_TIMER_INTNUM)
  96. #if XT_TIMER_INTNUM == XTHAL_TIMER_UNCONFIGURED
  97. #error "The timer selected by XT_TIMER_INDEX does not exist in this core."
  98. #elif XT_TIMER_INTPRI > XCHAL_EXCM_LEVEL
  99. #error "The timer interrupt cannot be high priority (use medium or low)."
  100. #endif
  101. #endif /* XCHAL_NUM_TIMERS */
  102. /*
  103. Set processor clock frequency, used to determine clock divisor for timer tick.
  104. User should BE SURE TO ADJUST THIS for the Xtensa platform being used.
  105. If using a supported board via the board-independent API defined in xtbsp.h,
  106. this may be left undefined and frequency and tick divisor will be computed
  107. and cached during run-time initialization.
  108. NOTE ON SIMULATOR:
  109. Under the Xtensa instruction set simulator, the frequency can only be estimated
  110. because it depends on the speed of the host and the version of the simulator.
  111. Also because it runs much slower than hardware, it is not possible to achieve
  112. real-time performance for most applications under the simulator. A frequency
  113. too low does not allow enough time between timer interrupts, starving threads.
  114. To obtain a more convenient but non-real-time tick duration on the simulator,
  115. compile with xt-xcc option "-DXT_SIMULATOR".
  116. Adjust this frequency to taste (it's not real-time anyway!).
  117. */
  118. #if defined(XT_SIMULATOR) && !defined(XT_CLOCK_FREQ)
  119. #define XT_CLOCK_FREQ configCPU_CLOCK_HZ
  120. #endif
  121. #if !defined(XT_CLOCK_FREQ) && !defined(XT_BOARD)
  122. #error "XT_CLOCK_FREQ must be defined for the target platform."
  123. #endif
  124. /*
  125. Default number of timer "ticks" per second (default 100 for 10ms tick).
  126. RTOS may define this in its own way (if applicable) in xtensa_rtos.h.
  127. User may redefine this to an optimal value for the application, either by
  128. editing this here or in xtensa_rtos.h, or compiling with xt-xcc option
  129. "-DXT_TICK_PER_SEC=<value>" where <value> is a suitable number.
  130. */
  131. #ifndef XT_TICK_PER_SEC
  132. #define XT_TICK_PER_SEC configTICK_RATE_HZ /* 10 ms tick = 100 ticks per second */
  133. #endif
  134. /*
  135. Derivation of clock divisor for timer tick and interrupt (one per tick).
  136. */
  137. #ifdef XT_CLOCK_FREQ
  138. #define XT_TICK_DIVISOR (XT_CLOCK_FREQ / XT_TICK_PER_SEC)
  139. #endif
  140. #ifndef __ASSEMBLER__
  141. extern unsigned _xt_tick_divisor;
  142. extern void _xt_tick_divisor_init(void);
  143. #endif
  144. #endif /* XTENSA_TIMER_H */