mcu: Support reset command
Extend the FIRMWARE_RESTART command so that it can use the firmware "reset" command instead of the "arduino" mechanism. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
1592395036
commit
9f9e3e61d6
|
@ -243,6 +243,13 @@ serial: /dev/ttyACM0
|
||||||
pin_map: arduino
|
pin_map: arduino
|
||||||
# This option may be used to enable Arduino pin name aliases. The
|
# This option may be used to enable Arduino pin name aliases. The
|
||||||
# default is to not enable the aliases.
|
# default is to not enable the aliases.
|
||||||
|
#restart_method: arduino
|
||||||
|
# This controls the mechanism the host will use to reset the
|
||||||
|
# micro-controller. The choices are 'arduino' and 'command'. The
|
||||||
|
# 'arduino' method (toggle DTR; set baud to 1200) is common on
|
||||||
|
# Arduino boards and clones. The 'command' method involves sending a
|
||||||
|
# Klipper command to the micro-controller so that it can reset
|
||||||
|
# itself. The default is 'arduino'.
|
||||||
custom:
|
custom:
|
||||||
# This option may be used to specify a set of custom
|
# This option may be used to specify a set of custom
|
||||||
# micro-controller commands to be sent at the start of the
|
# micro-controller commands to be sent at the start of the
|
||||||
|
|
|
@ -246,8 +246,8 @@ class Printer:
|
||||||
try:
|
try:
|
||||||
if self.mcu is not None:
|
if self.mcu is not None:
|
||||||
self.stats(self.reactor.monotonic())
|
self.stats(self.reactor.monotonic())
|
||||||
self.mcu.disconnect()
|
|
||||||
self.mcu.microcontroller_restart()
|
self.mcu.microcontroller_restart()
|
||||||
|
self.mcu.disconnect()
|
||||||
except:
|
except:
|
||||||
logging.exception("Unhandled exception during firmware_restart")
|
logging.exception("Unhandled exception during firmware_restart")
|
||||||
def get_startup_state(self):
|
def get_startup_state(self):
|
||||||
|
|
|
@ -371,7 +371,6 @@ class MCU:
|
||||||
COMM_TIMEOUT = 3.5
|
COMM_TIMEOUT = 3.5
|
||||||
def __init__(self, printer, config):
|
def __init__(self, printer, config):
|
||||||
self._printer = printer
|
self._printer = printer
|
||||||
self._config = config
|
|
||||||
# Serial port
|
# Serial port
|
||||||
baud = config.getint('baud', 250000)
|
baud = config.getint('baud', 250000)
|
||||||
self._serialport = config.get('serial', '/dev/ttyS0')
|
self._serialport = config.get('serial', '/dev/ttyS0')
|
||||||
|
@ -382,8 +381,13 @@ class MCU:
|
||||||
self._is_fileoutput = False
|
self._is_fileoutput = False
|
||||||
self._timeout_timer = printer.reactor.register_timer(
|
self._timeout_timer = printer.reactor.register_timer(
|
||||||
self.timeout_handler)
|
self.timeout_handler)
|
||||||
|
rmethods = {m: m for m in ['arduino', 'command']}
|
||||||
|
self._restart_method = config.getchoice(
|
||||||
|
'restart_method', rmethods, 'arduino')
|
||||||
# Config building
|
# Config building
|
||||||
self._emergency_stop_cmd = self._clear_shutdown_cmd = None
|
self._config_error = config.error
|
||||||
|
self._emergency_stop_cmd = self._reset_cmd = None
|
||||||
|
self._clear_shutdown_cmd = None
|
||||||
self._oids = []
|
self._oids = []
|
||||||
self._config_cmds = []
|
self._config_cmds = []
|
||||||
self._config_crc = None
|
self._config_crc = None
|
||||||
|
@ -430,6 +434,10 @@ class MCU:
|
||||||
'STATS_SUMSQ_BASE')
|
'STATS_SUMSQ_BASE')
|
||||||
self._emergency_stop_cmd = self.lookup_command("emergency_stop")
|
self._emergency_stop_cmd = self.lookup_command("emergency_stop")
|
||||||
self._clear_shutdown_cmd = self.lookup_command("clear_shutdown")
|
self._clear_shutdown_cmd = self.lookup_command("clear_shutdown")
|
||||||
|
try:
|
||||||
|
self._reset_cmd = self.lookup_command("reset")
|
||||||
|
except self.serial.msgparser.error, e:
|
||||||
|
pass
|
||||||
self.register_msg(self.handle_shutdown, 'shutdown')
|
self.register_msg(self.handle_shutdown, 'shutdown')
|
||||||
self.register_msg(self.handle_shutdown, 'is_shutdown')
|
self.register_msg(self.handle_shutdown, 'is_shutdown')
|
||||||
self.register_msg(self.handle_mcu_stats, 'stats')
|
self.register_msg(self.handle_mcu_stats, 'stats')
|
||||||
|
@ -469,9 +477,24 @@ class MCU:
|
||||||
logging.info("Sending clear_shutdown command")
|
logging.info("Sending clear_shutdown command")
|
||||||
self.send(self._clear_shutdown_cmd.encode())
|
self.send(self._clear_shutdown_cmd.encode())
|
||||||
def microcontroller_restart(self):
|
def microcontroller_restart(self):
|
||||||
|
reactor = self._printer.reactor
|
||||||
|
if self._restart_method == 'command':
|
||||||
|
last_clock, last_clock_time = self.serial.get_last_clock()
|
||||||
|
eventtime = reactor.monotonic()
|
||||||
|
if (self._reset_cmd is None
|
||||||
|
or eventtime > last_clock_time + self.COMM_TIMEOUT):
|
||||||
|
logging.info("Unable to issue reset command")
|
||||||
|
return
|
||||||
|
# Attempt reset via command
|
||||||
|
logging.info("Attempting a microcontroller reset command")
|
||||||
|
self.send(self._reset_cmd.encode())
|
||||||
|
reactor.pause(reactor.monotonic() + 0.015)
|
||||||
|
self.disconnect()
|
||||||
|
return
|
||||||
|
# Attempt reset via arduino mechanism
|
||||||
logging.info("Attempting a microcontroller reset")
|
logging.info("Attempting a microcontroller reset")
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
serialhdl.arduino_reset(self._serialport, self._printer.reactor)
|
serialhdl.arduino_reset(self._serialport, reactor)
|
||||||
def is_fileoutput(self):
|
def is_fileoutput(self):
|
||||||
return self._is_fileoutput
|
return self._is_fileoutput
|
||||||
# Configuration phase
|
# Configuration phase
|
||||||
|
@ -501,7 +524,7 @@ class MCU:
|
||||||
updated_cmds.append(pins.update_command(
|
updated_cmds.append(pins.update_command(
|
||||||
cmd, self._mcu_freq, pnames))
|
cmd, self._mcu_freq, pnames))
|
||||||
except:
|
except:
|
||||||
raise self._config.error("Unable to translate pin name: %s" % (
|
raise self._config_error("Unable to translate pin name: %s" % (
|
||||||
cmd,))
|
cmd,))
|
||||||
self._config_cmds = updated_cmds
|
self._config_cmds = updated_cmds
|
||||||
|
|
||||||
|
|
|
@ -184,6 +184,7 @@ class UnknownFormat:
|
||||||
return {'#msgid': msgid, '#msg': msg}, len(s)-MESSAGE_TRAILER_SIZE
|
return {'#msgid': msgid, '#msg': msg}, len(s)-MESSAGE_TRAILER_SIZE
|
||||||
|
|
||||||
class MessageParser:
|
class MessageParser:
|
||||||
|
error = error
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.unknown = UnknownFormat()
|
self.unknown = UnknownFormat()
|
||||||
self.messages_by_id = {}
|
self.messages_by_id = {}
|
||||||
|
|
Loading…
Reference in New Issue