mirror of https://github.com/Desuuuu/klipper.git
flash_can: add usb support
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
39535b15ba
commit
b829a89069
|
@ -496,9 +496,70 @@ class CanSocket:
|
||||||
self._loop.remove_reader(self.cansock.fileno())
|
self._loop.remove_reader(self.cansock.fileno())
|
||||||
self.cansock.close()
|
self.cansock.close()
|
||||||
|
|
||||||
|
class SerialSocket:
|
||||||
|
def __init__(self, loop: asyncio.AbstractEventLoop):
|
||||||
|
self._loop = loop
|
||||||
|
self.serial = self.serial_error = None
|
||||||
|
self.node = CanNode(0, self)
|
||||||
|
|
||||||
|
def _handle_response(self) -> None:
|
||||||
|
try:
|
||||||
|
data = self.serial.read(4096)
|
||||||
|
except self.serial_error as e:
|
||||||
|
logging.exception("Error on serial read")
|
||||||
|
self.close()
|
||||||
|
self.node.feed_data(data)
|
||||||
|
|
||||||
|
def send(self, can_id: int, payload: bytes = b"") -> None:
|
||||||
|
try:
|
||||||
|
self.serial.write(payload)
|
||||||
|
except self.serial_error as e:
|
||||||
|
logging.exception("Error on serial write")
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
async def run(self, intf: str, baud: int, fw_path: pathlib.Path) -> None:
|
||||||
|
if not fw_path.is_file():
|
||||||
|
raise FlashCanError("Invalid firmware path '%s'" % (fw_path))
|
||||||
|
import serial
|
||||||
|
self.serial_error = serial.SerialException
|
||||||
|
try:
|
||||||
|
serial_dev = serial.Serial(baudrate=baud, timeout=0,
|
||||||
|
exclusive=True)
|
||||||
|
serial_dev.port = intf
|
||||||
|
serial_dev.open()
|
||||||
|
except (OSError, IOError, self.serial_error) as e:
|
||||||
|
raise FlashCanError("Unable to open serial port: %s" % (e,))
|
||||||
|
self.serial = serial_dev
|
||||||
|
self._loop.add_reader(self.serial.fileno(), self._handle_response)
|
||||||
|
flasher = CanFlasher(self.node, fw_path)
|
||||||
|
try:
|
||||||
|
await flasher.connect_btl()
|
||||||
|
await flasher.send_file()
|
||||||
|
await flasher.verify_file()
|
||||||
|
finally:
|
||||||
|
# always attempt to send the complete command. If
|
||||||
|
# there is an error it will exit the bootloader
|
||||||
|
# unless comms were broken
|
||||||
|
await flasher.finish()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.serial is None:
|
||||||
|
return
|
||||||
|
self._loop.remove_reader(self.serial.fileno())
|
||||||
|
self.serial.close()
|
||||||
|
self.serial = None
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Can Bootloader Flash Utility")
|
description="Can Bootloader Flash Utility")
|
||||||
|
parser.add_argument(
|
||||||
|
"-d", "--device", metavar='<serial device>',
|
||||||
|
help="Serial Device"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-b", "--baud", default=250000, metavar='<baud rate>',
|
||||||
|
help="Serial baud rate"
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-i", "--interface", default="can0", metavar='<can interface>',
|
"-i", "--interface", default="can0", metavar='<can interface>',
|
||||||
help="Can Interface"
|
help="Can Interface"
|
||||||
|
@ -522,27 +583,37 @@ def main():
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if not args.verbose:
|
if not args.verbose:
|
||||||
logging.getLogger().setLevel(logging.CRITICAL)
|
logging.getLogger().setLevel(logging.ERROR)
|
||||||
intf = args.interface
|
intf = args.interface
|
||||||
fpath = pathlib.Path(args.firmware).expanduser().resolve()
|
fpath = pathlib.Path(args.firmware).expanduser().resolve()
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
iscan = args.device is None
|
||||||
|
sock = None
|
||||||
try:
|
try:
|
||||||
cansock = CanSocket(loop)
|
if iscan:
|
||||||
if args.query:
|
sock = CanSocket(loop)
|
||||||
loop.run_until_complete(cansock.run_query(intf))
|
if args.query:
|
||||||
|
loop.run_until_complete(sock.run_query(intf))
|
||||||
|
else:
|
||||||
|
if args.uuid is None:
|
||||||
|
raise FlashCanError(
|
||||||
|
"The 'uuid' option must be specified to flash a device"
|
||||||
|
)
|
||||||
|
uuid = int(args.uuid, 16)
|
||||||
|
loop.run_until_complete(sock.run(intf, uuid, fpath))
|
||||||
else:
|
else:
|
||||||
if args.uuid is None:
|
if args.device is None:
|
||||||
raise FlashCanError(
|
raise FlashCanError(
|
||||||
"The 'uuid' option must be specified to flash a device"
|
"The 'device' option must be specified to flash a device"
|
||||||
)
|
)
|
||||||
uuid = int(args.uuid, 16)
|
sock = SerialSocket(loop)
|
||||||
loop.run_until_complete(cansock.run(intf, uuid, fpath))
|
loop.run_until_complete(sock.run(args.device, args.baud, fpath))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception("Can Flash Error")
|
logging.exception("Can Flash Error")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
finally:
|
finally:
|
||||||
if cansock is not None:
|
if sock is not None:
|
||||||
cansock.close()
|
sock.close()
|
||||||
if args.query:
|
if args.query:
|
||||||
output_line("Query Complete")
|
output_line("Query Complete")
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue