ubuntu下vscode+STM32CubeMX+openocd+stlinkv2搭建STM32开发调试下载环境

1、换源

清华源

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

复制上边内容进行修改

sudo vim /etc/apt/sources.list

换源后,更新当前的源

sudo apt update
sudo apt upgrade

2、下载相关软件

在这里插入图片描述
下载deb版本

sudo dpki -i 下载的VScode deb文件

安装插件

在这里插入图片描述

3、STM32CubeMX安装

STM32CubeMX
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
给他增加图标到菜单

vim /usr/share/applications/stm32CubeMX.desktop
[Desktop Entry]
Type=Application
Name=STM32CubeMX
Comment=STM32 Development Environment
Encoding=UTF-8
StartupNotify=true
Terminal=false
Categories=Development;java;c;c++;
icon= ##########图标的路径,自己找一个就行
Exec= #########可执行文件的路径

3.1 新建一个点灯的小程序

一定一定记得这个,要不后期调试和下载报错
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4 arm-none-eabi交叉编译工具安装

arm-none-eabi
在这里插入图片描述
解压后将bin文件夹加入PATH

vim ~/.bashrc
加入PATH=你的路径:$PATH

在这里插入图片描述

source ~/.bashrc

验证是否成功

arm-none-eabi-gcc --version

在这里插入图片描述

arm-none-eabi-gdb --version

在这里插入图片描述
如果这里你的GDB有问题,请一定要解决。我碰到了缺少 error while loading shared libraries: libncurses.so.5,解决方法参考

5、安装gdbserver

wget https://ftp.gnu.org/gnu/gdb/gdb-14.2.tar.xz

tar -xvf gdb-14.2.tar.xz

./configure

make

sudo make install

6、安装openocd

sudo apt-get install build-essential pkg-config autoconf automake libtool libusb-dev libusb-1.0-0-dev libhidapi-dev

sudo apt-get install libtool libsysfs-dev

git clone git://git.code.sf.net/p/openocd/code openocd

cd openocd

./bootstrap

./configure 

make

sudo make install

如果这里bootstrap阶段,告诉你子模块出错,碰到EOF提前结束,说明子模块下载失败,可以按照如下修改

vim .gitmodules
修改其子模块地址,可以去gitee上找镜像拉取,我这里jimtcl报错

在这里插入图片描述

6.1验证是否成功

openocd --version

在这里插入图片描述

7、STLink安装

依赖安装

sudo apt-get install libusb-1.0

sudo apt-get install cmake

sudo apt-get install libgtk-3-dev
git clone https://github.com/stlink-org/stlink

cd stlink

cmake .

make

cd bin

sudo cp st-* /usr/local/bin

cd ../lib

sudo cp *.so* /lib32

sudo cp config/udev/rules.d/49-stlinkv* /etc/udev/rules.d/

验证是否成功

lsusb

在这里插入图片描述

7.1

安装界面化烧录工具
ubuntu deb下载地址

sudo dpkg -i 你的下载文件.deb

在这里插入图片描述

8、进入Vscode进行点灯

在main.c的while(1)中加入

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, 1);

点击vscode的调试,添加配置,选择
在这里插入图片描述

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "cwd": "${workspaceRoot}",
            "executable": "./build/你的项目生成的文件.elf",
            "name": "Debug with OpenOCD",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "openocd",
            "configFiles": [
                "/usr/share/openocd/scripts/interface/stlink-v2-1.cfg",
                "/usr/share/openocd/scripts/target/stm32f1x.cfg"
            ],
            "searchDir": [],
            "runToEntryPoint": "main",
            "showDevDebugOutput": "none"
        }
    ]
}

需要修改的是executable,变成你的elf文件
configFiles,变成你的stlink-v2-1.cfg(STLink驱动),stm32f1x.cfg(和你开发板型号有关)文件的地址,具体的可以查看

/usr/share/openocd/scripts/interface/stlink-v2-1.cfg
/usr/share/openocd/scripts/target/stm32f1x.cfg

进入终端,输入

make

即可编译。当然你也可以配置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",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        
    ]
}

9、下载烧录

  1. 命令行烧录
st-flash write 你生成的hex文件 0x8000000
  1. UI界面烧录
    在这里插入图片描述

  2. 配置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",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "download",
            "type": "shell",
            "command": "st-flash write 你生成的hex文件 0x8000000"
        }
    ]
}

10、调试

在这里插入图片描述
如果你卡在 IMPORTANT: Set “showDevDebugOutput”: “raw” in “launch.json” to see verbose GDB transactions here. Very helpful to debug issues or report problems
的话,很大可能是你的arm-none-eabi-gdb出错了,可以尝试看看

arm-none-eabi-gdb -v

一切路径不能有中文,有中文很容易报错.OK,差不多就这样,折腾了两个晚上,记录一下,坑比较多,很少有写Ubuntu在STM32开发的

  • 14
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在VSCode中配置STM32开发环境OpenOCD,请按照以下步骤操作: 1. 安装VSCode:前往VSCode的官方网站(https://code.visualstudio.com/)下载并安装VSCode。 2. 安装PlatformIO插件:在VSCode中,点击左侧的扩展图标,搜索并安装PlatformIO IDE插件。 3. 创建新的PlatformIO项目:点击左侧的Activity Bar中的PlatformIO图标,选择"New Project"创建一个新项目。 4. 选择STM32开发板:在PlatformIO的"New Project"向导中,选择你的STM32开发板型号,并选择你想要的框架和开发平台。 5. 配置OpenOCD:在项目根目录下创建一个名为`.vscode`的文件夹,并在其中创建一个名为`launch.json`的文件。在`launch.json`中添加以下配置: ```json { "version": "0.2.0", "configurations": [ { "type": "openocd", "request": "launch", "name": "Debug STM32", "executable": "openocd", "configFiles": [ "${workspaceFolder}/.pio/build/<YOUR_BUILD_ENV>/openocd.cfg" ] } ] } ``` 确保将`<YOUR_BUILD_ENV>`替换为你使用的编译环境。 6. 配置OpenOCD配置文件:在项目根目录下,找到`.pio/build/<YOUR_BUILD_ENV>/`文件夹(这里的`<YOUR_BUILD_ENV>`是你使用的编译环境),创建一个名为`openocd.cfg`的文件,并在其中添加以下配置: ```cfg source [find interface/<YOUR_DEBUG_INTERFACE>.cfg] source [find target/<YOUR_TARGET_DEVICE>.cfg] ``` 确保将`<YOUR_DEBUG_INTERFACE>`替换为你使用的调试接口,例如`stlink-v2.cfg`或`jlink.cfg`。将`<YOUR_TARGET_DEVICE>`替换为你的目标设备,例如`stm32f4x.cfg`或`stm32f1x.cfg`。 7. 连接STM32开发板和调试器:将STM32开发板连接到计算机,并将调试器连接到开发板上。 8. 开始调试:在VSCode中,点击左侧的调试图标,选择你刚刚创建的调试配置,并点击"Start Debugging"开始调试。 以上步骤应该能够帮助你在VSCode中配置STM32开发环境OpenOCD。如果你遇到任何问题,请参考PlatformIO的文档或OpenOCD的文档,或在相关论坛上寻求帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值