文章目录

  • 一、环境
  • 二、需要安装
  • 三、安装步骤
  • 3.1 安装STM32CubeMX(工程代码生成)
  • 3.2 安装openocd(调试和烧写固件)
  • 3.3 安装gcc-arm-none-eabi(编译和gdb调试)
  • 3.4 安装vscode
  • 四、工程创建
  • 五、VSCODE工程配置
  • 5.1 VSCODE打开工程
  • 5.2 打开terminal(Ctrl+~),直接make编译生成elf
  • 5.3 配置Debug,创建launch.json
  • 六、烧写固件


一、环境

  • PC: Ubuntu20.04 x64
  • 开发板: stm32f407discovery(ST官方开发板,板子自带stlink-v2.1)
开发板通mini usb线连接PC(注意按照开发板说明书中说的接好跳线,如下图)

ubuntu vscode 使用docker vscode ubuntu arm_ubuntu

二、需要安装

STM32CubeMX,openocd,gcc-arm-none-eabi,vscode(或者其他IDE)

三、安装步骤

3.1 安装STM32CubeMX(工程代码生成)

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

3.2 安装openocd(调试和烧写固件)

sudo apt install openocd

3.3 安装gcc-arm-none-eabi(编译和gdb调试)

首先安装gcc-arm-none-eabi的一个依赖:sudo apt install libncurses5

gcc-arm-none-eabi比较特殊,我直接apt install安装的版本是2019第四季度编译的版本,安装完后的可执行文件中没有找到arm-none-eabi-gdb。当然你可以先试一下用apt安装,不行就卸载掉。

直接从官网下载最新版本即可:https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads

(现在北京时间2021/04/12)我这里下载的是:gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2

解压后根据:解压目录/share/doc/gcc-arm-none-eabi/readme.txt中的描述进行安装。这里简单讲一下

  1. sudo -s
  2. 我安装到/opt/目录下,cd /opt/ && tar xjf gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2
  3. 配置环境变量:编辑/etc/profile,最后加一行 export PATH=$PATH:/opt/gcc-arm-none-eabi-10-2020-q4-major/bin
  4. 重启,或者,source /etc/profile使得在当前bash有效

3.4 安装vscode

直接微软vscode官网下载.deb包安装即可

需要安装vscode如下扩展:

  1. C/C++
  2. C++ Intellisense
  3. Cortex-Debug

四、工程创建

  1. 首先打开STM32CubeMX
  2. 两种选择:通过芯片型号创建项目,通关开发板型号创建项目(当然必须是ST官方的开发板)

我这里直接是官方开发板,所以,选第二个。第一个的话,还要自己根据原理图配置引脚。

  1. 然后配置好时钟,中断等等
  2. 项目管理,工具链/IDE,选择makefile。不依赖IDE比较轻量。其他项目信息填好即可。
  3. 点击生成代码

五、VSCODE工程配置

5.1 VSCODE打开工程

5.2 打开terminal(Ctrl+~),直接make编译生成elf

5.3 配置Debug,创建launch.json

模版如下

其中的"build/usb.elf",要改为你对应的elf文件。interface改为你用的stlink版本,target是你用的芯片。

{
    // 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": [
        {
            "name": "Cortex Debug",
            "cwd": "${workspaceRoot}",
            "executable": "build/usb.elf",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "openocd",
            "configFiles": [
                "interface/stlink-v2-1.cfg",
                "target/stm32f4x.cfg"
            ],
            "runToMain": true
        }
    ]
}

我自己填写的时候之前configFiles填写的绝对路径,导致一直未能成功启动调试。

六、烧写固件

直接将下面的内容加到makefile中,然后执行make flash即可。

固件名称要更改为你设置的.(usb.bin改为你的固件名称)

#######################################
# flash .bin to device
#######################################
flash:
	openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg -c init \
		-c "reset halt" \
		-c "flash write_image erase build/usb.bin 0x08000000 bin" \
		-c "verify_image build/usb.bin 0x08000000 bin" \
		-c "reset run" \
		-c shutdown 
.PHONY: flash