VSCode + CubeMX + Makefile 构建STM32工程:分层架构与双调试配置

引言

1. Makefile文件

1.1 顶层Makefile

bash 复制代码
# Simple Makefile for STM32 Project

TARGET = Controller
BUILD_DIR = build
SUBDIRS = Hardware Core Log_C Test Drivers Middlewares seqlog FATFS DroneControl utility Comm
ROOT_DIR = $(CURDIR)
APP_VERSION = V_1.2.9_Final

MAKEFILE_DEPS = Makefile

MAKE_JOBS ?= 8

# Toolchain - Use relative path for cross-platform compilation
# Arm GNU Toolchain is bundled in the project directory
GCC_BIN_DIR = $(ROOT_DIR)/Compiler_Tools/Arm GNU Toolchain arm-none-eabi/14.3 rel1/bin
PREFIX = $(GCC_BIN_DIR)/arm-none-eabi-
CC = $(PREFIX)gcc
CXX = $(PREFIX)g++
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size

# Compiler flags
BASE_CFLAGS = -mcpu=cortex-m3 -mthumb -mfloat-abi=soft -Os -Wall -fdata-sections -ffunction-sections
C_DEFS = \
-DSTM32F103xB \
-DUSE_HAL_DRIVER \
-D_DEBUG

# Default: All modules enabled (1)
ENABLE_SENSOR_IMU			?= 0
ENABLE_SENSOR_GPS			?= 0
ENABLE_SENSOR_BATTERY 		?= 0
ENABLE_SENSOR_TEMPERATURE	?= 0
ENABLE_SENSOR_GEOMAGNETIC	?= 0
ENABLE_SENSOR_ULTRASONIC	?= 0
ENABLE_SENSOR_CAMERA		?= 0
ENABLE_MODULE_LORA			?= 0
ENABLE_MODULE_LOG			?= 0
ENABLE_MODULE_SEQ			?= 0

# Convert to compiler definitions (POSITIVE: define when enabled)
SENSOR_DEFS =
ifeq ($(ENABLE_SENSOR_IMU),1)
SENSOR_DEFS += -DENABLE_SENSOR_IMU
endif
ifeq ($(ENABLE_SENSOR_GPS),1)
SENSOR_DEFS += -DENABLE_SENSOR_GPS
endif
ifeq ($(ENABLE_SENSOR_BATTERY),1)
SENSOR_DEFS += -DENABLE_SENSOR_BATTERY
endif
ifeq ($(ENABLE_SENSOR_TEMPERATURE),1)
SENSOR_DEFS += -DENABLE_SENSOR_TEMPERATURE
endif
ifeq ($(ENABLE_SENSOR_GEOMAGNETIC),1)
SENSOR_DEFS += -DENABLE_SENSOR_GEOMAGNETIC
endif
ifeq ($(ENABLE_SENSOR_ULTRASONIC),1)
SENSOR_DEFS += -DENABLE_SENSOR_ULTRASONIC
endif
ifeq ($(ENABLE_SENSOR_CAMERA),1)
SENSOR_DEFS += -DENABLE_SENSOR_CAMERA
endif
ifeq ($(ENABLE_MODULE_LORA),1)
SENSOR_DEFS += -DENABLE_MODULE_LORA
endif

ifeq ($(ENABLE_MODULE_LOG),1)
MODULE_DEFS += -DENABLE_MODULE_LOG
endif
ifeq ($(ENABLE_MODULE_SEQ),1)
MODULE_DEFS += -DENABLE_MODULE_SEQ
endif

VERSION_DEF = -DAPP_VERSION='\"$(APP_VERSION)\"'

# Add sensor definitions to compiler flags
C_DEFS += $(SENSOR_DEFS) $(VERSION_DEF)
C_DEFS += $(MODULE_DEFS)

INCLUDE_PATHS = \
-I$(ROOT_DIR)/Core/Inc \
-I$(ROOT_DIR)/Drivers/STM32F1xx_HAL_Driver/Inc \
-I$(ROOT_DIR)/Drivers/CMSIS/Device/ST/STM32F1xx/Include \
-I$(ROOT_DIR)/Drivers/CMSIS/Include \
-I$(ROOT_DIR)/Middlewares/Third_Party/FatFs/src \
-I$(ROOT_DIR)/Hardware \
-I$(ROOT_DIR)/FATFS/Target \
-I$(ROOT_DIR)/FATFS/App \
-I$(ROOT_DIR)/Log_C/include \
-I$(ROOT_DIR)/seqlog/src \
-I$(ROOT_DIR)/Test \
-I$(ROOT_DIR)/DroneControl/Inc \
-I$(ROOT_DIR)/MAVLink \
-I$(ROOT_DIR)/utility/Inc \
-I$(ROOT_DIR)/Comm/Inc

CFLAGS = $(BASE_CFLAGS) $(C_DEFS) $(INCLUDE_PATHS) -include stdint.h
CPPFLAGS = $(BASE_CFLAGS) $(C_DEFS) $(INCLUDE_PATHS) -include stdint.h -std=c++17 -fno-exceptions -fno-rtti
ASFLAGS = $(BASE_CFLAGS)

# Add these flags
# Optimize for size (removed -g for release)
# Optimize for size (removed -g for release)
# Debug symbols in linker
CFLAGS += -g
CPPFLAGS += -g
LDFLAGS += -g

# Linker flags
LDSCRIPT = STM32F103C8Tx_FLASH.ld
LIBS = -lc -lm -lnosys -lstdc++
LDFLAGS = -mcpu=cortex-m3 -mthumb -mfloat-abi=soft -specs=nano.specs -T$(LDSCRIPT) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections,--print-memory-usage -u _printf_float


# Startup file
STARTUP_ASM = startup_stm32f103xb.s
STARTUP_OBJ = $(BUILD_DIR)/$(notdir $(STARTUP_ASM:.s=.o))

# Default target - build everything including final ELF, HEX, BIN
all: $(SUBDIRS) $(STARTUP_OBJ) $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
	@echo "Build complete"

# Build subdirectories (object files)
.PHONY: $(SUBDIRS)
$(SUBDIRS):
	@echo "Building $@..."
	@if not exist $(BUILD_DIR)\$@ mkdir $(BUILD_DIR)\$@
	$(MAKE) -j$(MAKE_JOBS) -C $@ ROOT_DIR=$(ROOT_DIR) BUILD_DIR=$(ROOT_DIR)/$(BUILD_DIR)/$@ CC="$(CC)" CXX="$(CXX)" AS="$(AS)" CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" MAKEFILE_DEPS="$(ROOT_DIR)/Makefile"

# Compile startup assembly file
$(STARTUP_OBJ): $(STARTUP_ASM) $(MAKEFILE_DEPS)
	@echo "Compiling startup file..."
	@if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)
	$(AS) -c $(ASFLAGS) $< -o $@

# Function to find all object files
find_objects = $(shell if exist $(BUILD_DIR) dir /b /s $(BUILD_DIR)\*.o 2>nul)

# Link object files into ELF
$(BUILD_DIR)/$(TARGET).elf: $(SUBDIRS) $(STARTUP_OBJ)
	@echo "Linking $(TARGET).elf..."
	$(CXX) $(call find_objects) $(LDFLAGS) -o $@
	$(SZ) $@

# Create HEX file from ELF
$(BUILD_DIR)/$(TARGET).hex: $(BUILD_DIR)/$(TARGET).elf
	@echo "Creating $(TARGET).hex..."
	$(CP) -O ihex $< $@

# Create BIN file from ELF
$(BUILD_DIR)/$(TARGET).bin: $(BUILD_DIR)/$(TARGET).elf
	@echo "Creating $(TARGET).bin..."
	$(CP) -O binary -S $< $@

#######################################
# openocd - replace YOUR_SERIAL_1 and YOUR_SERIAL_2 with actual ST-Link SN
#######################################
STLINK_SERIAL_1 = 43006100170000393333574E
STLINK_SERIAL_2 = 000000000000000000000001

flash: all
	openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "adapter serial $(STLINK_SERIAL_2)" -c "program $(BUILD_DIR)/$(TARGET).elf verify exit"

flash1: all
	openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "adapter serial $(STLINK_SERIAL_1)" -c "program $(BUILD_DIR)/$(TARGET).elf verify exit"

flash2: all
	openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "adapter serial $(STLINK_SERIAL_2)" -c "program $(BUILD_DIR)/$(TARGET).elf verify exit"

