Skip to content

tqfx/stm32f1

Repository files navigation

环境搭建

安装

不要忘记将工具链加入系统环境变量

配置

STM32CubeMX

  • File -> New Project

  • 选择匹配的芯片,点击 Start Project

  • Project Manager -> Project
    设置 Project NameProject Location
    Application Structure 修改为 Basic
    Toolchain/IDE 修改为 Makefile
    勾选 Use latest available version

  • Project Manager -> Code Generator
    STM32Cube MCU packages and embedded software packs 选择 Copy only the necessary library files
    Generated files 勾选 Generate peripheral initialization as a pair of '.c/.h' files per peripheral

  • Project Manager -> Advanced Settings,将 HAL 改为 LL

  • 点击 GENETATE CODE -> Open Folder

Visual Studio Code

  • 右键通过 Code 打开

  • 安装插件 Chinese (Simplified) LanguageC/C++C++ IntellisenseCortex-Debug

  • F1 键或者 ctrl+shift+P,输入 编辑配置JSON,然后选择下图选项

用以下内容替换原内容

{
    "configurations": [
        {
            "name": "ARM",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "USE_FULL_LL_DRIVER",
                "USE_HAL_DRIVER",
                "STM32MCU"
            ],
            "cStandard": "gnu11",
            "cppStandard": "gnu++11",
            "compilerPath": "arm-none-eabi-gcc",
            "intelliSenseMode": "gcc-arm"
        }
    ],
    "version": 4
}
字符 解释
includePath 头文件路径
defines 宏定义
cStandard C标准
cppStandard C++标准
compilerPath 编译器路径
intelliSenseMode 智能感知模式
  • 新建文件 openocd.cfg,写入以下内容
source [find interface/stlink-v2.cfg]
source [find target/stm32f1x.cfg]
字符 解释
interface 调试器
target 目标板
  • 运行 -> 添加配置 -> Cortex Debug 或者按下图操作
{
    "version": "0.2.0",
    "configurations": [
        {
            "cwd": "${workspaceRoot}",
            "executable": "build/ELFNAME.elf",
            "name": "Debug",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "openocd",
            "configFiles": ["openocd.cfg"],
            "preLaunchTask": "Build",
            "postDebugTask": "Reset"
        }
    ]
}
字符 解释
executable 工程的 .elf 文件路径
servertype 调试应用
configFiles 配置文件路径
preLaunchTask 调试开始前运行的任务
postDebugTask 调试结束后运行的任务
  • 打开Makefile文件,追加以下内容
flash:
	openocd -f ../openocd.cfg -c init -c halt -c "program $(BUILD_DIR)/$(TARGET).hex verify reset exit"
reset:
	openocd -f ../openocd.cfg -c init -c halt -c reset -c shutdown
  • 终端 -> 配置任务,用以下内容替换原内容
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "Build",
            "command": "make -j",
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
        },
        {
            "type": "shell",
            "label": "Flash",
            "command": "make -j;make flash",
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
        },
        {
            "type": "shell",
            "label": "Reset",
            "command": "make reset",
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
        },
    ]
}

终端 -> 运行任务,会出现三个任务 BuildFlashReset

任务 解释
Build 构建工程
Flash 下载程序
Reset 复位单片机
  • ctrl+` 切换终端,输入以下命令
命令 解释
make 编译工程代码
make flash 将代码烧录进单片机
make reset 将单片机复位