18.04 PX4 CUAV x7 pro VScode STlink debug

18.04 PX4 CUAV x7 pro VScode ST_link debug

写这篇文章的目的主要是为了记录下在Ubuntu 18.04环境下利用vscode 软件编译并且debug px4代码的过程
看这篇文章前提你已经有了一个Ubuntu 18.04系统 并且在系统中安装了VScode软件
整个文章主要分以下几个部分

安装 PX4 代码的编译环境

在linux上配置PX4 的整个编译环境其实是一个老生常谈的事情,官方给出了很多种解决方案,主要的难点就是配置好基本的依赖包,坑点就是依赖的版本问题,我编译的PX4固件版本是最新的版本。值得注意的有几点

  • cmake的版本一定要在3以上,cmake的版本太低会出现错误,我的cmake版本是3.10.2
  • 然后要安装一些依赖包
sudo apt-get update -y --quiet
sudo DEBIAN_FRONTEND=noninteractive apt-get -y --quiet --no-install-recommends install \
	astyle \
	build-essential \
	cmake \
	cppcheck \
	file \
	g++ \
	gcc \
	gdb \
	git \
	lcov \
	libxml2-dev \
	libxml2-utils \
	make \
	ninja-build \
	python3 \
	python3-dev \
	python3-pip \
	python3-setuptools \
	python3-wheel \
	rsync \
	shellcheck \
	unzip \
	zip \
	;

-安装python3 默认系统自带,主要是安装python依赖库文件

python3 -m pip install --user -r argcomplete
argparse>=1.2
cerberus
coverage
empy>=3.3
future
jinja2>=2.8
jsonschema
kconfiglib
lxml
matplotlib>=3.0.*
numpy>=1.13
nunavut>=1.1.0
packaging
pandas>=0.21
pkgconfig
psutil
pygments
wheel>=0.31.1
pymavlink
pyros-genmsg
pyserial
pyulog>=0.5.0
pyyaml
requests
setuptools>=39.2.0
six>=1.12.0
toml>=0.9
  • 安装arm-none-eabi-gcc编译器
    • 我使用的版本是 gcc-arm-none-eabi-7-2017-q4-major 其他版本的并不好用,编译会有一些问题
sudo apt-get install python-serial openocd \
    flex bison libncurses5-dev autoconf texinfo \
    libftdi-dev libtool zlib1g-dev -y

# Clean up old GCC
sudo apt-get remove gcc-arm-none-eabi gdb-arm-none-eabi binutils-arm-none-eabi gcc-arm-embedded -y
sudo add-apt-repository --remove ppa:team-gcc-arm-embedded/ppa -y


# GNU Arm Embedded Toolchain: 7-2017-q4-major December 18, 2017
gcc_dir=$HOME/gcc-arm-none-eabi-7-2017-q4-major
echo "Installing GCC to: $gcc_dir"
if [ -d "$gcc_dir" ]
then
    echo " GCC already installed."
else
    pushd .
    cd ~    
    wget https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-rm/7-2017q4/gcc-arm-none-eabi-7-2017-q4-major-linux.tar.bz2
    tar -jxf gcc-arm-none-eabi-7-2017-q4-major-linux.tar.bz2
    exportline="export PATH=$HOME/gcc-arm-none-eabi-7-2017-q4-major/bin:\$PATH"
    if grep -Fxq "$exportline" ~/.profile; then echo " GCC path already set." ; else echo $exportline >> ~/.profile; fi
    . ~/.profile
    popd
fi
  • 至此已经安装好编译环境完全可以编译PX4 的代码
cd PX4-Autopilot
# 开始编译 输入make之后Tab可以查到具体固件的编译命令
# make *******
# 我使用的是雷迅的x7pro硬件
make cuav_x7pro

18.04 PX4 CUAV x7 pro VScode STlink debug_第1张图片

安装stlink的驱动部分

  • stlink驱动的安装是使用github的源码进行安装的
  1. 安装相关库
    sudo apt-get install git make cmake libusb-1.0-0-dev 
    sudo apt-get install gcc build-essential
    
  2. 下载编译
    git clone https://github.com/stlink-org/stlink 
    cd stlink
    cmake .
    make
    
    在配置cmake的时候建议看一下配置输出
  3. 把二进制文件放到系统路径方便调用
    cd bin 
    sudo cp st-* /usr/local/bin 
    cd ../lib 
    sudo cp *.so* /lib32
    sudo cp stlink/config/udev/rules.d/49-stlinkv* /etc/udev/rules.d/
    
  4. 插入stlink输入lsubs可以看到STM的usb设备

配置vscode debug的环境

  • 如果你直接使用vscode打开px4源码的文件夹的话,建议直接下载他所推荐的插件,调试使用的插件是Cortex Builder
  • 这个是我的launch.json,配置好Cmake的目标,编译链选择arm-none-eabi之后就可以愉快的调试了,
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "jlink (cuav_x7pro)",
            "device": "STM32H743XI",
            "svdFile": "",
            "executable": "${command:cmake.launchTargetPath}",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "jlink",
            "interface": "swd",
            //"ipAddress": "server:19020",
            "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",
            ]
        },
        {
            "name": "stlink (cuav_x7pro)",
            "device": "STM32H743XI",
            "svdFile": "",
            "executable": "${command:cmake.launchTargetPath}",
            "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",
            ]
        },
        {
            "name": "blackmagic (cuav_x7pro)",
            "device": "STM32H743XI",
            "svdFile": "",
            "executable": "${command:cmake.launchTargetPath}",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "bmp",
            "BMPGDBSerialPort": "/dev/ttyACM0",
            "interface": "swd",
            "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",
            ]
        }
    ]
}

18.04 PX4 CUAV x7 pro VScode STlink debug_第2张图片

你可能感兴趣的:(vscode,linux,ubuntu)