proxy.py: Move M1N1DEVICE/UART port open logic into UartInterface

Signed-off-by: Hector Martin <marcan@marcan.st>
This commit is contained in:
Hector Martin 2021-04-27 19:48:35 +09:00
parent 82978081f8
commit 80f73926e8
3 changed files with 20 additions and 13 deletions

View file

@ -3,10 +3,7 @@
import serial, os
from proxy import *
uartdev = os.environ.get("M1N1DEVICE", "/dev/ttyUSB0")
usbuart = serial.Serial(uartdev, 115200)
iface = UartInterface(usbuart, debug=False)
iface = UartInterface()
proxy = M1N1Proxy(iface, debug=False)
SCRATCH = 0x24F00000

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
import os, sys, struct
import os, sys, struct, serial
from serial.tools.miniterm import Miniterm
def hexdump(s, sep=" "):
@ -80,8 +80,21 @@ class UartInterface:
CMD_LEN = 56
REPLY_LEN = 36
def __init__(self, device, debug=False):
def __init__(self, device=None, debug=False):
self.debug = debug
self.devpath = None
if device is None:
device = os.environ.get("M1N1DEVICE", "/dev/ttyUSB0:115200")
if isinstance(device, str):
baud = 115200
if ":" in device:
device, baud = device.rsplit(":", 1)
baud = int(baud)
self.devpath = device
self.baudrate = baud
device = serial.Serial(self.devpath, baud)
self.dev = device
self.dev.timeout = 0
self.dev.flushOutput()

View file

@ -3,21 +3,18 @@ from proxy import *
from tgtypes import *
from utils import *
uartdev = os.environ.get("M1N1DEVICE", "/dev/ttyUSB0")
uart = serial.Serial(uartdev, 115200)
iface = UartInterface(uart, debug=False)
iface = UartInterface()
p = M1N1Proxy(iface, debug=False)
try:
uart.timeout = 0.15
iface.dev.timeout = 0.15
iface.nop()
p.set_baud(1500000)
except UartTimeout:
uart.baudrate = 1500000
iface.dev.baudrate = 1500000
iface.nop()
uart.timeout = 3
iface.dev.timeout = 3
u = ProxyUtils(p)
mon = RegMonitor(u)