-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicrotvm_transport.py
49 lines (40 loc) · 1.44 KB
/
microtvm_transport.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
from tvm.micro.transport import Transport, TransportTimeouts
from riotctrl.ctrl import RIOTCtrl
import serial
class UTOETransport(Transport):
def __init__(self, riot_ctrl, timeouts=None):
self._riot_ctrl : RIOTCtrl = riot_ctrl
self._timeouts = timeouts
self._serial = None
def timeouts(self):
assert self._timeouts is not None, "Transport not yet opened"
return self._timeouts
def open(self):
port = get_local_serial_port()
self._serial = serial.Serial(port, baudrate=115200, timeout=10)
self._timeouts = TransportTimeouts(
session_start_retry_timeout_sec=2.0,
session_start_timeout_sec=15.0,
session_established_timeout_sec=15.0,
)
def close(self):
if self._serial is None:
return
self._serial.close()
self._serial = None
self._riot_ctrl.stop_exp()
def write(self, data, timeout_sec):
if self._serial is None:
return
self._serial.write_timeout = timeout_sec
return self._serial.write(data)
def read(self, n, timeout_sec):
if self._serial is None:
return
self._serial.timeout = timeout_sec
return self._serial.read(n)
def get_local_serial_port():
import subprocess, json
output = subprocess.check_output(f"make list-ttys-json", shell=True)
tty_info = json.loads(output)
return tty_info[0]['path']