From 5eee2bf37def04f8620aa46a46044051ffa56e52 Mon Sep 17 00:00:00 2001 From: krzychb Date: Sun, 9 Sep 2018 16:13:26 +0200 Subject: [PATCH 1/2] Corrected ULP wakeup period setup API to account for time the ULP FSM spends on internal tasks before being able to execute the program. Inspired by https://esp32.com/viewtopic.php?f=2&t=7081. --- components/ulp/include/esp32/ulp.h | 9 +++++++++ components/ulp/ulp.c | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/components/ulp/include/esp32/ulp.h b/components/ulp/include/esp32/ulp.h index 8acbd7120..e9668593d 100644 --- a/components/ulp/include/esp32/ulp.h +++ b/components/ulp/include/esp32/ulp.h @@ -23,6 +23,8 @@ extern "C" { #endif +#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 */ /** * @defgroup ulp_registers ULP coprocessor registers @@ -898,6 +900,13 @@ esp_err_t ulp_run(uint32_t entry_point); * * @param period_index wakeup period setting number (0 - 4) * @param period_us wakeup period, us + * @note The ULP FSM requires some time to wakeup before being able to run the program. + * Then additional time is reserved after wakeup waiting until the 8M clock is stable. + * The FSM also requires time to go to sleep after the program execution is halted. + * The minimum wakeup period that may be set up for the ULP + * is the total time spent on the above internal tasks. + * For a default configuration of the ULP running at 150kHz + * the minimum wakeup period is about 160us. * @return * - ESP_OK on success * - ESP_ERR_INVALID_ARG if period_index is out of range diff --git a/components/ulp/ulp.c b/components/ulp/ulp.c index d325bccf3..3424da21e 100644 --- a/components/ulp/ulp.c +++ b/components/ulp/ulp.c @@ -112,6 +112,15 @@ esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us) } uint64_t period_us_64 = period_us; 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 + + ULP_FSM_WAKEUP_SLEEP_CYCLES + + REG_GET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT); + if (period_cycles < min_sleep_period_cycles) { + period_cycles = 0; + ESP_LOGW(TAG, "Sleep period clipped to minimum of %d cycles", (uint32_t) min_sleep_period_cycles); + } else { + period_cycles -= min_sleep_period_cycles; + } REG_SET_FIELD(SENS_ULP_CP_SLEEP_CYC0_REG + period_index * sizeof(uint32_t), SENS_SLEEP_CYCLES_S0, (uint32_t) period_cycles); return ESP_OK; From f6b0b270263e8e206a04889322ebc509a2798d3f Mon Sep 17 00:00:00 2001 From: krzychb Date: Mon, 10 Sep 2018 07:14:19 +0200 Subject: [PATCH 2/2] Corrected number of FSM cycles and related description --- components/ulp/include/esp32/ulp.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/components/ulp/include/esp32/ulp.h b/components/ulp/include/esp32/ulp.h index e9668593d..6960ac97d 100644 --- a/components/ulp/include/esp32/ulp.h +++ b/components/ulp/include/esp32/ulp.h @@ -900,13 +900,12 @@ esp_err_t ulp_run(uint32_t entry_point); * * @param period_index wakeup period setting number (0 - 4) * @param period_us wakeup period, us - * @note The ULP FSM requires some time to wakeup before being able to run the program. - * Then additional time is reserved after wakeup waiting until the 8M clock is stable. - * The FSM also requires time to go to sleep after the program execution is halted. + * @note The ULP FSM requires two clock cycles to wakeup before being able to run the program. + * Then additional 16 cycles are reserved after wakeup waiting until the 8M clock is stable. + * The FSM also requires two more clock cycles to go to sleep after the program execution is halted. * The minimum wakeup period that may be set up for the ULP - * is the total time spent on the above internal tasks. - * For a default configuration of the ULP running at 150kHz - * the minimum wakeup period is about 160us. + * is equal to the total number of cycles spent on the above internal tasks. + * For a default configuration of the ULP running at 150kHz it makes about 133us. * @return * - ESP_OK on success * - ESP_ERR_INVALID_ARG if period_index is out of range