1 /*
2 * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdlib.h>
8 #include "hal/timer_hal.h"
9 #include "hal/timer_ll.h"
10 #include "soc/soc_caps.h"
11
timer_hal_init(timer_hal_context_t * hal,uint32_t group_num,uint32_t timer_num)12 void timer_hal_init(timer_hal_context_t *hal, uint32_t group_num, uint32_t timer_num)
13 {
14 hal->dev = TIMER_LL_GET_HW(group_num);
15 hal->timer_id = timer_num;
16 // enable peripheral clock
17 timer_ll_enable_clock(hal->dev, timer_num, true);
18 // stop counter, alarm, auto-reload at first place
19 timer_ll_enable_counter(hal->dev, timer_num, false);
20 timer_ll_enable_auto_reload(hal->dev, timer_num, false);
21 timer_ll_enable_alarm(hal->dev, timer_num, false);
22 // enable RTM subsystem if available
23 #if SOC_TIMER_SUPPORT_ETM
24 timer_ll_enable_etm(hal->dev, true);
25 #endif
26 }
27
timer_hal_deinit(timer_hal_context_t * hal)28 void timer_hal_deinit(timer_hal_context_t *hal)
29 {
30 // disable peripheral clock
31 timer_ll_enable_clock(hal->dev, hal->timer_id, false);
32 // ensure counter, alarm, auto-reload are disabled
33 timer_ll_enable_counter(hal->dev, hal->timer_id, false);
34 timer_ll_enable_auto_reload(hal->dev, hal->timer_id, false);
35 timer_ll_enable_alarm(hal->dev, hal->timer_id, false);
36 #if SOC_TIMER_SUPPORT_ETM
37 timer_ll_enable_etm(hal->dev, false);
38 #endif
39 hal->dev = NULL;
40 }
41