Explorar o código

Merge branch 'bugfix/rtc_time' into 'master'

Fix compilation issue when using RTC time only, update Kconfig help and source code

See merge request !1650

Ivan Grokhotkov %!s(int64=8) %!d(string=hai) anos
pai
achega
1c314b34fc
Modificáronse 2 ficheiros con 36 adicións e 26 borrados
  1. 12 8
      components/esp32/Kconfig
  2. 24 18
      components/newlib/time.c

+ 12 - 8
components/esp32/Kconfig

@@ -631,6 +631,10 @@ config BROWNOUT_DET_LVL
     default 7 if BROWNOUT_DET_LVL_SEL_7
     default 7 if BROWNOUT_DET_LVL_SEL_7
 
 
 
 
+# Note about the use of "FRC1" name: currently FRC1 timer is not used for
+# high resolution timekeeping anymore. Instead the esp_timer API, implemented
+# using FRC2 timer, is used.
+# FRC1 name in the option name is kept for compatibility.
 choice ESP32_TIME_SYSCALL
 choice ESP32_TIME_SYSCALL
     prompt "Timers used for gettimeofday function"
     prompt "Timers used for gettimeofday function"
     default ESP32_TIME_SYSCALL_USE_RTC_FRC1
     default ESP32_TIME_SYSCALL_USE_RTC_FRC1
@@ -638,12 +642,12 @@ choice ESP32_TIME_SYSCALL
         This setting defines which hardware timers are used to
         This setting defines which hardware timers are used to
         implement 'gettimeofday' and 'time' functions in C library.
         implement 'gettimeofday' and 'time' functions in C library.
 
 
-        - If only FRC1 timer is used, gettimeofday will provide time at
-          microsecond resolution. Time will not be preserved when going
-          into deep sleep mode.
-        - If both FRC1 and RTC timers are used, timekeeping will
+        - If both high-resolution and RTC timers are used, timekeeping will
           continue in deep sleep. Time will be reported at 1 microsecond
           continue in deep sleep. Time will be reported at 1 microsecond
-          resolution.
+          resolution. This is the default, and the recommended option.
+        - If only high-resolution timer is used, gettimeofday will
+          provide time at microsecond resolution. 
+          Time will not be preserved when going into deep sleep mode.
         - If only RTC timer is used, timekeeping will continue in
         - If only RTC timer is used, timekeeping will continue in
           deep sleep, but time will be measured at 6.(6) microsecond
           deep sleep, but time will be measured at 6.(6) microsecond
           resolution. Also the gettimeofday function itself may take
           resolution. Also the gettimeofday function itself may take
@@ -653,12 +657,12 @@ choice ESP32_TIME_SYSCALL
         - When RTC is used for timekeeping, two RTC_STORE registers are
         - When RTC is used for timekeeping, two RTC_STORE registers are
           used to keep time in deep sleep mode.
           used to keep time in deep sleep mode.
 
 
+config ESP32_TIME_SYSCALL_USE_RTC_FRC1
+    bool "RTC and high-resolution timer"
 config ESP32_TIME_SYSCALL_USE_RTC
 config ESP32_TIME_SYSCALL_USE_RTC
     bool "RTC"
     bool "RTC"
-config ESP32_TIME_SYSCALL_USE_RTC_FRC1
-    bool "RTC and FRC1"
 config ESP32_TIME_SYSCALL_USE_FRC1
 config ESP32_TIME_SYSCALL_USE_FRC1
-    bool "FRC1"
+    bool "High-resolution timer"
 config ESP32_TIME_SYSCALL_USE_NONE
 config ESP32_TIME_SYSCALL_USE_NONE
     bool "None"
     bool "None"
 endchoice
 endchoice

+ 24 - 18
components/newlib/time.c

@@ -42,7 +42,7 @@
 #endif
 #endif
 
 
 #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
 #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
-#define WITH_FRC1 1
+#define WITH_FRC 1
 #endif
 #endif
 
 
 #ifdef WITH_RTC
 #ifdef WITH_RTC
@@ -72,19 +72,21 @@ static uint64_t get_rtc_time_us()
 // s_boot_time: time from Epoch to the first boot time
 // s_boot_time: time from Epoch to the first boot time
 #ifdef WITH_RTC
 #ifdef WITH_RTC
 // when RTC is used to persist time, two RTC_STORE registers are used to store boot time
 // when RTC is used to persist time, two RTC_STORE registers are used to store boot time
-#elif defined(WITH_FRC1)
+#elif defined(WITH_FRC)
 static uint64_t s_boot_time;
 static uint64_t s_boot_time;
-#endif
+#endif // WITH_RTC
 
 
-#if defined(WITH_RTC) || defined(WITH_FRC1)
+#if defined(WITH_RTC) || defined(WITH_FRC)
 static _lock_t s_boot_time_lock;
 static _lock_t s_boot_time_lock;
 #endif
 #endif
 
 
-#ifdef WITH_RTC
+// 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;
 uint64_t s_microseconds_offset;
 #endif
 #endif
 
 
