micropython/ports/rp2
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 rp2/boards: Add SIL_RP2040_SHIM board by Silicognition LLC. 2023-12-18 16:32:20 +11:00
btstack_inc rp2: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
lwip_inc rp2/lwip_inc: Enable lwIP status callback. 2022-06-30 16:03:21 +10:00
mbedtls extmod/mbedtls: Enable certificate time/date validation by default. 2023-12-01 15:08:11 +11:00
modules all: Update Python formatting to ruff-format. 2023-11-03 13:30:41 +11:00
CMakeLists.txt ports: Switch build to use common lib/libm list of source files. 2023-12-08 15:42:41 +11:00
Makefile py/mkrules.mk: Add MICROPY_PREVIEW_VERSION_2. 2023-10-27 15:28:46 +11:00
README.md rp2/README: Fix name of RPI_PICO_W board. 2023-10-02 11:46:38 +11:00
cyw43_configport.h rp2: Switch rp2 and drivers to use new event functions. 2023-12-08 12:49:43 +11:00
fatfs_port.c rp2/fatfs_port: Fix the modification date of files. 2022-10-12 00:31:38 +11:00
help.c ports: Standardise docs link in help text. 2023-06-02 11:48:46 +10:00
libm_extra.c rp2: Switch to locally provided math library. 2023-12-06 14:00:07 +11:00
machine_adc.c rp2/machine_adc: Refactor channel/pin validation code. 2023-11-03 18:20:31 +11:00
machine_bitstream.c rp2/machine_bitstream: Implement the machine.bitstream driver. 2021-11-17 10:01:15 +11:00
machine_i2c.c rp2: Add new NO_DEFAULT_PINS config options for i2c, spi, and uart. 2023-12-18 14:07:03 +11:00
machine_i2s.c extmod/machine_i2s: Factor comments, some enums and macros. 2023-10-20 17:40:17 +11:00
machine_pin.c tools/boardgen.py: Add initial implementation of a common make-pins.py. 2023-11-03 14:06:27 +11:00
machine_pin.h tools/boardgen.py: Add initial implementation of a common make-pins.py. 2023-11-03 14:06:27 +11:00
machine_pin_cyw43.c rp2/machine_pin: Add support for named pins and alternate functions. 2023-01-16 11:44:26 +11:00
machine_pwm.c extmod/machine_pwm: Remove header file and move decls to .c file. 2023-10-20 16:24:46 +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 rp2: Add new NO_DEFAULT_PINS config options for i2c, spi, and uart. 2023-12-18 14:07:03 +11:00
machine_timer.c rp2/machine_timer: Fix printing of timer period. 2023-09-02 17:09:51 +10:00
machine_uart.c rp2: Add new NO_DEFAULT_PINS config options for i2c, spi, and uart. 2023-12-18 14:07:03 +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 rp2/main: Enable SEVONPEND CPU interrupt bit. 2023-12-08 12:49:49 +11:00
memmap_mp.ld all: Fix spelling mistakes based on codespell check. 2023-04-27 18:03:06 +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 all: Rename *umodule*.c to remove the "u" prefix. 2023-06-08 17:54:17 +10:00
modrp2.c ports: Move definitions of ATOMIC_SECTION macros to mphalport.h. 2023-12-01 14:37:48 +11:00
modrp2.h rp2: Add new port to Raspberry Pi RP2 microcontroller. 2021-01-30 00:42:29 +11:00
modtime.c all: Rename *umodule*.c to remove the "u" prefix. 2023-06-08 17:54:17 +10:00
mpbthciport.c rp2/mpbthciport: Rework HCI polling timer to use soft_timer. 2023-11-29 16:23:52 +11:00
mpbthciport.h rp2: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
mpbtstackport.c rp2: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
mpconfigport.h rp2/mpconfigport: Enable MICROPY_PY_OS_DUPTERM_NOTIFY. 2023-12-18 12:20:47 +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
mpnetworkport.c rp2/mpnetworkport: Rework lwIP polling to use soft_timer. 2023-11-29 16:25:20 +11:00
mpnetworkport.h stm32: Update to use the open-source lib version of cyw43-driver. 2023-03-01 01:27:12 +11:00
mpnimbleport.c rp2/mpbthciport: Rework HCI polling timer to use soft_timer. 2023-11-29 16:23:52 +11:00
mpnimbleport.h rp2: Add support for bluetooth module using NimBLE. 2021-09-19 23:09:59 +10:00
mpthreadport.c ports: Move definitions of ATOMIC_SECTION macros to mphalport.h. 2023-12-01 14:37:48 +11:00
mpthreadport.h rp2/mpthreadport: Add mp_thread_deinit to reset core1 on soft reset. 2021-05-09 00:08:30 +10:00
msc_disk.c rp2/msc_disk: Allow configuring the USB MSC inquiry response. 2023-09-29 16:36:45 +10:00
pendsv.c rp2/pendsv: Add MICROPY_BOARD_PENDSV_ENTRIES for board customisation. 2023-03-07 15:59:37 +11:00
pendsv.h rp2/mpnetworkport: Rework lwIP polling to use soft_timer. 2023-11-29 16:25:20 +11:00
qstrdefsport.h rp2: Add new port to Raspberry Pi RP2 microcontroller. 2021-01-30 00:42:29 +11:00
rp2_flash.c rp2: Switch rp2 and drivers to use new event functions. 2023-12-08 12:49:43 +11:00
rp2_pio.c rp2: Switch rp2 and drivers to use new event functions. 2023-12-08 12:49:43 +11:00
uart.c all: Rename mp_keyboard_interrupt to mp_sched_keyboard_interrupt. 2021-04-30 15:13:43 +10:00
uart.h rp2: Add new port to Raspberry Pi RP2 microcontroller. 2021-01-30 00:42:29 +11:00
usbd.c shared/tinyusb: Add a helper for hex string conversion. 2023-11-16 14:59:48 +11:00