#######################################
# stlink-cli - ST-LINK_CLI.exe flashing
#######################################
# ST-LINK_CLI syntax:
#   -c SWD SN=<serial> UR  : Connect via SWD, under reset
#   -P <file>              : Program file (.hex recommended)
#   -Rst                   : Reset after programming
flash-cli: all
	ST-LINK_CLI.exe -c SWD UR SN=$(STLINK_SERIAL_2) -P "$(BUILD_DIR)/$(TARGET).hex" -Rst

flash-cli1: all
	ST-LINK_CLI.exe -c SWD UR SN=$(STLINK_SERIAL_1) -P "$(BUILD_DIR)/$(TARGET).hex" -Rst

flash-cli2: all
	ST-LINK_CLI.exe -c SWD UR SN=$(STLINK_SERIAL_2) -P "$(BUILD_DIR)/$(TARGET).hex" -Rst

#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)

# Clean
clean:
	@echo "Cleaning..."
	@if exist $(BUILD_DIR) rmdir /s /q $(BUILD_DIR)

# Help
help:
	@echo "Commands:"
	@echo "  make all         - Build project (ELF, HEX, BIN)"
	@echo "  make flash       - Build + flash via openocd (ST-Link 2)"
	@echo "  make flash1      - Build + flash via openocd (ST-Link 1)"
	@echo "  make flash2      - Build + flash via openocd (ST-Link 2)"
	@echo "  make flash-cli   - Build + flash via ST-LINK_CLI.exe (ST-Link 2)"
	@echo "  make flash-cli1  - Build + flash via ST-LINK_CLI.exe (ST-Link 1)"
	@echo "  make flash-cli2  - Build + flash via ST-LINK_CLI.exe (ST-Link 2)"
	@echo "  make clean       - Clean build"
	@echo "  make help        - Show help"
	@echo ""
	@echo "Subdirectories: $(SUBDIRS)"
	@echo ""
	@echo "Edit Makefile to set STLINK_SERIAL_1 and STLINK_SERIAL_2"

1.2 子模块Makefile示例

bash 复制代码
# Hardware directory Makefile

# Source files in this directory
C_SOURCES = \
	usr_pwm.c \
	ST7789.c \
	usr_adc.c

CPP_SOURCES = \
	usr_uart.cpp \
	disk_opt.cpp \
	AttitudeIndicator.cpp

# Object files
C_OBJECTS = $(addprefix $(BUILD_DIR)/,$(C_SOURCES:.c=.o))
CPP_OBJECTS = $(addprefix $(BUILD_DIR)/,$(CPP_SOURCES:.cpp=.o))
OBJECTS = $(C_OBJECTS) $(CPP_OBJECTS)

C_DEPENDS   = $(C_OBJECTS:.o=.d)
CPP_DEPENDS = $(CPP_OBJECTS:.o=.d)
DEPENDS = $(C_DEPENDS) $(CPP_DEPENDS)

override MAKEFILE_DEPS += Makefile

# Default target - mark as phony
.PHONY: all
# Default target
all: $(OBJECTS)
	@echo "Hardware files compiled"

# C source files compilation rule
$(BUILD_DIR)/%.o: %.c $(MAKEFILE_DEPS)
	@if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)
	$(CC) -c $(CFLAGS) \
		-MMD -MP -MF $(BUILD_DIR)/$*.d \
		-Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) \
		$< -o $@

# C++ source file compilation rule
$(BUILD_DIR)/%.o: %.cpp $(MAKEFILE_DEPS)
	@if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)
	$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) \
		-MMD -MP -MF $(BUILD_DIR)/$*.d \
		-Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.cpp=.lst)) \
		$< -o $@

# Include generated dependency files
-include $(DEPENDS)

# Clean rule
clean:
	@if exist "$(BUILD_DIR)" rmdir /s /q "$(BUILD_DIR)"

.PHONY: all clean

2. 分级目录

2.1 项目目录结构

