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

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

commit 96d71ea565f24092f5e40551b84afe14093825dc
Author: Tiago Medicci Serrano <[email protected]>
AuthorDate: Tue May 26 13:25:27 2026 +0200

    xtensa/esp32s3: Re-apply WDT prescaler and timeout at start.
    
    With the BOARD_LATE_INITIALIZE flow, board_late_initialize() runs
    on a kernel thread and sets the MWDT0 prescaler. However, after it
    returns, the init task (NSH) is spawned and lib_cxx_initialize()
    invokes constructor functions, including
    enable_timer_group0_for_calibration() from esp-hal-3rdparty which
    calls timg_ll_reset_register(0), resetting all Timer Group 0
    registers (including the WDT prescaler) back to their defaults.
    
    Re-apply the prescaler, timeout, and feed the WDT counter in
    wdt_lh_start() just before enabling the timer. This ensures
    correct WDT configuration regardless of any intermediate register
    resets by external code.
    
    Signed-off-by: Tiago Medicci Serrano <[email protected]>
---
 arch/xtensa/src/esp32s3/esp32s3_wdt_lowerhalf.c | 29 +++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/arch/xtensa/src/esp32s3/esp32s3_wdt_lowerhalf.c 
b/arch/xtensa/src/esp32s3/esp32s3_wdt_lowerhalf.c
index 5e91e4a65b8..e60ca250d8c 100644
--- a/arch/xtensa/src/esp32s3/esp32s3_wdt_lowerhalf.c
+++ b/arch/xtensa/src/esp32s3/esp32s3_wdt_lowerhalf.c
@@ -237,6 +237,33 @@ static int wdt_lh_start(struct watchdog_lowerhalf_s *lower)
 
       ESP32S3_WDT_UNLOCK(priv->wdt);
 
+      /* Re-apply the prescaler for MWDT in case the Timer Group
+       * registers were reset after initialization (e.g. by a
+       * constructor function that resets the peripheral).
+       */
+
+      if (priv->peripheral == TIMER)
+        {
+          ESP32S3_MWDT_PRE(priv->wdt, MWDT_CLK_PRESCALER_VALUE);
+        }
+
+      /* Re-apply the timeout value */
+
+      if (priv->timeout > 0)
+        {
+          if (priv->peripheral == TIMER)
+            {
+              ESP32S3_WDT_STO(priv->wdt, MWDT_TIMEOUT_MS(priv->timeout),
+                              ESP32S3_WDT_STAGE0);
+            }
+          else if (priv->peripheral == RTC)
+            {
+              uint16_t rtc_cycles = ESP32S3_RWDT_CLK(priv->wdt);
+              ESP32S3_WDT_STO(priv->wdt, priv->timeout * rtc_cycles,
+                              ESP32S3_WDT_STAGE0);
+            }
+        }
+
       /* No User Handler */
 
       if (priv->handler == NULL)
@@ -272,6 +299,8 @@ static int wdt_lh_start(struct watchdog_lowerhalf_s *lower)
           ESP32S3_WDT_ENABLEINT(priv->wdt);
         }
 
+      ESP32S3_WDT_FEED(priv->wdt);
+
       flags = spin_lock_irqsave(&priv->lock);
       priv->lastreset = clock_systime_ticks();
       ESP32S3_WDT_START(priv->wdt);

Reply via email to