micropython/ports/stm32
Maarten van der Schrieck 3bca93b2d0 ports: Fix sys.stdout.buffer.write() return value.
MicroPython code may rely on the return value of sys.stdout.buffer.write()
to reflect the number of bytes actually written. While in most scenarios a
write() operation is successful, there are cases where it fails, leading to
data loss. This problem arises because, currently, write() merely returns
the number of bytes it was supposed to write, without indication of
failure.

One scenario where write() might fail, is where USB is used and the
receiving end doesn't read quickly enough to empty the receive buffer. In
that case, write() on the MicroPython side can timeout, resulting in the
loss of data without any indication, a behavior observed notably in
communication between a Pi Pico as a client and a Linux host using the ACM
driver.

A complex issue arises with mp_hal_stdout_tx_strn() when it involves
multiple outputs, such as USB, dupterm and hardware UART. The challenge is
in handling cases where writing to one output is successful, but another
fails, either fully or partially. This patch implements the following
solution:

mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible
destinations for that data, and returns the minimum successful write
length.

The implementation of this is complicated by several factors:
- multiple outputs may be enabled or disabled at compiled time
- multiple outputs may be enabled or disabled at runtime
- mp_os_dupterm_tx_strn() is one such output, optionally containing
  multiple additional outputs
- each of these outputs may or may not be able to report success
- each of these outputs may or may not be able to report partial writes

As a result, there's no single strategy that fits all ports, necessitating
unique logic for each instance of mp_hal_stdout_tx_strn().

Note that addressing sys.stdout.write() is more complex due to its data
modification process ("cooked" output), and it remains unchanged in this
patch. Developers who are concerned about accurate return values from
write operations should use sys.stdout.buffer.write().

