Skip to content

SPI Controller

The PxSPIController class drives clocked SPI transfers on a PxSPIBus. Attach the controller to a bus before you call transfer methods.

Transfers are full duplex. send_data writes MOSI and returns the bytes sampled on MISO during the same transaction. Use dummy_count and dummy_val to clock additional read bytes without meaningful write data. Set should_rise_cs to choose whether CS deasserts when the transaction completes.

Use send_transactions when you need multiple clocked segments or idle gaps between segments.

SCLK timing

Set SCLK timing with PxSPITimingConfiguration. low and high are how many ticks SCLK stays low and high.

A tick is one period of the exerciser base clock.

Tick length

With the current base frequency of 200 MHz, 1 tick is 5 ns. If that frequency changes, convert ticks using the new period.

Each phase is split into two halves that add up to the tick count you set. The first half is total // 2 ticks. The second half gets the rest. Each half must be from 1 to 255 ticks, so low and high must each be from 2 to 510. The default is 10 ticks low and 10 ticks high (50 ns each at 200 MHz).

Field Unit Range Default At 200 MHz
low Ticks SCLK stays low 2 to 510 10 50 ns
high Ticks SCLK stays high 2 to 510 10 50 ns

A controller created with no config uses that default. Omitting timing on PxSPIControllerConfig does the same. A value outside 2 to 510 raises ValueError before the controller is created.

from aqpxlib import AqProtocolExerciser
from aqpxlib.spi import PxSPIBus, PxSPIController, PxSPIControllerConfig, PxSPITimingConfiguration

with AqProtocolExerciser.connect(port=60600) as px:
    spi_bus = PxSPIBus(px)
    # 20 ticks low and 20 ticks high: 100 ns each at 200 MHz.
    timing = PxSPITimingConfiguration(low=20, high=20)
    controller = PxSPIController(
        px,
        config=PxSPIControllerConfig(enabled=1, timing=timing),
    )
    controller.attach_to_bus(spi_bus)

API reference

PxSPIController

PxSPIController(
    exerciser: AqProtocolExerciser,
    config: PxSPIControllerConfig | None = None,
    *args,
    **kwargs
)

An SPI controller device class.

This class provides a high-level interface, to an SPI controller with base SPI functionality. It does not automatically create an actual instance on the Protocol Exerciser until user call the attach_to_bus method.

Typical usage example:

spi_bus = PxSPIBus(exerciser_session)
spi_controller = PxSPIController(exerciser_session)
spi_controller.attach_to_bus(spi_bus)

... # Do any operation from this controller

spi_controller.detach_from_bus()
METHOD DESCRIPTION
add_event_handler

Add an event handler to the current device instance, by event class or event name.

remove_event_handler

Remove an event handler from the current device instance, by event class or event name.

get_event_handlers

Retreive handlers registerd on this device.

on_event

Add an event handler to the instance by decorator, by event class or event name.

match

Return whether a device config describes a device of this class.

update_config

Set the config of the controller.

set_config

Set the config of the controller.

attach_to_bus

Attach the controller to a bus.

update_additional_data

Atomically update additional_data with revision-aware retry.

detach_from_bus

Detach the device from the bus.

perform_operation

Send an operation.

send_data

Perfom a sending data operation to SPI targets.

send_transactions

Send SPI transactions.

check_param

Verify the correctness of the config.

ATTRIBUTE DESCRIPTION
available_events_map

Map each available event name to its id in event topics.

TYPE: dict[str, str]

is_attached

Check if the device is attached to a bus.

TYPE: bool

state

Get the state of the device.

TYPE: DeviceStateT | None

name

Get the name of the device.

TYPE: str

cts_op_name

Get the name of CTS operation.

TYPE: str | None

additional_data

Get the opaque additional data blob associated with this device.

TYPE: bytes

config

Get the config of the device.

TYPE: DeviceConfigT | None

available_events_map

available_events_map: dict[str, str]

Map each available event name to its id in event topics.

is_attached

is_attached: bool

Check if the device is attached to a bus.

state

state: DeviceStateT | None

Get the state of the device.

name

name: str

Get the name of the device.

cts_op_name

cts_op_name: str | None

Get the name of CTS operation.

additional_data

additional_data: bytes

Get the opaque additional data blob associated with this device.

config

config: DeviceConfigT | None

Get the config of the device.

add_event_handler

add_event_handler(event: str, handler: Callable[[Any], Any]) -> None
add_event_handler(event: type[EventT], handler: Callable[[EventT], Any]) -> None
add_event_handler(
    event: str | type[PxAbstractEvent], handler: Callable[[Any], Any]
) -> None

Add an event handler to the current device instance, by event class or event name.

remove_event_handler

remove_event_handler(
    event: str | type[PxAbstractEvent], handler: Callable[[Any], Any] | None = None
) -> None

Remove an event handler from the current device instance, by event class or event name.

get_event_handlers

get_event_handlers(
    filter_by: Callable[[str], bool] | None = None,
) -> dict[str, list[Callable[[Any], Any]]]

