• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __HAL_TIMER_H__
16 #define __HAL_TIMER_H__
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include "plat_types.h"
23 #include "hal_cmu.h"
24 
25 //=============================================================================
26 // Slow Timer (Default Timer)
27 
28 #ifdef FPGA
29 #define CONFIG_SYSTICK_HZ_NOMINAL   (32000)
30 #else
31 #define CONFIG_SYSTICK_HZ_NOMINAL   (16000)
32 #endif
33 
34 //#if (CONFIG_SYSTICK_HZ_NOMINAL % 1000)
35 //#error "Bad CONFIG_SYSTICK_HZ_NOMINAL configuration"
36 //#endif
37 
38 #ifdef CALIB_SLOW_TIMER
39 
40 #define CONFIG_SYSTICK_HZ           hal_sys_timer_systick_hz()
41 
42 #define CONFIG_SYSTICK_HZ_FLOAT     hal_sys_timer_systick_hz_float()
43 
44 #define __MS_TO_TICKS(ms)           hal_sys_timer_ms_to_ticks(ms)
45 
46 #define __US_TO_TICKS(us)           hal_sys_timer_us_to_ticks(us)
47 
48 #define __TICKS_TO_MS(tick)         hal_sys_timer_ticks_to_ms(tick)
49 
50 #define __SLIM_TICKS_TO_MS(tick)    ((tick) / ((uint32_t)CONFIG_SYSTICK_HZ / 1000))
51 
52 #define __TICKS_TO_US(tick)         hal_sys_timer_ticks_to_us(tick)
53 
54 #else
55 
56 #define CONFIG_SYSTICK_HZ           CONFIG_SYSTICK_HZ_NOMINAL
57 
58 #define CONFIG_SYSTICK_HZ_FLOAT     ((float)CONFIG_SYSTICK_HZ_NOMINAL)
59 
60 #define __MS_TO_TICKS(ms)           ((ms) * ((uint32_t)CONFIG_SYSTICK_HZ / 1000))
61 
62 #define __US_TO_TICKS(us)           (((us) * ((uint32_t)CONFIG_SYSTICK_HZ / 1000) + 1000 - 1) / 1000 + 1)
63 
64 #define __TICKS_TO_MS(tick)         ((tick) / ((uint32_t)CONFIG_SYSTICK_HZ / 1000))
65 
66 #define __SLIM_TICKS_TO_MS(tick)    ((tick) / ((uint32_t)CONFIG_SYSTICK_HZ / 1000))
67 
68 #define __TICKS_TO_US(tick)         ((tick) * 1000 / ((uint32_t)CONFIG_SYSTICK_HZ / 1000))
69 
70 #endif
71 
72 /*
73  *
74  * This is very confused with the common sense, because
75  * MS_TO_TICKS is always refer to ms converted to os ticks
76  * but here it is converted to a hardware timer's tick
77  * which ticks is 16K one second;
78  *
79  * The same is as US_TO_TICKS, TICKS_TO_MS series;
80  * They are reserved for historic reason;
81  *
82  * Note, don't use these macros, use MS_TO_HWTICKS/US_TO_HWTICKS/HWTICKS_TO_MS
83  * alternately
84  */
85 #define MS_TO_TICKS(ms)             __MS_TO_TICKS(ms)
86 
87 #define US_TO_TICKS(us)             __US_TO_TICKS(us)
88 
89 #define TICKS_TO_MS(tick)           __TICKS_TO_MS(tick)
90 
91 #define TICKS_TO_US(tick)           __TICKS_TO_US(tick)
92 
93 #define MS_TO_HWTICKS(ms)           __MS_TO_TICKS(ms)
94 
95 #define US_TO_HWTICKS(us)           __US_TO_TICKS(us)
96 
97 #define HWTICKS_TO_MS(tick)         __TICKS_TO_MS(tick)
98 
99 #define HWTICKS_TO_US(tick)         __TICKS_TO_US(tick)
100 
101 #define GET_CURRENT_TICKS()         hal_sys_timer_get()
102 
103 #define GET_CURRENT_MS()            TICKS_TO_MS(GET_CURRENT_TICKS())
104 
105 #define HAL_TIMER_LOAD_DELTA        1
106 
107 enum HAL_TIMER_TYPE_T {
108     HAL_TIMER_TYPE_FREERUNNING = 0,
109     HAL_TIMER_TYPE_ONESHOT,
110     HAL_TIMER_TYPE_PERIODIC,
111     HAL_TIMER_TYPE_QTY
112 };
113 
114 typedef void (*HAL_TIMER_IRQ_HANDLER_T)(uint32_t elapsed);
115 
116 void hal_sys_timer_open(void);
117 
118 void hal_sys_timer_wakeup(void);
119 
120 uint32_t hal_sys_timer_get(void);
121 
122 uint32_t hal_aco_timer_get(void);
123 
124 uint32_t hal_sys_timer_get_in_sleep(void);
125 
126 uint32_t hal_sys_timer_get_max(void);
127 
128 void hal_sys_timer_delay(uint32_t ticks);
129 
130 void hal_sys_timer_delay_in_sleep(uint32_t ticks);
131 
132 void hal_sys_timer_delay_us(uint32_t us);
133 
134 void hal_sys_timer_delay_ns(uint32_t ns);
135 
136 uint32_t hal_sys_timer_calc_cpu_freq(uint32_t interval_ms, int high_res);
137 
138 uint32_t flash_hal_sys_timer_get(void);
139 
140 void flash_hal_sys_timer_delay(uint32_t ticks);
141 
142 void hal_sys_timer_calib_start(void);
143 
144 int hal_sys_timer_calib_end(void);
145 
146 void hal_sys_timer_calib(void);
147 
148 uint32_t hal_sys_timer_systick_hz(void);
149 
150 float hal_sys_timer_systick_hz_float(void);
151 
152 uint32_t hal_sys_timer_ms_to_ticks(uint32_t ms);
153 
154 uint32_t hal_sys_timer_us_to_ticks(uint32_t us);
155 
156 uint32_t hal_sys_timer_ticks_to_ms(uint32_t tick);
157 
158 uint32_t hal_sys_timer_ticks_to_us(uint32_t tick);
159 
160 //=============================================================================
161 // Fast Timer
162 
163 #define CONFIG_FAST_SYSTICK_HZ      (hal_cmu_get_fast_timer_freq())
164 
165 #define MS_TO_FAST_TICKS(ms)        ((uint32_t)(ms) * (CONFIG_FAST_SYSTICK_HZ / 1000))
166 
167 #define US_TO_FAST_TICKS(us)        ((uint32_t)(us) * (CONFIG_FAST_SYSTICK_HZ / 1000 / 100) / 10)
168 
169 #define NS_TO_FAST_TICKS(ns)        ((uint32_t)(ns) * (CONFIG_FAST_SYSTICK_HZ / 1000 / 100) / 10 / 1000)
170 
171 #define FAST_TICKS_TO_MS(tick)      ((uint32_t)(tick) / (CONFIG_FAST_SYSTICK_HZ / 1000))
172 
173 #define FAST_TICKS_TO_US(tick)      ((uint32_t)(tick) * 10 / (CONFIG_FAST_SYSTICK_HZ / 1000 / 100))
174 
175 #define FAST_TICKS_TO_NS(tick)      ((uint32_t)(tick) * 10 * 1000 / (CONFIG_FAST_SYSTICK_HZ / 1000 / 100))
176 
177 uint32_t hal_fast_sys_timer_get(void);
178 
179 void hal_fast_timer_sleep();
180 
181 void hal_fast_timer_wakeup();
182 
183 //=============================================================================
184 // Non-OS compatibility
185 
186 #ifndef RTOS
187 int osDelay(uint32_t ms);
188 #endif
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
194 #endif
195 
196