2022-01-21 04:13:48 -05:00
|
|
|
/*
|
2024-01-21 22:43:38 -05:00
|
|
|
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
|
2022-01-21 04:13:48 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "ulp_common.h"
|
|
|
|
#include "esp_private/esp_clk.h"
|
|
|
|
#include "soc/rtc.h"
|
2022-07-12 08:53:26 -04:00
|
|
|
#include "soc/rtc_cntl_periph.h"
|
2022-01-21 04:13:48 -05:00
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2022-07-12 08:53:26 -04:00
|
|
|
#include "soc/sens_reg.h"
|
2022-01-21 04:13:48 -05:00
|
|
|
#define ULP_FSM_PREPARE_SLEEP_CYCLES 2 /*!< Cycles spent by FSM preparing ULP for sleep */
|
|
|
|
#define ULP_FSM_WAKEUP_SLEEP_CYCLES 2 /*!< Cycles spent by FSM waking up ULP from sleep */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us)
|
|
|
|
{
|
|
|
|
if (period_index > 4) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t period_us_64 = period_us;
|
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
uint64_t period_cycles = (period_us_64 << RTC_CLK_CAL_FRACT) / esp_clk_slowclk_cal_get();
|
|
|
|
uint64_t min_sleep_period_cycles = ULP_FSM_PREPARE_SLEEP_CYCLES
|
2024-01-21 22:43:38 -05:00
|
|
|
+ ULP_FSM_WAKEUP_SLEEP_CYCLES
|
|
|
|
+ REG_GET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT);
|
2022-01-21 04:13:48 -05:00
|
|
|
if (period_cycles < min_sleep_period_cycles) {
|
|
|
|
period_cycles = min_sleep_period_cycles;
|
2023-03-12 16:47:24 -04:00
|
|
|
ESP_LOGW("ulp", "Sleep period clipped to minimum of %"PRIu32" cycles", (uint32_t) min_sleep_period_cycles);
|
2022-01-21 04:13:48 -05:00
|
|
|
} else {
|
|
|
|
period_cycles -= min_sleep_period_cycles;
|
|
|
|
}
|
|
|
|
REG_SET_FIELD(SENS_ULP_CP_SLEEP_CYC0_REG + period_index * sizeof(uint32_t),
|
2024-01-21 22:43:38 -05:00
|
|
|
SENS_SLEEP_CYCLES_S0, (uint32_t) period_cycles);
|
2022-01-21 04:13:48 -05:00
|
|
|
#elif defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
|
2022-04-21 06:24:03 -04:00
|
|
|
soc_rtc_slow_clk_src_t slow_clk_src = rtc_clk_slow_src_get();
|
2022-01-21 04:13:48 -05:00
|
|
|
rtc_cal_sel_t cal_clk = RTC_CAL_RTC_MUX;
|
2022-04-21 06:24:03 -04:00
|
|
|
if (slow_clk_src == SOC_RTC_SLOW_CLK_SRC_XTAL32K) {
|
2022-01-21 04:13:48 -05:00
|
|
|
cal_clk = RTC_CAL_32K_XTAL;
|
2022-04-21 06:24:03 -04:00
|
|
|
} else if (slow_clk_src == SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256) {
|
2022-01-21 04:13:48 -05:00
|
|
|
cal_clk = RTC_CAL_8MD256;
|
|
|
|
}
|
|
|
|
uint32_t slow_clk_period = rtc_clk_cal(cal_clk, 100);
|
|
|
|
uint64_t period_cycles = rtc_time_us_to_slowclk(period_us_64, slow_clk_period);
|
|
|
|
|
|
|
|
REG_SET_FIELD(RTC_CNTL_ULP_CP_TIMER_1_REG, RTC_CNTL_ULP_CP_TIMER_SLP_CYCLE, ((uint32_t)period_cycles));
|
|
|
|
#endif
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2022-02-17 01:14:34 -05:00
|
|
|
|
|
|
|
void ulp_timer_stop(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
|
|
|
|
#else
|
|
|
|
CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ulp_timer_resume(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
|
|
|
|
#else
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
|
|
|
|
#endif
|
|
|
|
}
|