]> begriffs open source - cmsis-freertos/blob - Source/portable/ThirdParty/Partner-Supported-Ports/Cadence/Xtensa/xtensa_timer.h
Update FreeRTOS kernel to v11.2.0
[cmsis-freertos] / Source / portable / ThirdParty / Partner-Supported-Ports / Cadence / Xtensa / xtensa_timer.h
1  /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2015-2024 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 /*
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
46 #ifndef XTENSA_TIMER_H
47 #define XTENSA_TIMER_H
48
49 #ifdef __ASSEMBLER__
50 #include    <xtensa/coreasm.h>
51 #endif
52
53 #include    <xtensa/corebits.h>
54 #include    <xtensa/config/system.h>
55
56 #include    <FreeRTOSConfig.h>
57
58 /*
59 Select timer to use for periodic tick, and determine its interrupt number
60 and priority. User may specify a timer by defining XT_TIMER_INDEX with -D,
61 in which case its validity is checked (it must exist in this core and must
62 not be on a high priority interrupt - an error will be reported in invalid).
63 Otherwise select the highest priority low or medium priority interrupt timer
64 available.
65 */
66 #if XCHAL_NUM_TIMERS == 0
67
68   #error "This Xtensa configuration is unsupported, it has no timers."
69
70 #else
71
72 #ifndef XT_TIMER_INDEX
73   #if XCHAL_TIMER3_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
74     #if XCHAL_INT_LEVEL(XCHAL_TIMER3_INTERRUPT) <= XCHAL_EXCM_LEVEL
75       #undef  XT_TIMER_INDEX
76       #define XT_TIMER_INDEX    3
77       #undef  XT_TIMER_LEVEL
78       #define XT_TIMER_LEVEL    XCHAL_INT_LEVEL(XCHAL_TIMER3_INTERRUPT)
79     #endif
80   #endif
81   #if XCHAL_TIMER2_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
82     #if XCHAL_INT_LEVEL(XCHAL_TIMER2_INTERRUPT) <= XCHAL_EXCM_LEVEL && \
83         (!defined(XT_TIMER_INDEX) || \
84          XCHAL_INT_LEVEL(XCHAL_TIMER2_INTERRUPT) > XT_TIMER_LEVEL)
85       #undef  XT_TIMER_INDEX
86       #define XT_TIMER_INDEX    2
87       #undef  XT_TIMER_LEVEL
88       #define XT_TIMER_LEVEL    XCHAL_INT_LEVEL(XCHAL_TIMER2_INTERRUPT)
89     #endif
90   #endif
91   #if XCHAL_TIMER1_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
92     #if XCHAL_INT_LEVEL(XCHAL_TIMER1_INTERRUPT) <= XCHAL_EXCM_LEVEL && \
93         (!defined(XT_TIMER_INDEX) || \
94          XCHAL_INT_LEVEL(XCHAL_TIMER1_INTERRUPT) > XT_TIMER_LEVEL)
95       #undef  XT_TIMER_INDEX
96       #define XT_TIMER_INDEX    1
97       #undef  XT_TIMER_LEVEL
98       #define XT_TIMER_LEVEL    XCHAL_INT_LEVEL(XCHAL_TIMER1_INTERRUPT)
99     #endif
100   #endif
101   #if XCHAL_TIMER0_INTERRUPT != XTHAL_TIMER_UNCONFIGURED
102     #if XCHAL_INT_LEVEL(XCHAL_TIMER0_INTERRUPT) <= XCHAL_EXCM_LEVEL && \
103         (!defined(XT_TIMER_INDEX) || \
104          XCHAL_INT_LEVEL(XCHAL_TIMER0_INTERRUPT) > XT_TIMER_LEVEL)
105       #undef  XT_TIMER_INDEX
106       #define XT_TIMER_INDEX    0
107       #undef  XT_TIMER_LEVEL
108       #define XT_TIMER_LEVEL    XCHAL_INT_LEVEL(XCHAL_TIMER0_INTERRUPT)
109     #endif
110   #endif
111 #endif
112 #ifndef XT_TIMER_INDEX
113   #error "There is no suitable timer in this Xtensa configuration."
114 #endif
115
116 #define XT_CCOMPARE             (CCOMPARE + XT_TIMER_INDEX)
117 #define XT_TIMER_INTNUM         XCHAL_TIMER_INTERRUPT(XT_TIMER_INDEX)
118 #define XT_TIMER_INTPRI         XCHAL_INT_LEVEL(XT_TIMER_INTNUM)
119
120 /*
121  * Xtensa LX8 supports up to 128 interrupts whereas LX7 supports up to 32.
122  *
123  * Redefine the legacy XT_TIMER_INTEN (which limited the interrupt number to
124  * be in the lower 32 or lower 64 bits depending on data type) as two defines
125  * describing the INTEN bit within a 32-bit component and a 4-block component, 
126  * where each block contains a group of 32 interrupt bits.
127  *
128  * Name changes are intentional such that any dependencies are caught at 
129  * compile-time and are found regardless of timer configuration.
130  */
131 #define XT_TIMER_INTEN_BIT      (1 << (XT_TIMER_INTNUM & 31U))
132 #define XT_TIMER_INTEN_BLK      (XT_TIMER_INTNUM >> 5)
133
134 #if XT_TIMER_INTNUM == XTHAL_TIMER_UNCONFIGURED
135   #error "The timer selected by XT_TIMER_INDEX does not exist in this core."
136 #elif XT_TIMER_INTPRI > XCHAL_EXCM_LEVEL
137   #error "The timer interrupt cannot be high priority (use medium or low)."
138 #endif
139
140 #endif /* XCHAL_NUM_TIMERS */
141
142 /*
143 Set processor clock frequency, used to determine clock divisor for timer tick.
144 User should BE SURE TO ADJUST THIS for the Xtensa platform being used.
145 If using a supported board via the board-independent API defined in xtbsp.h,
146 this may be left undefined and frequency and tick divisor will be computed
147 and cached during run-time initialization.
148
149 NOTE ON SIMULATOR:
150 Under the Xtensa instruction set simulator, the frequency can only be estimated
151 because it depends on the speed of the host and the version of the simulator.
152 Also because it runs much slower than hardware, it is not possible to achieve
153 real-time performance for most applications under the simulator. A frequency
154 too low does not allow enough time between timer interrupts, starving threads.
155 To obtain a more convenient but non-real-time tick duration on the simulator,
156 compile with the option "-DXT_SIMULATOR".
157 Adjust this frequency to taste (it's not real-time anyway!).
158 */
159 #if defined(XT_SIMULATOR) && !defined(XT_CLOCK_FREQ)
160 #define XT_CLOCK_FREQ       configCPU_CLOCK_HZ
161 #endif
162
163 #if !defined(XT_CLOCK_FREQ) && !defined(XT_BOARD)
164   #error "XT_CLOCK_FREQ must be defined for the target platform."
165 #endif
166
167 /*
168 Default number of timer "ticks" per second (default 100 for 10ms tick).
169 RTOS may define this in its own way (if applicable) in xtensa_rtos.h.
170 User may redefine this to an optimal value for the application, either by
171 editing this here or in xtensa_rtos.h, or compiling with the option
172 "-DXT_TICK_PER_SEC=<value>" where <value> is a suitable number.
173 */
174 #ifndef XT_TICK_PER_SEC
175 #define XT_TICK_PER_SEC    configTICK_RATE_HZ        /* 10 ms tick = 100 ticks per second */
176 #endif
177
178 /*
179 Derivation of clock divisor for timer tick and interrupt (one per tick).
180 */
181 #ifdef XT_CLOCK_FREQ
182 #define XT_TICK_DIVISOR     (XT_CLOCK_FREQ / XT_TICK_PER_SEC)
183 #endif
184
185 #ifndef __ASSEMBLER__
186 extern unsigned _xt_tick_divisor;
187 extern void     _xt_tick_divisor_init(void);
188 #endif
189
190 #endif  /* XTENSA_TIMER_H */