Debug support with STM8 S103F3P6, having external ST-Link debugger

Hi,

I have STM8 S103F3P6 Mini Development Board and an stlink debugger. I am interested in onboard debugging when and External St link debugger is connected via SWIM.

GDB support is available for the said target and can be used with eclipse as shown in the link https://stm8-binutils-gdb.sourceforge.io/

Currently there is no support for this in platformio.
The MCU and debugger are both dirt cheap and greatly become the choice for deeply embedded applications.

Can we expect an upcoming support for the S103F3P6. Greatly appreciate your response.

Thank you,
Muhammad Osaid

If I read it right it provides the stm8-gdb binary? We already have that in our tool-stm8binutils package. Using configuration directives one can also enable debugging with custom programs. But the OpenOCD version included in PIO also has STM8 support and I can connect to my chip…

>C:\Users\Max\.platformio\packages/tool-openocd/bin/openocd -f interface\stlink-dap.cfg -f target\stm8s103.cfg
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 V2J37S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.255209
Info : clock speed 800 kHz
Info : starting gdb server for stm8s.cpu on 3333
Info : Listening on port 3333 for gdb connections

but I can’t get programming to work…

>C:\Users\Max\Downloads\xpack-openocd-0.10.0-15\bin\openocd -f interface\stlink-dap.cfg -f target\stm8s103.cfg -c "program {C:\Users\Max\Documents\PlatformIO\Projects\stm8_testing\.pio\build\stm8sblue\firmware.bin} 0x8000 verify reset exit"
xPack OpenOCD, x86_64 Open On-Chip Debugger 0.10.0+dev (2020-10-13-17: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 : STLINK V2J37S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.253616
Info : clock speed 800 kHz
Info : starting gdb server for stm8s.cpu on 3333
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, pc: 0x00008000
** Programming Started **
flash
  flash bank bank_id driver_name base_address size_bytes chip_width_bytes
            bus_width_bytes target [driver_options ...]
  flash banks
  flash init
  flash list
gdb_flash_program ('enable'|'disable')
nand
program <filename> [address] [pre-verify] [verify] [reset] [exit]
  stm8 enable_stm8l [1/0]
    stm8s.cpu stm8 enable_stm8l [1/0]
Error: invalid subcommand "write_image erase C:\Users\Max\Documents\PlatformIO\Projects\stm8_testing\.pio\build\stm8sblue\firmware.bin 0x8000"
** Programming Failed **
shutdown command invoked
embedded:startup.tcl:583: Error:
at file "embedded:startup.tcl", line 583

I’ll try and figure out why that’s the case…

I’ve further looked into it and can now properly flash the chip using OpenOCD and connect to it, but there’s a problem that no debug symbols are in the ELF file, because the build process doesn’t have the right flags and SDCC needs a workaround and the binutils too… Seems like a longer project.

current platformio.ini


[env:stm8sblue]
;build_type = debug 
platform = ststm8
board = stm8sblue
framework = arduino
;upload_protocol = stlinkv2
upload_protocol = custom
upload_flags =
    -f
    interface\stlink-dap.cfg
    -c 
    transport select swim
    -f 
    target\stm8s103.cfg
upload_command = $PROJECT_PACKAGES_DIR/tool-openocd/bin/openocd $UPLOAD_FLAGS -c "init; reset halt; load_image {$SOURCE}; verify_image {$SOURCE}; reset; shutdown"
debug_tool = custom 
; empty. You need to start openocd yourself with
; openocd -f interface\stlink-dap.cfg -f target\stm8s103.cfg
debug_server =
;    $PROJECT_PACKAGES_DIR/tool-openocd/bin/openocd
;    -f
;    interface\stlink-dap.cfg
;    -f 
;    target\stm8s103.cfg
debug_port = localhost:3333
;debug_load_mode = manual
debug_init_break = tbreak setup
debug_init_cmds =
    $INIT_BREAK
;    monitor init
;    monitor reset halt
build_flags = --debug --fverbose-asm --verbose
debug_build_flags = --debug
extra_scripts = fix_debug_compile.py
import shutil
Import("env")

def dos2unix(file_to_strip, output_file):
    content = ''
    outsize = 0
    with open(file_to_strip, 'rb') as infile:
        content = infile.read()
    with open(output_file, 'wb') as output:
        for line in content.splitlines():
            outsize += len(line) + 1
            output.write(line + b'\n')

def fix_elf(source, target, env):  
    # build our own 
    #print(env["LINKCOM"])
    #env.Execute([env["LINKCOM"]])
    pass

def fix_rel_file(source, target, env):

    for src in source:
        print("dos2unix fix: " + str(src))
        dos2unix(str(src), str(src)+".fixed")
        shutil.move(str(src)+".fixed", str(src))
# so that debug output ends up in the final ELF file
#env.Append(LINKFLAGS=["--debug"])
# hook pre-build actions of these files to workaround SDCC error: 
# if .rel files compiled in --debug mode have Windows line endings, linking will fail.
env.AddPreAction("$BUILD_DIR/libFrameworkArduino.lib", fix_rel_file)
env.AddPreAction("$BUILD_DIR/libFrameworkArduinoVariant.lib", fix_rel_file)
env.AddPreAction("$BUILD_DIR/${PROGNAME}.elf", fix_rel_file)
env.AddPostAction("$BUILD_DIR/${PROGNAME}.elf", fix_elf)

env.Append(LINKFLAGS = ["--verbose", "--debug", "--fverbose-asm", "--no-peep"])

At least I can connect and see register content in GDB

Once I get this working I can file an issue or PR at GitHub - platformio/platform-ststm8: ST STM8: development platform for PlatformIO so that it can be integrated.