]> granicus.if.org Git - esp-idf/commitdiff
soc/rtc: add a function to wait for slow clock cycle
authorIvan Grokhotkov <ivan@espressif.com>
Thu, 26 Oct 2017 10:33:13 +0000 (18:33 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Thu, 26 Oct 2017 11:53:53 +0000 (19:53 +0800)
Some RTC features are synchronized to RTC_SLOW_CLK, so sometimes
software needs to wait for the next slow clock cycle.
This function implements waiting using Timer Group clock calibration
feature.

components/soc/esp32/include/soc/rtc.h
components/soc/esp32/rtc_time.c

index f13c113b4e487e63b16cdcf2e9d0ba6bc1f30352..e2fa4791f058b32329f709d760f29253d04a3c1a 100644 (file)
@@ -400,6 +400,15 @@ uint64_t rtc_time_slowclk_to_us(uint64_t rtc_cycles, uint32_t period);
  */
 uint64_t rtc_time_get();
 
+/**
+ * @brief Busy loop until next RTC_SLOW_CLK cycle
+ *
+ * This function returns not earlier than the next RTC_SLOW_CLK clock cycle.
+ * In some cases (e.g. when RTC_SLOW_CLK cycle is very close), it may return
+ * one RTC_SLOW_CLK cycle later.
+ */
+void rtc_clk_wait_for_slow_cycle();
+
 /**
  * @brief sleep configuration for rtc_sleep_init function
  */
index 07a9337a301b83ebc5cb847bc1701b9e965250d7..6f354f88495380d236f176dc323537c5b5a98b3b 100644 (file)
@@ -135,3 +135,20 @@ uint64_t rtc_time_get()
     t |= ((uint64_t) READ_PERI_REG(RTC_CNTL_TIME1_REG)) << 32;
     return t;
 }
+
+void rtc_clk_wait_for_slow_cycle()
+{
+    REG_CLR_BIT(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START_CYCLING | TIMG_RTC_CALI_START);
+    REG_CLR_BIT(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY);
+    REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_CLK_SEL, RTC_CAL_RTC_MUX);
+    /* Request to run calibration for 0 slow clock cycles.
+     * RDY bit will be set on the nearest slow clock cycle.
+     */
+    REG_SET_FIELD(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_MAX, 0);
+    REG_SET_BIT(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
+    ets_delay_us(1); /* RDY needs some time to go low */
+    while (!GET_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_RDY)) {
+        ets_delay_us(1);
+    }
+}
+