xtensa_timer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2019 Cadence Design Systems, Inc.
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
  7. */
  8. /*
  9. * Copyright (c) 2015-2019 Cadence Design Systems, Inc.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining
  12. * a copy of this software and associated documentation files (the
  13. * "Software"), to deal in the Software without restriction, including
  14. * without limitation the rights to use, copy, modify, merge, publish,
  15. * distribute, sublicense, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  25. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  26. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  27. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  28. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. /*
  31. * XTENSA INFORMATION FOR RTOS TICK TIMER AND CLOCK FREQUENCY
  32. *
  33. * This header contains definitions and macros for use primarily by Xtensa
  34. * RTOS assembly coded source files. It includes and uses the Xtensa hardware
  35. * abstraction layer (HAL) to deal with config specifics. It may also be
  36. * included in C source files.
  37. *
  38. * Edit this file to modify timer selection and to specify clock frequency and
  39. * tick duration to match timer interrupt to the real-time tick duration.
  40. *
  41. * If the RTOS has no timer interrupt, then there is no tick timer and the
  42. * clock frequency is irrelevant, so all of these macros are left undefined
  43. * and the Xtensa core configuration need not have a timer.
  44. */
  45. #ifndef XTENSA_TIMER_H
  46. #define XTENSA_TIMER_H
  47. #ifdef __ASSEMBLER__
  48. #include <xtensa/coreasm.h>
  49. #endif
  50. #include <xtensa/corebits.h>
  51. #include <xtensa/config/system.h>
  52. #include "xtensa_rtos.h" /* in case this wasn't included directly */
  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 */