STM32G474 CMAKE VSCODE 开发环境搭建

发布于:2024-05-05 ⋅ 阅读:(27) ⋅ 点赞:(0)

本篇博文尝试搭建 stm32g474 的开发环境

一. 工具安装

1. 关于 MinGWOpenOCDZadig 这些工具的下载和安装见 Jlink+OpenOCD+STM32 Vscode 下载和调试环境搭建_vscode openocd stm32 jlink-CSDN博客

2. 导出一个 STM32 的 CMAKE 工程,这里略过。

3. 安装 ninja Ninja, a small build system with a focus on speed (ninja-build.org)

4. 安装 CMAKE Download CMake

在环境变量中包含以下路径:

二. 编译

安装所以以上工具就可以进行 STM32 CAMKE 工具的编译。

但是生成的 download 文件只有 elf 格式,在工程最外层的 CMakeLists.txt 中加入

add_custom_command( TARGET ${EXECUTABLE_NAME} moto_control POST_BUILD 
COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_NAME} moto_control.elf ${PROJECT_NAME}.hex 
COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE_NAME} moto_control.elf ${PROJECT_NAME}.bin )

其中  moto_control 是工程名。

 在 TERMINAL 中执行

cmake . -G "Unix Makefiles"

可生成 Makefile 文件

通过

make -j4 

可以达到 ninja 同样的编译效果。

或者通过

make -j4 2>&1|tee xxx.log

可以把 log 输出到 xxx.log 文件

通过

make clean
cd build/Debug
ninja clean

可以实现编译的清除。

三. 调试

通过建立 launch.json 实现 Debug。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "cwd": "${workspaceRoot}",
            "executable": "./build/moto_control.elf",
            "name": "Debug Microcontroller",
            "request": "launch",
            "type": "cortex-debug",
            "showDevDebugOutput": false,
            "servertype": "openocd",
            "configFiles": [
                "E:/Project/tools/OpenOCD-20231002-0.12.0/share/openocd/scripts/interface/jlink_swd.cfg",
                "E:/Project/tools/OpenOCD-20231002-0.12.0/share/openocd/scripts/target/stm32g4x.cfg"
            ]
        }
    ]
}

四. 下载

通过建立 task 的方式完成

在 .vscode 文件夹下建立 tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "make",
            "args": [],
            "group": "build"
        },
        {
            "label": "download",
            "type": "shell",
            "command": "openocd",
            "args": [
                "-f",
                "E:/Project/tools/OpenOCD-20231002-0.12.0/share/openocd/scripts/interface/jlink_swd.cfg",
                "-f",
                "E:/Project/tools/OpenOCD-20231002-0.12.0/share/openocd/scripts/target/stm32g4x.cfg",
                "-c",
                "program build/moto_control.elf verify reset exit"
            ],
            "group": "build",
            "problemMatcher": []
        }
    ]
}

通过 TERMINAL 出的 Run Task... 在搜索框出现 download 任务,点击后即可下载。 


网站公告

今日签到

点亮在社区的每一天
去签到