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.

This commit is contained in:
krzychb
2018-09-09 16:13:26 +02:00
parent 27d1b04500
commit 5eee2bf37d
2 changed files with 18 additions and 0 deletions

View File

@@ -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;