1 // Copyright 2020 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 #pragma once
16 
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include "soc/soc_caps.h"
20 #include "soc/systimer_struct.h"
21 #include "hal/systimer_types.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef struct {
28     systimer_dev_t *dev;
29 } systimer_hal_context_t;
30 
31 /**
32  * @brief initialize systimer in HAL layer
33  */
34 void systimer_hal_init(systimer_hal_context_t *hal);
35 
36 /**
37  * @brief enable systimer counter
38  */
39 void systimer_hal_enable_counter(systimer_hal_context_t *hal, uint32_t counter_id);
40 
41 /**
42  * @brief get current counter value
43  */
44 uint64_t systimer_hal_get_counter_value(systimer_hal_context_t *hal, uint32_t counter_id);
45 
46 /**
47  * @brief get current time (in microseconds)
48  */
49 uint64_t systimer_hal_get_time(systimer_hal_context_t *hal, uint32_t counter_id);
50 
51 /*
52  * @brief set alarm target value (used in one-shot mode)
53  */
54 void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t target);
55 
56 /**
57  * @brief set alarm period value (used in period mode)
58  */
59 void systimer_hal_set_alarm_period(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t period);
60 
61 /**
62  * @brief get alarm time
63  */
64 uint64_t systimer_hal_get_alarm_value(systimer_hal_context_t *hal, uint32_t alarm_id);
65 
66 /**
67  * @brief enable alarm interrupt
68  */
69 void systimer_hal_enable_alarm_int(systimer_hal_context_t *hal, uint32_t alarm_id);
70 
71 /**
72  * @brief select alarm mode
73  */
74 void systimer_hal_select_alarm_mode(systimer_hal_context_t *hal, uint32_t alarm_id, systimer_alarm_mode_t mode);
75 
76 /**
77  * @brief update systimer step when apb clock gets changed
78  */
79 void systimer_hal_on_apb_freq_update(systimer_hal_context_t *hal, uint32_t apb_ticks_per_us);
80 
81 /**
82  * @brief move systimer counter value forward or backward
83  */
84 void systimer_hal_counter_value_advance(systimer_hal_context_t *hal, uint32_t counter_id, int64_t time_us);
85 
86 /**
87  * @brief  connect alarm unit to selected counter
88  */
89 void systimer_hal_connect_alarm_counter(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t counter_id);
90 
91 /**
92  * @brief  set if a counter should be stalled when CPU is halted by the debugger
93  */
94 void systimer_hal_counter_can_stall_by_cpu(systimer_hal_context_t *hal, uint32_t counter_id, uint32_t cpu_id, bool can);
95 
96 #if !SOC_SYSTIMER_FIXED_TICKS_US
97 /**
98  * @brief set increase steps for systimer counter on different clock source
99  */
100 void systimer_hal_set_steps_per_tick(systimer_hal_context_t *hal, int clock_source, uint32_t steps);
101 #endif
102 
103 #ifdef __cplusplus
104 }
105 #endif
106