Raspberry Pi Pico (RP2040)#

These examples target the Raspberry Pi Pico (RP2040) through the alpha ARM backend (pip install pymcu-arm). The backend currently supports GPIO + UART0 on core 0 — see Language Limitations for the full scope.

Each compiles with pymcu build to dist/firmware.bin, a flat flash image (boot2 at offset 0) that the RP2040Sharp emulator runs headlessly in CI. All three API styles work on the Pico: the native PyMCU HAL, the MicroPython machine shim, and the CircuitPython board / digitalio / busio shim.

UART echo — native HAL#

Echoes bytes received on UART0 (GP0 = TX, GP1 = RX) back to the sender.

# Echo bytes received on UART0 back to the sender.
from pymcu.hal.uart import UART


def main():
    uart = UART(115200)
    uart.println("ECHO")
    while True:
        c = uart.read()
        uart.write(c)

CircuitPython style — DigitalIO + UART#

This exact file also runs unmodified under CircuitPython on a Pico (save it as code.py). It needs the CircuitPython stdlib, selected per-project:

[tool.pymcu]
board     = "raspberry_pi_pico"
frequency = 125000000
sources   = "src"
entry     = "main.py"
stdlib    = ["circuitpython"]
import board
import digitalio
import busio
from pymcu.types import uint8


def main():
    led = digitalio.DigitalInOut(board.LED)
    led.direction = digitalio.Direction.OUTPUT

    uart = busio.UART(board.TX, board.RX, baudrate=115200)
    uart.write(b"READY\r\n")

    buf: uint8[1] = bytearray(1)
    while True:
        uart.readinto(buf)
        led.value = buf[0] & 1
        uart.write(buf)

MicroPython style — UART echo#

Runs unmodified under MicroPython too. Needs the MicroPython stdlib (stdlib = ["micropython"] in [tool.pymcu]):

from machine import Pin, UART


def main():
    uart = UART(0, 115200)
    led = Pin(25, Pin.OUT)
    uart.println("READY")
    while True:
        c: int = uart.read()
        led.toggle()
        uart.write(c)

Validating in CI#

Compiled firmware.bin images are driven headlessly by the RP2040Sharp TestKit — assert on GPIO and UART exactly as on real silicon, with deterministic, bounded runs. See the RP2040Sharp firmware testing guide.