1 /*
2  * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdint.h>
7 #include <time.h>
8 #include <sys/time.h>
9 #include <sys/lock.h>
10 
11 #include "esp_attr.h"
12 #include "esp_system.h"
13 
14 #include "soc/rtc.h"
15 #include "esp_rom_sys.h"
16 
17 #include "esp_private/system_internal.h"
18 #include "esp_private/esp_clk.h"
19 
20 #include "esp_time_impl.h"
21 
22 #include "sdkconfig.h"
23 
24 #if CONFIG_IDF_TARGET_ESP32
25 #include "esp32/rom/rtc.h"
26 #include "esp32/rtc.h"
27 #elif CONFIG_IDF_TARGET_ESP32S2
28 #include "esp32s2/rom/rtc.h"
29 #include "esp32s2/rtc.h"
30 #elif CONFIG_IDF_TARGET_ESP32S3
31 #include "esp32s3/rom/rtc.h"
32 #include "esp32s3/rtc.h"
33 #elif CONFIG_IDF_TARGET_ESP32C3
34 #include "esp32c3/rom/rtc.h"
35 #include "esp32c3/rtc.h"
36 #elif CONFIG_IDF_TARGET_ESP32C2
37 #include "esp32c2/rom/rtc.h"
38 #include "esp32c2/rtc.h"
39 #elif CONFIG_IDF_TARGET_ESP32C6
40 #include "esp32c6/rom/rtc.h"
41 #include "esp32c6/rtc.h"
42 #elif CONFIG_IDF_TARGET_ESP32H2
43 #include "esp32h2/rom/rtc.h"
44 #include "esp32h2/rtc.h"
45 #endif
46 
47 
48 
49 // Offset between High resolution timer and the RTC.
50 // Initialized after reset or light sleep.
51 #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER)
52 int64_t s_microseconds_offset = 0;
53 #endif
54 
55 #ifndef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
56 static uint64_t s_boot_time; // when RTC is used to persist time, two RTC_STORE registers are used to store boot time instead
57 #endif
58 
59 static _lock_t s_boot_time_lock;
60 
61 #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
esp_time_impl_get_time_since_boot(void)62 uint64_t esp_time_impl_get_time_since_boot(void)
63 {
64     uint64_t microseconds = 0;
65 
66 #ifdef CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
67 #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
68     microseconds = s_microseconds_offset + esp_system_get_time();
69 #else
70     microseconds = esp_system_get_time();
71 #endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
72 #elif defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
73     microseconds = esp_rtc_get_time_us();
74 #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
75     return microseconds;
76 }
77 
esp_time_impl_get_time(void)78 uint64_t esp_time_impl_get_time(void)
79 {
80     uint64_t microseconds = 0;
81 #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
82     microseconds = esp_system_get_time();
83 #elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
84     microseconds = esp_rtc_get_time_us();
85 #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
86     return microseconds;
87 }
88 
89 #endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
90 
91 
esp_time_impl_set_boot_time(uint64_t time_us)92 void esp_time_impl_set_boot_time(uint64_t time_us)
93 {
94     _lock_acquire(&s_boot_time_lock);
95 #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
96     REG_WRITE(RTC_BOOT_TIME_LOW_REG, (uint32_t) (time_us & 0xffffffff));
97     REG_WRITE(RTC_BOOT_TIME_HIGH_REG, (uint32_t) (time_us >> 32));
98 #else
99     s_boot_time = time_us;
100 #endif
101     _lock_release(&s_boot_time_lock);
102 }
103 
esp_time_impl_get_boot_time(void)104 uint64_t esp_time_impl_get_boot_time(void)
105 {
106     uint64_t result;
107     _lock_acquire(&s_boot_time_lock);
108 #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
109     result = ((uint64_t) REG_READ(RTC_BOOT_TIME_LOW_REG)) + (((uint64_t) REG_READ(RTC_BOOT_TIME_HIGH_REG)) << 32);
110 #else
111     result = s_boot_time;
112 #endif
113     _lock_release(&s_boot_time_lock);
114     return result;
115 }
116 
esp_set_time_from_rtc(void)117 void esp_set_time_from_rtc(void)
118 {
119 #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
120     // initialize time from RTC clock
121     s_microseconds_offset = esp_rtc_get_time_us() - esp_system_get_time();
122 #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER && CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
123 }
124 
esp_sync_timekeeping_timers(void)125 void esp_sync_timekeeping_timers(void)
126 {
127 #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
128     struct timeval tv;
129     gettimeofday(&tv, NULL);
130     settimeofday(&tv, NULL);
131     int64_t s_microseconds_offset_cur = esp_rtc_get_time_us() - esp_system_get_time();
132     esp_time_impl_set_boot_time(esp_time_impl_get_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur));
133 #endif
134 }
135 
esp_time_impl_init(void)136 void esp_time_impl_init(void)
137 {
138     esp_set_time_from_rtc();
139 }
140