This patch might disrupt some existing code, but it's also expected to
resolve issues, considering that the peculiar return value behavior of
sys.stdout.buffer.write() is not well-documented and likely not widely
known. Therefore, it's improbable that much existing code relies on the
previous behavior.

Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
2023-12-22 10:32:46 +11:00
..
boards stm32/machine_i2s: Improve accuracy of SCK frequency. 2023-12-06 11:48:03 +11:00
lwip_inc stm32: Add optional lwip loopback support. 2023-11-17 13:37:04 +11:00
mbedtls extmod/mbedtls: Enable certificate time/date validation by default. 2023-12-01 15:08:11 +11:00
mboot stm32/mboot: Add support for Microsoft WCID. 2023-12-20 19:40:03 +11:00
usbdev stm32/usbdev: Optionally pass through vendor requests to Setup function. 2023-12-20 17:25:10 +11:00
usbhost
Makefile ports: Switch build to use common lib/libm list of source files. 2023-12-08 15:42:41 +11:00
README.md
accel.c
accel.h
adc.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
adc.h stm32: Add initial support for H5 MCUs. 2023-06-15 11:09:20 +10:00
autoflash
boardctrl.c
boardctrl.h
bufhelper.c
bufhelper.h
can.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
can.h
cyw43_configport.h extmod/modnetwork: Forward if.config(hostname) to network.hostname. 2023-10-04 12:39:51 +11:00
dac.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
dac.h
dma.c stm32/sdcard: Add SD card support for H5 MCUs. 2023-11-17 14:17:37 +11:00
dma.h stm32/dac: Add STM32H5 DAC support, with dma_nohal implementation. 2023-09-28 17:11:37 +10:00
eth.c stm32/eth: Add Ethernet support for H5 MCUs. 2023-10-17 11:40:54 +11:00
eth.h
extint.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
extint.h stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
factoryreset.c ports: Standardise docs link in help text. 2023-06-02 11:48:46 +10:00
factoryreset.h
fatfs_port.c
fdcan.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
flash.c stm32: Add initial support for H5 MCUs. 2023-06-15 11:09:20 +10:00
flash.h
flashbdev.c
gccollect.c
gccollect.h
help.c stm32/help: Exclude legacy entries from help, and adjust constant names. 2023-06-02 12:43:58 +10:00
i2c.c stm32: Add missing header include for debug builds. 2023-11-28 23:32:52 +11:00
i2c.h
i2cslave.c
i2cslave.h stm32: Add initial support for H5 MCUs. 2023-06-15 11:09:20 +10:00
irq.c
irq.h ports: Move definitions of ATOMIC_SECTION macros to mphalport.h. 2023-12-01 14:37:48 +11:00
lcd.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
lcd.h
led.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
led.h
machine_adc.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
machine_bitstream.c extmod/modmachine: Consolidate simple machine headers into modmachine.h. 2023-10-26 16:49:27 +11:00
machine_i2c.c extmod/modmachine: Consolidate mem, i2c and spi headers to modmachine.h. 2023-10-26 17:40:22 +11:00
machine_i2s.c stm32/machine_i2s: Improve accuracy of SCK frequency. 2023-12-06 11:48:03 +11:00
machine_spi.c extmod/modmachine: Consolidate mem, i2c and spi headers to modmachine.h. 2023-10-26 17:40:22 +11:00
machine_uart.c extmod/machine_uart: Factor ports' UART Python bindings to common code. 2023-10-26 10:46:42 +11:00
machine_wdt.c extmod/machine_wdt: Factor ports' WDT Python bindings to common code. 2023-10-20 15:36:09 +11:00
main.c extmod/machine_uart: Factor ports' UART Python bindings to common code. 2023-10-26 10:46:42 +11:00
make-stmconst.py stm32/boards/make-pins.py: Don't generate qstrs. 2023-11-03 13:59:19 +11:00
modmachine.c extmod/modmachine: Provide common bindings for 6 bare-metal functions. 2023-11-30 16:11:11 +11:00
modmachine.h extmod/modmachine: Provide common bindings for 6 bare-metal functions. 2023-11-30 16:11:11 +11:00
modos.c extmod/machine_uart: Factor ports' UART Python bindings to common code. 2023-10-26 10:46:42 +11:00
modpyb.c extmod/modmachine: Provide common Python bindings for machine.idle(). 2023-11-30 16:11:11 +11:00
modstm.c extmod/modmachine: Consolidate mem, i2c and spi headers to modmachine.h. 2023-10-26 17:40:22 +11:00
modtime.c all: Rename *umodule*.c to remove the "u" prefix. 2023-06-08 17:54:17 +10:00
mpbthciport.c extmod/machine_uart: Factor ports' UART Python bindings to common code. 2023-10-26 10:46:42 +11:00
mpbthciport.h
mpbtstackport.c extmod/btstack: Switch to use hci_dump_init instead of hci_dump_open. 2023-04-22 00:39:31 +10:00
mpbtstackport.h
mpconfigboard_common.h extmod/machine_i2s: Factor ports' I2S Python bindings to common code. 2023-10-20 17:39:57 +11:00
mpconfigport.h ports: Move definitions of ATOMIC_SECTION macros to mphalport.h. 2023-12-01 14:37:48 +11:00
mpconfigport.mk
mpconfigport_nanbox.h
mphalport.c ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
mphalport.h ports: Move definitions of ATOMIC_SECTION macros to mphalport.h. 2023-12-01 14:37:48 +11:00
mpnetworkport.c stm32: Add optional lwip loopback support. 2023-11-17 13:37:04 +11:00
mpnimbleport.c
mpnimbleport.h
mpthreadport.c py/modthread: Return thread id from start_new_thread(). 2023-09-03 18:49:18 +10:00
mpthreadport.h
mpu.h ports: Move definitions of ATOMIC_SECTION macros to mphalport.h. 2023-12-01 14:37:48 +11:00
network_lan.c
octospi.c stm32/octospi: Add support for dual-line SPI interface. 2023-09-13 16:48:15 +10:00
octospi.h stm32/octospi: Add preliminary support for OCTOSPI peripheral. 2023-06-15 11:09:37 +10:00
pendsv.c all: Fix spelling mistakes based on codespell check. 2023-04-27 18:03:06 +10:00
pendsv.h
pin.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
pin.h stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
pin_defs_stm32.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
pin_defs_stm32.h stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
pin_named_pins.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
pin_static_af.h stm32: Add missing header include for debug builds. 2023-11-28 23:32:52 +11:00
portmodules.h all: Rename mp_umodule*, mp_module_umodule* to remove the "u" prefix. 2023-06-08 17:54:04 +10:00
powerctrl.c extmod/modmachine: Provide common bindings for 6 bare-metal functions. 2023-11-30 16:11:11 +11:00
powerctrl.h extmod/modmachine: Provide common bindings for 6 bare-metal functions. 2023-11-30 16:11:11 +11:00
powerctrlboot.c stm32: Add STM32H5 support for sleep mode. 2023-11-03 17:14:05 +11:00
pyb_can.c all: Fix spelling mistakes based on codespell check. 2023-04-27 18:03:06 +10:00
pyb_i2c.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
pyb_spi.c extmod/modmachine: Consolidate mem, i2c and spi headers to modmachine.h. 2023-10-26 17:40:22 +11:00
pybcdc.inf_template
pybthread.c
pybthread.h
qspi.c stm32/qspi: Allow qspi_write_cmd_data to write cmd with 1 data byte. 2023-07-13 12:49:51 +10:00
qspi.h
qstrdefsport.h all: Rename mod_umodule*, ^umodule* to remove the "u" prefix. 2023-06-08 17:54:07 +10:00
resethandler.s
resethandler_m0.s
resethandler_m3.s
rfcore.c all: Fix spelling mistakes based on codespell check. 2023-04-27 18:03:06 +10:00
rfcore.h
rng.c stm32: Add initial support for H5 MCUs. 2023-06-15 11:09:20 +10:00
rng.h
rtc.c stm32: Add STM32H5 support for sleep mode. 2023-11-03 17:14:05 +11:00
rtc.h
sdcard.c stm32/sdcard: Add SD card support for H5 MCUs. 2023-11-17 14:17:37 +11:00
sdcard.h
sdio.c
sdio.h
sdram.c all: Fix spelling mistakes based on codespell check. 2023-04-27 18:03:06 +10:00
sdram.h
servo.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
servo.h
spi.c stm32: Add missing header include for debug builds. 2023-11-28 23:32:52 +11:00
spi.h extmod/modmachine: Clean up decls of machine types to use common ones. 2023-10-26 16:20:53 +11:00
spibdev.c
stm32.mk stm32: Add initial support for H5 MCUs. 2023-06-15 11:09:20 +10:00
stm32_it.c stm32/uart: Add support for UART10 when it's a USART. 2023-09-13 16:48:15 +10:00
stm32_it.h
storage.c
storage.h
subghz.c stm32/subghz: Add STM32WL55 subghz radio interface to stm module. 2023-08-23 11:58:11 +10:00
subghz.h stm32/subghz: Add STM32WL55 subghz radio interface to stm module. 2023-08-23 11:58:11 +10:00
system_stm32.c stm32: Add configuration options for analog switches. 2023-11-03 16:27:10 +11:00
systick.c all: Fix spelling mistakes based on codespell check. 2023-04-27 18:03:06 +10:00
systick.h
timer.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
timer.h
uart.c stm32: Rename pin_obj_t to machine_pin_obj_t. 2023-11-03 14:09:08 +11:00
uart.h extmod/machine_uart: Factor ports' UART Python bindings to common code. 2023-10-26 10:46:42 +11:00
ulpi.c
ulpi.h
usb.c py/stream: Add mp_stream___exit___obj that calls mp_stream_close. 2023-07-21 18:49:03 +10:00
usb.h
usbd_cdc_interface.c stm32/usbd_cdc_interface: Include header to get machine_bootloader decl. 2023-12-04 22:19:59 +11:00
usbd_cdc_interface.h
usbd_conf.c stm32: Add USB support for STM32L1 MCUs. 2023-06-27 22:28:31 +09:00
usbd_conf.h stm32/usbdev: Optionally pass through vendor requests to Setup function. 2023-12-20 17:25:10 +11:00
usbd_desc.c all: Fix spelling mistakes based on codespell check. 2023-04-27 18:03:06 +10:00
usbd_desc.h
usbd_hid_interface.c
usbd_hid_interface.h
usbd_msc_interface.c stm32/usbd_msc_interface: Allow configuring the MSC inquiry response. 2023-09-29 16:35:48 +10:00
usbd_msc_interface.h
usrsw.c
usrsw.h

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, STM32H7, STM32L0, STM32L4 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.

The STM32H7 series has preliminary support: there is a working REPL via USB and UART, as well as very basic peripheral support, but some things do not work and none of the advanced features of the STM32H7 are yet supported, such as the clock tree. At this point the STM32H7 should be considered as a fast version of the STM32F7.

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 (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

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