API referencestd/embedded

std/embedded/peripheral/pl022

Public declarations, types, methods, and source contracts for std/embedded/peripheral/pl022.

On this page

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/peripheral/pl022"

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.

Alias Package Source file
cpu core/cpu pl022.dodo
hal std/embedded/hal pl022.dodo
ptr core/ptr pl022.dodo

MIN_FRAME_BITS

Constant · Source

The frame sizes the controller supports, in bits.

pub const MIN_FRAME_BITS: u32 = 4

MAX_FRAME_BITS

Constant · Source

pub const MAX_FRAME_BITS: u32 = 16

FrameFormat

Enum · Source

How frames look on the wire.

pub enum FrameFormat {
    // Motorola SPI, the usual format, with the clock polarity and phase of
    // the bus's hal.SpiMode. The frame signal stays low during a frame.
    Motorola,
    // Texas Instruments synchronous serial: the frame signal pulses high
    // for one clock before each frame, and data changes on rising edges
    // and is sampled on falling ones. The SpiMode does not apply.
    TexasInstruments,
    // National Microwire, half duplex: the master sends an 8-bit control
    // word, waits one clock, and receives a frame of the configured size
    // while the frame signal stays low. Words sent carry the control word
    // in their low 8 bits. The SpiMode does not apply.
    Microwire
}

Divisors

Struct · Source

The clock dividers for one SPI clock frequency, from divisors.

pub struct Divisors {
    // The controller's input clock, in hertz.
    pub clock_hz: u32
    // The prescaler, an even number from 2 to 254.
    pub prescale: u32
    // The second divider, 1 to 256.
    pub rate: u32
    // The resulting SPI clock, clock_hz / (prescale * rate), in hertz.
    pub frequency_hz: u32
}

divisors

Function · Source

The fastest SPI clock from an input clock of clock_hz that does not exceed frequency_hz. The fastest possible is clock_hz / 2; a request above it gets that. InvalidInput below clock_hz / 65024, about 1.9 kHz at 125 MHz, the slowest possible.

pub fn divisors(clock_hz: u32, frequency_hz: u32) -> Divisors!hal.Error

Controller

Struct · Source

An SPI bus master on one PL022 with registers R, any hal.Registers, such as a hal.RegisterBlock or a simulated controller in a test.

pub struct Controller<R: hal.Registers>: hal.Spi + hal.SpiPoll {
    // Private implementation fields omitted.
}

Controller.new

Function · Source

Configure a controller fresh out of reset as a master with divisors, computed from the clock that feeds it, 8-bit Motorola frames in mode, and enable it.

pub fn new(registers: R, divisors: Divisors, mode: hal.SpiMode) -> Controller<R>

Controller.frequency_hz

Function · Source

The SPI clock frequency, in hertz.

pub fn frequency_hz(&self) -> u32

Controller.set_frequency

Function · Source

Change the clock between transfers, for example to talk to devices with different limits on a shared bus. InvalidInput leaves it unchanged.

pub fn set_frequency(&mut self, frequency_hz: u32) -> void!hal.Error

Controller.set_mode

Function · Source

Change the clock polarity and phase between transfers.

pub fn set_mode(&mut self, mode: hal.SpiMode)

Controller.frame_bits

Function · Source

The frame size, in bits.

pub fn frame_bits(&self) -> u32

Controller.set_frame_bits

Function · Source

Change the frame size between transfers: MIN_FRAME_BITS to MAX_FRAME_BITS. Frames above 8 bits move with the word methods only. InvalidInput leaves it unchanged.

pub fn set_frame_bits(&mut self, bits: u32) -> void!hal.Error

Controller.set_frame_format

Function · Source

Change the frame format between transfers.

pub fn set_frame_format(&mut self, format: FrameFormat)

Controller.set_loopback

Function · Source

Connect the transmitter to the receiver inside the controller, so every transfer reads back what it sent and the pins stay idle. Meant for self-tests.

pub fn set_loopback(&mut self, on: bool)

Controller.transfer

Function · Source

Send bytes and replace each with the byte received at the same time. Overrun if received bytes were lost to a full receive FIFO, which the driver’s pacing prevents unless something else used the controller; Unsupported for frames above 8 bits.

pub fn transfer(&mut self, bytes: &mut[u8]) -> void!hal.Error

Controller.write

Function · Source

Send bytes and discard what arrives. Returns once the last bit has left, so a chip select can be raised right after. Unsupported for frames above 8 bits.

pub fn write(&mut self, bytes: &[u8]) -> void!hal.Error

Controller.transfer_words

Function · Source

transfer and write with one frame per word, for frames of any size. Bits above the frame size are ignored when sending and zero when receiving.

pub fn transfer_words(&mut self, words: &mut[u16]) -> void!hal.Error

Controller.write_words

Function · Source

pub fn write_words(&mut self, words: &[u16]) -> void!hal.Error

Controller.dma_at

Function · Source

Move bytes between buffer and the bus by DMA until the returned DmaBus is dropped: tx feeds the transmit FIFO and rx empties the receive FIFO, while the processor does other work. data is the address of the controller’s data register as the channels see it, which chip packages know; Bus.dma passes it. Unsupported for frames above 8 bits; InvalidInput for an empty buffer; Busy if a channel is still running.

pub fn dma_at(&mut self, data: usize, tx: &mut dyn hal.DmaChannel, rx: &mut dyn hal.DmaChannel, buffer: &mut[u8]) -> DmaBus!hal.Error from(self, tx, rx, buffer)

Controller.poll_transfer

Function · Source

Advance transfer without waiting: each poll moves the frames the FIFOs allow now, never more in flight than the receive FIFO holds, and returns true once every byte has been exchanged; see hal.SpiPoll. Errors as for transfer, and InvalidInput for a slice of another length, or a write, while a polled transfer runs. Frames stop between polls, which SPI devices accept, as the master drives the clock.

pub fn poll_transfer(&mut self, bytes: &mut[u8]) -> bool!hal.Error

Controller.poll_write

Function · Source

Advance write without waiting, as for poll_transfer: true once the last bit has left, so a chip select can be raised right after.

pub fn poll_write(&mut self, bytes: &[u8]) -> bool!hal.Error

Controller.cancel

Function · Source

Abandon a polled transfer: wait for the frames already queued, at most a FIFO’s worth, and discard what they bring.

pub fn cancel(&mut self)

Controller.is_busy

Function · Source

True while a polled transfer runs.

pub fn is_busy(&self) -> bool

Controller.release

Function · Source

Disable the controller and give back its registers.

pub fn release(self) -> R

DmaBus

Struct · Source

An SPI bus master that moves bytes between a buffer it holds and the bus by DMA, from Bus.dma(tx, rx, buffer). A transfer runs while the program does other work, without gaps between frames at any clock rate:

dma := bus.dma(&mut tx, &mut rx, &mut buffer)! fill dma.buffer()!, then: dma.start_transfer(count)! … other work, or return Pending from a task … if !dma.is_busy() { dma.finish()! }

The bus holds the controller, both channels, and the buffer until it is dropped, and lends the buffer out only between transfers, so the program never touches memory the channels are moving. A task keeps the DmaBus in a field and waits for rx’s signal (rp_dma.Channel.set_signal), which is raised once the last byte has arrived. Dropping the bus aborts a running transfer; the device then saw only part of it.

pub struct DmaBus: hal.SpiDma {
    // Private implementation fields omitted.
}

DmaBus.buffer

Function · Source

The buffer, to fill before a transfer or read after one. Busy from start_transfer or start_write until finish.

pub fn buffer(&mut self) -> &mut[u8]!hal.Error from(self)

DmaBus.capacity

Function · Source

The buffer’s length, the most a transfer moves.

pub fn capacity(&self) -> usize

DmaBus.start_transfer

Function · Source

Start sending buffer[..count] and replacing each byte with the one received at the same time, and return at once. InvalidInput for 0 or more than the buffer holds; Busy until the last transfer is finished.

pub fn start_transfer(&mut self, count: usize) -> void!hal.Error

DmaBus.start_write

Function · Source

Start sending buffer[..count] and discard what arrives, as for start_transfer; the buffer stays as it is.

pub fn start_write(&mut self, count: usize) -> void!hal.Error

DmaBus.is_busy

Function · Source

True while a transfer runs: until the last byte has been received.

pub fn is_busy(&mut self) -> bool

DmaBus.finish

Function · Source

Wait until the running transfer is done, if one is, and end it, so the buffer is free again. Overrun if received bytes were lost, which happens only if the receive channel was held up, and Bus if a channel hit a bus error; either way the transfer is over.

pub fn finish(&mut self) -> void!hal.Error

DmaBus.poll_finish

Function · Source

Without waiting: true once no transfer runs, ending a transfer that is done, false while one runs. Errors as for finish.

pub fn poll_finish(&mut self) -> bool!hal.Error

DmaBus.abort

Function · Source

Stop a running transfer; the device saw only part of it.

