1 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <stdint.h>
15 #include <time.h>
16 #include <sys/time.h>
17 #include <sys/lock.h>
18 
19 #include "esp_attr.h"
20 #include "esp_system.h"
21 
22 #include "soc/rtc.h"
23 #include "esp_rom_sys.h"
24 
25 #include "esp_private/system_internal.h"
26 
27 #include "esp_time_impl.h"
28 
29 #include "sdkconfig.h"
30 
31 #if CONFIG_IDF_TARGET_ESP32
32 #include "esp32/rom/rtc.h"
33 #include "esp32/clk.h"
34 #include "esp32/rtc.h"
35 #elif CONFIG_IDF_TARGET_ESP32S2
36 #include "esp32s2/rom/rtc.h"
37 #include "esp32s2/clk.h"
38 #include "esp32s2/rtc.h"
39 #elif CONFIG_IDF_TARGET_ESP32S3
40 #include "esp32s3/rom/rtc.h"
41 #include "esp32s3/clk.h"
42 #include "esp32s3/rtc.h"
43 #elif CONFIG_IDF_TARGET_ESP32C3
44 #include "esp32c3/rom/rtc.h"
45 #include "esp32c3/clk.h"
46 #include "esp32c3/rtc.h"
47 #endif
48 
49 
50 
51 // Offset between FRC timer and the RTC.
52 // Initialized after reset or light sleep.
53 #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER)
54 uint64_t s_microseconds_offset;
55 #endif
56 
57 #ifndef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
58 static uint64_t s_boot_time; // when RTC is used to persist time, two RTC_STORE registers are used to store boot time instead
59 #endif
60 
61 static _lock_t s_boot_time_lock;
62 
63 static _lock_t s_esp_rtc_time_lock;
64 static RTC_DATA_ATTR uint64_t s_esp_rtc_time_us = 0, s_rtc_last_ticks = 0;
65 
66 #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)67 uint64_t esp_time_impl_get_time_since_boot(void)
68 {
69     uint64_t microseconds = 0;
70 
71 #ifdef CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
72 #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
73     microseconds = s_microseconds_offset + esp_system_get_time();
74 #else
75     microseconds = esp_system_get_time();
76 #endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
77 #elif defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
78     microseconds = esp_rtc_get_time_us();
79 #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
80     return microseconds;
81 }
82 
esp_time_impl_get_time(void)83 uint64_t esp_time_impl_get_time(void)
84 {
85     uint64_t microseconds = 0;
86 #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
87     microseconds = esp_system_get_time();
88 #elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
89     microseconds = esp_rtc_get_time_us();
90 #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
91     return microseconds;
92 }
93 
94 #endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
95 
96 
esp_time_impl_set_boot_time(uint64_t time_us)97 void esp_time_impl_set_boot_time(uint64_t time_us)
98 {
99     _lock_acquire(&s_boot_time_lock);
100 #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
101     REG_WRITE(RTC_BOOT_TIME_LOW_REG, (uint32_t) (time_us & 0xffffffff));
102     REG_WRITE(RTC_BOOT_TIME_HIGH_REG, (uint32_t) (time_us >> 32));
103 #else
104     s_boot_time = time_us;
105 #endif
106     _lock_release(&s_boot_time_lock);
107 }
108 
esp_clk_rtc_time(void)109 uint64_t esp_clk_rtc_time(void)
110 {
111 #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
112     return esp_rtc_get_time_us();
113 #else
114     return 0;
115 #endif
116 }
117 
esp_time_impl_get_boot_time(void)118 uint64_t esp_time_impl_get_boot_time(void)
119 {
120     uint64_t result;
121     _lock_acquire(&s_boot_time_lock);
122 #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
123     result = ((uint64_t) REG_READ(RTC_BOOT_TIME_LOW_REG)) + (((uint64_t) REG_READ(RTC_BOOT_TIME_HIGH_REG)) << 32);
124 #else
125     result = s_boot_time;
126 #endif
127     _lock_release(&s_boot_time_lock);
128     return result;
129 }
130 
esp_clk_slowclk_cal_get(void)131 uint32_t esp_clk_slowclk_cal_get(void)
132 {
133     return REG_READ(RTC_SLOW_CLK_CAL_REG);
134 }
135 
esp_rtc_get_time_us(void)136 uint64_t esp_rtc_get_time_us(void)
137 {
138     _lock_acquire(&s_esp_rtc_time_lock);
139     const uint32_t cal = esp_clk_slowclk_cal_get();
140     const uint64_t rtc_this_ticks = rtc_time_get();
141     const uint64_t ticks = rtc_this_ticks - s_rtc_last_ticks;
142     /* RTC counter result is up to 2^48, calibration factor is up to 2^24,
143      * for a 32kHz clock. We need to calculate (assuming no overflow):
144      *   (ticks * cal) >> RTC_CLK_CAL_FRACT
145      *
146      * An overflow in the (ticks * cal) multiplication would cause time to
147      * wrap around after approximately 13 days, which is probably not enough
148      * for some applications.
149      * Therefore multiplication is split into two terms, for the lower 32-bit
150      * and the upper 16-bit parts of "ticks", i.e.:
151      *   ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT
152      */
153     const uint64_t ticks_low = ticks & UINT32_MAX;
154     const uint64_t ticks_high = ticks >> 32;
155     const uint64_t delta_time_us = ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) +
156            ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT));
157     s_esp_rtc_time_us += delta_time_us;
158     s_rtc_last_ticks = rtc_this_ticks;
159     _lock_release(&s_esp_rtc_time_lock);
160     return s_esp_rtc_time_us;
161 }
162 
esp_clk_slowclk_cal_set(uint32_t new_cal)163 void esp_clk_slowclk_cal_set(uint32_t new_cal)
164 {
165 #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
166     /* To force monotonic time values even when clock calibration value changes,
167      * we adjust esp_rtc_time
168      */
169     esp_rtc_get_time_us();
170 #endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
171     REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal);
172 }
173 
esp_set_time_from_rtc(void)174 void esp_set_time_from_rtc(void)
175 {
176 #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
177     // initialize time from RTC clock
178     s_microseconds_offset = esp_rtc_get_time_us() - esp_system_get_time();
179 #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER && CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
180 }
181 
esp_sync_counters_rtc_and_frc(void)182 void esp_sync_counters_rtc_and_frc(void)
183 {
184 #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
185     struct timeval tv;
186     gettimeofday(&tv, NULL);
187     settimeofday(&tv, NULL);
188     int64_t s_microseconds_offset_cur = esp_rtc_get_time_us() - esp_system_get_time();
189     esp_time_impl_set_boot_time(esp_time_impl_get_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur));
190 #endif
191 }
192 
esp_time_impl_init(void)193 void esp_time_impl_init(void)
194 {
195     esp_set_time_from_rtc();
196 }
197