Skip to main content
Perform full-duplex SPI (Serial Peripheral Interface) communication with devices connected to a Lager Box.

Import

Methods

Method Reference

Net.get(name, type=NetType.SPI)

Get an SPI net by name.
Parameters: Returns: SPI Net instance

config(mode, bit_order, frequency_hz, word_size, cs_active, cs_mode)

Configure SPI bus parameters. Only explicitly-provided parameters are changed; omitted parameters retain their stored values. config() changes the driver in the running script. It does not write the values to saved_nets.json. To store a configuration on the net, use lager spi NET config from the CLI.

SPI Modes

read(n_words, fill, keep_cs, output_format)

Read data from an SPI device. Sends fill bytes while receiving data (full duplex).
Returns: list[int] - Received words as integers (when output_format="list")

read_write(data, keep_cs, output_format)

Perform simultaneous full-duplex SPI read and write. Sends data while simultaneously receiving the response.
Returns: list[int] - Received words (same length as transmitted data)

transfer(n_words, data, fill, keep_cs, output_format)

Perform SPI transfer with automatic padding or truncation. If data is shorter than n_words, it is padded with the fill value. If longer, it is truncated.
Returns: list[int] - Received words

write(data, keep_cs)

Write data to an SPI device, discarding the response. Convenience method for write-only operations.

get_config()

Get the raw net configuration dictionary.
Returns: dict - Full net configuration including name, role, instrument, and params

Output Formats

The output_format parameter controls how data is returned. Hex formatting is word-size-aware:

Examples

Read SPI Flash JEDEC ID

Read Flash Memory

Multi-Part Transaction with keep_cs

A LabJack U3 in auto CS mode refuses keep_cs=True. On a U3, use the manual CS example below.

Manual Chip Select on a LabJack U3

A U3 drives CS low for each transfer and releases it at the end. To hold CS across transfers, or to drive an active-high CS, use an SPI net without CS and a GPIO net for CS. An SPI net without CS uses manual CS mode.

Write to SPI Flash

16-bit Word Mode

Supported Hardware

Multi-channel FTDI adapters

An FTDI net takes its channel from params.interface on the net record, accepting A-D or 0-3. Set it with lager nets add --interface. A net with no interface uses channel A, the only choice on a single-channel FT232H. SPI runs over the FTDI part’s MPSSE engine, and on an FT4232H only channels A and B have one. lager nets add refuses C or D for an SPI net. A net record edited by hand with C or D fails when the net opens, and the error names the channel. See Nets for how channels are assigned across net types on one chip.

LabJack U3

  • SPI on a U3 needs U3 hardware version 1.21 or later.
  • A transfer is at most 50 bytes: 50 words at an 8-bit word size, 25 words at 16 bits, or 12 words at 32 bits. A larger transfer raises an error.
  • The clock runs from about 5.4 kHz to 71.4 kHz, and it is never faster than frequency_hz. A request outside that range is clamped, with a warning on the standard error output of the script.
  • A net with no stored frequency_hz runs at the maximum rate.
  • cs_active="high" raises an error. keep_cs=True raises an error in auto CS mode.
  • FIO0-FIO3 cannot carry SPI. The driver raises an error for a net that uses them.
See SPI for the pin order and for how to create a U3 net with other pins.

Notes

  • Net must be configured as NetType.SPI
  • All SPI operations are full duplex; data is sent and received simultaneously
  • write() performs a full-duplex transfer but discards the received data
  • keep_cs=True holds the chip select line asserted between calls for multi-part transactions
  • transfer() pads short data arrays with the fill value or truncates long arrays to n_words
  • LabJack T7 supports up to 56 bytes per transaction and a maximum of approximately 800 kHz. A multi-byte transfer with a request between about 1 kHz and 800 kHz runs at about 1 kHz
  • LabJack U3 supports up to 50 bytes per transaction, from about 5.4 kHz to 71.4 kHz
  • Aardvark uses GPIO bit-bang mode; actual speed is limited by USB round-trip time regardless of frequency_hz
  • config() applies to the running script only. lager spi NET config stores the configuration in saved_nets.json
  • LSB-first mode (bit_order="lsb") uses software bit reversal on LabJack T7