Skip to content

Files

Latest commit

27b7bf3 · Oct 30, 2025

History

History
This branch is 4 commits behind micropython/micropython:master.

stm32

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Oct 29, 2025
Jul 8, 2025
Jan 30, 2024
Aug 1, 2025
Oct 30, 2025
May 24, 2018
Oct 30, 2025
Mar 28, 2024
Mar 7, 2024
Feb 15, 2018
Oct 29, 2025
Oct 29, 2025
Sep 6, 2017
May 13, 2025
Jul 8, 2025
Feb 12, 2019
Feb 15, 2018
Mar 14, 2025
Mar 14, 2025
May 8, 2025
Oct 29, 2025
Jan 27, 2022
Oct 29, 2025
Oct 29, 2025
Feb 4, 2025
Mar 7, 2024
Mar 7, 2024
Aug 18, 2025
Oct 29, 2025
Oct 29, 2025
Jun 2, 2023
Nov 25, 2019
Jul 14, 2018
Mar 14, 2025
Oct 29, 2025
Jan 8, 2024
Jan 8, 2024
Mar 9, 2023
Jun 14, 2019
Jun 2, 2023
Oct 29, 2025
Aug 1, 2025
Sep 2, 2025
Sep 2, 2025
Mar 7, 2024
Aug 1, 2025
Mar 7, 2024
Feb 15, 2018
Jul 25, 2025
Feb 15, 2018
Oct 29, 2025
Oct 26, 2023
Oct 29, 2025
Aug 1, 2025
Dec 18, 2024
Mar 7, 2024
Oct 30, 2025
Mar 7, 2024
Oct 30, 2025
Sep 23, 2025
Oct 30, 2025
Nov 30, 2023
Oct 30, 2025
Oct 30, 2025
Mar 7, 2024
Aug 15, 2025
Oct 30, 2025
Oct 20, 2021
Mar 7, 2024
Sep 8, 2020
Oct 30, 2025
Oct 23, 2025
Aug 26, 2022
Oct 23, 2025
Oct 30, 2025
Oct 30, 2025
Mar 15, 2024
Aug 8, 2024
Mar 7, 2024
Jun 23, 2021
Sep 8, 2020
Mar 29, 2024
Mar 7, 2024
Feb 29, 2024
Jul 8, 2025
Oct 30, 2025
Jun 4, 2024
Apr 9, 2025
Jun 15, 2023
Oct 14, 2024
Oct 14, 2024
Jul 25, 2025
Jun 27, 2024
Nov 3, 2023
Jul 8, 2025
Nov 3, 2023
Nov 28, 2023
Jun 8, 2023
Oct 29, 2025
Jul 8, 2025
Sep 23, 2025
Mar 14, 2025
Mar 14, 2025
Oct 29, 2025
Mar 7, 2024
Sep 6, 2017
Mar 7, 2024
Mar 9, 2023
May 26, 2025
Apr 9, 2025
Jun 8, 2023
Mar 8, 2023
Jul 8, 2025
Mar 8, 2023
Mar 8, 2023
Aug 22, 2024
Mar 8, 2023
Mar 7, 2024
Feb 15, 2018
Oct 29, 2025
Sep 10, 2020
Jul 8, 2025
Mar 22, 2022
Jul 8, 2025
Apr 29, 2021
Dec 9, 2024
Apr 11, 2022
Mar 7, 2024
Feb 15, 2018
Oct 29, 2025
Dec 9, 2024
Jul 8, 2025
Oct 29, 2025
Oct 30, 2025
Feb 15, 2018
Oct 30, 2025
Jul 8, 2025
Mar 7, 2024
Aug 23, 2023
Oct 29, 2025
Apr 27, 2023
Jun 23, 2021
Oct 29, 2025
Feb 15, 2018
Oct 30, 2025
Oct 30, 2025
May 5, 2022
Apr 26, 2022
Oct 30, 2025
Oct 30, 2025
Oct 30, 2025
Oct 14, 2024
Jun 23, 2022
Oct 30, 2025
Oct 30, 2025
Mar 7, 2024
Feb 15, 2018
Oct 2, 2025
Jun 24, 2022
Mar 7, 2024
Jul 24, 2020
Mar 7, 2024
Feb 15, 2018
Jul 8, 2025
Jul 8, 2025
Jul 8, 2025

README.md

MicroPython port to STM32 MCUs

This directory contains the port of MicroPython to ST's line of STM32 microcontrollers. Supported MCU series are: STM32F0, STM32F4, STM32F7, STM32G0, STM32G4, STM32H5, STM32H7, STM32L0, STM32L1, STM32L4, STM32WL and STM32WB. Parts of the code here utilise the STM32Cube HAL library.

The officially supported boards are the line of pyboards: PYBv1.0 and PYBv1.1 (both with STM32F405), PYBLITEv1.0 (with STM32F411) and PYBD-SFx (with STM32F7xx MCUs). See micropython.org/pyboard for further details.

Other boards that are supported include ST Discovery and Nucleo boards. See the boards/ subdirectory, which contains the configuration files used to build each individual board.

Build instructions

