You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32-S3-DevKitC-1看门狗使用报错及兼容问题求助

解决ESP32-S3-DevKitC-1的RTC看门狗兼容问题

问题背景

在ESP32 Dev开发板上,以下RTC看门狗代码与PlatformIO配置可正常运行,但移植到ESP32-S3-DevKitC-1时出现编译错误。

ESP32 Dev上的正常代码与配置

Arduino代码

#include <Arduino.h>
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_task_wdt.h>
#include <esp_log.h>
#include <soc/rtc_wdt.h>

// 初始化看门狗
watchdogRTC();

// 喂狗操作
rtc_wdt_feed(); //Alimenta o RTC WDT

// 测试代码:串口输入后进入死循环,触发看门狗复位
if (Serial.available())
{
  while(1){
    Serial.println("preso");
    delay(1000);
  }
}

// 看门狗初始化函数
void watchdogRTC()
{
   rtc_wdt_protect_off();      //Disable RTC WDT write protection
   //Set stage 0 to trigger a system reset after 1000ms
   rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_RTC);
   rtc_wdt_set_time(RTC_WDT_STAGE0, 9000);
   rtc_wdt_enable();           //Start the RTC WDT timer
   rtc_wdt_protect_on();       //Enable RTC WDT write protection
}

PlatformIO配置(platformio.ini)

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
board_build.f_flash = 80000000L

ESP32-S3移植后的编译错误

编译时出现以下宏未声明的错误:

  • ‘RTC_WDT_STG_SEL_OFF’ 未在此作用域中声明
  • ‘RTC_WDT_STG_SEL_INT’ 未在此作用域中声明
  • ‘RTC_WDT_STG_SEL_RESET_CPU’ 未在此作用域中声明
  • ‘RTC_WDT_STG_SEL_RESET_SYSTEM’ 未在此作用域中声明
  • ‘RTC_WDT_STG_SEL_RESET_RTC’ 未在此作用域中声明

ESP32-S3对应的PlatformIO配置:

[env:esp32-s3-devkitc-1]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
monitor_speed = 115200
board_build.f_flash = 80000000L

解决方案

核心原因

ESP32-S3采用的ESP-IDF v5.x及以上版本,对RTC看门狗的宏命名规则做了调整:初代ESP32使用RTC_WDT_STAGE_ACTION_*格式的宏,而ESP32-S3改用RTC_WDT_STG_SEL_*格式,直接复用旧代码会导致宏未定义错误。同时需要确保PlatformIO平台版本足够新,以支持ESP32-S3的SDK。

修改后的兼容代码

使用条件编译自动识别芯片类型,适配不同的宏定义:

#include <Arduino.h>
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_task_wdt.h>
#include <esp_log.h>
#include <soc/rtc_wdt.h>

// 条件编译适配不同芯片的看门狗动作宏
#if CONFIG_IDF_TARGET_ESP32S3
#define WDT_RESET_ACTION RTC_WDT_STG_SEL_RESET_RTC
#else
#define WDT_RESET_ACTION RTC_WDT_STAGE_ACTION_RESET_RTC
#endif

void watchdogRTC();

void setup() {
  Serial.begin(115200);
  watchdogRTC(); // 初始化看门狗
}

void loop() {
  rtc_wdt_feed(); // 定时喂狗,避免复位

  // 测试逻辑:串口收到输入后进入死循环,触发看门狗复位
  if (Serial.available()) {
    while(1){
      Serial.println("preso");
      delay(1000);
    }
  }
}

void watchdogRTC()
{
  rtc_wdt_protect_off();      // 关闭看门狗写保护,允许配置
  // 设置看门狗阶段0:9秒后触发RTC复位
  rtc_wdt_set_stage(RTC_WDT_STAGE0, WDT_RESET_ACTION);
  rtc_wdt_set_time(RTC_WDT_STAGE0, 9000); // 参数单位为毫秒
  rtc_wdt_enable();           // 启动看门狗定时器
  rtc_wdt_protect_on();       // 开启写保护,防止误修改配置
}

调整PlatformIO配置

指定支持ESP32-S3的最新平台版本(建议6.x及以上):

[env:esp32-s3-devkitc-1]
platform = espressif32 @ 6.6.0
board = esp32-s3-devkitc-1
framework = arduino
monitor_speed = 115200
board_build.f_flash = 80000000L

内容的提问来源于stack exchange,提问作者Carlos Costa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:16:10