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