Cannot upload to STM32 Bluepill board over STLink-V2

I have just confirmed that this works with the staging version and no extra config.

[env:bluepill_f103c8]
platform = https://github.com/platformio/platform-ststm32.git
board = bluepill_f103c8
framework = arduino
1 Like

Thank you so much!!! Now it should work with stable 4.0.1 version

1 Like

Hi,
Has anyone else been successful uploading to bluepill using an stlink v2 clone probe and libopencm3?

I have a similar problem to the above. I am using bluepill, stlink v2 clone probe, libopencm3 framework. I am using VSCode 1.36.1 on Ubuntu 16.04.

I get a “Warn : UNEXPECTED idcode: 0x2ba01477; Error: expected 1 of 1 0x1ba01477” error when I try to upload. However when I use “st-util flash firmware.bin 0x8000000” using GitHub - stlink-org/stlink: Open source STM32 MCU programming toolset I am sucessful.

Here is my platformio.ini:

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

Here is the output from Platformio while trying to upload:
> Executing task in folder 190713-174118-libopencm3-blink: platformio run --target upload <

Processing bluepill_f103c8 (platform: ststm32; board: bluepill_f103c8; framework: libopencm3)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/bluepill_f103c8.html
PLATFORM: ST STM32 5.4.3 > BluePill F103C8
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (stlink) External (blackmagic, jlink, stlink)
PACKAGES: tool-stm32duino 1.0.1, framework-libopencm3 1.1.0, toolchain-gccarmnoneeabi 1.70201.0 (7.2.1), tool-openocd 2.1000.190422 (10.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Checking size .pio/build/bluepill_f103c8/firmware.elf
Memory Usage -> http://bit.ly/pio-memory-usage
DATA:    [          ]   0.0% (used 0 bytes from 20480 bytes)
PROGRAM: [          ]   1.0% (used 684 bytes from 65536 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, dfu, jlink, mbed, stlink
CURRENT: upload_protocol = stlink
Uploading .pio/build/bluepill_f103c8/firmware.elf
GNU MCU Eclipse OpenOCD, 64-bitOpen On-Chip Debugger 0.10.0+dev-00593-g23ad80df4 (2019-04-22-20:18)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1
hla_swd
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
none separate
Warn : UNEXPECTED idcode: 0x2ba01477
Error: expected 1 of 1: 0x1ba01477
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked

*** [upload] Error 1
================================================================================== [ERROR] Took 1.05 seconds ==================================================================================
The terminal process terminated with exit code: 1

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

Here is the output when I upload using https://github.com/texane/stlink:

st-flash write firmware.bin 0x8000000
st-flash 1.5.1-30-g84f63d2
2019-07-14T11:30:04 INFO common.c: Loading device parameters....
2019-07-14T11:30:04 INFO common.c: Device connected is: F1 Medium-density device, id 0x20036410
2019-07-14T11:30:04 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x10000 bytes (64 KiB) in pages of 1024 bytes
2019-07-14T11:30:04 INFO common.c: Attempting to write 684 (0x2ac) bytes to stm32 address: 134217728 (0x8000000)
Flash page at addr: 0x08000000 erased
2019-07-14T11:30:04 INFO common.c: Finished erasing 1 pages of 1024 (0x400) bytes
2019-07-14T11:30:04 INFO common.c: Starting Flash write for VL/F0/F3/F1_XL core id
2019-07-14T11:30:04 INFO flash_loader.c: Successfully loaded flash loader in sram
  1/1 pages written
2019-07-14T11:30:04 INFO common.c: Starting verification of write complete
2019-07-14T11:30:04 INFO common.c: Flash written and verified! jolly good!

It shows I was able to successfully upload and the “blink” program was flashing the LED on my bluepill

Here is what I get when I do a “pio update”

    pio update
    Updating tool-scons                      @ 2.20501.7      [Up-to-date]
    Updating tool-unity                      @ 1.20403.0      [Up-to-date]
    Updating contrib-pysite                  @ 2.27.190418    [Up-to-date]
    Updating contrib-piohome                 @ 2.2.0          [Up-to-date]
    Updating tool-pioplus                    @ 2.5.2          [Up-to-date]

    Platform Manager
    ================
    Platform ST STM32
    --------
    Updating ststm32                         @ 5.4.3          [Up-to-date]
    Updating tool-stm32duino                 @ 1.0.1          [Up-to-date]
    Updating framework-libopencm3            @ 1.1.0          [Up-to-date]
    Updating tool-openocd                    @ 2.1000.190422  [Up-to-date]
    Updating framework-arduinoststm32        @ 3.10500.190327 [Up-to-date]
    Updating toolchain-gccarmnoneeabi        @ 1.70201.0      [Up-to-date]

Thanks for your help. PlatformIO is a nice tool.

I have successfully used it with a Black Pill board (main difference: correct resistor for USB bus) - no problem.

Are you sure your blue bill has an STM32F103C8 MCU? Can you provide the chip marking / full text on top of your MCU?

From the IDCODE, it looks as if you have a slightly different MCU.

1 Like

Chances are you don’t have an STM32F103C8T6 chip, but a Chinese clone, namely CS32F103C8T6 from China Key System & Integrated. That one is reported to use the 0x2ba01477 (as the IDcode or rather as the SW-DP tap id).

As a workaround, you can edit the file ~/.platformio/packages/tool-openocd/scripts/target/stm32f1x.cfg and change the line:

set _CPUTAPID 0x1ba01477

to:

set _CPUTAPID 0x2ba01477

If it’s really the Chinese clone, you should also initiate that the chip gets added to OpenOCD and PlatformIO.

3 Likes

Thanks for the quick reply. I will only be able try your recommended fix later this week. I will update everyone with results.

Yes, this work around worked. by changing the above .cfg file, I was able to upload the firmware.elf successfully.

With regards to initiating the chip gets added to OpenOCD and PlatformIO, the markings on the chip say: STM32F103C8T6 991KA 93 MYS 807. So it could be the CS32F103C8T6 but it has been possibly relabeled. Should initiate the chip gets added with this STM label?

Strange. It probably doesn’t make sense to create a second stm32f1x configuration if it is not possible to distinguish the chips from their marking.

It would be great if the configuration could be changed such that it works with both tap IDs. I’m not an OpenOCD expert and I have no real clue how it is integrated into PlatformIO. Somebody with more knowledge about it would need to comment on this.

1 Like

You’ll probably want to open a ticket in the OpenOCD project ticket tracker: OpenOCD - Open On-Chip Debugger / Tickets

1 Like

Hi, ok I will do that. Will post here with any updates.

Thank you very much !

I was thinking my stlink-v2 (a clone without a doubt) was not recognized properly by platformio, instead it was indeed the stm32 chip, or rather its clone.

This workaround worked for me. The markings on the chip are : CKS32F 103C8T6 NK1M5 1845 E

It is a 128k flash chip btw.

Could you post the link to the ticket?

Here’s a way to use a single .cfg for both types of mcus:

According to the docs it is possible to add more than one -expected-id argument. So just append this to the command line in stm32f1x.cfg:

swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID -expected-id 0x2ba01477

You can also add this to ‘platformio.ini’.

; set CKS mcu ID for knock off of STM chip
upload_flags = -c set CPUTAPID 0x2ba01477

BTW I have a dozen Blue Pills and the mcu cover appearance does not match the CPUTAPID. With Chinese manufactured BPs you have absolutely no idea of what your getting.

I’m still trying to figure out why USB Serial only work on some of them.

Regards,
John

2 Likes

Related to Flashing Bootloader for BluePill Boards · rogerclarkmelbourne/Arduino_STM32 Wiki · GitHub?

1 Like

maxgerhardt I’ve replaced the 10K with 1.5K on the boards without working USB cdc serial. It doesn’t fix the problem. They still don’t get recognized by the PC.

The few BPs that do work, work fine with 10K. That resistor signals that the USB device(BP) is a USB Full Speed device. I have removed the 10K resistor, leaving it open(infinite resistance) on working boards and they continue to work fine on an old machine running XP, a win7 desktop and a win10 notebook.

I plan to dump the contents of all the registers to learn what might be different. I have working usb serial on stm and cks devices as per their manufacturer ID, not the bogus cover marks.

Thank you very much! It worked perfectly for me!
My .ini file became this in my configurations:

[env:genericSTM32F103C8]
platform = https://github.com/platformio/platform-ststm32.git
board = genericSTM32F103C8
framework = arduino
lib_extra_dirs = ~/Documents/Arduino/libraries
upload_protocol = stlink
upload_flags = -c set CPUTAPID 0x2ba01477
1 Like

I implemented all of the above , but still have problems on some combination of stlinkv2 and bluepills. I do however have three ‘golden base lines’ bluepils that work, but only on one PC setup, if you move only the USB cable to another PC, same setup, the ‘golden base lines’ is not golden any more. However they all work perfectly if I use stlink-util from STM, so the question is why not in platformio & openOCD. I first suggested it was a speed problem but it is not, the problem ‘move’ . The standard for platformio is

hla_swd
none separate

I tried to locate the ‘separate’ to change it to hardware reset but cannot find the option anywhere in stm32f1x.cfg.

Indeed the default is

reset_config srst_nogate

I first was under the impression I have the wrong file, but if I change the CPU ID from 0x1b to 0x2b … it does detect it, so do not understand why I have none separate

  1. How does one change this to use hardware reset?

ST stlink-utils only works well with the option hardware reset to connects.
Note : without the hardware mod I could not get ST stlink-utils to operate reliably…

Please update the cfg to cater for both the standard ST32F103 and Chinese ‘copies’ …not sure this works…

swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID -expected-id 0x2ba01477

This would solve a lot of trouble and save time waisted…
Thanks

I tried to read your post many time, but I couldn’t understand what can I do to solve this problem.

I solved the problem by adding the reset line from ST link to the blue-pill. It now works every time without any problem and by adding the CPU ID in the INI file, Thanks, appreciated.