-#if defined(WITH_RTC) || defined(WITH_FRC1)
+#if defined(WITH_RTC) || defined(WITH_FRC)
 static void set_boot_time(uint64_t time_us)
 static void set_boot_time(uint64_t time_us)
 {
 {
     _lock_acquire(&s_boot_time_lock);
     _lock_acquire(&s_boot_time_lock);
@@ -109,7 +111,7 @@ static uint64_t get_boot_time()
     _lock_release(&s_boot_time_lock);
     _lock_release(&s_boot_time_lock);
     return result;
     return result;
 }
 }
-#endif //defined(WITH_RTC) || defined(WITH_FRC1)
+#endif //defined(WITH_RTC) || defined(WITH_FRC)
 
 
 
 
 void esp_clk_slowclk_cal_set(uint32_t new_cal)
 void esp_clk_slowclk_cal_set(uint32_t new_cal)
@@ -139,10 +141,10 @@ uint32_t esp_clk_slowclk_cal_get()
 
 
 void esp_set_time_from_rtc()
 void esp_set_time_from_rtc()
 {
 {
-#if defined( WITH_FRC1 ) && defined( WITH_RTC )
+#if defined( WITH_FRC ) && defined( WITH_RTC )
     // initialize time from RTC clock
     // initialize time from RTC clock
     s_microseconds_offset = get_rtc_time_us() - esp_timer_get_time();
     s_microseconds_offset = get_rtc_time_us() - esp_timer_get_time();
-#endif // WITH_FRC1 && WITH_RTC
+#endif // WITH_FRC && WITH_RTC
 }
 }
 
 
 uint64_t esp_clk_rtc_time(void)
 uint64_t esp_clk_rtc_time(void)
@@ -166,23 +168,27 @@ clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
     return (clock_t) tv.tv_sec;
     return (clock_t) tv.tv_sec;
 }
 }
 
 
-#if defined( WITH_FRC1 ) || defined( WITH_RTC )
+#if defined( WITH_FRC ) || defined( WITH_RTC )
 static uint64_t get_time_since_boot()
 static uint64_t get_time_since_boot()
 {
 {
     uint64_t microseconds = 0;
     uint64_t microseconds = 0;
-#ifdef WITH_FRC1
+#ifdef WITH_FRC
+#ifdef WITH_RTC
     microseconds = s_microseconds_offset + esp_timer_get_time();
     microseconds = s_microseconds_offset + esp_timer_get_time();
+#else
+    microseconds = esp_timer_get_time();
+#endif // WITH_RTC
 #elif defined(WITH_RTC)
 #elif defined(WITH_RTC)
     microseconds = get_rtc_time_us();
     microseconds = get_rtc_time_us();
-#endif
+#endif // WITH_FRC
     return microseconds;
     return microseconds;
 }
 }
-#endif // defined( WITH_FRC1 ) || defined( WITH_RTC )
+#endif // defined( WITH_FRC ) || defined( WITH_RTC )
 
 
 int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
 int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
 {
 {
     (void) tz;
     (void) tz;
-#if defined( WITH_FRC1 ) || defined( WITH_RTC )
+#if defined( WITH_FRC ) || defined( WITH_RTC )
     if (tv) {
     if (tv) {
         uint64_t microseconds = get_boot_time() + get_time_since_boot();
         uint64_t microseconds = get_boot_time() + get_time_since_boot();
         tv->tv_sec = microseconds / 1000000;
         tv->tv_sec = microseconds / 1000000;
@@ -192,13 +198,13 @@ int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
 #else
 #else
     __errno_r(r) = ENOSYS;
     __errno_r(r) = ENOSYS;
     return -1;
     return -1;
-#endif // defined( WITH_FRC1 ) || defined( WITH_RTC )
+#endif // defined( WITH_FRC ) || defined( WITH_RTC )
 }
 }
 
 
 int settimeofday(const struct timeval *tv, const struct timezone *tz)
 int settimeofday(const struct timeval *tv, const struct timezone *tz)
 {
 {
     (void) tz;
     (void) tz;
-#if defined( WITH_FRC1 ) || defined( WITH_RTC )
+#if defined( WITH_FRC ) || defined( WITH_RTC )
     if (tv) {
     if (tv) {
         uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec;
         uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec;
         uint64_t since_boot = get_time_since_boot();
         uint64_t since_boot = get_time_since_boot();
@@ -233,7 +239,7 @@ unsigned int sleep(unsigned int seconds)
 
 
 uint32_t system_get_time(void)
 uint32_t system_get_time(void)
 {
 {
-#if defined( WITH_FRC1 ) || defined( WITH_RTC )
+#if defined( WITH_FRC ) || defined( WITH_RTC )
     return get_time_since_boot();
     return get_time_since_boot();
 #else
 #else
     return 0;
     return 0;
@@ -244,7 +250,7 @@ uint32_t system_get_current_time(void) __attribute__((alias("system_get_time")))
 
 
 uint32_t system_relative_time(uint32_t current_time)
 uint32_t system_relative_time(uint32_t current_time)
 {
 {
-#if defined( WITH_FRC1 ) || defined( WITH_RTC )
+#if defined( WITH_FRC ) || defined( WITH_RTC )
     return get_time_since_boot() - current_time;
     return get_time_since_boot() - current_time;
 #else
 #else
     return 0;
     return 0;