PlatformIO Community

Debugging on WSL2 + VSCode + JLink

For my project I found a way how to use PlatformIO inside WSL2. The main reason to do so is compilation time: 102 sec for Windows OS (Defender Antivirus Real-time protection is off) vs 35 sec for WSL2 (the same desktop, the same Windows OS).

The only issue was to get debugging working as usb devices support currently is not present in WSL2. Thanks Segger they have ability to use JLink via network. The whole developing process (editing, uploading firmware, debugging) works as expected and I just would like to have platformio.ini neat.

I ended up with this configuration in [env] section:

debug_server = $PLATFORMIO_HOME_DIR/packages/tool-jlink/JLinkGDBServerCLExe
               -singlerun
               -if
               SWD
               -port
               2331
               -device
               STM32F302RE
               -nogui
               -select
               ip=172.20.128.1

Is it possible to set debug_server via advanced scripting? Because ini-file is not the right place for the temporary value of ip address. For the uploader it can be done with quite simple post-script:

Import('env')
from os import environ

wslip = environ.get('WSLIP')
if wslip is not None:
    flags = env['UPLOADERFLAGS']
    flags.append('-nogui')
    flags.append('1')
    flags.append('-ip')
    flags.append(wslip)
    env.Replace(UPLOADERFLAGS=flags)

WSLIP is the environment variable. I set it in .bashrc file as:
export WSLIP=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}')

Links related to this topic:
VSCode - Developing in WSL
Accessing a J-Link remotely

Additional links to the post:
WSL with Desktop Environment via RDP (optional)
Adding USB support to WSL 2 (I was able to attach USB-JLink to WSL, but it didn’t work. FTDI USB Serial Converter worked fine.)

Per docs you should be able to use system environment variables there with the syntax ${sysenv.WSLIP} here e.g.

1 Like

Thanks! I overlooked that system environment variables can be used as a section. Now I am almost happy. The ideal solution for me would be if I could set debug_server on the fly as UPLOADERFLAGS. In that case it doesn’t matter whether I use JLink via USB or WSL network, in both cases I don’t have to change platformio.ini. But it looks like I don’t have access to debug_server from pre/post scripts.