You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ag...@apache.org on 2020/12/16 13:37:52 UTC
[incubator-nuttx] 01/02: xtensa/esp32: Changes in rtc driver to
support rtc wdt driver
This is an automated email from the ASF dual-hosted git repository.
aguettouche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
commit be12c79c52106ca009aaa861cc3cc5b4b56df7a7
Author: Sara Souza <sa...@espressif.com>
AuthorDate: Wed Dec 2 20:01:38 2020 -0300
xtensa/esp32: Changes in rtc driver to support rtc wdt driver
---
arch/xtensa/src/esp32/esp32_rtc.c | 160 +++++++++++++++-----------------------
arch/xtensa/src/esp32/esp32_rtc.h | 107 +++++++++++++++++++++++++
2 files changed, 171 insertions(+), 96 deletions(-)
diff --git a/arch/xtensa/src/esp32/esp32_rtc.c b/arch/xtensa/src/esp32/esp32_rtc.c
index 2f0171f..9e7e130 100644
--- a/arch/xtensa/src/esp32/esp32_rtc.c
+++ b/arch/xtensa/src/esp32/esp32_rtc.c
@@ -30,6 +30,7 @@
#include "hardware/esp32_rtccntl.h"
#include "hardware/esp32_dport.h"
#include "hardware/esp32_i2s.h"
+#include "esp32_rtc.h"
#include "xtensa.h"
#include "xtensa_attr.h"
@@ -79,18 +80,9 @@
#define RTC_FAST_CLK_FREQ_APPROX 8500000
-/* Number of cycles to wait from the 32k XTAL oscillator to
- * consider it running. Larger values increase startup delay.
- * Smaller values may cause false positive detection
- * (i.e. oscillator runs for a few cycles and then stops).
- */
-
-#define SLOW_CLK_CAL_CYCLES 1024
-
/* Disable logging from the ROM code. */
#define RTC_DISABLE_ROM_LOG ((1 << 0) | (1 << 16))
-#define EXT_OSC_FLAG BIT(3)
/* Default initializer for esp32_rtc_sleep_config_t
* This initializer sets all fields to "reasonable" values
@@ -136,56 +128,6 @@
* Private Types
****************************************************************************/
-/* RTC SLOW_CLK frequency values */
-
-enum esp32_rtc_slow_freq_e
-{
- RTC_SLOW_FREQ_RTC = 0, /* Internal 150 kHz RC oscillator */
- RTC_SLOW_FREQ_32K_XTAL = 1, /* External 32 kHz XTAL */
- RTC_SLOW_FREQ_8MD256 = 2, /* Internal 8 MHz RC oscillator, divided by 256 */
-};
-
-/* RTC FAST_CLK frequency values */
-
-enum esp32_rtc_fast_freq_e
-{
- RTC_FAST_FREQ_XTALD4 = 0, /* Main XTAL, divided by 4 */
- RTC_FAST_FREQ_8M = 1, /* Internal 8 MHz RC oscillator */
-};
-
-/* This is almost the same as esp32_rtc_slow_freq_e, except that we define
- * an extra enum member for the external 32k oscillator.For convenience,
- * lower 2 bits should correspond to esp32_rtc_slow_freq_e values.
- */
-
-enum esp32_slow_clk_sel_e
-{
- /* Internal 150 kHz RC oscillator */
-
- SLOW_CLK_150K = RTC_SLOW_FREQ_RTC,
-
- /* External 32 kHz XTAL */
-
- SLOW_CLK_32K_XTAL = RTC_SLOW_FREQ_32K_XTAL,
-
- /* Internal 8 MHz RC oscillator, divided by 256 */
-
- SLOW_CLK_8MD256 = RTC_SLOW_FREQ_8MD256,
-
- /* External 32k oscillator connected to 32K_XP pin */
-
- SLOW_CLK_32K_EXT_OSC = RTC_SLOW_FREQ_32K_XTAL | EXT_OSC_FLAG
-};
-
-/* Clock source to be calibrated using rtc_clk_cal function */
-
-enum esp32_rtc_cal_sel_e
-{
- RTC_CAL_RTC_MUX = 0, /* Currently selected RTC SLOW_CLK */
- RTC_CAL_8MD256 = 1, /* Internal 8 MHz RC oscillator, divided by 256 */
- RTC_CAL_32K_XTAL = 2 /* External 32 kHz XTAL */
-};
-
/* RTC power and clock control initialization settings */
struct esp32_rtc_priv_s
@@ -245,8 +187,6 @@ static void IRAM_ATTR esp32_rtc_clk_fast_freq_set(
enum esp32_rtc_fast_freq_e fast_freq);
static uint32_t IRAM_ATTR esp32_rtc_clk_cal_internal(
enum esp32_rtc_cal_sel_e cal_clk, uint32_t slowclk_cycles);
-static uint32_t IRAM_ATTR esp32_rtc_clk_cal(enum esp32_rtc_cal_sel_e cal_clk,
- uint32_t slowclk_cycles);
static void IRAM_ATTR esp32_rtc_clk_slow_freq_set(
enum esp32_rtc_slow_freq_e slow_freq);
static void esp32_select_rtc_slow_clk(enum esp32_slow_clk_sel_e slow_clk);
@@ -476,41 +416,6 @@ static uint32_t IRAM_ATTR esp32_rtc_clk_cal_internal(
}
/****************************************************************************
- * Name: esp32_rtc_clk_cal
- *
- * Description:
- * Measure RTC slow clock's period, based on main XTAL frequency
- *
- * Input Parameters:
- * cal_clk - clock to be measured
- * slowclk_cycles - number of slow clock cycles to average
- *
- * Returned Value:
- * Average slow clock period in microseconds, Q13.19 fixed point format
- * or 0 if calibration has timed out
- *
- ****************************************************************************/
-
-static uint32_t IRAM_ATTR esp32_rtc_clk_cal(enum esp32_rtc_cal_sel_e cal_clk,
- uint32_t slowclk_cycles)
-{
- enum esp32_rtc_xtal_freq_e xtal_freq;
- uint64_t xtal_cycles;
- uint64_t divider;
- uint64_t period_64;
- uint32_t period;
-
- xtal_freq = esp32_rtc_clk_xtal_freq_get();
- xtal_cycles = esp32_rtc_clk_cal_internal(cal_clk, slowclk_cycles);
- divider = ((uint64_t)xtal_freq) * slowclk_cycles;
- period_64 = ((xtal_cycles << RTC_CLK_CAL_FRACT) + divider / 2 - 1)
- / divider;
- period = (uint32_t)(period_64 & UINT32_MAX);
-
- return period;
-}
-
-/****************************************************************************
* Name: esp32_rtc_clk_slow_freq_set
*
* Description:
@@ -574,6 +479,69 @@ static void esp32_select_rtc_slow_clk(enum esp32_slow_clk_sel_e slow_clk)
* Public Functions
****************************************************************************/
+/****************************************************************************
+ * Name: esp32_rtc_get_slow_clk_rtc
+ *
+ * Description:
+ * Get slow_clk_rtc source.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * The clock source:
+ * - SLOW_CK
+ * - CK_XTAL_32K
+ * - CK8M_D256_OUT
+ *
+ ****************************************************************************/
+
+enum esp32_rtc_slow_freq_e IRAM_ATTR esp32_rtc_get_slow_clk(void)
+{
+ enum esp32_rtc_slow_freq_e slow_freq;
+
+ /* Get the clock source for slow_clk_rtc */
+
+ slow_freq = REG_GET_FIELD(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_ANA_CLK_RTC_SEL);
+
+ return slow_freq;
+}
+
+/****************************************************************************
+ * Name: esp32_rtc_clk_cal
+ *
+ * Description:
+ * Measure RTC slow clock's period, based on main XTAL frequency
+ *
+ * Input Parameters:
+ * cal_clk - clock to be measured
+ * slowclk_cycles - number of slow clock cycles to average
+ *
+ * Returned Value:
+ * Average slow clock period in microseconds, Q13.19 fixed point format
+ * or 0 if calibration has timed out
+ *
+ ****************************************************************************/
+
+uint32_t IRAM_ATTR esp32_rtc_clk_cal(enum esp32_rtc_cal_sel_e cal_clk,
+ uint32_t slowclk_cycles)
+{
+ enum esp32_rtc_xtal_freq_e xtal_freq;
+ uint64_t xtal_cycles;
+ uint64_t divider;
+ uint64_t period_64;
+ uint32_t period;
+
+ xtal_freq = esp32_rtc_clk_xtal_freq_get();
+ xtal_cycles = esp32_rtc_clk_cal_internal(cal_clk, slowclk_cycles);
+ divider = ((uint64_t)xtal_freq) * slowclk_cycles;
+ period_64 = ((xtal_cycles << RTC_CLK_CAL_FRACT) + divider / 2 - 1)
+ / divider;
+ period = (uint32_t)(period_64 & UINT32_MAX);
+
+ return period;
+}
+
enum esp32_rtc_xtal_freq_e rtc_get_xtal(void)
__attribute__((alias("esp32_rtc_clk_xtal_freq_get")));
diff --git a/arch/xtensa/src/esp32/esp32_rtc.h b/arch/xtensa/src/esp32/esp32_rtc.h
index 9fa886b..2091515 100644
--- a/arch/xtensa/src/esp32/esp32_rtc.h
+++ b/arch/xtensa/src/esp32/esp32_rtc.h
@@ -30,6 +30,7 @@
****************************************************************************/
#include <nuttx/config.h>
+#include "hardware/esp32_soc.h"
#ifndef __ASSEMBLY__
@@ -43,6 +44,24 @@ extern "C"
#endif
/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Number of cycles to wait from the 32k XTAL oscillator to
+ * consider it running. Larger values increase startup delay.
+ * Smaller values may cause false positive detection
+ * (i.e. oscillator runs for a few cycles and then stops).
+ */
+
+#define SLOW_CLK_CAL_CYCLES 1024
+
+/* Indicates that 32k oscillator gets input from external oscillator
+ * instead of a crystal.
+ */
+
+#define EXT_OSC_FLAG BIT(3)
+
+/****************************************************************************
* Public Types
****************************************************************************/
@@ -58,11 +77,99 @@ enum esp32_rtc_xtal_freq_e
RTC_XTAL_FREQ_24M = 24, /* 24 MHz XTAL */
};
+/* RTC SLOW_CLK frequency values */
+
+enum esp32_rtc_slow_freq_e
+{
+ RTC_SLOW_FREQ_RTC = 0, /* Internal 150 kHz RC oscillator */
+ RTC_SLOW_FREQ_32K_XTAL = 1, /* External 32 kHz XTAL */
+ RTC_SLOW_FREQ_8MD256 = 2, /* Internal 8 MHz RC oscillator, divided by 256 */
+};
+
+/* RTC FAST_CLK frequency values */
+
+enum esp32_rtc_fast_freq_e
+{
+ RTC_FAST_FREQ_XTALD4 = 0, /* Main XTAL, divided by 4 */
+ RTC_FAST_FREQ_8M = 1, /* Internal 8 MHz RC oscillator */
+};
+
+/* This is almost the same as esp32_rtc_slow_freq_e, except that we define
+ * an extra enum member for the external 32k oscillator. For convenience,
+ * lower 2 bits should correspond to esp32_rtc_slow_freq_e values.
+ */
+
+enum esp32_slow_clk_sel_e
+{
+ /* Internal 150 kHz RC oscillator */
+
+ SLOW_CLK_150K = RTC_SLOW_FREQ_RTC,
+
+ /* External 32 kHz XTAL */
+
+ SLOW_CLK_32K_XTAL = RTC_SLOW_FREQ_32K_XTAL,
+
+ /* Internal 8 MHz RC oscillator, divided by 256 */
+
+ SLOW_CLK_8MD256 = RTC_SLOW_FREQ_8MD256,
+
+ /* External 32k oscillator connected to 32K_XP pin */
+
+ SLOW_CLK_32K_EXT_OSC = RTC_SLOW_FREQ_32K_XTAL | EXT_OSC_FLAG
+};
+
+/* Clock source to be calibrated using rtc_clk_cal function */
+
+enum esp32_rtc_cal_sel_e
+{
+ RTC_CAL_RTC_MUX = 0, /* Currently selected RTC SLOW_CLK */
+ RTC_CAL_8MD256 = 1, /* Internal 8 MHz RC oscillator, divided by 256 */
+ RTC_CAL_32K_XTAL = 2 /* External 32 kHz XTAL */
+};
+
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
+ * Name: esp32_rtc_get_slow_clk_rtc
+ *
+ * Description:
+ * Get slow_clk_rtc source.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * The clock source:
+ * - SLOW_CK
+ * - CK_XTAL_32K
+ * - CK8M_D256_OUT
+ *
+ ****************************************************************************/
+
+enum esp32_rtc_slow_freq_e esp32_rtc_get_slow_clk(void);
+
+/****************************************************************************
+ * Name: esp32_rtc_clk_cal
+ *
+ * Description:
+ * Measure RTC slow clock's period, based on main XTAL frequency
+ *
+ * Input Parameters:
+ * cal_clk - clock to be measured
+ * slowclk_cycles - number of slow clock cycles to average
+ *
+ * Returned Value:
+ * Average slow clock period in microseconds, Q13.19 fixed point format
+ * or 0 if calibration has timed out
+ *
+ ****************************************************************************/
+
+uint32_t esp32_rtc_clk_cal(enum esp32_rtc_cal_sel_e cal_clk,
+ uint32_t slowclk_cycles);
+
+/****************************************************************************
* Name: esp32_rtc_clk_xtal_freq_get
*
* Description: