spi-controller

CLI reference

glasgow run spi-controller

Initiate transactions on the Motorola SPI bus.

usage: glasgow run spi-controller [-h] [-V SPEC] [--cs PIN] [--sck PIN]
                                  [--copi PIN] [--cipo PIN] [-m MODE]
                                  [-f FREQ]
                                  DATA [DATA ...]
data

hex bytes to exchange with the device

-h, --help

show this help message and exit

-V <spec>, --voltage <spec>

configure I/O port voltage to SPEC (e.g.: ‘3.3’, ‘A=5.0,B=3.3’, ‘A=SA’)

--cs <pin>

bind the applet I/O line ‘cs’ to PIN (default: ‘A0’, required)

--sck <pin>

bind the applet I/O line ‘sck’ to PIN (default: ‘A1’, required)

--copi <pin>

bind the applet I/O line ‘copi’ to PIN (default: ‘A2’, optional)

--cipo <pin>

bind the applet I/O line ‘cipo’ to PIN (default: ‘A3’, optional)

-m {0,1,2,3}, --mode {0,1,2,3}

configure active edge and idle state according to MODE (default: 0)

-f <freq>, --frequency <freq>

set SCK frequency to FREQ kHz (default: 100)

API reference

class glasgow.applet.interface.spi_controller.SPIControllerInterface(logger: Logger, assembly: AbstractAssembly, *, cs: GlasgowPin | None = None, sck: GlasgowPin, copi: GlasgowPin | None = None, cipo: GlasgowPin | None = None, mode: Mode | Literal[0, 1, 2, 3] = spi.Mode.IdleLow_SampleRising)
property mode: Mode

SPI mode.

Cannot be changed while a transaction is active.

property clock: ClockDivisor

SCK clock divisor.

select(index=0)

Perform a transaction.

Starting a transaction asserts index-th chip select signal and configures the mode; ending a transaction deasserts the chip select signal. Methods write(), read(), exchange(), and dummy() may be called while a transaction is active (only) to exchange data on the bus.

For example, to read 4 bytes from an SPI flash, use the following code:

iface.mode = 3
async with iface.select():
    await iface.write([0x03, 0, 0, 0]) # READ = 0x03, address = 0x000000
    data = await iface.read(4)

An empty transaction (where the body does not call write(), read(), exchange(), or dummy()) is allowed and causes chip select activity only, in addition to any clock edges required to switch the SPI bus mode with chip select inactive.

async exchange(data: bytes | bytearray | memoryview) memoryview

Exchange data.

Must be used within a transaction (see select()). Shifts data into the peripheral while shifting len(data) bytes out of the peripheral.

Returns the bytes shifted out.

async write(data: bytes | bytearray | memoryview)

Write data.

Must be used within a transaction (see select()). Shifts data into the peripheral.

async read(count: int) memoryview

Read data.

Must be used within a transaction (see select()). Shifts len(data) bytes out of the peripheral.

Returns the bytes shifted out.

async delay_us(duration: int)

Delay operations.

Delays the following SPI bus operations by duration microseconds.

async delay_ms(duration: int)

Delay operations.

Delays the following SPI bus operations by duration milliseconds. Equivalent to delay_us(duration * 1000).

async synchronize()

Synchronization barrier.

Ensures that once this method returns, all previously submitted operations have completed.