Anyone has experience with STM HID Bootloader? I am stuck with this issue:

Program stats from VSCODE: (using Arduino libs)
RAM: [=== ] 32.9% (used 6740 bytes from 20480 bytes)
Flash: [========= ] 86.9% (used 56936 bytes from 65536 bytes)

1 I upload my program using SWD interface from VSCode - it works and the STM32F103C8 enumerates on USB)
2 with BOOT pins low, I flash bootloder STM32_HID_Bootloader$ STM32_Programmer_CLI -c port=SWD mode=UR -w stm32_binaries/F103/low_and_medium_density/hid_generic_pc13.bin 0x08000000
3 I switch BOOT1 to high, reset - bootloader is active and LED flashing fast
4 I flash same firmware as in step# 1 : hid-flash .pio/build/genericSTM32F103C8/firmware.bin /dev/ttyUSB
Flashing ends with a with > Done! The last count I see is: . 57344 Bytes seems good.

I move the BOOT1 jumper to low. reboot
The device is NOT running normally, does not enumerate on USB (which it did before)
I move the BOOT1 jumper to HIGH, reboot - and the bootloader is alive and ready for next attempt.

Please help

What firmware are you uploading exactly? If you don’t configure the uploaded firmware to also start a e.g. USB CDC, it won’t be enumerated on the USB.

It’s a program that uses Arduino framework, built by PlatformIO, the binary file works fine if uploaded by SWD, but does not run if uploaded by the STM HID Bootloader. Unlike a .hex file, I can’t see which address the .bin file is starting at.

[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
framework = arduino
upload_protocol = stlink
build_flags = 
	-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
	-D USBD_VID=0x0483
	-D USBD_PID=0x5740
	-D USBD_USE_CDC
	-D USBCON
	-D USB_MANUFACTURER="Unknown"
    -D USB_PRODUCT=\"STM32F103C8\"
	-D HAL_PCD_MODULE_ENABLED
	-D ENABLE_HWSERIAL1
	-Wl,--no-warn-rwx-segments
board_build.upload_method = stlink
monitor_port = /dev/ttyACM0
monitor_speed = 115200
lib_deps = 
	Wire

No. Use

upload_protocol = hid

here. Then, PlatformIO will also be able to flash it with the regular upload button.

There is special logic to support the uploader as well as reconfiguration of some vector table offsets going on so that booting and interrupts work. It will surely crash very quickly if that is not configured correctly.

In fact, you should also throw -D PIO_FRAMEWORK_ARDUINO_ENABLE_HID into the build_flags

You miss in point 4. FW from point 1 cant be used here. Normal fw place and start is upquote, but for custom bootloader this adress is used with bootloader …

@maxgerhardt It sounds like you are trying to help me make PIO upload using HID.
I actually prefer SWD during development, (and yes, doing so erases the bootloader) So I am good while programming, but I wish to have a way to send somebody a file that can be flashed using the HID method. (from terminal)

@marian.mlcoch The bootloader is at addess 0x0800000 - and id-flash tool does not even have an option to start at some specific address.

You must set upload_protocol = hid so that PlatformIO produces a .bin file that works when flashed via hid-flash. Otherwise all offsets will be wrong.

You can make two environments in the platformio.ini, one for “SWD development” and one for “Binary for HID upload”.

Thank you - I will try that. - I assume that requires two different platforio.ini files for that build ?

And while I have attention from you, as a skilled programmer, could you please point me to some example on how to (if a button is pressed on startup) - jump to the bootloader from my program?

There is one platformio.ini and it can have multiple build environments inside it. See documentation

https://docs.platformio.org/en/latest/projectconf/index.html

Don’t forget that you can switch between different project environments in VSCode using

https://docs.platformio.org/en/latest/integration/ide/vscode.html#project-tasks

E.g.,

[env] ; common settings
platform = ststm32
board = genericSTM32F103C8
framework = arduino
build_flags = 
	-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
	-D USBD_VID=0x0483
	-D USBD_PID=0x5740
	-D USBD_USE_CDC
	-D USBCON
	-D USB_MANUFACTURER="Unknown"
    -D USB_PRODUCT=\"STM32F103C8\"
	-D HAL_PCD_MODULE_ENABLED
	-D ENABLE_HWSERIAL1
	-Wl,--no-warn-rwx-segments
monitor_port = /dev/ttyACM0
monitor_speed = 115200
lib_deps = 
	Wire

[env:for_stlink]
upload_protocol = stlink

[env:for_hid]
upload_protocol = hid
; extra flags
build_flags =
   ${env.build_flags}
   -D PIO_FRAMEWORK_ARDUINO_ENABLE_HID 
1 Like

Thank you very much. tried, as exactly as you suggested, the build-flag was essential and made all the difference.

Do you have a clue where I can find an example for how to jump to bootloader from main program (as when I press a button during power-on ?)

Calling the CMSIS function NVIC_SystemReset(); should be the same as pressing the reset button just using a software flag. It should start execution at the start of flash again (0x8000000), if the BOOT pins are set for it; The HID bootloader should live there.

I’m not sure how exactly the HID bootloader works or for how long it stays open. After all, it will want to boot an existing application pretty quickly.

Looking at https://github.com/Serasidis/STM32_HID_Bootloader, conencting to the serial port (at possibly 1200 baud?) and toggling DTR also seems to put the device back into bootloader mode.

Wow, you are good! :slight_smile: I did not try to read the bootloader source and failed to “discover” that it was trigged by DTR (and could be flashed without the use of a jumper - or otherwise running the bootloader.)

  • It works just fine.

Thank you very much.