Before building the firmware for a given board the MicroPython cross-compiler must be built; it will be used to pre-compile some of the built-in scripts to bytecode. The cross-compiler is built and run on the host machine, using:

$ make -C mpy-cross

This command should be executed from the root directory of this repository. All other commands below should be executed from the ports/stm32/ directory.

An ARM compiler is required for the build, along with the associated binary utilities. The default compiler is arm-none-eabi-gcc, which is available for Arch Linux via the package arm-none-eabi-gcc, for Ubuntu via instructions here, or see here for the main GCC ARM Embedded page. The compiler can be changed using the CROSS_COMPILE variable when invoking make.

Next, the board to build must be selected. The default board is PYBV10 but any of the names of the subdirectories in the boards/ directory is a valid board. The board name must be passed as the argument to BOARD= when invoking make.

All boards require certain submodules to be obtained before they can be built. The correct set of submodules can be initialised using (with PYBV11 as an example of the selected board):

$ make BOARD=PYBV11 submodules

Then to build the board's firmware run:

$ make BOARD=PYBV11

The above command should produce binary images in the build-PYBV11/ subdirectory (or the equivalent directory for the board specified).

Note that some boards require the mboot bootloader to be built and deployed before flashing the main firmware. For such boards an information message about this will be printed at the end of the main firmware build. Mboot can be built via:

$ make -C mboot BOARD=STM32F769DISC

For more information about mboot see mboot/README.md.

Link Time Optimization

Link Time Optimization (LTO) reduces the firmware binary size when enabled (typically 2-3% smaller). However it may make build time longer, particularly on older GCC versions.

Currently LTO is enabled by default for some smaller STM32 boards with less flash, but disabled on other boards.

To enable LTO, pass LTO=1 on the command line:

$ make BOARD=boardname LTO=1

To disable LTO, pass LTO=0 in the same way.

Note that make clean BOARD=boardname will be needed before changing the LTO setting of a firmware that is already built.

Flashing the Firmware using DFU mode

You must then get your board/microcontroller into DFU (Device Firmware Update) mode.

If you already have MicroPython installed on the board you can do that by calling machine.bootloader() on the board, either at the REPL or using pyboard.py. A nice property of this approach is that you can automate it so you can update the board without manually pressing any buttons.

If you do not have MicroPython running yet, temporarily jumper your board's DFU pin (typ. BOOT0) to 3.3v and reset or power-on the board.

On a pyboard the P1/DFU pin and a 3.3v pin are next to each other (on the bottom left of the board, second row from the bottom) and the reset button is labeled RST.

For the pyboard D-series you can enter the mboot DFU bootloader by holding down the USR button, pressing and releasing the RST button, and continuing to hold down USR until the LED is white (4th in the cycle), then let go of USR while the LED is white. The LED will then flash red once per second to indicate it is in USB DFU mode.

Once the board is in DFU mode, flash the firmware using the command:

$ make BOARD=PYBV11 deploy

This will use the included tools/pydfu.py script. You can use instead the dfu-util program (available here) by passing USE_PYDFU=0:

$ make BOARD=PYBV11 USE_PYDFU=0 deploy

If flashing the firmware does not work it may be because you don't have the correct permissions. Try then:

$ sudo make BOARD=PYBV11 deploy

Or using dfu-util directly:

$ sudo dfu-util -a 0 -d 0483:df11 -D build-PYBV11/firmware.dfu

Flashing the Firmware with stlink

ST Discovery or Nucleo boards have a builtin programmer called ST-LINK. With these boards and using Linux or OS X, you have the option to upload the stm32 firmware using the st-flash utility from the stlink project. To do so, connect the board with a mini USB cable to its ST-LINK USB port and then use the make target deploy-stlink. For example, if you have the STM32F4DISCOVERY board, you can run:

$ make BOARD=STM32F4DISC deploy-stlink

The st-flash program should detect the USB connection to the board automatically. If not, run lsusb to determine its USB bus and device number and set the STLINK_DEVICE environment variable accordingly, using the format <USB_BUS>:<USB_ADDR>. Example:

$ lsusb
[...]
Bus 002 Device 035: ID 0483:3748 STMicroelectronics ST-LINK/V2
$ export STLINK_DEVICE="002:0035"
$ make BOARD=STM32F4DISC deploy-stlink

Flashing the Firmware with OpenOCD

Another option to deploy the firmware on ST Discovery or Nucleo boards with a ST-LINK interface uses OpenOCD. Connect the board with a mini USB cable to its ST-LINK USB port and then use the make target deploy-openocd. For example, if you have the STM32F4DISCOVERY board:

$ make BOARD=STM32F4DISC deploy-openocd

The openocd program, which writes the firmware to the target board's flash, is configured via the file ports/stm32/boards/openocd_stm32f4.cfg. This configuration should work for all boards based on a STM32F4xx MCU with a ST-LINKv2 interface. You can override the path to this configuration by setting OPENOCD_CONFIG in your Makefile or on the command line.

Accessing the board

Once built and deployed, access the MicroPython REPL (the Python prompt) via USB serial or UART, depending on the board. There are many ways to do this, one of which is via mpremote (install it using pip install mpremote):

$ mpremote

Other options are picocom and screen, for example:

$ picocom /dev/ttyACM0