You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2024/06/04 20:42:13 UTC

(nuttx) branch master updated: esp32[c3]: Add XTWDT support

This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 24af23e49c esp32[c3]: Add XTWDT support
24af23e49c is described below

commit 24af23e49cf0daa8b3514206cf851d1aec420006
Author: Eren Terzioglu <er...@espressif.com>
AuthorDate: Thu May 23 11:12:15 2024 +0300

    esp32[c3]: Add XTWDT support
---
 .../esp32c3/boards/esp32c3-generic/index.rst       |   5 +
 Documentation/platforms/risc-v/esp32c3/index.rst   |   2 +-
 arch/risc-v/src/common/espressif/Kconfig           |  78 ++++
 arch/risc-v/src/common/espressif/Make.defs         |   2 +-
 arch/risc-v/src/common/espressif/esp_wdt.c         | 418 ++++++++++++++++-----
 arch/risc-v/src/common/espressif/esp_wdt.h         |  24 +-
 arch/risc-v/src/esp32c3/hal_esp32c3.mk             |   1 +
 .../esp32c3/esp32c3-generic/src/esp32c3_bringup.c  |   8 +
 8 files changed, 440 insertions(+), 98 deletions(-)

diff --git a/Documentation/platforms/risc-v/esp32c3/boards/esp32c3-generic/index.rst b/Documentation/platforms/risc-v/esp32c3/boards/esp32c3-generic/index.rst
index eaf89cac04..95826e181b 100644
--- a/Documentation/platforms/risc-v/esp32c3/boards/esp32c3-generic/index.rst
+++ b/Documentation/platforms/risc-v/esp32c3/boards/esp32c3-generic/index.rst
@@ -241,6 +241,11 @@ To test it, just run the following command::
 
 Where X is the watchdog instance.
 
+To test the XTWDT(/dev/watchdog3) an interrupt handler needs to be
+implemented because XTWDT does not have system reset feature. To implement
+an interrupt handler `WDIOC_CAPTURE` command can be used. When interrupt
+rises, XTAL32K clock can be restored with `WDIOC_RSTCLK` command.
+
 wifi
 ----
 
diff --git a/Documentation/platforms/risc-v/esp32c3/index.rst b/Documentation/platforms/risc-v/esp32c3/index.rst
index c6c870d12d..d1524b86f9 100644
--- a/Documentation/platforms/risc-v/esp32c3/index.rst
+++ b/Documentation/platforms/risc-v/esp32c3/index.rst
@@ -164,7 +164,7 @@ SPIFLASH     Yes
 Timers       Yes
 Touch        No
 UART         Yes
-Watchdog     Yes
+Watchdog     Yes     XTWDT supported
 Wifi         Yes     WPA3-SAE supported
 =========== ======= ====================
 
diff --git a/arch/risc-v/src/common/espressif/Kconfig b/arch/risc-v/src/common/espressif/Kconfig
index 4db4627d62..79c9467dfb 100644
--- a/arch/risc-v/src/common/espressif/Kconfig
+++ b/arch/risc-v/src/common/espressif/Kconfig
@@ -251,6 +251,10 @@ endmenu # Bootloader and Image Configuration
 
 menu "Peripheral Support"
 
+config ESPRESSIF_RTC
+	bool "Real Time Clock (RTC)"
+	default y
+
 config ESPRESSIF_UART
 	bool
 	default n
@@ -360,6 +364,37 @@ config ESPRESSIF_RWDT
 		to have the RTC module reset, please, use the Timers' Module WDTs.
 		They will only reset Main System.
 
