micropython/ports/rp2
Damien George 79edaddf50 rp2/CMakeLists: Apply O2 optimisation to map, mpz and vm source code.
Enabling this optimisation increases the RPI_PICO firmware by +3344 bytes.

Performace change is:

diff of scores (higher is better)
N=100 M=100             rpi-base -> rpi-opt      diff      diff% (error%)
bm_chaos.py               196.56 ->  215.26 :  +18.70 =  +9.514% (+/-0.05%)
bm_fannkuch.py             52.47 ->   54.37 :   +1.90 =  +3.621% (+/-0.05%)
bm_fft.py                1476.74 -> 1530.06 :  +53.32 =  +3.611% (+/-0.01%)
bm_float.py              2305.65 -> 2444.11 : +138.46 =  +6.005% (+/-0.08%)
bm_hexiom.py               32.83 ->   35.09 :   +2.26 =  +6.884% (+/-0.05%)
bm_nqueens.py            2335.85 -> 2259.78 :  -76.07 =  -3.257% (+/-0.06%)
bm_pidigits.py            366.23 ->  465.81 :  +99.58 = +27.191% (+/-0.04%)
bm_wordcount.py            41.20 ->   41.87 :   +0.67 =  +1.626% (+/-0.01%)
core_import_mpy_multi.py  327.44 ->  335.24 :   +7.80 =  +2.382% (+/-0.08%)
core_import_mpy_single.py  63.41 ->   64.98 :   +1.57 =  +2.476% (+/-0.21%)
core_locals.py             27.24 ->   29.19 :   +1.95 =  +7.159% (+/-0.01%)
core_qstr.py              137.31 ->  140.84 :   +3.53 =  +2.571% (+/-0.03%)
core_str.py                18.44 ->   18.10 :   -0.34 =  -1.844% (+/-0.03%)
core_yield_from.py        221.69 ->  211.72 :   -9.97 =  -4.497% (+/-0.01%)
misc_aes.py               303.38 ->  308.72 :   +5.34 =  +1.760% (+/-0.02%)
misc_mandel.py           1501.00 -> 1746.60 : +245.60 = +16.362% (+/-0.04%)
misc_pystone.py          1348.22 -> 1456.75 : +108.53 =  +8.050% (+/-0.07%)
misc_raytrace.py          223.81 ->  246.16 :  +22.35 =  +9.986% (+/-0.07%)
viper_call0.py            331.05 ->  331.10 :   +0.05 =  +0.015% (+/-0.00%)
viper_call1a.py           323.34 ->  323.39 :   +0.05 =  +0.015% (+/-0.00%)
viper_call1b.py           241.01 ->  241.04 :   +0.03 =  +0.012% (+/-0.00%)
viper_call1c.py           242.88 ->  242.92 :   +0.04 =  +0.016% (+/-0.00%)
viper_call2a.py           318.41 ->  318.46 :   +0.05 =  +0.016% (+/-0.00%)
viper_call2b.py           211.27 ->  211.30 :   +0.03 =  +0.014% (+/-0.00%)

And the test `tests/basics/builtin_pow3_intbig.py` now passes (it uses a
lot of mpz code and previously took longer than 10 seconds to run on
RPI_PICO, which would lead to a timeout in the test runner).

Signed-off-by: Damien George <damien@micropython.org>
2024-03-27 12:16:30 +11:00
..
boards rp2/boards: Add named digital pins for Arduino Nano RP2040 Connect. 2024-03-08 13:10:13 +11:00
btstack_inc
lwip_inc
mbedtls
modules
CMakeLists.txt rp2/CMakeLists: Apply O2 optimisation to map, mpz and vm source code. 2024-03-27 12:16:30 +11:00
Makefile
README.md
cyw43_configport.h
fatfs_port.c
help.c
libm_extra.c
machine_adc.c
machine_bitstream.c
machine_i2c.c
machine_i2s.c
machine_pin.c
machine_pin.h
machine_pin_cyw43.c
machine_pwm.c
machine_rtc.c
machine_spi.c
machine_timer.c
machine_uart.c
machine_wdt.c
main.c rp2: Enable support for Python USB devices. 2024-03-15 14:24:52 +11:00
memmap_mp.ld
modmachine.c
modmachine.h
modos.c
modrp2.c
modrp2.h
modtime.c
mpbthciport.c
mpbthciport.h
mpbtstackport.c
mpconfigport.h rp2: Enable support for Python USB devices. 2024-03-15 14:24:52 +11:00
mphalport.c
mphalport.h
mpnetworkport.c
mpnetworkport.h
mpnimbleport.c
mpnimbleport.h
mpthreadport.c
mpthreadport.h
msc_disk.c
mutex_extra.c
mutex_extra.h
pendsv.c
pendsv.h
qstrdefsport.h
rp2_dma.c
rp2_flash.c
rp2_pio.c
uart.c
uart.h
usbd.c

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.