ubuntu PX4 vscode stlink debug设置

硬件

stlink
holybro debug板
pixhawk4

请添加图片描述

安装openocd

官方文档,但是第一步安装建议从源码安装,bug少很多
github链接

编译安装,参考

  ./bootstrap (when building from the git repository)
  ./configure [options]
  make
  sudo make install

安装后在usr/local/bin下面有一个openocd

px4qgc@ubuntu:~$ which openocd
/usr/local/bin/openocd

另外要注意gcc-arm路径

px4qgc@ubuntu:~$ which arm-none-eabi-gdb
/opt/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-gdb

然后进行一点测试,看看环境对不对再往下走
比如我用fmuv5的pixhawk4:

openocd -f interface/stlink.cfg -f target/stm32f7x.cfg
arm-none-eabi-gdb build/px4_fmu-v5_default/px4_fmu-v5_default.elf -ex "target extended-remote :3333"

在这里插入图片描述
可能的报错:

arm-none-eabi-gdb: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

安装对应的库即可

sudo apt-get update
sudo apt-get install libncurses5

usb设备权限问题

px4qgc@ubuntu:~$ openocd -f interface/stlink.cfg -f target/stm32f7x.cfg
Open On-Chip Debugger 0.11.0-dirty (2023-10-28-03:57)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 2000 kHz
Error: libusb_open() failed with LIBUSB_ERROR_ACCESS

为stlink添加usb规则

sudo gedit /etc/udev/rules.d/99-openocd.rules
# For ST-Link
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3744", MODE:="666"
# For ST-Link V2
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", MODE:="666"
# For ST-Link V2-1 (STM32 Nucleo boards)
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374b", MODE:="666"
# For ST-Link V3
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3753", MODE:="666"
# For ST-Link V3 MINIE
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3754", MODE:="666"

vscode配置

从github上面clone下来代码有一个.vscode文件夹,这个非常重要,给定了vscode的很多配置
按照官方文档安装vscode插件,注意,如果用的arm-gcc版本是2020-q2,gdb版本就是8,不能用最新的cortex-bug,我试了1.4.3可以

在这里插入图片描述

task.json里面加上

        {
            "label": "Build and Download",
            "type": "shell",
            "command":"openocd",
            "args": [
            "-f",
            "interface/stlink.cfg",
            "-f",
            "target/stm32f7x.cfg",
            "-c",
            "program ./build/px4_fmu-v5_default/px4_fmu-v5_default.bin 0x8008000  verify reset exit "
            ],
            "problemMatcher": []
        },

launch.json加上:

        {
            "name": "FMUv5 Debug ST-Link",
            "type": "cortex-debug",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "executable": "./build/px4_fmu-v5_default/px4_fmu-v5_default.elf",
            //"serverpath": "${env:JLINK_SERVER}",
            "servertype": "openocd",
            "device": "STM32F765II",
            "interface": "swd",
            "configFiles": [
            "interface/stlink.cfg",
            "target/stm32f7x.cfg"
             ],
            "serialNumber": "", //If you have more than one J-Link probe, add the serial number here.
            "svdFile": "STM32F7x5.svd",
           "preLaunchTask":"Build and Download"
        },

给px4_simple_app.c加一个断点,点击调试,openocd会负责用stlink刷入最新固件,并启动调试
在这里插入图片描述

效果如下图:
在这里插入图片描述
在这里插入图片描述
默认的那个st-util从来没在fmuv5上面好使过,会进入下面这个图的莫名其妙的地方,不用了,v6c倒是可以

在这里插入图片描述
如果遇到没有要编译的对象,比如6c,在这自己加就行
在这里插入图片描述

pixhawk6c

默认的就可以,但是感觉费劲死了,写一下openocd的配置
烧写命令:

openocd -f interface/stlink.cfg -f target/stm32h7x_dual_bank.cfg -c "program ./build/px4_fmu-v6c_default/px4_fmu-v6c_default.elf verify reset exit "
openocd -f interface/stlink.cfg -f target/stm32h7x_dual_bank.cfg
arm-none-eabi-gdb build/px4_fmu-v6c_default/px4_fmu-v6c_default.elf -ex "target extended-remote :3333"

