跳到主要内容
版本:Next

5.7 VSCode + STM32 二

通过Makefile进行项目编译

1. 安装软件和工具

1.1 STM32开发套件

STM32CubeMX 下载地址:https://www.st.com.cn/zh/development-tools/stm32cubemx.html

STM32CubeCLT 下载地址:https://www.st.com.cn/zh/development-tools/stm32cubeclt.html

STM32CubeMX使用时,出现无法连接网络、.ld文件缺少字段导致编译等一系列问题,请自行选择较低版本安装。

因为STM32开发套件依赖jar环境,需要与当前电脑系统版本匹配。

1.2 VS Code和扩展插件

VS Code下载地址:https://code.visualstudio.com/

扩展插件:

    C/C++、C/C++ Extension Pack、C/C++ Themes、CMake、CMake Tools、STM32 VS Code Extension(可以忽略)

1.3 Openocd-esp

Openocd-esp 下载地址:https://github.com/espressif/openocd-esp32/releases

添加 环境变量 - path:

    C:\Openocd-esp\openocd-esp32-win64-0.12.0-esp32-20240821\openocd-esp32\bin

C:\Openocd-esp\openocd-esp32-win64-0.12.0-esp32-20240821\openocd-esp32\share\openocd\scripts

1.4 gcc-arm-none-eabi

gcc-arm-none-eabi 下载地址:https://developer.arm.com/downloads/-/gnu-rm

添加 环境变量 - path:

    C:\GNU Arm Embedded Toolchain\10 2021.10\bin

1.5 cygwin64

cygwin64下载地址:Cygwin Installation

​ 选择下载gcc-core、gcc-g++、libgccpp1、gdb、make、cmake软件包

添加 环境变量 - path:

C:\cygwin64\bin
C:\cygwin64\sbin

2. 搭建编译环境

2.1 配置工程

【Pinout&Configuration】-> 【SYS】->【Debug】选择【 JTAG (5 pins) 】

imag

【Project Manager】 ->【Project】->【Toolchain/IDE】选择Makefile

imag

2.2【GENERATE CODE】

