Community编程与开发github.com

ESP32 固件开发(PlatformIO: 优先 Arduino,按需 ESP-IDF)

ESP32 固件开发技能,基于 PlatformIO——主框架 Arduino(或作为 IDF 组件的 Arduino)和 ESP-IDF——支持 ESP32、S2、S3、C3、C6。涵盖 platformio.ini 配置、Arduino core 2.x 与 3.x API 差异、安全引脚选择、FreeRTOS 任务与 ISR、内存/PSRAM、Wi-Fi/BLE、NVS/Preferences、OTA、深度睡眠、分区表、USB/串口烧录、解码 Guru Meditation 崩溃与启动循环。当用户处理任何 ESP32 / ESP32-S3 / C3 / C6 开发板、platformio.ini 搭配 espressif32、ESP32 Arduino 草图、idf.py/sdkconfig,或粘贴 ESP32 串口输出(rst:, Guru Meditation, Brownout, watchdog)时,请使用此技能——即使他们只说“板子重启了”或“上传失败”。与嵌入式 C/C++ 技能配合使用,保证通用 C/C++ 质量、测试与架构。

ESP32 固件开发(PlatformIO: 优先 Arduino,按需 ESP-IDF) 是什么?

该技能是面向 ESP32 系列(ESP32/S2/S3/C3/C6)固件开发的专业辅助工具,基于 PlatformIO 构建系统,优先使用 Arduino 框架,必要时可切换到 ESP-IDF(或将 Arduino 作为 IDF 组件)。它帮助开发者避免经典陷阱:如错误使用闪存引脚或 PSRAM 引脚导致无法启动、未正确处理引导引脚(尤其是 GPIO12 上拉导致 3.3V 闪存模块启动失败)、ADC2 在 Wi-Fi 开启时不可用、因长时间阻塞循环触发任务看门狗、中断服务程序中使用非法操作等。技能提供详细的引脚参考、2.x 与 3.x API 迁移指南、分区表与文件系统配置、OTA 与安全启动等生产级功能说明,以及深度睡眠、PSRAM 使用等低功耗与内存管理的指导。当崩溃或重启发生时,技能引导用户启用监视器过滤器与调试构建,解码 Guru Meditation 异常、看门狗、brownout 等串口输出,并给出系统的排查步骤。它支持所有主流 ESP32 变体,并为连接性(Wi-Fi/BLE 鲁棒性)、NVS/Preferences 写入优化、flash 磨损管理提供了可操作的规则。该技能适用于 Visual Studio Code 中的 Cline、Cursor、Windsurf 等 AI 编码助手,以及与 Cline 兼容的插件。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/budenga/firmware-skills/tree/HEAD/skills/esp32-firmware

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

ESP32 固件(PlatformIO:优先 Arduino,按需 ESP-IDF)

同时加载 embedded-c-cpp 以获得编码规范、架构、单元测试和通用调试。本技能仅包含 ESP32 特定部分。

首先,识别项目

查找位置为什么重要
确切的芯片和模块(ESP32-WROOM-32E、ESP32-S3-WROOM-1-N16R8、ESP32-C3-MINI-1...)platformio.ini 中的 board =,用户,板子丝印不同变体的核心、引脚、PSRAM、USB、ADC 差异很大
框架framework = arduino、espidf 或 arduino, espidfAPI 和配置模型
Arduino core 版本(2.x 或 3.x)构建日志中的 framework-arduinoespressif32 @ x.y.z 行,platform = 行,或 ESP_ARDUINO_VERSION_MAJOR3.x 中多个 API 已更改(LEDC、定时器)——为错误版本编写的代码无法编译
闪存 / PSRAM 大小模块名称(N16R8 = 16 MB 闪存,8 MB 八进制 PSRAM),board_upload.flash_size分区、内存布局
板子接线用户/原理图避免引导引脚以及被闪存/PSRAM/USB/LED 占用的引脚

如果变体未知,且任务涉及引脚、外设、电源或内存,请在编写代码前查明——在 S3 上正常的引脚可能会阻止经典 ESP32 启动。

