Merge branch 'feature/esp32s3_support_gettimeofday' into 'master'

time: Fix gettimeofday for ESP32-S3

See merge request espressif/esp-idf!10871
This commit is contained in:
Angus Gratton 2020-10-20 14:09:36 +08:00
commit e5f06d7f47
10 changed files with 58 additions and 69 deletions

View File

@ -523,14 +523,17 @@ menu "ESP32-specific"
config ESP32_TIME_SYSCALL_USE_RTC_FRC1
bool "RTC and high-resolution timer"
select ESP_TIMER_RTC_USE
select ESP_TIME_FUNCS_USE_RTC_TIMER
select ESP_TIME_FUNCS_USE_ESP_TIMER
config ESP32_TIME_SYSCALL_USE_RTC
bool "RTC"
select ESP_TIMER_RTC_USE
select ESP_TIME_FUNCS_USE_RTC_TIMER
config ESP32_TIME_SYSCALL_USE_FRC1
bool "High-resolution timer"
select ESP_TIME_FUNCS_USE_ESP_TIMER
config ESP32_TIME_SYSCALL_USE_NONE
bool "None"
select ESP_TIME_FUNCS_USE_NONE
endchoice
choice ESP32_RTC_CLK_SRC

View File

@ -392,14 +392,17 @@ menu "ESP32S2-specific"
config ESP32S2_TIME_SYSCALL_USE_RTC_FRC1
bool "RTC and high-resolution timer"
select ESP_TIMER_RTC_USE
select ESP_TIME_FUNCS_USE_RTC_TIMER
select ESP_TIME_FUNCS_USE_ESP_TIMER
config ESP32S2_TIME_SYSCALL_USE_RTC
bool "RTC"
select ESP_TIMER_RTC_USE
select ESP_TIME_FUNCS_USE_RTC_TIMER
config ESP32S2_TIME_SYSCALL_USE_FRC1
bool "High-resolution timer"
select ESP_TIME_FUNCS_USE_ESP_TIMER
config ESP32S2_TIME_SYSCALL_USE_NONE
bool "None"
select ESP_TIME_FUNCS_USE_NONE
endchoice
choice ESP32S2_RTC_CLK_SRC

View File

@ -430,14 +430,17 @@ menu "ESP32S3-Specific"
config ESP32S3_TIME_SYSCALL_USE_RTC_FRC1
bool "RTC and high-resolution timer"
select ESP_TIMER_RTC_USE
select ESP_TIME_FUNCS_USE_RTC_TIMER
select ESP_TIME_FUNCS_USE_ESP_TIMER
config ESP32S3_TIME_SYSCALL_USE_RTC
bool "RTC"
select ESP_TIMER_RTC_USE
select ESP_TIME_FUNCS_USE_RTC_TIMER
config ESP32S3_TIME_SYSCALL_USE_FRC1
bool "High-resolution timer"
select ESP_TIME_FUNCS_USE_ESP_TIMER
config ESP32S3_TIME_SYSCALL_USE_NONE
bool "None"
select ESP_TIME_FUNCS_USE_NONE
endchoice
choice ESP32S3_RTC_CLK_SRC

View File

@ -23,7 +23,7 @@ menu "Power Management"
config PM_USE_RTC_TIMER_REF
bool "Use RTC timer to prevent time drift (EXPERIMENTAL)"
depends on (PM_ENABLE && ESP_TIMER_IMPL_FRC2 && ESP_TIMER_RTC_USE)
depends on (PM_ENABLE && ESP_TIMER_IMPL_FRC2 && ESP_TIME_FUNCS_USE_RTC_TIMER)
default n
help
When APB clock frequency changes, high-resolution timer (esp_timer)

View File

@ -9,7 +9,13 @@ menu "High resolution timer (esp_timer)"
This option has some effect on timer performance and the amount of memory used for timer
storage, and should only be used for debugging/testing purposes.
config ESP_TIMER_RTC_USE # [refactor-todo] remove when timekeeping and persistence are separate
config ESP_TIME_FUNCS_USE_RTC_TIMER # [refactor-todo] remove when timekeeping and persistence are separate
bool
config ESP_TIME_FUNCS_USE_ESP_TIMER # [refactor-todo] remove when timekeeping and persistence are separate
bool
config ESP_TIME_FUNCS_USE_NONE # [refactor-todo] remove when timekeeping and persistence are separate
bool
config ESP_TIMER_TASK_STACK_SIZE

