Fast Debug and Upload by using JLINK in platformio

Make sure the JLINK driver is SEGGER’s and use the latest version.

This is my settings.
platformio.ini:

[env:jlink_debug_and_upload]
platform = gd32v
board = sipeed-longan-nano
framework = gd32vf103-sdk
extra_scripts = extra_script.py
upload_protocol = custom
debug_tool = custom
debug_server =
  C:\Program Files (x86)\SEGGER\JLink\JLinkGDBServerCL.exe
  -singlerun
  -if
  JTAG
  -select
  USB
  -port
  2331
  -device
  GD32VF103CBT6
debug_extra_cmds = set $pc=0x08000000

Make this file: extra_scripts.py in same directory as platformio.ini:

from os import makedirs
from os.path import isdir, join
Import('env')

def _jlink_cmd_script(env, source):
    build_dir = env.subst("$BUILD_DIR")
    if not isdir(build_dir):
        makedirs(build_dir)
    script_path = join(build_dir, "upload.jlink")
    commands = ["h", "loadfile %s,0" % source , "r", "q"]
    with open(script_path, "w") as fp:
        fp.write("\n".join(commands))
    return script_path

env.Replace(
    __jlink_cmd_script=_jlink_cmd_script,
    UPLOADER="C:/Program Files (x86)/SEGGER/JLink/JLink.exe",
    UPLOADERFLAGS=[
        "-device", "GD32VF103CBT6",
        "-speed", "4000",
        "-if", "jtag",
        "-autoconnect", "1"
    ],
    UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS -CommanderScript ${__jlink_cmd_script(__env__, SOURCE) } '
)

works very well,
much faster than debug_tool = jlink

It’s not working unfortunatelly :frowning:

There is one parameter missing in the commandline for jlink.exe. Should look like this:

env.Replace(
__jlink_cmd_script=_jlink_cmd_script,
UPLOADER="C:\Program Files (x86)\SEGGER\JLink\JLink.exe",
UPLOADERFLAGS=[
    "-device", "GD32VF103CBT6",
    "-speed", "4000",
    "-if", "jtag",
    "-autoconnect", "1",
    "-JTAGConf", "-1,-1"
],
UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS -CommanderScript ${__jlink_cmd_script(__env__, SOURCE) } '
)

Tested with Jlink 6.80d and Jlink-EDU and - its really fast!

And there is another advantage in this scenario - the original Jlink driver does not need to be replaced by the Libusb/Winusb driver.

1 Like

After working some hours with Longan Nano I found some Issues with JLink.

Issue 1. Reset via Software / Debug register doesn’t work
Issue 2. depending on the JLink version uploading from .bin files has a different behavior.

Solution for Issue 1:
Connect Jlink Reset to Longan Nano RST-Pin.

J-Link 20 pin Interface Longan NANO
Pin 1 (VTref) 3V3
Pin 4 (GND) GND
Pin 5 (TDI) JTDI
Pin 7 (TMS) JTMS
Pin 9 (TCK) JTCK
Pin 13 (TDO) JTDO
Pin 15 (Reset) RST

Solution for Issue 1 and 2:
expand script with prog start address, change Reset to hardware reset.

from os import makedirs
from os.path import isdir, join
Import('env')

def _jlink_cmd_script(env):
build_dir = env.subst("$BUILD_DIR")
prog_name = env.subst("$PROGNAME")
if not isdir(build_dir):
    makedirs(build_dir)
script_path = join(build_dir, "upload.jlink")
prog_file = join(build_dir, prog_name)
commands = ["h", "loadfile %s" % prog_file + ".bin, 0x08000000" , "RSetType 1", "r", "q"]
with open(script_path, "w") as fp:
    fp.write("\n".join(commands))
return script_path

env.Replace(
__jlink_cmd_script=_jlink_cmd_script,
UPLOADER="C:\Program Files (x86)\SEGGER\JLink\JLink.exe",
UPLOADERFLAGS=[
    "-device", "GD32VF103CBT6",
    "-speed", "4000",
    "-if", "jtag",
    "-autoconnect", "1",
    "-JTAGConf", "-1,-1"
],
UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS -CommanderScript ${__jlink_cmd_script(__env__) } '
)

The following changes in the script are done:

  • adding RSetType 1 - this changes Reset to Hardware reset

  • changing path generation for upload file - this gives more flexibility, e.g. use .hex instead of .bin

  • add prog start address.

an alternative command generation for uploading .hex files looks like:

commands = ["h", "loadfile %s" % prog_file + ".hex" , "RSetType 1", "r", "q"]

But I found an bug (or feature :slight_smile:) in Platform IO - when you run only upload after a clean,
the .hex file will not be generated, only if you start build before.

Tested with Jlink 6.80d and Jlink-EDU 10.10

Hope this helps :sunglasses: