Programming WeAct BlackPill STM32F411CE Fails uploading to board

I have installed VS-Code, Python, and the PlatformIO at two different computers.
Have a Arduino project for WeAct-Blackpill-STM32F411CE that are compiled OK

Before I could do anything, I had to install the packages: framework-arduinoststm32, tool-stm32duino
then made some changes in main.cpy, blackpill_f411ce.json,
the changes are all around adding WeAct_HID_Flash-Cli loader, and the offset after the WeAct bootloader.

Every thing works just fine, but of cause if You update the packages You will have to reinsert those changes again, as it is for now.

the upload fails because the PC have no COM ports as default, the WeAct loader are not started because of the missing COM port. If the blackpill are running with an App using USB VCOM, the WeAct loader starts correctly but can’t of cause find the usbbootloader code.

What shall I do to get platformio to detect the device and start the WeAct loader when I have none COM ports?

the errors are those:
Looking for upload port…
*** [upload] UnboundLocalError : local variable ‘port’ referenced before assignment
Traceback (most recent call last):
File “C:\Users\Christian.platformio\packages\tool-scons\scons-local-4.4.0\SCons\”, line 1318, in execute
result = self.execfunction(target=target, source=rsources, env=env)
File “C:\Users\Christian.platformio\packages\tool-scons\scons-local-4.4.0\SCons\”, line 737, in call
return self.method(*nargs, **kwargs)
File “C:\Users\Christian.platformio\penv\lib\site-packages\platformio\builder\tools\”, line 113, in AutodetectUploadPort
File “C:\Users\Christian.platformio\penv\lib\site-packages\platformio\device\”, line 156, in find
return best_port or port
UnboundLocalError: local variable ‘port’ referenced before assignment

Well i just had to read the error information and understand the message, and the solution are
Set the board in WeAct upload mode, by pressing the user KEY and hold it until after reset, it will go
in WeAct mode. Now check the device id found in the list of HID devices on the computer, get the device: USB\VID_0483&PID_572A and set the upload_port = USB\VID_0483&PID_572A in platformio.ini.
Rebuild, and upload, succeded, case open for questions

Should be fixed in latest PlatformIO core, open a CLIpio upgrade --dev and retry.

Tried Upgrade the PlatformIO and endded up here, with no upload_port define in the platformio.ini

