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.

Hi dtokarev,

Thanks for your post.

I’m also trying to use PlatformIO in WSL2 with JLink Remote Server. To flash the code from WSL2 to the board connected to the host machine, I followed your configuration for debug_server.

What I did are:

At host:

  1. Connect the board with Jlink to laptop
  2. Start JLink Remote Server

In the WSL2:

  1. Edit the platformio.ini as below
  2. Press upload in PlatformIO
  3. A pop-up window shows up to ask for some credential stuff for JLink Server connection.
  4. Console shows the target is connected, also the Remote Server at the host
  5. Nothing happens afterward. The Build stucks, I can neither debug nor upload the code anymore.

Could you pls share what are the right steps to make this work?

This is how I configure the [env]:

[env:debug_jlink]
platform = nordicnrf52
board = nano33ble
framework = arduino
extra_scripts = extra_script.py
upload_protocol = jlink
debug_tool = jlink
debug_port = :2331
debug_server = /opt/SEGGER/JLink_V694d/JLinkGDBServerCLExe
               -singlerun
               -if
               SWD
               -port
               2331
               -device  
               nRF52840_xxAA
               -nogui
               -select
               ip=tunnel:haoyu_jlink:rhyrhy123:jlink-europe.segger.com

Thanks in advance!

Hi, here is my [env] with configuration related to uploading/debugging:

upload_protocol           = jlink
debug_tool                = jlink
debug_server              = $PLATFORMIO_HOME_DIR/packages/tool-jlink/JLinkGDBServerCLExe
                            -singlerun
                            -if
                            SWD
                            -port
                            2331
                            -nogui
                            -select
                            ip=${sysenv.WSLIP}
extra_scripts             = post:tools/wsl/post_wsl_support.py

And post_wsl_support.py:

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 with JLink server ip address. Hope this helps.

1 Like