README.md

The RP2 port

This is a port of MicroPython to the Raspberry Pi RP2 series of microcontrollers. Currently supported features are:

  • REPL over USB VCP, and optionally over UART (on GP0/GP1).
  • Filesystem on the internal flash, using littlefs2.
  • Support for native code generation and inline assembler.
  • time module with sleep, time and ticks functions.
  • os module with VFS support.
  • machine module with the following classes: Pin, ADC, PWM, I2C, SPI, SoftI2C, SoftSPI, Timer, UART, WDT.
  • rp2 module with programmable IO (PIO) support.

See the examples/rp2/ directory for some example code.

Building

The MicroPython cross-compiler must be built first, which will be used to pre-compile (freeze) built-in Python code. This 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/rp2/ directory.

Building of the RP2 firmware is done entirely using CMake, although a simple Makefile is also provided as a convenience. To build the firmware run (from this directory):

$ make submodules
$ make clean
$ make

You can also build the standard CMake way. The final firmware is found in the top-level of the CMake build directory (build by default) and is called firmware.uf2.

If you are using a different board other than a Rasoberry Pi Pico, then you should pass the board name to the build; e.g. for Raspberry Pi Pico W:

$ make BOARD=RPI_PICO_W submodules
$ make BOARD=RPI_PICO_W clean
$ make BOARD=RPI_PICO_W

Deploying firmware to the device

Firmware can be deployed to the device by putting it into bootloader mode (hold down BOOTSEL while powering on or resetting) and then copying firmware.uf2 to the USB mass storage device that appears.

If MicroPython is already installed then the bootloader can be entered by executing import machine; machine.bootloader() at the REPL.

Sample code

The following samples can be easily run on the board by entering paste mode with Ctrl-E at the REPL, then cut-and-pasting the sample code to the REPL, then executing the code with Ctrl-D.

Blinky

This blinks the on-board LED on the Pico board at 1.25Hz, using a Timer object with a callback.

from machine import Pin, Timer
led = Pin(25, Pin.OUT)
tim = Timer()
def tick(timer):
    global led
    led.toggle()

tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick)

PIO blinky

This blinks the on-board LED on the Pico board at 1Hz, using a PIO peripheral and PIO assembler to directly toggle the LED at the required rate.

from machine import Pin
import rp2

@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def blink_1hz():
    # Turn on the LED and delay, taking 1000 cycles.
    set(pins, 1)
    set(x, 31)                  [6]
    label("delay_high")
    nop()                       [29]
    jmp(x_dec, "delay_high")

    # Turn off the LED and delay, taking 1000 cycles.
    set(pins, 0)
    set(x, 31)                  [6]
    label("delay_low")
    nop()                       [29]
    jmp(x_dec, "delay_low")

# Create StateMachine(0) with the blink_1hz program, outputting on Pin(25).
sm = rp2.StateMachine(0, blink_1hz, freq=2000, set_base=Pin(25))
sm.active(1)

See the examples/rp2/ directory for further example code.