Library guide · API index and notation
This page is generated from the bundled library in this checkout. Signatures and adjacent source comments are reproduced below; the linked guide explains usage, storage, failures, and platform support.
import "std/embedded/chip/rp2040"
Names used in signatures
Unqualified names denote this package’s types (including other source files in the same package), language built-ins, or generic parameters such as T. Qualified names use the import aliases below. These aliases belong to the library source; import a dependency yourself to use its alias in your program.
SYS_HZ
Constant · Source
clk_sys, the processor and peripheral clock, after take().
pub const SYS_HZ: u32 = 125_000_000
PIN_COUNT
Constant · Source
User GPIOs 0 to 29.
pub const PIN_COUNT: u32 = 30
PIO_BLOCKS
Constant · Source
PIO blocks 0 to 1.
pub const PIO_BLOCKS: u32 = 2
ALARM_COUNT
Constant · Source
Alarms 0 to 3, raising TIMER_IRQ_0 + n.
pub const ALARM_COUNT: u32 = 4
TIMER_IRQ_0
Constant · Source
Interrupt numbers (2.3.2), for std/embedded/cortex_m’s enable, pend, and set_priority. Each is also the vector name of its
@interrupthandler.
pub const TIMER_IRQ_0: u32 = 0
TIMER_IRQ_1
Constant · Source
pub const TIMER_IRQ_1: u32 = 1
TIMER_IRQ_2
Constant · Source
pub const TIMER_IRQ_2: u32 = 2
TIMER_IRQ_3
Constant · Source
pub const TIMER_IRQ_3: u32 = 3
PWM_IRQ_WRAP
Constant · Source
pub const PWM_IRQ_WRAP: u32 = 4
USBCTRL_IRQ
Constant · Source
pub const USBCTRL_IRQ: u32 = 5
XIP_IRQ
Constant · Source
pub const XIP_IRQ: u32 = 6
PIO0_IRQ_0
Constant · Source
pub const PIO0_IRQ_0: u32 = 7
PIO0_IRQ_1
Constant · Source
pub const PIO0_IRQ_1: u32 = 8
PIO1_IRQ_0
Constant · Source
pub const PIO1_IRQ_0: u32 = 9
PIO1_IRQ_1
Constant · Source
pub const PIO1_IRQ_1: u32 = 10
DMA_IRQ_0
Constant · Source
pub const DMA_IRQ_0: u32 = 11
DMA_IRQ_1
Constant · Source
pub const DMA_IRQ_1: u32 = 12
IO_IRQ_BANK0
Constant · Source
pub const IO_IRQ_BANK0: u32 = 13
IO_IRQ_QSPI
Constant · Source
pub const IO_IRQ_QSPI: u32 = 14
SIO_IRQ_PROC0
Constant · Source
pub const SIO_IRQ_PROC0: u32 = 15
SIO_IRQ_PROC1
Constant · Source
pub const SIO_IRQ_PROC1: u32 = 16
CLOCKS_IRQ
Constant · Source
pub const CLOCKS_IRQ: u32 = 17
SPI0_IRQ
Constant · Source
pub const SPI0_IRQ: u32 = 18
SPI1_IRQ
Constant · Source
pub const SPI1_IRQ: u32 = 19
UART0_IRQ
Constant · Source
pub const UART0_IRQ: u32 = 20
UART1_IRQ
Constant · Source
pub const UART1_IRQ: u32 = 21
ADC_IRQ_FIFO
Constant · Source
pub const ADC_IRQ_FIFO: u32 = 22
I2C0_IRQ
Constant · Source
pub const I2C0_IRQ: u32 = 23
I2C1_IRQ
Constant · Source
pub const I2C1_IRQ: u32 = 24
RTC_IRQ
Constant · Source
pub const RTC_IRQ: u32 = 25
CLOCK_ADC
Constant · Source
Clock gates for
Power(2.15.3.5), as std/embedded/power.Clocks: bit n of WAKE_EN0 and SLEEP_EN0 is bit n, bit n of WAKE_EN1 and SLEEP_EN1 bit 32 + n. Each peripheral’s constant holds every clock it uses, its clk_sys register clock and, where it has one, its own clock.
pub const CLOCK_ADC: power.Clocks = (1 << 1) | (1 << 2)
CLOCK_BUSCTRL
Constant · Source
pub const CLOCK_BUSCTRL: power.Clocks = 1 << 3
CLOCK_DMA
Constant · Source
pub const CLOCK_DMA: power.Clocks = 1 << 5
CLOCK_I2C0
Constant · Source
pub const CLOCK_I2C0: power.Clocks = 1 << 6
CLOCK_I2C1
Constant · Source
pub const CLOCK_I2C1: power.Clocks = 1 << 7
CLOCK_GPIO
Constant · Source
GPIO control and pads: their interrupts need these to wake a deep sleep.
pub const CLOCK_GPIO: power.Clocks = (1 << 8) | (1 << 11)
CLOCK_JTAG
Constant · Source
pub const CLOCK_JTAG: power.Clocks = 1 << 9
CLOCK_PIO0
Constant · Source
pub const CLOCK_PIO0: power.Clocks = 1 << 12
CLOCK_PIO1
Constant · Source
pub const CLOCK_PIO1: power.Clocks = 1 << 13
CLOCK_PLL_USB
Constant · Source
PLL_USB’s registers; PLL_USB runs while the ADC or USB serial port does.
pub const CLOCK_PLL_USB: power.Clocks = 1 << 15
CLOCK_PWM
Constant · Source
pub const CLOCK_PWM: power.Clocks = 1 << 17
CLOCK_RTC
Constant · Source
pub const CLOCK_RTC: power.Clocks = (1 << 21) | (1 << 22)
CLOCK_SPI0
Constant · Source
pub const CLOCK_SPI0: power.Clocks = (1 << 24) | (1 << 25)
CLOCK_SPI1
Constant · Source
pub const CLOCK_SPI1: power.Clocks = (1 << 26) | (1 << 27)
CLOCK_MEMORY
Constant · Source
The SRAM banks and the bus fabric, for DMA during a deep sleep.
pub const CLOCK_MEMORY: power.Clocks = (1 << 4) | (0xF << 28) | (0x3 << 32)
CLOCK_SYSCFG
Constant · Source
pub const CLOCK_SYSCFG: power.Clocks = 1 << 34
CLOCK_SYSINFO
Constant · Source
pub const CLOCK_SYSINFO: power.Clocks = 1 << 35
CLOCK_TBMAN
Constant · Source
pub const CLOCK_TBMAN: power.Clocks = 1 << 36
CLOCK_TIMER
Constant · Source
pub const CLOCK_TIMER: power.Clocks = 1 << 37
CLOCK_UART0
Constant · Source
pub const CLOCK_UART0: power.Clocks = (1 << 38) | (1 << 39)
CLOCK_UART1
Constant · Source
pub const CLOCK_UART1: power.Clocks = (1 << 40) | (1 << 41)
CLOCK_USB
Constant · Source
pub const CLOCK_USB: power.Clocks = (1 << 42) | (1 << 43)
CLOCK_WATCHDOG
Constant · Source
pub const CLOCK_WATCHDOG: power.Clocks = 1 << 44
ALARM_CLOCKS
Constant · Source
What the timer and its alarms need: keep them in the sleep set to wake from a deep sleep on an alarm, as an executor on an alarm does.
pub const ALARM_CLOCKS: power.Clocks = CLOCK_TIMER | CLOCK_WATCHDOG
ESSENTIAL_CLOCKS
Constant · Source
Clocks the program cannot run without, which Power.disable_clocks refuses: the clock, reset, and power-on controls, the bus fabric, the boot ROM, SRAM, flash (XIP), the cores’ SIO, GPIO, the voltage regulator, and the oscillators and PLL_SYS, which
dormantrestarts.
pub const ESSENTIAL_CLOCKS: power.Clocks = (1 << 0) | (1 << 4) | CLOCK_GPIO | (1 << 10) | (1 << 14) | (1 << 16) | (1 << 18) | (1 << 19) | (1 << 20) | (1 << 23) | (0xF << 28) | (0x3 << 32) | (1 << 45) | (1 << 46)
ALL_CLOCKS
Constant · Source
Every gate.
pub const ALL_CLOCKS: power.Clocks = 0x7FFF_FFFF_FFFF
LEVEL_LOW
Constant · Source
Pin events for
Pin.listen,ignore,events, andclear_events: the pin’s level is low or high, or it fell or rose. Levels last as long as the level does; edges stay latched until cleared.
pub const LEVEL_LOW: u32 = 1 << 0
LEVEL_HIGH
Constant · Source
pub const LEVEL_HIGH: u32 = 1 << 1
FALLING_EDGE
Constant · Source
pub const FALLING_EDGE: u32 = 1 << 2
RISING_EDGE
Constant · Source
pub const RISING_EDGE: u32 = 1 << 3
reboot_to_bootloader
Function · Source
Restart into the boot ROM’s USB bootloader, as if BOOTSEL were held at power-up: the board reappears as the RPI-RP2 drive and accepts new firmware, for example from
dodo run. Pinactivity_led, if below PIN_COUNT, lights while the drive is accessed. Does not return.
pub fn reboot_to_bootloader(activity_led: u32)
report_panic
Function · Source
Write a failed check’s message to every serial port claimed from
pins.uartand not yet released, as the default panic hook does before it halts:dodo: arithmetic overflow check failed at main.dodo:12:5. Call it from a panic hook of your own that, for example, restarts into the bootloader:extern “C” fn dodo_board_panic(check: *const u8, file: *const u8, line: u32, column: u32) { unsafe { chip.report_panic(check, file, line, column) } chip.reboot_to_bootloader(25) }
SAFETY:
checkandfilemust be the hook’s NUL-terminated strings, and the caller must halt or reset afterwards, as this takes over the ports.
pub unsafe fn report_panic(check: *const u8, file: *const u8, line: u32, column: u32)
Requires an unsafe context. Follow the source safety preconditions and the linked guide before calling this API.
Config
Struct · Source
What the circuit board decides. Board packages pass theirs to
take.
pub struct Config {
// The crystal on XIN/XOUT, in hertz: 1 to 15 MHz, a whole number of
// megahertz (for the 1 MHz timer tick) that divides 1.5 GHz (for the
// PLL). 12 MHz, as on the Raspberry Pi Pico, is the usual choice.
pub crystal_hz: u32
}
Peripherals
Struct · Source
The chip’s peripherals, handed out once by
take.
pub struct Peripherals {
pub pins: Pins
pub timer: Timer
// The real-time clock: the time of day, counted in seconds.
pub rtc: Rtc
// The watchdog, stopped; rp_watchdog documents it.
pub watchdog: rp_watchdog.Watchdog
pub usb: Usb
// The DMA controller's channels; rp_dma documents them.
pub dma: rp_dma.Dma
// The random number generator, from the ring oscillator's jitter:
// about 3 KB a second, fine for seeds and games but not for
// cryptographic keys; rp_rng documents it.
pub rng: rp_rng.Rosc
// Core 1, which runs a task once started, and the FIFOs and spinlocks
// the two cores share; see std/embedded/chip/rp_multicore.
pub core1: rp_multicore.Core1
pub fifos: rp_multicore.Fifos
pub spinlocks: rp_multicore.Spinlocks
// The program flash, for storing data above the program; see
// std/embedded/chip/rp_flash.
pub flash: rp_flash.Flash
// Sleep modes, clock gating, the dormant state, and the system clock's
// rate; see Power.
pub power: Power
}
take
Function · Source
Configure clocks, take GPIO, the timer, and DMA out of reset, and return the peripherals. A second call returns Error.Busy, so no two owners exist; an unusable crystal frequency returns Error.InvalidInput, and a crystal or PLL that does not start returns Error.Timeout.
pub fn take(config: Config) -> Peripherals!hal.Error
pin_count
Function · Source
The user GPIOs: PIN_COUNT, as rp2350.pin_count() for the RP2350’s packages, so portable code asks either chip the same way.
pub fn pin_count() -> u32
Pins
Struct · Source
The 30 user GPIOs, and the I2C, SPI, and UART controllers that use them. Each pin and each controller can be taken once, and given back.
pub struct Pins {
// Private implementation fields omitted.
}
Pins.pin_count
Function · Source
The GPIOs the chip has: PIN_COUNT.
pub fn pin_count(&self) -> u32
Pins.output
Function · Source
Claim pin
numberas a push-pull output, initially low.
pub fn output(&mut self, number: u32) -> Pin!hal.Error
Pins.input
Function · Source
Claim pin
numberas an input.
pub fn input(&mut self, number: u32) -> Pin!hal.Error
Pins.release
Function · Source
Give back a pin from
outputorinput. It stops driving and is disconnected until claimed again, for example as part of a bus.
pub fn release(&mut self, pin: Pin)
Pins.i2c
Function · Source
Claim pins
sdaandsclas an I2C bus with an SCL frequency offrequency_hz, at most hal.I2C_FAST_PLUS_HZ. Even pins carry SDA and odd pins SCL. GPIO 0 and 1, 4 and 5, and so on in steps of four belong to I2C0; GPIO 2 and 3, 6 and 7, and so on to I2C1. InvalidInput if the two pins cannot form a bus or the frequency is out of range, Busy if a pin or the controller is taken. The pads’ own pull-ups (about 50 kOhm) keep an idle bus high but are too weak for fast mode: most sensor boards add 4.7 kOhm pull-ups, and a bare bus needs them. The designware_i2c driver documents the bus’s errors.
pub fn i2c(&mut self, sda: u32, scl: u32, frequency_hz: u32) -> designware_i2c.Bus!hal.Error
Pins.spi
Function · Source
Claim pins as an SPI bus in
mode, clocked at the fastest rate up tofrequency_hz(at most SYS_HZ / 2, 62.5 MHz):clock(SCK),tx(data out to the devices, MOSI), andrx(data in from them, MISO), or none when they only listen. GPIO n carries RX when n % 4 is 0, the clock when it is 2, and TX when it is 3. GPIO 0 to 7 and 16 to 23 belong to SPI0, GPIO 8 to 15 and 24 to 29 to SPI1. Give each device an output pin as its chip select, through hal.SpiDevice. Errors as fori2c.
pub fn spi(&mut self, clock: u32, tx: u32, rx: Option<u32>, mode: hal.SpiMode, frequency_hz: u32) -> pl022.Bus!hal.Error
Pins.uart
Function · Source
Claim pin
txas a serial port’s transmit line (to the other end’s RX), andrxas its receive line, or none to only send. Frames are 8N1 at the rate closest tobaud, from 120 baud up to SYS_HZ / 16 (7.8 Mbaud); 115_200 is the usual choice for a terminal. GPIO n carries TX when n % 4 is 0 and RX when it is 1: UART0 has TX on 0, 12, 16, and 28 and RX on 1, 13, 17, and 29, UART1 TX on 4, 8, 20, and 24 and RX on 5, 9, 21, and 25. The port implements the std/io Reader and Writer protocols, so std/fmt prints to it; until it is released, a panic writes its failure message to it before the chip halts. InvalidInput if the pins cannot form a port or the baud rate is out of range, Busy if a pin or the controller is taken. The pl011 driver documents the port.
pub fn uart(&mut self, tx: u32, rx: Option<u32>, baud: u32) -> pl011.Port!hal.Error
Pins.uart_flow
Function · Source
uartwith hardware flow control, turned on:ctsis an input, low while the other end may receive, andrtsan output, low while this port may. GPIO n carries CTS when n % 4 is 2 and RTS when it is 3, in the TX and RX pins’ group of four: UART0 has CTS on 2, 14, 18 and RTS on 3, 15, 19, UART1 CTS on 6, 10, 22, 26 and RTS on 7, 11, 23, 27. Errors as foruart.
pub fn uart_flow(&mut self, tx: u32, rx: u32, cts: u32, rts: u32, baud: u32) -> pl011.Port!hal.Error
Pins.spi_with_select
Function · Source
spiwith the controller’s own chip select on pinselectas well, for devices in the TI and Microwire frame formats (see pl022.FrameFormat), which need its timing. GPIO n carries it when n % 4 is 1, in the bus’s block of eight: SPI0 on 1, 5, 17, 21, SPI1 on 9, 13, 25, 29. Errors as forspi.
pub fn spi_with_select(&mut self, clock: u32, tx: u32, rx: Option<u32>, select: u32, mode: hal.SpiMode, frequency_hz: u32) -> pl022.Bus!hal.Error
Pins.spi_target
Function · Source
Claim pins as an SPI target (slave) in
mode, for another master to clock, at up to SYS_HZ / 12 (10.4 MHz), and select:rxreceives what it sends,txanswers, and the pins are numbered as forspiandspi_with_select. The pl022 driver documents the target.
pub fn spi_target(&mut self, clock: u32, tx: u32, rx: Option<u32>, select: u32, mode: hal.SpiMode) -> pl022.TargetBus!hal.Error
Pins.i2c_target
Function · Source
Claim pins
sdaandscl, numbered as fori2c, as an I2C target (slave) answering another master ataddress, a 10-bit address withten_bit.frequency_hzis the master’s SCL frequency, which sets the input filter. InvalidInput for an address the I2C specification reserves (7-bit 0x00 to 0x07 and 0x78 to 0x7F), otherwise errors as fori2c. The designware_i2c driver documents the target.
pub fn i2c_target(&mut self, sda: u32, scl: u32, address: u16, ten_bit: bool, frequency_hz: u32) -> designware_i2c.TargetBus!hal.Error
Pins.pwm
Function · Source
Claim pin
pinas a PWM output at the frequency closest tofrequency_hz, from 8 Hz up to SYS_HZ / 2, low until a duty is set. GPIO n is output A (n even) or B (n odd) of PWM slice (n / 2) % 8: GPIO n and n + 16 carry the same output, and the two outputs of a slice share its frequency. The output implements hal.PwmOutput, with max_duty() steps up to 65535; rp_pwm documents it. InvalidInput above GPIO 29 or for a frequency out of range, Busy if the pin or its output is taken, or if the slice’s other output runs at another frequency.
pub fn pwm(&mut self, pin: u32, frequency_hz: u32) -> rp_pwm.Pwm!hal.Error
Pins.pwm_divided
Function · Source
pwmwith the slice’s divider of clk_sys, in sixteenths from 16 (1) to 4095 (255 15/16), and its TOP, up to 65534, given instead: the output’s frequency is SYS_HZ * 16 / divider / (top + 1), and its duty runs from 0 to top + 1. Errors as forpwm.
pub fn pwm_divided(&mut self, pin: u32, divider: u32, top: u32) -> rp_pwm.Pwm!hal.Error
Pins.pwm_input
Function · Source
Claim pin
pin, an odd GPIO, as a PWM input that measures the frequency and duty cycle of the signal on it: GPIO n is input B of PWM slice (n / 2) % 8, whose counter counts its rising edges and its high time in windows ofwindow_us, up to 134 ms, two per measurement. Its pull-down holds an unconnected pin low. The input implements hal.PwmInput; rp_pwm.Capture documents the measurement and its precision. InvalidInput for an even pin, above GPIO 29, or for a window of 0 or too long, Busy if the pin is taken or either output of its slice is in use.
pub fn pwm_input(&mut self, pin: u32, window_us: u32) -> rp_pwm.PwmCapture!hal.Error
Pins.analog
Function · Source
Claim pin
pin, GPIO 26 to 29 (ADC inputs 0 to 3), as an analog input, with its digital input and pulls turned off. It implements hal.AnalogInput: 12-bit polled reads, 0 to 4095 for 0 V to the ADC’s reference, ADC_AVDD; rp_adc documents it. InvalidInput for another pin, Busy if the pin is taken, Timeout if the ADC does not start.
pub fn analog(&mut self, pin: u32) -> rp_adc.Analog!hal.Error
Pins.temperature
Function · Source
Claim the on-chip temperature sensor, ADC input 4, an analog input like those of
analog: rp_adc.millicelsius(value, 3300) converts a reading. Busy if it is claimed already.
pub fn temperature(&mut self) -> rp_adc.Analog!hal.Error
Pins.sampler
Function · Source
Claim the ADC as a sampler that converts
inputs, a set such as rp_adc.GPIO26 | rp_adc.TEMPERATURE, one after the other atrate_hzconversions a second in all (733 to 500 000), into its FIFO, for polled, interrupt-driven, or DMA reads; rp_adc.Sampler documents it. Its pins are claimed as analog inputs. InvalidInput for an empty set or a rate out of range, Busy if a pin is taken or an analog input or the temperature sensor is claimed, as they share the converter; Timeout if the ADC does not start.
pub fn sampler(&mut self, inputs: rp_adc.Inputs, rate_hz: u32) -> rp_adc.AdcSampler!hal.Error
Pins.pio
Function · Source
Claim PIO block
number, 0 to 1, with its four state machines stopped, for programs from rp_pio.assemble; rp_pio documents it. Its state machines run at up to SYS_HZ instructions a second and drive the pins given to it withpio_pins. InvalidInput for another number, Busy if the block is claimed.
pub fn pio(&mut self, number: u32) -> rp_pio.Pio!hal.Error
Pins.pio_pins
Function · Source
Connect the
countpins from GPIOfirstto PIO blockpio, for its state machines to drive through their pin mappings and read. They also read pins claimed in other ways: claim a pin they only read as an input, for example, to share it. InvalidInput above GPIO 29 or for a count of 0, Busy if a pin is taken.
pub fn pio_pins(&mut self, pio: &rp_pio.Pio, first: u32, count: u32) -> void!hal.Error
Pins.edge_capture
Function · Source
Claim pin
pinfor PIO blockpioand capture its edges (rp_capture.RISING, FALLING, or BOTH) with one of the block’s state machines: a hal.InputCapture whose edge times the state machine latches, in ticks oftick_hz, up to SYS_HZ / 4. rp_capture documents it. Errors as for pio_pins, and InvalidInput for a rate rp_capture.divider refuses; Busy without a free state machine or 14 free instruction slots.
pub fn edge_capture(&mut self, pio: &mut rp_pio.Pio, pin: u32, edges: u32, tick_hz: u32) -> rp_capture.EdgeCapture!hal.Error
Pins.release_edge_capture
Function · Source
Give back a capture from
edge_captureto the block it came from.
pub fn release_edge_capture(&mut self, capture: rp_capture.EdgeCapture, pio: &mut rp_pio.Pio)
Pins.release_pio
Function · Source
Give back a PIO block, once its state machines have been given back to it with
pio.release_state_machine: its pins are disconnected and the block is held in reset, which forgets its programs.
pub fn release_pio(&mut self, pio: rp_pio.Pio)
Pins.release_pwm
Function · Source
Give back a PWM output. Its pin stops driving and is disconnected.
pub fn release_pwm(&mut self, output: rp_pwm.Pwm)
Pins.pwm_timer
Function · Source
Claim PWM slice
slice, 0 to 7, as a periodic timer and counter counting attick_hz: clk_sys divided by a whole number of sixteenths from 1 to 255 15/16, such as 125 MHz, 10 MHz, 1 MHz, or 500 kHz. It is a hal.PeriodicTimer whose periods, up to 65536 ticks, end when the slice’s counter wraps, raising PWM_IRQ_WRAP while it listens, and a hal.TickCounter; rp_pwm.SliceTimer documents it. It drives no pins but takes both outputs of its slice. InvalidInput for another slice or rate, Busy if the slice is in use.
pub fn pwm_timer(&mut self, slice: u32, tick_hz: u32) -> rp_pwm.PwmTimer!hal.Error
Pins.release_pwm_timer
Function · Source
Give back a timer from
pwm_timer.
pub fn release_pwm_timer(&mut self, timer: rp_pwm.PwmTimer)
Pins.release_pwm_input
Function · Source
Give back a PWM input. Its slice stops and its pin is disconnected.
pub fn release_pwm_input(&mut self, input: rp_pwm.PwmCapture)
Pins.release_analog
Function · Source
Give back an analog input or the temperature sensor; the ADC and its clock stop once no input is left.
pub fn release_analog(&mut self, input: rp_adc.Analog)
Pins.release_sampler
Function · Source
Give back the ADC’s sampler: it stops, its pins are disconnected, and the ADC and its clock stop.
pub fn release_sampler(&mut self, sampler: rp_adc.AdcSampler)
Pins.release_i2c
Function · Source
Give back an I2C bus. Its controller is held in reset and its pins are disconnected but still pulled up, so the bus stays idle. A device that holds SDA low after an interrupted transfer, which makes the bus Timeout, can then be freed by claiming SCL as an output and clocking it by hand before claiming the bus again.
pub fn release_i2c(&mut self, bus: designware_i2c.Bus)
Pins.release_spi
Function · Source
Give back an SPI bus: its controller is held in reset and its pins are disconnected.
pub fn release_spi(&mut self, bus: pl022.Bus)
Pins.release_uart
Function · Source
Give back a serial port, once everything written to it is out: its controller is held in reset and its pins are disconnected but still pulled up, so the line stays idle.
pub fn release_uart(&mut self, port: pl011.Port)
Pins.release_i2c_target
Function · Source
Give back an I2C target, as for
release_i2c.
pub fn release_i2c_target(&mut self, target: designware_i2c.TargetBus)
Pins.release_spi_target
Function · Source
Give back an SPI target, as for
release_spi.
pub fn release_spi_target(&mut self, target: pl022.TargetBus)
Usb
Struct · Source
The USB controller, as a USB serial port for the host the board is plugged into. rp_usb documents the port.
pub struct Usb {
// Private implementation fields omitted.
}
Usb.serial
Function · Source
Start the controller as a USB serial port (CDC-ACM) named “RP2040”, with rp_usb’s default identity, and return it. The host enumerates it within about a second; what is written before a terminal opens it is kept, up to rp_usb.TX_CAPACITY bytes. The port is served from the USBCTRL_IRQ interrupt, so it keeps working while the program busy-waits; a program’s own USBCTRL_IRQ handler replaces that. Until it is released, a panic is reported on it, and a host that sets 1200 baud, as
dodo rundoes, restarts the board into its bootloader. Busy if a port is open or clk_sys runs at 48 MHz or less (Power.set_system_hz), Timeout if PLL_USB or the controller does not start.
pub fn serial(&mut self) -> rp_usb.Serial!hal.Error
Usb.serial_with
Function · Source
serial, with theidentitythe host sees, and the pin the USB bootloader lights while its drive is accessed after the host restarts the board into it, or PIN_COUNT for none. Board packages pass the default identity with the board’s name, and the board’s LED.
pub fn serial_with(&mut self, identity: rp_usb.Identity, activity_led: u32) -> rp_usb.Serial!hal.Error
Usb.release_serial
Function · Source
Give back the port: the board disconnects from the host, output not yet sent is dropped, and the controller and its clock stop.
pub fn release_serial(&mut self, serial: rp_usb.Serial)
Pin
Struct · Source
One GPIO driven by the single-cycle I/O block. Implements the std/embedded/hal output and input pin protocols.
pub struct Pin {
// Private implementation fields omitted.
}
Pin.number
Function · Source
The GPIO number.
pub fn number(&self) -> u32
Pin.set_high
Function · Source
pub fn set_high(&mut self)
Pin.set_low
Function · Source
pub fn set_low(&mut self)
Pin.toggle
Function · Source
pub fn toggle(&mut self)
Pin.is_high
Function · Source
pub fn is_high(&self) -> bool
Pin.set_as_output
Function · Source
Drive the pin at its output level, for a line that changes direction such as a half-duplex data line. Pins claimed as inputs start released.
pub fn set_as_output(&mut self)
Pin.set_as_input
Function · Source
Release the pin so another device can drive it; is_high reads it.
pub fn set_as_input(&mut self)
Pin.listen
Function · Source
Raise IO_IRQ_BANK0 on the calling core on
events, any of LEVEL_LOW, LEVEL_HIGH, FALLING_EDGE, and RISING_EDGE, after forgetting edges seen so far. Enable the interrupt in that core’s NVIC too:cortex_m.enable(pin.irq()). Every pin shares the one interrupt; its handler tells them apart withevents. Each core has its own enables, and both cores share the vector table, so the one@interrupt(IO_IRQ_BANK0)handler runs on whichever core listens: calllistenandcortex_m.enablefrom code running on core 1 to handle a pin there.
pub fn listen(&mut self, events: u32)
Pin.ignore
Function · Source
Stop raising the calling core’s interrupt on
events.
pub fn ignore(&mut self, events: u32)
Pin.events
Function · Source
The events the calling core listens to that are raising its interrupt now.
pub fn events(&self) -> u32
Pin.listen_on
Function · Source
listenfor corecore, 0 or 1, from either core. Another number traps.
pub fn listen_on(&mut self, core: u32, events: u32)
Pin.ignore_on
Function · Source
ignorefor corecore, 0 or 1.
pub fn ignore_on(&mut self, core: u32, events: u32)
Pin.events_on
Function · Source
eventsfor corecore, 0 or 1.
pub fn events_on(&self, core: u32) -> u32
Pin.clear_events
Function · Source
Forget the edges among
events, acknowledging them; a handler or task must, or the interrupt stays raised. Levels cannot be cleared.
pub fn clear_events(&mut self, events: u32)
Pin.irq
Function · Source
The interrupt pin events raise, IO_IRQ_BANK0, for cortex_m.enable.
pub fn irq(&self) -> u32
Rtc
Struct · Source
The real-time clock: a calendar that counts whole seconds from clk_rtc, with an alarm. Implements the std/embedded/hal Rtc and RtcAlarm protocols, so
hal.rtc_now(&mut b.rtc)reads it as a date and time. It starts unset at every take(), as a reset stops it, and keeps time while the processor sleeps: in deep sleep only with CLOCK_RTC in the power layer’s sleep set. clk_rtc comes from the crystal, which stops while the chip is dormant, untiluse_clock_inputfeeds it from a clock on GPIO 20 or 22, such as a 32.768 kHz oscillator: then it keeps counting while the chip is dormant, and a timeout in a power.Wake ends a dormant state (4.8.5.5). std/embedded/chip/rp_rtc describes its limits. The alarm raises RTC_IRQ whilelistening, during the alarm’s second, and its handler callsclear_alarm.
pub struct Rtc: hal.Rtc + hal.RtcAlarm {
// Private implementation fields omitted.
}
Rtc.use_clock_input
Function · Source
Feed clk_rtc from the clock on GPIO
pin, 20 (GPIN0) or 22 (GPIN1), ofinput_hz: divided by the smallest whole number that makes it 65536 Hz or less, which must leave a whole number of hertz from 1 kHz up (rp_rtc.calendar_divider). The pin is claimed as an input, with no pull, untilrelease_clock_input. The time keeps counting, though up to a second is lost. InvalidInput for another pin or such a clock, Busy if the pin is taken or a clock input is in use already, Timeout if the RTC does not stop or start; clk_rtc comes from the crystal then.
pub fn use_clock_input(&mut self, pins: &mut Pins, pin: u32, input_hz: u32) -> void!hal.Error
Rtc.release_clock_input
Function · Source
Feed clk_rtc from the crystal again, as take() does, and give back the pin from
use_clock_input; nothing happens without one. Timeout if the RTC does not stop or start.
pub fn release_clock_input(&mut self, pins: &mut Pins) -> void!hal.Error
Rtc.clock_hz
Function · Source
clk_rtc’s rate in hertz.
pub fn clock_hz(&self) -> u32
Rtc.runs_while_dormant
Function · Source
Whether clk_rtc keeps running while the chip is dormant, so that a timeout can end a dormant state: true with a clock input.
pub fn runs_while_dormant(&self) -> bool
Rtc.is_set
Function · Source
pub fn is_set(&mut self) -> bool!hal.Error
Rtc.now_ms
Function · Source
pub fn now_ms(&mut self) -> u64!hal.Error
Rtc.set_ms
Function · Source
InvalidInput past the year 4095.
pub fn set_ms(&mut self, time_ms: u64) -> void!hal.Error
Rtc.resolution_ms
Function · Source
1000: the RTC counts seconds.
pub fn resolution_ms(&self) -> u32
Rtc.set_alarm_ms
Function · Source
Fire at the first whole second at or after
time_ms.
pub fn set_alarm_ms(&mut self, time_ms: u64) -> void!hal.Error
Rtc.alarm_fired
Function · Source
pub fn alarm_fired(&mut self) -> bool!hal.Error
Rtc.clear_alarm
Function · Source
pub fn clear_alarm(&mut self) -> void!hal.Error
Rtc.irq
Function · Source
The alarm’s interrupt: its number for cortex_m.enable, and its vector name, RTC_IRQ, for
@interrupt.
pub fn irq(&self) -> u32
Rtc.listen
Function · Source
Raise RTC_IRQ when the alarm fires. Enable it in the NVIC too:
cortex_m.enable(rtc.irq()).
pub fn listen(&mut self)
Rtc.ignore
Function · Source
Stop raising the interrupt; alarm_fired still reports.
pub fn ignore(&mut self)
Rtc.is_listening
Function · Source
pub fn is_listening(&mut self) -> bool
Timer
Struct · Source
The free-running 64-bit microsecond counter. Implements the std/embedded/hal delay and counter protocols.
pub struct Timer {
// Private implementation fields omitted.
}
Timer.alarm
Function · Source
Claim alarm 0 to 3, to run code at a set time from its interrupt. Busy if it is claimed already; InvalidInput for another number.
pub fn alarm(&mut self, number: u32) -> Alarm!hal.Error
Timer.release_alarm
Function · Source
Disarm an alarm, stop its interrupt, and take it back.
pub fn release_alarm(&mut self, alarm: Alarm)
Timer.now_us
Function · Source
Microseconds since take().
pub fn now_us(&self) -> u64
Timer.tick_hz
Function · Source
The counter as a hal.TickCounter: ticks of 1 MHz, as now_us.
pub fn tick_hz(&self) -> u32
Timer.now_ticks
Function · Source
pub fn now_ticks(&self) -> u64
Timer.delay_us
Function · Source
pub fn delay_us(&mut self, microseconds: u32)
Timer.delay_ms
Function · Source
pub fn delay_ms(&mut self, milliseconds: u32)
Alarm
Struct · Source
One of the timer’s four alarms, claimed with
Timer.alarm(n). An alarm fires once when the microsecond counter reaches the time it was given; withlistenit raises interrupt TIMER_IRQ_0 + n, whose handler callsclearand may schedule the next time. Move it into astatic mut Option<Alarm>for the handler to use. Implements the std/embedded/hal alarm and counter protocols, with times up to 2^31 - 1 us (35 minutes) ahead, and, counting microseconds as ticks of 1 MHz, the compare channel and periodic timer protocols:start(period)fires it every period, without drift, and the interrupt handler callspoll_periodsinstead ofclear. rp_timer.Alarm documents the modes.
pub struct Alarm: hal.Alarm + hal.Compare + hal.PeriodicTimer {
// Private implementation fields omitted.
}
Alarm.number
Function · Source
0 to 3.
pub fn number(&self) -> u32
Alarm.irq
Function · Source
The interrupt the alarm raises: its number for cortex_m.enable, and its vector name, TIMER_IRQ_0 + n, for
@interrupt.
pub fn irq(&self) -> u32
Alarm.now
Function · Source
The low 32 bits of the microsecond counter, which alarm times use.
pub fn now(&self) -> u32
Alarm.schedule_at
Function · Source
Fire when the counter’s low 32 bits equal
time, replacing any earlier time and periodic mode. A time that has just passed fires only after the counter wraps, 71.6 minutes later.
pub fn schedule_at(&mut self, time: u32)
Alarm.schedule_in
Function · Source
Fire
microsecondsfrom now; at least 1, and below 2^31 so the time is still ahead when the write lands.
pub fn schedule_in(&mut self, microseconds: u32)
Alarm.schedule_at_us
Function · Source
Fire when now_us() reaches
time_us, replacing any earlier time and periodic mode, and clearing an earlier firing; the std/embedded/hal alarm protocol. A time already reached fires at once, raising the interrupt if the alarm listens. InvalidInput for a time more than 2^31 - 1 us ahead.
pub fn schedule_at_us(&mut self, time_us: u64) -> void!hal.Error
Alarm.cancel
Function · Source
Disarm, and end periodic mode: a scheduled time no longer fires.
pub fn cancel(&mut self)
Alarm.is_armed
Function · Source
True from scheduling until it fires or is cancelled.
pub fn is_armed(&self) -> bool
Alarm.fired
Function · Source
True once it has fired, until
clear.
pub fn fired(&self) -> bool
Alarm.clear
Function · Source
Acknowledge that it fired. An interrupt handler has to, or the interrupt stays raised and the handler runs again at once.
pub fn clear(&mut self)
Alarm.listen
Function · Source
Raise the interrupt when it fires. Enable the interrupt in the NVIC too:
cortex_m.enable(alarm.irq()).
pub fn listen(&mut self)
Alarm.ignore
Function · Source
Stop raising the interrupt;
firedstill reports.
pub fn ignore(&mut self)
Alarm.is_listening
Function · Source
pub fn is_listening(&self) -> bool
Alarm.now_us
Function · Source
Microseconds since take(), from the whole 64-bit counter, as Timer.now_us reads it.
pub fn now_us(&self) -> u64
Alarm.tick_hz
Function · Source
The compare channel and periodic timer protocols count ticks of 1 MHz: microseconds.
pub fn tick_hz(&self) -> u32
Alarm.now_ticks
Function · Source
pub fn now_ticks(&self) -> u64
Alarm.schedule_at_ticks
Function · Source
As schedule_at_us; hal.Compare.
pub fn schedule_at_ticks(&mut self, ticks: u64) -> void!hal.Error
Alarm.start
Function · Source
Fire every
period_usmicroseconds, the first time one period from now, each a whole number of periods after the start; hal.PeriodicTimer. InvalidInput for 0 or more than 2^31 - 1 us (35 minutes).
pub fn start(&mut self, period_us: u64) -> void!hal.Error
Alarm.poll_periods
Function · Source
How many periods ended since
startor the last call, 0 while none has. Clears the firing and arms the alarm for the next period, so the interrupt handler calls it instead ofclear.
pub fn poll_periods(&mut self) -> u32
Alarm.is_periodic
Function · Source
Whether
startruns it, untilcancelor a schedule.
pub fn is_periodic(&self) -> bool
Alarm.period_us
Function · Source
The period
startset, in microseconds.
pub fn period_us(&self) -> u64
Alarm.sleep_until
Function · Source
Sleep (
wfi) until an interrupt is pending or the counter reachesdeadline, a now_us time; none waits for an interrupt only. With now_us this is the std/embedded/executor clock, so an alarm runs an executor:executor.run(&mut tasks, &mut alarm). The alarm’s interrupt ends the sleep while interrupts are masked and is cleared before they are unmasked, so it needs no@interrupthandler; other interrupts’ handlers run after the return, or once the caller’s own critical section ends. Times more than 35 minutes ahead wake early. Leaves the alarm disarmed and not listening.
pub fn sleep_until(&mut self, deadline: Option<u64>)
Power
Struct · Source
Low power, as
Peripherals.power; implements std/embedded/power’s protocol, whose package comment describes the modes.On the RP2040, a deep sleep stops the clocks outside the sleep set only while both cores deep-sleep; a core 1 that was never started does. Every clock starts in the sleep set, so a deep sleep stops nothing more than a light one until
set_sleep_clockssays otherwise. The clock constants above name the gates. Claiming a peripheral starts its clocks, sodisable_unused_clocksis safe at any time; the timer (CLOCK_TIMER) and DMA (CLOCK_DMA) are not claimed through Pins, so it leaves them to the program.
dormantruns clk_sys from the crystal, stops PLL_SYS and the ring oscillator, and stops the crystal until a pin event, then starts them again. Both cores stop. The microsecond timer pauses meanwhile, sonow_usdoes not count the time spent dormant. It returns Busy while the USB serial port or an ADC input runs, since their 48 MHz clock would stop. Wait until serial ports have sent their last bytes first, as clk_peri slows down to the crystal’s rate during the switch.A timeout in the power.Wake uses the RTC’s alarm (4.8.5.5), so it needs clk_rtc from a clock input (
Rtc.use_clock_input), which runs while the crystal is stopped, and the RTC set: Unsupported without the clock input, Busy while the RTC has no time. The alarm matches whole seconds, so the chip wakes up to two seconds after the timeout. An alarm set through the Rtc is put back afterwards.
set_system_hzruns clk_sys, and with it clk_peri, slower to save power: from the crystal divided by a whole number, with PLL_SYS stopped, or from PLL_SYS at a lower rate. The timer, the watchdog, and the RTC keep their rates, as they count clk_ref or clk_rtc, and the ADC and USB keep their 48 MHz clock; flash reads slow down with clk_sys.
pub struct Power: power.Power {
// Private implementation fields omitted.
}
Power.system_hz
Function · Source
clk_sys in hertz now: SYS_HZ until
set_system_hz.
pub fn system_hz(&self) -> u32
Power.crystal_hz
Function · Source
The crystal’s frequency, the fastest rate
set_system_hzreaches with PLL_SYS stopped.
pub fn crystal_hz(&self) -> u32
Power.set_system_hz
Function · Source
Run clk_sys at exactly
hz: the crystal divided by a whole number up to 256, with PLL_SYS stopped, which saves the most, or else PLL_SYS at a rate it reaches exactly, or SYS_HZ, take()’s rate, again. Peripherals claimed later compute their dividers from the new rate, and a SysTick reload the program set counts it too. InvalidInput for 0, above SYS_HZ, or a rate neither reaches, Busy while a UART, SPI, I2C, PWM, or PIO block is claimed (release them and claim them again afterwards) or while the USB serial port runs andhzis 48 MHz or less, Timeout if PLL_SYS does not lock, leaving clk_sys at the crystal’s rate.
pub fn set_system_hz(&mut self, hz: u32) -> void!hal.Error
Power.mode
Function · Source
pub fn mode(&self) -> power.Mode
Power.set_mode
Function · Source
pub fn set_mode(&mut self, mode: power.Mode)
Power.sleep
Function · Source
pub fn sleep(&mut self)
Power.enabled_clocks
Function · Source
pub fn enabled_clocks(&self) -> power.Clocks
Power.enable_clocks
Function · Source
pub fn enable_clocks(&mut self, clocks: power.Clocks)
Power.disable_clocks
Function · Source
pub fn disable_clocks(&mut self, clocks: power.Clocks) -> void!hal.Error
Power.disable_unused_clocks
Function · Source
pub fn disable_unused_clocks(&mut self) -> power.Clocks
Power.sleep_clocks
Function · Source
pub fn sleep_clocks(&self) -> power.Clocks
Power.set_sleep_clocks
Function · Source
pub fn set_sleep_clocks(&mut self, clocks: power.Clocks)
Power.dormant
Function · Source
pub fn dormant(&mut self, wake: &power.Wake) -> u64!hal.Error