pub fn abort(&mut self)

DmaBus.transfer

Function · Source

start_transfer, then finish.

pub fn transfer(&mut self, count: usize) -> void!hal.Error

DmaBus.write

Function · Source

start_write, then finish.

pub fn write(&mut self, count: usize) -> void!hal.Error

Target

Struct · Source

An SPI target (slave) on one PL022 with registers R: a master drives the clock and the chip select, and the target shifts out what it queued while it shifts in what the master sends. The controller follows a clock of up to a twelfth of its own input clock (10.4 MHz at 125 MHz).

Queue replies before the master clocks them: a frame the master clocks while the transmit FIFO is empty repeats the last frame sent, and the controller cannot tell. In modes 0 and 2 the PL022 needs the chip select raised between every two frames; masters that keep it low for a whole transfer need modes 1 or 3. The frame size and format work as for Controller.

pub struct Target<R: hal.Registers>: hal.SpiTarget {
    // Private implementation fields omitted.
}

Target.new

Function · Source

Configure a controller fresh out of reset as a target with 8-bit Motorola frames in mode, and enable it.

pub fn new(registers: R, mode: hal.SpiMode) -> Target<R>

Target.set_mode

Function · Source

Change the clock polarity and phase, while the master is idle.

pub fn set_mode(&mut self, mode: hal.SpiMode)

Target.frame_bits

Function · Source

The frame size, in bits.

pub fn frame_bits(&self) -> u32

Target.set_frame_bits

Function · Source

Change the frame size, as for Controller.set_frame_bits.

pub fn set_frame_bits(&mut self, bits: u32) -> void!hal.Error

Target.set_frame_format

Function · Source

Change the frame format, while the master is idle.

pub fn set_frame_format(&mut self, format: FrameFormat)

Target.set_output

Function · Source

Drive the data output while selected (on, the default), or never, for a target that only listens on a bus where another target answers.

pub fn set_output(&mut self, on: bool)

Target.queue

Function · Source

Queue the start of bytes as replies, as many as the transmit FIFO has room for, without waiting; returns how many. Unsupported for frames above 8 bits.

pub fn queue(&mut self, bytes: &[u8]) -> usize!hal.Error

Target.read

Function · Source

Move what the master sent, up to buffer.len frames, into buffer without waiting; returns how many. Overrun, after them, if frames were lost to a full receive FIFO; the program then has to find the next frame boundary with the master, for example at the next chip select. Unsupported for frames above 8 bits.

pub fn read(&mut self, buffer: &mut[u8]) -> usize!hal.Error

Target.transfer

Function · Source

Exchange bytes with the master: send them, replacing each with the byte received in its place. Waits as long as the master takes, and keeps the transmit FIFO ahead of the master but never more frames in flight than the receive FIFO holds; a master that clocks faster than the program refills gets repeated frames. Overrun as for read, Unsupported for frames above 8 bits.

pub fn transfer(&mut self, bytes: &mut[u8]) -> void!hal.Error

Target.queue_words

Function · Source

queue, read, and transfer with one frame per word, for frames of any size.

pub fn queue_words(&mut self, words: &[u16]) -> usize

Target.read_words

Function · Source

pub fn read_words(&mut self, buffer: &mut[u16]) -> usize!hal.Error

Target.transfer_words

Function · Source

pub fn transfer_words(&mut self, words: &mut[u16]) -> void!hal.Error

Target.release

Function · Source

Disable the controller and give back its registers.

pub fn release(self) -> R

Bus

Struct · Source

A PL022 on the chip, as chip packages hand it to applications. It implements hal.Spi like Controller but keeps the register block: RegisterBlock trusts its owner with every offset, which only the driver may be.

pub struct Bus: hal.Spi + hal.SpiPoll {
    // Private implementation fields omitted.
}

Bus.at

Function · Source

SAFETY: as for hal.RegisterBlock.at, with base a PL022 whose reset and pins the caller has set up and that only this bus uses.

pub unsafe fn at(base: usize, divisors: Divisors, mode: hal.SpiMode) -> Bus

Requires an unsafe context. Follow the source safety preconditions and the linked guide before calling this API.

Bus.transfer

Function · Source

pub fn transfer(&mut self, bytes: &mut[u8]) -> void!hal.Error

Bus.write

Function · Source

pub fn write(&mut self, bytes: &[u8]) -> void!hal.Error

Bus.poll_transfer

Function · Source

The transfers above without waiting; see Controller.poll_transfer.