+config ESPRESSIF_XTWDT
+	bool "XTAL32K Watchdog Timer"
+	depends on ARCH_CHIP_ESP32C3_GENERIC
+	depends on ESPRESSIF_RTCIO_IRQ
+	depends on ESPRESSIF_RTC_CLK_EXT_OSC || ESPRESSIF_RTC_CLK_EXT_XTAL
+	default n
+	select ESPRESSIF_WDT
+	---help---
+		Includes XTWDT. This watchdog timer monitors the status of the
+		external 32 kHz crystal oscillator (XTAL32K). When XTAL32K_CLK works
+		as the clock source of RTC_SLOW_CLK and it stops oscillating, the
+		XTAL32K watchdog timer first switches to BACKUP32K_CLK derived from
+		RC_SLOW_CLK (if ESP32S2_XTWDT_BACKUP_CLK_ENABLE) and, then, generates
+		an interrupt that could be captured by WDIOC_CAPTURE.
+
+config ESPRESSIF_XTWDT_TIMEOUT
+	int "XTAL32K watchdog timeout period"
+	depends on ESPRESSIF_XTWDT
+	range 1 255
+	default 200
+	help
+		Timeout period configuration for the XTAL32K watchdog timer based on RTC_CLK.
+
+config ESPRESSIF_XTWDT_BACKUP_CLK_ENABLE
+	bool "Automatically switch to BACKUP32K_CLK when timer expires"
+	depends on ESPRESSIF_XTWDT
+	default y
+	---help---
+		Enable this to automatically switch to BACKUP32K_CLK as the source of
+		RTC_SLOW_CLK when the watchdog timer expires.
+
 config ESPRESSIF_BROWNOUT_DET
 	bool "Brownout Detector"
 	default y
@@ -925,6 +960,49 @@ config ESPRESSIF_STORAGE_MTD_SIZE
 
 endmenu # SPI Flash Configuration
 
+menu "RTC Configuration"
+	depends on ESPRESSIF_RTC
+
+choice ESPRESSIF_RTC_CLK_SRC
+	prompt "RTC clock source"
+	default ESPRESSIF_RTC_CLK_INT_RC
+	---help---
+		Choose which clock is used as RTC clock source.
+
+		- "Internal 90KHz oscillator" option provides lowest deep sleep current
+			consumption, and does not require extra external components. However
+			frequency stability with respect to temperature is poor, so time may
+			drift in deep/light sleep modes.
+		- "External 32KHz crystal" provides better frequency stability, at the
+			expense of slightly higher (1uA) deep sleep current consumption.
+		- "External 32KHz oscillator" allows using 32KHz clock generated by an
+			external circuit. In this case, external clock signal must be connected
+			to 32K_XN pin. Amplitude should be <1.2V in case of sine wave signal,
+			and <1V in case of square wave signal. Common mode voltage should be
+			0.1 < Vcm < 0.5Vamp, where Vamp is the signal amplitude.
+			Additionally, 1nF capacitor must be connected between 32K_XP pin and
+			ground. 32K_XP pin can not be used as a GPIO in this case.
+		- "Internal 8.5MHz oscillator divided by 256" option results in higher
+			deep sleep current (by 5uA) but has better frequency stability than
+			the internal 90KHz oscillator. It does not require external components.
+
+config ESPRESSIF_RTC_CLK_INT_RC
+	bool "Internal 90KHz RC oscillator"
+
+config ESPRESSIF_RTC_CLK_EXT_XTAL
+	bool "External 32KHz crystal"
+	select ESP_SYSTEM_RTC_EXT_XTAL
+
+config ESPRESSIF_RTC_CLK_EXT_OSC
+	bool "External 32KHz oscillator at 32K_XN pin"
+	select ESP_SYSTEM_RTC_EXT_XTAL
+
+config ESPRESSIF_RTC_CLK_INT_8MD256
+	bool "Internal 8.5MHz oscillator, divided by 256 (~33kHz)"
+
+endchoice
+endmenu # "RTC Configuration"
+
 menu "LEDC configuration"
 	depends on ESPRESSIF_LEDC
 
diff --git a/arch/risc-v/src/common/espressif/Make.defs b/arch/risc-v/src/common/espressif/Make.defs
index a0ca100eb5..ccb05252c9 100644
--- a/arch/risc-v/src/common/espressif/Make.defs
+++ b/arch/risc-v/src/common/espressif/Make.defs
@@ -107,7 +107,7 @@ endif
 
 ESP_HAL_3RDPARTY_REPO   = esp-hal-3rdparty
 ifndef ESP_HAL_3RDPARTY_VERSION
-	ESP_HAL_3RDPARTY_VERSION = ee8c7b0ad6a117465312d4c9a6e654cb7b2b9469
+	ESP_HAL_3RDPARTY_VERSION = 6c58b9c8d29fc2faa8cb6273a169340b80229df5
 endif
 
 ifndef ESP_HAL_3RDPARTY_URL
