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 #include <sys/param.h>
16 #include "soc/soc_caps.h"
17 #include "hal/systimer_hal.h"
18 #include "hal/systimer_ll.h"
19 #include "hal/systimer_types.h"
20 #include "hal/clk_gate_ll.h"
21 
22 #define SYSTIMER_TICKS_PER_US  (16) // Systimer clock source is fixed to 16MHz
23 
systimer_hal_get_counter_value(systimer_counter_id_t counter_id)24 uint64_t systimer_hal_get_counter_value(systimer_counter_id_t counter_id)
25 {
26     uint32_t lo, lo_start, hi;
27     /* Set the "update" bit and wait for acknowledgment */
28     systimer_ll_counter_snapshot(counter_id);
29     while (!systimer_ll_is_counter_value_valid(counter_id));
30     /* Read LO, HI, then LO again, check that LO returns the same value.
31      * This accounts for the case when an interrupt may happen between reading
32      * HI and LO values, and this function may get called from the ISR.
33      * In this case, the repeated read will return consistent values.
34      */
35     lo_start = systimer_ll_get_counter_value_low(counter_id);
36     do {
37         lo = lo_start;
38         hi = systimer_ll_get_counter_value_high(counter_id);
39         lo_start = systimer_ll_get_counter_value_low(counter_id);
40     } while (lo_start != lo);
41 
42     systimer_counter_value_t result = {
43         .lo = lo,
44         .hi = hi
45     };
46 
47     return result.val;
48 }
49 
systimer_hal_get_time(systimer_counter_id_t counter_id)50 uint64_t systimer_hal_get_time(systimer_counter_id_t counter_id)
51 {
52     return systimer_hal_get_counter_value(counter_id) / SYSTIMER_TICKS_PER_US;
53 }
54 
systimer_hal_set_alarm_target(systimer_alarm_id_t alarm_id,uint64_t target)55 void systimer_hal_set_alarm_target(systimer_alarm_id_t alarm_id, uint64_t target)
56 {
57     systimer_counter_value_t alarm = { .val = target * SYSTIMER_TICKS_PER_US};
58     systimer_ll_disable_alarm(alarm_id);
59     systimer_ll_set_alarm_target(alarm_id, alarm.val);
60     systimer_ll_apply_alarm_value(alarm_id);
61     systimer_ll_enable_alarm(alarm_id);
62 }
63 
systimer_hal_set_alarm_period(systimer_alarm_id_t alarm_id,uint32_t period)64 void systimer_hal_set_alarm_period(systimer_alarm_id_t alarm_id, uint32_t period)
65 {
66     systimer_ll_disable_alarm(alarm_id);
67     systimer_ll_set_alarm_period(alarm_id, period * SYSTIMER_TICKS_PER_US);
68     systimer_ll_apply_alarm_value(alarm_id);
69     systimer_ll_enable_alarm(alarm_id);
70 }
71 
systimer_hal_get_alarm_value(systimer_alarm_id_t alarm_id)72 uint64_t systimer_hal_get_alarm_value(systimer_alarm_id_t alarm_id)
73 {
74     return systimer_ll_get_alarm_target(alarm_id);
75 }
76 
systimer_hal_enable_alarm_int(systimer_alarm_id_t alarm_id)77 void systimer_hal_enable_alarm_int(systimer_alarm_id_t alarm_id)
78 {
79     systimer_ll_enable_alarm_int(alarm_id);
80 }
81 
systimer_hal_on_apb_freq_update(uint32_t apb_ticks_per_us)82 void systimer_hal_on_apb_freq_update(uint32_t apb_ticks_per_us)
83 {
84     /* Nothing to do here, SYSTIMER clock is independent of APB clock */
85     (void)apb_ticks_per_us;
86 }
87 
systimer_hal_counter_value_advance(systimer_counter_id_t counter_id,int64_t time_us)88 void systimer_hal_counter_value_advance(systimer_counter_id_t counter_id, int64_t time_us)
89 {
90     systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(counter_id) + time_us * SYSTIMER_TICKS_PER_US };
91     systimer_ll_set_counter_value(counter_id, new_count.val);
92     systimer_ll_apply_counter_value(counter_id);
93 }
94 
systimer_hal_enable_counter(systimer_counter_id_t counter_id)95 void systimer_hal_enable_counter(systimer_counter_id_t counter_id)
96 {
97     systimer_ll_enable_counter(counter_id);
98 }
99 
systimer_hal_init(void)100 void systimer_hal_init(void)
101 {
102     periph_ll_enable_clk_clear_rst(PERIPH_SYSTIMER_MODULE);
103     systimer_ll_enable_clock();
104 }
105 
systimer_hal_select_alarm_mode(systimer_alarm_id_t alarm_id,systimer_alarm_mode_t mode)106 void systimer_hal_select_alarm_mode(systimer_alarm_id_t alarm_id, systimer_alarm_mode_t mode)
107 {
108     switch (mode) {
109     case SYSTIMER_ALARM_MODE_ONESHOT:
110         systimer_ll_enable_alarm_oneshot(alarm_id);
111         break;
112     case SYSTIMER_ALARM_MODE_PERIOD:
113         systimer_ll_enable_alarm_period(alarm_id);
114         break;
115     default:
116         break;
117     }
118 }
119 
systimer_hal_connect_alarm_counter(systimer_alarm_id_t alarm_id,systimer_counter_id_t counter_id)120 void systimer_hal_connect_alarm_counter(systimer_alarm_id_t alarm_id, systimer_counter_id_t counter_id)
121 {
122     systimer_ll_connect_alarm_counter(alarm_id, counter_id);
123 }
124