pub fn poll_transfer(&mut self, bytes: &mut[u8]) -> bool!hal.Error

Bus.poll_write

Function · Source

pub fn poll_write(&mut self, bytes: &[u8]) -> bool!hal.Error

Bus.cancel

Function · Source

pub fn cancel(&mut self)

Bus.is_busy

Function · Source

pub fn is_busy(&self) -> bool

Bus.frequency_hz

Function · Source

The clock frequency, in hertz: the fastest the controller can make that does not exceed the requested one.

pub fn frequency_hz(&self) -> u32

Bus.set_frequency

Function · Source

Change the clock between transfers, for devices with different limits on one bus.

pub fn set_frequency(&mut self, frequency_hz: u32) -> void!hal.Error

Bus.set_mode

Function · Source

Change the clock polarity and phase between transfers.

pub fn set_mode(&mut self, mode: hal.SpiMode)

Bus.frame_bits

Function · Source

The frame size, in bits.

pub fn frame_bits(&self) -> u32

Bus.set_frame_bits

Function · Source

Change the frame size: 4 to 16 bits. Frames above 8 bits move with the word methods only.

pub fn set_frame_bits(&mut self, bits: u32) -> void!hal.Error

Bus.set_frame_format

Function · Source

Change the frame format. The TI and Microwire formats need the controller’s own chip select on a pin.

pub fn set_frame_format(&mut self, format: FrameFormat)

Bus.transfer_words

Function · Source

transfer and write with one frame per word, for frames of any size.

pub fn transfer_words(&mut self, words: &mut[u16]) -> void!hal.Error

Bus.write_words

Function · Source

pub fn write_words(&mut self, words: &[u16]) -> void!hal.Error

Bus.set_loopback

Function · Source

Loop the transmitter back to the receiver inside the chip, for self-tests: transfers read back what they send.

pub fn set_loopback(&mut self, on: bool)

Bus.dma

Function · Source

Move bytes between buffer and the bus by DMA, tx feeding it and rx emptying it, until the returned DmaBus is dropped; see DmaBus. Unsupported for frames above 8 bits; InvalidInput for an empty buffer or a channel that cannot serve this controller.

pub fn dma(&mut self, tx: &mut dyn hal.DmaChannel, rx: &mut dyn hal.DmaChannel, buffer: &mut[u8]) -> DmaBus!hal.Error from(self, tx, rx, buffer)

Bus.release

Function · Source

Disable the controller and return its base address, for the chip package to take the bus back.

pub fn release(self) -> usize

TargetBus

Struct · Source

A PL022 on the chip as a target, as chip packages hand it to applications; see Target.

pub struct TargetBus: hal.SpiTarget {
    // Private implementation fields omitted.
}

TargetBus.at

Function · Source

SAFETY: as for Bus.at.

pub unsafe fn at(base: usize, mode: hal.SpiMode) -> TargetBus

Requires an unsafe context. Follow the source safety preconditions and the linked guide before calling this API.

TargetBus.set_mode

Function · Source

pub fn set_mode(&mut self, mode: hal.SpiMode)

TargetBus.frame_bits

Function · Source

pub fn frame_bits(&self) -> u32

TargetBus.set_frame_bits

Function · Source

pub fn set_frame_bits(&mut self, bits: u32) -> void!hal.Error

TargetBus.set_frame_format

Function · Source

pub fn set_frame_format(&mut self, format: FrameFormat)

TargetBus.set_output

Function · Source

pub fn set_output(&mut self, on: bool)

TargetBus.queue

Function · Source

Queue replies without waiting; see Target.queue.

pub fn queue(&mut self, bytes: &[u8]) -> usize!hal.Error

TargetBus.read

Function · Source

What the master sent, without waiting; see Target.read.

pub fn read(&mut self, buffer: &mut[u8]) -> usize!hal.Error

TargetBus.transfer

Function · Source

Exchange bytes with the master, waiting for it; see Target.transfer.

pub fn transfer(&mut self, bytes: &mut[u8]) -> void!hal.Error

TargetBus.queue_words

Function · Source

pub fn queue_words(&mut self, words: &[u16]) -> usize

TargetBus.read_words

Function · Source

pub fn read_words(&mut self, buffer: &mut[u16]) -> usize!hal.Error

TargetBus.transfer_words

Function · Source

pub fn transfer_words(&mut self, words: &mut[u16]) -> void!hal.Error

TargetBus.release

Function · Source

Disable the controller and return its base address, for the chip package to take it back.

pub fn release(self) -> usize

Search every guide, reference page, and API declaration.