1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
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 /*******************************************************************************
16  * NOTICE
17  * The hal is not public api, don't use in application code.
18  * See readme.md in hal/include/hal/readme.md
19  ******************************************************************************/
20 
21 // The HAL layer for Timer Group.
22 // There is no parameter check in the hal layer, so the caller must ensure the correctness of the parameters.
23 
24 #pragma once
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include "soc/soc_caps.h"
31 #include "hal/timer_ll.h"
32 #include "hal/timer_types.h"
33 
34 /**
35  * Context that should be maintained by both the driver and the HAL
36  */
37 typedef struct {
38     timg_dev_t *dev;
39     timer_idx_t idx;
40 } timer_hal_context_t;
41 
42 /**
43  * @brief Init the timer hal. This function should be called first before other hal layer function is called
44  *
45  * @param hal Context of the HAL layer
46  * @param group_num The timer group number
47  * @param timer_num The timer number
48  *
49  * @return None
50  */
51 void timer_hal_init(timer_hal_context_t *hal, timer_group_t group_num, timer_idx_t timer_num);
52 
53 /**
54  * @brief Get interrupt status register address and corresponding control bits mask
55  *
56  * @param hal Context of the HAL layer
57  * @param status_reg[out] interrupt status register address
58  * @param mask_bit[out] control bits mask
59  */
60 void timer_hal_get_status_reg_mask_bit(timer_hal_context_t *hal, uint32_t *status_reg, uint32_t *mask_bit);
61 
62 /**
63  * @brief Reset timer peripheral
64  *
65  * @param hal Context of the HAL layer
66  *
67  * @return None
68  */
69 void timer_hal_reset_periph(timer_hal_context_t *hal);
70 
71 /**
72  * @brief Set timer clock prescale value
73  *
74  * @param hal Context of the HAL layer
75  * @param divider Prescale value
76  *
77  * @return None
78  */
79 #define timer_hal_set_divider(hal, divider)  timer_ll_set_divider((hal)->dev, (hal)->idx, divider)
80 
81 /**
82  * @brief Get timer clock prescale value
83  *
84  * @param hal Context of the HAL layer
85  * @param divider Pointer to accept the prescale value
86  *
87  * @return None
88  */
89 #define timer_hal_get_divider(hal, divider)  timer_ll_get_divider((hal)->dev, (hal)->idx, divider)
90 
91 /**
92  * @brief Load counter value into time-base counter
93  *
94  * @param hal Context of the HAL layer
95  * @param load_val Counter value
96  *
97  * @return None
98  */
99 #define timer_hal_set_counter_value(hal, load_val)  timer_ll_set_counter_value((hal)->dev, (hal)->idx, load_val)
100 
101 /**
102  * @brief Get counter value from time-base counter
103  *
104  * @param hal Context of the HAL layer
105  * @param timer_val Pointer to accept the counter value
106  *
107  * @return None
108  */
109 #define timer_hal_get_counter_value(hal, timer_val)  timer_ll_get_counter_value((hal)->dev, (hal)->idx, timer_val)
110 
111 /**
112  * @brief Set counter mode, include increment mode and decrement mode.
113  *
114  * @param hal Context of the HAL layer
115  * @param increase_en True to increment mode, fasle to decrement mode
116  *
117  * @return None
118  */
119 #define timer_hal_set_counter_increase(hal, increase_en)  timer_ll_set_counter_increase((hal)->dev, (hal)->idx, increase_en)
120 
121 /**
122  * @brief Get counter mode, include increment mode and decrement mode.
123  *
124  * @param hal Context of the HAL layer
125  * @param counter_dir Pointer to accept the counter mode
126  *
127  * @return
128  *     - true Increment mode
129  *     - false Decrement mode
130  */
131 #define timer_hal_get_counter_increase(hal)  timer_ll_get_counter_increase((hal)->dev, (hal)->idx)
132 
133 /**
134  * @brief Set counter status, enable or disable counter.
135  *
136  * @param hal Context of the HAL layer
137  * @param counter_en True to enable counter, false to disable counter
138  *
139  * @return None
140  */
141 #define timer_hal_set_counter_enable(hal, counter_en)  timer_ll_set_counter_enable((hal)->dev, (hal)->idx, counter_en)
142 
143 /**
144  * @brief Get counter status.
145  *
146  * @param hal Context of the HAL layer
147  *
148  * @return
149  *     - true Enable counter
150  *     - false Disable conuter
151  */
152 #define timer_hal_get_counter_enable(hal)  timer_ll_get_counter_enable((hal)->dev, (hal)->idx)
153 
154 /**
155  * @brief Set auto reload mode.
156  *
157  * @param hal Context of the HAL layer
158  * @param auto_reload_en True to enable auto reload mode, flase to disable auto reload mode
159  *
160  * @return None
161  */
162 #define timer_hal_set_auto_reload(hal, auto_reload_en)  timer_ll_set_auto_reload((hal)->dev, (hal)->idx, auto_reload_en)
163 
164 /**
165  * @brief Get auto reload mode.
166  *
167  * @param hal Context of the HAL layer
168  *
169  * @return
170  *     - true Enable auto reload mode
171  *     - false Disable auto reload mode
172  */
173 #define timer_hal_get_auto_reload(hal)  timer_ll_get_auto_reload((hal)->dev, (hal)->idx)
174 
175 /**
176  * @brief Set the counter value to trigger the alarm.
177  *
178  * @param hal Context of the HAL layer
179  * @param alarm_value Counter value to trigger the alarm
180  *
181  * @return None
182  */
183 #define timer_hal_set_alarm_value(hal, alarm_value)  timer_ll_set_alarm_value((hal)->dev, (hal)->idx, alarm_value)
184 
185 /**
186  * @brief Get the counter value to trigger the alarm.
187  *
188  * @param hal Context of the HAL layer
189  * @param alarm_value Pointer to accept the counter value to trigger the alarm
190  *
191  * @return None
192  */
193 #define timer_hal_get_alarm_value(hal, alarm_value)  timer_ll_get_alarm_value((hal)->dev, (hal)->idx, alarm_value)
194 
195 /**
196  * @brief Set the alarm status, enable or disable the alarm.
197  *
198  * @param hal Context of the HAL layer
199  * @param alarm_en True to enable alarm, false to disable alarm
200  *
201  * @return None
202  */
203 #define timer_hal_set_alarm_enable(hal, alarm_en)  timer_ll_set_alarm_enable((hal)->dev, (hal)->idx, alarm_en)
204 
205 /**
206  * @brief Get the alarm status.
207  *
208  * @param hal Context of the HAL layer
209  *
210  * @return
211  *     - true Enable alarm
212  *     - false Disable alarm
213  */
214 #define timer_hal_get_alarm_enable(hal)  timer_ll_get_alarm_enable((hal)->dev, (hal)->idx)
215 
216 /**
217  * @brief Set the level interrupt status, enable or disable the level interrupt.
218  *
219  * @param hal Context of the HAL layer
220  * @param level_int_en True to enable level interrupt, false to disable level interrupt
221  *
222  * @return None
223  */
224 #define timer_hal_set_level_int_enable(hal, level_int_en)  timer_ll_set_level_int_enable((hal)->dev, (hal)->idx, level_int_en)
225 
226 /**
227  * @brief Get the level interrupt status.
228  *
229  * @param hal Context of the HAL layer
230  *
231  * @return
232  *     - true Enable level interrupt
233  *     - false Disable level interrupt
234  */
235 #define timer_hal_get_level_int_enable(hal)  timer_ll_get_level_int_enable((hal)->dev, (hal)->idx)
236 
237 /**
238  * @brief Set the edge interrupt status, enable or disable the edge interrupt.
239  *
240  * @param hal Context of the HAL layer
241  * @param edge_int_en True to enable edge interrupt, false to disable edge interrupt
242  *
243  * @return None
244  */
245 #define timer_hal_set_edge_int_enable(hal, edge_int_en)  timer_ll_set_edge_int_enable((hal)->dev, (hal)->idx, edge_int_en)
246 
247 /**
248  * @brief Get the edge interrupt status.
249  *
250  * @param hal Context of the HAL layer
251  *
252  * @return
253  *     - true Enable edge interrupt
254  *     - false Disable edge interrupt
255  */
256 #define timer_hal_get_edge_int_enable(hal)  timer_ll_get_edge_int_enable((hal)->dev, (hal)->idx)
257 
258 /**
259  * @brief Enable timer interrupt.
260  *
261  * @param hal Context of the HAL layer
262  *
263  * @return None
264  */
265 #define timer_hal_intr_enable(hal)  timer_ll_intr_enable((hal)->dev, (hal)->idx)
266 
267 /**
268  * @brief Disable timer interrupt.
269  *
270  * @param hal Context of the HAL layer
271  *
272  * @return None
273  */
274 #define timer_hal_intr_disable(hal)  timer_ll_intr_disable((hal)->dev, (hal)->idx)
275 
276 /**
277  * @brief Clear interrupt status.
278  *
279  * @param hal Context of the HAL layer
280  *
281  * @return None
282  */
283 #define timer_hal_clear_intr_status(hal)  timer_ll_clear_intr_status((hal)->dev, (hal)->idx)
284 
285 /**
286  * @brief Get interrupt status.
287  *
288  * @param hal Context of the HAL layer
289  * @param intr_status Interrupt status
290  *
291  * @return None
292  */
293 #define timer_hal_get_intr_status(hal, intr_status)  timer_ll_get_intr_status((hal)->dev, intr_status)
294 
295 /**
296  * @brief Get interrupt raw status.
297  *
298  * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
299  * @param intr_raw_status Interrupt raw status
300  *
301  * @return None
302  */
303 #define timer_hal_get_intr_raw_status(group_num, intr_raw_status)  timer_ll_get_intr_raw_status(group_num, intr_raw_status)
304 
305 /**
306  * @brief Get interrupt status register address.
307  *
308  * @param hal Context of the HAL layer
309  *
310  * @return Interrupt status register address
311  */
312 #define timer_hal_get_intr_status_reg(hal)  timer_ll_get_intr_status_reg((hal)->dev)
313 
314 #if SOC_TIMER_GROUP_SUPPORT_XTAL
315 /**
316  * @brief Set clock source.
317  *
318  * @param hal Context of the HAL layer
319  * @param use_xtal_en True to use XTAL clock, flase to use APB clock
320  *
321  * @return None
322  */
323 #define timer_hal_set_use_xtal(hal, use_xtal_en)  timer_ll_set_use_xtal((hal)->dev, (hal)->idx, use_xtal_en)
324 
325 /**
326  * @brief Get clock source.
327  *
328  * @param hal Context of the HAL layer
329  *
330  * @return
331  *     - true Use XTAL clock
332  *     - false Use APB clock
333  */
334 #define timer_hal_get_use_xtal(hal)  timer_ll_get_use_xtal((hal)->dev, (hal)->idx)
335 #endif
336 
337 #ifdef __cplusplus
338 }
339 #endif
340