From 7083a33ecd1cdcf5402cccbdb53f56cdfaf80f5e Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 3 Sep 2017 22:02:48 -0400 Subject: [PATCH] pru: Support config_reset command to manually reset mcu Add support for resetting the MCU via a software only mechanism. This is useful on the PRU. Signed-off-by: Kevin O'Connor --- klippy/mcu.py | 36 ++++++++++++++++++++++++++---------- src/basecmd.c | 19 +++++++++++++++++++ src/basecmd.h | 1 + src/pru/main.c | 3 +++ src/sched.c | 4 ++-- src/sched.h | 1 + 6 files changed, 52 insertions(+), 12 deletions(-) diff --git a/klippy/mcu.py b/klippy/mcu.py index 506a5b5d..c72b4ee8 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -430,14 +430,17 @@ class MCU: self._shutdown_msg = "" self._timeout_timer = printer.reactor.register_timer( self.timeout_handler) - rmethods = {m: m for m in ['arduino', 'command', 'rpi_usb']} - self._restart_method = config.getchoice( - 'restart_method', rmethods, 'arduino') + self._restart_method = 'command' + if baud: + rmethods = {m: m for m in ['arduino', 'command', 'rpi_usb']} + self._restart_method = config.getchoice( + 'restart_method', rmethods, 'arduino') # Config building if printer.bglogger is not None: printer.bglogger.set_rollover_info("mcu", None) pins.get_printer_pins(printer).register_chip("mcu", self) - self._emergency_stop_cmd = self._reset_cmd = None + self._emergency_stop_cmd = None + self._reset_cmd = self._config_reset_cmd = None self._oid_count = 0 self._config_objects = [] self._init_cmds = [] @@ -502,10 +505,8 @@ class MCU: self._stats_sumsq_base = self.serial.msgparser.get_constant_float( 'STATS_SUMSQ_BASE') self._emergency_stop_cmd = self.lookup_command("emergency_stop") - try: - self._reset_cmd = self.lookup_command("reset") - except self.serial.msgparser.error as e: - pass + self._reset_cmd = self.try_lookup_command("reset") + self._config_reset_cmd = self.try_lookup_command("config_reset") self.register_msg(self.handle_shutdown, 'shutdown') self.register_msg(self.handle_shutdown, 'is_shutdown') self.register_msg(self.handle_mcu_stats, 'stats') @@ -560,11 +561,21 @@ class MCU: if self._restart_method == 'command': last_clock, last_clock_time = self.serial.get_last_clock() eventtime = reactor.monotonic() - if (self._reset_cmd is None + if ((self._reset_cmd is None and self._config_reset_cmd is None) or eventtime > last_clock_time + self.COMM_TIMEOUT): logging.info("Unable to issue reset command") return - # Attempt reset via command + if self._reset_cmd is None: + # Attempt reset via config_reset command + logging.info("Attempting a microcontroller config_reset command") + self.is_shutdown = True + self.force_shutdown() + reactor.pause(reactor.monotonic() + 0.015) + self.send(self._config_reset_cmd.encode()) + reactor.pause(reactor.monotonic() + 0.015) + self.disconnect() + return + # Attempt reset via reset command logging.info("Attempting a microcontroller reset command") self.send(self._reset_cmd.encode()) reactor.pause(reactor.monotonic() + 0.015) @@ -682,6 +693,11 @@ class MCU: return self.serial.alloc_command_queue() def lookup_command(self, msgformat): return self.serial.msgparser.lookup_command(msgformat) + def try_lookup_command(self, msgformat): + try: + return self.serial.msgparser.lookup_command(msgformat) + except self.serial.msgparser.error as e: + return None def create_command(self, msg): return self.serial.msgparser.create_command(msg) # Clock syncing diff --git a/src/basecmd.c b/src/basecmd.c index 831b12e4..b5ff738a 100644 --- a/src/basecmd.c +++ b/src/basecmd.c @@ -215,6 +215,25 @@ command_finalize_config(uint32_t *args) } DECL_COMMAND(command_finalize_config, "finalize_config crc=%u"); +// Attempt a full manual reset of the config +void +config_reset(uint32_t *args) +{ + if (! sched_is_shutdown()) + shutdown("config_reset only available when shutdown"); + irq_disable(); + config_crc = 0; + oid_count = 0; + oids = NULL; + move_free_list = NULL; + move_list = NULL; + move_count = move_item_size = 0; + alloc_init(); + sched_timer_reset(); + sched_clear_shutdown(); + irq_enable(); +} + /**************************************************************** * Timing and load stats diff --git a/src/basecmd.h b/src/basecmd.h index 8787b194..21555bf8 100644 --- a/src/basecmd.h +++ b/src/basecmd.h @@ -10,6 +10,7 @@ void *oid_lookup(uint8_t oid, void *type); void *oid_alloc(uint8_t oid, void *type, uint16_t size); void *oid_next(uint8_t *i, void *type); void stats_update(uint32_t start, uint32_t cur); +void config_reset(uint32_t *args); #define foreach_oid(pos,data,oidtype) \ for (pos=-1; (data=oid_next(&pos, oidtype)); ) diff --git a/src/pru/main.c b/src/pru/main.c index 49d929ef..3edce8ec 100644 --- a/src/pru/main.c +++ b/src/pru/main.c @@ -212,6 +212,9 @@ struct my_resource_table { * Startup ****************************************************************/ +// Support config_reset +DECL_COMMAND_FLAGS(config_reset, HF_IN_SHUTDOWN, "config_reset"); + // Main entry point int main(void) diff --git a/src/sched.c b/src/sched.c index 9aa5aea2..e4b51355 100644 --- a/src/sched.c +++ b/src/sched.c @@ -165,8 +165,8 @@ sched_timer_dispatch(void) return next_waketime; } -// Remove all user timers on a shutdown -static void +// Remove all user timers +void sched_timer_reset(void) { timer_list = &deleted_timer; diff --git a/src/sched.h b/src/sched.h index ffe17e03..0a2bdc4b 100644 --- a/src/sched.h +++ b/src/sched.h @@ -29,6 +29,7 @@ struct task_wake { void sched_add_timer(struct timer*); void sched_del_timer(struct timer *del); unsigned int sched_timer_dispatch(void); +void sched_timer_reset(void); void sched_wake_tasks(void); uint8_t sched_tasks_busy(void); void sched_wake_task(struct task_wake *w);