launch.json

        {
            "name": "FMUv6c Debug ST-Link",
            "type": "cortex-debug",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "executable": "${command:cmake.launchTargetPath}",
            //"serverpath": "${env:JLINK_SERVER}",
            "servertype": "openocd",
            "device": "STM32H743VI",
            "interface": "swd",
            "configFiles": [
            "interface/stlink.cfg",
            "target/stm32h7x_dual_bank.cfg"
             ],
            "serialNumber": "", //If you have more than one J-Link probe, add the serial number here.
            "svdFile": "STM32H743.svd",
           "preLaunchTask":"Build and Download"
        },

launch.json 如果不想每次都重新编译,就把executable改了

        {
            "name": "FMUv6c Debug ST-Link",
            "type": "cortex-debug",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "executable": "./build/px4_fmu-v6c_default/px4_fmu-v6c_default.elf",
            //"serverpath": "${env:JLINK_SERVER}",
            "servertype": "openocd",
            "device": "STM32H743VI",
            "interface": "swd",
            "configFiles": [
            "interface/stlink.cfg",
            "target/stm32h7x_dual_bank.cfg"
             ],
            "serialNumber": "", //If you have more than one J-Link probe, add the serial number here.
            "svdFile": "STM32H743.svd",
           "preLaunchTask":"Build and Download"
        },

tasks.json

        {
            "label": "echo",
            "type": "shell",
            "command": "echo ${env:USERNAME}"
        },
        {
            // "dependsOn":"Build",
            "label": "Build and Download",
            "type": "shell",
            "command": "openocd",
            "args": [
            "-f",
            "interface/stlink.cfg",
            "-f",
            "target/stm32h7x_dual_bank.cfg",
            "-c",
            "program ./build/px4_fmu-v6c_default/px4_fmu-v6c_default.elf verify reset exit "
            ],
            "problemMatcher": []
        },

原版st-util配置文件:

        {
            "name": "stlink (px4_fmu-v6c)",
            "gdbPath": "/opt/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-gdb",
            "device": "STM32H743VI",
            "svdFile": "STM32H743.svd",
            "executable": "./build/px4_fmu-v6c_default/px4_fmu-v6c_default.elf",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "stutil",
            "cwd": "${workspaceFolder}",
            "internalConsoleOptions": "openOnSessionStart",
            "preLaunchCommands": [
                "source ${workspaceFolder}/platforms/nuttx/Debug/PX4",
                "source ${workspaceFolder}/platforms/nuttx/Debug/NuttX",
                "source ${workspaceFolder}/platforms/nuttx/Debug/ARMv7M",
                "set mem inaccessible-by-default off",
                "set print pretty",
            ]
        },

补充一点pixhawk4的debug

突然发现后面临近停产的pixhawk4,openocd刷不进去flash

