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

ESP32-S3迁移后BTC_TASK栈金丝雀触发崩溃求助

ESP32-S3 BLE连接时BTC_TASK栈溢出崩溃排查求助

我们将定制PCB从ESP32-S1升级到ESP32-S3,同步升级固件后,在BLE连接过程中出现BTC_TASK崩溃。崩溃原因为栈金丝雀监视点触发,栈回溯显示问题涉及vfprintf.c及BLEService.cpp文件。已排查多日未解决,现附上崩溃日志、栈信息、platformio.ini配置及编译轨迹,恳请指点排查方向与解决方案,也欢迎指出环境配置或版本中的异常点。

崩溃信息

Guru Meditation Error: Core  0 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered (BTC_TASK) 
Core  0 register dump:
PC      : 0x4216e4f1  PS      : 0x00060236  A0      : 0x8216aa0c  A1      : 0x3fcb71c0  
A2      : 0x3fcb755c  A3      : 0x00000004  A4      : 0x00000001  A5      : 0x00000010  
A6      : 0x3fcb71d0  A7      : 0x0000000c  A8      : 0x00000001  A9      : 0x3fcb71d0  
A10     : 0x0000005a  A11     : 0x0000002a  A12     : 0x00000064  A13     : 0x3c1f4220  
A14     : 0x00000064  A15     : 0xff000000  SAR     : 0x0000000a  EXCCAUSE: 0x00000001  
EXCVADDR: 0x00000000  LBEG    : 0x400556d5  LEND    : 0x400556e5  LCOUNT  : 0xffffffff  


Backtrace: 0x4216e4ee:0x3fcb71c0 0x4216aa09:0x3fcb74d0 0x42045db1:0x3fcb7590 0x42045e2d:0x3fcb75c0 0x42046069:0x3fcb7620 0x420108a9:0x3fcb7690 0x421858a3:0x3fcb79c0 0x42051721:0x3fcb79e0 0x420571ed:0x3fcb7ca0 0x42054aa5:0x3fcb7cc0 0x42055215:0x3fcb7d10 0x420545d1:0x3fcb7d30 0x4205276d:0x3fcb7d90 0x4209d122:0x3fcb7db0 0x4209dca1:0x3fcb7dd0 0x420c20c1:0x3fcb7e20 0x420c6d33:0x3fcb7e40

栈信息

0x4216e4ee: _svfprintf_r at /Users/brnomac003/.gitlab-runner/builds/qR2TxTby/1/idf/crosstool-NG/.build/xtensa-esp32s3-elf/src/newlib/newlib/libc/stdio/vfprintf.c:1192
0x3fcb7620: ?? ??:0
0x42054aa5: BLEService::handleGATTServerEvent(esp_gatts_cb_event_t, unsigned char, esp_ble_gatts_cb_param_t*) at /Users/stephanedeluca/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src/BLEService.cpp:362
0x3fcb7db0: ?? ??:0

platformio.ini配置片段

[env]
platform = espressif32
;@ ~3.5.0
board = esp32-s3-devkitm-1  ; PCB_REV ≥ 10 ; 
; board = esp32-s3-devkitc-1
; board = esp-wrover-kit    ; PCB_REC < 10
framework = arduino
platform_packages =
    framework-arduinoespressif32@3.20017.0 ;@ 3.20003.220626
    ;@3.20005.220925 
    ;@3.10006.210326
    tool-esptoolpy@ 1.30300.0
    ;@1.40201.0 
    ;@1.30100.210531
    toolchain-xtensa-esp32 @ 12.2.0+20230208 ;@ 8.4.0+2021r2-patch3
    toolchain-xtensa32 ;@ 2.50200.97
    ;@2.50200.97
    ;@2.50200.97

编译轨迹

CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32-s3-devkitm-1.html
PLATFORM: Espressif 32 (6.9.0) > Espressif ESP32-S3-DevKitM-1
HARDWARE: ESP32S3 240MHz, 320KB RAM, 8MB Flash
DEBUG: Current (esp-builtin) On-board (esp-builtin) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: 
 - framework-arduinoespressif32 @ 3.20017.0 (2.0.17) 
 - tool-esptoolpy @ 1.30300.0 (3.3.0) 
 - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5 
 - toolchain-xtensa-esp32 @ 12.2.0+20230208 
 - toolchain-xtensa-esp32s3 @ 8.4.0+2021r2-patch5 
 - toolchain-xtensa32 @ 2.50200.97 (5.2.0)
LDF: Library Dependency Finder -> xxxxx
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 43 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Tone32 @ 1.0.0
|-- NTPClient @ 3.2.1
|-- base64 @ 1.2.1
|-- PubSubClient @ 2.8.0
|-- ArduinoJson @ 6.21.3
|-- EspSoftwareSerial @ 6.16.1
|-- Time32 @ 1.1.3
|-- idf-flash-vendor-patches @ 0.0.0+20231025182901.sha.182e042
|-- SSLClient @ 1.6.11+sha.712e593
|-- ESP32 BLE Arduino @ 2.0.0
|-- FFat @ 2.0.0
|-- SPIFFS @ 2.0.0
|-- Update @ 2.0.0
|-- WiFi @ 2.0.0
|-- Preferences @ 2.0.0
|-- HTTPClient @ 2.0.0
|-- SPI @ 2.0.0
|-- FS @ 2.0.0
|-- SD @ 2.0.0
|-- Wire @ 2.0.0
|-- WiFiClientSecure @ 2.0.0
Building in debug mode

排查方向与解决方案

  • 增大BTC_TASK栈空间:栈金丝雀触发核心原因是栈溢出,ESP32-S3的BTC_TASK默认栈空间可能不足以支撑当前BLE业务逻辑。可通过在platformio.ini中添加build_flags = -DCONFIG_BT_BTC_TASK_STACK_SIZE=8192(默认一般为4096)来调整,或者通过menuconfig进入蓝牙配置项修改栈大小。
  • 检查BLEService.cpp第362行代码:栈回溯明确指向该位置,重点排查是否存在以下情况:
    • 定义了大量局部变量(尤其是数组、结构体)占用栈空间;
    • 递归调用导致栈深度过大;
    • 调用了printf/Serial.print等格式化输出函数——vfprintf本身栈开销较高,若在BLE回调(属于高优先级任务)中频繁做字符串格式化,极易撑爆栈。
  • 优化日志输出逻辑:若在BLE事件回调中有日志输出,改为使用全局缓冲区预先格式化,或者减少格式化操作,避免在任务栈中执行高开销的字符串处理。
  • 清理环境配置冲突:当前platformio配置中同时指定了toolchain-xtensa-esp32、toolchain-xtensa32和toolchain-xtensa-esp32s3,ESP32-S3仅需toolchain-xtensa-esp32s3,多余的toolchain可能引发编译或运行时冲突,建议删除toolchain-xtensa-esp32和toolchain-xtensa32的配置项。
  • 升级BLE库版本:当前使用的ESP32 BLE Arduino库版本为2.0.0,可尝试升级到最新稳定版,排查是否存在已知的栈溢出bug。
  • 栈溢出定位:开启ESP-IDF的栈跟踪功能(在PlatformIO中添加build_flags = -DCONFIG_HEAP_TRACE_ENABLE=y),或者使用IDE的调试功能,查看BTC_TASK的栈使用情况,定位具体的栈占用大户。

内容的提问来源于stack exchange,提问作者Stéphane de Luca

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 22:47:04