mcu: Send mcu config and init immediately after building it

Don't issue a get_config command after building the config as the
built config may contain time sensitive initialization commands.
Instead, send the config and init commands immediately after invoking
the build_config() callbacks.  This avoids some rare "timer too close"
errors during configuration.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-07-12 12:54:12 -04:00
parent 211e34e62b
commit 5cdba1fda8
1 changed files with 35 additions and 34 deletions

View File

@ -430,7 +430,6 @@ class MCU:
self._config_objects = [] self._config_objects = []
self._init_cmds = [] self._init_cmds = []
self._config_cmds = [] self._config_cmds = []
self._config_crc = None
self._pin_map = config.get('pin_map', None) self._pin_map = config.get('pin_map', None)
self._custom = config.get('custom', '') self._custom = config.get('custom', '')
self._mcu_freq = 0. self._mcu_freq = 0.
@ -505,14 +504,13 @@ class MCU:
if not line: if not line:
continue continue
self.add_config_cmd(line) self.add_config_cmd(line)
def _build_config(self): def _send_config(self, prev_crc):
# Build config commands # Build config commands
for co in self._config_objects: for co in self._config_objects:
co.build_config() co.build_config()
self._add_custom() self._add_custom()
self._config_cmds.insert(0, "allocate_oids count=%d" % ( self._config_cmds.insert(0, "allocate_oids count=%d" % (
self._oid_count,)) self._oid_count,))
# Resolve pin names # Resolve pin names
mcu_type = self._serial.msgparser.get_constant('MCU') mcu_type = self._serial.msgparser.get_constant('MCU')
pin_resolver = pins.PinResolver(mcu_type) pin_resolver = pins.PinResolver(mcu_type)
@ -522,44 +520,50 @@ class MCU:
self._config_cmds[i] = pin_resolver.update_command(cmd) self._config_cmds[i] = pin_resolver.update_command(cmd)
for i, cmd in enumerate(self._init_cmds): for i, cmd in enumerate(self._init_cmds):
self._init_cmds[i] = pin_resolver.update_command(cmd) self._init_cmds[i] = pin_resolver.update_command(cmd)
# Calculate config CRC # Calculate config CRC
self._config_crc = zlib.crc32('\n'.join(self._config_cmds)) & 0xffffffff config_crc = zlib.crc32('\n'.join(self._config_cmds)) & 0xffffffff
self.add_config_cmd("finalize_config crc=%d" % (self._config_crc,)) self.add_config_cmd("finalize_config crc=%d" % (config_crc,))
def _send_config(self): # Transmit config messages (if needed)
if prev_crc is None:
logging.info("Sending MCU '%s' printer configuration...", self._name)
for c in self._config_cmds:
self._serial.send(c)
elif config_crc != prev_crc:
self._check_restart("CRC mismatch")
raise error("MCU '%s' CRC does not match config" % (self._name,))
# Transmit init messages
for c in self._init_cmds:
self._serial.send(c)
def _send_get_config(self):
get_config_cmd = self.lookup_command("get_config") get_config_cmd = self.lookup_command("get_config")
if self.is_fileoutput(): if self.is_fileoutput():
config_params = { return { 'is_config': 0, 'move_count': 500, 'crc': 0 }
'is_config': 0, 'move_count': 500, 'crc': self._config_crc} config_params = get_config_cmd.send_with_response(response='config')
else: if self._is_shutdown:
config_params = get_config_cmd.send_with_response(response='config') raise error("MCU '%s' error during config: %s" % (
self._name, self._shutdown_msg))
if config_params['is_shutdown']:
raise error("Can not update MCU '%s' config as it is shutdown" % (
self._name,))
return config_params
def _check_config(self):
config_params = self._send_get_config()
if not config_params['is_config']: if not config_params['is_config']:
if self._restart_method == 'rpi_usb': if self._restart_method == 'rpi_usb':
# Only configure mcu after usb power reset # Only configure mcu after usb power reset
self._check_restart("full reset before config") self._check_restart("full reset before config")
# Send config commands # Not configured - send config and issue get_config again
logging.info("Sending MCU '%s' printer configuration...", self._send_config(None)
self._name) config_params = self._send_get_config()
for c in self._config_cmds: if not config_params['is_config'] and not self.is_fileoutput():
self._serial.send(c) raise error("Unable to configure MCU '%s'" % (self._name,))
if not self.is_fileoutput():
config_params = get_config_cmd.send_with_response(
response='config')
if not config_params['is_config']:
if self._is_shutdown:
raise error("MCU '%s' error during config: %s" % (
self._name, self._shutdown_msg))
raise error("Unable to configure MCU '%s'" % (self._name,))
else: else:
start_reason = self._printer.get_start_args().get("start_reason") start_reason = self._printer.get_start_args().get("start_reason")
if start_reason == 'firmware_restart': if start_reason == 'firmware_restart':
raise error("Failed automated reset of MCU '%s'" % (self._name,)) raise error("Failed automated reset of MCU '%s'" % (self._name,))
if self._config_crc != config_params['crc']: # Already configured - send init commands
if config_params['is_shutdown']: self._send_config(config_params['crc'])
raise error("Can not update MCU '%s' config as it is shutdown" # Setup steppersync with the move_count returned by get_config
% (self._name,))
self._check_restart("CRC mismatch")
raise error("MCU '%s' CRC does not match config" % (self._name,))
move_count = config_params['move_count'] move_count = config_params['move_count']
msgparser = self._serial.msgparser msgparser = self._serial.msgparser
info = [ info = [
@ -574,8 +578,6 @@ class MCU:
self._serial.serialqueue, self._stepqueues, len(self._stepqueues), self._serial.serialqueue, self._stepqueues, len(self._stepqueues),
move_count) move_count)
self._ffi_lib.steppersync_set_time(self._steppersync, 0., self._mcu_freq) self._ffi_lib.steppersync_set_time(self._steppersync, 0., self._mcu_freq)
for c in self._init_cmds:
self._serial.send(c)
def _connect(self): def _connect(self):
if self.is_fileoutput(): if self.is_fileoutput():
self._connect_file() self._connect_file()
@ -600,8 +602,7 @@ class MCU:
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')
self._build_config() self._check_config()
self._send_config()
# Config creation helpers # Config creation helpers
def setup_pin(self, pin_params): def setup_pin(self, pin_params):
pcs = {'stepper': MCU_stepper, 'endstop': MCU_endstop, pcs = {'stepper': MCU_stepper, 'endstop': MCU_endstop,