Skip to main content

STM32Loader

PyPI package Test Lint License Downloads

Python module to upload or download firmware to / from ST Microelectronics STM32 microcontrollers over UART.

Also supports ST BlueNRG devices, and the SweetPeas bootloader for Wiznet W7500.

Compatible with Python version 3.9-3.15 and PyPy 3.10-3.11.

Run using uvx

uvx stm32loader

Installation with pip

pip install stm32loader

To also support loading .hex files:

pip install stm32loader[hex]

To install the latest development version:

pip install git+https://github.com/florisla/stm32loader.git

Usage

usage: stm32loader [-h] [-e] [-E] [-u] [-x] [--write-unprotect]
                   [--write-protect] [-w] [-v] [-r] [-l LENGTH] -p PORT
                   [-b BAUD] [-a ADDRESS | -o OFFSET] [-g ADDRESS] [-f FAMILY]
                   [-V] [-q] [-s] [-R] [-B] [-n] [-P {even,none}] [--version]
                   [FILE.BIN]

Flash firmware to STM32 microcontrollers.

positional arguments:
  FILE.BIN              File to read from or store to flash.

options:
  -h, --help            show this help message and exit
  -e, --erase           Erase the full flash memory or a specific region
                        (support --address and --length). Note: this is
                        required on previously written memory.
  -E, --auto-erase      Before writing, erase only the flash pages that the
                        written data covers.
  -u, --unprotect       Unprotect flash from readout.
  -x, --protect         Protect flash against readout.
  --write-unprotect     Disable write protection before flashing
  --write-protect       Enable write protection after flashing
  -w, --write           Write file content to flash.
  -v, --verify          Verify flash content versus local file (recommended).
  -r, --read            Read from flash and store in local file.
  -l, --length LENGTH   Length of read or erase. For write and verify, use
                        only this many bytes of the file.
  -p, --port PORT       Serial port (default: $STM32LOADER_SERIAL_PORT).
  -b, --baud BAUD       Baudrate. (default: 115200)
  -a, --address ADDRESS
                        Target address for read or write. For erase, this is
                        used when you supply --length. Default: the start of
                        flash memory.
  -o, --offset OFFSET   Alternative to --address: an offset from the start of
                        flash memory.
  -g, --go-address ADDRESS
                        Start executing from address (0x08000000, usually).
  -f, --family FAMILY   Device family to read out device UID and flash size;
                        e.g F1 for STM32F1xx. Possible values: C0, F0, F1, F2,
                        F3, F4, F7, G0, G4, H5, H7, L0, L1, L4, L5, WBA, WB0,
                        WB, WL, U5, W, NRG, WIZ, GD32VW55X (default:
                        $STM32LOADER_FAMILY).
  -V, --verbose         Verbose mode.
  -q, --quiet           Quiet mode.
  -s, --swap-rts-dtr    Swap RTS and DTR: use RTS for reset and DTR for boot0.
  -R, --reset-active-high
                        Make RESET active high.
  -B, --boot0-active-low
                        Make BOOT0 active low.
  -n, --no-progress     Don't show progress bar.
  -P, --parity {even,none}
                        Parity: "even" for STM32, "none" for BlueNRG.
                        (default: even)
  --version             show program's version number and exit

examples:
  stm32loader --port COM7 --family F1
  stm32loader --erase --write --verify example/main.bin

Command-line example

stm32loader --port /dev/tty.usbserial-ftCYPMYJ --erase --write --verify somefile.bin

This will pre-erase flash, write somefile.bin to the flash on the device, and then perform a verification after writing is finished.

To erase only the flash pages that the new data will occupy, use --auto-erase instead of --erase:

stm32loader --port /dev/tty.usbserial-ftCYPMYJ --auto-erase --write --verify somefile.bin

The erased range is rounded out to whole pages (sectors), following the device's flash layout. This also works on devices with mixed sector sizes.

You can skip the --port option by configuring environment variable STM32LOADER_SERIAL_PORT. Similarly, --family may be supplied through STM32LOADER_FAMILY.

To read out firmware and store it in a file:

stm32loader --read --port /dev/cu.usbserial-A5XK3RJT --family F1 --length 0x10000 --address 0x08000000 dump.bin 

To erase the full device:

stm32loader --erase --port /dev/cu.usbserial-A5XK3RJT

Or erase only a specific region of the flash:

stm32loader --erase --address 0x08000000 --length 0x2000 --port /dev/cu.usbserial-A5XK3RJT

Reference documents

  • ST AN2606: STM32 microcontroller system memory boot mode
  • ST AN3155: USART protocol used in the STM32 bootloader
  • ST AN4872: BlueNRG-1 and BlueNRG-2 UART bootloader protocol

Acknowledgement

Original version by Ivan A-R (tuxotronic.org). See CONTRIBUTORS.md for all contributors and sources of inspiration.

Alternatives

If you don't need the flexibility of a Python tool, you can take a look at other similar tools in ALTERNATIVES.md.

Electrically

The below assumes you are connecting an STM32F10x. For other chips, the serial pins and/or the BOOT0 / BOOT1 values may differ.

Make the following connections:

  • Serial adapter GND to MCU GND.
  • Serial adapter power to MCU power or vice versa (either 3.3 or 5 Volt).
  • Note if you're using 5 Volt signaling or 3V3 on the serial adapter.
  • Serial TX to MCU RX (PA10).
  • Serial RX to MCU TX (PA9).
  • Serial DTR to MCU RESET.
  • Serial RTS to MCU BOOT0 (or BOOT0 to 3.3V).
  • MCU BOOT1 to GND.

If either RTS or DTR are not available on your serial adapter, you'll have to manually push buttons or work with jumpers. When given a choice, set BOOT0 manually high and drive RESET through the serial adapter (it needs to toggle, whereas BOOT0 does not).

Not currently supported

  • Command-line argument for write protection for some devices (e.g. those with dual-bank flash).
  • STM8 devices (ST UM0560).
  • Other bootloader protocols (e.g. I2C, HEX -> implemented in stm32flash).

Metadata

Release files for stm32loader 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stm32loader 1.0.0
File Size Uploaded Provenance Actions
stm32loader-1.0.0.tar.gz 48.3 kB Without provenance Details for stm32loader-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for stm32loader 1.0.0
File Interpreter ABI Platform Actions
stm32loader-1.0.0-py3-none-any.whl Python 3 none any Details for stm32loader-1.0.0-py3-none-any.whl

Total release size: 98.2 kB

Release files / stm32loader-1.0.0.tar.gz

Download URL stm32loader-1.0.0.tar.gz
Size 48.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9fe13136be89e4114d6be2480a477da42c94cc655c90e3a89ee722a483119116
BLAKE2b-256 checksum
How to use checksums
aede757df47eb42da21ea5e15bf59538a36d1d16b4fdca0a5be33fe5a57accf6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / stm32loader-1.0.0-py3-none-any.whl

Download URL stm32loader-1.0.0-py3-none-any.whl
Size 49.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a5b7a2135ded591cd61ecd3dc7c019e59e2dd5ac04fbfbb7c7e2fd4cb97347c
BLAKE2b-256 checksum
How to use checksums
ee7b9c07e1b9bd708356442bdaf92cdbf4be8ae8d57939fb0eb9f8da65c1cae5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for stm32loader 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page