diff --git a/arch/risc-v/src/common/espressif/esp_wdt.c b/arch/risc-v/src/common/espressif/esp_wdt.c
index f059fcb370..b7e1bbeba3 100644
--- a/arch/risc-v/src/common/espressif/esp_wdt.c
+++ b/arch/risc-v/src/common/espressif/esp_wdt.c
@@ -29,6 +29,7 @@
 #include <stdbool.h>
 
 #include <nuttx/timers/watchdog.h>
+#include "riscv_internal.h"
 
 #include "esp_irq.h"
 #include "esp_wdt.h"
@@ -37,6 +38,12 @@
 
 #include "hal/mwdt_ll.h"
 #include "hal/rwdt_ll.h"
+#ifdef CONFIG_ESPRESSIF_XTWDT
+#include "hal/xt_wdt_ll.h"
+#include "hal/xt_wdt_hal.h"
+#include "esp_xt_wdt.h"
+#include "esp_rom_sys.h"
+#endif
 #include "hal/wdt_hal.h"
 #include "soc/rtc.h"
 #include "periph_ctrl.h"
@@ -73,6 +80,30 @@
                                           rwdt_ll_set_intr_enable(ctx->rwdt_dev, en) : \
                                           mwdt_ll_set_intr_enable(ctx->mwdt_dev, en))
 
+#define WDT_WP_DISABLE(dev)   do{ if (IS_XTWDT(dev) != true)                 \
+                                    wdt_hal_write_protect_disable(dev->ctx); \
+                                } while(0);
+
+#define WDT_WP_ENABLE(dev)    do{ if (IS_XTWDT(dev) != true)                \
+                                    wdt_hal_write_protect_enable(dev->ctx); \
+                                } while(0);
+
+/* Check whether the provided device is a XTAL32K Watchdog Timer */
+
+#define IS_XTWDT(dev)    (((struct esp_wdt_lowerhalf_s *)dev)->peripheral == XTAL32K)
+
+/* XTWDT clock period in nanoseconds */
+
+#define XTWDT_CLK_PERIOD_NS        (30)
+
+/* Maximum number of cycles supported for a XTWDT stage timeout */
+
+#define XTWDT_FULL_STAGE          (UINT8_MAX)
+
+/* Number of cycles for RTC_SLOW_CLK calibration */
+
+#define XT_WDT_CLK_CAL_CYCLES     (500)
+
 /* Helpers for converting from Q13.19 fixed-point format to float */
 
 #define N             19
@@ -91,6 +122,7 @@ typedef enum
 {
     RTC,
     TIMER,
+    XTAL32K,
 } wdt_peripherals_t;
 
 /* This structure provides the private representation of the "lower-half"
@@ -132,7 +164,8 @@ static int    wdt_settimeout(struct watchdog_lowerhalf_s *lower,
                              uint32_t timeout);
 static xcpt_t wdt_capture(struct watchdog_lowerhalf_s *lower,
                           xcpt_t handler);
-
+static int wdt_ioctl(struct watchdog_lowerhalf_s *lower, int cmd,
+                     unsigned long arg);
 /****************************************************************************
  * Private Data
  ****************************************************************************/
@@ -147,7 +180,7 @@ static const struct watchdog_ops_s g_esp_wdg_ops =
   .getstatus  = wdt_getstatus,
   .settimeout = wdt_settimeout,
   .capture    = wdt_capture,
-  .ioctl      = NULL
+  .ioctl      = wdt_ioctl
 };
 
 #ifdef CONFIG_ESPRESSIF_MWDT0
@@ -204,6 +237,29 @@ static struct esp_wdt_lowerhalf_s g_esp_rwdt_lowerhalf =
 };
 #endif
 
+#ifdef CONFIG_ESPRESSIF_XTWDT
+static xt_wdt_hal_context_t xtwdt_hal_ctx;
+static esp_xt_wdt_config_t cfg =
+  {
+    .timeout = CONFIG_ESPRESSIF_XTWDT_TIMEOUT,
+#ifdef CONFIG_ESPRESSIF_XTWDT_BACKUP_CLK_ENABLE
+    .auto_backup_clk_enable = true
+#else
+    .auto_backup_clk_enable = false
+#endif
+  };
+
+struct esp_wdt_lowerhalf_s g_esp_xtwdt_lowerhalf =
+{
+  .ops    = &g_esp_wdg_ops,
+  .periph = RTC_CORE_INTR_SOURCE,
+  .peripheral = XTAL32K,
+  .irq    = ESP_IRQ_RTC_XTAL32K_DEAD,
+  .started = false,
+  .ctx = (wdt_hal_context_t *)&xtwdt_hal_ctx
+};
+#endif
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -245,58 +301,80 @@ static int wdt_start(struct watchdog_lowerhalf_s *lower)
 
   priv->started = true;
 
-  wdt_hal_write_protect_disable(priv->ctx);
+  WDT_WP_DISABLE(priv);
 
   if (priv->handler == NULL)
     {
       /* No user handler, so configure WDT to reset on timeout */
 
-      if (priv->peripheral == TIMER)
+      if (IS_XTWDT(priv) != true)
         {
-          priv->action = WDT_STAGE_ACTION_RESET_SYSTEM;
-          timeout = MWDT_TIMEOUT_MS(priv->timeout);
-        }
-      else
-        {
-          priv->action = WDT_STAGE_ACTION_RESET_RTC;
-          timeout = RWDT_TIMEOUT_MS(priv->timeout);
-        }
+          if (priv->peripheral == TIMER)
+            {
+              priv->action = WDT_STAGE_ACTION_RESET_SYSTEM;
+              timeout = MWDT_TIMEOUT_MS(priv->timeout);
+            }
+          else
+            {
+              priv->action = WDT_STAGE_ACTION_RESET_RTC;
+              timeout = RWDT_TIMEOUT_MS(priv->timeout);
+            }
 
-      wdt_hal_config_stage(priv->ctx, WDT_STAGE0,
-                           timeout,
-                           priv->action);
+          wdt_hal_config_stage(priv->ctx, WDT_STAGE0,
+                              timeout,
+                              priv->action);
+        }
     }
   else
     {
       /* Configure WDT to call the user handler on timeout */
 
-      if (priv->peripheral == TIMER)
+      if (IS_XTWDT(priv) != true)
         {
-          priv->action = WDT_STAGE_ACTION_INT;
-          timeout = MWDT_TIMEOUT_MS(priv->timeout);
+          if (priv->peripheral == TIMER)
+            {
+              priv->action = WDT_STAGE_ACTION_INT;
+              timeout = MWDT_TIMEOUT_MS(priv->timeout);
+            }
+          else
+            {
+              priv->action = WDT_STAGE_ACTION_INT;
+              timeout = RWDT_TIMEOUT_MS(priv->timeout);
+            }
+
+          wdt_hal_config_stage(priv->ctx, WDT_STAGE0,
+                              timeout,
+                              priv->action);
+
+          /* Enable interrupt */
+
+          WDT_INTR_ENABLE(priv->peripheral, priv->ctx, true);
         }
+#ifdef CONFIG_ESPRESSIF_XTWDT
       else
         {
-          priv->action = WDT_STAGE_ACTION_INT;
-          timeout = RWDT_TIMEOUT_MS(priv->timeout);
+          xt_wdt_ll_intr_enable(((xt_wdt_hal_context_t *)priv->ctx)->dev,
+                                true);
         }
-
-      wdt_hal_config_stage(priv->ctx, WDT_STAGE0,
-                           timeout,
-                           priv->action);
-
-      /* Enable interrupt */
-
-      WDT_INTR_ENABLE(priv->peripheral, priv->ctx, true);
+#endif
     }
 
   flags = enter_critical_section();
   priv->lastreset = clock_systime_ticks();
-  wdt_hal_enable(priv->ctx);
-  leave_critical_section(flags);
 
-  wdt_hal_write_protect_enable(priv->ctx);
+  if (IS_XTWDT(priv) != true)
+    {
+      wdt_hal_enable(priv->ctx);
+    }
+#ifdef CONFIG_ESPRESSIF_XTWDT
+  else
+    {
+      xt_wdt_hal_enable((xt_wdt_hal_context_t *)priv->ctx, true);
+    }
+#endif
 
+  leave_critical_section(flags);
+  WDT_WP_ENABLE(priv);
   return ret;
 }
 
