diff --git a/components/esp32/esp_timer_esp32.c b/components/esp32/esp_timer_esp32.c index 4c21910e8a..1bc4885756 100644 --- a/components/esp32/esp_timer_esp32.c +++ b/components/esp32/esp_timer_esp32.c @@ -311,6 +311,18 @@ void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us) portEXIT_CRITICAL_ISR(&s_time_update_lock); } +void esp_timer_impl_advance(int64_t time_us) +{ + assert(time_us > 0 && "negative adjustments not supported yet"); + + portENTER_CRITICAL(&s_time_update_lock); + uint64_t count = REG_READ(FRC_TIMER_COUNT_REG(1)); + REG_WRITE(FRC_TIMER_LOAD_REG(1), 0); + s_time_base_us += count / s_timer_ticks_per_us + time_us; + esp_timer_impl_set_alarm(esp_timer_get_next_alarm()); + portEXIT_CRITICAL(&s_time_update_lock); +} + esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler) { s_alarm_handler = alarm_handler; diff --git a/components/esp32/esp_timer_impl.h b/components/esp32/esp_timer_impl.h index 7d49e3eeb0..9c4642c474 100644 --- a/components/esp32/esp_timer_impl.h +++ b/components/esp32/esp_timer_impl.h @@ -60,6 +60,15 @@ void esp_timer_impl_set_alarm(uint64_t timestamp); */ void esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us); +/** + * @brief Adjust current esp_timer time by a certain value + * + * Called from light sleep code to synchronize esp_timer time with RTC time. + * + * @param time_us adjustment to apply to esp_timer time, in microseconds + */ +void esp_timer_impl_advance(int64_t time_us); + /** * @brief Get time, in microseconds, since esp_timer_impl_init was called * @return timestamp in microseconds