复制代码
目录: C:\C8T6_hasCompiler


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2026/5/25     10:30                .vscode
d-----         2026/5/25     12:20                build
d-----         2026/5/25     10:30                Comm
d-----         2026/5/25     12:14                Compiler_Tools
d-----         2026/5/25     10:31                Core
d-----         2026/5/25     10:31                Drivers
d-----         2026/5/25     10:31                DroneControl
d-----         2026/5/25     10:31                FATFS
d-----         2026/5/25     10:31                Hardware
d-----         2026/5/25     10:31                Log_C
d-----         2026/5/25     10:31                MAVLink
d-----         2026/5/25     10:31                Middlewares
d-----         2026/5/25     10:31                seqlog
d-----         2026/5/25     10:31                STM32CubeIDE
d-----         2026/5/25     10:31                Test
d-----         2026/5/25     10:31                tools
d-----         2026/5/25     10:31                utility
-a----         2026/1/28     18:04          25057 .cproject
-a----          2026/5/5     16:12             39 .gitignore
-a----         2026/5/17     18:18          14070 .mxproject
-a----         2026/1/28     18:04           1215 .project
-a----         2026/5/17     18:38          10378 Controller.ioc
-a----         2026/5/25     10:18           7213 Makefile
-a----         2026/1/28     18:04          10089 startup_stm32f103xb.s
-a----         2026/3/11     13:09           5904 STM32F103C8Tx_FLASH.ld

2.2 Compiler_Tools 工具路径说明

Compiler_Tools 目录是本工程的核心工具链目录,包含了所有必需的编译、调试和烧录工具。为了满足不同用户的需求,我提供了两个版本的工程文件:

  1. C8T6.7z(精简版) - 仅包含源码
    https://download.csdn.net/download/oqqsoap1234567/92905318?spm=1003.2166.3001.6637.2

    • 需要用户自行下载并copy以下工具到 Compiler_Tools 目录:
      • Arm GNU Toolchain arm-none-eabi(交叉编译工具链)
      • OpenOCD-20250710-0.12.0(开源调试工具)
      • stlink-gdbserver(ST-LINK GDB服务器)
      • stlink-server(ST-LINK服务器)
      • STM32CubeProgrammer(ST官方编程工具)
      • ST_LINK-Utility(ST-LINK工具)
  2. C8T6_hasCompiler.7z(完整版) - 已集成所有编译工具
    https://download.csdn.net/download/oqqsoap1234567/92905353?spm=1003.2166.3001.6637.1

    • 下载解压后即可直接编译,无需额外配置
    • 包含完整的工具链,开箱即用
    • 适合快速开始项目开发

注意 :Makefile中已经配置了相对路径 $(ROOT_DIR)/Compiler_Tools/...,确保工具链的正确引用。

注意 :Makefile中已经配置了相对路径 $(ROOT_DIR)/Compiler_Tools/...,确保工具链的正确引用。

2.3 模块化编译机制

本工程采用模块化编译设计,每个子目录都包含独立的Makefile文件。这种设计具有以下特点:

  1. 独立修改:各子目录的Makefile可以独立修改,互不影响
  2. 增量编译:修改子目录的Makefile、头文件或源代码时,仅该子模块会重新编译
  3. 全局影响:修改根目录的Makefile会导致整个项目重新编译
  4. 编译效率:通过依赖关系管理,避免不必要的重复编译

这种分层级的Makefile结构既保证了编译的灵活性,又提高了大型项目的编译效率。

3. 调试工具

bash 复制代码
    目录: C:\C8T6_hasCompiler\Compiler_Tools


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2026/5/25     10:30                Arm GNU Toolchain arm-none-eabi
d-----         2026/5/25     10:31                OpenOCD-20250710-0.12.0
d-----         2026/5/25     12:14                stlink-gdbserver
d-----         2026/5/25     12:14                stlink-server
da----         2026/5/25     12:14                STM32CubeProgrammer
d-----         2026/5/25     10:31                ST_LINK-Utility

3.2 ST-Link烧录程序配置

bash 复制代码
flash-cli: all
	ST-LINK_CLI.exe -c SWD UR SN=$(STLINK_SERIAL_2) -P "$(BUILD_DIR)/$(TARGET).hex" -Rst

flash-cli1: all
	ST-LINK_CLI.exe -c SWD UR SN=$(STLINK_SERIAL_1) -P "$(BUILD_DIR)/$(TARGET).hex" -Rst

flash-cli2: all
	ST-LINK_CLI.exe -c SWD UR SN=$(STLINK_SERIAL_2) -P "$(BUILD_DIR)/$(TARGET).hex" -Rst

