platformio 开发esp32/esp8266问题诊断
主要是帮助以arduino 框架开发的同学。解决异常栈无法查看 。 日志不够详细,报错不明显。库的日志 没有打印,等问题。以及debug调试相关可能有帮助的事项。
新建一个用调试分析的env
platformio显著的特性 可以配置多个环境(env)配置。可以用于编译上传不同固件,监控不同的串口使用。我们可以在不动原有配置情况下新增一个用调试分析的env。
env的名字 但要符合文件夹命名规范和cmake或者其他编译器能识别的规范上一次加了空格出问题了
这个时候需要调用这个配置烧录 监控 等 使用 pio 命令时 使用 `-e for_debug` 指定env
例如: `pio run -t upload -e for_debug`。
当然使用 vscode和clion可以z直接在树上选择对应节点。clion如果没有树形gui,请在插件市场搜索 Platformio Plus。推荐2022以后的版本 ,精力有限 最新的功能只兼容了2022.*的版本。
该插件可以通过以下方式选中需要的环境配置项。

设置esp32-hal-log的日志工具的日志级别
比如使用其提供的宏记录info级别日志
日志会把 时间:[ 198] , 级别:[I] info级别 ,文件名和行号: [CamQuadruped.cpp:77] 函数名: setup() : 日志内容打出来

而arduino -esp32 内置库的日志打印均是这一套宏 ,只是日志级别不同。同时我们也能使用该日志工具记录日志。如果日志级别设对了,常规esp32使用串口都能看到这个日志打印。
也可以使用USB来查看这种日志打印。这样的话合宙esp32C3简约版则可以直接使用这种log_i log_d log_e log_v log_w log_e 等宏来打印日志。
设置日志级别,则需要设置这个宏 CORE_DEBUG_LEVEL
在build_flags中添加以下设置
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
注意单个 -Dkey=value 不要有空格。个人习惯每个宏独占一行。
可选值有 6 种 默认不设置则是关闭。
这里我们将其设置为详细:ARDUHAL_LOG_LEVEL_VERBOSE
看看arduino 下esp32 wifi连接日志。

如果连接失败会有原因打印,也借助日志打印进入对应的代码看看其中的内容。clion复制文件名和行号。按ctrl shift n 搜索跳转到对应文件具体行。
这个日志级别建议真正使用的时候设置为info级别,不然verbose打印的日志太多了。
esp32/esp8266异常栈的十六进制 转为对应的代码调用栈
对于这种例如问题 abort() was called at PC 0xxxx on core x。
Backtrace: 0x40377c6e:0x3fc96680 0x4037d519:0x3fc966a0 0x403838c9:0x3fc966c0 0x42015f98:0x3fc96740 0x40379065:0x3fc96760 0x400559dd:0x3fcabb20 |<-CORRUPTED
在arduino开发esp32 esp8266遇到之后很难去定位,很多时候可以需要串口打印二分法,逼近出错的行。或者esp32采用debug调试找到出错的行。
platformio集成该栈的十六进制解码功能。在串口监控时。添加过滤器,调用编译信息中生成的表,对应相关调用信息。所以有时候会提示需要重新编译。
设置 monitor_filters
monitor_filters = esp32_exception_decoder, time
这里我加了 一个time, 会在行首把任何打印加上时间戳。
如果是解析esp8266的异常栈,则需要esp8266_exception_decoder 。
修改之后会提示需要重新编译,因为需要一份详细的映射表,映射栈与代码。

有时候栈出现不全的情况,可能是需要重新编译一下。
所有的platformio已内置的过滤器:
default :
Remove typical terminal control codes from inputcolorize
: Apply different colors for received and echodebug :
Print what is sent and receiveddirect :
Do-nothing: forward all data unchangedhexlify :
Show a hexadecimal representation of the data (code point of each character)log2file :
Log data to a file “platformio-device-monitor-%date%.log” located in the current working directorynocontrol :
Remove all control codes, incl. CR+LFprintable :
Show decimal code for all non-ASCII characters and replace most control codestime :
Add timestamp with milliseconds for each new linesend_on_enter :
Send a text to device on ENTEResp32_exception_decoder
:Custom filter for Espressif 32 which decodes crash exceptionesp8266_exception_decoder
:Custom filter for Espressif 8266 which decodes crash exception
当然也有第三方过滤器,你也可以自己指定。可以参考官方文档。
设置build_type为debug
buid_type 支持 release test debug 将 buid_type 设置为debug 可以打印详细信息。
再搭配上一段的过滤器使用。
效果如下

红色的部分则是 RX的内容,当然其实在串口打印上还是一样存在,但它时间细分更加精细。
有时候一行串口打印的一个字符也带上了时间。
build_type可选项如下:
release
:Default configuration. A “release” configuration of your firmware/program does not contain symbolic debug information and is optimized for the firmware size or speed (depending on Development Platforms)
test
:A “test” configuration extends a build environment with macro and with extra flags provided by the Unit Testing framework
PIO_UNIT_TESTING
debug
:A “debug” configuration of your firmware/program is compiled with full symbolic debug information and no optimization. Optimization complicates debugging because the relationship between source code and generated instructions is more complex.
debug断点调试
这里以esp32s3 usb接口 + clion为例。
只需要点击一个debug按钮,则可以debug。但有些需要注意跳过对debug适配不好的代码,可以正常查看 单步执行 , 查看变量。 简单查看线程。
和GDB控制台等。

