2021-11-06 05:25:49 -04:00
|
|
|
/*
|
2022-01-17 21:32:56 -05:00
|
|
|
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
|
2021-11-06 05:25:49 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-06-22 23:53:58 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
2020-08-23 23:33:57 -04:00
|
|
|
#include <sys/lock.h>
|
2020-06-22 23:53:58 -04:00
|
|
|
|
2021-03-12 04:30:21 -05:00
|
|
|
#include "esp_attr.h"
|
2020-06-22 23:53:58 -04:00
|
|
|
#include "esp_system.h"
|
|
|
|
|
|
|
|
#include "soc/rtc.h"
|
|
|
|
#include "esp_rom_sys.h"
|
|
|
|
|
2020-08-11 05:49:48 -04:00
|
|
|
#include "esp_private/system_internal.h"
|
2021-11-18 22:42:01 -05:00
|
|
|
#include "esp_private/esp_clk.h"
|
2020-08-11 05:49:48 -04:00
|
|
|
|
2020-06-22 23:53:58 -04:00
|
|
|
#include "esp_time_impl.h"
|
|
|
|
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#include "esp32/rom/rtc.h"
|
2020-06-23 04:07:09 -04:00
|
|
|
#include "esp32/rtc.h"
|
2020-06-22 23:53:58 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/rom/rtc.h"
|
2020-06-23 04:07:09 -04:00
|
|
|
#include "esp32s2/rtc.h"
|
2020-07-29 01:13:51 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/rom/rtc.h"
|
|
|
|
#include "esp32s3/rtc.h"
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/rom/rtc.h"
|
|
|
|
#include "esp32c3/rtc.h"
|
2021-06-10 07:47:41 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
#include "esp32h2/rom/rtc.h"
|
|
|
|
#include "esp32h2/rtc.h"
|
2022-01-17 21:32:56 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C2
|
|
|
|
#include "esp32c2/rom/rtc.h"
|
|
|
|
#include "esp32c2/rtc.h"
|
2020-06-22 23:53:58 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-12-14 06:01:19 -05:00
|
|
|
// Offset between High resolution timer and the RTC.
|
2020-06-22 23:53:58 -04:00
|
|
|
// Initialized after reset or light sleep.
|
2020-10-20 02:09:32 -04:00
|
|
|
#if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER)
|
2021-03-08 10:00:31 -05:00
|
|
|
int64_t s_microseconds_offset = 0;
|
2020-06-22 23:53:58 -04:00
|
|
|
#endif
|
|
|
|
|
2020-10-20 02:09:32 -04:00
|
|
|
#ifndef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
|
2020-06-22 23:53:58 -04:00
|
|
|
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
|
|
|
|
|
2020-08-23 23:33:57 -04:00
|
|
|
static _lock_t s_boot_time_lock;
|
2020-06-22 23:53:58 -04:00
|
|
|
|
2020-10-20 02:09:32 -04:00
|
|
|
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
|
2020-06-22 23:53:58 -04:00
|
|
|
uint64_t esp_time_impl_get_time_since_boot(void)
|
|
|
|
{
|
|
|
|
uint64_t microseconds = 0;
|
|
|
|
|
2020-10-20 02:09:32 -04:00
|
|
|
#ifdef CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
|
|
|
|
#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
|
2020-06-23 04:46:06 -04:00
|
|
|
microseconds = s_microseconds_offset + esp_system_get_time();
|
2020-06-22 23:53:58 -04:00
|
|
|
#else
|
2020-06-23 04:46:06 -04:00
|
|
|
microseconds = esp_system_get_time();
|
2020-10-20 02:09:32 -04:00
|
|
|
#endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
|
|
|
|
#elif defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
|
2020-06-23 04:07:09 -04:00
|
|
|
microseconds = esp_rtc_get_time_us();
|
2020-10-20 02:09:32 -04:00
|
|
|
#endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
|
2020-06-22 23:53:58 -04:00
|
|
|
return microseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t esp_time_impl_get_time(void)
|
|
|
|
{
|
2020-06-23 04:46:06 -04:00
|
|
|
uint64_t microseconds = 0;
|
2020-10-20 02:09:32 -04:00
|
|
|
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
|
2020-06-23 04:46:06 -04:00
|
|
|
microseconds = esp_system_get_time();
|
2020-10-20 02:09:32 -04:00
|
|
|
#elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
|
2020-06-23 04:46:06 -04:00
|
|
|
microseconds = esp_rtc_get_time_us();
|
2020-10-20 02:09:32 -04:00
|
|
|
#endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
|
2020-06-23 04:46:06 -04:00
|
|
|
return microseconds;
|
2020-06-22 23:53:58 -04:00
|
|
|
}
|
2020-06-23 04:46:06 -04:00
|
|
|
|
2020-10-20 02:09:32 -04:00
|
|
|
#endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
|
2020-06-22 23:53:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
void esp_time_impl_set_boot_time(uint64_t time_us)
|
|
|
|
{
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_acquire(&s_boot_time_lock);
|
2020-10-20 02:09:32 -04:00
|
|
|
#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
|
2020-06-22 23:53:58 -04:00
|
|
|
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
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_release(&s_boot_time_lock);
|
2020-06-22 23:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t esp_time_impl_get_boot_time(void)
|
|
|
|
{
|
|
|
|
uint64_t result;
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_acquire(&s_boot_time_lock);
|
2020-10-20 02:09:32 -04:00
|
|
|
#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
|
2020-06-22 23:53:58 -04:00
|
|
|
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
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_release(&s_boot_time_lock);
|
2020-06-22 23:53:58 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_set_time_from_rtc(void)
|
|
|
|
{
|
2020-10-20 02:09:32 -04:00
|
|
|
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
|
2020-06-22 23:53:58 -04:00
|
|
|
// initialize time from RTC clock
|
2020-06-23 04:46:06 -04:00
|
|
|
s_microseconds_offset = esp_rtc_get_time_us() - esp_system_get_time();
|
2020-10-20 02:09:32 -04:00
|
|
|
#endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER && CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
|
2020-06-22 23:53:58 -04:00
|
|
|
}
|
|
|
|
|
2021-12-14 06:01:19 -05:00
|
|
|
void esp_sync_timekeeping_timers(void)
|
2020-06-22 23:53:58 -04:00
|
|
|
{
|
2020-10-20 02:09:32 -04:00
|
|
|
#if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
|
2020-06-22 23:53:58 -04:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
settimeofday(&tv, NULL);
|
2020-06-23 04:46:06 -04:00
|
|
|
int64_t s_microseconds_offset_cur = esp_rtc_get_time_us() - esp_system_get_time();
|
2020-06-22 23:53:58 -04:00
|
|
|
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();
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|