Semihosting issues on Chinese Bluepill Clones

I am trying to get semihosting to work on my bluepill. I followed this thread’s guides here step by step but I am getting the following error when running the task “OpenOCD (semihosting output)” and when trying to debug as well.

When debugging, the Consol’s output is:

Processing bluepill_f103c8 (platform: ststm32; board: bluepill_f103c8; framework: cmsis)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/bluepill_f103c8.html
PLATFORM: ST STM32 (12.1.1) > BluePill F103C8
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (custom) External (blackmagic, cmsis-dap, jlink, stlink)
PACKAGES:
 - framework-cmsis 2.50501.200527 (5.5.1)
 - framework-cmsis-stm32f1 4.3.1
 - tool-ldscripts-ststm32 0.1.0
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
Warning! Cannot find a linker script for the required board! An auto-generated script will be used to link firmware!
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in debug mode
Checking size .pio/build/bluepill_f103c8/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   1.6% (used 328 bytes from 20480 bytes)
Flash: [=         ]   6.6% (used 4320 bytes from 65536 bytes)
========================= [SUCCESS] Took 1.76 seconds =========================
Reading symbols from /Users/engmoh/Embedded/PlatformIO_Projects/STM32_Projects/0-SemiHosting_Configurations/.pio/build/bluepill_f103c8/firmware.elf...
done.
PlatformIO Unified Debugger -> http://bit.ly/pio-debug
PlatformIO: debug_tool = custom
PlatformIO: Initializing remote target...
.pioinit:13: Error in sourced command file:
:3333: Operation timed out.

When running the task, Terminal’s output is:

> Executing task in folder 0-SemiHosting_Configurations: cd ~/.platformio/packages/tool-openocd && bin/openocd -f scripts/interface/stlink.cfg -f target/stm32f1x.cfg <

xPack OpenOCD, x86_64 Open On-Chip Debugger 0.10.0+dev-00378-ge5be992df (2020-06-26-12:31)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 1000 kHz
Info : STLINK V2J37S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 1.436903
Error: target voltage may be too low for reliable debugging
Warn : UNEXPECTED idcode: 0x2ba01477
Error: expected 1 of 1: 0x1ba01477


The terminal process "/bin/zsh '-c', 'cd ~/.platformio/packages/tool-openocd && bin/openocd -f scripts/interface/stlink.cfg -f target/stm32f1x.cfg'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

My platformio.ini

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = cmsis
upload_protocol = stlink

; ============ Enable Arm Semihosting in debugger to show logs =============
extra_scripts = extra_script.py ; Must be located in the main project folder
build_unflags =
    --specs=nosys.specs 
    -lnosys
debug_extra_cmds =
    monitor arm semihosting enable
debug_tool = custom
debug_port = :3333
; ===========================================================================

; Enable Chinese clones STM32 MCUs recognition (Fixing "Warn : UNEXPECTED idcode: 0x2ba01477" Error)
upload_flags = -c set CPUTAPID 0    ;This will allow Originals and Clones to debug 

my Tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "command": "cd ~/.platformio/packages/tool-openocd && bin/openocd -f scripts/interface/stlink.cfg -f target/stm32f1x.cfg",
            "args": [
            ],
            "problemMatcher": [
                "$platformio"
            ],
            "label": "OpenOCD (semihosting output)"
        }
    ]
}

and my main.c

#include <stm32f1xx.h>
#include <stdio.h>

/* semihosting Initializing */
extern void initialise_monitor_handles(void);

int main(void)
{
    initialise_monitor_handles();
    printf("Hello .. Echo .. Echo\n");
}

I think I should use “-c set CPUTAPID 0” somewhere in the tasks.json probably but I don’t know where! but what about debugging? why It’s not working?

Ok, I found a partial solution to this :wink:
I added set CPUTAPID 0 on the top of the first if-statement in the stm32f1x.cfg file located at /Users/MYNAME/.platformio/packages/tool-openocd/scripts/target/stm32f1x.cfg and now debugging and semihosting is working. But I want to set the cpu ID using an argument in the script, I mean not permanently but when needed. I don’t want to edit the .cfg file. Is there any way to achieve that?

What I want exactly is
1- To be able to click on debug and,
2- before the debugger starts, Automatically the task OpenOCD (semihosting output) in tasks.json runs and sets the CPUTAPID to 0.
3- Debugging starts
I am sorry I am new to scripting so forgive my ignorance :slight_smile:

As per Cannot upload to STM32 Bluepill board over STLink-V2 - #25 by StefanoOliveira you should try adding -c "set CPUTAPID 0x2ba01477" in the command… I think before the last -f switch.

Thank you a lot, I confirm it’s working after a small edit, I used -c 'set CPUTAPID 0', The double quotation marks didn’t work for me, the single ones did. The final tasks.json looks like:

    "version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "command": "cd ~/.platformio/packages/tool-openocd && bin/openocd -f scripts/interface/stlink.cfg -c 'set CPUTAPID 0' -f target/stm32f1x.cfg", // Change target MCU from stm32f1x.cfg to any supported MCU, The command "-c 'set CPUTAPID 0'" allows STM32 Clones to be debugged.
        "args": [
        ],
        "problemMatcher": [
            "$platformio"
        ],
        "label": "OpenOCD (semihosting output)"
    }
]

}

How do I automate running this task as soon as I press the debugging button, so the task runs first and then the debugging session starts?

I’m not sure if this is possible at all – this also however would be a VSCode issue as it’s their GUI that implements this. I hope ARM Semihosting · Issue #3516 · platformio/platformio-core · GitHub will get resolved soon so that we just have this built-in into PlatformIO.

Actually on second thought the .vscode/launch.json might be able to do this by specifying it in the preLaunchTask? However that file is automatically generated so it’s prone to being overwritten.

No problems, I will be creating a template project and edit all of its needed parts. I want to implement this in launch.json so no worries about it being overwritten. I just don’t know the syntax and where to put it in launch.json and I need the syntax to be as compatible with any operating system (MAC, WINDOWS, LINUX) as possible (If it wasn’t already!)

Ok! This repository solved my issues I made it public, easy to configure and well-detailed

Many thanks to you for your help!