1 // Copyright 2021 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 #include "hal/assert.h"
22 
systimer_hal_init(systimer_hal_context_t * hal)23 void systimer_hal_init(systimer_hal_context_t *hal)
24 {
25     hal->dev = &SYSTIMER;
26     periph_ll_enable_clk_clear_rst(PERIPH_SYSTIMER_MODULE);
27     systimer_ll_enable_clock(hal->dev, true);
28 }
29 
systimer_hal_get_counter_value(systimer_hal_context_t * hal,uint32_t counter_id)30 uint64_t systimer_hal_get_counter_value(systimer_hal_context_t *hal, uint32_t counter_id)
31 {
32     uint32_t lo, lo_start, hi;
33     /* Set the "update" bit and wait for acknowledgment */
34     systimer_ll_counter_snapshot(hal->dev, counter_id);
35     while (!systimer_ll_is_counter_value_valid(hal->dev, counter_id));
36     /* Read LO, HI, then LO again, check that LO returns the same value.
37      * This accounts for the case when an interrupt may happen between reading
38      * HI and LO values, and this function may get called from the ISR.
39      * In this case, the repeated read will return consistent values.
40      */
41     lo_start = systimer_ll_get_counter_value_low(hal->dev, counter_id);
42     do {
43         lo = lo_start;
44         hi = systimer_ll_get_counter_value_high(hal->dev, counter_id);
45         lo_start = systimer_ll_get_counter_value_low(hal->dev, counter_id);
46     } while (lo_start != lo);
47 
48     systimer_counter_value_t result = {
49         .lo = lo,
50         .hi = hi
51     };
52 
53     return result.val;
54 }
55 
systimer_hal_get_time(systimer_hal_context_t * hal,uint32_t counter_id)56 uint64_t systimer_hal_get_time(systimer_hal_context_t *hal, uint32_t counter_id)
57 {
58     return systimer_hal_get_counter_value(hal, counter_id) / SYSTIMER_LL_TICKS_PER_US;
59 }
60 
61 #if SOC_SYSTIMER_ALARM_MISS_COMPENSATE
systimer_hal_set_alarm_target(systimer_hal_context_t * hal,uint32_t alarm_id,uint64_t target)62 void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t target)
63 {
64     systimer_counter_value_t alarm = { .val = target * SYSTIMER_LL_TICKS_PER_US};
65     systimer_ll_enable_alarm(hal->dev, alarm_id, false);
66     systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val);
67     systimer_ll_apply_alarm_value(hal->dev, alarm_id);
68     systimer_ll_enable_alarm(hal->dev, alarm_id, true);
69 }
70 #else
systimer_hal_set_alarm_target(systimer_hal_context_t * hal,uint32_t alarm_id,uint64_t timestamp)71 void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t timestamp)
72 {
73     int64_t offset = SYSTIMER_LL_TICKS_PER_US * 2;
74     uint64_t now_time = systimer_hal_get_counter_value(hal, 0);
75     systimer_counter_value_t alarm = { .val = MAX(timestamp * SYSTIMER_LL_TICKS_PER_US, now_time + offset) };
76     do {
77         systimer_ll_enable_alarm(hal->dev, alarm_id, false);
78         systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val);
79         systimer_ll_enable_alarm(hal->dev, alarm_id, true);
80         now_time = systimer_hal_get_counter_value(hal, 0);
81         int64_t delta = (int64_t)alarm.val - (int64_t)now_time;
82         if (delta <= 0 && !systimer_ll_is_alarm_int_fired(hal->dev, alarm_id)) {
83             // new alarm is less than the counter and the interrupt flag is not set
84             offset += -1 * delta + SYSTIMER_LL_TICKS_PER_US * 2;
85             alarm.val = now_time + offset;
86         } else {
87             // finish if either (alarm > counter) or the interrupt flag is already set.
88             break;
89         }
90     } while (1);
91 }
92 #endif
93 
systimer_hal_set_alarm_period(systimer_hal_context_t * hal,uint32_t alarm_id,uint32_t period)94 void systimer_hal_set_alarm_period(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t period)
95 {
96     systimer_ll_enable_alarm(hal->dev, alarm_id, false);
97     systimer_ll_set_alarm_period(hal->dev, alarm_id, period * SYSTIMER_LL_TICKS_PER_US);
98     systimer_ll_apply_alarm_value(hal->dev, alarm_id);
99     systimer_ll_enable_alarm(hal->dev, alarm_id, true);
100 }
101 
systimer_hal_get_alarm_value(systimer_hal_context_t * hal,uint32_t alarm_id)102 uint64_t systimer_hal_get_alarm_value(systimer_hal_context_t *hal, uint32_t alarm_id)
103 {
104     return systimer_ll_get_alarm_target(hal->dev, alarm_id);
105 }
106 
systimer_hal_enable_alarm_int(systimer_hal_context_t * hal,uint32_t alarm_id)107 void systimer_hal_enable_alarm_int(systimer_hal_context_t *hal, uint32_t alarm_id)
108 {
109     systimer_ll_enable_alarm_int(hal->dev, alarm_id, true);
110 }
111 
systimer_hal_counter_value_advance(systimer_hal_context_t * hal,uint32_t counter_id,int64_t time_us)112 void systimer_hal_counter_value_advance(systimer_hal_context_t *hal, uint32_t counter_id, int64_t time_us)
113 {
114     systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(hal, counter_id) + time_us * SYSTIMER_LL_TICKS_PER_US };
115     systimer_ll_set_counter_value(hal->dev, counter_id, new_count.val);
116     systimer_ll_apply_counter_value(hal->dev, counter_id);
117 }
118 
systimer_hal_enable_counter(systimer_hal_context_t * hal,uint32_t counter_id)119 void systimer_hal_enable_counter(systimer_hal_context_t *hal, uint32_t counter_id)
120 {
121     systimer_ll_enable_counter(hal->dev, counter_id, true);
122 }
123 
systimer_hal_select_alarm_mode(systimer_hal_context_t * hal,uint32_t alarm_id,systimer_alarm_mode_t mode)124 void systimer_hal_select_alarm_mode(systimer_hal_context_t *hal, uint32_t alarm_id, systimer_alarm_mode_t mode)
125 {
126     switch (mode) {
127     case SYSTIMER_ALARM_MODE_ONESHOT:
128         systimer_ll_enable_alarm_oneshot(hal->dev, alarm_id);
129         break;
130     case SYSTIMER_ALARM_MODE_PERIOD:
131         systimer_ll_enable_alarm_period(hal->dev, alarm_id);
132         break;
133     default:
134         break;
135     }
136 }
137 
systimer_hal_connect_alarm_counter(systimer_hal_context_t * hal,uint32_t alarm_id,uint32_t counter_id)138 void systimer_hal_connect_alarm_counter(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t counter_id)
139 {
140     systimer_ll_connect_alarm_counter(hal->dev, alarm_id, counter_id);
141 }
142 
systimer_hal_counter_can_stall_by_cpu(systimer_hal_context_t * hal,uint32_t counter_id,uint32_t cpu_id,bool can)143 void systimer_hal_counter_can_stall_by_cpu(systimer_hal_context_t *hal, uint32_t counter_id, uint32_t cpu_id, bool can)
144 {
145     systimer_ll_counter_can_stall_by_cpu(hal->dev, counter_id, cpu_id, can);
146 }
147 
148 #if !SOC_SYSTIMER_FIXED_TICKS_US
systimer_hal_set_steps_per_tick(systimer_hal_context_t * hal,int clock_source,uint32_t steps)149 void systimer_hal_set_steps_per_tick(systimer_hal_context_t *hal, int clock_source, uint32_t steps)
150 {
151     /* Configure the counter:
152      * - increment by 1 when running from PLL (80 ticks per microsecond),
153      * - increment by 2 when running from XTAL (40 ticks per microsecond).
154      * Note that if the APB frequency is derived from XTAL with divider != 1,
155      * XTAL_STEP needs to be adjusted accordingly. For example, if
156      * the APB frequency is XTAL/4 = 10 MHz, then XTAL_STEP should be set to 8.
157      * This is handled in systimer_hal_on_apb_freq_update function.
158      */
159     switch (clock_source) {
160     case 0:
161         systimer_ll_set_step_for_xtal(hal->dev, steps);
162         break;
163     case 1:
164         systimer_ll_set_step_for_pll(hal->dev, steps);
165     default:
166         break;
167     }
168 }
169 
systimer_hal_on_apb_freq_update(systimer_hal_context_t * hal,uint32_t apb_ticks_per_us)170 void systimer_hal_on_apb_freq_update(systimer_hal_context_t *hal, uint32_t apb_ticks_per_us)
171 {
172     /* If this function was called when switching APB clock to PLL, don't need
173     * do anything: the SYSTIMER_TIMER_PLL_STEP is already correct.
174     * If this was called when switching APB clock to XTAL, need to adjust
175     * XTAL_STEP value accordingly.
176     */
177     if (apb_ticks_per_us != SYSTIMER_LL_TICKS_PER_US) {
178         HAL_ASSERT((SYSTIMER_LL_TICKS_PER_US % apb_ticks_per_us) == 0 && "TICK_PER_US should be divisible by APB frequency (in MHz)");
179         systimer_ll_set_step_for_xtal(hal->dev, SYSTIMER_LL_TICKS_PER_US / apb_ticks_per_us);
180     }
181 }
182 #endif
183