ulp_common.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include "esp_err.h"
  8. #include "esp_log.h"
  9. #include "ulp_common.h"
  10. #include "esp_private/esp_clk.h"
  11. #include "soc/rtc.h"
  12. #include "soc/rtc_cntl_reg.h"
  13. #include "soc/sens_reg.h"
  14. #if CONFIG_IDF_TARGET_ESP32
  15. #define ULP_FSM_PREPARE_SLEEP_CYCLES 2 /*!< Cycles spent by FSM preparing ULP for sleep */
  16. #define ULP_FSM_WAKEUP_SLEEP_CYCLES 2 /*!< Cycles spent by FSM waking up ULP from sleep */
  17. #endif
  18. esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us)
  19. {
  20. if (period_index > 4) {
  21. return ESP_ERR_INVALID_ARG;
  22. }
  23. uint64_t period_us_64 = period_us;
  24. #if CONFIG_IDF_TARGET_ESP32
  25. uint64_t period_cycles = (period_us_64 << RTC_CLK_CAL_FRACT) / esp_clk_slowclk_cal_get();
  26. uint64_t min_sleep_period_cycles = ULP_FSM_PREPARE_SLEEP_CYCLES
  27. + ULP_FSM_WAKEUP_SLEEP_CYCLES
  28. + REG_GET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT);
  29. if (period_cycles < min_sleep_period_cycles) {
  30. period_cycles = min_sleep_period_cycles;
  31. ESP_LOGW("ulp", "Sleep period clipped to minimum of %d cycles", (uint32_t) min_sleep_period_cycles);
  32. } else {
  33. period_cycles -= min_sleep_period_cycles;
  34. }
  35. REG_SET_FIELD(SENS_ULP_CP_SLEEP_CYC0_REG + period_index * sizeof(uint32_t),
  36. SENS_SLEEP_CYCLES_S0, (uint32_t) period_cycles);
  37. #elif defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
  38. rtc_slow_freq_t slow_clk_freq = rtc_clk_slow_freq_get();
  39. rtc_slow_freq_t rtc_slow_freq_x32k = RTC_SLOW_FREQ_32K_XTAL;
  40. rtc_slow_freq_t rtc_slow_freq_8MD256 = RTC_SLOW_FREQ_8MD256;
  41. rtc_cal_sel_t cal_clk = RTC_CAL_RTC_MUX;
  42. if (slow_clk_freq == (rtc_slow_freq_x32k)) {
  43. cal_clk = RTC_CAL_32K_XTAL;
  44. } else if (slow_clk_freq == rtc_slow_freq_8MD256) {
  45. cal_clk = RTC_CAL_8MD256;
  46. }
  47. uint32_t slow_clk_period = rtc_clk_cal(cal_clk, 100);
  48. uint64_t period_cycles = rtc_time_us_to_slowclk(period_us_64, slow_clk_period);
  49. REG_SET_FIELD(RTC_CNTL_ULP_CP_TIMER_1_REG, RTC_CNTL_ULP_CP_TIMER_SLP_CYCLE, ((uint32_t)period_cycles));
  50. #endif
  51. return ESP_OK;
  52. }
  53. void ulp_timer_stop(void)
  54. {
  55. #if CONFIG_IDF_TARGET_ESP32
  56. CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  57. #else
  58. CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  59. #endif
  60. }
  61. void ulp_timer_resume(void)
  62. {
  63. #if CONFIG_IDF_TARGET_ESP32
  64. SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  65. #else
  66. SET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  67. #endif
  68. }