diff --git a/docs/Todo.md b/docs/Todo.md index b0583dbe..1ad01dcc 100644 --- a/docs/Todo.md +++ b/docs/Todo.md @@ -71,8 +71,6 @@ Hardware features * Support for additional kinematics: scara, etc. -* Support shared motor enable GPIO lines. - * Support for bed-level probes. * Possible support for touch panels attached to the micro-controller. diff --git a/klippy/pins.py b/klippy/pins.py index e46bc06b..c612432c 100644 --- a/klippy/pins.py +++ b/klippy/pins.py @@ -1,6 +1,6 @@ # Pin name to pin number definitions # -# Copyright (C) 2016,2017 Kevin O'Connor +# Copyright (C) 2016-2018 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. import re @@ -180,7 +180,8 @@ class PrinterPins: error = error def __init__(self): self.chips = {} - def lookup_pin(self, pin_type, pin_desc): + self.active_pins = {} + def lookup_pin(self, pin_type, pin_desc, share_type=None): can_invert = pin_type in ['stepper', 'endstop', 'digital_out', 'pwm'] can_pullup = pin_type == 'endstop' pullup = invert = 0 @@ -196,8 +197,19 @@ class PrinterPins: chip_name, pin = [s.strip() for s in pin_desc.split(':', 1)] if chip_name not in self.chips: raise error("Unknown pin chip name '%s'" % (chip_name,)) - return {'chip': self.chips[chip_name], 'chip_name': chip_name, - 'type': pin_type, 'pin': pin, 'invert': invert, 'pullup': pullup} + share_name = "%s:%s" % (chip_name, pin) + if share_name in self.active_pins: + pin_params = self.active_pins[share_name] + if share_type is None or share_type != pin_params['share_type']: + raise error("pin %s used multiple times in config" % (pin,)) + if invert != pin_params['invert'] or pullup != pin_params['pullup']: + raise error("Shared pin %s must have same polarity" % (pin,)) + return pin_params + pin_params = {'chip': self.chips[chip_name], 'chip_name': chip_name, + 'type': pin_type, 'share_type': share_type, + 'pin': pin, 'invert': invert, 'pullup': pullup} + self.active_pins[share_name] = pin_params + return pin_params def setup_pin(self, pin_type, pin_desc): pin_params = self.lookup_pin(pin_type, pin_desc) return pin_params['chip'].setup_pin(pin_params) diff --git a/klippy/stepper.py b/klippy/stepper.py index dc1242a1..61d59568 100644 --- a/klippy/stepper.py +++ b/klippy/stepper.py @@ -1,11 +1,38 @@ # Printer stepper support # -# Copyright (C) 2016,2017 Kevin O'Connor +# Copyright (C) 2016-2018 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. import math, logging import homing, pins +# Tracking of shared stepper enable pins +class StepperEnablePin: + def __init__(self, mcu_enable, enable_count=0): + self.mcu_enable = mcu_enable + self.enable_count = enable_count + def set_enable(self, print_time, enable): + if enable: + if not self.enable_count: + self.mcu_enable.set_digital(print_time, 1) + self.enable_count += 1 + else: + self.enable_count -= 1 + if not self.enable_count: + self.mcu_enable.set_digital(print_time, 0) + +def lookup_enable_pin(printer, pin): + if pin is None: + return StepperEnablePin(None, 9999) + pin_params = pins.get_printer_pins(printer).lookup_pin( + 'digital_out', pin, 'stepper_enable') + enable = pin_params.get('class') + if enable is None: + mcu_enable = pin_params['chip'].setup_pin(pin_params) + mcu_enable.setup_max_duration(0.) + pin_params['class'] = enable = StepperEnablePin(mcu_enable) + return enable + # Code storing the definitions for a stepper motor class PrinterStepper: def __init__(self, printer, config): @@ -23,12 +50,7 @@ class PrinterStepper: self.mcu_stepper.setup_step_distance(self.step_dist) self.step_const = self.mcu_stepper.step_const self.step_delta = self.mcu_stepper.step_delta - # Enable pin - enable_pin = config.get('enable_pin', None) - self.mcu_enable = None - if enable_pin is not None: - self.mcu_enable = pins.setup_pin(printer, 'digital_out', enable_pin) - self.mcu_enable.setup_max_duration(0.) + self.enable = lookup_enable_pin(printer, config.get('enable_pin', None)) def _dist_to_time(self, dist, start_velocity, accel): # Calculate the time it takes to travel a distance with constant accel time_offset = start_velocity / accel @@ -44,9 +66,8 @@ class PrinterStepper: def set_position(self, pos): self.mcu_stepper.set_position(pos) def motor_enable(self, print_time, enable=0): - if (self.mcu_enable is not None - and self.need_motor_enable != (not enable)): - self.mcu_enable.set_digital(print_time, enable) + if self.need_motor_enable != (not enable): + self.enable.set_enable(print_time, enable) self.need_motor_enable = not enable # Support for stepper controlled linear axis with an endstop