Skip to content

Instantly share code, notes, and snippets.

@Josverl
Last active June 27, 2023 17:13
Show Gist options
  • Save Josverl/981b42f34c05648875226ded5ebe49f0 to your computer and use it in GitHub Desktop.
Save Josverl/981b42f34c05648875226ded5ebe49f0 to your computer and use it in GitHub Desktop.
Flashing STM32 devices

Flash STM32 boards on Windows, Linux and Mac OS using the STM32CubeProgrammer

Download the STM32CubeProgrammer software from the ST website. https://www.st.com/en/development-tools/stm32cubeprog.html#overview

install it and add it to your shell's path (e.g. .bashrc or .profile)

add to path if not already there. $env:Path += ";C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin"

Flash a device.

In this case the board is a pyboard v1.1 connected to COM7:

mpremote list

COM7 206437A1304E f055:9800 Microsoft None

Put the stm32 board in bootloader mode. mpremote COM7 bootloader

show/confirm device(s) in DFU-Mode

STM32_Programmer_CLI --list

Note , STM32 Cube programmer uses USB1 even if the serialport that is exposed is COM7
-------------------------------------------------------------------
                 STM32CubeProgrammer v2.13.0
-------------------------------------------------------------------
=====  DFU Interface   =====
Total number of available STM32 device in DFU mode: 1
Device Index           : USB1
USB Bus Number         : 001
USB Address Number     : 002
Product ID             : STM32  BOOTLOADER
Serial number          : 206437A1304E
Firmware version       : 0x011a
Device ID              : 0x0413
===== STLink Interface =====
Error: No ST-Link detected!

Test connection. STM32_Programmer_CLI --connect port=USB1

# -------------------------------------------------------------------
# STM32CubeProgrammer v2.13.0
# -------------------------------------------------------------------
# USB speed   : Full Speed (12MBit/s)
# Manuf. ID   : STMicroelectronics
# Product ID  : STM32  BOOTLOADER
# SN          : 206437A1304E
# DFU protocol: 1.1
# Board       : --
# Device ID   : 0x0413
# Device name : STM32F405xx/F407xx/F415xx/F417xx
# Flash size  : 1 MBytes (default)
# Device type : MCU
# Revision ID : --
# Device CPU  : Cortex-M4

Optionally you can erase the device before flashing the new fimware. STM32_Programmer_CLI --connect port=USB1 --erase all

Mass erase ...
Mass erase command correctly executed.
Note: if there's any flash protection, it will not be erased.

**Flash the firmware from the specified hex file Note: the older .dfu files are not supported by STM32_Programmer_CLI STM32_Programmer_CLI --connect port=USB1 --write "STM32\pybv11-thread-20220618-v1.19.1.hex"

STM32CubeProgrammer v2.13.0
-------------------------------------------------------------------
USB speed   : Full Speed (12MBit/s)
Manuf. ID   : STMicroelectronics
Product ID  : STM32  BOOTLOADER
SN          : 206437A1304E
DFU protocol: 1.1
-------------------------------------------------------------------
Board       : --
Device ID   : 0x0413
Device name : STM32F405xx/F407xx/F415xx/F417xx
Flash size  : 1 MBytes (default)
Device type : MCU
Revision ID : --  
Device CPU  : Cortex-M4
Memory Programming ...
Opening and parsing file: pybv11-thread-20220618-v1.19.1.hex
File          : pybv11-thread-20220618-v1.19.1.hex
Size          : 362.69 KB
Address       : 0x08000000
Erasing memory corresponding to segment 0:
Erasing internal memory sector 0
erasing sector 0000 @: 0x08000000 done
Erasing memory corresponding to segment 1:
Erasing internal memory sectors [5 7]
erasing sector 0005 @: 0x08020000 done
erasing sector 0006 @: 0x08040000 done
erasing sector 0007 @: 0x08060000 done
Download in Progress:
█████████████████████████████████████████▒▒▒▒▒▒▒▒▒ 78%
File download complete
Time elapsed during download operation: 00:00:13.961

Restarting the device. I have not yet found a way to restart the MCU from script. none of the likely commands work --go, --start or not while connected using USB -rst

There is a way by inspecting the .hex file before flashing, and retrieving the start address from it

bincopy info "STM32\pybv11-thread-20220618-v1.19.1.hex"

outputs Start address : 0x0805aa95

before-last line of the HEX file

:040000050805AA95AB

:04|0000|05|0805AA95|AB

import bincopy 
hex = bincopy.BinFile("STM32\pybv11-thread-20220618-v1.19.1.hex")
start_address = f"0x{hex.execution_start_address:08X}"

STM32_Programmer_CLI --connect port=USB1 --write "STM32\pybv11-thread-20220618-v1.19.1.hex" --go 0x0805aa95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment