VSCode+PlatformIO开发环境搭建

发布时间 2024-01-10 15:27:11作者: biiigwang

VSCode+PlatformIO开发环境搭建

使用VSCode搭建platform开发环境,可以实现在vscode中搭建MCU等平台的嵌入式开发IDE,实现类似于IAR、Keil等功能,包括代码编辑、项目编译、程序烧录、调试等基础功能,同时可以使用Platform及VSCode的众多插件功能,从而提高开发效率,接下来时开发环境搭建的步骤。

插件安装

  • PlatformIO IDE​插件:

    image

  • C/C++​相关插件

    提供基础的C/C++语言支持​image

  • 可选:Clangd

    通过配合编译生成的编译数据库信息compile_commands.json​,提供高效的代码检查、跳转等功能

    image

  • 可选Teleplot​:数据可视化工具

    image

项目设置

新建/打开项目

项目通过PlatformIO提供的GUI工具进行管理,通过New Project​开新建pio项目,新建项目时会要求设置项目名称、Board​类型、软件框架类型、项目保存地址(默认保存在默认地址,需要自信修改项目保存位置)

image

项目配置

pio的项目根目录存在一个platformio.ini​的项目配置文件,以GD32官方Platform项目里程配置为例:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env]
platform = https://github.com/CommunityGD32Cores/platform-gd32.git
platform_packages = 
    framework-spl-gd32@https://github.com/CommunityGD32Cores/gd32-pio-spl-package.git
; GD32F30x series

[env:genericGD32F305VC]
board = genericGD32F305VC
framework = spl

[env:genericGD32F303CC]
board = genericGD32F303CC
framework = spl

一个项目可以根据不同的Env​同时配置多个不同的Board​,会在点击Build时会同时生成多种型号的产物,配置文件中各字段详见官方文档

Clangd插件支持

生成compile_commands.json

Clangd插件通过编译生成的数据库文件compile_commands.json​对项目进行分析,默认build时不会生成该文件,根据官方文档介绍,我们可以在build结束后执行> pio run -t compiledb​来生成,默认生成在项目根目录下,可以通过官方方案为每一个Env​生成各自的compile_commands.json

# Example

Generate compile_commands.json with toolchain includes for each project environment and save database to the “build_dir/envname” folder:

- platformio.ini:

‍‍```platformio.ini
[env:myenv]
platform = ...
board = ...
extra_scripts = pre:extra_script.py
‍‍```
- extra_script.py:

‍‍```python

import os
Import("env")

# include toolchain paths
env.Replace(COMPILATIONDB_INCLUDE_TOOLCHAIN=True)

# override compilation DB path
env.Replace(COMPILATIONDB_PATH=os.path.join("$BUILD_DIR", "compile_commands.json"))
‍‍```
## Generate compile_commands.json

> pio run -t compiledb

配置Clangd插件

Clangd目前还是找不到文件无法进行分析的,需要告诉一下clangd数据库文件在哪里

    "clangd.arguments": [
		// ...
        "--compile-commands-dir=/path/to/youConfigFile"
    ]

image