PlatformIO 是一个跨平台的嵌入式开发工具链,支持超过 50 种开发平台和 2000 多个开发板。
官方资源链接(复制到浏览器打开):
https://docs.platformio.org/en/latest/what-is-platformio.html
它集成了构建系统、库管理、调试工具和持续集成等功能,为嵌入式开发者提供了高效的一站式解决方案。
# 使用 Python pip 安装
python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)"
安装完成后,PlatformIO 会自动下载必要的工具链。首次启动可能需要一些时间,因为它会:
典型的 PlatformIO 项目结构如下:
my_project/
├── include/ # 头文件目录
├── lib/ # 本地库目录
├── src/ # 源代码目录
│ └── main.cpp # 主程序文件
├── test/ # 测试代码目录
├── platformio.ini # 项目配置文件
└── .pio/ # PlatformIO 生成的文件(自动创建)
platformio.ini
是 PlatformIO 项目的核心配置文件,示例:
[env:uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 9600
lib_deps =
adafruit/Adafruit NeoPixel@^1.7.0
bblanchon/ArduinoJson@^6.18.5
常用配置选项:
platform
: 硬件平台(如 atmelavr、espressif32 等)board
: 具体开发板型号framework
: 使用的框架(arduino、espidf 等)monitor_speed
: 串口监视器波特率lib_deps
: 项目依赖的库build_flags
: 编译选项upload_port
: 上传端口debug_tool
: 调试工具配置pio run
构建过程中会显示详细的编译信息,包括:
pio run --target upload
上传前确保:
pio device monitor
常用串口监视器命令:
Ctrl+T Ctrl+H
- 显示帮助Ctrl+T Ctrl+X
- 重置终端Ctrl+T Ctrl+Q
- 退出PlatformIO 提供了强大的库管理功能:
pio lib search "sensor"
pio lib install 12 # 通过ID安装
pio lib install "Adafruit NeoPixel" # 通过名称安装
pio lib update
在 platformio.ini 中可指定库版本:
lib_deps =
adafruit/Adafruit NeoPixel@^1.7.0
# 或精确版本
bblanchon/ArduinoJson@6.18.5
版本说明符:
^1.2.3
- 兼容版本(允许次版本和修订号更新)~1.2.3
- 近似版本(只允许修订号更新)1.2.3
- 精确版本PlatformIO 支持在单个项目中配置多个环境:
[env:uno]
platform = atmelavr
board = uno
framework = arduino
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_speed = 115200
切换环境:
pio run -e nodemcuv2
[env:custom_build]
platform = espressif32
board = esp32dev
framework = arduino
build_flags =
-D DEBUG_LEVEL=2
-Os
-Wall
[env:debug]
platform = ststm32
board = nucleo_f401re
framework = mbed
debug_tool = stlink
launch.json
调试配置PlatformIO 集成了单元测试框架:
test
目录下创建测试代码pio test -e uno
使用 PlatformIO Remote 进行远程开发:
PlatformIO 内置 CI 支持,示例 .travis.yml
:
language: python
python:
- "3.7"
install:
- pip install -U platformio
script:
- platformio run
pio lib list
查看已安装库pio run -t clean
.pio
和 .vscode
目录PlatformIO 是现代嵌入式开发的强大工具,它通过整合各种工具链和简化开发流程,显著提高了开发效率。虽然初期学习曲线比 Arduino IDE 陡峭,但其提供的专业功能和灵活性使其成为复杂嵌入式项目的理想选择。
通过本指南,您应该已经掌握了 PlatformIO 的核心功能和使用方法。随着实践的深入,您将能够充分利用 PlatformIO 的强大功能,开发出更加专业和高效的嵌入式应用程序。
觉得文章不错,点击“分享”、“赞”、“推荐” 呗!