micropython/ports/rp2
cpottle9 c80e7c14e6 rp2: Allocate GC heap from unused RAM.
Borrowing an idea from the mimxrt port (also stm32 port): in the loader
input file memmap_mp.ld calculate __GcHeapStart and __GcHeapEnd as the
unused RAM.  Then in main.c use these addresses as arguments to gc_init().

The benefits of this change are:

1) When libraries are added or removed in the future changing BSS usage,
   main.c's sizing of the GC heap does not need to be changed.

2) Currently these changes make the GC area about 30 KBytes larger, eg on
   PICO_W the GC heap increases from 166016 to 192448 bytes.  Without that
   change this RAM would never get used.

3) If someone wants to disable one or more SRAM blocks on the RP2040 to
   reduce power consumption it will be easy: just change the MEMORY section
   in memmap_mp.ld.  For instance to not use SRAM2 and SRAM3 change it to:

        MEMORY
        {
            FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 2048k
            RAM(rwx) : ORIGIN =  0x21000000, LENGTH = 128k
            SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k
            SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k
        }

   Then to turn off clocks for SRAM2 and SRAM3 from MicroPython, set the
   appropriate bits in WAKE_EN0 and SLEEP_EN0.

Tested by running the firmware.uf2 file on PICO_W and displaying
micropython.mem_info().  Confirmed GC total size approximately matched the
size calculated by the loader.

Signed-off-by: cpottle9 <cpottle9@outlook.com>
2023-03-09 12:00:02 +11:00
..
boards rp2/boards/ARDUINO_NANO_RP2040_CONNECT: Include "senml" in manifest. 2023-03-09 10:38:06 +11:00
lwip_inc rp2/lwip_inc: Enable lwIP status callback. 2022-06-30 16:03:21 +10:00
mbedtls extmod/mbedtls: Enable elliptic curve DH and DSA cryptography. 2022-10-22 19:08:21 +11:00
modules rp2: Add USB MSC support. 2022-03-09 00:38:07 +11:00
CMakeLists.txt stm32: Update to use the open-source lib version of cyw43-driver. 2023-03-01 01:27:12 +11:00
cyw43_configport.h extmod/modnetwork: Add network.hostname() and network.country(). 2023-03-01 01:26:17 +11:00
fatfs_port.c rp2/fatfs_port: Fix the modification date of files. 2022-10-12 00:31:38 +11:00
machine_adc.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_bitstream.c rp2/machine_bitstream: Implement the machine.bitstream driver. 2021-11-17 10:01:15 +11:00
machine_i2c.c rp2: Rename machine I2C type consistently across ports. 2022-10-22 12:56:58 +11:00
machine_i2s.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_pin_cyw43.c rp2/machine_pin: Add support for named pins and alternate functions. 2023-01-16 11:44:26 +11:00
machine_pin.c rp2/machine_pin: Fix configuring OPEN_DRAIN with initial value. 2023-01-24 17:29:45 +11:00
machine_pin.h rp2/machine_pin: Add support for named pins and alternate functions. 2023-01-16 11:44:26 +11:00
machine_pwm.c rp2/machine_pwm: Fix overflows with freq > 268 MHz. 2023-01-12 16:28:12 +11:00
machine_rtc.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_spi.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_timer.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_uart.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
machine_wdt.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
main.c rp2: Allocate GC heap from unused RAM. 2023-03-09 12:00:02 +11:00
Makefile rp2/Makefile: Allow specifying BOARD_DIR on make command line. 2023-02-23 09:50:07 +11:00
memmap_mp.ld rp2: Allocate GC heap from unused RAM. 2023-03-09 12:00:02 +11:00
modmachine.c rp2: Fix lightsleep to work with interrupts and cyw43 driver. 2022-12-20 15:54:51 +11:00
modmachine.h rp2: Rename machine I2C type consistently across ports. 2022-10-22 12:56:58 +11:00
modrp2.c rp2/modrp2: Disable other core, shorten delay to 8us in bootsel_button. 2023-03-09 11:44:20 +11:00
modrp2.h rp2: Add new port to Raspberry Pi RP2 microcontroller. 2021-01-30 00:42:29 +11:00
moduos.c py/objstr: Split mp_obj_str_from_vstr into bytes/str versions. 2022-08-26 16:43:55 +10:00
modutime.c all: Remove third argument to MP_REGISTER_MODULE. 2022-06-02 16:31:37 +10:00
mpbthciport.c py/obj: Convert make_new into a mp_obj_type_t slot. 2022-09-19 19:06:15 +10:00
mpbthciport.h rp2: Add support for bluetooth module using NimBLE. 2021-09-19 23:09:59 +10:00
mpconfigport.h rp2/modrp2: Disable other core, shorten delay to 8us in bootsel_button. 2023-03-09 11:44:20 +11:00
mphalport.c rp2: Allow enabling USB device without enabling USB-CDC. 2022-11-11 16:49:18 +11:00
mphalport.h rp2/machine_pin: Fix configuring OPEN_DRAIN with initial value. 2023-01-24 17:29:45 +11:00
mpnetworkport.c extmod/modnetwork: Add network.hostname() and network.country(). 2023-03-01 01:26:17 +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: Add support for bluetooth module using NimBLE. 2021-09-19 23:09:59 +10:00
mpnimbleport.h rp2: Add support for bluetooth module using NimBLE. 2021-09-19 23:09:59 +10:00
mpthreadport.c rp2: Make atomic sections suspend the other core (if active). 2022-07-12 16:20:21 +10:00
mpthreadport.h rp2/mpthreadport: Add mp_thread_deinit to reset core1 on soft reset. 2021-05-09 00:08:30 +10:00
msc_disk.c rp2: Add USB MSC support. 2022-03-09 00:38:07 +11:00
pendsv.c rp2/pendsv: Add MICROPY_BOARD_PENDSV_ENTRIES for board customisation. 2023-03-07 15:59:37 +11:00
pendsv.h rp2/pendsv: Add MICROPY_BOARD_PENDSV_ENTRIES for board customisation. 2023-03-07 15:59:37 +11:00
qstrdefsport.h rp2: Add new port to Raspberry Pi RP2 microcontroller. 2021-01-30 00:42:29 +11:00
README.md rp2: Integrate CYW43xx WiFi driver. 2022-06-30 17:03:51 +10:00
rp2_flash.c rp2/rp2_flash: Call MICROPY_EVENT_POLL_HOOK_FAST after reading flash. 2023-01-12 16:53:02 +11:00
rp2_pio.c rp2/rp2_pio: Track use of PIO resources and free them on soft reset. 2023-02-07 14:52:36 +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: Further refactor static USB device implementation. 2022-11-11 16:47:36 +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.
  • 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.

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=PICO_W submodules
$ make BOARD=PICO_W clean
$ make BOARD=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.