Skip to content

Commit

Permalink
noncritical mcus
Browse files Browse the repository at this point in the history
  • Loading branch information
bwnance authored and rogerlz committed Aug 31, 2024
1 parent 89935d2 commit 542c30e
Show file tree
Hide file tree
Showing 19 changed files with 292 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ See the [Danger Features document](https://dangerklipper.io/Danger_Features.html

- [z_tilt, quad_gantry_level: adaptive horizontal move z](https://github.com/DangerKlippers/danger-klipper/pull/336)

- [core: non-critical-mcus](https://github.com/DangerKlippers/danger-klipper/pull/339)

If you're feeling adventurous, take a peek at the extra features in the bleeding-edge-v2 branch [feature documentation](docs/Bleeding_Edge.md)
and [feature configuration reference](docs/Config_Reference_Bleeding_Edge.md):

- [extruder/pa: do not smooth base extruder position, only advance](https://github.com/DangerKlippers/danger-klipper/pull/266)

- [core: non-critical-mcus](https://github.com/DangerKlippers/danger-klipper/pull/265)

- [dmbutyugin's advanced-features branch - Pull Request #262](https://github.com/DangerKlippers/danger-klipper/pull/262)
- stepper: high precision stepping protocol
- extruder: sync extruder motion with input shaper
Expand Down
4 changes: 4 additions & 0 deletions docs/Config_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ serial:
# sending a Klipper command to the micro-controller so that it can
# reset itself. The default is 'arduino' if the micro-controller
# communicates over a serial port, 'command' otherwise.
#is_non_critical: False
# Setting this to True will allow the mcu to be disconnected and
# reconnected at will without errors. Helpful for USB-accelerometer boards
# and USB-probes
```

### [mcu my_extra_mcu]
Expand Down
2 changes: 1 addition & 1 deletion docs/Danger_Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- [`[exclude_object]`](./Config_Reference.md#exclude_object) is enabled by default. Use `[exclude_object] enable_exclude_object: False` to disable it

## Additional configuration options

- [`[mcu] is_non_critical`](./Config_Reference.md#mcu) enables marking of an mcu as optional - it can be freely disconnected and connected at will. (useful for MCU-based accelerometer boards, mcu-based probes that shut down in hot chambers, etc...)
- [`[danger_options]`](./Config_Reference.md#danger-options) - New configuration options to adjust klipper values that were previously hidden
- Additional kinematics versions enabled per-axis acceleration, see [limited_cartesian](./Config_Reference.md#⚠️-cartesian-kinematics-with-limits-for-x-and-y-axes) and [limited_corexy](./Config_Reference.md#⚠️-corexy-kinematics-with-limits-for-x-and-y-axes)
- `--rotate-log-at-restart` can be added to your klipper start script or service to force log rotation every restart.
Expand Down
4 changes: 4 additions & 0 deletions klippy/clocksync.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def __init__(self, reactor):
self.prediction_variance = 0.0
self.last_prediction_time = 0.0

# XXX - test to see if this is used or not
# def disconnect(self):
# self.reactor.update_timer(self.get_clock_timer, self.reactor.NEVER)

def connect(self, serial):
self.serial = serial
self.mcu_freq = serial.msgparser.get_constant_float("CLOCK_FREQ")
Expand Down
7 changes: 7 additions & 0 deletions klippy/extras/adxl345.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ def _build_config(self):
"query_adxl345_status oid=%c", oid=self.oid, cq=cmdqueue
)

def check_connected(self):
if self.mcu.non_critical_disconnected:
raise self.printer.command_error(
f"ADXL: {self.name} could not connect because mcu: {self.mcu.get_name()} is non_critical_disconnected!"
)

def read_reg(self, reg):
params = self.spi.spi_transfer([reg | REG_MOD_READ, 0x00])
response = bytearray(params["response"])
Expand All @@ -327,6 +333,7 @@ def set_reg(self, reg, val, minclock=0):
)

def start_internal_client(self):
self.check_connected()
aqh = AccelQueryHelper(self.printer)
self.batch_bulk.add_client(aqh.handle_batch)
return aqh
Expand Down
7 changes: 7 additions & 0 deletions klippy/extras/display/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ def __init__(self, config):
raise config.error("Unknown display_data group '%s'" % (dgroup,))
# Screen updating
self.printer.register_event_handler("klippy:ready", self.handle_ready)
self.printer.register_event_handler(
self.lcd_chip.mcu.get_non_critical_reconnect_event_name(),
self.handle_reconnect,
)
self.screen_update_timer = self.reactor.register_timer(
self.screen_update_event
)
Expand All @@ -255,6 +259,9 @@ def __init__(self, config):
def get_dimensions(self):
return self.lcd_chip.get_dimensions()

def handle_reconnect(self):
self.lcd_chip.init()

def handle_ready(self):
self.lcd_chip.init()
# Start screen update timer
Expand Down
1 change: 1 addition & 0 deletions klippy/extras/display/st7920.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def __init__(self, config):
sw_pins = tuple([pin_params["pin"] for pin_params in sw_pin_params])
speed = config.getint("spi_speed", 1000000, minval=100000)
self.spi = bus.MCU_SPI(mcu, None, None, 0, speed, sw_pins)
self.mcu = mcu
# create enable helper
self.en_helper = EnableHelper(config.get("en_pin"), self.spi)
self.en_set = False
Expand Down
2 changes: 2 additions & 0 deletions klippy/extras/display/uc1701.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def init(self):
class UC1701(DisplayBase):
def __init__(self, config):
io = SPI4wire(config, "a0_pin")
self.mcu = io.spi.get_mcu()
DisplayBase.__init__(self, io)
self.contrast = config.getint("contrast", 40, minval=0, maxval=63)
self.reset = ResetHelper(config.get("rst_pin", None), io.spi)
Expand Down Expand Up @@ -232,6 +233,7 @@ def __init__(self, config, columns=128, x_offset=0):
else:
io = SPI4wire(config, "dc_pin")
io_bus = io.spi
self.mcu = io_bus.get_mcu()
self.reset = ResetHelper(config.get("reset_pin", None), io_bus)
DisplayBase.__init__(self, io, columns, x_offset)
self.contrast = config.getint("contrast", 239, minval=0, maxval=255)
Expand Down
7 changes: 7 additions & 0 deletions klippy/extras/lis2dw.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def _build_config(self):
"query_lis2dw_status oid=%c", oid=self.oid, cq=cmdqueue
)

def check_connected(self):
if self.mcu.non_critical_disconnected:
raise self.printer.command_error(
f"LIS2DW: {self.name} could not connect because mcu: {self.mcu.get_name()} is non_critical_disconnected!"
)

def read_reg(self, reg):
params = self.spi.spi_transfer([reg | REG_MOD_READ, 0x00])
response = bytearray(params["response"])
Expand All @@ -96,6 +102,7 @@ def set_reg(self, reg, val, minclock=0):
)

def start_internal_client(self):
self.check_connected()
aqh = adxl345.AccelQueryHelper(self.printer)
self.batch_bulk.add_client(aqh.handle_batch)
return aqh
Expand Down
7 changes: 7 additions & 0 deletions klippy/extras/mpu9250.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def _build_config(self):
"query_mpu9250_status oid=%c", oid=self.oid, cq=cmdqueue
)

def check_connected(self):
if self.mcu.non_critical_disconnected:
raise self.printer.command_error(
f"MPU: {self.name} could not connect because mcu: {self.mcu.get_name()} is non_critical_disconnected!"
)

def read_reg(self, reg):
params = self.i2c.i2c_read([reg], 1)
return bytearray(params["response"])[0]
Expand All @@ -113,6 +119,7 @@ def set_reg(self, reg, val, minclock=0):
self.i2c.i2c_write([reg, val & 0xFF], minclock=minclock)

def start_internal_client(self):
self.check_connected()
aqh = adxl345.AccelQueryHelper(self.printer)
self.batch_bulk.add_client(aqh.handle_batch)
return aqh
Expand Down
3 changes: 3 additions & 0 deletions klippy/extras/neopixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def __init__(self, config):
self.old_color_data = bytearray([d ^ 1 for d in self.color_data])
# Register callbacks
printer.register_event_handler("klippy:connect", self.send_data)
printer.register_event_handler(
self.mcu.get_non_critical_reconnect_event_name(), self.send_data
)

def build_config(self):
bmt = self.mcu.seconds_to_clock(BIT_MAX_TIME)
Expand Down
4 changes: 4 additions & 0 deletions klippy/extras/temperature_mcu.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, config):
self.printer.register_event_handler(
"klippy:mcu_identify", self._mcu_identify
)
self.mcu_adc.get_mcu().register_config_callback(self._build_config)

def setup_callback(self, temperature_callback):
self.temperature_callback = temperature_callback
Expand All @@ -80,6 +81,9 @@ def calc_base(self, temp, adc):
return temp - adc * self.slope

def _mcu_identify(self):
self._build_config()

def _build_config(self):
# Obtain mcu information
_mcu = self.mcu_adc.get_mcu()
self.debug_read_cmd = _mcu.lookup_query_command(
Expand Down
9 changes: 8 additions & 1 deletion klippy/extras/tmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,14 @@ def _handle_connect(self):
)
# Send init
try:
self._init_registers()
if self.mcu_tmc.mcu.non_critical_disconnected:
logging.info(
"TMC %s failed to init - non_critical_mcu: %s is disconnected!",
self.name,
self.mcu_tmc.mcu.get_name(),
)
else:
self._init_registers()
except self.printer.command_error as e:
logging.info("TMC %s failed to init: %s", self.name, str(e))

Expand Down
1 change: 1 addition & 0 deletions klippy/extras/tmc2130.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ def __init__(self, config, name_to_reg, fields, tmc_frequency):
self.name_to_reg = name_to_reg
self.fields = fields
self.tmc_frequency = tmc_frequency
self.mcu = self.tmc_spi.spi.get_mcu()

def get_fields(self):
return self.fields
Expand Down
1 change: 1 addition & 0 deletions klippy/extras/tmc2660.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def __init__(self, config, name_to_reg, fields):
self.spi = bus.MCU_SPI_from_config(config, 0, default_speed=4000000)
self.name_to_reg = name_to_reg
self.fields = fields
self.mcu = self.spi.get_mcu()

def get_fields(self):
return self.fields
Expand Down
1 change: 1 addition & 0 deletions klippy/extras/tmc_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def __init__(self, config, name_to_reg, fields, max_addr, tmc_frequency):
)
self.mutex = self.mcu_uart.mutex
self.tmc_frequency = tmc_frequency
self.mcu = self.mcu_uart.mcu

def get_fields(self):
return self.fields
Expand Down
Loading

0 comments on commit 542c30e

Please sign in to comment.