px4qgc@ubuntu:~/1.14.0/PX4-Autopilot$ openocd -f interface/stlink.cfg -f target/stm32f7x.cfg -c "program ./build/px4_fmu-v5_default/px4_fmu-v5_default.elf 0x8000 verify reset exit "
Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64 (2023-11-30-21:36)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : clock speed 2000 kHz
Info : STLINK V3J8M3 (API v3) VID:PID 0483:3754
Info : Target voltage: 3.276368
Info : [stm32f7x.cpu] Cortex-M7 r1p0 processor detected
Info : [stm32f7x.cpu] target has 8 breakpoints, 4 watchpoints
Info : starting gdb server for stm32f7x.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : Unable to match requested speed 2000 kHz, using 1000 kHz
Info : Unable to match requested speed 2000 kHz, using 1000 kHz
[stm32f7x.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08001468 msp: 0x20020000
** Programming Started **
Info : device id = 0x10016451
Info : flash size = 2048 KiB
Info : Single Bank 2048 kiB STM32F76x/77x found
Info : flash size = 1024 bytes
Warn : no flash bank found for address 0x08200000
** Programming Finished **
** Verify Started **
Error: timed out while waiting for target halted
Error: error executing cortex_m crc algorithm
** Verify Failed **
shutdown command invoked

故修改launch.json,

        {
            "name": "FMUv5 Debug ST-Link",
            "type": "cortex-debug",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "executable": "/home/px4qgc/1.14.0/PX4-Autopilot/build/px4_fmu-v5_default/px4_fmu-v5_default.elf",
            "servertype": "openocd",
            "device": "STM32F765II",
            "interface": "swd",
            "configFiles": [
            "interface/stlink.cfg",
            "target/stm32f7x.cfg"
             ],
            "serialNumber": "", //If you have more than one J-Link probe, add the serial number here.
            "svdFile": "STM32F7x5.svd",
           "preLaunchTask":"flash"
        },

task.json:用st-flash可以正常刷写

        {
            "label": "flash",
            "type": "shell",
            "command": "st-flash --hot-plug write ./build/px4_fmu-v5_default/px4_fmu-v5_default.bin 0x08008000",
        },

如果觉得刷写费时间,可以把launch.json里面这行注释掉

           "preLaunchTask":"flash"
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是在Ubuntu中使用VS Code实现LED灯的亮灭的步骤: 1. 安装必要的软件包: 在Ubuntu中打开终端,输入以下命令,安装必要的软件包: ``` sudo apt-get update sudo apt-get install build-essential gdb-multiarch gcc-arm-none-eabi openocd ``` 2. 创建工程: 在终端中进入您的工作目录,使用以下命令创建一个名为"led_blink"的文件夹: ``` mkdir led_blink cd led_blink ``` 在"led_blink"文件夹中创建一个名为"led_blink.c"的文件,并将以下代码复制到该文件中: ``` #include "stm32f4xx.h" int main(void) { // Enable GPIOA clock RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // Configure PA5 as output GPIOA->MODER |= GPIO_MODER_MODE5_0; while (1) { // Turn on LED GPIOA->BSRR |= GPIO_BSRR_BS5; // Wait for a while for (int i = 0; i < 1000000; i++); // Turn off LED GPIOA->BSRR |= GPIO_BSRR_BR5; // Wait for a while for (int i = 0; i < 1000000; i++); } } ``` 该代码将使用STM32F4xx的GPIOA端口控制PA5引脚,使LED灯在开和关之间循环。 3. 配置VS Code: 在Ubuntu中安装VS Code,打开VS Code并安装"Cortex-Debug"插件。 4. 配置调试器: 在"led_blink"文件夹中创建一个名为".vscode/launch.json"的文件,并将以下代码复制到该文件中: ``` { "version": "0.2.0", "configurations": [ { "type": "cortex-debug", "request": "launch", "name": "Debug STM32F4 Discovery Board", "cwd": "${workspaceRoot}", "executable": "${workspaceRoot}/led_blink.elf", "servertype": "openocd", "device": "STM32F407VG", "configFiles": [ "./openocd.cfg" ] } ] } ``` 该配置文件将使用Cortex-Debug插件来启动OpenOCD调试器并连接到STM32F4 Discovery开发板。 5. 配置OpenOCD: 在"led_blink"文件夹中创建一个名为"openocd.cfg"的文件,并将以下代码复制到该文件中: ``` source [find interface/stlink-v2.cfg] source [find target/stm32f4x.cfg] reset_config srst_only ``` 该文件将配置OpenOCD调试器连接到STM32F4 Discovery开发板。 6. 构建和调试项目: 在终端中输入以下命令,构建项目: ``` arm-none-eabi-gcc -g -O0 -mcpu=cortex-m4 -mthumb -T stm32f407vg.ld led_blink.c -o led_blink.elf ``` 该命令将使用arm-none-eabi-gcc交叉编译器编译"led_blink.c"文件,并生成一个名为"led_blink.elf"的可执行文件。 在VS Code中,点击"Run"菜单,选择"Start Debugging",该命令将启动OpenOCD调试器并连接到STM32F4 Discovery开发板,然后开始调试"led_blink.elf"文件。 现在,您应该可以看到STM32F4 Discovery开发板上的LED灯在开和关之间循环了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值