SPI FIFO 目標¶
PxSPIFifoTarget 模擬具有主機可存取 RX 與 TX FIFO 的 SPI 目標裝置。控制器在 MOSI 上時脈輸入的位元組進入 RX FIFO(以事件交給 Python);Python 將資料入隊到 TX FIFO,在後續傳輸中從 MISO 輸出。
資料流¶
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
快速開始¶
將目標掛載到 SPI 匯流排並入隊 TX 資料:
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 載入 TX FIFO。這本身不會驅動匯流排時脈。請在同一條匯流排上使用 PxSPIController 執行傳輸,將已入隊位元組從 MISO 移出。
匯流排接收事件¶
當控制器在 MOSI 上時脈輸入資料時,exerciser 會發出 fifo_target_recv 事件。每個事件帶有 data 欄位(該事件分組收到的位元組),以及標示目前 SPI 傳輸最後一個事件的 last 旗標。
| 欄位 | 類型 | 說明 |
|---|---|---|
data |
bytes |
該事件中收到的位元組 |
last |
bool |
目前傳輸最後一個事件時為 True |
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}")
匯流排傳送事件¶
當控制器完成從 TX FIFO 經 MISO 移出位元組的傳輸時,exerciser 會發出 fifo_target_sent 事件。每個事件回報該次傳輸傳送的位元組數,last 標示該傳輸結束。
| 欄位 | 類型 | 說明 |
|---|---|---|
count |
int |
該次傳輸在 MISO 上傳送的位元組數 |
last |
bool |
目前傳輸最後一個事件時為 True |
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 與狀態查詢不會觸發 recv 或 sent 事件。
另見:事件註冊與處理函式設定。
狀態¶
透過 target.state 或便捷屬性 tx_fill、total_read、total_written 查詢 FIFO 計數:
| 屬性 | 說明 |
|---|---|
tx_fill |
TX FIFO 中目前排隊的位元組數 |
total_read |
自設定以來控制器從目標讀出的累計位元組數 |
total_written |
自設定以來控制器寫入目標的累計位元組數 |
state = target.state
assert state.tx_fill >= 0
assert state.total_read >= 0
assert state.total_written >= 0
控制器存取¶
使用 PxSPIController 在匯流排上操作目標:
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 參考¶
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. |
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. Currently not available now.
TYPE:
|
additional_data |
Get the opaque additional data blob associated with this device.
TYPE:
|
config |
Get the config of the device.
TYPE:
|
tx_fill |
Number of bytes currently in the TX FIFO.
TYPE:
|
total_read |
Total bytes read by the controller from the target since configuration.
TYPE:
|
total_written |
Total bytes written by the controller to the target since configuration.
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.
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 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.
attach_to_bus
¶
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 |
|---|---|
|
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.