time: Fix gettimeofday for ESP32-S3

This commit is contained in:
Konstantin Kondrashov
2020-10-20 14:09:32 +08:00
committed by Angus Gratton
parent a416452657
commit 9386cafbc3
10 changed files with 58 additions and 69 deletions

View File

@@ -41,69 +41,55 @@
#include "esp32s3/rtc.h"
#endif
#if defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC ) \
|| defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 ) \
|| defined( CONFIG_ESP32S2_TIME_SYSCALL_USE_RTC ) \
|| defined( CONFIG_ESP32S2_TIME_SYSCALL_USE_RTC_FRC1 ) \
|| defined( CONFIG_ESP32S3_TIME_SYSCALL_USE_RTC ) \
|| defined( CONFIG_ESP32S3_TIME_SYSCALL_USE_RTC_FRC1 )
#define WITH_RTC 1
#endif
#if defined( CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 ) \
|| defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 ) \
|| defined( CONFIG_ESP32S2_TIME_SYSCALL_USE_FRC1 ) \
|| defined( CONFIG_ESP32S2_TIME_SYSCALL_USE_RTC_FRC1 )
#define WITH_FRC 1
#endif
// Offset between FRC timer and the RTC.
// Initialized after reset or light sleep.
#if defined(WITH_RTC) && defined(WITH_FRC)
#if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER)
uint64_t s_microseconds_offset;
#endif
#ifndef WITH_RTC
#ifndef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
static uint64_t s_boot_time; // when RTC is used to persist time, two RTC_STORE registers are used to store boot time instead
#endif
static _lock_t s_boot_time_lock;
#if defined( WITH_FRC ) || defined( WITH_RTC )
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
uint64_t esp_time_impl_get_time_since_boot(void)
{
uint64_t microseconds = 0;
#ifdef WITH_FRC
#ifdef WITH_RTC
#ifdef CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
microseconds = s_microseconds_offset + esp_system_get_time();
#else
microseconds = esp_system_get_time();
#endif // WITH_RTC
#elif defined(WITH_RTC)
#endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
#elif defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
microseconds = esp_rtc_get_time_us();
#endif // WITH_FRC
#endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
return microseconds;
}
uint64_t esp_time_impl_get_time(void)
{
uint64_t microseconds = 0;
#if defined( WITH_FRC )
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
microseconds = esp_system_get_time();
#elif defined( WITH_RTC )
#elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
microseconds = esp_rtc_get_time_us();
#endif // WITH_FRC
#endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
return microseconds;
}
#endif // defined( WITH_FRC ) || defined( WITH_RTC )
#endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
void esp_time_impl_set_boot_time(uint64_t time_us)
{
_lock_acquire(&s_boot_time_lock);
#ifdef WITH_RTC
#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
REG_WRITE(RTC_BOOT_TIME_LOW_REG, (uint32_t) (time_us & 0xffffffff));
REG_WRITE(RTC_BOOT_TIME_HIGH_REG, (uint32_t) (time_us >> 32));
#else
@@ -114,7 +100,7 @@ void esp_time_impl_set_boot_time(uint64_t time_us)
uint64_t esp_clk_rtc_time(void)
{
#ifdef WITH_RTC
#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
return esp_rtc_get_time_us();
#else
return 0;
@@ -125,7 +111,7 @@ uint64_t esp_time_impl_get_boot_time(void)
{
uint64_t result;
_lock_acquire(&s_boot_time_lock);
#ifdef WITH_RTC
#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
result = ((uint64_t) REG_READ(RTC_BOOT_TIME_LOW_REG)) + (((uint64_t) REG_READ(RTC_BOOT_TIME_HIGH_REG)) << 32);
#else
result = s_boot_time;
@@ -162,7 +148,7 @@ uint64_t esp_rtc_get_time_us(void)
void esp_clk_slowclk_cal_set(uint32_t new_cal)
{
#if defined(WITH_RTC)
#if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
/* To force monotonic time values even when clock calibration value changes,
* we adjust boot time, given current time and the new calibration value:
* T = boot_time_old + cur_cal * ticks / 2^19
@@ -176,21 +162,21 @@ void esp_clk_slowclk_cal_set(uint32_t new_cal)
int64_t boot_time_diff = ticks * cal_diff / (1LL << RTC_CLK_CAL_FRACT);
uint64_t boot_time_adj = esp_time_impl_get_boot_time() + boot_time_diff;
esp_time_impl_set_boot_time(boot_time_adj);
#endif // WITH_RTC
#endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal);
}
void esp_set_time_from_rtc(void)
{
#if defined( WITH_FRC ) && defined( WITH_RTC )
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
// initialize time from RTC clock
s_microseconds_offset = esp_rtc_get_time_us() - esp_system_get_time();
#endif // WITH_FRC && WITH_RTC
#endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER && CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
}
void esp_sync_counters_rtc_and_frc(void)
{
#if defined( WITH_FRC ) && defined( WITH_RTC )
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
struct timeval tv;
gettimeofday(&tv, NULL);
settimeofday(&tv, NULL);