diff --git a/components/esp_timer/src/esp_timer_impl_systimer.c b/components/esp_timer/src/esp_timer_impl_systimer.c index 19c963110b..ead5d24cab 100644 --- a/components/esp_timer/src/esp_timer_impl_systimer.c +++ b/components/esp_timer/src/esp_timer_impl_systimer.c @@ -75,7 +75,7 @@ int64_t IRAM_ATTR esp_timer_impl_get_time(void) if (unlikely(s_alarm_handler == NULL)) { return 0; } - return systimer_hal_get_time(&systimer_hal, SYSTIMER_LL_COUNTER_CLOCK); + return systimer_hal_get_counter_value(&systimer_hal, SYSTIMER_LL_COUNTER_CLOCK) / SYSTIMER_LL_TICKS_PER_US; } int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time"))); diff --git a/components/hal/esp32c3/include/hal/systimer_ll.h b/components/hal/esp32c3/include/hal/systimer_ll.h index e7d50778ab..9aa0c32da5 100644 --- a/components/hal/esp32c3/include/hal/systimer_ll.h +++ b/components/hal/esp32c3/include/hal/systimer_ll.h @@ -23,6 +23,8 @@ #define SYSTIMER_LL_ALARM_OS_TICK_CORE0 (0) // Alarm used for OS tick of CPU core 0 #define SYSTIMER_LL_ALARM_CLOCK (2) // Alarm used for "wallclock" time +#define SYSTIMER_LL_TICKS_PER_US (16) // 16 systimer ticks == 1us + #ifdef __cplusplus extern "C" { #endif diff --git a/components/hal/esp32s2/include/hal/systimer_ll.h b/components/hal/esp32s2/include/hal/systimer_ll.h index 388f00580f..158c4a8aa9 100644 --- a/components/hal/esp32s2/include/hal/systimer_ll.h +++ b/components/hal/esp32s2/include/hal/systimer_ll.h @@ -21,6 +21,8 @@ #define SYSTIMER_LL_COUNTER_CLOCK (0) // Counter used for "wallclock" time #define SYSTIMER_LL_ALARM_CLOCK (2) // Alarm used for "wallclock" time +#define SYSTIMER_LL_TICKS_PER_US (80) // 80 systimer ticks == 1us + #ifdef __cplusplus extern "C" { #endif diff --git a/components/hal/esp32s3/include/hal/systimer_ll.h b/components/hal/esp32s3/include/hal/systimer_ll.h index ba07f4658b..d540b99b37 100644 --- a/components/hal/esp32s3/include/hal/systimer_ll.h +++ b/components/hal/esp32s3/include/hal/systimer_ll.h @@ -24,6 +24,8 @@ #define SYSTIMER_LL_ALARM_OS_TICK_CORE1 (1) // Alarm used for OS tick of CPU core 1 #define SYSTIMER_LL_ALARM_CLOCK (2) // Alarm used for "wallclock" time +#define SYSTIMER_LL_TICKS_PER_US (16) // 16 systimer ticks == 1us + #ifdef __cplusplus extern "C" { #endif diff --git a/components/hal/systimer_hal.c b/components/hal/systimer_hal.c index 2521060308..9ecf8c70ca 100644 --- a/components/hal/systimer_hal.c +++ b/components/hal/systimer_hal.c @@ -19,13 +19,6 @@ #include "hal/systimer_types.h" #include "hal/clk_gate_ll.h" -// Number of timer ticks per microsecond -#if SOC_SYSTIMER_FIXED_TICKS_US -#define SYSTIMER_TICKS_PER_US (SOC_SYSTIMER_FIXED_TICKS_US) -#else -#define SYSTIMER_TICKS_PER_US (80) -#endif - void systimer_hal_init(systimer_hal_context_t *hal) { hal->dev = &SYSTIMER; @@ -61,13 +54,13 @@ uint64_t systimer_hal_get_counter_value(systimer_hal_context_t *hal, uint32_t co uint64_t systimer_hal_get_time(systimer_hal_context_t *hal, uint32_t counter_id) { - return systimer_hal_get_counter_value(hal, counter_id) / SYSTIMER_TICKS_PER_US; + return systimer_hal_get_counter_value(hal, counter_id) / SYSTIMER_LL_TICKS_PER_US; } #if SOC_SYSTIMER_ALARM_MISS_COMPENSATE void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t target) { - systimer_counter_value_t alarm = { .val = target * SYSTIMER_TICKS_PER_US}; + systimer_counter_value_t alarm = { .val = target * SYSTIMER_LL_TICKS_PER_US}; systimer_ll_enable_alarm(hal->dev, alarm_id, false); systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val); systimer_ll_apply_alarm_value(hal->dev, alarm_id); @@ -76,9 +69,9 @@ void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_i #else void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t timestamp) { - int64_t offset = SYSTIMER_TICKS_PER_US * 2; + int64_t offset = SYSTIMER_LL_TICKS_PER_US * 2; uint64_t now_time = systimer_hal_get_counter_value(hal, 0); - systimer_counter_value_t alarm = { .val = MAX(timestamp * SYSTIMER_TICKS_PER_US, now_time + offset) }; + systimer_counter_value_t alarm = { .val = MAX(timestamp * SYSTIMER_LL_TICKS_PER_US, now_time + offset) }; do { systimer_ll_enable_alarm(hal->dev, alarm_id, false); systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val); @@ -87,7 +80,7 @@ void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_i int64_t delta = (int64_t)alarm.val - (int64_t)now_time; if (delta <= 0 && !systimer_ll_is_alarm_int_fired(hal->dev, alarm_id)) { // new alarm is less than the counter and the interrupt flag is not set - offset += -1 * delta + SYSTIMER_TICKS_PER_US * 2; + offset += -1 * delta + SYSTIMER_LL_TICKS_PER_US * 2; alarm.val = now_time + offset; } else { // finish if either (alarm > counter) or the interrupt flag is already set. @@ -100,7 +93,7 @@ void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_i void systimer_hal_set_alarm_period(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t period) { systimer_ll_enable_alarm(hal->dev, alarm_id, false); - systimer_ll_set_alarm_period(hal->dev, alarm_id, period * SYSTIMER_TICKS_PER_US); + systimer_ll_set_alarm_period(hal->dev, alarm_id, period * SYSTIMER_LL_TICKS_PER_US); systimer_ll_apply_alarm_value(hal->dev, alarm_id); systimer_ll_enable_alarm(hal->dev, alarm_id, true); } @@ -117,7 +110,7 @@ void systimer_hal_enable_alarm_int(systimer_hal_context_t *hal, uint32_t alarm_i void systimer_hal_counter_value_advance(systimer_hal_context_t *hal, uint32_t counter_id, int64_t time_us) { - systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(hal, counter_id) + time_us * SYSTIMER_TICKS_PER_US }; + systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(hal, counter_id) + time_us * SYSTIMER_LL_TICKS_PER_US }; systimer_ll_set_counter_value(hal->dev, counter_id, new_count.val); systimer_ll_apply_counter_value(hal->dev, counter_id); } @@ -180,9 +173,9 @@ void systimer_hal_on_apb_freq_update(systimer_hal_context_t *hal, uint32_t apb_t * If this was called when switching APB clock to XTAL, need to adjust * XTAL_STEP value accordingly. */ - if (apb_ticks_per_us != SYSTIMER_TICKS_PER_US) { - assert((SYSTIMER_TICKS_PER_US % apb_ticks_per_us) == 0 && "TICK_PER_US should be divisible by APB frequency (in MHz)"); - systimer_ll_set_step_for_xtal(hal->dev, SYSTIMER_TICKS_PER_US / apb_ticks_per_us); + if (apb_ticks_per_us != SYSTIMER_LL_TICKS_PER_US) { + assert((SYSTIMER_LL_TICKS_PER_US % apb_ticks_per_us) == 0 && "TICK_PER_US should be divisible by APB frequency (in MHz)"); + systimer_ll_set_step_for_xtal(hal->dev, SYSTIMER_LL_TICKS_PER_US / apb_ticks_per_us); } } #endif