2021-08-05 11:35:07 -04:00
|
|
|
/*
|
2022-01-17 21:32:56 -05:00
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
2021-08-05 11:35:07 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2021-01-29 01:40:59 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/param.h>
|
2021-05-21 02:45:50 -04:00
|
|
|
#include <sys/lock.h>
|
2021-01-29 01:40:59 -05:00
|
|
|
|
2022-07-28 01:49:44 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
2021-01-29 01:40:59 -05:00
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "soc/rtc.h"
|
2021-11-06 05:23:21 -04:00
|
|
|
#include "soc/soc_caps.h"
|
|
|
|
#include "esp_rom_caps.h"
|
2022-01-17 04:44:25 -05:00
|
|
|
#include "esp_rom_sys.h"
|
2021-11-18 22:42:01 -05:00
|
|
|
#include "esp_private/esp_clk.h"
|
2022-05-26 14:23:17 -04:00
|
|
|
#include "hal/clk_tree_ll.h"
|
2021-01-29 01:40:59 -05:00
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#include "esp32/rom/rtc.h"
|
|
|
|
#include "esp32/rtc.h"
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/rom/rtc.h"
|
|
|
|
#include "esp32s2/rtc.h"
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/rom/rtc.h"
|
|
|
|
#include "esp32s3/rtc.h"
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/rom/rtc.h"
|
|
|
|
#include "esp32c3/rtc.h"
|
2021-06-10 03:22:43 -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"
|
2021-01-29 01:40:59 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MHZ (1000000)
|
|
|
|
|
|
|
|
// g_ticks_us defined in ROMs for PRO and APP CPU
|
|
|
|
extern uint32_t g_ticks_per_us_pro;
|
2022-03-25 06:41:25 -04:00
|
|
|
#if SOC_CPU_CORES_NUM > 1
|
2021-01-29 01:40:59 -05:00
|
|
|
#ifndef CONFIG_FREERTOS_UNICORE
|
|
|
|
extern uint32_t g_ticks_per_us_app;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-07-28 01:49:44 -04:00
|
|
|
static portMUX_TYPE s_esp_rtc_time_lock = portMUX_INITIALIZER_UNLOCKED;
|
2021-11-06 05:23:21 -04:00
|
|
|
|
|
|
|
// TODO: IDF-4239
|
2022-10-27 09:45:38 -04:00
|
|
|
static RTC_NOINIT_ATTR uint64_t s_esp_rtc_time_us, s_rtc_last_ticks;
|
2021-01-29 01:40:59 -05:00
|
|
|
|
2021-04-22 20:28:42 -04:00
|
|
|
inline static int IRAM_ATTR s_get_cpu_freq_mhz(void)
|
2021-01-29 01:40:59 -05:00
|
|
|
{
|
2021-11-06 05:23:21 -04:00
|
|
|
#if ESP_ROM_GET_CLK_FREQ
|
2022-01-17 04:44:25 -05:00
|
|
|
return esp_rom_get_cpu_ticks_per_us();
|
2021-01-29 01:40:59 -05:00
|
|
|
#else
|
2021-04-22 20:28:42 -04:00
|
|
|
return g_ticks_per_us_pro;
|
2021-01-29 01:40:59 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:28:42 -04:00
|
|
|
int IRAM_ATTR esp_clk_cpu_freq(void)
|
|
|
|
{
|
|
|
|
return s_get_cpu_freq_mhz() * MHZ;
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:40:59 -05:00
|
|
|
int IRAM_ATTR esp_clk_apb_freq(void)
|
|
|
|
{
|
2021-11-14 22:03:23 -05:00
|
|
|
return MIN(s_get_cpu_freq_mhz() * MHZ, APB_CLK_FREQ);
|
2021-01-29 01:40:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int IRAM_ATTR esp_clk_xtal_freq(void)
|
|
|
|
{
|
|
|
|
return rtc_clk_xtal_freq_get() * MHZ;
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:32:56 -05:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32C2
|
2021-01-29 01:40:59 -05:00
|
|
|
void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us)
|
|
|
|
{
|
|
|
|
/* Update scale factors used by esp_rom_delay_us */
|
|
|
|
g_ticks_per_us_pro = ticks_per_us;
|
2022-03-25 06:41:25 -04:00
|
|
|
#if SOC_CPU_CORES_NUM > 1
|
2021-01-29 01:40:59 -05:00
|
|
|
#ifndef CONFIG_FREERTOS_UNICORE
|
|
|
|
g_ticks_per_us_app = ticks_per_us;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint64_t esp_rtc_get_time_us(void)
|
|
|
|
{
|
2021-11-06 05:23:21 -04:00
|
|
|
#if !SOC_RTC_FAST_MEM_SUPPORTED
|
|
|
|
//IDF-3901
|
|
|
|
return 0;
|
|
|
|
#endif
|
2022-07-28 01:49:44 -04:00
|
|
|
portENTER_CRITICAL_SAFE(&s_esp_rtc_time_lock);
|
2021-01-29 01:40:59 -05:00
|
|
|
const uint32_t cal = esp_clk_slowclk_cal_get();
|
2022-10-27 09:45:38 -04:00
|
|
|
if (cal == 0) {
|
|
|
|
s_esp_rtc_time_us = 0;
|
|
|
|
s_rtc_last_ticks = 0;
|
|
|
|
}
|
2021-01-29 01:40:59 -05:00
|
|
|
const uint64_t rtc_this_ticks = rtc_time_get();
|
|
|
|
const uint64_t ticks = rtc_this_ticks - s_rtc_last_ticks;
|
|
|
|
/* 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;
|
|
|
|
const uint64_t delta_time_us = ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) +
|
2021-11-14 22:03:23 -05:00
|
|
|
((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT));
|
2021-01-29 01:40:59 -05:00
|
|
|
s_esp_rtc_time_us += delta_time_us;
|
|
|
|
s_rtc_last_ticks = rtc_this_ticks;
|
2022-07-28 01:49:44 -04:00
|
|
|
portEXIT_CRITICAL_SAFE(&s_esp_rtc_time_lock);
|
2021-01-29 01:40:59 -05:00
|
|
|
return s_esp_rtc_time_us;
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_clk_slowclk_cal_set(uint32_t new_cal)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
|
|
|
|
/* To force monotonic time values even when clock calibration value changes,
|
|
|
|
* we adjust esp_rtc_time
|
|
|
|
*/
|
|
|
|
esp_rtc_get_time_us();
|
|
|
|
#endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
|
2022-05-26 14:23:17 -04:00
|
|
|
clk_ll_rtc_slow_store_cal(new_cal);
|
2021-01-29 01:40:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t esp_clk_slowclk_cal_get(void)
|
|
|
|
{
|
2022-05-26 14:23:17 -04:00
|
|
|
return clk_ll_rtc_slow_load_cal();
|
2021-01-29 01:40:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t esp_clk_rtc_time(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
|
|
|
|
return esp_rtc_get_time_us();
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
2022-07-28 02:20:16 -04:00
|
|
|
|
|
|
|
void esp_clk_private_lock(void)
|
|
|
|
{
|
|
|
|
portENTER_CRITICAL(&s_esp_rtc_time_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_clk_private_unlock(void)
|
|
|
|
{
|
|
|
|
portEXIT_CRITICAL(&s_esp_rtc_time_lock);
|
|
|
|
}
|