@@ -320,23 +398,31 @@ static int wdt_stop(struct watchdog_lowerhalf_s *lower)
 {
   struct esp_wdt_lowerhalf_s *priv = (struct esp_wdt_lowerhalf_s *)lower;
 
-  wdt_hal_write_protect_disable(priv->ctx);
+  WDT_WP_DISABLE(priv);
 
-  /* Disable the WDT */
+  if (IS_XTWDT(priv) != true)
+    {
+      /* Disable the WDT */
 
-  wdt_hal_disable(priv->ctx);
+      wdt_hal_disable(priv->ctx);
 
-  /* In case there is a callback registered, ensure WDT interrupts are
-   * disabled.
-   */
+      /* In case there is a callback registered, ensure WDT interrupts are
+       * disabled.
+       */
 
-  if (priv->handler != NULL)
+      if (priv->handler != NULL)
+        {
+          WDT_INTR_ENABLE(priv->peripheral, priv->ctx, false);
+        }
+    }
+#ifdef CONFIG_ESPRESSIF_XTWDT
+  else
     {
-      WDT_INTR_ENABLE(priv->peripheral, priv->ctx, false);
+      xt_wdt_hal_enable((xt_wdt_hal_context_t *)priv->ctx, false);
     }
+#endif
 
-  wdt_hal_write_protect_enable(priv->ctx);
-
+  WDT_WP_ENABLE(priv);
   priv->started = false;
 
   return OK;
@@ -364,6 +450,11 @@ static int wdt_keepalive(struct watchdog_lowerhalf_s *lower)
   struct esp_wdt_lowerhalf_s *priv = (struct esp_wdt_lowerhalf_s *)lower;
   irqstate_t flags;
 
+  if (IS_XTWDT(priv) == true)
+    {
+      return OK;
+    }
+
   wdt_hal_write_protect_disable(priv->ctx);
 
   /* Feed the dog and update the time of last reset */
@@ -468,52 +559,78 @@ static int wdt_settimeout(struct watchdog_lowerhalf_s *lower,
   struct esp_wdt_lowerhalf_s *priv = (struct esp_wdt_lowerhalf_s *)lower;
   uint16_t rtc_cycles = 0;
   uint32_t rtc_ms_max = 0;
+  uint32_t xtal32k_cycles = 0;
+  uint32_t xtal32k_ms_max = 0;
 
   wdinfo("Entry: timeout=%" PRIu32 "\n", timeout);
 
   DEBUGASSERT(priv != NULL);
 
-  wdt_hal_write_protect_disable(priv->ctx);
-
   priv->timeout = timeout;
 
-  if (priv->peripheral == TIMER)
+  if (IS_XTWDT(priv) != true)
     {
-      if (timeout == 0 || timeout > MWDT_MAX_TIMEOUT_MS)
+      wdt_hal_write_protect_disable(priv->ctx);
+      if (priv->peripheral == TIMER)
         {
-          wderr("ERROR: Cannot represent timeout=%"PRIu32" > %"PRIu32"\n",
-                timeout, MWDT_MAX_TIMEOUT_MS);
-          return -ERANGE;
+          if (timeout == 0 || timeout > MWDT_MAX_TIMEOUT_MS)
+            {
+              wderr("ERROR: Cannot represent timeout=%"PRIu32"> %"PRIu32"\n",
+                    timeout, MWDT_MAX_TIMEOUT_MS);
+              return -ERANGE;
+            }
+
+          timeout = MWDT_TIMEOUT_MS(priv->timeout);
         }
+      else if (priv->peripheral == RTC)
+        {
+          uint32_t period_13q19 = esp_clk_slowclk_cal_get();
+          float period = Q_TO_FLOAT(period_13q19);
+          rtc_cycles = 1000.0f / period;
+          rtc_ms_max = (RWDT_FULL_STAGE / (uint32_t)rtc_cycles);
 
-      timeout = MWDT_TIMEOUT_MS(priv->timeout);
+          /* Is this timeout a valid value for RTC WDT? */
+
+          if (timeout == 0 || timeout > rtc_ms_max)
+            {
+              wderr("ERROR: Cannot represent timeout=%"PRIu32"> %"PRIu32"\n",
+                    timeout, rtc_ms_max);
+              return -ERANGE;
+            }
+
+          timeout = timeout * rtc_cycles;
+        }
+
+      wdt_hal_config_stage(priv->ctx, WDT_STAGE0,
+                          timeout,
+                          priv->action);
+
+      wdt_hal_feed(priv->ctx);
+
+      wdt_hal_write_protect_enable(priv->ctx);
     }
+#ifdef CONFIG_ESPRESSIF_XTWDT
   else
     {
-      uint32_t period_13q19 = esp_clk_slowclk_cal_get();
-      float period = Q_TO_FLOAT(period_13q19);
-      rtc_cycles = 1000.0f / period;
-      rtc_ms_max = (RWDT_FULL_STAGE / (uint32_t)rtc_cycles);
+      xtal32k_cycles =  XTWDT_CLK_PERIOD_NS;
+      xtal32k_ms_max = (XTWDT_FULL_STAGE * NSEC_PER_USEC
+                        / (uint32_t)xtal32k_cycles);
 
       /* Is this timeout a valid value for RTC WDT? */
 
-      if (timeout == 0 || timeout > rtc_ms_max)
+      if (timeout == 0 || timeout > xtal32k_ms_max)
         {
-          wderr("ERROR: Cannot represent timeout=%"PRIu32" > %"PRIu32"\n",
+          wderr("Cannot represent timeout=%" PRIu32 " > %" PRIu32 "\n",
                 timeout, rtc_ms_max);
           return -ERANGE;
         }
 
-      timeout = timeout * rtc_cycles;
+      timeout = timeout * xtal32k_cycles;
+      xt_wdt_ll_set_timeout(((xt_wdt_hal_context_t *)priv->ctx)->dev,
+                             timeout);
+      return OK;
     }
-
-  wdt_hal_config_stage(priv->ctx, WDT_STAGE0,
-                       timeout,
-                       priv->action);
-
-  wdt_hal_feed(priv->ctx);
-
-  wdt_hal_write_protect_enable(priv->ctx);
+#endif
 
   return OK;
 }
@@ -553,7 +670,7 @@ static xcpt_t wdt_capture(struct watchdog_lowerhalf_s *lower, xcpt_t handler)
 
   oldhandler = priv->handler;
 
-  wdt_hal_write_protect_disable(priv->ctx);
+  WDT_WP_DISABLE(priv);
 
   flags = enter_critical_section();
 
@@ -574,54 +691,131 @@ static xcpt_t wdt_capture(struct watchdog_lowerhalf_s *lower, xcpt_t handler)
            * then change to interrupt.
            */
 
+          if (IS_XTWDT(priv) != true)
+            {
+              if (priv->peripheral == TIMER)
+                {
+                  timeout = MWDT_TIMEOUT_MS(priv->timeout);
+                }
+              else
+                {
+                  timeout = RWDT_TIMEOUT_MS(priv->timeout);
+                }
+
+              priv->action = WDT_STAGE_ACTION_INT;
+
+              wdt_hal_config_stage(priv->ctx, WDT_STAGE0,
+                                  timeout,
+                                  priv->action);
+            }
+        }
+
+      if (IS_XTWDT(priv) != true)
+        {
+          WDT_INTR_ENABLE(priv->peripheral, priv->ctx, true);
+        }
+#ifdef CONFIG_ESPRESSIF_XTWDT
+      else
+        {
+      xt_wdt_hal_enable((xt_wdt_hal_context_t *)priv->ctx, true);
+        }
+#endif
+    }
+
+  /* In case the user wants to disable the callback */
+
+  else
+    {
+      if (IS_XTWDT(priv) != true)
+        {
           if (priv->peripheral == TIMER)
             {
               timeout = MWDT_TIMEOUT_MS(priv->timeout);
+              priv->action = WDT_STAGE_ACTION_RESET_SYSTEM;
             }
           else
             {
               timeout = RWDT_TIMEOUT_MS(priv->timeout);
+              priv->action = WDT_STAGE_ACTION_RESET_RTC;
             }
 
-          priv->action = WDT_STAGE_ACTION_INT;
+          WDT_INTR_ENABLE(priv->peripheral, priv->ctx, false);
+
+          /* Then configure it to reset on WDT expiration */
 
           wdt_hal_config_stage(priv->ctx, WDT_STAGE0,
-                               timeout,
-                               priv->action);
+                              timeout,
+                              priv->action);
         }
