1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 #ifndef __HAL_TIMER_H__
5 #define __HAL_TIMER_H__
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #include "plat_types.h"
12 #include "hal_cmu.h"
13 
14 //=============================================================================
15 // Slow Timer (Default Timer)
16 
17 #ifdef FPGA
18 #define CONFIG_SYSTICK_HZ_NOMINAL   (32000)
19 #else
20 #define CONFIG_SYSTICK_HZ_NOMINAL   (16000)
21 #endif
22 
23 //#if (CONFIG_SYSTICK_HZ_NOMINAL % 1000)
24 //#error "Bad CONFIG_SYSTICK_HZ_NOMINAL configuration"
25 //#endif
26 
27 #ifdef CALIB_SLOW_TIMER
28 
29 #define CONFIG_SYSTICK_HZ           hal_sys_timer_systick_hz()
30 
31 #define __MS_TO_TICKS(ms)           hal_sys_timer_ms_to_ticks(ms)
32 
33 #define __US_TO_TICKS(us)           hal_sys_timer_us_to_ticks(us)
34 
35 #define __TICKS_TO_MS(tick)         hal_sys_timer_ticks_to_ms(tick)
36 
37 #define __TICKS_TO_US(tick)         hal_sys_timer_ticks_to_us(tick)
38 
39 #else
40 
41 #define CONFIG_SYSTICK_HZ           CONFIG_SYSTICK_HZ_NOMINAL
42 
43 #define __MS_TO_TICKS(ms)           ((ms) * ((uint32_t)CONFIG_SYSTICK_HZ / 1000))
44 
45 #define __US_TO_TICKS(us)           (((us) * ((uint32_t)CONFIG_SYSTICK_HZ / 1000) + 1000 - 1) / 1000 + 1)
46 
47 #define __TICKS_TO_MS(tick)         ((tick) / ((uint32_t)CONFIG_SYSTICK_HZ / 1000))
48 
49 #define __TICKS_TO_US(tick)         ((tick) * 1000 / ((uint32_t)CONFIG_SYSTICK_HZ / 1000))
50 
51 #endif
52 
53 /*
54  *
55  * This is very confused with the common sense, because
56  * MS_TO_TICKS is always refer to ms converted to os ticks
57  * but here it is converted to a hardware timer's tick
58  * which ticks is 16K one second;
59  *
60  * The same is as US_TO_TICKS, TICKS_TO_MS series;
61  * They are reserved for historic reason;
62  *
63  * Note, don't use these macros, use MS_TO_HWTICKS/US_TO_HWTICKS/HWTICKS_TO_MS
64  * alternately
65  */
66 #define MS_TO_TICKS(ms)             __MS_TO_TICKS(ms)
67 
68 #define US_TO_TICKS(us)             __US_TO_TICKS(us)
69 
70 #define TICKS_TO_MS(tick)           __TICKS_TO_MS(tick)
71 
72 #define TICKS_TO_US(tick)           __TICKS_TO_US(tick)
73 
74 #define MS_TO_HWTICKS(ms)           __MS_TO_TICKS(ms)
75 
76 #define US_TO_HWTICKS(us)             __US_TO_TICKS(us)
77 
78 #define HWTICKS_TO_MS(tick)           __TICKS_TO_MS(tick)
79 
80 #define HWTICKS_TO_US(tick)           __TICKS_TO_US(tick)
81 
82 #define GET_CURRENT_TICKS()         hal_sys_timer_get()
83 
84 #define GET_CURRENT_MS()            TICKS_TO_MS(GET_CURRENT_TICKS())
85 
86 uint32_t hal_systick_timer_get(void);
87 
88 void hal_sys_timer_open(void);
89 
90 uint32_t hal_sys_timer_get(void);
91 
92 uint32_t hal_sys_timer_get_in_sleep(void);
93 
94 uint32_t hal_sys_timer_get_max(void);
95 
96 void hal_sys_timer_delay(uint32_t ticks);
97 
98 void hal_sys_timer_delay_in_sleep(uint32_t ticks);
99 
100 void hal_sys_timer_delay_us(uint32_t us);
101 
102 void hal_sys_timer_delay_ns(uint32_t ns);
103 
104 uint32_t hal_sys_timer_calc_cpu_freq(uint32_t interval_ms, int high_res);
105 
106 uint32_t flash_hal_sys_timer_get(void);
107 
108 void flash_hal_sys_timer_delay(uint32_t ticks);
109 
110 void hal_sys_timer_calib_start(void);
111 
112 int hal_sys_timer_calib_end(void);
113 
114 void hal_sys_timer_calib(void);
115 
116 uint32_t hal_sys_timer_systick_hz(void);
117 
118 uint32_t hal_sys_timer_ms_to_ticks(uint32_t ms);
119 
120 uint32_t hal_sys_timer_us_to_ticks(uint32_t us);
121 
122 uint32_t hal_sys_timer_ticks_to_ms(uint32_t tick);
123 
124 uint32_t hal_sys_timer_ticks_to_us(uint32_t tick);
125 
126 uint32_t hal_timer_get_passed_ticks(uint32_t curr_ticks, uint32_t prev_ticks);
127 
128 //=============================================================================
129 // Fast Timer
130 
131 #define CONFIG_FAST_SYSTICK_HZ      (hal_cmu_get_crystal_freq() / 4)
132 
133 #define MS_TO_FAST_TICKS(ms)        ((uint32_t)(ms) * (CONFIG_FAST_SYSTICK_HZ / 1000))
134 
135 #define US_TO_FAST_TICKS(us)        ((uint32_t)(us) * (CONFIG_FAST_SYSTICK_HZ / 1000 / 100) / 10)
136 
137 #define NS_TO_FAST_TICKS(ns)        ((uint32_t)(ns) * (CONFIG_FAST_SYSTICK_HZ / 1000 / 100) / 10 / 1000)
138 
139 #define FAST_TICKS_TO_MS(tick)      ((uint32_t)(tick) / (CONFIG_FAST_SYSTICK_HZ / 1000))
140 
141 #define FAST_TICKS_TO_US(tick)      ((uint32_t)(tick) * 10 / (CONFIG_FAST_SYSTICK_HZ / 1000 / 100))
142 
143 #define FAST_TICKS_TO_NS(tick)      ((uint32_t)(tick) * 10 * 1000 / (CONFIG_FAST_SYSTICK_HZ / 1000 / 100))
144 
145 uint32_t hal_fast_sys_timer_get(void);
146 
147 #ifndef RTOS
148 int osDelay(uint32_t ms);
149 #endif
150 
151 typedef void (*SYS_TICK_HANDLER)(void);
152 
153 void hal_systick_timer_open(uint32_t freq, SYS_TICK_HANDLER handler);
154 void hal_systick_timer_start();
155 void hal_systick_timer_stop();
156 void hal_timer2_start(uint32_t ticks);
157 void hal_timer2_stop(void);
158 void hal_timer2_open(void(*handler)(void));
159 void hal_timer2_close(void);
160 
161 #ifdef __cplusplus
162 }
163 #endif
164 
165 #endif
166 
167