Arduino 还是 ESP-IDF?

坚持使用 Arduino(默认)切换到 ESP-IDF(在 PlatformIO 中:framework = espidf)或 Arduino + IDF(framework = arduino, espidf)
原型和大多数产品需要更改 Arduino 预编译核心未暴露的 sdkconfig 选项(回滚 OTA、安全启动、闪存加密、PM 自动浅睡眠、核心转储到闪存、FreeRTOS 设置)
大量可用库最新 IDF 特性,对内存/性能的严格控制
快速迭代生产环境加固

ESP32 上的 Arduino 已经运行在 FreeRTOS + ESP-IDF 之上,因此 IDF API(esp_*、xTaskCreatePinnedToCore、nvs_*、esp_sleep_*、heap_caps_*)可以直接在 Arduino 代码中使用。“Arduino 作为 IDF 组件”(arduino, espidf)保留了 Arduino 库并增加了 sdkconfig 控制——这是常见的迁移路径。

设置、platformio.ini 模板以及 2.x 与 3.x API 变更:请参考 references/platformio-setup.md。

防止典型 ESP32 故障的规则

  1. 引脚:绝不要使用闪存引脚(ESP32 上的 GPIO6–11)、PSRAM 引脚(WROVER 上的 16/17;S3 八进制 PSRAM 模块上的 33–37)或使用 USB 时的 USB 引脚;将引导引脚(ESP32:0、2、5、12、15)视为“需谨慎使用”——GPIO12 在启动时被拉高会导致 3.3V 闪存模块无法启动。Wi-Fi 开启时 ADC2 不可用(经典 ESP32):请使用 ADC1 引脚。各变体的完整表格:references/pins-and-variants.md。
  2. 不要永久阻塞 loop() 或任务:长时间 while 循环不包含 delay()/vTaskDelay() 会触发任务看门狗;while (WiFi.status() != WL_CONNECTED) 需要超时。
  3. ISR:标记为 IRAM_ATTR,保持短小,严禁调用 Serial.print、delay 或 Wi-Fi 函数;将工作传递给任务(队列 / 任务通知 / 标志)。共享变量使用 volatile + 临界区(portENTER_CRITICAL_ISR 自旋锁——ESP32 是双核的)。
  4. 任务栈的大小以字节为单位(在 ESP-IDF/Arduino 中,不同于原生 FreeRTOS 的“字”)。使用 uxTaskGetStackHighWaterMark(NULL)(返回字节)测量。Arduino loop() 默认有 8 KB 栈(使用 SET_LOOP_TASK_STACK_SIZE(16 * 1024) 来更改)。
  5. 电源:Wi-Fi 发送峰值电流约 300–500 mA。“Brownout detector was triggered”意味着需要改善供电(稳压器、大电容、线缆)——不要禁用 brownout 检测器。
  6. 堆:避免在循环中拼接 String 和频繁 new/malloc(长时间运行后会产生内存碎片);预先分配。定期检查 ESP.getFreeHeap() / heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT)。
  7. NVS/flash 磨损:不要在每次循环迭代中都写入 Preferences/NVS;仅在数据变化时写入,并限速。
  8. 不可逆操作:烧写 eFuse、安全启动、生产模式下的闪存加密是永久性的。未经用户明确确认,绝不运行 espefuse.py burn_*、在 sdkconfig 中启用这些功能,或者对含有数据的设备执行 erase_flash。

构建、烧录、监视(PlatformIO)

pio run -e <env>                       # 构建
pio run -e <env> -t upload             # 通过 USB/串口烧录
pio device monitor -e <env>            # 串口监视器(使用 monitor_speed / monitor_filters)
pio run -e <env> -t upload -t monitor  # 同时进行
pio run -e <env> -t uploadfs           # 将 data/ 目录以 LittleFS/SPIFFS 镜像上传
pio run -e <env> -t erase              # 擦除整个闪存(会先提示确认:会清除 NVS/WiFi 凭据)
pio run -e <env> -t menuconfig         # 仅限 ESP-IDF / arduino+espidf
pio test -e native                     # 主机单元测试(参见 embedded-c-cpp)

