-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (78 loc) · 2.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Program entry point"""
import serial as sr
import waiting as wt
import sys
import io
# Serial communication timeout in ms
SERIAL_TIMEOUT_MS = 1e4
# Messages for changing debugee clock speed
CLOCK_DOWN = "8\n"
CLOCK_UP = "1\n"
# Messages for toggling debugWIRE
DWIRE_ON = "+\n"
DWIRE_OFF = "-\n"
# Messages for entering debugging mode
ENTER_DEBUG_SESSION = "b\n"
class WayneProbeSession:
def __init__(self, port: str, ostream: io.StringIO):
"""
Bind a debugging session to a serial port
port -- port of the Wayne probe
ostream -- output stream for communication logs
"""
self._port = port
self._ostream = ostream
def __enter__(self):
"""
Open a serial communication to a Wayne probe and automatically start a debugging session
"""
# Open a serial communication to the Wayne probe and wait until it is
# up
self._serial = sr.Serial(
port=self._port,
baudrate=115200,
bytesize=8,
parity=sr.PARITY_NONE,
stopbits=sr.STOPBITS_ONE,
timeout=self.SERIAL_TIMEOUT_MS,
)
wt.wait(
lambda: self._serial.in_waiting > 0,
timeout_seconds=(self.SERIAL_TIMEOUT_MS * 1e-3),
)
self._serial.read(self._serial.in_waiting)
self._ostream.write(self._send(self.CLOCK_DOWN))
self._ostream.write(self._send(self.DWIRE_ON))
self._ostream.write(self._send(self.ENTER_DEBUG_SESSION))
return self
def __exit__(self, exception_type, value, traceback):
"""
Restore debuggee configuration and close the serial communication
"""
self._ostream.write(self._send("EXIT\n"))
self._ostream.write(self._send(self.DWIRE_OFF))
self._ostream.write(self._send(self.CLOCK_DOWN))
self._serial.close()
def next(self):
"""
Reach next instruction
return the Wayne probe response
"""
return self._send("STEP\n")
def _send(self, message: str):
"""
Send a message to the Wayne probe and return the response
"""
self._serial.write(message.encode())
try:
wt.wait(
lambda: self._serial.in_waiting > 0,
timeout_seconds=(self.SERIAL_TIMEOUT_MS * 1e-3),
)
return self._serial.read(self._serial.in_waiting).decode("ascii")
except BaseException:
return None
with WayneProbeSession("/dev/ttyUSB0", sys.stdout) as session:
while True:
input()
print(session.next())