micropython/ports/rp2
Alessandro Gatti cf0ef5a9d7 rp2/rp2_dma: Reduce footprint of the DMA control fields table.
This commit shortens the amount of space taken by the DMA control fields
table, and explicitly marks it as `const`.

The DMA fields info table used a full-size QSTR index value, and 9 bits
of numeric information.  Given that the QSTR index could be converted
into a `qstr_short_t`, there is no fields spill outside a machine word
boundary - albeit with having 7 unused bits but there isn't much that
can be done for that.  The effective structure size for each entry is
halved, from 8 bytes down to 4.

Also, the structure is only read from, yet it was not marked as `const`.
Marking the structure as constant did not help reduce the final size but
at least correctly signals the compiler that no write accesses are
possible.

This shrinks the RPI_PICO/RPI_PICO_W build by 56 bytes, with a similar
size reduction for RPI_PICO2/RPI_PICO2_W.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2026-04-15 16:03:38 +10:00
..
boards rp2/boards/SEEED_XIAO_RP2040: Add XIAO RP2040 board definition. 2026-03-25 23:48:48 +11:00
btstack_inc rp2: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
lwip_inc rp2: Switch all RNG sources from ROSC to pico_rand. 2026-02-24 18:34:30 +11:00
mbedtls extmod/mbedtls: Factor out mbedtls_hardware_poll() to common code. 2026-03-03 23:43:42 +11:00
modules rp2/modules/rp2.py: Don't corrupt globals on asm_pio() exception. 2026-03-05 00:07:14 +11:00
clocks_extra.c rp2/modmachine: Do not use deprecated XOSC_MHZ and XOSC_KHZ. 2025-07-25 11:25:24 +10:00
clocks_extra.h rp2/clocks_extra: Update runtime_clocks_init based on new pico-sdk. 2024-10-15 12:15:48 +11:00
CMakeLists.txt extmod/mbedtls: Factor out mbedtls_hardware_poll() to common code. 2026-03-03 23:43:42 +11:00
cyw43_configport.h rp2,extmod/cyw43: Move the LWIP responder fix into common CYW43 config. 2025-05-08 15:32:05 +10:00
datetime_patch.c rp2/datetime_patch: Fix year and month offset for mktime wrapper. 2024-10-24 11:22:59 +11:00
fatfs_port.c rp2: Integrate RP2350 and use aon_timer instead of rtc API. 2024-10-15 12:07:59 +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: Add ADC support for RP2350B. 2024-10-15 12:16:46 +11:00
machine_bitstream.c rp2/machine_bitstream: Tweak MP_HAL_BITSTREAM_NS_OVERHEAD for RP2350. 2025-01-11 14:23:51 +11:00
machine_i2c_target.c rp2/machine_i2c_target: Implement I2CTarget class. 2025-08-01 23:03:17 +10:00
machine_i2c.c rp2/machine_i2c: Factor default pin macros to header file. 2025-08-01 23:03:17 +10:00
machine_i2c.h rp2/machine_i2c: Factor default pin macros to header file. 2025-08-01 23:03:17 +10:00
machine_i2s.c rp2/machine_i2s: Deinit all active I2S instances on soft reset. 2024-08-07 12:01:34 +10:00
machine_pin_cyw43.c rp2: Add exception text wrappers. 2025-04-22 11:29:36 +10:00
machine_pin.c rp2: Add HSTX alternate function. 2025-12-01 22:19:32 +11:00
machine_pin.h rp2/machine_pin: Reduce footprint of pin structures. 2026-01-04 11:48:11 +11:00
machine_pwm.c rp2/machine_pwm: Add RP2350 slices to machine.PWM. 2024-10-15 12:19:52 +11:00
machine_rtc.c rp2: Integrate RP2350 and use aon_timer instead of rtc API. 2024-10-15 12:07:59 +11:00
machine_spi.c rp2/machine_spi: Allow MISO to be unspecified. 2025-03-26 17:08:08 +11:00
machine_timer.c rp2/machine_timer: Use mp_irq_dispatch() to reduce duplication. 2025-09-25 23:59:24 +10:00
machine_uart.c rp2/machine_uart: Fix unintended UART buffer allocation on init(). 2025-03-27 11:37:57 +11:00
machine_wdt.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +11:00
main.c rp2: Switch all RNG sources from ROSC to pico_rand. 2026-02-24 18:34:30 +11:00
Makefile rp2: Allow setting MICROPY_C_HEAP_SIZE from make command line. 2026-04-15 15:25:50 +10:00
memmap_mp_rp2040.ld rp2: Fix linker scripts to match SDK 2.2.0. 2026-02-03 12:58:53 +11:00
memmap_mp_rp2350.ld rp2: Fix linker scripts to match SDK 2.2.0. 2026-02-03 12:58:53 +11:00
modmachine.c rp2/modmachine: Do not use deprecated XOSC_MHZ and XOSC_KHZ. 2025-07-25 11:25:24 +10:00
modmachine.h rp2/machine_uart: Implement a Python UART IRQ handler. 2024-08-29 16:27:43 +10:00
modrp2.c rp2/modrp2: Fix rp2.bootsel_button() function for RP2350. 2025-03-13 13:54:43 +11:00
modrp2.h rp2/rp2_dma: Introduce a new rp2.DMA class for control over DMA xfers. 2023-12-22 13:04:51 +11:00
modtime.c extmod/modtime: Move tuple creation to common localtime implementation. 2025-08-15 12:09:13 +10:00
mpbthciport.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +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 py/mpconfig: Automatically configure thumb2 and float emitter features. 2026-04-15 15:49:44 +10:00
mphalport.c ports: Refactor os.urandom(). 2026-02-24 23:36:02 +11:00
mphalport.h extmod,rp2: Keep LWIP timer running if lwip poll_sockets() is called. 2026-03-04 11:44:30 +11:00
mpnetworkport.c extmod,rp2: Keep LWIP timer running if lwip poll_sockets() is called. 2026-03-04 11:44:30 +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 rp2/pendsv: Account for PendSV running on both cores, and without CYW43. 2025-03-27 00:07:50 +11:00
mpthreadport.h rp2: Migrate to the new mp_thread_recursive_mutex_t. 2025-02-03 15:02:02 +11:00
msc_disk.c rp2/msc_disk: Allow configuring the USB MSC inquiry response. 2023-09-29 16:36:45 +10:00
mutex_extra.c rp2: Fix power consumption when sleeping with a timeout. 2024-07-23 16:01:42 +10:00
mutex_extra.h all: Use "static inline" consistently in function definitions. 2025-10-02 16:30:52 +10:00
pendsv.c rp2/pendsv: Fix PendSV_Handler dispatch check when threading enabled. 2025-11-19 16:35:10 +11:00
pendsv.h rp2/pendsv: Account for PendSV running on both cores, and without CYW43. 2025-03-27 00:07:50 +11:00
qstrdefsport.h rp2: Add new port to Raspberry Pi RP2 microcontroller. 2021-01-30 00:42:29 +11:00
README.md rp2/Makefile: Add deploy target that uses picotool load. 2025-05-09 12:01:50 +10:00
rp2_dma.c rp2/rp2_dma: Reduce footprint of the DMA control fields table. 2026-04-15 16:03:38 +10:00
rp2_flash.c rp2/rp2_flash: Add binary info for ROMFS. 2025-08-02 00:36:50 +10:00
rp2_flash.h rp2/rp2_flash: Configure optimal flash timings. 2025-04-08 11:01:14 +10:00
rp2_pio.c rp2/rp2_pio: Fix support for pin wrapping and RP2350B upper-bank pins. 2025-12-02 00:09:06 +11:00
rp2_psram.c rp2: Add support for PSRAM with auto-detection. 2025-04-08 10:56:59 +10:00
rp2_psram.h rp2: Add support for PSRAM with auto-detection. 2025-04-08 10:56:59 +10: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

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 board other than a Raspberry Pi Pico, 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 either copying firmware.uf2 to the USB mass storage device that appears, or using picotool load -x firmware.elf. The latter command can be accessed conveniently via make deploy.

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):
    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.