View File

@ -36,17 +36,12 @@
#include "esp32/rtc.h"
#elif CONFIG_IDF_TARGET_ESP32S2
#include "esp32s2/rtc.h"
#elif CONFIG_IDF_TARGET_ESP32S3
#include "esp32s3/rtc.h"
#endif
#include "sdkconfig.h"
#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
#ifdef CONFIG_ESP_TIMER_PROFILING
#define WITH_PROFILING 1
#endif
@ -396,7 +391,7 @@ esp_err_t esp_timer_init(void)
goto out;
}
#if WITH_FRC
#if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
// [refactor-todo] this logic, "esp_rtc_get_time_us() - g_startup_time", is also
// the weak definition of esp_system_get_time; find a way to remove this duplication.
esp_timer_private_advance(esp_rtc_get_time_us() - g_startup_time);
@ -531,7 +526,7 @@ int64_t IRAM_ATTR esp_timer_get_next_alarm(void)
// Provides strong definition for system time functions relied upon
// by core components.
#if WITH_FRC
#if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
int64_t IRAM_ATTR esp_system_get_time(void)
{
return esp_timer_get_time();

View File

@ -296,7 +296,7 @@ menu "mbedTLS"
config MBEDTLS_HAVE_TIME
bool "Enable mbedtls time"
depends on !ESP32_TIME_SYSCALL_USE_NONE
depends on !ESP_TIME_FUNCS_USE_NONE
default y
help
System has time.h and time().

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);

View File

@ -336,25 +336,18 @@ TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=35]")
}
#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 )
#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
void test_posix_timers_clock (void)
{
#ifndef _POSIX_TIMERS
TEST_ASSERT_MESSAGE(false, "_POSIX_TIMERS - is not defined");
#endif
#if defined( WITH_FRC )
printf("WITH_FRC ");
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
printf("CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ");
#endif
#if defined( WITH_RTC )
printf("WITH_RTC ");
#if defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
printf("CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER ");
#endif
#ifdef CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
@ -371,7 +364,7 @@ void test_posix_timers_clock (void)
TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, NULL) == -1);
TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, NULL) == -1);
#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 now = {0};
now.tv_sec = 10L;
now.tv_usec = 100000L;
@ -398,7 +391,7 @@ void test_posix_timers_clock (void)
TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
uint64_t delta_monotonic_us = 0;
#if defined( WITH_FRC )
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
TEST_ASSERT_EQUAL_INT(1000, ts.tv_nsec);
@ -410,7 +403,7 @@ void test_posix_timers_clock (void)
TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
#elif defined( WITH_RTC )
#elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
TEST_ASSERT(clock_getres(CLOCK_REALTIME, &ts) == 0);
TEST_ASSERT_EQUAL_INT(1000000000L / rtc_clk_slow_freq_get_hz(), ts.tv_nsec);
@ -422,7 +415,7 @@ void test_posix_timers_clock (void)
TEST_ASSERT(delta_monotonic_us > 0 || delta_monotonic_us == 0);
TEST_ASSERT_INT_WITHIN(5000L, 0, delta_monotonic_us);
#endif // WITH_FRC
#endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
#else
struct timespec ts = {0};
@ -433,7 +426,7 @@ void test_posix_timers_clock (void)
TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1);
TEST_ASSERT(clock_gettime(CLOCK_MONOTONIC, &ts) == -1);
TEST_ASSERT(clock_getres(CLOCK_MONOTONIC, &ts) == -1);
#endif // defined( WITH_FRC ) || defined( WITH_RTC )
#endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
}
TEST_CASE("test posix_timers clock_... functions", "[newlib]")

View File

@ -42,7 +42,7 @@ _Static_assert(sizeof(time_t) == 8, "The toolchain does not support time_t wide
_Static_assert(sizeof(time_t) == 4, "The toolchain supports time_t wide 64-bits. Please enable CONFIG_SDK_TOOLCHAIN_SUPPORTS_TIME_WIDE_64_BITS.");
#endif
#if !CONFIG_ESP32_TIME_SYSCALL_USE_NONE && !CONFIG_ESP32S2_TIME_SYSCALL_USE_NONE
#if !CONFIG_ESP_TIME_FUNCS_USE_NONE
#define IMPL_NEWLIB_TIME_FUNCS 1
#endif