Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

non critical mcus v2 #265

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
exclude = .git,.history,.github,config,docs,lib,scripts,src,test
ignore = E401,E203,W503,C901,E501,E722,E302,E303,E306,E741,F841,W605,E721
ignore = E401,E203,W503,C901,E501,E722,E302,E303,E306,E741,F841,W605,E721,E231

max-complexity=10
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ and [feature configuration reference](docs/Config_Reference_Bleeding_Edge.md):
- input_shaper: smooth input shapers
- input_shaper: new print_ringing_tower utility

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

## Switch to Danger Klipper

> [!NOTE]
Expand Down
3 changes: 3 additions & 0 deletions klippy/clocksync.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def __init__(self, reactor):
self.prediction_variance = 0.0
self.last_prediction_time = 0.0

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
67 changes: 35 additions & 32 deletions klippy/extras/adxl345.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,38 +195,41 @@ def cmd_ACCELEROMETER_MEASURE(self, gcmd):
cmd_ACCELEROMETER_QUERY_help = "Query accelerometer for the current values"

def cmd_ACCELEROMETER_QUERY(self, gcmd):
num_samples = gcmd.get_int("SAMPLES", 1)
samples = []
while num_samples > 0:
aclient = self.chip.start_internal_client()
self.printer.lookup_object("toolhead").dwell(1.0)
aclient.finish_measurements()
values = aclient.get_samples()
if not values:
raise gcmd.error("No accelerometer measurements found")
take = min(len(values), num_samples)
num_samples -= take
samples.extend(values[-take:])

accel_x = sum([x for (_, x, y, z) in samples]) / len(samples)
accel_y = sum([y for (_, x, y, z) in samples]) / len(samples)
accel_z = sum([z for (_, x, y, z) in samples]) / len(samples)

return_type = gcmd.get("RETURN", "vector")
if return_type == "tilt":
tilt_x = math.degrees(math.atan2(accel_x, accel_z))
tilt_y = math.degrees(math.atan2(accel_y, accel_z))
gcmd.respond_info(
"accelerometer plane tilt degrees (x, y): %.6f, %.6f"
% (tilt_x, tilt_y)
)
elif return_type == "vector":
gcmd.respond_info(
"accelerometer values (x, y, z): %.6f, %.6f, %.6f"
% (accel_x, accel_y, accel_z)
)
else:
raise gcmd.error("Unknown 'return' type '%s'" % (return_type,))
try:
num_samples = gcmd.get_int("SAMPLES", 1)
samples = []
while num_samples > 0:
aclient = self.chip.start_internal_client()
self.printer.lookup_object("toolhead").dwell(1.0)
aclient.finish_measurements()
values = aclient.get_samples()
if not values:
raise gcmd.error("No accelerometer measurements found")
take = min(len(values), num_samples)
num_samples -= take
samples.extend(values[-take:])

accel_x = sum([x for (_, x, y, z) in samples]) / len(samples)
accel_y = sum([y for (_, x, y, z) in samples]) / len(samples)
accel_z = sum([z for (_, x, y, z) in samples]) / len(samples)

return_type = gcmd.get("RETURN", "vector")
if return_type == "tilt":
tilt_x = math.degrees(math.atan2(accel_x, accel_z))
tilt_y = math.degrees(math.atan2(accel_y, accel_z))
gcmd.respond_info(
"accelerometer plane tilt degrees (x, y): %.6f, %.6f"
% (tilt_x, tilt_y)
)
elif return_type == "vector":
gcmd.respond_info(
"accelerometer values (x, y, z): %.6f, %.6f, %.6f"
% (accel_x, accel_y, accel_z)
)
else:
raise gcmd.error("Unknown 'return' type '%s'" % (return_type,))
except Exception as e:
raise gcmd.error("Failed to query accelerometer: %s" % (e,))

cmd_ACCELEROMETER_DEBUG_READ_help = "Query register (for debugging)"

Expand Down
8 changes: 8 additions & 0 deletions klippy/extras/display/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"hd44780_spi": hd44780_spi.hd44780_spi,
}


# Storage of [display_template my_template] config sections
class DisplayTemplate:
def __init__(self, config):
Expand Down Expand Up @@ -232,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 @@ -254,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
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
53 changes: 29 additions & 24 deletions klippy/extras/temperature_mcu.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,15 @@ 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

def get_report_time_delta(self):
return REPORT_TIME

def adc_callback(self, read_time, read_value):
temp = self.base_temperature + read_value * self.slope
self.temperature_callback(read_time + SAMPLE_COUNT * SAMPLE_TIME, temp)

def setup_minmax(self, min_temp, max_temp):
self.min_temp = min_temp
self.max_temp = max_temp

def calc_adc(self, temp):
return (temp - self.base_temperature) / self.slope

def calc_base(self, temp, adc):
return temp - adc * self.slope

def _mcu_identify(self):
# Obtain mcu information
_mcu = self.mcu_adc.get_mcu()
self.debug_read_cmd = _mcu.lookup_query_command(
def _build_config(self):
self.debug_read_cmd = self.mcu_adc.get_mcu().lookup_query_command(
"debug_read order=%c addr=%u", "debug_result val=%u"
)

# Obtain mcu information
_mcu = self._mcu = self.mcu_adc.get_mcu()
self.mcu_type = _mcu.get_constants().get("MCU", "")
# Run MCU specific configuration
cfg_funcs = [
Expand Down Expand Up @@ -133,6 +115,29 @@ def _mcu_identify(self):
range_check_count=self._danger_check_count,
)

def setup_callback(self, temperature_callback):
self.temperature_callback = temperature_callback

def get_report_time_delta(self):
return REPORT_TIME

def adc_callback(self, read_time, read_value):
temp = self.base_temperature + read_value * self.slope
self.temperature_callback(read_time + SAMPLE_COUNT * SAMPLE_TIME, temp)

def setup_minmax(self, min_temp, max_temp):
self.min_temp = min_temp
self.max_temp = max_temp

def calc_adc(self, temp):
return (temp - self.base_temperature) / self.slope

def calc_base(self, temp, adc):
return temp - adc * self.slope

def _mcu_identify(self):
self._build_config()

def config_unknown(self):
raise self.printer.config_error(
"MCU temperature not supported on %s" % (self.mcu_type,)
Expand Down
9 changes: 8 additions & 1 deletion klippy/extras/tmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,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 @@ -405,6 +405,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 @@ -234,6 +234,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
3 changes: 3 additions & 0 deletions klippy/extras/tmc_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def activate(self, instance_id):
# TMC uart communication
######################################################################


# Share mutexes so only one active tmc_uart command on a single mcu at
# a time. This helps limit cpu usage on slower micro-controllers.
class PrinterTMCUartMutexes:
Expand All @@ -85,6 +86,7 @@ def lookup_tmc_uart_mutex(mcu):
TMC_BAUD_RATE = 40000
TMC_BAUD_RATE_AVR = 9000


# Code for sending messages on a TMC uart
class MCU_TMC_uart_bitbang:
def __init__(self, rx_pin_params, tx_pin_params, select_pins_desc):
Expand Down Expand Up @@ -270,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
Loading