circuitpython/ports/rp2
Damien George 50a7362b3e rp2: Implement vfs.rom_ioctl with support for external flash.
Not enabled by default on any board.  A board can enable a ROMFS partition
by defining `MICROPY_HW_ROMFS_BYTES` in its `mpconfigboard.h` file.  For
example:

    #define MICROPY_HW_ROMFS_BYTES (128 * 1024)

The ROMFS partition is placed at the end of the flash allocated for the
firmware, giving less space for the firmware.  It then lives between the
firmware and the read/write filesystem.

Signed-off-by: Damien George <damien@micropython.org>
2025-03-06 12:52:35 +11:00
..
boards rp2/boards/SPARKFUN_XRP_CONTROLLER_BETA: Add SparkFun XRP Controller. 2025-02-28 14:48:32 +11:00
btstack_inc rp2: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
lwip_inc extmod/lwip-include: Factor common lwIP config into lwipopts_common.h. 2025-01-29 12:36:27 +11:00
mbedtls rp2: Integrate RP2350 and use aon_timer instead of rtc API. 2024-10-15 12:07:59 +11:00
modules rp2/modules: Fix memory leak and logic bug in handling of _pio_funcs. 2025-02-11 12:44:48 +11:00
clocks_extra.c rp2/clocks_extra: Update runtime_clocks_init based on new pico-sdk. 2024-10-15 12:15:48 +11: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 lib/pico-sdk: Update to version 2.1.1. 2025-02-28 14:21:35 +11:00
cyw43_configport.h rp2/cyw43_configport: Define cyw43 pins. 2024-12-19 16:54:39 +11: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.c rp2/machine_i2c: Make I2C bus ID arg optional with default. 2025-02-28 15:17:34 +11: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.c rp2/machine_pin: Generalise gpio_irq handler for pins >32. 2024-10-15 12:14:59 +11:00
machine_pin.h rp2/machine_pin: Move decl of machine_pin_obj_table to public header. 2024-10-15 12:09:48 +11:00
machine_pin_cyw43.c rp2/machine_pin_cyw43: Include check for CYW43_GPIO. 2024-09-19 12:58:43 +10: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 all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +11:00
machine_timer.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +11:00
machine_uart.c rp2/machine_uart: Make it so TX is done only when no longer busy. 2024-10-22 10:17:05 +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 all: Upgrade codespell to v2.4.1. 2025-02-25 16:11:33 +11:00
Makefile py/mkrules: Add GIT_SUBMODULES_FAIL_IF_EMPTY flag for CMake ports. 2025-01-29 11:41:32 +11:00
memmap_mp_rp2040.ld rp2: Update custom linker scripts for new pico-sdk. 2024-10-15 11:51:56 +11:00
memmap_mp_rp2350.ld rp2: Update custom linker scripts for new pico-sdk. 2024-10-15 11:51:56 +11:00
modmachine.c rp2/modmachine: Make lightsleep preserve SLEEP_EN0 and SLEEP_EN1. 2025-02-07 11:51:41 +11:00
modmachine.h rp2/machine_uart: Implement a Python UART IRQ handler. 2024-08-29 16:27:43 +10:00
modos.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +11:00
modrp2.c all: Remove the "STATIC" macro and just use "static" instead. 2024-03-07 14:20:42 +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 rp2: Integrate RP2350 and use aon_timer instead of rtc API. 2024-10-15 12:07:59 +11: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 rp2: Implement vfs.rom_ioctl with support for external flash. 2025-03-06 12:52:35 +11:00
mphalport.c rp2/mphalport: Add mp_hal_is_pin_reserved() function. 2024-12-19 16:54:39 +11:00
mphalport.h rp2/mphalport: Fix mp_hal_pin_low/high() for pin>=32. 2025-01-11 14:23:40 +11:00
mpnetworkport.c rp2/boards/RPI_PICO2_W: Add RISCV variant for Pico 2 W. 2024-12-19 16:54:39 +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: Migrate to the new mp_thread_recursive_mutex_t. 2025-02-03 15:02:02 +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 rp2: Fix power consumption when sleeping with a timeout. 2024-07-23 16:01:42 +10:00
pendsv.c rp2: Fix build failure if threads are disabled. 2025-02-18 12:09:23 +11:00
pendsv.h rp2: Support calling pendsv_suspend/resume from core 1. 2024-05-31 16:46:27 +10:00
qstrdefsport.h rp2: Add new port to Raspberry Pi RP2 microcontroller. 2021-01-30 00:42:29 +11:00
README.md rp2/README: Remove redundant global statement from example code. 2024-10-28 11:16:04 +11:00
rp2_dma.c rp2/rp2_dma: Generalise DMA for RP2350. 2024-10-15 11:57:06 +11:00
rp2_flash.c rp2: Implement vfs.rom_ioctl with support for external flash. 2025-03-06 12:52:35 +11:00
rp2_pio.c rp2/rp2_pio: Add support for RP2350A/B variants in PIO interface. 2024-10-15 12:09:48 +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

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