From a395a00d2ce0a02ca64273ad37816dc24a6ddc8e Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Tue, 23 Jun 2020 11:53:58 +0800 Subject: [PATCH 1/5] newlib: separate low-level code in time.c implementation --- components/newlib/CMakeLists.txt | 3 + components/newlib/component.mk | 3 + .../newlib/platform_include/esp_newlib.h | 5 + components/newlib/port/CMakeLists.txt | 1 + components/newlib/port/esp_time_impl.c | 207 +++++++++++ .../newlib/priv_include/esp_time_impl.h | 26 ++ components/newlib/test/test_time.c | 11 +- components/newlib/time.c | 330 ++++-------------- 8 files changed, 331 insertions(+), 255 deletions(-) create mode 100644 components/newlib/port/CMakeLists.txt create mode 100644 components/newlib/port/esp_time_impl.c create mode 100644 components/newlib/priv_include/esp_time_impl.h diff --git a/components/newlib/CMakeLists.txt b/components/newlib/CMakeLists.txt index cac5347de2..8a3463b63d 100644 --- a/components/newlib/CMakeLists.txt +++ b/components/newlib/CMakeLists.txt @@ -20,6 +20,7 @@ list(APPEND ldfragments newlib.lf) idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "${include_dirs}" + PRIV_INCLUDE_DIRS priv_include PRIV_REQUIRES soc esp_timer LDFRAGMENTS "${ldfragments}") @@ -40,3 +41,5 @@ target_link_libraries(${COMPONENT_LIB} INTERFACE "${EXTRA_LINK_FLAGS}") if(CONFIG_NEWLIB_NANO_FORMAT) target_link_libraries(${COMPONENT_LIB} INTERFACE "--specs=nano.specs") endif() + +add_subdirectory(port) diff --git a/components/newlib/component.mk b/components/newlib/component.mk index eb66a19e15..d86f3a5ebc 100644 --- a/components/newlib/component.mk +++ b/components/newlib/component.mk @@ -12,6 +12,9 @@ ifdef CONFIG_SPIRAM_CACHE_WORKAROUND COMPONENT_ADD_LDFRAGMENTS := esp32-spiram-rom-functions-c.lf endif +COMPONENT_PRIV_INCLUDEDIRS := priv_include +COMPONENT_SRCDIRS := . port + # Forces the linker to include locks, heap, and syscalls from this component, # instead of the implementations provided by newlib. COMPONENT_ADD_LDFLAGS += -u newlib_include_locks_impl diff --git a/components/newlib/platform_include/esp_newlib.h b/components/newlib/platform_include/esp_newlib.h index 11658776a6..c43d6ec204 100644 --- a/components/newlib/platform_include/esp_newlib.h +++ b/components/newlib/platform_include/esp_newlib.h @@ -17,6 +17,11 @@ #include +/* + * Initialize newlib time functions + */ +void esp_newlib_time_init(void); + /** * Replacement for newlib's _REENT_INIT_PTR and __sinit. * diff --git a/components/newlib/port/CMakeLists.txt b/components/newlib/port/CMakeLists.txt new file mode 100644 index 0000000000..adac67103b --- /dev/null +++ b/components/newlib/port/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(${COMPONENT_LIB} PRIVATE "${CMAKE_CURRENT_LIST_DIR}/esp_time_impl.c") \ No newline at end of file diff --git a/components/newlib/port/esp_time_impl.c b/components/newlib/port/esp_time_impl.c new file mode 100644 index 0000000000..4a7a800565 --- /dev/null +++ b/components/newlib/port/esp_time_impl.c @@ -0,0 +1,207 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include +#include +#include + +#include "esp_timer.h" + +#include "esp_system.h" + +#include "soc/spinlock.h" +#include "soc/rtc.h" +#include "esp_rom_sys.h" + +#include "esp_time_impl.h" + +#include "sdkconfig.h" + +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/rtc.h" +#include "esp32/clk.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/rtc.h" +#include "esp32s2/clk.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 ) +#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) +uint64_t s_microseconds_offset; +#endif + +#ifndef WITH_RTC +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 spinlock_t s_time_lock = SPINLOCK_INITIALIZER; + +#ifdef WITH_RTC +static uint64_t get_rtc_time_us(void) +{ + const uint64_t ticks = rtc_time_get(); + const uint32_t cal = esp_clk_slowclk_cal_get(); + /* RTC counter result is up to 2^48, calibration factor is up to 2^24, + * for a 32kHz clock. We need to calculate (assuming no overflow): + * (ticks * cal) >> RTC_CLK_CAL_FRACT + * + * An overflow in the (ticks * cal) multiplication would cause time to + * wrap around after approximately 13 days, which is probably not enough + * for some applications. + * Therefore multiplication is split into two terms, for the lower 32-bit + * and the upper 16-bit parts of "ticks", i.e.: + * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT + */ + const uint64_t ticks_low = ticks & UINT32_MAX; + const uint64_t ticks_high = ticks >> 32; + return ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) + + ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT)); +} +#endif // WITH_RTC + +#if defined( WITH_FRC ) || defined( WITH_RTC ) +uint64_t esp_time_impl_get_time_since_boot(void) +{ + uint64_t microseconds = 0; + +#ifdef WITH_FRC +#ifdef WITH_RTC + microseconds = s_microseconds_offset + esp_timer_get_time(); +#else + microseconds = esp_timer_get_time(); +#endif // WITH_RTC +#elif defined(WITH_RTC) + microseconds = get_rtc_time_us(); +#endif // WITH_FRC + return microseconds; +} + +uint64_t esp_time_impl_get_time(void) +{ +#if defined( WITH_FRC ) + return esp_timer_get_time(); +#elif defined( WITH_RTC ) + return get_rtc_time_us(); +#endif // WITH_FRC +} +#endif // defined( WITH_FRC ) || defined( WITH_RTC ) + + +void esp_time_impl_set_boot_time(uint64_t time_us) +{ + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); +#ifdef WITH_RTC + 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 + s_boot_time = time_us; +#endif + spinlock_release(&s_time_lock); +} + +uint64_t esp_clk_rtc_time(void) +{ +#ifdef WITH_RTC + return esp_rtc_get_time_us(); +#else + return 0; +#endif +} + +uint64_t esp_time_impl_get_boot_time(void) +{ + uint64_t result; + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); +#ifdef WITH_RTC + 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; +#endif + spinlock_release(&s_time_lock); + return result; +} + +uint32_t esp_clk_slowclk_cal_get(void) +{ + return REG_READ(RTC_SLOW_CLK_CAL_REG); +} + +void esp_clk_slowclk_cal_set(uint32_t new_cal) +{ +#if defined(WITH_RTC) + /* 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 + * T = boot_time_adj + new_cal * ticks / 2^19 + * which results in: + * boot_time_adj = boot_time_old + ticks * (cur_cal - new_cal) / 2^19 + */ + const int64_t ticks = (int64_t) rtc_time_get(); + const uint32_t cur_cal = REG_READ(RTC_SLOW_CLK_CAL_REG); + int32_t cal_diff = (int32_t) (cur_cal - 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 + REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal); +} + +void esp_set_time_from_rtc(void) +{ +#if defined( WITH_FRC ) && defined( WITH_RTC ) + // initialize time from RTC clock + s_microseconds_offset = get_rtc_time_us() - esp_timer_get_time(); +#endif // WITH_FRC && WITH_RTC +} + +void esp_sync_counters_rtc_and_frc(void) +{ +#if defined( WITH_FRC ) && defined( WITH_RTC ) + struct timeval tv; + gettimeofday(&tv, NULL); + settimeofday(&tv, NULL); + int64_t s_microseconds_offset_cur = get_rtc_time_us() - esp_timer_get_time(); + esp_time_impl_set_boot_time(esp_time_impl_get_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur)); +#endif +} + +void esp_time_impl_init(void) +{ + esp_set_time_from_rtc(); +} + +uint32_t esp_time_impl_get_time_resolution(void) +{ +#if defined( WITH_FRC ) + return 1L; +#elif defined( WITH_RTC ) + uint32_t rtc_freq = rtc_clk_slow_freq_get_hz(); + assert(rtc_freq != 0); + return 1000000L / rtc_freq; +#endif // WITH_FRC +} \ No newline at end of file diff --git a/components/newlib/priv_include/esp_time_impl.h b/components/newlib/priv_include/esp_time_impl.h new file mode 100644 index 0000000000..638e692911 --- /dev/null +++ b/components/newlib/priv_include/esp_time_impl.h @@ -0,0 +1,26 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#pragma once + +void esp_time_impl_init(void); + +uint64_t esp_time_impl_get_time(void); + +uint64_t esp_time_impl_get_time_since_boot(void); + +uint32_t esp_time_impl_get_time_resolution(void); + +void esp_time_impl_set_boot_time(uint64_t t); + +uint64_t esp_time_impl_get_boot_time(void); \ No newline at end of file diff --git a/components/newlib/test/test_time.c b/components/newlib/test/test_time.c index 8a761f9f4b..d5aa58c097 100644 --- a/components/newlib/test/test_time.c +++ b/components/newlib/test/test_time.c @@ -13,6 +13,13 @@ #include "test_utils.h" #include "esp_log.h" #include "esp_rom_sys.h" +#include "esp_system.h" + +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/clk.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/clk.h" +#endif #if portNUM_PROCESSORS == 2 @@ -379,8 +386,8 @@ void test_posix_timers_clock (void) ts.tv_nsec = 100000000L; TEST_ASSERT(clock_settime(CLOCK_REALTIME, &ts) == 0); TEST_ASSERT(gettimeofday(&now, NULL) == 0); - TEST_ASSERT(now.tv_sec == ts.tv_sec); - TEST_ASSERT_INT_WITHIN(5000L, now.tv_usec, ts.tv_nsec / 1000L); + TEST_ASSERT_EQUAL(ts.tv_sec, now.tv_sec); + TEST_ASSERT_INT_WITHIN(5000L, ts.tv_nsec / 1000L, now.tv_usec); TEST_ASSERT(clock_settime(CLOCK_MONOTONIC, &ts) == -1); diff --git a/components/newlib/time.c b/components/newlib/time.c index 8a7429cea9..91f6bfc6c8 100644 --- a/components/newlib/time.c +++ b/components/newlib/time.c @@ -21,26 +21,18 @@ #include #include #include -#include + #include "esp_attr.h" -#include "esp_intr_alloc.h" -#include "esp_timer.h" -#include "soc/soc.h" -#include "soc/rtc.h" -#include "soc/frc_timer_reg.h" + #include "freertos/FreeRTOS.h" -#include "freertos/xtensa_api.h" #include "freertos/task.h" -#include "limits.h" + +#include "soc/spinlock.h" +#include "soc/rtc.h" + +#include "esp_time_impl.h" + #include "sdkconfig.h" -#include "esp_rom_sys.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/clk.h" -#include "esp32/rom/rtc.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/clk.h" -#include "esp32s2/rom/rtc.h" -#endif #ifdef CONFIG_SDK_TOOLCHAIN_SUPPORTS_TIME_WIDE_64_BITS _Static_assert(sizeof(time_t) == 8, "The toolchain does not support time_t wide 64-bits"); @@ -48,161 +40,95 @@ _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 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 +#if !CONFIG_ESP32_TIME_SYSCALL_USE_NONE && !CONFIG_ESP32S2_TIME_SYSCALL_USE_NONE +#define IMPL_NEWLIB_TIME_FUNCS 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 - -#ifdef WITH_RTC -static uint64_t get_rtc_time_us(void) -{ - const uint64_t ticks = rtc_time_get(); - const uint32_t cal = esp_clk_slowclk_cal_get(); - /* RTC counter result is up to 2^48, calibration factor is up to 2^24, - * for a 32kHz clock. We need to calculate (assuming no overflow): - * (ticks * cal) >> RTC_CLK_CAL_FRACT - * - * An overflow in the (ticks * cal) multiplication would cause time to - * wrap around after approximately 13 days, which is probably not enough - * for some applications. - * Therefore multiplication is split into two terms, for the lower 32-bit - * and the upper 16-bit parts of "ticks", i.e.: - * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT - */ - const uint64_t ticks_low = ticks & UINT32_MAX; - const uint64_t ticks_high = ticks >> 32; - return ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) + - ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT)); -} -#endif // WITH_RTC - - -// s_boot_time: time from Epoch to the first boot time -#ifdef WITH_RTC -// when RTC is used to persist time, two RTC_STORE registers are used to store boot time -#elif defined(WITH_FRC) -static uint64_t s_boot_time; -#endif // WITH_RTC - -#if defined(WITH_RTC) || defined(WITH_FRC) -static _lock_t s_boot_time_lock; -static _lock_t s_adjust_time_lock; +#if IMPL_NEWLIB_TIME_FUNCS // stores the start time of the slew -static uint64_t adjtime_start = 0; +static uint64_t s_adjtime_start_us; // is how many microseconds total to slew -static int64_t adjtime_total_correction = 0; -#define ADJTIME_CORRECTION_FACTOR 6 -static uint64_t get_time_since_boot(void); -#endif -// Offset between FRC timer and the RTC. -// Initialized after reset or light sleep. -#if defined(WITH_RTC) && defined(WITH_FRC) -uint64_t s_microseconds_offset; -#endif +static int64_t s_adjtime_total_correction_us; -#if defined(WITH_RTC) || defined(WITH_FRC) -static void set_boot_time(uint64_t time_us) -{ - _lock_acquire(&s_boot_time_lock); -#ifdef WITH_RTC - 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 - s_boot_time = time_us; -#endif - _lock_release(&s_boot_time_lock); -} - -static uint64_t get_boot_time(void) -{ - uint64_t result; - _lock_acquire(&s_boot_time_lock); -#ifdef WITH_RTC - 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; -#endif - _lock_release(&s_boot_time_lock); - return result; -} +static spinlock_t s_time_lock = SPINLOCK_INITIALIZER; // This function gradually changes boot_time to the correction value and immediately updates it. static uint64_t adjust_boot_time(void) { - uint64_t boot_time = get_boot_time(); - if ((boot_time == 0) || (get_time_since_boot() < adjtime_start)) { - adjtime_start = 0; + #define ADJTIME_CORRECTION_FACTOR 6 + + uint64_t boot_time = esp_time_impl_get_boot_time(); + if ((boot_time == 0) || (esp_time_impl_get_time_since_boot() < s_adjtime_start_us)) { + s_adjtime_start_us = 0; } - if (adjtime_start > 0) { - uint64_t since_boot = get_time_since_boot(); - // If to call this function once per second, then (since_boot - adjtime_start) will be 1_000_000 (1 second), + if (s_adjtime_start_us > 0) { + uint64_t since_boot = esp_time_impl_get_time_since_boot(); + // If to call this function once per second, then (since_boot - s_adjtime_start_us) will be 1_000_000 (1 second), // and the correction will be equal to (1_000_000us >> 6) = 15_625 us. // The minimum possible correction step can be (64us >> 6) = 1us. // Example: if the time error is 1 second, then it will be compensate for 1 sec / 0,015625 = 64 seconds. - int64_t correction = (since_boot >> ADJTIME_CORRECTION_FACTOR) - (adjtime_start >> ADJTIME_CORRECTION_FACTOR); + int64_t correction = (since_boot >> ADJTIME_CORRECTION_FACTOR) - (s_adjtime_start_us >> ADJTIME_CORRECTION_FACTOR); if (correction > 0) { - adjtime_start = since_boot; - if (adjtime_total_correction < 0) { - if ((adjtime_total_correction + correction) >= 0) { - boot_time = boot_time + adjtime_total_correction; - adjtime_start = 0; + s_adjtime_start_us = since_boot; + if (s_adjtime_total_correction_us < 0) { + if ((s_adjtime_total_correction_us + correction) >= 0) { + boot_time = boot_time + s_adjtime_total_correction_us; + s_adjtime_start_us = 0; } else { - adjtime_total_correction += correction; + s_adjtime_total_correction_us += correction; boot_time -= correction; } } else { - if ((adjtime_total_correction - correction) <= 0) { - boot_time = boot_time + adjtime_total_correction; - adjtime_start = 0; + if ((s_adjtime_total_correction_us - correction) <= 0) { + boot_time = boot_time + s_adjtime_total_correction_us; + s_adjtime_start_us = 0; } else { - adjtime_total_correction -= correction; + s_adjtime_total_correction_us -= correction; boot_time += correction; } } - set_boot_time(boot_time); + esp_time_impl_set_boot_time(boot_time); } } return boot_time; } + // Get the adjusted boot time. -static uint64_t get_adjusted_boot_time (void) +static uint64_t get_adjusted_boot_time(void) { - _lock_acquire(&s_adjust_time_lock); + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); uint64_t adjust_time = adjust_boot_time(); - _lock_release(&s_adjust_time_lock); + spinlock_release(&s_time_lock); return adjust_time; } -// Applying the accumulated correction to boot_time and stopping the smooth time adjustment. +// Applying the accumulated correction to base_time and stopping the smooth time adjustment. static void adjtime_corr_stop (void) { - _lock_acquire(&s_adjust_time_lock); - if (adjtime_start != 0){ + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); + if (s_adjtime_start_us != 0){ adjust_boot_time(); - adjtime_start = 0; + s_adjtime_start_us = 0; } - _lock_release(&s_adjust_time_lock); + spinlock_release(&s_time_lock); } -#endif //defined(WITH_RTC) || defined(WITH_FRC) +#endif int adjtime(const struct timeval *delta, struct timeval *outdelta) { -#if defined( WITH_FRC ) || defined( WITH_RTC ) +#if IMPL_NEWLIB_TIME_FUNCS if(outdelta != NULL){ - _lock_acquire(&s_adjust_time_lock); + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); adjust_boot_time(); - if (adjtime_start != 0) { - outdelta->tv_sec = adjtime_total_correction / 1000000L; - outdelta->tv_usec = adjtime_total_correction % 1000000L; + if (s_adjtime_start_us != 0) { + outdelta->tv_sec = s_adjtime_total_correction_us / 1000000L; + outdelta->tv_usec = s_adjtime_total_correction_us % 1000000L; } else { outdelta->tv_sec = 0; outdelta->tv_usec = 0; } - _lock_release(&s_adjust_time_lock); + spinlock_release(&s_time_lock); } if(delta != NULL){ int64_t sec = delta->tv_sec; @@ -215,59 +141,16 @@ int adjtime(const struct timeval *delta, struct timeval *outdelta) * and the delta of the second call is not NULL, the earlier tuning is stopped, * but the already completed part of the adjustment is not canceled. */ - _lock_acquire(&s_adjust_time_lock); - // If correction is already in progress (adjtime_start != 0), then apply accumulated corrections. + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); + // If correction is already in progress (s_adjtime_start_time_us != 0), then apply accumulated corrections. adjust_boot_time(); - adjtime_start = get_time_since_boot(); - adjtime_total_correction = sec * 1000000L + usec; - _lock_release(&s_adjust_time_lock); + s_adjtime_start_us = esp_time_impl_get_time_since_boot(); + s_adjtime_total_correction_us = sec * 1000000L + usec; + spinlock_release(&s_time_lock); } - return 0; -#else - return -1; -#endif - -} - -void esp_clk_slowclk_cal_set(uint32_t new_cal) -{ -#if defined(WITH_RTC) - /* 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 - * T = boot_time_adj + new_cal * ticks / 2^19 - * which results in: - * boot_time_adj = boot_time_old + ticks * (cur_cal - new_cal) / 2^19 - */ - const int64_t ticks = (int64_t) rtc_time_get(); - const uint32_t cur_cal = REG_READ(RTC_SLOW_CLK_CAL_REG); - int32_t cal_diff = (int32_t) (cur_cal - new_cal); - int64_t boot_time_diff = ticks * cal_diff / (1LL << RTC_CLK_CAL_FRACT); - uint64_t boot_time_adj = get_boot_time() + boot_time_diff; - set_boot_time(boot_time_adj); -#endif // WITH_RTC - REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal); -} - -uint32_t esp_clk_slowclk_cal_get(void) -{ - return REG_READ(RTC_SLOW_CLK_CAL_REG); -} - -void esp_set_time_from_rtc(void) -{ -#if defined( WITH_FRC ) && defined( WITH_RTC ) - // initialize time from RTC clock - s_microseconds_offset = get_rtc_time_us() - esp_timer_get_time(); -#endif // WITH_FRC && WITH_RTC -} - -uint64_t esp_clk_rtc_time(void) -{ -#ifdef WITH_RTC - return get_rtc_time_us(); -#else return 0; +#else + return -1; #endif } @@ -283,29 +166,13 @@ clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms) return (clock_t) tv.tv_sec; } -#if defined( WITH_FRC ) || defined( WITH_RTC ) -static uint64_t get_time_since_boot(void) -{ - uint64_t microseconds = 0; -#ifdef WITH_FRC -#ifdef WITH_RTC - microseconds = s_microseconds_offset + esp_timer_get_time(); -#else - microseconds = esp_timer_get_time(); -#endif // WITH_RTC -#elif defined(WITH_RTC) - microseconds = get_rtc_time_us(); -#endif // WITH_FRC - return microseconds; -} -#endif // defined( WITH_FRC ) || defined( WITH_RTC ) - int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz) { (void) tz; -#if defined( WITH_FRC ) || defined( WITH_RTC ) + +#if IMPL_NEWLIB_TIME_FUNCS if (tv) { - uint64_t microseconds = get_adjusted_boot_time() + get_time_since_boot(); + uint64_t microseconds = get_adjusted_boot_time() + esp_time_impl_get_time_since_boot(); tv->tv_sec = microseconds / 1000000; tv->tv_usec = microseconds % 1000000; } @@ -313,18 +180,18 @@ int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz) #else __errno_r(r) = ENOSYS; return -1; -#endif // defined( WITH_FRC ) || defined( WITH_RTC ) +#endif } int settimeofday(const struct timeval *tv, const struct timezone *tz) { (void) tz; -#if defined( WITH_FRC ) || defined( WITH_RTC ) +#if IMPL_NEWLIB_TIME_FUNCS if (tv) { adjtime_corr_stop(); uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec; - uint64_t since_boot = get_time_since_boot(); - set_boot_time(now - since_boot); + uint64_t since_boot = esp_time_impl_get_time_since_boot(); + esp_time_impl_set_boot_time(now - since_boot); } return 0; #else @@ -353,48 +220,9 @@ unsigned int sleep(unsigned int seconds) return 0; } -uint32_t system_get_time(void) +int clock_settime(clockid_t clock_id, const struct timespec *tp) { -#if defined( WITH_FRC ) || defined( WITH_RTC ) - return get_time_since_boot(); -#else - return 0; -#endif -} - -uint32_t system_get_current_time(void) __attribute__((alias("system_get_time"))); - -uint32_t system_relative_time(uint32_t current_time) -{ -#if defined( WITH_FRC ) || defined( WITH_RTC ) - return get_time_since_boot() - current_time; -#else - return 0; -#endif -} - -uint64_t system_get_rtc_time(void) -{ -#ifdef WITH_RTC - return get_rtc_time_us(); -#else - return 0; -#endif -} - -void esp_sync_counters_rtc_and_frc(void) -{ -#if defined( WITH_FRC ) && defined( WITH_RTC ) - adjtime_corr_stop(); - int64_t s_microseconds_offset_cur = get_rtc_time_us() - esp_timer_get_time(); - set_boot_time(get_adjusted_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur)); -#endif -} - - -int clock_settime (clockid_t clock_id, const struct timespec *tp) -{ -#if defined( WITH_FRC ) || defined( WITH_RTC ) +#if IMPL_NEWLIB_TIME_FUNCS if (tp == NULL) { errno = EINVAL; return -1; @@ -419,7 +247,7 @@ int clock_settime (clockid_t clock_id, const struct timespec *tp) int clock_gettime (clockid_t clock_id, struct timespec *tp) { -#if defined( WITH_FRC ) || defined( WITH_RTC ) +#if IMPL_NEWLIB_TIME_FUNCS if (tp == NULL) { errno = EINVAL; return -1; @@ -433,11 +261,7 @@ int clock_gettime (clockid_t clock_id, struct timespec *tp) tp->tv_nsec = tv.tv_usec * 1000L; break; case CLOCK_MONOTONIC: -#if defined( WITH_FRC ) - monotonic_time_us = (uint64_t) esp_timer_get_time(); -#elif defined( WITH_RTC ) - monotonic_time_us = get_rtc_time_us(); -#endif // WITH_FRC + monotonic_time_us = esp_time_impl_get_time(); tp->tv_sec = monotonic_time_us / 1000000LL; tp->tv_nsec = (monotonic_time_us % 1000000LL) * 1000L; break; @@ -454,23 +278,23 @@ int clock_gettime (clockid_t clock_id, struct timespec *tp) int clock_getres (clockid_t clock_id, struct timespec *res) { -#if defined( WITH_FRC ) || defined( WITH_RTC ) +#if IMPL_NEWLIB_TIME_FUNCS if (res == NULL) { errno = EINVAL; return -1; } -#if defined( WITH_FRC ) + res->tv_sec = 0; - res->tv_nsec = 1000L; -#elif defined( WITH_RTC ) - res->tv_sec = 0; - uint32_t rtc_freq = rtc_clk_slow_freq_get_hz(); - assert(rtc_freq != 0); - res->tv_nsec = 1000000000L / rtc_freq; -#endif // WITH_FRC + res->tv_nsec = esp_time_impl_get_time_resolution() * 1000; + return 0; #else errno = ENOSYS; return -1; #endif } + +void esp_newlib_time_init(void) +{ + esp_time_impl_init(); +} \ No newline at end of file From 5785e4dfb62698da9df234ed770a927289830aed Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Tue, 23 Jun 2020 16:07:09 +0800 Subject: [PATCH 2/5] newlib: move some functions to soc, esp32, esp32s2 --- components/esp32/include/esp32/rtc.h | 39 ++++++++++++++ components/esp32s2/include/esp32s2/rtc.h | 39 ++++++++++++++ components/esp_system/port/cpu_start.c | 2 + components/newlib/port/esp_time_impl.c | 54 ++++++++++---------- components/soc/soc/esp32/include/soc/rtc.h | 2 + components/soc/soc/esp32s2/include/soc/rtc.h | 1 + components/soc/src/esp32s2/rtc_time.c | 3 +- 7 files changed, 111 insertions(+), 29 deletions(-) create mode 100644 components/esp32/include/esp32/rtc.h create mode 100644 components/esp32s2/include/esp32s2/rtc.h diff --git a/components/esp32/include/esp32/rtc.h b/components/esp32/include/esp32/rtc.h new file mode 100644 index 0000000000..9b3e343d79 --- /dev/null +++ b/components/esp32/include/esp32/rtc.h @@ -0,0 +1,39 @@ +// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file esp32/rtc.h + * + * This file contains declarations of rtc related functions. + */ + +/** + * @brief Get current value of RTC counter in microseconds + * + * Note: this function may take up to 1 RTC_SLOW_CLK cycle to execute + * + * @return current value of RTC counter in microseconds + */ +uint64_t esp_rtc_get_time_us(void); + +#ifdef __cplusplus +} +#endif diff --git a/components/esp32s2/include/esp32s2/rtc.h b/components/esp32s2/include/esp32s2/rtc.h new file mode 100644 index 0000000000..296292e21d --- /dev/null +++ b/components/esp32s2/include/esp32s2/rtc.h @@ -0,0 +1,39 @@ +// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file esp32s2/rtc.h + * + * This file contains declarations of rtc related functions. + */ + +/** + * @brief Get current value of RTC counter in microseconds + * + * Note: this function may take up to 1 RTC_SLOW_CLK cycle to execute + * + * @return current value of RTC counter in microseconds + */ +uint64_t esp_rtc_get_time_us(void); + +#ifdef __cplusplus +} +#endif diff --git a/components/esp_system/port/cpu_start.c b/components/esp_system/port/cpu_start.c index 4d694bf923..e86be35430 100644 --- a/components/esp_system/port/cpu_start.c +++ b/components/esp_system/port/cpu_start.c @@ -30,11 +30,13 @@ #include "sdkconfig.h" #if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rtc.h" #include "esp32/cache_err_int.h" #include "esp32/rom/cache.h" #include "esp32/rom/rtc.h" #include "esp32/spiram.h" #elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rtc.h" #include "esp32s2/brownout.h" #include "esp32s2/cache_err_int.h" #include "esp32s2/rom/cache.h" diff --git a/components/newlib/port/esp_time_impl.c b/components/newlib/port/esp_time_impl.c index 4a7a800565..08d3f4130f 100644 --- a/components/newlib/port/esp_time_impl.c +++ b/components/newlib/port/esp_time_impl.c @@ -30,9 +30,11 @@ #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/rtc.h" #include "esp32/clk.h" +#include "esp32/rtc.h" #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/rtc.h" #include "esp32s2/clk.h" +#include "esp32s2/rtc.h" #endif #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC ) \ @@ -61,29 +63,6 @@ static uint64_t s_boot_time; // when RTC is used to persist time, two RTC_STORE static spinlock_t s_time_lock = SPINLOCK_INITIALIZER; -#ifdef WITH_RTC -static uint64_t get_rtc_time_us(void) -{ - const uint64_t ticks = rtc_time_get(); - const uint32_t cal = esp_clk_slowclk_cal_get(); - /* RTC counter result is up to 2^48, calibration factor is up to 2^24, - * for a 32kHz clock. We need to calculate (assuming no overflow): - * (ticks * cal) >> RTC_CLK_CAL_FRACT - * - * An overflow in the (ticks * cal) multiplication would cause time to - * wrap around after approximately 13 days, which is probably not enough - * for some applications. - * Therefore multiplication is split into two terms, for the lower 32-bit - * and the upper 16-bit parts of "ticks", i.e.: - * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT - */ - const uint64_t ticks_low = ticks & UINT32_MAX; - const uint64_t ticks_high = ticks >> 32; - return ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) + - ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT)); -} -#endif // WITH_RTC - #if defined( WITH_FRC ) || defined( WITH_RTC ) uint64_t esp_time_impl_get_time_since_boot(void) { @@ -96,7 +75,7 @@ uint64_t esp_time_impl_get_time_since_boot(void) microseconds = esp_timer_get_time(); #endif // WITH_RTC #elif defined(WITH_RTC) - microseconds = get_rtc_time_us(); + microseconds = esp_rtc_get_time_us(); #endif // WITH_FRC return microseconds; } @@ -106,7 +85,7 @@ uint64_t esp_time_impl_get_time(void) #if defined( WITH_FRC ) return esp_timer_get_time(); #elif defined( WITH_RTC ) - return get_rtc_time_us(); + return esp_rtc_get_time_us(); #endif // WITH_FRC } #endif // defined( WITH_FRC ) || defined( WITH_RTC ) @@ -151,6 +130,27 @@ uint32_t esp_clk_slowclk_cal_get(void) return REG_READ(RTC_SLOW_CLK_CAL_REG); } +uint64_t esp_rtc_get_time_us(void) +{ + const uint64_t ticks = rtc_time_get(); + const uint32_t cal = esp_clk_slowclk_cal_get(); + /* RTC counter result is up to 2^48, calibration factor is up to 2^24, + * for a 32kHz clock. We need to calculate (assuming no overflow): + * (ticks * cal) >> RTC_CLK_CAL_FRACT + * + * An overflow in the (ticks * cal) multiplication would cause time to + * wrap around after approximately 13 days, which is probably not enough + * for some applications. + * Therefore multiplication is split into two terms, for the lower 32-bit + * and the upper 16-bit parts of "ticks", i.e.: + * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT + */ + const uint64_t ticks_low = ticks & UINT32_MAX; + const uint64_t ticks_high = ticks >> 32; + return ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) + + ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT)); +} + void esp_clk_slowclk_cal_set(uint32_t new_cal) { #if defined(WITH_RTC) @@ -175,7 +175,7 @@ void esp_set_time_from_rtc(void) { #if defined( WITH_FRC ) && defined( WITH_RTC ) // initialize time from RTC clock - s_microseconds_offset = get_rtc_time_us() - esp_timer_get_time(); + s_microseconds_offset = esp_rtc_get_time_us() - esp_timer_get_time(); #endif // WITH_FRC && WITH_RTC } @@ -185,7 +185,7 @@ void esp_sync_counters_rtc_and_frc(void) struct timeval tv; gettimeofday(&tv, NULL); settimeofday(&tv, NULL); - int64_t s_microseconds_offset_cur = get_rtc_time_us() - esp_timer_get_time(); + int64_t s_microseconds_offset_cur = esp_rtc_get_time_us() - esp_timer_get_time(); esp_time_impl_set_boot_time(esp_time_impl_get_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur)); #endif } diff --git a/components/soc/soc/esp32/include/soc/rtc.h b/components/soc/soc/esp32/include/soc/rtc.h index 96982a038d..e955a69f30 100644 --- a/components/soc/soc/esp32/include/soc/rtc.h +++ b/components/soc/soc/esp32/include/soc/rtc.h @@ -645,6 +645,8 @@ rtc_vddsdio_config_t rtc_vddsdio_get_config(void); */ void rtc_vddsdio_set_config(rtc_vddsdio_config_t config); + + #ifdef __cplusplus } #endif diff --git a/components/soc/soc/esp32s2/include/soc/rtc.h b/components/soc/soc/esp32s2/include/soc/rtc.h index 2f7ba5c4e6..a69be33266 100644 --- a/components/soc/soc/esp32s2/include/soc/rtc.h +++ b/components/soc/soc/esp32s2/include/soc/rtc.h @@ -809,6 +809,7 @@ rtc_vddsdio_config_t rtc_vddsdio_get_config(void); */ void rtc_vddsdio_set_config(rtc_vddsdio_config_t config); + #ifdef __cplusplus } #endif diff --git a/components/soc/src/esp32s2/rtc_time.c b/components/soc/src/esp32s2/rtc_time.c index 19aa1ade64..d59e9ea64e 100644 --- a/components/soc/src/esp32s2/rtc_time.c +++ b/components/soc/src/esp32s2/rtc_time.c @@ -183,5 +183,4 @@ void rtc_clk_wait_for_slow_cycle(void) //This function may not by useful any mor while (GET_PERI_REG_MASK(RTC_CNTL_SLOW_CLK_CONF_REG, RTC_CNTL_SLOW_CLK_NEXT_EDGE)) { esp_rom_delay_us(1); } -} - +} \ No newline at end of file From 346cf4430d198aa5a021adebd9de8f5b0e35be12 Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Tue, 23 Jun 2020 16:46:06 +0800 Subject: [PATCH 3/5] esp_system: introduce system time functions - Introduce system time function and concept of system time provider. esp_timer is system time provider when present. - Set the reference point for system time, g_startup_time. - Use the system time functions in newlib instead of calling esp_timer functions directly --- .../include/esp_private/system_internal.h | 10 +++++ components/esp_system/CMakeLists.txt | 4 +- .../include/esp_private/startup_internal.h | 4 ++ components/esp_system/include/esp_system.h | 16 +++++++ components/esp_system/port/cpu_start.c | 22 ++++++++- components/esp_system/startup.c | 7 +-- components/esp_system/system_time.c | 41 +++++++++++++++++ components/esp_timer/CMakeLists.txt | 3 +- .../private_include/esp_timer_impl.h | 5 +++ components/esp_timer/src/esp_timer.c | 9 ++-- components/esp_timer/src/timekeeping.c | 45 +++++++++++++++++++ components/newlib/CMakeLists.txt | 2 +- components/newlib/port/esp_time_impl.c | 32 ++++++------- components/newlib/time.c | 3 +- 14 files changed, 172 insertions(+), 31 deletions(-) create mode 100644 components/esp_system/system_time.c create mode 100644 components/esp_timer/src/timekeeping.c diff --git a/components/esp_common/include/esp_private/system_internal.h b/components/esp_common/include/esp_private/system_internal.h index 9b383cd60e..bcc1dad20c 100644 --- a/components/esp_common/include/esp_private/system_internal.h +++ b/components/esp_common/include/esp_private/system_internal.h @@ -61,6 +61,16 @@ void esp_reset_reason_set_hint(esp_reset_reason_t hint); */ esp_reset_reason_t esp_reset_reason_get_hint(void); +/** + * @brief Set function which provides `esp_system_get_time`. + * + * @param time_fn function which provides system time + * @param resolution resolution in microseconds of the time provider function specified + */ +void esp_system_set_time_provider(esp_system_time_fn_t time_fn, uint32_t resolution); + + + #ifdef __cplusplus } #endif diff --git a/components/esp_system/CMakeLists.txt b/components/esp_system/CMakeLists.txt index 542af29d63..0856648800 100644 --- a/components/esp_system/CMakeLists.txt +++ b/components/esp_system/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "panic.c" "system_api.c" "startup.c" +idf_component_register(SRCS "panic.c" "system_api.c" "startup.c" "system_time.c" INCLUDE_DIRS include PRIV_REQUIRES spi_flash app_update # requirements due to startup code @@ -21,4 +21,4 @@ endif() set_source_files_properties( startup.c PROPERTIES COMPILE_FLAGS - -fno-stack-protector) \ No newline at end of file + -fno-stack-protector) diff --git a/components/esp_system/include/esp_private/startup_internal.h b/components/esp_system/include/esp_private/startup_internal.h index 8a12ee3673..fc91b0fa85 100644 --- a/components/esp_system/include/esp_private/startup_internal.h +++ b/components/esp_system/include/esp_private/startup_internal.h @@ -64,6 +64,10 @@ static __attribute__((used)) esp_system_init_fn_t _SECTION_ATTR_IMPL(".esp_syste static __attribute__((used)) __VA_ARGS__ void __esp_system_init_fn_##f(void) // [refactor-todo] this can be made public API if we allow components to declare init functions, // instead of calling them explicitly +extern uint64_t g_startup_time; // Startup time that serves as the point of origin for system time. Should be set by the entry + // function in the port layer. May be 0 as well if this is not backed by a persistent counter, in which case + // startup time = system time = 0 at the point the entry function sets this variable. + #ifdef __cplusplus } #endif diff --git a/components/esp_system/include/esp_system.h b/components/esp_system/include/esp_system.h index 3e5b777204..aa4833b4dc 100644 --- a/components/esp_system/include/esp_system.h +++ b/components/esp_system/include/esp_system.h @@ -28,6 +28,8 @@ extern "C" { #endif +typedef int64_t (*esp_system_time_fn_t)(void); + typedef enum { ESP_MAC_WIFI_STA, ESP_MAC_WIFI_SOFTAP, @@ -286,6 +288,20 @@ typedef struct { */ void esp_chip_info(esp_chip_info_t* out_info); +/** + * @brief Get the time in microseconds since startup + * + * @returns time since startup in microseconds + */ +int64_t esp_system_get_time(void); + +/** + * @brief Get the resolution of the time returned by `esp_system_get_time`. + * + * @returns the resolution in microseconds + */ +uint32_t esp_system_get_time_resolution(void); + #if CONFIG_ESP32_ECO3_CACHE_LOCK_FIX /** * @brief Cache lock bug exists or not diff --git a/components/esp_system/port/cpu_start.c b/components/esp_system/port/cpu_start.c index e86be35430..94013103aa 100644 --- a/components/esp_system/port/cpu_start.c +++ b/components/esp_system/port/cpu_start.c @@ -55,6 +55,8 @@ #include "soc/dport_reg.h" #include "soc/efuse_reg.h" #include "soc/cpu.h" +#include "soc/rtc.h" +#include "soc/spinlock.h" #include "trax.h" @@ -67,6 +69,7 @@ #endif #include "esp_private/startup_internal.h" +#include "esp_private/system_internal.h" extern int _bss_start; extern int _bss_end; @@ -95,16 +98,27 @@ static volatile bool s_cpu_inited[SOC_CPU_CORES_NUM] = { false }; static volatile bool s_resume_cores; #endif +uint64_t g_startup_time = 0; + // If CONFIG_SPIRAM_IGNORE_NOTFOUND is set and external RAM is not found or errors out on testing, this is set to false. bool g_spiram_ok = true; +static int64_t default_system_time_fn(void) +{ + int64_t t = 0; + static spinlock_t s_time_lock = SPINLOCK_INITIALIZER; + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); + t = (esp_rtc_get_time_us() - g_startup_time); + spinlock_release(&s_time_lock); + return t; +} + #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE void startup_resume_other_cores(void) { s_resume_cores = true; } - void IRAM_ATTR call_start_cpu1(void) { cpu_hal_set_vecbase(&_init_start); @@ -352,6 +366,12 @@ void IRAM_ATTR call_start_cpu0(void) esp_clk_init(); esp_perip_clk_init(); + + // Now that the clocks have been set-up, set the startup time from RTC + // and default RTC-backed system time provider. + g_startup_time = esp_rtc_get_time_us(); + esp_system_set_time_provider(default_system_time_fn, 1000000L / rtc_clk_slow_freq_get_hz()); + intr_matrix_clear(); #ifdef CONFIG_ESP_CONSOLE_UART diff --git a/components/esp_system/startup.c b/components/esp_system/startup.c index 2fbe46804a..5298c6d0d1 100644 --- a/components/esp_system/startup.c +++ b/components/esp_system/startup.c @@ -183,6 +183,7 @@ static void IRAM_ATTR do_core_init(void) fail initializing it properly. */ heap_caps_init(); esp_setup_syscall_table(); + esp_newlib_time_init(); if (g_spiram_ok) { #if CONFIG_SPIRAM_BOOT_INIT && (CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC) @@ -244,9 +245,6 @@ static void IRAM_ATTR do_core_init(void) esp_efuse_disable_basic_rom_console(); #endif - esp_timer_init(); - esp_set_time_from_rtc(); - // [refactor-todo] move this to secondary init #if CONFIG_APPTRACE_ENABLE err = esp_apptrace_init(); @@ -301,6 +299,7 @@ static void IRAM_ATTR do_secondary_init(void) void IRAM_ATTR start_cpu0_default(void) { + ESP_EARLY_LOGI(TAG, "Pro cpu start user code"); // Display information about the current running image. @@ -353,6 +352,8 @@ void IRAM_ATTR start_cpu0_default(void) IRAM_ATTR ESP_SYSTEM_INIT_FN(init_components0, BIT(0)) { + esp_timer_init(); + #if defined(CONFIG_PM_ENABLE) && defined(CONFIG_ESP_CONSOLE_UART) /* When DFS is enabled, use REFTICK as UART clock source */ uart_ll_set_baudrate(UART_LL_GET_HW(CONFIG_ESP_CONSOLE_UART_NUM), UART_SCLK_REF_TICK, CONFIG_ESP_CONSOLE_UART_BAUDRATE); diff --git a/components/esp_system/system_time.c b/components/esp_system/system_time.c new file mode 100644 index 0000000000..a399c5a05c --- /dev/null +++ b/components/esp_system/system_time.c @@ -0,0 +1,41 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "esp_system.h" +#include "esp_attr.h" + +typedef struct { + esp_system_time_fn_t fn; // time provider function + uint32_t resolution; // resolution in microseconds of the time provider +} system_time_provider_t; + +// This is expected to be modified only on startup, so +// it should be safe to not put locks on it. +static system_time_provider_t s_system_time_provider; + +int64_t IRAM_ATTR esp_system_get_time(void) +{ + return (*s_system_time_provider.fn)(); +} + +uint32_t IRAM_ATTR esp_system_get_time_resolution(void) +{ + return s_system_time_provider.resolution; +} + +void esp_system_set_time_provider(esp_system_time_fn_t time_fn, uint32_t resolution) +{ + s_system_time_provider.fn = time_fn; + s_system_time_provider.resolution = resolution; +} \ No newline at end of file diff --git a/components/esp_timer/CMakeLists.txt b/components/esp_timer/CMakeLists.txt index 68be83b9fa..b8890b3674 100644 --- a/components/esp_timer/CMakeLists.txt +++ b/components/esp_timer/CMakeLists.txt @@ -1,7 +1,8 @@ idf_build_get_property(target IDF_TARGET) set(srcs "src/esp_timer.c" - "src/ets_timer_legacy.c") + "src/ets_timer_legacy.c" + "src/timekeeping.c") if(CONFIG_ESP_TIMER_IMPL_FRC2) list(APPEND srcs "src/esp_timer_impl_frc_legacy.c") diff --git a/components/esp_timer/private_include/esp_timer_impl.h b/components/esp_timer/private_include/esp_timer_impl.h index 75e7e1b0d9..c5300f59ed 100644 --- a/components/esp_timer/private_include/esp_timer_impl.h +++ b/components/esp_timer/private_include/esp_timer_impl.h @@ -117,3 +117,8 @@ uint64_t esp_timer_impl_get_counter_reg(void); * @return the value of the alarm register */ uint64_t esp_timer_impl_get_alarm_reg(void); + +/** + * @brief Initialize and provide system timekeeping functions. + */ +esp_err_t esp_timer_timekeeping_impl_init(void); diff --git a/components/esp_timer/src/esp_timer.c b/components/esp_timer/src/esp_timer.c index 2bbb439e5e..a316a6692f 100644 --- a/components/esp_timer/src/esp_timer.c +++ b/components/esp_timer/src/esp_timer.c @@ -97,7 +97,6 @@ static StaticQueue_t s_timer_semaphore_memory; static portMUX_TYPE s_timer_lock = portMUX_INITIALIZER_UNLOCKED; - esp_err_t esp_timer_create(const esp_timer_create_args_t* args, esp_timer_handle_t* out_handle) { @@ -349,7 +348,6 @@ static IRAM_ATTR bool is_initialized(void) return s_timer_task != NULL; } - esp_err_t esp_timer_init(void) { esp_err_t err; @@ -380,6 +378,11 @@ esp_err_t esp_timer_init(void) goto out; } + err = esp_timer_timekeeping_impl_init(); + if (err != ESP_OK) { + goto out; + } + return ESP_OK; out: @@ -505,4 +508,4 @@ int64_t IRAM_ATTR esp_timer_get_next_alarm(void) } timer_list_unlock(); return next_alarm; -} +} \ No newline at end of file diff --git a/components/esp_timer/src/timekeeping.c b/components/esp_timer/src/timekeeping.c new file mode 100644 index 0000000000..767a85b751 --- /dev/null +++ b/components/esp_timer/src/timekeeping.c @@ -0,0 +1,45 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "esp_timer.h" + +#include "esp_private/esp_timer_private.h" +#include "esp_private/system_internal.h" + +#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 + +#if WITH_FRC +void esp_timer_timekeeping_impl_init(void) +{ + // esp_system_get_time here calls the previous system time provider. + // This should add the time elapsed from g_startup_time up to esp_timer_init, + // therefore keeping it as the point of reference (g_startup_time, that is). + esp_timer_private_advance(esp_system_get_time()); + + // esp_timer provides microsecond-resolution timers to the system + esp_system_set_time_provider(esp_timer_get_time, 1); +} +#else +void esp_timer_timekeeping_impl_init(void) +{ + // Do not override default system time provider +} +#endif \ No newline at end of file diff --git a/components/newlib/CMakeLists.txt b/components/newlib/CMakeLists.txt index 8a3463b63d..055e098551 100644 --- a/components/newlib/CMakeLists.txt +++ b/components/newlib/CMakeLists.txt @@ -21,7 +21,7 @@ list(APPEND ldfragments newlib.lf) idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "${include_dirs}" PRIV_INCLUDE_DIRS priv_include - PRIV_REQUIRES soc esp_timer + PRIV_REQUIRES soc LDFRAGMENTS "${ldfragments}") # Toolchain libraries require code defined in this component diff --git a/components/newlib/port/esp_time_impl.c b/components/newlib/port/esp_time_impl.c index 08d3f4130f..35281aadc5 100644 --- a/components/newlib/port/esp_time_impl.c +++ b/components/newlib/port/esp_time_impl.c @@ -15,8 +15,6 @@ #include #include -#include "esp_timer.h" - #include "esp_system.h" #include "soc/spinlock.h" @@ -70,24 +68,31 @@ uint64_t esp_time_impl_get_time_since_boot(void) #ifdef WITH_FRC #ifdef WITH_RTC - microseconds = s_microseconds_offset + esp_timer_get_time(); + microseconds = s_microseconds_offset + esp_system_get_time(); #else - microseconds = esp_timer_get_time(); + microseconds = esp_system_get_time(); #endif // WITH_RTC #elif defined(WITH_RTC) + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); microseconds = esp_rtc_get_time_us(); + spinlock_release(&s_time_lock); #endif // WITH_FRC return microseconds; } uint64_t esp_time_impl_get_time(void) { + uint64_t microseconds = 0; #if defined( WITH_FRC ) - return esp_timer_get_time(); + microseconds = esp_system_get_time(); #elif defined( WITH_RTC ) - return esp_rtc_get_time_us(); + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); + microseconds = esp_rtc_get_time_us(); + spinlock_release(&s_time_lock); #endif // WITH_FRC + return microseconds; } + #endif // defined( WITH_FRC ) || defined( WITH_RTC ) @@ -175,7 +180,7 @@ void esp_set_time_from_rtc(void) { #if defined( WITH_FRC ) && defined( WITH_RTC ) // initialize time from RTC clock - s_microseconds_offset = esp_rtc_get_time_us() - esp_timer_get_time(); + s_microseconds_offset = esp_rtc_get_time_us() - esp_system_get_time(); #endif // WITH_FRC && WITH_RTC } @@ -185,7 +190,7 @@ void esp_sync_counters_rtc_and_frc(void) struct timeval tv; gettimeofday(&tv, NULL); settimeofday(&tv, NULL); - int64_t s_microseconds_offset_cur = esp_rtc_get_time_us() - esp_timer_get_time(); + int64_t s_microseconds_offset_cur = esp_rtc_get_time_us() - esp_system_get_time(); esp_time_impl_set_boot_time(esp_time_impl_get_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur)); #endif } @@ -193,15 +198,4 @@ void esp_sync_counters_rtc_and_frc(void) void esp_time_impl_init(void) { esp_set_time_from_rtc(); -} - -uint32_t esp_time_impl_get_time_resolution(void) -{ -#if defined( WITH_FRC ) - return 1L; -#elif defined( WITH_RTC ) - uint32_t rtc_freq = rtc_clk_slow_freq_get_hz(); - assert(rtc_freq != 0); - return 1000000L / rtc_freq; -#endif // WITH_FRC } \ No newline at end of file diff --git a/components/newlib/time.c b/components/newlib/time.c index 91f6bfc6c8..909963277f 100644 --- a/components/newlib/time.c +++ b/components/newlib/time.c @@ -22,6 +22,7 @@ #include #include +#include "esp_system.h" #include "esp_attr.h" #include "freertos/FreeRTOS.h" @@ -285,7 +286,7 @@ int clock_getres (clockid_t clock_id, struct timespec *res) } res->tv_sec = 0; - res->tv_nsec = esp_time_impl_get_time_resolution() * 1000; + res->tv_nsec = esp_system_get_time_resolution() * 1000; return 0; #else From da88671491a6636b9724c808cb58e0841e57f7c7 Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Tue, 14 Jul 2020 14:46:13 +0800 Subject: [PATCH 4/5] esp_system: link time, not runtime, dependency on esp_timer --- .../include/esp_private/system_internal.h | 7 +-- components/esp_system/include/esp_system.h | 2 - components/esp_system/port/cpu_start.c | 13 ----- components/esp_system/startup.c | 2 + components/esp_system/system_time.c | 39 +++++++------- components/esp_timer/CMakeLists.txt | 5 +- .../private_include/esp_timer_impl.h | 5 -- components/esp_timer/src/esp_timer.c | 51 +++++++++++++++++-- .../esp_timer/src/esp_timer_impl_frc_legacy.c | 2 - components/esp_timer/src/esp_timer_impl_lac.c | 2 - .../esp_timer/src/esp_timer_impl_systimer.c | 3 -- components/esp_timer/src/timekeeping.c | 45 ---------------- 12 files changed, 73 insertions(+), 103 deletions(-) delete mode 100644 components/esp_timer/src/timekeeping.c diff --git a/components/esp_common/include/esp_private/system_internal.h b/components/esp_common/include/esp_private/system_internal.h index bcc1dad20c..fcbd250dc1 100644 --- a/components/esp_common/include/esp_private/system_internal.h +++ b/components/esp_common/include/esp_private/system_internal.h @@ -62,13 +62,8 @@ void esp_reset_reason_set_hint(esp_reset_reason_t hint); esp_reset_reason_t esp_reset_reason_get_hint(void); /** - * @brief Set function which provides `esp_system_get_time`. - * - * @param time_fn function which provides system time - * @param resolution resolution in microseconds of the time provider function specified + * @brief Get the time in microseconds since startup */ -void esp_system_set_time_provider(esp_system_time_fn_t time_fn, uint32_t resolution); - #ifdef __cplusplus diff --git a/components/esp_system/include/esp_system.h b/components/esp_system/include/esp_system.h index aa4833b4dc..94a2355425 100644 --- a/components/esp_system/include/esp_system.h +++ b/components/esp_system/include/esp_system.h @@ -28,8 +28,6 @@ extern "C" { #endif -typedef int64_t (*esp_system_time_fn_t)(void); - typedef enum { ESP_MAC_WIFI_STA, ESP_MAC_WIFI_SOFTAP, diff --git a/components/esp_system/port/cpu_start.c b/components/esp_system/port/cpu_start.c index 94013103aa..ca611f7945 100644 --- a/components/esp_system/port/cpu_start.c +++ b/components/esp_system/port/cpu_start.c @@ -98,21 +98,9 @@ static volatile bool s_cpu_inited[SOC_CPU_CORES_NUM] = { false }; static volatile bool s_resume_cores; #endif -uint64_t g_startup_time = 0; - // If CONFIG_SPIRAM_IGNORE_NOTFOUND is set and external RAM is not found or errors out on testing, this is set to false. bool g_spiram_ok = true; -static int64_t default_system_time_fn(void) -{ - int64_t t = 0; - static spinlock_t s_time_lock = SPINLOCK_INITIALIZER; - spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); - t = (esp_rtc_get_time_us() - g_startup_time); - spinlock_release(&s_time_lock); - return t; -} - #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE void startup_resume_other_cores(void) { @@ -370,7 +358,6 @@ void IRAM_ATTR call_start_cpu0(void) // Now that the clocks have been set-up, set the startup time from RTC // and default RTC-backed system time provider. g_startup_time = esp_rtc_get_time_us(); - esp_system_set_time_provider(default_system_time_fn, 1000000L / rtc_clk_slow_freq_get_hz()); intr_matrix_clear(); diff --git a/components/esp_system/startup.c b/components/esp_system/startup.c index 5298c6d0d1..523136f257 100644 --- a/components/esp_system/startup.c +++ b/components/esp_system/startup.c @@ -78,6 +78,8 @@ #define STRINGIFY(s) STRINGIFY2(s) #define STRINGIFY2(s) #s +uint64_t g_startup_time = 0; + // App entry point for core 0 extern void start_app(void); diff --git a/components/esp_system/system_time.c b/components/esp_system/system_time.c index a399c5a05c..c4603bdc7d 100644 --- a/components/esp_system/system_time.c +++ b/components/esp_system/system_time.c @@ -15,27 +15,32 @@ #include "esp_system.h" #include "esp_attr.h" -typedef struct { - esp_system_time_fn_t fn; // time provider function - uint32_t resolution; // resolution in microseconds of the time provider -} system_time_provider_t; +#include "soc/spinlock.h" +#include "soc/rtc.h" -// This is expected to be modified only on startup, so -// it should be safe to not put locks on it. -static system_time_provider_t s_system_time_provider; +#include "sdkconfig.h" -int64_t IRAM_ATTR esp_system_get_time(void) +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rtc.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rtc.h" +#endif + +#include "esp_private/startup_internal.h" + +// A component in the build should provide strong implementations that make use of +// and actual hardware timer to provide timekeeping functions. +int64_t IRAM_ATTR __attribute__((weak)) esp_system_get_time(void) { - return (*s_system_time_provider.fn)(); + int64_t t = 0; + static spinlock_t s_time_lock = SPINLOCK_INITIALIZER; + spinlock_acquire(&s_time_lock, SPINLOCK_WAIT_FOREVER); + t = (esp_rtc_get_time_us() - g_startup_time); + spinlock_release(&s_time_lock); + return t; } -uint32_t IRAM_ATTR esp_system_get_time_resolution(void) +uint32_t IRAM_ATTR __attribute__((weak)) esp_system_get_time_resolution(void) { - return s_system_time_provider.resolution; -} - -void esp_system_set_time_provider(esp_system_time_fn_t time_fn, uint32_t resolution) -{ - s_system_time_provider.fn = time_fn; - s_system_time_provider.resolution = resolution; + return 1000000L / rtc_clk_slow_freq_get_hz(); } \ No newline at end of file diff --git a/components/esp_timer/CMakeLists.txt b/components/esp_timer/CMakeLists.txt index b8890b3674..0fbbee530b 100644 --- a/components/esp_timer/CMakeLists.txt +++ b/components/esp_timer/CMakeLists.txt @@ -1,8 +1,7 @@ idf_build_get_property(target IDF_TARGET) -set(srcs "src/esp_timer.c" - "src/ets_timer_legacy.c" - "src/timekeeping.c") +set(srcs "src/esp_timer.c" + "src/ets_timer_legacy.c") if(CONFIG_ESP_TIMER_IMPL_FRC2) list(APPEND srcs "src/esp_timer_impl_frc_legacy.c") diff --git a/components/esp_timer/private_include/esp_timer_impl.h b/components/esp_timer/private_include/esp_timer_impl.h index c5300f59ed..75e7e1b0d9 100644 --- a/components/esp_timer/private_include/esp_timer_impl.h +++ b/components/esp_timer/private_include/esp_timer_impl.h @@ -117,8 +117,3 @@ uint64_t esp_timer_impl_get_counter_reg(void); * @return the value of the alarm register */ uint64_t esp_timer_impl_get_alarm_reg(void); - -/** - * @brief Initialize and provide system timekeeping functions. - */ -esp_err_t esp_timer_timekeeping_impl_init(void); diff --git a/components/esp_timer/src/esp_timer.c b/components/esp_timer/src/esp_timer.c index a316a6692f..c0f14b2d1f 100644 --- a/components/esp_timer/src/esp_timer.c +++ b/components/esp_timer/src/esp_timer.c @@ -24,10 +24,27 @@ #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/xtensa_api.h" +#include "soc/spinlock.h" #include "esp_timer.h" #include "esp_timer_impl.h" + +#include "esp_private/startup_internal.h" +#include "esp_private/esp_timer_private.h" + +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rtc.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/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 @@ -378,10 +395,11 @@ esp_err_t esp_timer_init(void) goto out; } - err = esp_timer_timekeeping_impl_init(); - if (err != ESP_OK) { - goto out; - } +#if WITH_FRC + // [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); +#endif return ESP_OK; @@ -508,4 +526,27 @@ int64_t IRAM_ATTR esp_timer_get_next_alarm(void) } timer_list_unlock(); return next_alarm; -} \ No newline at end of file +} + +int64_t IRAM_ATTR esp_timer_get_time(void) +{ + if(is_initialized()) { + return esp_timer_impl_get_time(); + } else { + return 0; + } +} + +// Provides strong definition for system time functions relied upon +// by core components. +#if WITH_FRC +int64_t IRAM_ATTR esp_system_get_time(void) +{ + return esp_timer_get_time(); +} + +uint32_t IRAM_ATTR esp_system_get_time_resolution(void) +{ + return 1; +} +#endif \ No newline at end of file diff --git a/components/esp_timer/src/esp_timer_impl_frc_legacy.c b/components/esp_timer/src/esp_timer_impl_frc_legacy.c index e432b1390b..88c254950f 100644 --- a/components/esp_timer/src/esp_timer_impl_frc_legacy.c +++ b/components/esp_timer/src/esp_timer_impl_frc_legacy.c @@ -209,8 +209,6 @@ int64_t IRAM_ATTR esp_timer_impl_get_time(void) return result; } -int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time"))); - void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp) { portENTER_CRITICAL_SAFE(&s_time_update_lock); diff --git a/components/esp_timer/src/esp_timer_impl_lac.c b/components/esp_timer/src/esp_timer_impl_lac.c index c2d66a33f3..6c2942f500 100644 --- a/components/esp_timer/src/esp_timer_impl_lac.c +++ b/components/esp_timer/src/esp_timer_impl_lac.c @@ -149,8 +149,6 @@ int64_t IRAM_ATTR esp_timer_impl_get_time(void) return esp_timer_impl_get_counter_reg() / TICKS_PER_US; } -int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time"))); - void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp) { portENTER_CRITICAL_SAFE(&s_time_update_lock); diff --git a/components/esp_timer/src/esp_timer_impl_systimer.c b/components/esp_timer/src/esp_timer_impl_systimer.c index 30c7a3a120..1bdda19557 100644 --- a/components/esp_timer/src/esp_timer_impl_systimer.c +++ b/components/esp_timer/src/esp_timer_impl_systimer.c @@ -68,9 +68,6 @@ int64_t IRAM_ATTR esp_timer_impl_get_time(void) return systimer_hal_get_time(SYSTIMER_COUNTER_0); } -// Xtensa architecture doesn't have tail call optimization, using alias here can improve performance somehow -int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time"))); - void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp) { portENTER_CRITICAL_SAFE(&s_time_update_lock); diff --git a/components/esp_timer/src/timekeeping.c b/components/esp_timer/src/timekeeping.c deleted file mode 100644 index 767a85b751..0000000000 --- a/components/esp_timer/src/timekeeping.c +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2020 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "esp_timer.h" - -#include "esp_private/esp_timer_private.h" -#include "esp_private/system_internal.h" - -#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 - -#if WITH_FRC -void esp_timer_timekeeping_impl_init(void) -{ - // esp_system_get_time here calls the previous system time provider. - // This should add the time elapsed from g_startup_time up to esp_timer_init, - // therefore keeping it as the point of reference (g_startup_time, that is). - esp_timer_private_advance(esp_system_get_time()); - - // esp_timer provides microsecond-resolution timers to the system - esp_system_set_time_provider(esp_timer_get_time, 1); -} -#else -void esp_timer_timekeeping_impl_init(void) -{ - // Do not override default system time provider -} -#endif \ No newline at end of file From c70c227591708484fce2b9b540fc9fb2c6cc7f73 Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Tue, 11 Aug 2020 17:49:48 +0800 Subject: [PATCH 5/5] esp_system: private system time functions for now --- .../include/esp_private/system_internal.h | 10 ++++++++++ components/esp_system/include/esp_system.h | 13 ------------- components/esp_timer/src/esp_timer.c | 1 + components/newlib/port/esp_time_impl.c | 2 ++ components/newlib/time.c | 2 ++ 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/components/esp_common/include/esp_private/system_internal.h b/components/esp_common/include/esp_private/system_internal.h index fcbd250dc1..4f5c90e817 100644 --- a/components/esp_common/include/esp_private/system_internal.h +++ b/components/esp_common/include/esp_private/system_internal.h @@ -61,10 +61,20 @@ void esp_reset_reason_set_hint(esp_reset_reason_t hint); */ esp_reset_reason_t esp_reset_reason_get_hint(void); + /** * @brief Get the time in microseconds since startup + * + * @returns time since startup in microseconds */ +int64_t esp_system_get_time(void); +/** + * @brief Get the resolution of the time returned by `esp_system_get_time`. + * + * @returns the resolution in microseconds + */ +uint32_t esp_system_get_time_resolution(void); #ifdef __cplusplus } diff --git a/components/esp_system/include/esp_system.h b/components/esp_system/include/esp_system.h index 94a2355425..9be72c3e07 100644 --- a/components/esp_system/include/esp_system.h +++ b/components/esp_system/include/esp_system.h @@ -286,19 +286,6 @@ typedef struct { */ void esp_chip_info(esp_chip_info_t* out_info); -/** - * @brief Get the time in microseconds since startup - * - * @returns time since startup in microseconds - */ -int64_t esp_system_get_time(void); - -/** - * @brief Get the resolution of the time returned by `esp_system_get_time`. - * - * @returns the resolution in microseconds - */ -uint32_t esp_system_get_time_resolution(void); #if CONFIG_ESP32_ECO3_CACHE_LOCK_FIX /** diff --git a/components/esp_timer/src/esp_timer.c b/components/esp_timer/src/esp_timer.c index c0f14b2d1f..a1d870c6d0 100644 --- a/components/esp_timer/src/esp_timer.c +++ b/components/esp_timer/src/esp_timer.c @@ -30,6 +30,7 @@ #include "esp_private/startup_internal.h" #include "esp_private/esp_timer_private.h" +#include "esp_private/system_internal.h" #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rtc.h" diff --git a/components/newlib/port/esp_time_impl.c b/components/newlib/port/esp_time_impl.c index 35281aadc5..b8fb85d9cf 100644 --- a/components/newlib/port/esp_time_impl.c +++ b/components/newlib/port/esp_time_impl.c @@ -21,6 +21,8 @@ #include "soc/rtc.h" #include "esp_rom_sys.h" +#include "esp_private/system_internal.h" + #include "esp_time_impl.h" #include "sdkconfig.h" diff --git a/components/newlib/time.c b/components/newlib/time.c index 909963277f..d8f8173436 100644 --- a/components/newlib/time.c +++ b/components/newlib/time.c @@ -28,6 +28,8 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "esp_private/system_internal.h" + #include "soc/spinlock.h" #include "soc/rtc.h"