Retreive handlers registerd on this device.

Handlers are always scoped to this device; an optional filter_by is applied on friendly event names.

on_event

on_event(event_type: str) -> Callable[[Callable[..., Any]], Callable[..., Any]]
on_event(
    event_type: type[EventT],
) -> Callable[[Callable[[EventT], Any]], Callable[[EventT], Any]]
on_event(
    event_type: str | type[PxAbstractEvent],
) -> Callable[[Callable[..., Any]], Callable[..., Any]]

Add an event handler to the instance by decorator, by event class or event name.

match

match(config: PxAbstractDeviceConfig) -> bool

Return whether a device config describes a device of this class.

update_config

update_config(updater: Callable[[Any], Any]) -> None

Set the config of the controller.

set_config

set_config(config: DeviceConfigT) -> None

Set the config of the controller.

attach_to_bus

attach_to_bus(bus: PxAbstractBus) -> None

Attach the controller to a bus.

This method is used to make the device aware of the bus it is connected to. It is used to set the bus attribute of the device.

Note

This method will not automatically create an actual instance on the Protocol Exerciser until user call the attach_to_bus method.

PARAMETER DESCRIPTION

bus

The bus to attach the device to

TYPE: PxAbstractBus

RAISES DESCRIPTION
ValueError

If the device is already attached to a bus

update_additional_data

update_additional_data(updater: Callable[[bytes], bytes]) -> None

Atomically update additional_data with revision-aware retry.

detach_from_bus

detach_from_bus() -> None

Detach the device from the bus.

The opposite of the attach_to_bus method.

perform_operation

perform_operation(
    operation: PxDeviceOperation, *, timeout: int | None = None
) -> PxDeviceOperation

Send an operation.

PARAMETER DESCRIPTION

operation

Device operation to perform.

TYPE: PxDeviceOperation

timeout

Optional base STOP-handshake wait in milliseconds. When omitted, the instrument uses subsystem defaults.

TYPE: int | None DEFAULT: None

send_data

send_data(
    data: bytes | list[int],
    mode: int = 0,
    dummy_count: int = 0,
    dummy_val: int = 255,
    should_rise_cs: bool = True,
) -> bytes | None

Perfom a sending data operation to SPI targets.

This method sends data to SPI targets, and also read data from targets at the same time.

PARAMETER DESCRIPTION

data

The data to write to the SPI targets. It can be a bytes object or a list of integers.

TYPE: bytes | list[int]

mode

User defined transaction mode.

TYPE: int DEFAULT: 0

dummy_count

The number of dummy bytes used to pad the transaction until the bytes that user expects to read appear.

TYPE: int DEFAULT: 0

dummy_val

User defined dummy byte value.

TYPE: int DEFAULT: 255

should_rise_cs

Determine whether pull CS high after exerciser send the data bytes.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
bytes | None

The data read from the targets.

send_transactions

send_transactions(
    transactions: list[SpiTransaction], *, timeout: int | None = None
) -> list[SpiTransaction]

Send SPI transactions.

This method enqueues a SPI transaction to be sent to the SPI bus. Each element in transactions is a pxmsg.SpiTransaction object, which contains one of:

  • pxmsg.SpiTransactionData (SPI write/read segment)
  • pxmsg.IdleTime (bus idle gap before the next SPI segment)

Use should_rise_cs=False on the segment before an idle gap when chip select must stay low.

The transactions are sent in the order they are provided.

PARAMETER DESCRIPTION

transactions

A list of pxmsg.SpiSendTransaction object to be sent.

TYPE: list[SpiTransaction]

timeout

Optional base STOP-handshake wait in milliseconds.

TYPE: int | None DEFAULT: None

RETURNS DESCRIPTION
PxError

If the transaction fails.

TYPE: list[SpiTransaction]

check_param

check_param(config: PxSPIControllerConfig) -> None

Verify the correctness of the config.

PxSPITimingConfiguration

PxSPITimingConfiguration(low: int = 10, high: int = 10)

SCLK low and high durations, in ticks.

low and high are how many ticks SCLK stays low and high. A tick is one period of the exerciser base clock. At the current base frequency of 200 MHz, one tick is 5 ns.

Each phase is split into two halves that sum to the requested length. The first half is total // 2 ticks and the second half receives the remainder. Each half must be from 1 to 255 ticks, so values must be from 2 to 510. The default for each phase is 10 ticks.

METHOD DESCRIPTION
from_protobuf

Build from a wire SpiTimingConfiguration message.

to_protobuf

Encode as a wire SpiTimingConfiguration message.

validate

Raise ValueError when a phase cannot be split into programmable halves.

from_protobuf

from_protobuf(msg: SpiTimingConfiguration | None) -> Self | None

Build from a wire SpiTimingConfiguration message.

to_protobuf

to_protobuf() -> SpiTimingConfiguration

Encode as a wire SpiTimingConfiguration message.

validate

validate() -> None

Raise ValueError when a phase cannot be split into programmable halves.