Skip to content

SPI FIFO Target

The PxSPIFifoTarget class emulates an SPI target with host-accessible RX and TX FIFOs. The controller clocks bytes on MOSI into the RX FIFO (Python receives fifo_target_recv events). You enqueue bytes into the TX FIFO; later transfers shift them out on MISO.

Data flow

Controller clocks MOSI  →  RX FIFO  →  fifo_target_recv events  →  Python
Python enqueue          →  TX FIFO  →  MISO on later transfers
Completed transfer      →  fifo_target_sent events  →  Python

Quick start

Attach a target to an SPI bus and enqueue TX data:

from aqpxlib import AqProtocolExerciser
from aqpxlib.spi import PxSPIBus, PxSPIFifoTarget

with AqProtocolExerciser.connect(port=60600) as px:
    spi_bus = PxSPIBus(px)
    target = PxSPIFifoTarget(px)
    target.attach_to_bus(spi_bus)

    target.enqueue([0x01, 0x02, 0x03])

    state = target.state
    print(f"TX fill: {state.tx_fill}")
    print(f"Total read: {state.total_read}")
    print(f"Total written: {state.total_written}")

    target.detach_from_bus()

Enqueue (host → bus)

Use enqueue to load the TX FIFO. This does not clock the bus by itself. Use a PxSPIController on the same bus to run a transfer that shifts enqueued bytes out on MISO.

target.enqueue(b"\xde\xad\xbe\xef")

Bus receive events

When the controller clocks data in on MOSI, the exerciser emits fifo_target_recv events. Each event carries a data field with the bytes received in that event packet, and a last flag that marks the final event for the current SPI transfer.

Field Type Description
data bytes Bytes received in this event
last bool True when this is the last event for the current transfer
from aqpxlib.event import PxFifoTargetRecvEvent

@target.on_event(PxFifoTargetRecvEvent)
def on_recv(recv_event: PxFifoTargetRecvEvent):
    print(f"Recv {recv_event.data.hex()} last={recv_event.last}")

Bus sent events

When the controller completes a transfer that drains bytes from the TX FIFO on MISO, the exerciser emits fifo_target_sent events. Each event reports how many bytes were sent in the completed transfer, with last marking the end of that transfer.

Field Type Description
count int Bytes sent on MISO in this transfer
last bool True when this is the last event for the current transfer
from aqpxlib.event import PxFifoTargetSentEvent

@target.on_event(PxFifoTargetSentEvent)
def on_sent(sent_event: PxFifoTargetSentEvent):
    print(f"Sent {sent_event.count} bytes last={sent_event.last}")

enqueue and state queries do not emit recv or sent events.

See also: Event Registration and Handler Setup.

State

Query FIFO counters via target.state or the convenience properties tx_fill, total_read, and total_written:

Property Description
tx_fill Bytes currently queued in the TX FIFO
total_read Cumulative bytes read by the controller from the target since configure
total_written Cumulative bytes written by the controller to the target since configure
state = target.state
assert state.tx_fill >= 0
assert state.total_read >= 0
assert state.total_written >= 0

Controller access

Use a PxSPIController to exercise the target on the bus:

from aqpxlib import AqProtocolExerciser
from aqpxlib.event import PxFifoTargetRecvEvent
from aqpxlib.spi import PxSPIBus, PxSPIController, PxSPIFifoTarget

with AqProtocolExerciser.connect(port=60600) as px:
    spi_bus = PxSPIBus(px)
    target = PxSPIFifoTarget(px)
    target.attach_to_bus(spi_bus)

    controller = PxSPIController(px)
    controller.attach_to_bus(spi_bus)

    received: list[int] = []

    @target.on_event(PxFifoTargetRecvEvent)
    def on_recv(recv_event: PxFifoTargetRecvEvent) -> None:
        received.extend(recv_event.data)

    target.enqueue([0xAA, 0xBB, 0xCC])
    read_back = controller.send_data([0x00, 0x00, 0x00])
    assert list(read_back) == [0xAA, 0xBB, 0xCC]

    controller.send_data([0x11, 0x22])
    assert received == [0x11, 0x22]

    controller.detach_from_bus()
    target.detach_from_bus()

API reference

PxSPIFifoTarget

PxSPIFifoTarget(
    exerciser: AqProtocolExerciser,
    config: PxSPIFifoTargetConfig | None = None,
    *args,
    **kwargs
)

Bases: PxAbstractTarget[PxSPIFifoTargetConfig, PxSPIFifoTargetState]

SPI FIFO target.

Emulates an SPI slave with host-accessible RX and TX FIFOs. Bytes clocked in on MOSI are delivered as packeted fifo_target_recv events. Bytes enqueued from the host are presented on MISO during subsequent transfers; completed transfers are reported as fifo_target_sent events.

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 target.

set_config

Set the config of the target.

attach_to_bus

Attach the target 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.

enqueue

Enqueue bytes into the host-side TX FIFO.

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. Currently not available now.

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

tx_fill

Number of bytes currently in the TX FIFO.

TYPE: int

total_read

Total bytes read by the controller from the target since configuration.

TYPE: int

total_written

Total bytes written by the controller to the target since configuration.

TYPE: int

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. Currently not available now.

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.

tx_fill

tx_fill: int

Number of bytes currently in the TX FIFO.

total_read

total_read: int

Total bytes read by the controller from the target since configuration.

total_written

total_written: int

Total bytes written by the controller to the target since configuration.

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 target.

set_config

set_config(config: DeviceConfigT) -> None

Set the config of the target.

attach_to_bus

attach_to_bus(bus: PxAbstractBus) -> None

Attach the target 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

enqueue

enqueue(data: list[int] | bytes) -> None

Enqueue bytes into the host-side TX FIFO.