3.3 openocd烧录程序配置

bash 复制代码
flash: all
	openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "adapter serial $(STLINK_SERIAL_2)" -c "program $(BUILD_DIR)/$(TARGET).elf verify exit"

flash1: all
	openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "adapter serial $(STLINK_SERIAL_1)" -c "program $(BUILD_DIR)/$(TARGET).elf verify exit"

flash2: all
	openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "adapter serial $(STLINK_SERIAL_2)" -c "program $(BUILD_DIR)/$(TARGET).elf verify exit"

serial no. 是用来绑定你自己的st-link序列号,当你电脑上存在多个st-link,需要指定哪个st-link烧录到哪个板子上,不然就烧录错了。

4. VSCode JSON配置

4.1 launch.json调试配置

xml 复制代码
{
    "version": "0.2.0",
    "configurations": [
        { 
            "name": "[OpenOCD] Debug with openocd Server",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/Controller.elf",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "${workspaceFolder}/Compiler_Tools/Arm GNU Toolchain arm-none-eabi/14.3 rel1/bin/arm-none-eabi-gdb.exe",
            "preLaunchTask": "start-openocd-server",
            "postDebugTask": "stop-openocd-server",
            "setupCommands": [
                {
                    "description": "Wait for server to be ready",
                    "text": "shell sleep 1",
                    "ignoreFailures": true
                },
                {
                    "description": "Connect to GDB server",
                    "text": "target remote localhost:3333",
                    "ignoreFailures": false
                },
                {
                    "description": "Reset and halt",
                    "text": "monitor reset halt",
                    "ignoreFailures": false
                },
            ]
        },
        {
            "name": "[ST-LINK] Debug with ST-LINK Server",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/Controller.elf",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "arm-none-eabi-gdb.exe",
            "preLaunchTask": "start-stlink-gdbserver",
            "postDebugTask": "stop-stlink-gdbserver",
            "setupCommands": [
                {
                    "description": "Wait for server to be ready",
                    "text": "shell sleep 1",
                    "ignoreFailures": true
                },
                {
                    "description": "Connect to ST-LINK GDB server",
                    "text": "target remote localhost:61234",
                    "ignoreFailures": false
                },
                {
                    "description": "Hardware reset",
                    "text": "monitor reset",
                    "ignoreFailures": false
                },
                {
                    "description": "Halt after reset",
                    "text": "monitor halt",
                    "ignoreFailures": false
                },
            ]
        },
        {
            "name": "[BUILD] Make and Flash-cli",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "args": [
                "/c",
                "make && make flash-cli"
            ],
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
        },
        {
            "name": "[BUILD] Make and Flash",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "args": [
                "/c",
                "make && make flash"
            ],
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
        },
        {
            "name": "[Clean] Make Clean",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "args": [
                "/c",
                "make clean"
            ],
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
        },
        {
            "name": "[Update] Update ST-LINK",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "powershell",
            "args": ["/c", "exit 0"],
            "preLaunchTask": "upgrade-stlink-firmware",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
        },
    ]
}

4.2 tasks.json构建任务

