micropython/ports/rp2
David Lechner ffb43b2dd3 py/modthread: Return thread id from start_new_thread().
In CPython, `_thread.start_new_thread()` returns an ID that is the same ID
that is returned by `_thread.get_ident()`.  The current MicroPython
implementation of `_thread.start_new_thread()` always returns `None`.

This modifies the required functions to return a value. The native thread
id is returned since this can be used for interop with other functions, for
example, `pthread_kill()` on *nix. `_thread.get_ident()` is also modified
to return the native thread id so that the values match and avoids the need
for a separate `native_id` attribute.

Fixes issue #12153.

Signed-off-by: David Lechner <david@pybricks.com>
2023-09-03 18:49:18 +10:00
..
boards rp2: Rename PICO, PICO_W to RPI_PICO, RPI_PICO_W. 2023-08-23 15:49:37 +10:00
btstack_inc rp2: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
lwip_inc
mbedtls
modules all: Replace all uses of umodule in Python code. 2023-06-08 17:54:24 +10:00
CMakeLists.txt rp2: Rename PICO, PICO_W to RPI_PICO, RPI_PICO_W. 2023-08-23 15:49:37 +10:00
Makefile rp2: Rename PICO, PICO_W to RPI_PICO, RPI_PICO_W. 2023-08-23 15:49:37 +10:00
README.md all: Rename MP_QSTR_umodule to MP_QSTR_module everywhere. 2023-06-08 17:53:57 +10:00
cyw43_configport.h rp2: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
fatfs_port.c
help.c ports: Standardise docs link in help text. 2023-06-02 11:48:46 +10:00
machine_adc.c
machine_bitstream.c
machine_i2c.c rp2/machine_i2c: Add timeout parameter for machine.I2C(). 2023-04-21 18:03:33 +10:00
machine_i2s.c ports: In machine_i2s.c, rename uasyncio to asyncio. 2023-06-19 18:36:54 +10:00
machine_pin.c rp2/machine_pin: Factor out pin-find code from machine_pin_make_new. 2023-07-20 17:30:17 +10:00
machine_pin.h
machine_pin_cyw43.c
machine_pwm.c rp2/machine_pwm: Add duty_x() checks and return 0 if PWM is not started. 2023-05-04 13:18:58 +10:00
machine_rtc.c
machine_spi.c
machine_timer.c rp2/machine_timer: Fix printing of timer period. 2023-09-02 17:09:51 +10:00
machine_uart.c rp2/machine_uart: Fix setting of UART LCR parameters. 2023-03-22 16:21:48 +11:00
machine_wdt.c
main.c rp2: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
memmap_mp.ld all: Fix spelling mistakes based on codespell check. 2023-04-27 18:03:06 +10:00
modmachine.c all: Use MP_REGISTER_EXTENSIBLE_MODULE for overrideable built-ins. 2023-06-08 17:54:21 +10:00
modmachine.h rp2/machine_pwm: Enable keyword args in constructor and add init method. 2023-05-04 13:13:05 +10:00
modos.c all: Rename *umodule*.c to remove the "u" prefix. 2023-06-08 17:54:17 +10: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
modtime.c all: Rename *umodule*.c to remove the "u" prefix. 2023-06-08 17:54:17 +10:00
mpbthciport.c rp2/mpbthciport: Cancel existing alarms. 2023-06-14 22:20:20 +10: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: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
mphalport.c all: Rename mp_umodule*, mp_module_umodule* to remove the "u" prefix. 2023-06-08 17:54:04 +10:00
mphalport.h
mpnetworkport.c rp2: Add Bluetooth support via cyw43. 2023-06-14 22:20:20 +10:00
mpnetworkport.h
mpnimbleport.c
mpnimbleport.h
mpthreadport.c py/modthread: Return thread id from start_new_thread(). 2023-09-03 18:49:18 +10:00
mpthreadport.h
msc_disk.c
pendsv.c
pendsv.h
qstrdefsport.h
rp2_flash.c
rp2_pio.c rp2: Make rp2_state_machine_exec accept integers. 2023-05-18 12:33:02 +10:00
uart.c
uart.h
usbd.c shared/tinyusb: Allow max USB descriptor string to be configured. 2023-04-04 11:57:25 +10:00

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=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.