STLink v3 for STM8 board - device "is no longer connected"

I am attempting to flash an STM8S103F3P6 dev board (platform = ststm8, board = stm8sblue) using an STLink v3 on Windows. I see the STLink listed in the Serial tab in PIO Home. The blink program successfully compiles, but fails during upload step. The output I get during the upload step is:

Configuring upload protocol...
AVAILABLE: serial, stlinkv2
CURRENT: upload_protocol = stlinkv2
Uploading .pio\build\stm8sblue\firmware.hex
Determine FLASH area

libusb: error [init_device] device '\\.\USB#VID_0483&PID_374F&MI_02#6&3470BFC9&0&0002' is no longer connected!
libusb: info [windows_get_device_list] The following device has no driver: '\\.\USB#VID_1235&PID_8210&MI_03#6&1612EDE&0&0003'
libusb: info [windows_get_device_list] libusb will not be able to access it.
libusb: info [windows_get_device_list] The following device has no driver: '\\.\USB#VID_0A5C&PID_21E8#001986003B14'
libusb: info [windows_get_device_list] libusb will not be able to access it.
libusb: info [windows_get_device_list] The following device has no driver: '\\.\USB#VID_1235&PID_8210&MI_03#6&1612EDE&0&0003'
libusb: info [windows_get_device_list] libusb will not be able to access it.
libusb: info [windows_get_device_list] The following device has no driver: '\\.\USB#VID_0A5C&PID_21E8#001986003B14'
libusb: info [windows_get_device_list] libusb will not be able to access it.
Could not open USB device.
*** [upload] Error 4294967295

The other two USB devices are an audio interface and bluetooth adapter. I installed the ST USB drivers before connecting the STLink. I originally thought it was an issue with the board being write-protected, but I used ST Visual Programmer to unlock the board (confirmed by using STVP to successfully flash the .hex file built by PIO) and this didn’t change the output from PIO trying to upload.

Well,

>C:\Users\Max\.platformio\packages\tool-stm8flash\stm8flash.exe

Usage: C:\Users\Max\.platformio\packages\tool-stm8flash\stm8flash.exe [-c programmer] [-S serialno] [-p partno] [-s memtype] [-b bytes] [-r|-w|-v] <filename>
Options:
        -?             Display this help
        -c programmer  Specify programmer used (stlink, stlinkv2, stlinkv21 or stlinkv3)
        -S serialno    Specify programmer's serial number. If not given and more than one programmer is available, they'll be listed.
        -d port        Specify the serial device for espstlink (default: /dev/ttyUSB0)
        -p partno      Specify STM8 device
        -l             List supported STM8 devices
        -s memtype     Specify memory type (flash, eeprom, ram, opt or explicit address)
        -b bytes       Specify number of bytes
        -r <filename>  Read data from device to file
        -w <filename>  Write data from file to device
        -v <filename>  Verify data in device against file
        -V             Print Date(YearMonthDay-Version) and Version format is IE: 20171204-1.0
        -u             Unlock. Reset option bytes to factory default to remove write protection.

and you have

so just add

upload_protocol = stlinkv3

in the platformio.ini.

I hadn’t tried that because it didn’t list stlinkv3 as available - but unfortunately it doesn’t work either.

Configuring upload protocol...
AVAILABLE: serial, stlinkv2, stlinkv3
CURRENT: upload_protocol = stlinkv3
Uploading .pio\build\stm8sblue\firmware.hex
*** [upload] Error 3221225477

Hm, that’s the general error code for STATUS_ACCESS_VIOLATION, the program crashed. Let me see if I can compile the latest version from GitHub - vdudouyt/stm8flash: program your stm8 devices with SWIM/stlinkv(1,2) and if that makes a difference.

In the meantime, when you open a commandline (Windows+R → cmd.exe) and execute

C:\Users\user\.platformio\packages\tool-openocd\bin\openocd -s C:\Users\user\.platformio\packages\tool-openocd\scripts -f interface\stlink-dap.cfg -f target\stm8s103.cfg

(with user replaced with your username), what is the output?

I’ve recompiled stm8flash for Windows.

Can you add

platform_packages =
   tool-stm8flash@https://github.com/maxgerhardt/pio-stm8flash/raw/main/tool-stm8flash-windows-1.1.221016.tar.gz

to the platformio.ini and retry uploading by opening a CLI and executing pio run -v -t upload?

Running with the new version, I don’t see any changes and it looks like the verbosity only increased during the build step. Full log including compile, just so I don’t miss anything:

