跳轉到

SPI 控制器

PxSPIController 在 PxSPIBus 上實作 SPI 控制器。呼叫傳輸方法前須將控制器掛載到匯流排。

SPI 傳輸為全雙工:send_data 驅動 MOSI,並在同一時脈事務中傳回 MISO 上取樣的位元組。需要額外讀時脈而無有意義寫資料時,可使用 dummy_count 與 dummy_val。透過 should_rise_cs 控制事務結束後是否釋放 CS。

需要多段時脈傳輸或段間閒置間隔時,使用 send_transactions。

SCLK 時序

用 PxSPITimingConfiguration 設定 SCLK 時序。low 與 high 是 SCLK 維持低電位與高電位的 tick 數。

一個 tick 是儀器基頻時脈的一個週期。

tick 時長

目前基頻為 200 MHz,1 tick = 5 ns。基頻改變後,請用新的時脈週期換算 tick。

每個相位分成兩半,兩者之和等於所設定的 tick 數。前半為 total // 2 個 tick,後半為餘數。每一半必須介於 1 到 255 個 tick,因此 low 與 high 各自必須介於 2 到 510。預設低電位與高電位各為 10 tick(在 200 MHz 下各為 50 ns)。

欄位 單位 範圍 預設值 200 MHz 下
low SCLK 維持低電位的 tick 數 2 到 510 10 50 ns
high SCLK 維持高電位的 tick 數 2 到 510 10 50 ns

不傳入 config 而建立的控制器使用該預設值。在 PxSPIControllerConfig 中省略 timing 時相同。超出 2 到 510 的值會在建立控制器之前引發 ValueError。

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

with AqProtocolExerciser.connect(port=60600) as px:
    spi_bus = PxSPIBus(px)
    # 低電位與高電位各 20 tick:在 200 MHz 下各為 100 ns。
    timing = PxSPITimingConfiguration(low=20, high=20)
    controller = PxSPIController(
        px,
        config=PxSPIControllerConfig(enabled=1, timing=timing),
    )
    controller.attach_to_bus(spi_bus)

API 參考

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.