esp_system: Adds sync of FRC & RTC counters in esp_restart

In case when FRC and RTC counters are very different then
the need to sync them before to restart the ESP
to get the correct system time after reboot.
This commit is contained in:
KonstantinKondrashov
2020-12-30 21:24:31 +08:00
parent 7b922bb8c6
commit c19b37d2a9
3 changed files with 119 additions and 1 deletions

View File

@@ -19,6 +19,9 @@
#if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
#include "esp_timer.h"
#include "esp_timer_impl.h"
#include "esp_system.h"
#include "esp_newlib.h"
#include "esp_log.h"
#include "esp_private/startup_internal.h"
@@ -34,6 +37,8 @@
#include "esp32h2/rtc.h"
#endif
__attribute__((unused)) static const char* TAG = "system_time";
// Correction for underlying timer to keep definition
// of system time consistent.
static int64_t s_correction_us = 0;
@@ -41,6 +46,12 @@ static int64_t s_correction_us = 0;
void esp_timer_impl_init_system_time(void)
{
s_correction_us = esp_rtc_get_time_us() - g_startup_time - esp_timer_impl_get_time();
#if defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
esp_err_t err = esp_register_shutdown_handler(esp_sync_counters_rtc_and_frc);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Register shutdown handler failed, err = 0x%x", err);
}
#endif
}
int64_t IRAM_ATTR esp_system_get_time(void)