-
-      WDT_INTR_ENABLE(priv->peripheral, priv->ctx, true);
+#ifdef CONFIG_ESPRESSIF_XTWDT
+      else
+        {
+          xt_wdt_hal_enable((xt_wdt_hal_context_t *)priv->ctx, false);
+        }
+#endif
     }
 
-  /* In case the user wants to disable the callback */
+  leave_critical_section(flags);
 
-  else
+  WDT_WP_ENABLE(priv);
+
+  return oldhandler;
+}
+
+/****************************************************************************
+ * Name: wdt_ioctl
+ *
+ * Description:
+ *   Any ioctl commands that are not recognized by the "upper-half" driver
+ *   are forwarded to the lower half driver through this method.
+ *
+ * Input Parameters:
+ *   lower - A pointer the publicly visible representation of the
+ *           "lower-half" driver state structure.
+ *   cmd   - Command number to process.
+ *   arg   - Argument that sent to the command.
+ *
+ * Returned Value:
+ *   OK if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int wdt_ioctl(struct watchdog_lowerhalf_s *lower, int cmd,
+                     unsigned long arg)
+{
+  struct esp_wdt_lowerhalf_s *priv = (struct esp_wdt_lowerhalf_s *)lower;
+
+  wdinfo("ioctl Call: cmd=0x%x arg=0x%lx", cmd, arg);
+
+  /* Process the IOCTL command */
+
+  switch (cmd)
     {
-      if (priv->peripheral == TIMER)
+    case WDIOC_RSTCLK:
+#ifdef CONFIG_ESPRESSIF_XTWDT
+      if (IS_XTWDT(priv) == true)
         {
-          timeout = MWDT_TIMEOUT_MS(priv->timeout);
-          priv->action = WDT_STAGE_ACTION_RESET_SYSTEM;
-        }
-      else
-        {
-          timeout = RWDT_TIMEOUT_MS(priv->timeout);
-          priv->action = WDT_STAGE_ACTION_RESET_RTC;
-        }
+          xt_wdt_hal_enable((xt_wdt_hal_context_t *)priv->ctx, false);
 
-      WDT_INTR_ENABLE(priv->peripheral, priv->ctx, false);
+          REG_CLR_BIT(RTC_CNTL_EXT_XTL_CONF_REG, RTC_CNTL_XPD_XTAL_32K);
+          REG_SET_BIT(RTC_CNTL_EXT_XTL_CONF_REG, RTC_CNTL_XPD_XTAL_32K);
 
-      /* Then configure it to reset on WDT expiration */
+          /* Needs some time after switching to 32khz XTAL
+          * before turning on WDT again
+          */
 
-      wdt_hal_config_stage(priv->ctx, WDT_STAGE0,
-                           timeout,
-                           priv->action);
-    }
+          esp_rom_delay_us(300);
 
