From 5785e4dfb62698da9df234ed770a927289830aed Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Tue, 23 Jun 2020 16:07:09 +0800 Subject: [PATCH] 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