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. |
is_attached |
Check if the device is attached to a bus.
TYPE:
|
state |
Get the state of the device.
TYPE:
|
name |
Get the name of the device.
TYPE:
|
cts_op_name |
Get the name of CTS operation.
TYPE:
|
additional_data |
Get the opaque additional data blob associated with this device.
TYPE:
|
config |
Get the config of the device.
TYPE:
|
available_events_map
¶
Map each available event name to its id in event topics.
additional_data
¶
additional_data: bytes
Get the opaque additional data blob associated with this device.
add_event_handler
¶
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 | 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
¶
Set the config of the controller.
attach_to_bus
¶
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 |
|---|---|
|
The bus to attach the device to
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the device is already attached to a bus |
update_additional_data
¶
Atomically update additional_data with revision-aware retry.
detach_from_bus
¶
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_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 |
|---|---|
|
The data to write to the SPI targets. It can be a bytes object or a list of integers. |
|
User defined transaction mode.
TYPE:
|
|
The number of dummy bytes used to pad the transaction until the bytes that user expects to read appear.
TYPE:
|
|
User defined dummy byte value.
TYPE:
|
|
Determine whether pull CS high after exerciser send the data bytes.
TYPE:
|
| 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 |
|---|---|
|
A list of
TYPE:
|
|
Optional base STOP-handshake wait in milliseconds.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PxError
|
If the transaction fails.
TYPE:
|
check_param
¶
Verify the correctness of the config.
PxSPITimingConfiguration
¶
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 |
to_protobuf |
Encode as a wire |
validate |
Raise |