1 /*
2  * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "sdkconfig.h"
8 #include <stddef.h>
9 #include "hal/systimer_hal.h"
10 #include "hal/systimer_ll.h"
11 
12 #if CONFIG_HAL_SYSTIMER_USE_ROM_IMPL
13 
14 #if CONFIG_IDF_TARGET_ESP32C2 && (CONFIG_ESP32C2_REV_MIN_FULL < 200)
systimer_hal_init(systimer_hal_context_t * hal)15 void systimer_hal_init(systimer_hal_context_t *hal)
16 {
17     hal->dev = &SYSTIMER;
18     systimer_ll_enable_clock(hal->dev, true);
19 }
20 
systimer_hal_deinit(systimer_hal_context_t * hal)21 void systimer_hal_deinit(systimer_hal_context_t *hal)
22 {
23     systimer_ll_enable_clock(hal->dev, false);
24     hal->dev = NULL;
25 }
26 
systimer_hal_set_tick_rate_ops(systimer_hal_context_t * hal,systimer_hal_tick_rate_ops_t * ops)27 void systimer_hal_set_tick_rate_ops(systimer_hal_context_t *hal, systimer_hal_tick_rate_ops_t *ops)
28 {
29     hal->ticks_to_us = ops->ticks_to_us;
30     hal->us_to_ticks = ops->us_to_ticks;
31 }
32 
systimer_hal_get_time(systimer_hal_context_t * hal,uint32_t counter_id)33 uint64_t systimer_hal_get_time(systimer_hal_context_t *hal, uint32_t counter_id)
34 {
35     return hal->ticks_to_us(systimer_hal_get_counter_value(hal, counter_id));
36 }
37 
systimer_hal_set_alarm_target(systimer_hal_context_t * hal,uint32_t alarm_id,uint64_t target)38 void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t target)
39 {
40     systimer_counter_value_t alarm = {
41         .val = hal->us_to_ticks(target),
42     };
43     systimer_ll_enable_alarm(hal->dev, alarm_id, false);
44     systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val);
45     systimer_ll_apply_alarm_value(hal->dev, alarm_id);
46     systimer_ll_enable_alarm(hal->dev, alarm_id, true);
47 }
48 
systimer_hal_set_alarm_period(systimer_hal_context_t * hal,uint32_t alarm_id,uint32_t period)49 void systimer_hal_set_alarm_period(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t period)
50 {
51     systimer_ll_enable_alarm(hal->dev, alarm_id, false);
52     systimer_ll_set_alarm_period(hal->dev, alarm_id, hal->us_to_ticks(period));
53     systimer_ll_apply_alarm_value(hal->dev, alarm_id);
54     systimer_ll_enable_alarm(hal->dev, alarm_id, true);
55 }
56 
systimer_hal_counter_value_advance(systimer_hal_context_t * hal,uint32_t counter_id,int64_t time_us)57 void systimer_hal_counter_value_advance(systimer_hal_context_t *hal, uint32_t counter_id, int64_t time_us)
58 {
59     systimer_counter_value_t new_count = {
60         .val = systimer_hal_get_counter_value(hal, counter_id) + hal->us_to_ticks(time_us),
61     };
62     systimer_ll_set_counter_value(hal->dev, counter_id, new_count.val);
63     systimer_ll_apply_counter_value(hal->dev, counter_id);
64 }
65 #endif // CONFIG_IDF_TARGET_ESP32C2 && (CONFIG_ESP32C2_REV_MIN_FULL < 200)
66 
67 #if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2
systimer_hal_init(systimer_hal_context_t * hal)68 void systimer_hal_init(systimer_hal_context_t *hal)
69 {
70     hal->dev = &SYSTIMER;
71     systimer_ll_enable_clock(hal->dev, true);
72     systimer_ll_enable_etm(&SYSTIMER, true);
73 }
74 
systimer_hal_deinit(systimer_hal_context_t * hal)75 void systimer_hal_deinit(systimer_hal_context_t *hal)
76 {
77     systimer_ll_enable_etm(&SYSTIMER, false);
78     systimer_ll_enable_clock(hal->dev, false);
79     hal->dev = NULL;
80 }
81 #endif // CONFIG_IDF_TARGET_ESP32C6
82 
83 #endif // CONFIG_HAL_SYSTIMER_USE_ROM_IMPL
84