Skip to content

Commit dcba1de

Browse files
committed
[ot] python/qemu: ot.spi.spi_device: improve timeout report.
Signed-off-by: Emmanuel Blot <[email protected]>
1 parent 62a8af7 commit dcba1de

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

python/qemu/ot/spi/spi_device.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023-2024, Rivos, Inc.
1+
# Copyright (c) 2023-2025, Rivos, Inc.
22
# All rights reserved.
33

44
"""SPI device proxy.
@@ -13,7 +13,7 @@
1313
SHUT_RDWR, timeout as LegacyTimeoutError)
1414
from struct import calcsize as scalc, pack as spack
1515
from time import sleep, time as now
16-
from typing import Optional
16+
from typing import Optional, Union
1717

1818

1919
class SpiDevice:
@@ -94,15 +94,18 @@ def connect(self, host: str, port: int) -> None:
9494
self._socket.settimeout(None)
9595
self._socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
9696

97-
def quit(self) -> None:
97+
def disconnect(self) -> None:
9898
"""Close the communication socket."""
9999
if self._socket:
100100
self._socket.shutdown(SHUT_RDWR)
101101
self._socket.close()
102102
self._socket = None
103103

104+
quit = disconnect
105+
"""Old API."""
106+
104107
def transmit(self, cmd: Optional[int] = None,
105-
in_payload: Optional[bytes | bytearray | int] = None,
108+
in_payload: Union[bytes, bytearray, int, None] = None,
106109
out_len: int = 0, release: bool = True) -> bytes:
107110
"""SPI data transfer.
108111
@@ -137,13 +140,13 @@ def wait_idle(self, timeout: float = 1.0, pace: float = 0.0005) -> None:
137140
available after this delay
138141
:param pace: delay between each flash device poll request.
139142
"""
140-
timeout += now()
143+
expire = timeout + now()
141144
while True:
142145
status = self.read_status_register()
143146
if not status & self.BUSY:
144147
break
145-
if now() > timeout:
146-
raise TimeoutError('Flash stuck to busy')
148+
if now() > expire:
149+
raise TimeoutError(f'Busy bit stuck for {timeout:.1f}s')
147150
sleep(pace)
148151

149152
def read_jedec_id(self) -> tuple[int, bytes]:
@@ -309,8 +312,8 @@ def _build_cs_header(self, size: int, release: bool = True) -> bytes:
309312
return header
310313

311314
def _transmit(self, cmd: Optional[int] = None,
312-
in_payload: Optional[bytes | bytearray | int] = None,
313-
out_len: int = 0, release: bool = True) -> bytes:
315+
in_payload: Union[bytes, bytearray, int, None] = None,
316+
out_len: int = 0, release: bool = True) -> bytes:
314317
if isinstance(in_payload, int):
315318
in_payload = bytes([0xff] * in_payload)
316319
elif in_payload is not None:

0 commit comments

Comments
 (0)