micropython/ports/rp2
Damien George 6e71cde6aa ports: Use default VFS config for import_stat and builtin_open.
For ports with MICROPY_VFS and MICROPY_PY_IO enabled their configuration
can now be simplified to use the defaults for mp_import_stat and
mp_builtin_open.

This commit makes no functional change, except for the following minor
points:
- the built-in "open" is removed from the minimal port (it previously did
  nothing)
- the duplicate built-in "input" is removed from the esp32 port
- qemu-arm now delegates to VFS import/open

Signed-off-by: Damien George <damien@micropython.org>
2022-05-25 13:04:45 +10:00
..
boards ports: Reformat more C and Python source code. 2022-04-20 16:17:35 +10:00
modules
CMakeLists.txt rp2/mphalport: Fix USB CDC RX handling to not block when unprocessed. 2022-04-05 10:47:19 +10:00
Makefile rp2/Makefile: Add mbedtls to GIT_SUBMODULES list and use it in CI func. 2022-04-11 13:05:25 +10:00
README.md
fatfs_port.c
machine_adc.c all: Use mp_obj_malloc everywhere it's applicable. 2022-05-03 22:28:14 +10:00
machine_bitstream.c
machine_i2c.c
machine_i2s.c all: Use mp_obj_malloc everywhere it's applicable. 2022-05-03 22:28:14 +10:00
machine_pin.c
machine_pwm.c
machine_rtc.c
machine_spi.c
machine_timer.c
machine_uart.c rp2/machine_uart: Use read/write mutex to prevent char duplication. 2022-04-12 11:39:00 +10:00
machine_wdt.c
main.c
memmap_mp.ld
modmachine.c extmod: Make extmod modules use MP_REGISTER_MODULE. 2022-05-18 20:49:12 +10:00
modmachine.h
modrp2.c rp2: Make port-specific modules use MP_REGISTER_MODULE. 2022-05-18 20:57:09 +10:00
modrp2.h
moduos.c
modutime.c rp2: Make port-specific modules use MP_REGISTER_MODULE. 2022-05-18 20:57:09 +10:00
mpbthciport.c
mpbthciport.h
mpconfigport.h ports: Use default VFS config for import_stat and builtin_open. 2022-05-25 13:04:45 +10:00
mphalport.c rp2/mphalport: Fix USB CDC RX handling to not block when unprocessed. 2022-04-05 10:47:19 +10:00
mphalport.h
mpnimbleport.c
mpnimbleport.h
mpthreadport.c
mpthreadport.h
msc_disk.c
qstrdefsport.h
rp2_flash.c
rp2_pio.c
tusb_config.h
tusb_port.c
uart.c
uart.h

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.
  • utime module with sleep, time and ticks functions.
  • uos 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.

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.