Configuring upload protocol…
AVAILABLE: blackmagic, cmsis-dap, dfu, hid, jlink, serial, stlink, weact-hid
CURRENT: upload_protocol = weact-hid
Looking for upload port…
Error: Please specify upload_port for environment or use global --upload-port option.
For some development platforms it can be a USB flash drive (i.e. /media//)
*** [upload] Explicit exit, status 1

Did put back the line “upload_port = USB\VID_0483&PID_572A” and back to normal
only nessesary if PC have none COM ports when the device are in flash/programming/upload mode
dfu etc.

Please show these modifications in full. If you code adds the upload action “BeforeUpload”, then depending on the board JSON file (which you again don’t show) it will try to find an upload port.

So you might want custom logic for this.

The WeAct Blackpill STM32F411CE version 3.1 that are not in the board list, but the version 2.0 are fine, have a bootloader onboard from WeAct that have 3 funktions
1 jumping to Your App after reset
2 go into WeAct bootload, done by pressing the KEY button while reset
3 after switching to WeAct mode it can be switched to dfu mode by pressing and hold the KEY button until it starts blinking, but this mode will cause the full chip erase end the bootloader vill be gone.

now my changes:
file found in the .platformio/platforms/ststm32/builder at line 302 just before the line
elif upload_protocol in debug_tools:

I did insert this

elif upload_protocol == “weact-hid”:
def __configure_upload_port(env):
return basename(env.subst("$UPLOAD_PORT"))

    UPLOADCMD='$UPLOADER "$SOURCES" "${__configure_upload_port(__env__)}" "reboot"'
upload_actions = [
    env.VerboseAction(env.AutodetectUploadPort, "Looking for upload port..."),
    env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")

then in the file blackpill_f411ce.json found at .platformio/platforms/ststm32/boards I did the folowing
find the WeAct Studio BlackPill V2.0
change the “protocol”:“weact-hid”
then add another two protocols after the “dfu”

In the found at: .platformio/packages/framework-arduinoststm32/tools/platformio
I did insert the following at line at around line140:

elif upload_protocol == "weact-hid":
    if mcu.startswith("stm32f1"):
        offset = 0x800
    elif mcu.startswith("stm32f4"):
        offset = 0x4000


just before the
elif upload_protocol == “dfu”:
# STM32F103 series doesn’t have embedded DFU over USB

My platformio.ini are this:
; PlatformIO Project Configuration File
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting

;default_envs =
; custom_monitor_port
; custom_monitor_speedrate
;monitor_port = COM9
;monitor_speed = 115200

platform = ststm32
board = blackpill_f411ce
framework = arduino
build_flags =
board_upload_method_weacthidmethod = WeAct_HID_Bootloader
board_upload_method_weacthidmethod_upload_protocol = hid22
board_upload_method_weacthidmethod_upload_tool = weact_hid_upload
board_upload_method_weacthidmethod_build_bootloader_flags = -DBL_HID -DVECT_TAB_OFFSET={build_flash_offset}
upload_port = USB\VID_0483&PID_572A

monitor_port = COM9
monitor_speed = 115200

thats all

And when you remove

it accepts your custom upload_port? Because you should be running into this piece of code from the core then that just accepts your value

After removing the line
and removing the upload_port statement
I did rebuild, and upload with succes, thanks a lot.
Weel I have programmed a lot in the Years, but I havent used time to dig into all that amount of code
just using the google search for solutions processed it in my brain getting the bedst and right solutions for my use, so of go my hat, and I bend my knees in respect.

I forgot to teel where i did place the WeAct_HID_Flash-Cli.exe file, it is placed in:

here are the result for the upload command:

Processing blackpill_f411ce (platform: ststm32; board: blackpill_f411ce; framework: arduino)
--------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via -v, --verbose option
CONFIGURATION: Redirecting...
PLATFORM: ST STM32 (15.4.1) > WeAct Studio BlackPill V2.0 (STM32F411CE)
HARDWARE: STM32F411CEU6 100MHz, 128KB RAM, 512KB Flash
DEBUG: Current (blackmagic) External (blackmagic, cmsis-dap, jlink, stlink)

  • framework-arduinoststm32 @ 4.20200.220530 (2.2.0)
  • framework-cmsis @ 2.50700.210515 (5.7.0)
  • tool-dfuutil @ 1.11.0
  • tool-openocd @ 2.1100.211028 (11.0)
  • tool-stm32duino @ 1.0.2
  • toolchain-gccarmnoneeabi @ 1.90201.191206 (9.2.1)
    LDF: Library Dependency Finder → Library Dependency Finder (LDF) — PlatformIO latest documentation
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 14 compatible libraries
    Scanning dependencies…
    Dependency Graph
    |-- Adafruit Unified Sensor @ 1.1.6
    |-- Adafruit BME280 Library @ 2.2.2
    | |-- Adafruit BusIO @ 1.13.2
    | | |-- Wire @ 1.0
    | | |-- SPI @ 1.0
    | |-- Wire @ 1.0
    | |-- SPI @ 1.0
    | |-- Adafruit Unified Sensor @ 1.1.6
    |-- Wire @ 1.0
    Building in release mode
    Checking size .pio\build\blackpill_f411ce\firmware.elf
    Advanced Memory Usage is available via “PlatformIO Home > Project Inspect”
    RAM: [ ] 3.4% (used 4436 bytes from 131072 bytes)
    Flash: [= ] 9.5% (used 49776 bytes from 524288 bytes)
    Configuring upload protocol…
    AVAILABLE: blackmagic, cmsis-dap, dfu, hid, jlink, serial, stlink, weact-hid
    CURRENT: upload_protocol = weact-hid
    Uploading .pio\build\blackpill_f411ce\firmware.bin

| WeAct HID-Flash Cli v1.0.0 - STM32 HID Bootloader Flash Tool |
| Modified From HID-Flash v2.2.1 - STM32 HID Bootloader Flash Tool |
| WeAct. Modified and Write by zhuyix 20191220 |

Searching for HID Device [0483:572A] …
HID device [0483:572A] is found !
Filename Extension: .bin
Sending command …
Flashing Firmware …
. 1024 Bytes
. 2048 Bytes
. 3072 Bytes
. 4096 Bytes
. 5120 Bytes
. 6144 Bytes
. 7168 Bytes
. 8192 Bytes
. 9216 Bytes
. 10240 Bytes
. 11264 Bytes
. 12288 Bytes
. 13312 Bytes
. 14336 Bytes
. 15360 Bytes
. 16384 Bytes
. 17408 Bytes
. 18432 Bytes
. 19456 Bytes
. 20480 Bytes
. 21504 Bytes
. 22528 Bytes
. 23552 Bytes
. 24576 Bytes
. 25600 Bytes
. 26624 Bytes
. 27648 Bytes
. 28672 Bytes
. 29696 Bytes
. 30720 Bytes
. 31744 Bytes
. 32768 Bytes
. 33792 Bytes
. 34816 Bytes
. 35840 Bytes
. 36864 Bytes
. 37888 Bytes
. 38912 Bytes
. 39936 Bytes
. 40960 Bytes
. 41984 Bytes
. 43008 Bytes
. 44032 Bytes
. 45056 Bytes
. 46080 Bytes
. 47104 Bytes
. 48128 Bytes
. 49152 Bytes
. 50176 Bytes
. 51200 Bytes
Flash Done !
Sending command …
========================================================== [SUCCESS] Took 10.35 seconds ========================================================== * Terminal will be reused by tasks, press any key to close it.