-  leave_critical_section(flags);
+          xt_wdt_hal_enable((xt_wdt_hal_context_t *)priv->ctx, true);
+        }
+#endif
+      break;
 
-  wdt_hal_write_protect_enable(priv->ctx);
+    default:
+      return -ENOTTY;
+    }
 
-  return oldhandler;
+  return OK;
 }
 
 /****************************************************************************
@@ -650,12 +844,16 @@ static int wdt_handler(int irq, void *context, void *arg)
 
   priv->handler(irq, context, priv->upper);
 
+  WDT_WP_DISABLE(priv);
+
   /* Clear the Interrupt */
 
-  wdt_hal_write_protect_disable(priv->ctx);
-  wdt_hal_handle_intr(priv->ctx);
-  wdt_hal_write_protect_enable(priv->ctx);
+  if (IS_XTWDT(priv) != true)
+    {
+      wdt_hal_handle_intr(priv->ctx);
+    }
 
+  WDT_WP_ENABLE(priv);
   return OK;
 }
 
@@ -722,6 +920,25 @@ int esp_wdt_initialize(const char *devpath, enum esp_wdt_inst_e wdt_id)
         }
 #endif
 
+#ifdef CONFIG_ESPRESSIF_XTWDT
+      case ESP_WDT_XTAL32K:
+        {
+          lower = &g_esp_xtwdt_lowerhalf;
+          esp_rtcioirqenable(lower->irq);
+          xt_wdt_hal_init((xt_wdt_hal_context_t *)lower->ctx,
+                          (xt_wdt_hal_config_t *)&cfg);
+          if (cfg.auto_backup_clk_enable)
+            {
+              uint32_t rtc_clk_frequency_khz = rtc_clk_freq_cal(
+                          rtc_clk_cal(RTC_CAL_INTERNAL_OSC, 500)) / 1000;
+              xt_wdt_hal_enable_backup_clk((xt_wdt_hal_context_t *)
+                                            lower->ctx,
+                                            rtc_clk_frequency_khz);
+            }
+          break;
+        }
+#endif
+
       default:
         {
           wderr("ERROR: unsupported WDT %d\n", wdt_id);
@@ -733,7 +950,11 @@ int esp_wdt_initialize(const char *devpath, enum esp_wdt_inst_e wdt_id)
 
   lower->handler = NULL;
   lower->timeout = 0;
-  lower->started = wdt_hal_is_enabled(lower->ctx);
+
+  if (IS_XTWDT(lower) != true)
+    {
+      lower->started = wdt_hal_is_enabled(lower->ctx);
+    }
 
   /* Register the watchdog driver as /dev/watchdogX. If the registration goes
    * right the returned value from watchdog_register is a pointer to
@@ -764,7 +985,14 @@ int esp_wdt_initialize(const char *devpath, enum esp_wdt_inst_e wdt_id)
 
   /* Enable the allocated CPU interrupt */
 
-  up_enable_irq(lower->irq);
+  if (IS_XTWDT(lower) != true)
+    {
+      up_enable_irq(lower->irq);
+    }
+  else
+    {
+      up_enable_irq(ESP_IRQ_RTC_CORE);
+    }
 
   return OK;
 }
