micropython/ports/samd
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 samd/boards/make-pins.py: Update to use tools/boardgen.py. 2023-11-03 14:18:32 +11:00
mcu samd: Switch to shared TinyUSB implementation. 2023-11-16 14:54:29 +11:00
modules samd/modules/_boot.py: Add /lib to sys.path. 2023-09-02 00:09:26 +10:00
Makefile ports: Switch build to use common lib/libm list of source files. 2023-12-08 15:42:41 +11:00
README.md samd/README: Update README to reflect recent changes to the port. 2022-11-08 23:25:39 +11:00
clock_config.h samd: Change the symbol names for the peripheral clocks. 2022-10-06 23:10:08 +11:00
fatfs_port.c samd/machine_rtc: Add the machine.RTC class. 2022-10-25 23:20:09 +11:00
help.c ports: Standardise docs link in help text. 2023-06-02 11:48:46 +10:00
machine_adc.c samd/boards/make-pins.py: Update to use tools/boardgen.py. 2023-11-03 14:18:32 +11:00
machine_bitstream.c samd/machine_bitstream: Add the machine.bitstream() function. 2022-10-06 23:00:00 +11:00
machine_dac.c samd/boards/make-pins.py: Update to use tools/boardgen.py. 2023-11-03 14:18:32 +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_pin.c samd/boards/make-pins.py: Update to use tools/boardgen.py. 2023-11-03 14:18:32 +11:00
machine_pwm.c samd/boards/make-pins.py: Update to use tools/boardgen.py. 2023-11-03 14:18:32 +11:00
machine_rtc.c extmod/modmachine: Clean up decls of machine types to use common ones. 2023-10-26 16:20:53 +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 samd/machine_uart: Add machine_uart_set_baudrate() function. 2023-11-06 11:10:11 +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 samd/modmachine: Make some machine classes configurable by #defines. 2023-05-22 18:39:07 +10:00
modmachine.c extmod/modmachine: Provide common implementation of disable/enable_irq. 2023-11-30 16:11:11 +11:00
modmachine.h extmod/modmachine: Provide common Python bindings for bootloader(). 2023-11-30 16:11:11 +11:00
modos.c extmod/modos: Factor os.dupterm_notify() function to common extmod code. 2023-12-18 12:20:47 +11:00
modsamd.c samd: Adapt existing samd.Flash and integrate with (Q)SPI flash in boot. 2023-06-06 00:49:36 +10:00
modtime.c all: Rename *umodule*.c to remove the "u" prefix. 2023-06-08 17:54:17 +10:00
mpconfigport.h ports: Move definitions of ATOMIC_SECTION macros to mphalport.h. 2023-12-01 14:37:48 +11:00
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
pendsv.c samd/machine_timer: Add machine.Timer based on the shared soft-timer. 2022-10-06 22:52:39 +11:00
pendsv.h samd/pendsv: Add MICROPY_BOARD_PENDSV_ENTRIES for board customisation. 2023-03-07 15:59:58 +11:00
pin_af.c samd/pin_af: Fix a typo in a conditional compile. 2023-11-06 11:11:18 +11:00
pin_af.h samd/boards/make-pins.py: Update to use tools/boardgen.py. 2023-11-03 14:18:32 +11:00
qstrdefsport.h
samd_flash.c samd: Adapt existing samd.Flash and integrate with (Q)SPI flash in boot. 2023-06-06 00:49:36 +10:00
samd_isr.c samd: Switch TinyUSB to run via a scheduled task. 2023-11-09 12:30:04 +11:00
samd_qspiflash.c extmod/modmachine: Consolidate mem, i2c and spi headers to modmachine.h. 2023-10-26 17:40:22 +11:00
samd_soc.c samd: Use unique id for USB serial number. 2023-11-16 15:03:09 +11:00
samd_soc.h samd: Use unique id for USB serial number. 2023-11-16 15:03:09 +11:00
samd_spiflash.c extmod/modmachine: Consolidate mem, i2c and spi headers to modmachine.h. 2023-10-26 17:40:22 +11:00
sections.ld
usbd.c samd: Use unique id for USB serial number. 2023-11-16 15:03:09 +11:00

README.md

Port of MicroPython to Microchip SAMD MCUs

Supports SAMD21 and SAMD51. For each supported device there is a subdirectory in the boards/ directory.

The entry point for the specific port documentation is at https://docs.micropython.org/en/latest/samd/quickref.html, which also shows the assignment of IO-Functions to pins. The generic MicroPython documentation applies for anything not specific for the SAM port.

Due to the different flash sizes of SAMD21 and SAMD51 devices, the coverage of MicroPython modules differ. Use help("modules") to tell, which MicroPython modules are provided.

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. There is no default board. 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 ADAFRUIT_ITSYBITSY_M4_EXPRESS as an example of the selected board):

$ make BOARD=ADAFRUIT_ITSYBITSY_M4_EXPRESS submodules

Then to build the board's firmware run:

$ make BOARD=ADAFRUIT_ITSYBITSY_M4_EXPRESS clean
$ make BOARD=ADAFRUIT_ITSYBITSY_M4_EXPRESS

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

Flashing the Firmware

Most SAMD21 and SAMD51 boards have a built in firmware loader. To start it, push the reset button of the boards twice. The speed varies a little bit. If the firmware loader starts, a drive will appear in the file manager of your PC. Copy the created firmware.uf2 file to that drive. If the upload is finished, the drive will disappear and the board will reboot.