ubuntu PX4 vscode stlink debug settings

Hardware

stlink
holybro debug board
pixhawk4

Please add image description

Install openocd

Official documentation, but the first step of installation is recommended to install from the source code, which has fewer bugs.
github link

Compile and install, refer to

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

After installation, there is an openocd under usr/local/bin

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

Also pay attention to the gcc-arm path

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

Then do some testing to see if the environment is right before proceeding.
For example, I use pixhawk4 of fmuv5:

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"


Possible errors:

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

Just install the corresponding library

sudo apt-get update
sudo apt-get install libncurses5

usb device permissions issue

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

Add usb rules for stlink

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 configuration

The code cloned from github has a .vscode folder. This is very important because it provides many configurations of vscode.
Install the vscode plug-in according to the official documentation. Note that if the arm-gcc version used is 2020-q2, the gdb version is 8. The latest cortex-bug cannot be used. I tried 1.4.3 and it works.

Add to 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 plus:

 {<!-- -->
            "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"
        },

Add a breakpoint to px4_simple_app.c and click debug. Openocd will be responsible for flashing the latest firmware using stlink and starting debugging.

The effect is as follows:


The default st-util has never been used on fmuv5. It will enter the inexplicable place in the picture below. No need, v6c will work.


If you encounter an object that does not need to be compiled, such as 6c, just add it yourself here.

pixhawk6c

The default one is fine, but it feels very laborious. Write the configuration of openocd.
Burning command:

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 If you don’t want to recompile every time, change the 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": []
        },

Original st-util configuration file:

 {<!-- -->
            "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",
            ]
        },

Add some debugging of pixhawk4

Suddenly I discovered that pixhawk4 was about to be discontinued, and openocd could not access the 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

Therefore, modify 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: can be flashed normally with st-flash

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

If you think flashing is time-consuming, you can comment out this line in launch.json

 "preLaunchTask":"flash"