Uploading and debugging is working fine with ST-Link/V2 using standard PIO configuration:
upload_protocol = stlink
debug_tool = stlink
I would like use the Segger J-Link/V8 I have so I could hopefully use RTT (Getting printf Output from Target to Debugger - SEGGER Blog). However, I am not able to get upload/debug working with standard config:
; J-Link SWD interface
upload_protocol = jlink
debug_tool = jlink
I read everything related with J-Link on both this Community forum and platformio/platformio-core GitHub Issues:
Issues · platformio/platformio-core · GitHub
Issues · platformio/platformio-core · GitHub
I know for fact that others have it working fine with standard config. These are the findings I am suspecting being related to the problem:
- J-Link/V8 does not support USB CDC serial port (virtual COM port).
- The J-Link driver appears fine in my device drivers-Universal Serial Bus controllers.
- Running latest version of VSCode 1.29.1 w/ PlatformIO: Home 1.0.6, Core 3.6.2a2
- Win7 x64
- ‘ home_dir/packages/tool-jlink’ mentioned in J-LINK — PlatformIO latest documentation
is missing from my HD but ‘tool-stlink’ is there.
This is the error I get when uploading:
Configuring upload protocol...
AVAILABLE: blackmagic, dfu, jlink, mbed, stlink
CURRENT: upload_protocol = jlink
Uploading .pioenvs\bluepill_f103c8\firmware.elf
GNU MCU Eclipse 64-bits Open On-Chip Debugger 0.10.0+dev-00392-gbe9ef0b0 (2018-01-12-15:03)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
swd
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
cortex_m reset_config sysresetreq
Warn : Failed to open device: LIBUSB_ERROR_NOT_SUPPORTED.
Error: No J-Link device found.
Error: No Valid JTAG Interface Configured.
*** [upload] Error -1
And this is the Error I get when I choose ‘Start Debugging F5’:
I managed to get the upload working by adding the following to platformio.ini:
upload_protocol = jlink
extra_scripts = extra_script.py
However, I have to ‘Build’ first then ‘Upload’ next. The ‘firmware.bin’ doesn’t get generated when I click ‘Upload’ alone after ‘Clean’.
I don’t like the ‘extra_script’ method because it shows ‘SUCCESS’ at the end even though it couldn’t find/open ‘firmware.bin’.
Note: ‘JLink.exe’ used in the ‘extra_script‘ below doesn’t read ‘firmware.elf’; that’s why the script was modified not to use ‘SOURCE’.
Here is my ‘extra_script’:
# https://docs.platformio.org/en/latest/plus/debug-tools/custom.html?highlight=j-link#j-link-as-debugger-and-uploader
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", "loadbin %s, 0x0" % source, "r", "q"]
commands = ["h", "loadbin %s/firmware.bin, 0x0" % build_dir, "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_V640/JLink.exe",
UPLOADERFLAGS=[
"-device", "STM32F103C8",
"-speed", "4000",
"-if", "swd",
"-autoconnect", "1"
],
UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS -CommanderScript ${__jlink_cmd_script(__env__, SOURCE)}'
)
I also managed to get debug working by adding the following to platformio.ini:
debug_tool = custom
debug_server = C:\Program Files (x86)\SEGGER\JLink_V640\JLinkGDBServerCL.exe
-singlerun
-if
SWD
-select
USB
-port
2331
-device
STM32F103C8
However, it’s very slow to start the debugger (> 30 secs after compiling) compared to ST-Link (< 10 secs) and it’s almost impractical.
Correction: I exited VSCode and repeated the test and now they’re both fast enough (< 5 secs). So apparently memory is leaking seriously and tasks are probably left behind and new ones are spawned. What prompted me to exit is that the OS warned about running out of memory.
Bottom line, what do I have to do in order to ‘use official JLink GDB default without these extra scripts and hooks’ ?