Processing stm8sblue (platform: ststm8; platform_packages: tool-stm8flash@https://github.com/maxgerhardt/pio-stm8flash/raw/main/tool-stm8flash-windows-1.1.221016.tar.gz; board: stm8sblue; framework: spl; upload_protocol: stlinkv3)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Tool Manager: Installing https://github.com/maxgerhardt/pio-stm8flash/raw/main/tool-stm8flash-windows-1.1.221016.tar.gz
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: tool-stm8flash@1.1.221016 has been installed!
CONFIGURATION: https://docs.platformio.org/page/boards/ststm8/stm8sblue.html
PLATFORM: ST STM8 (2.1.0) > ST STM8S103F3 Breakout Board
HARDWARE: STM8S103F3P6 16MHz, 1KB RAM, 8KB Flash
DEBUG: Current (stlink) External (stlink)
PACKAGES: 
 - framework-ststm8spl @ 0.20301.181217 (2.3.1) 
 - tool-openocd @ 2.1000.200630 (10.0) 
 - tool-stm8binutils @ 0.230.0 (2.30) 
 - tool-stm8flash @ 1.1.221016 (https://github.com/maxgerhardt/pio-stm8flash/raw/main/tool-stm8flash-windows-1.1.221016.tar.gz) 
 - tool-stm8gal @ 1.5.0 
 - toolchain-sdcc @ 1.40200.0 (4.2.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 1 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
sdcc -o .pio\build\stm8sblue\SPL\stm8s_gpio.rel -c -mstm8 --opt-code-size -DF_CPU=16000000L -DPLATFORMIO=60104 -DSTM8S_BLUE -DSTM8S103 -DUSE_STDPERIPH_DRIVER -DUSE_STDINT -IC:\Users\jrand\.platformio\packages\framework-ststm8spl\Libraries\STM8S_StdPeriph_Driver\inc -Isrc C:\Users\jrand\.platformio\packages\framework-ststm8spl\Libraries\STM8S_StdPeriph_Driver\src\stm8s_gpio.c
sdcc -o .pio\build\stm8sblue\src\main.rel -c -mstm8 --opt-code-size -DF_CPU=16000000L -DPLATFORMIO=60104 -DSTM8S_BLUE -DSTM8S103 -DUSE_STDPERIPH_DRIVER -DUSE_STDINT -Iinclude -Isrc -IC:\Users\jrand\.platformio\packages\framework-ststm8spl\Libraries\STM8S_StdPeriph_Driver\inc -Isrc src\main.c
sdcc -o .pio\build\stm8sblue\src\stm8s_it.rel -c -mstm8 --opt-code-size -DF_CPU=16000000L -DPLATFORMIO=60104 -DSTM8S_BLUE -DSTM8S103 -DUSE_STDPERIPH_DRIVER -DUSE_STDINT -Iinclude -Isrc -IC:\Users\jrand\.platformio\packages\framework-ststm8spl\Libraries\STM8S_StdPeriph_Driver\inc -Isrc src\stm8s_it.c
src\main.c:112: warning 85: in function assert_failed unreferenced function argument : 'file'
src\main.c:112: warning 85: in function assert_failed unreferenced function argument : 'line'
sdcc -o .pio\build\stm8sblue\firmware.elf -mstm8 --nostdlib --code-size 8192 --iram-size 1024 --out-fmt-elf .pio\build\stm8sblue\SPL\stm8s_gpio.rel .pio\build\stm8sblue\src\main.rel .pio\build\stm8sblue\src\stm8s_it.rel -LC:\Users\jrand\.platformio\packages\toolchain-sdcc\lib\stm8 -lstm8
MethodWrapper(["checkprogsize"], [".pio\build\stm8sblue\firmware.elf"])
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   0.0% (used 0 bytes from 1024 bytes)
Flash: [=         ]   8.0% (used 658 bytes from 8192 bytes)
.pio\build\stm8sblue\firmware.elf  :

section   size    addr

HOME       111   32768

GSINIT      26   32879

GSFINAL      3   32905

CONST      106   32908

CODE       412   33014

SSEG         1   33426

Total      659
stm8-objcopy -O ihex --remove-section=".debug*" --remove-section=SSEG --remove-section=INITIALIZED --remove-section=DATA .pio\build\stm8sblue\firmware.elf .pio\build\stm8sblue\firmware.hex
<lambda>(["upload"], [".pio\build\stm8sblue\firmware.hex"])
AVAILABLE: serial, stlinkv2, stlinkv3
CURRENT: upload_protocol = stlinkv3
"stm8flash" -c stlinkv3 -p stm8s103?3 -s flash -w .pio\build\stm8sblue\firmware.hex
*** [upload] Error 3221225595

The openocd error might be more telling:

xPack OpenOCD, x86_64 Open On-Chip Debugger 0.10.0+dev-00378-ge5be992df (2020-06-26-09:29)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
srst_only separate srst_gates_jtag srst_open_drain connect_deassert_srst

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : STLINK V3J10M3B5S1 (API v3) VID:PID 0483:374F
Info : Target voltage: 3.263298
Error: Unable to set adapter speed
Error: stlink_swim_enter_failed (unable to connect to the target)

How do you connect the ST-Link and your target chip? SWIM, GND, and power from the stlink or external?

4 pins - SWIM, GND, NRST, and VCC - which I believe is sense and not power. The chip breakout board is separately powered via USB.

Have you ever succesfully programmed the target chip before? I remember on many occassions I had to do a flash unlock before it would do anything, even on dev boards. That is usually done with

C:\Users\<user>\.platformio\packages\tool-stm8flash\stm8flash.exe -c stlinkv3 -p stm8s103?3 -u

Does that crash as well?

Yes, that gives the same crash after attempting unlock. Before starting this thread, I unlocked using ST Visual Programmer, and successfully flashed the .hex file that PIO compiled.

And it’s stable able to connect it? Are you able to do a mass-erase?

The firmware on it doesn’t use the PD1 pin which is SWIM?

I was able to erase using STVP. I get the same error from stm8flash if I try to reprogram after erasing.
The breakout board I’m using has a separate 4 pins for GND/NRST/SWIM/VCC - I can try using the pin for D1 instead, but it doesn’t seem like it would change, given that STVP can read/write when using the dedicated debug pins

Can you hold down the reset button of the board, invoke OpenOCD, and when OpenOCD output seems to stall, release the reset button? Timing might be a bit tricky, but I’ve seen cases where that actually helps.

I created two different new failures.

This happened if I released the reset button any time before Listening on port 4444 for telnet connections:

xPack OpenOCD, x86_64 Open On-Chip Debugger 0.10.0+dev-00378-ge5be992df (2020-06-26-09:29)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
srst_only separate srst_gates_jtag srst_open_drain connect_deassert_srst

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : STLINK V0J12S4 (API v0) VID:PID 0000:0000
Error: init mode failed (unable to connect to the target)

This happened if I released reset after that line appeared:

Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
srst_only separate srst_gates_jtag srst_open_drain connect_deassert_srst

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Error: read version failed

Both states would then repeat on any future openocd runs until I disconnected/reconnected the stlink.

Was the NRST line connected between the ST-Link and target at that moment? What happens when you disconnect that line and only trigger reset manually via the button?

Same outcomes in all cases (no reset press, reset before telnet listen, reset during telnet listen) when NRST is disconnected

I’m afraid at this point, only the openocd / stm8flash developers can help you:

For reference, using a clone ST-Link v2, I did not have problems programming and debugging STM8 chips, with both stm8flash and openocd.

The upload error on my pio configuration was caused from a corrupt (!) version of stm8flash.exe
… having done an update pio …

*Workaraound:

Resolving this issue for others which also may having this issue, after having updated pio:

  1. Rename in pio-folder C:\Users<username>.platformio\packages\tool-stm8flash stm8flash.exe to stm8flash.exe.nok and libusb-1.0.dll in libusb-1.0.dll.nok
  2. Copy from a working Arduino version these two files in the pio-folder.
    Found them in C:\Users<username>\AppData\Local\Arduino15\packages\sduino\tools\STM8Tools\2019.02.05\win

Now:
CURRENT: upload_protocol = stlinkv2
Uploading .pio\build\stm8sblue\firmware.hex
Determine FLASH area

libusb: error [init_device] device ‘\.\USB#VID_04B4&PID_0060&MI_01#6&2BDC5FE1&1&0001’ is no longer connected!
libusb: error [init_device] device ‘\.\USB#VID_0C76&PID_1607&MI_03#6&33B77DAF&0&0003’ is no longer connected!
Due to its file extension (or lack thereof), “.pio\build\stm8sblue\firmware.hex” is considered as INTEL HEX format!
1821 bytes at 0x8000… OK
Bytes written: 1821
======================================================================================================================= [SUCCESS] Took 4.18 seconds
Before:
Building .pio\build\stm8sblue\firmware.hex
Configuring upload protocol…
AVAILABLE: serial, stlinkv2
CURRENT: upload_protocol = stlinkv2
Uploading .pio\build\stm8sblue\firmware.hex
**** [upload] Error 3221225595*
======================================================================================================================= [FAILED] Took 3.85 seconds ===