xml 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "make",
            "args": [
                "all"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ],
            "presentation": {
                "echo": true,
                "reveal": "silent",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "make",
            "args": [
                "clean"
            ],
            "group": "build",
            "problemMatcher": []
        },
        {
            "label": "flash",
            "type": "shell",
            "command": "make",
            "args": [
                "flash"
            ],
            "group": "build",
            "problemMatcher": []
        },
        {
            "label": "build-and-flash",
            "type": "shell",
            "command": "make",
            "args": [
                "flash"
            ],
            "group": "build",
            "problemMatcher": [
                "$gcc"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            }
        },
        {
            "label": "start-openocd-server",
            "type": "shell",
            "command": "openocd",
            "args": [
                "-f",
                "interface/stlink.cfg",
                "-f",
                "target/stm32f1x.cfg"
            ],
            "isBackground": true,
            "problemMatcher": {
                "pattern": [
                    {
                        "regexp": ".",
                        "file": 1,
                        "location": 2,
                        "message": 3
                    }
                ],
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": ".*",
                    "endsPattern": ".*"
                }
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            }
        },
        {
            "label": "stop-openocd-server",
            "type": "shell",
            "command": "taskkill",
            "args": [
                "/IM",
                "openocd.exe",
                "/F"
            ],
            "problemMatcher": []
        },
        {
            "label": "start-stlink-gdbserver",
            "type": "shell",
            "command": "${workspaceFolder}/Compiler_Tools/stlink-gdbserver/7.11.0+st.1/bin/ST-LINK_gdbserver.exe",
            "args": [
                "-d",
                "-p",
                "61234",
                "-cp",
                "${workspaceFolder}/Compiler_Tools/STM32CubeProgrammer/bin",
                "-k",
                "--halt",
                "--frequency",
                "4000",
                "-v"
            ],
            "isBackground": true,
            "problemMatcher": "$tsc-watch",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            }
        },
        {
            "label": "stop-stlink-gdbserver",
            "type": "shell",
            "command": "taskkill",
            "args": [
                "/IM",
                "ST-LINK_gdbserver.exe",
                "/F"
            ],
            "problemMatcher": []
        },
        {
            "label": "upgrade-stlink-firmware",
            "type": "shell",
            "command": "${workspaceFolder}/Compiler_Tools/ST_LINK-Utility/ST-LINK Utility/ST-LinkUpgrade.exe",
            "args": [],
            "problemMatcher": [],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            }
        }
    ]
}

5. 总结

本文详细介绍了使用VSCode配合STM32CubeMX配置STM32工程的完整流程,主要包含以下四个核心部分:

5.1 主要配置要点总结

  1. Makefile分层级构建

    • 顶层Makefile统一管理编译参数、工具链和模块依赖
    • 子模块Makefile实现模块化编译,支持C/C++混合编程
    • 支持条件编译,通过宏定义灵活控制功能模块
  2. 项目目录结构清晰

    • 分层级目录组织,包括硬件驱动、核心逻辑、通信模块等
    • 工具链内置,确保跨平台编译一致性
    • 构建输出统一到build目录,便于管理
  3. 双调试工具支持

    • OpenOCD:开源调试工具,支持多种调试器
    • ST-LINK:官方调试工具,稳定性好
    • 两种工具均集成到Makefile,一键烧录
  4. VSCode深度集成

    • launch.json配置两种调试方案
    • tasks.json定义构建、烧录、清理等任务
    • 支持后台服务启动/停止,调试体验流畅

5.2 优势特点

  • 工程化构建:Makefile分层管理,编译效率高
  • 模块化设计:各功能模块独立,便于维护和扩展
  • 调试便捷:VSCode直接调试,支持断点、单步等
  • Windows专用:工具链内置,针对Windows环境优化配置
  • 双工具备份:OpenOCD和ST-LINK互为备份,提高可靠性### 5.3 使用建议
  1. 首次使用 :先运行make all确认编译环境正常
  2. 日常开发:使用VSCode的调试配置直接调试
  3. 批量烧录 :使用make flash-cli快速烧录
  4. 问题排查:检查串口号配置和工具链路径

这套配置方案经过实际项目验证,能够显著提升STM32开发效率,特别适合需要频繁调试和迭代的嵌入式项目。

使用建议

  1. 建议从官网下载最新稳定版本的工具
  2. 安装时注意选择与本文配置相匹配的版本

这些工具的组合使用能够为STM32开发提供完整的工具链支持,从代码编写、编译、调试到烧录的全流程覆盖。

相关推荐
m0_3771081420 小时前
stm32-TIM
stm32·单片机·嵌入式硬件
相醉为友20 小时前
001 VSCode图形化提交也弹出GPG密码框
ide·vscode·编辑器
方也_arkling21 小时前
【Java-Day01】安装软件并修改基础配置项
java·ide·intellij-idea
雪的季节21 小时前
TRAE IDE的安装和使用
ide
东小东博客1 天前
STM32 WS2812 Proteus仿真 汉字显示 SPI控制
stm32·嵌入式硬件·proteus
独隅1 天前
Android Studio 接入 CodeX 的全面指南
android·ide·android studio
解道Jdon1 天前
[Budi插件:VsCode状态栏显示Copilot使用情况
ide·windows·git·svn·eclipse·github·visual studio
疏狂难除1 天前
JetBrains IDE插件开发教程(一)
ide
Jonathan_LF2 天前
DMA使用心得-STM32
stm32·单片机·嵌入式硬件