xtensa_timer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*******************************************************************************
  2. // Copyright (c) 2003-2015 Cadence Design Systems, Inc.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included
  13. // in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. --------------------------------------------------------------------------------
  23. XTENSA INFORMATION FOR RTOS TICK TIMER AND CLOCK FREQUENCY
  24. This header contains definitions and macros for use primarily by Xtensa
  25. RTOS assembly coded source files. It includes and uses the Xtensa hardware
  26. abstraction layer (HAL) to deal with config specifics. It may also be
  27. included in C source files.
  28. User may edit to modify timer selection and to specify clock frequency and
  29. tick duration to match timer interrupt to the real-time tick duration.
  30. If the RTOS has no timer interrupt, then there is no tick timer and the
  31. clock frequency is irrelevant, so all of these macros are left undefined
  32. and the Xtensa core configuration need not have a timer.
  33. *******************************************************************************/
  34. #ifndef XTENSA_TIMER_H
  35. #define XTENSA_TIMER_H
  36. #ifdef __ASSEMBLER__
  37. #include <xtensa/coreasm.h>
  38. #endif
  39. #include <xtensa/corebits.h>
  40. #include <xtensa/config/system.h>
  41. #include "xtensa_rtos.h" /* in case this wasn't included directly */
  42. #include "FreeRTOSConfig.h"
  43. /*
  44. Select timer to use for periodic tick, and determine its interrupt number
  45. and priority. User may specify a timer by defining XT_TIMER_INDEX with -D,
  46. in which case its validity is checked (it must exist in this core and must
  47. not be on a high priority interrupt - an error will be reported in invalid).
  48. Otherwise select the first low or medium priority interrupt timer available.
  49. */
  50. #if XCHAL_NUM_TIMERS == 0
  51. #error "This Xtensa configuration is unsupported, it has no timers."
  52. #else
  53. #ifndef XT_TIMER_INDEX
  54. #if XCHAL_TIMER3_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
  55. #if XCHAL_INT_LEVEL(XCHAL_TIMER3_INTERRUPT) <= XCHAL_EXCM_LEVEL
  56. #undef XT_TIMER_INDEX
  57. #define XT_TIMER_INDEX 3
  58. #endif
  59. #endif
  60. #if XCHAL_TIMER2_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
  61. #if XCHAL_INT_LEVEL(XCHAL_TIMER2_INTERRUPT) <= XCHAL_EXCM_LEVEL
  62. #undef XT_TIMER_INDEX
  63. #define XT_TIMER_INDEX 2
  64. #endif
  65. #endif
  66. #if XCHAL_TIMER1_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
  67. #if XCHAL_INT_LEVEL(XCHAL_TIMER1_INTERRUPT) <= XCHAL_EXCM_LEVEL
  68. #undef XT_TIMER_INDEX
  69. #define XT_TIMER_INDEX 1
  70. #endif
  71. #endif
  72. #if XCHAL_TIMER0_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
  73. #if XCHAL_INT_LEVEL(XCHAL_TIMER0_INTERRUPT) <= XCHAL_EXCM_LEVEL
  74. #undef XT_TIMER_INDEX
  75. #define XT_TIMER_INDEX 0
  76. #endif
  77. #endif
  78. #endif
  79. #ifndef XT_TIMER_INDEX
  80. #error "There is no suitable timer in this Xtensa configuration."
  81. #endif
  82. #define XT_CCOMPARE (CCOMPARE + XT_TIMER_INDEX)
  83. #define XT_TIMER_INTNUM XCHAL_TIMER_INTERRUPT(XT_TIMER_INDEX)
  84. #define XT_TIMER_INTPRI XCHAL_INT_LEVEL(XT_TIMER_INTNUM)
  85. #define XT_TIMER_INTEN (1 << XT_TIMER_INTNUM)
  86. #if XT_TIMER_INTNUM == XTHAL_TIMER_UNCONFIGURED
  87. #error "The timer selected by XT_TIMER_INDEX does not exist in this core."
  88. #elif XT_TIMER_INTPRI > XCHAL_EXCM_LEVEL
  89. #error "The timer interrupt cannot be high priority (use medium or low)."
  90. #endif
  91. #endif /* XCHAL_NUM_TIMERS */
  92. /*
  93. Set processor clock frequency, used to determine clock divisor for timer tick.
  94. User should BE SURE TO ADJUST THIS for the Xtensa platform being used.
  95. If using a supported board via the board-independent API defined in xtbsp.h,
  96. this may be left undefined and frequency and tick divisor will be computed
  97. and cached during run-time initialization.
  98. NOTE ON SIMULATOR:
  99. Under the Xtensa instruction set simulator, the frequency can only be estimated
  100. because it depends on the speed of the host and the version of the simulator.
  101. Also because it runs much slower than hardware, it is not possible to achieve
  102. real-time performance for most applications under the simulator. A frequency
  103. too low does not allow enough time between timer interrupts, starving threads.
  104. To obtain a more convenient but non-real-time tick duration on the simulator,
  105. compile with xt-xcc option "-DXT_SIMULATOR".
  106. Adjust this frequency to taste (it's not real-time anyway!).
  107. */
  108. #if defined(XT_SIMULATOR) && !defined(XT_CLOCK_FREQ)
  109. #define XT_CLOCK_FREQ configCPU_CLOCK_HZ
  110. #endif
  111. #if !defined(XT_CLOCK_FREQ) && !defined(XT_BOARD)
  112. #error "XT_CLOCK_FREQ must be defined for the target platform."
  113. #endif
  114. /*
  115. Default number of timer "ticks" per second (default 100 for 10ms tick).
  116. RTOS may define this in its own way (if applicable) in xtensa_rtos.h.
  117. User may redefine this to an optimal value for the application, either by
  118. editing this here or in xtensa_rtos.h, or compiling with xt-xcc option
  119. "-DXT_TICK_PER_SEC=<value>" where <value> is a suitable number.
  120. */
  121. #ifndef XT_TICK_PER_SEC
  122. #define XT_TICK_PER_SEC configTICK_RATE_HZ /* 10 ms tick = 100 ticks per second */
  123. #endif
  124. /*
  125. Derivation of clock divisor for timer tick and interrupt (one per tick).
  126. */
  127. #ifdef XT_CLOCK_FREQ
  128. #define XT_TICK_DIVISOR (XT_CLOCK_FREQ / XT_TICK_PER_SEC)
  129. #endif
  130. #ifndef __ASSEMBLER__
  131. extern unsigned _xt_tick_divisor;
  132. extern void _xt_tick_divisor_init(void);
  133. #endif
  134. #endif /* XTENSA_TIMER_H */