2.3 VS Code导入工程

  1. 工程项目文件夹右键菜单,选择【通过Code打开】;

  2. 【运行和调试】 -> 【创建launch.json 文件】-> 选择 【Cortex Debug】;

    imag

  3. 修改launch.json文件并保存;

    {
    "version": "0.2.0",
    "configurations": [
    {
    "name": "Build & launch Microcontroller - PowerDebugger",
    "type": "cortex-debug",
    "request": "launch",
    "servertype": "openocd",
    "configFiles": [
    "interface/cmsis-dap.cfg",
    "target/stm32f1x.cfg", // 修改 - 目标芯片配置文件 xxx.cfg
    ],
    "preLaunchTask": "Build",
    "executable": "${workspaceFolder}/build/STM32F103ZET.elf", // 修改 - 生成目标文件 xxx.elf
    "runToEntryPoint": "main",
    "svdFile": "C:/ST/STM32CubeCLT_1.16.0/STMicroelectronics_CMSIS_SVD/STM32F103.svd", // 修改 - 目标寄存器描述文件 xxx.svd
    "cwd": "${workspaceFolder}",
    },
    {
    "name": "Attach to Microcontroller - PowerDebugger",
    "type": "cortex-debug",
    "request": "attach",
    "servertype": "openocd",
    "configFiles": [
    "interface/cmsis-dap.cfg",
    "target/stm32f1x.cfg", // 修改 - 目标芯片配置文件 xxx.cfg
    ],
    "preLaunchTask": "Build",
    "executable": "${workspaceFolder}/build/STM32F103ZET.elf", // 修改 - 生成目标文件 xxx.elf
    "runToEntryPoint": "main",
    "svdFile": "C:/ST/STM32CubeCLT_1.16.0/STMicroelectronics_CMSIS_SVD/STM32F103.svd", // 修改 - 目标寄存器描述文件 xxx.svd
    "cwd": "${workspaceFolder}",
    },
    {
    "name": "Remote debug (gdb)",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceFolder}/build/STM32F103ZET.elf", // 修改 - 生成目标文件 xxx.elf
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "miDebuggerPath": "C:/GNU Arm Embedded Toolchain/10 2021.10/bin/arm-none-eabi-gdb.exe", // 修改 - GDB路径 xxx.exe
    "targetArchitecture": "arm",
    "preLaunchTask": "Build",
    "setupCommands": [
    {
    "text": "-enable-pretty-printing",
    "description": "Enable pretty printing",
    "ignoreFailures": true
    },
    {
    "text": "file F:/Users/Desktop/STM32F103ZET/build/STM32F103ZET.elf", // 修改 - 生成目标文件 xxx.elf
    "description": "Select the debug file (.elf) to gdb",
    "ignoreFailures": false
    },
    {
    "text": "target remote localhost:3333", // 修改 - Openocd server IP地址和端口
    "description": "Connect GDB Server",
    "ignoreFailures": false
    },
    {
    "text": "monitor reset",
    "description": "Reset MCU",
    "ignoreFailures": false
    },
    {
    "text": "monitor halt",
    "description": "Halt",
    "ignoreFailures": false
    },
    {
    "text": "load",
    "description": "Download file to MCU",
    "ignoreFailures": false
    },
    ],
    }
    ]
    }

    2.4 创建tasks.json

    菜单【终端】 -> 【配置任务..】 ->【使用模板创建tasks.json文件】-> 【Other】;

    imag

    imag

    2.5 修改tasks.json

    {
    "version": "2.0.0",
    "tasks": [
    {
    "label": "Clean",
    "type": "shell",
    "command": "make clean",
    "group": {
    "kind": "build",
    "isDefault": false
    }
    },
    {
    "label": "Build",
    "type": "shell",
    "options": {
    "cwd": "${workspaceRoot}"
    },
    "command": "make",
    "args": [
    "-j4"
    ],
    "group": {
    "kind": "build",
    "isDefault": true
    },
    "problemMatcher": []
    },
    {
    "label": "Clean & Rebuild",
    "type": "shell",
    "options": {
    "cwd": "${workspaceRoot}"
    },
    "command": "make",
    "args": [
    "-j4"
    ],
    "group": {
    "kind": "build",
    "isDefault": false
    },
    "dependsOn": "Clean"
    },
    {
    "label": "Erase",
    "type": "shell",
    "command": "openocd",
    "args": [
    "-f",
    "interface/cmsis-dap.cfg",
    "-f",
    "target/stm32f1x.cfg", // 修改 - 目标芯片配置文件 xxx.cfg
    "-c",
    "init",
    "-c",
    "reset halt",
    "-c",
    "flash erase_address 0x08000000 0x00010000",
    "-c",
    "reset",
    "-c",
    "exit"
    ],
    "group": {
    "kind": "build",
    "isDefault": false
    }
    },
    {
    "label": "Erase all",
    "type": "shell",
    "command": "openocd",
    "args": [
    "-f",
    "interface/cmsis-dap.cfg",
    "-f",
    "target/stm32f1x.cfg", // 修改 - 目标芯片配置文件 xxx.cfg
    "-c",
    "init",
    "-c",
    "reset halt",
    "-c",
    "flash erase_sector 0 0 last",
    "-c",
    "reset",
    "-c",
    "exit"
    ],
    "options": {
    "cwd": "${workspaceFolder}"
    },
    "group": {
    "kind": "build",
    "isDefault": false
    }
    },
    {
    "label": "Download",
    "type": "shell",
    "command": "openocd",
    "args": [
    "-f",
    "interface/cmsis-dap.cfg",
    "-f",
    "target/stm32f1x.cfg", // 修改 - 目标芯片配置文件 xxx.cfg
    "-c",
    "program build/STM32F103ZET_TEST.elf verify reset", // 修改 - 生成目标文件 xxx.elf
    "-c",
    "reset run",
    "-c",
    "exit"
    ],
    "options": {
    "cwd": "${workspaceFolder}"
    },
    "group": {
    "kind": "build",
    "isDefault": false
    }
    },
    {
    "label": "Build & Download",
    "type": "shell",
    "command": "openocd",
    "args": [
    "-f",
    "interface/cmsis-dap.cfg",
    "-f",
    "target/stm32f1x.cfg", // 修改 - 目标芯片配置文件 xxx.cfg
    "-c",
    "program build/STM32F103ZET_TEST.elf verify reset", // 修改 - 生成目标文件 xxx.elf
    "-c",
    "reset run",
    "-c",
    "exit"
    ],
    "options": {
    "cwd": "${workspaceFolder}"
    },
    "group": {
    "kind": "build",
    "isDefault": false
    },
    "dependsOn": "Build"
    },
    {
    "label": "Start openocd server",
    "type": "shell",
    "command": "openocd",
    "args": [
    "-f",
    "interface/cmsis-dap.cfg",
    "-f",
    "target/stm32f1x.cfg", // 修改 - 目标芯片配置文件 xxx.cfg
    "-c",
    "gdb_report_data_abort enable",
    "-c",
    "gdb_port 3333",
    "-c",
    "tcl_port 6666",
    "-c",
    "telnet_port 4444"
    ],
    "group": {
    "kind": "build",
    "isDefault": false
    }
    },
    ]
    }

3. 编译、调试程序

3.1 终端->运行任务

  • 编译:Build;

  • 下载:Download;

  • 擦除:Erase all;

  • 启动Openocd 服务:Start openocd server

imag

3.2 运行和调试

  • 本地调试:Attach to Microcontroller - PowerDebugger;

  • 远程调试:Remote debug (gdb)

4. IAP功能

IAP功能需要两套代码,一套是boot代码,用于开发完升级和跳转APP地址(如0x8006000)功能;

另一套是app代码,需要修改下面三个地方:

4.1 csystem_stm32f1xx.c

路径:Core\Src\system_stm32f1xx.csystem_stm32f1xx.c

        打开USER_VECT_TAB_ADDRESS,
并修改VECT_TAB_OFFSET为0x00006000U

4.2 STM32F103C8Tx_FLASH.ld

        修改
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 64K

FLASH (rx) : ORIGIN = 0x8006000, LENGTH = 40K

4.3 创建 download.cfg并添加

        set DOWNLOAD_ADDRESS 0x08006000

4.4 tasks.json Download下添加

        "-f",
"download.cfg"