Use VSCode for Hardware Debugging in Real Time [Solved]

I wanted an easy way to debug the code and understand how everything works in ArduCopter. This guide will tell you how to setup a debugging environment in Windows and connect directly to your Pixhawk4 although others will be similar.

1. This link describes basic hardware debugging, but fails to mention Enabling the Debug Pins
	a. https://ardupilot.org/dev/docs/debugging-with-gdb-on-stm32.html
	b. After first loop, the debug pins are normally turned into inputs to avoid any accidental 
                triggers via ESD.
	c. This disables hardware debugging.
2. Mission Planner
	a. Enable the Debug Pins
		i. BRD_OPTIONS must be set to Enable Debug Pins
3. Connect STLink to FMU Debug port (side of Pixhawk4)
	a. You will need to make a 6pin connector to STLink pins:
		i. PWR, GND, SWDIO and SWCLK
4. Install VSCode and Setup for WSL2
	a. There are many tutorials on Youtube for this.
	b. It allows you to run an instance of Ubuntu from your Windows PC
5. Plug STLink into USB port
	a. You will need to connect device from Windows to WSL Ubuntu
		i. https://devblogs.microsoft.com/commandline/connecting-usb-devices-to-wsl/
	b. Windows CMD Prompt - Run As Admin
		i. usbipd wsl list
			1) show all usb devices in windows
			2) find busid for STLink
		ii. usbipd wsl attach --busid 3-7
			i. shares STLink with WSL
		iii. usbipd wsl detach --busid 3-7
			i. allows windows to have control of STLink
	c. Ubuntu WSL
		i. start Ubuntu terminal window
		ii. lsusb - show all ports
		iii. Find BusID/DeviceID for STLink
		iv. ls -l /dev/bus/usb/001/008
			i. command shows permision for STLink Adapter
		v. sudo chmod +666 /dev/bus/usb/001/008 (BusID/DeviceID)
			i. changes permission so that sudo is not required to use STLink
			ii. This is normally handled with a usb plug in rule, but these don't seem to work in WSL
6. VSCode
	a. Compile copter with debug enabled
		i. ./waf configure --board=Pixhawk4 --debug --enable-asserts
		ii. ./waf -j8 copter
		iii. copy file from Ubuntu to Windows
			i. cp /home/mikeh9/ardupilot/build/Pixhawk4/bin/arducopter.apj /mnt/d/dev/S500_v2/ardupilot_bin/arducopter_debug.apj
7. Mission Planner
	a. load custom firmware
		i. arducopter_debug.apj
	b. make sure all the parameters are set as needed
8. STUTIL
	a. This will be used as the debug server (gdb will connect to it to issue commands)
	b. Clone from here and follow build / install instructions:
		i. https://github.com/stlink-org/stlink/blob/develop/doc/compiling.md
9. GDB
	a. This is the actual debugger.
	b. download gcc-arm-none-eabi-6-2017-q2-update-linux.tar.bz2 from here
		i. https://firmware.ardupilot.org/Tools/STM32-tools
	c. create sim links in /usr/bin to the arm-none-eabi-* programs (see pic below)
	d. make sure /usr/bin is in your path
		i. this allows VSCode to find the correct version of gdb
10. VSCode
	a. Install Cortex-Debug extension
	b. Run...Open Configurations (launch.json)
	c. Add configuration for Pixhawk4 CPU, STUTIL and GDB (via Cortex-Debug):
	{
	            "cwd": "/home/mikeh9/ardupilot/build/Pixhawk4/bin/",
	            "executable": "/home/mikeh9/ardupilot/build/Pixhawk4/bin/arducopter",
	            "name": "STUTIL Debug",
	            "request": "attach",
	            "servertype": "stutil",
	            "serverArgs": ["--connect-under-reset"],
	            "type": "cortex-debug",
	            "device": "STM32F765IIK",
	            "showDevDebugOutput": "none"
	        }
	d. Run & Debug...Start the "STUTIL Debug" configuration that was created above.
	e. Place a break point in the code where you want to start debugging
	f. Click on Continue
	g. The program will be halted on your breakpoint...Happy Debugging

1 Like

Does this work with Pixhawk 6c in vs-code running in wsl2(windows 11)?

It does work with few changes to the configuration.

Willing to share said changes? Much appreciated !