1 /*
2  * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <esp_types.h>
10 #include "sdkconfig.h"
11 #include "esp_attr.h"
12 #include "soc/soc.h"
13 #include "hal/lp_timer_ll.h"
14 
15 static DRAM_ATTR struct {
16     lp_timer_dev_t *dev;
17 } lp_timer_context = { .dev = &LP_TIMER };
18 
lp_timer_hal_set_alarm_target(uint8_t timer_id,uint64_t value)19 void IRAM_ATTR lp_timer_hal_set_alarm_target(uint8_t timer_id, uint64_t value)
20 {
21     lp_timer_ll_clear_alarm_intr_status(lp_timer_context.dev);
22     lp_timer_ll_set_alarm_target(lp_timer_context.dev, timer_id, value);
23     lp_timer_ll_set_target_enable(lp_timer_context.dev, timer_id, true);
24 }
25 
lp_timer_hal_get_cycle_count(void)26 uint64_t IRAM_ATTR lp_timer_hal_get_cycle_count(void)
27 {
28     /* Shifts current count to buffer 0, and the value in buffer 0 to buffer 1 */
29     lp_timer_ll_counter_snapshot(lp_timer_context.dev);
30 
31     uint32_t lo = lp_timer_ll_get_counter_value_low(lp_timer_context.dev, 0);
32     uint32_t hi = lp_timer_ll_get_counter_value_high(lp_timer_context.dev, 0);
33 
34     lp_timer_counter_value_t result = {
35         .lo = lo,
36         .hi = hi
37     };
38     return result.val;
39 }
40 
lp_timer_hal_clear_alarm_intr_status(void)41 void IRAM_ATTR lp_timer_hal_clear_alarm_intr_status(void)
42 {
43     lp_timer_ll_clear_alarm_intr_status(lp_timer_context.dev);
44 }
45 
lp_timer_hal_clear_overflow_intr_status(void)46 void IRAM_ATTR lp_timer_hal_clear_overflow_intr_status(void)
47 {
48     lp_timer_ll_clear_overflow_intr_status(lp_timer_context.dev);
49 }
50