micropython/ports/minimal
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
..
Makefile minimal/Makefile: Set linker to $(CC). 2022-11-15 17:09:37 +11:00
README.md minimal/README: Update text to better describe what "make run" does. 2018-01-31 21:05:21 +11:00
main.c py/builtinevex: Handle invalid filenames for execfile. 2023-10-12 15:17:59 +11:00
mpconfigport.h {mimxrt,powerpc,samd}/mpconfigport: Don't override parse chunk alloc. 2023-09-29 14:11:26 +10:00
mphalport.h all: Reformat C and Python source code with tools/codeformat.py. 2020-02-28 10:33:03 +11:00
qstrdefsport.h all: Add *FORMAT-OFF* in various places. 2020-02-28 10:31:07 +11:00
stm32f405.ld all: Fix spelling mistakes based on codespell check. 2023-04-27 18:03:06 +10:00
uart_core.c ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00

README.md

The minimal port

This port is intended to be a minimal MicroPython port that actually runs. It can run under Linux (or similar) and on any STM32F4xx MCU (eg the pyboard).

Building and running Linux version

By default the port will be built for the host machine:

$ make

To run the executable and get a basic working REPL do:

$ make run

Building for an STM32 MCU

The Makefile has the ability to build for a Cortex-M CPU, and by default includes some start-up code for an STM32F4xx MCU and also enables a UART for communication. To build:

$ make CROSS=1

If you previously built the Linux version, you will need to first run make clean to get rid of incompatible object files.

Building will produce the build/firmware.dfu file which can be programmed to an MCU using:

$ make CROSS=1 deploy

This version of the build will work out-of-the-box on a pyboard (and anything similar), and will give you a MicroPython REPL on UART1 at 9600 baud. Pin PA13 will also be driven high, and this turns on the red LED on the pyboard.

Building without the built-in MicroPython compiler

This minimal port can be built with the built-in MicroPython compiler disabled. This will reduce the firmware by about 20k on a Thumb2 machine, and by about 40k on 32-bit x86. Without the compiler the REPL will be disabled, but pre-compiled scripts can still be executed.

To test out this feature, change the MICROPY_ENABLE_COMPILER config option to "0" in the mpconfigport.h file in this directory. Then recompile and run the firmware and it will execute the frozentest.py file.