diff --git a/arch/risc-v/src/common/espressif/esp_wdt.h b/arch/risc-v/src/common/espressif/esp_wdt.h
index e3f904c202..ef1296a182 100644
--- a/arch/risc-v/src/common/espressif/esp_wdt.h
+++ b/arch/risc-v/src/common/espressif/esp_wdt.h
@@ -27,6 +27,27 @@
 
 #include <nuttx/config.h>
 
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* IOCTL Commands ***********************************************************/
+
+/* The watchdog driver uses a standard character driver framework.  However,
+ * since the watchdog driver is a device control interface and not a data
+ * transfer interface, the majority of the functionality is implemented in
+ * driver ioctl calls.
+ *
+ * See nuttx/timers/watchdog.h for the IOCTLs handled by the upper half.
+ *
+ * These are detected and handled by the "lower half" watchdog timer driver.
+ *
+ * WDIOC_RSTCLK     - Restores the xtal32k clock
+ *                    Argument: Ignored
+ */
+
+#define WDIOC_RSTCLK      _WDIOC(0x032)
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -37,7 +58,8 @@ enum esp_wdt_inst_e
 {
   ESP_WDT_MWDT0 = 0,  /* Main System Watchdog Timer (MWDT) of Timer Group 0 */
   ESP_WDT_MWDT1,      /* Main System Watchdog Timer (MWDT) of Timer Group 1 */
-  ESP_WDT_RWDT        /* RTC Watchdog Timer (RWDT) */
+  ESP_WDT_RWDT,       /* RTC Watchdog Timer (RWDT) */
+  ESP_WDT_XTAL32K     /* XTAL32K Watchdog Timer (XTWDT) */
 };
 
 /****************************************************************************
diff --git a/arch/risc-v/src/esp32c3/hal_esp32c3.mk b/arch/risc-v/src/esp32c3/hal_esp32c3.mk
index 188aaf1ab5..876e8bd6f5 100644
--- a/arch/risc-v/src/esp32c3/hal_esp32c3.mk
+++ b/arch/risc-v/src/esp32c3/hal_esp32c3.mk
@@ -129,6 +129,7 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$
 CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)uart_hal.c
 CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)uart_hal_iram.c
 CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)wdt_hal_iram.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)xt_wdt_hal.c
 CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)$(CHIP_SERIES)$(DELIM)clk_tree_hal.c
 CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)$(CHIP_SERIES)$(DELIM)efuse_hal.c
 CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)log$(DELIM)log.c
diff --git a/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c b/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c
index 1dd0f62e82..6d9d2d3c60 100644
--- a/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c
+++ b/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c
@@ -152,6 +152,14 @@ int esp_bringup(void)
     }
 #endif
 
+#ifdef CONFIG_ESPRESSIF_XTWDT
+  ret = esp_wdt_initialize("/dev/watchdog3", ESP_WDT_XTAL32K);
+  if (ret < 0)
+    {
+      _err("Failed to initialize WDT: %d\n", ret);
+    }
+#endif
+
 #ifdef CONFIG_TIMER
   ret = esp_timer_initialize(0);
   if (ret < 0)