1616#include "esp_log.h"
1717#include "esp_private/esp_clk.h"
1818#include "esp_private/periph_ctrl.h"
19+ #include "esp_private/systimer.h"
1920#include "soc/soc.h"
2021#include "soc/timer_group_reg.h"
2122#include "soc/rtc.h"
3233 * The timer can be configured to produce an edge or a level interrupt.
3334 */
3435
35- /* Selects which Timer Group peripheral to use */
36- #define LACT_MODULE 0
37-
3836#if LACT_MODULE == 0
3937#define INTR_SOURCE_LACT ETS_TG0_LACT_LEVEL_INTR_SOURCE
4038#define PERIPH_LACT PERIPH_TIMG0_MODULE
4543#error "Incorrect the number of LACT module (only 0 or 1)"
4644#endif
4745
48- /* Desired number of timer ticks per microsecond.
49- * This value should be small enough so that all possible APB frequencies
50- * could be divided by it without remainder.
51- * On the other hand, the smaller this value is, the longer we need to wait
52- * after setting UPDATE_REG before the timer value can be read.
53- * If TICKS_PER_US == 1, then we need to wait up to 1 microsecond, which
54- * makes esp_timer_impl_get_time function take too much time.
55- * The value TICKS_PER_US == 2 allows for most of the APB frequencies, and
56- * allows reading the counter quickly enough.
57- */
58- #define TICKS_PER_US 2
59-
6046/* Shorter register names, used in this file */
6147#define CONFIG_REG (TIMG_LACTCONFIG_REG(LACT_MODULE))
6248#define RTC_STEP_REG (TIMG_LACTRTC_REG(LACT_MODULE))
@@ -139,7 +125,7 @@ uint64_t ESP_TIMER_IRAM_ATTR esp_timer_impl_get_counter_reg(void)
139125
140126int64_t ESP_TIMER_IRAM_ATTR esp_timer_impl_get_time (void )
141127{
142- return esp_timer_impl_get_counter_reg () / TICKS_PER_US ;
128+ return esp_timer_impl_get_counter_reg () / LACT_TICKS_PER_US ;
143129}
144130
145131int64_t esp_timer_get_time (void ) __attribute__((alias ("esp_timer_impl_get_time" )));
@@ -151,9 +137,9 @@ void ESP_TIMER_IRAM_ATTR esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigne
151137 timestamp_id [alarm_id ] = timestamp ;
152138 timestamp = MIN (timestamp_id [0 ], timestamp_id [1 ]);
153139 if (timestamp != UINT64_MAX ) {
154- int64_t offset = TICKS_PER_US * 2 ;
140+ int64_t offset = LACT_TICKS_PER_US * 2 ;
155141 uint64_t now_time = esp_timer_impl_get_counter_reg ();
156- timer_64b_reg_t alarm = { .val = MAX (timestamp * TICKS_PER_US , now_time + offset ) };
142+ timer_64b_reg_t alarm = { .val = MAX (timestamp * LACT_TICKS_PER_US , now_time + offset ) };
157143 do {
158144 REG_CLR_BIT (CONFIG_REG , TIMG_LACT_ALARM_EN );
159145 REG_WRITE (ALARM_LO_REG , alarm .lo );
@@ -163,7 +149,7 @@ void ESP_TIMER_IRAM_ATTR esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigne
163149 int64_t delta = (int64_t )alarm .val - (int64_t )now_time ;
164150 if (delta <= 0 && REG_GET_FIELD (INT_ST_REG , TIMG_LACT_INT_ST ) == 0 ) {
165151 // new alarm is less than the counter and the interrupt flag is not set
166- offset += llabs (delta ) + TICKS_PER_US * 2 ;
152+ offset += llabs (delta ) + LACT_TICKS_PER_US * 2 ;
167153 alarm .val = now_time + offset ;
168154 } else {
169155 // finish if either (alarm > counter) or the interrupt flag is already set.
@@ -220,19 +206,10 @@ static void ESP_TIMER_IRAM_ATTR timer_alarm_isr(void *arg)
220206#endif // ISR_HANDLERS != 1
221207}
222208
223- void ESP_TIMER_IRAM_ATTR esp_timer_impl_update_apb_freq (uint32_t apb_ticks_per_us )
224- {
225- portENTER_CRITICAL_SAFE (& s_time_update_lock );
226- assert (apb_ticks_per_us >= 3 && "divider value too low" );
227- assert (apb_ticks_per_us % TICKS_PER_US == 0 && "APB frequency (in MHz) should be divisible by TICK_PER_US" );
228- REG_SET_FIELD (CONFIG_REG , TIMG_LACT_DIVIDER , apb_ticks_per_us / TICKS_PER_US );
229- portEXIT_CRITICAL_SAFE (& s_time_update_lock );
230- }
231-
232209void esp_timer_impl_set (uint64_t new_us )
233210{
234211 portENTER_CRITICAL (& s_time_update_lock );
235- timer_64b_reg_t dst = { .val = new_us * TICKS_PER_US };
212+ timer_64b_reg_t dst = { .val = new_us * LACT_TICKS_PER_US };
236213 REG_WRITE (LOAD_LO_REG , dst .lo );
237214 REG_WRITE (LOAD_HI_REG , dst .hi );
238215 REG_WRITE (LOAD_REG , 1 );
@@ -261,7 +238,7 @@ esp_err_t esp_timer_impl_early_init(void)
261238 REG_WRITE (ALARM_HI_REG , UINT32_MAX );
262239 REG_WRITE (LOAD_REG , 1 );
263240 REG_SET_BIT (INT_CLR_REG , TIMG_LACT_INT_CLR );
264- REG_SET_FIELD (CONFIG_REG , TIMG_LACT_DIVIDER , APB_CLK_FREQ / 1000000 / TICKS_PER_US );
241+ REG_SET_FIELD (CONFIG_REG , TIMG_LACT_DIVIDER , APB_CLK_FREQ / 1000000 / LACT_TICKS_PER_US );
265242 REG_SET_BIT (CONFIG_REG , TIMG_LACT_INCREASE |
266243 TIMG_LACT_LEVEL_INT_EN |
267244 TIMG_LACT_EN );
@@ -299,12 +276,10 @@ esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
299276 * will not cause issues in practice.
300277 */
301278 REG_SET_BIT (INT_ENA_REG , TIMG_LACT_INT_ENA );
302-
303- esp_timer_impl_update_apb_freq (esp_clk_apb_freq () / 1000000 );
304-
279+ timer_ll_set_lact_clock_prescale (TIMER_LL_GET_HW (LACT_MODULE ), esp_clk_apb_freq () / MHZ / LACT_TICKS_PER_US );
305280 // Set the step for the sleep mode when the timer will work
306281 // from a slow_clk frequency instead of the APB frequency.
307- uint32_t slowclk_ticks_per_us = esp_clk_slowclk_cal_get () * TICKS_PER_US ;
282+ uint32_t slowclk_ticks_per_us = esp_clk_slowclk_cal_get () * LACT_TICKS_PER_US ;
308283 REG_SET_FIELD (RTC_STEP_REG , TIMG_LACT_RTC_STEP_LEN , slowclk_ticks_per_us );
309284 }
310285
@@ -347,6 +322,5 @@ uint64_t esp_timer_impl_get_alarm_reg(void)
347322 return alarm .val ;
348323}
349324
350- void esp_timer_private_update_apb_freq (uint32_t apb_ticks_per_us ) __attribute__((alias ("esp_timer_impl_update_apb_freq" )));
351325void esp_timer_private_set (uint64_t new_us ) __attribute__((alias ("esp_timer_impl_set" )));
352326void esp_timer_private_advance (int64_t time_diff_us ) __attribute__((alias ("esp_timer_impl_advance" )));
0 commit comments