上传失败,提示“Failed to connect / Wrong boot mode detected”→ 按住 BOOT(GPIO0 / C3 上是 GPIO9),轻按 EN/RST,松开 BOOT;检查线缆是否支持数据传输;关闭任何其他占用该串口的监视器。在尝试上传前,确认板子已连接并确定端口(pio device list)。

当崩溃或重启时

可粘贴的串口输出是关键证据。启用 monitor_filters = esp32_exception_decoder 和 build_type = debug,以便将回溯信息解码为文件:行。然后参考 references/panics-and-debug.md 了解:Guru Meditation 类型(LoadProhibited、StoreProhibited、IllegalInstruction、RISC-V 上的 Load/Store 访问错误)、栈金丝雀、任务/中断看门狗、rst: 代码、brownout、启动循环、JTAG 调试(S3/C3/C6 上使用 debug_tool = esp-builtin)。

参考文件

文件何时查阅
references/platformio-setup.md创建/修复 platformio.ini、选择平台/核心版本、Arduino 2.x→3.x API 变更、PSRAM、USB-CDC 串口、分区与文件系统、在 PlatformIO 中使用 ESP-IDF、sdkconfig
references/pins-and-variants.md选择或审查引脚、比较 ESP32/S2/S3/C3/C6、ADC、触摸、深度睡眠唤醒引脚
references/rtos-memory-power.md任务、核心、ISR、队列、看门狗、栈/堆/PSRAM、IRAM、深度/浅睡眠、电池
references/panics-and-debug.md任何崩溃、复位、看门狗、brownout、启动循环、上传失败、JTAG 调试、核心转储、复位原因
references/connectivity-ota-security.mdWi-Fi/BLE 鲁棒性、NVS/Preferences、OTA 更新与回滚、安全启动/闪存加密决策

Individual skills in this repo

This repo contains 2 individual skills — each has its own dedicated page.

pic-firmware

Microchip PIC and dsPIC firmware with MPLAB X IDE, XC8 / XC16 / XC-DSC / XC32 compilers and MPLAB Code Configurator (MCC Melody/Classic) for PIC16, PIC18, PIC24, dsPIC33 (F/EP/EV/CK/CH dual-core/A) and PIC32 — configuration bits, oscillator/PLL, TRIS/LAT/PORT/ANSEL, PPS, interrupts per family, 8/16-bit atomicity and hardware-stack limits, dsPIC DSP engine and Q15 fixed-point, high-speed PWM/ADC control loops, motor control and digital power, XC8 memory and optimization, headless builds, programming and debugging with PICkit 4/5, MPLAB Snap and ICD (ipecmd), reset causes and traps. Use this skill whenever the user mentions any PIC or dsPIC part (PIC16F, PIC18F, PIC24F, dsPIC33EP/CK/CH, PIC32MX/MZ), MPLAB X, MCC, XC8/XC16/XC-DSC/XC32, PICkit, Snap, ICD, `#pragma config`, motorBench, or a .X project folder — even for a short question like "o pino não lê nada no PIC" or "o meu controlo PI no dsPIC oscila". Use together with embedded-c-cpp for general C quality, testing and arc...

stm32-firmware

STM32 firmware development with STM32CubeMX / STM32CubeIDE + HAL/LL and with PlatformIO — project structure and USER CODE rules, peripherals (UART/I2C/SPI/ADC/timers/DMA), clocks, low power, flash/option bytes, building from the command line, flashing and debugging with ST-Link (STM32CubeProgrammer CLI, OpenOCD, GDB, SWV), and HardFault analysis. Use this skill whenever the user works on any STM32 / Nucleo / Discovery / Blue Pill board, a `.ioc` file, CubeMX/CubeIDE, HAL_ functions, ST-Link, or sees a HardFault — even if they just say "o meu STM32 não arranca" or paste a HAL call. Use together with embedded-c-cpp for general C/C++ quality, testing and architecture.

相关技能