From c160f9ef5eb1e60d122bb2636ea346a12f4c78c7 Mon Sep 17 00:00:00 2001 From: bigtreetech Date: Wed, 5 Jul 2023 20:23:40 +0800 Subject: [PATCH 1/3] lis2dw12: Add support for lis2dw12 accelerometer lis2dw12 is an accelerometer from STMicroelectronics(https://www.st.com/resource/en/datasheet/lis2dw12.pdf) With better performance than the ADXL345 according to the datasheet. Signed-off-by: XM.Zhou from BigTreeTech zhouxm@biqu3d.com Signed-off-by: Alan.Ma from BigTreeTech tech@biqu3d.com --- klippy/extras/lis2dw.py | 265 ++++++++++++++++++++++++++++++++++++++++ src/Makefile | 2 +- src/sensor_lis2dw.c | 221 +++++++++++++++++++++++++++++++++ 3 files changed, 487 insertions(+), 1 deletion(-) create mode 100644 klippy/extras/lis2dw.py create mode 100644 src/sensor_lis2dw.c diff --git a/klippy/extras/lis2dw.py b/klippy/extras/lis2dw.py new file mode 100644 index 000000000000..5cf6763939c5 --- /dev/null +++ b/klippy/extras/lis2dw.py @@ -0,0 +1,265 @@ +# Support for reading acceleration data from an LIS2DW chip +# +# Copyright (C) 2023 Zhou.XianMing +# Copyright (C) 2020-2021 Kevin O'Connor +# +# This file may be distributed under the terms of the GNU GPLv3 license. +import logging, time, collections, threading, multiprocessing, os +from . import bus, motion_report, adxl345 + +# LIS2DW registers +REG_LIS2DW_WHO_AM_I_ADDR = 0x0F +REG_LIS2DW_CTRL_REG1_ADDR = 0x20 +REG_LIS2DW_CTRL_REG2_ADDR = 0x21 +REG_LIS2DW_CTRL_REG3_ADDR = 0x22 +REG_LIS2DW_CTRL_REG6_ADDR = 0x25 +REG_LIS2DW_STATUS_REG_ADDR = 0x27 +REG_LIS2DW_OUT_XL_ADDR = 0x28 +REG_LIS2DW_OUT_XH_ADDR = 0x29 +REG_LIS2DW_OUT_YL_ADDR = 0x2A +REG_LIS2DW_OUT_YH_ADDR = 0x2B +REG_LIS2DW_OUT_ZL_ADDR = 0x2C +REG_LIS2DW_OUT_ZH_ADDR = 0x2D +REG_LIS2DW_FIFO_CTRL = 0x2E +REG_LIS2DW_FIFO_SAMPLES = 0x2F +REG_MOD_READ = 0x80 +# REG_MOD_MULTI = 0x40 + +LIS2DW_DEV_ID = 0x44 + +FREEFALL_ACCEL = 9.80665 +SCALE = FREEFALL_ACCEL * 1.952 / 4 + +Accel_Measurement = collections.namedtuple( + 'Accel_Measurement', ('time', 'accel_x', 'accel_y', 'accel_z')) + +MIN_MSG_TIME = 0.100 + +BYTES_PER_SAMPLE = 6 +SAMPLES_PER_BLOCK = 8 + +# Printer class that controls LIS2DW chip +class LIS2DW: + def __init__(self, config): + self.printer = config.get_printer() + adxl345.AccelCommandHelper(config, self) + self.query_rate = 0 + am = {'x': (0, SCALE), 'y': (1, SCALE), 'z': (2, SCALE), + '-x': (0, -SCALE), '-y': (1, -SCALE), '-z': (2, -SCALE)} + axes_map = config.getlist('axes_map', ('x','y','z'), count=3) + if any([a not in am for a in axes_map]): + raise config.error("Invalid lis2dw axes_map parameter") + self.axes_map = [am[a.strip()] for a in axes_map] + self.data_rate = 1600 + # Measurement storage (accessed from background thread) + self.lock = threading.Lock() + self.raw_samples = [] + # Setup mcu sensor_lis2dw bulk query code + self.spi = bus.MCU_SPI_from_config(config, 3, default_speed=5000000) + self.mcu = mcu = self.spi.get_mcu() + self.oid = oid = mcu.create_oid() + self.query_lis2dw_cmd = self.query_lis2dw_end_cmd = None + self.query_lis2dw_status_cmd = None + mcu.add_config_cmd("config_lis2dw oid=%d spi_oid=%d" + % (oid, self.spi.get_oid())) + mcu.add_config_cmd("query_lis2dw oid=%d clock=0 rest_ticks=0" + % (oid,), on_restart=True) + mcu.register_config_callback(self._build_config) + mcu.register_response(self._handle_lis2dw_data, "lis2dw_data", oid) + # Clock tracking + self.last_sequence = self.max_query_duration = 0 + self.last_limit_count = self.last_error_count = 0 + self.clock_sync = adxl345.ClockSyncRegression(self.mcu, 640) + # API server endpoints + self.api_dump = motion_report.APIDumpHelper( + self.printer, self._api_update, self._api_startstop, 0.100) + self.name = config.get_name().split()[-1] + wh = self.printer.lookup_object('webhooks') + wh.register_mux_endpoint("lis2dw/dump_lis2dw", "sensor", self.name, + self._handle_dump_lis2dw) + + def _build_config(self): + cmdqueue = self.spi.get_command_queue() + self.query_lis2dw_cmd = self.mcu.lookup_command( + "query_lis2dw oid=%c clock=%u rest_ticks=%u", cq=cmdqueue) + self.query_lis2dw_end_cmd = self.mcu.lookup_query_command( + "query_lis2dw oid=%c clock=%u rest_ticks=%u", + "lis2dw_status oid=%c clock=%u query_ticks=%u next_sequence=%hu" + " buffered=%c fifo=%c limit_count=%hu", oid=self.oid, cq=cmdqueue) + self.query_lis2dw_status_cmd = self.mcu.lookup_query_command( + "query_lis2dw_status oid=%c", + "lis2dw_status oid=%c clock=%u query_ticks=%u next_sequence=%hu" + " buffered=%c fifo=%c limit_count=%hu", oid=self.oid, cq=cmdqueue) + def read_reg(self, reg): + params = self.spi.spi_transfer([reg | REG_MOD_READ, 0x00]) + response = bytearray(params['response']) + return response[1] + def set_reg(self, reg, val, minclock=0): + self.spi.spi_send([reg, val & 0xFF], minclock=minclock) + stored_val = self.read_reg(reg) + if stored_val != val: + raise self.printer.command_error( + "Failed to set LIS2DW register [0x%x] to 0x%x: got 0x%x. " + "This is generally indicative of connection problems " + "(e.g. faulty wiring) or a faulty lis2dw chip." % ( + reg, val, stored_val)) + # Measurement collection + def is_measuring(self): + return self.query_rate > 0 + def _handle_lis2dw_data(self, params): + with self.lock: + self.raw_samples.append(params) + def _extract_samples(self, raw_samples): + # Load variables to optimize inner loop below + (x_pos, x_scale), (y_pos, y_scale), (z_pos, z_scale) = self.axes_map + last_sequence = self.last_sequence + time_base, chip_base, inv_freq = self.clock_sync.get_time_translation() + # Process every message in raw_samples + count = seq = 0 + samples = [None] * (len(raw_samples) * SAMPLES_PER_BLOCK) + for params in raw_samples: + seq_diff = (last_sequence - params['sequence']) & 0xffff + seq_diff -= (seq_diff & 0x8000) << 1 + seq = last_sequence - seq_diff + d = bytearray(params['data']) + msg_cdiff = seq * SAMPLES_PER_BLOCK - chip_base + + for i in range(len(d) // BYTES_PER_SAMPLE): + d_xyz = d[i*BYTES_PER_SAMPLE:(i+1)*BYTES_PER_SAMPLE] + xlow, xhigh, ylow, yhigh, zlow, zhigh = d_xyz + # Merge and perform twos-complement + + rx = (((xhigh << 8) | xlow)) - ((xhigh & 0x80) << 9) + ry = (((yhigh << 8) | ylow)) - ((yhigh & 0x80) << 9) + rz = (((zhigh << 8) | zlow)) - ((zhigh & 0x80) << 9) + + raw_xyz = (rx, ry, rz) + + x = round(raw_xyz[x_pos] * x_scale, 6) + y = round(raw_xyz[y_pos] * y_scale, 6) + z = round(raw_xyz[z_pos] * z_scale, 6) + + ptime = round(time_base + (msg_cdiff + i) * inv_freq, 6) + samples[count] = (ptime, x, y, z) + count += 1 + self.clock_sync.set_last_chip_clock(seq * SAMPLES_PER_BLOCK + i) + del samples[count:] + return samples + def _update_clock(self, minclock=0): + # Query current state + for retry in range(5): + params = self.query_lis2dw_status_cmd.send([self.oid], + minclock=minclock) + fifo = params['fifo'] & 0x1f + if fifo <= 32: + break + else: + raise self.printer.command_error("Unable to query lis2dw fifo") + mcu_clock = self.mcu.clock32_to_clock64(params['clock']) + sequence = (self.last_sequence & ~0xffff) | params['next_sequence'] + if sequence < self.last_sequence: + sequence += 0x10000 + self.last_sequence = sequence + buffered = params['buffered'] + limit_count = (self.last_limit_count & ~0xffff) | params['limit_count'] + if limit_count < self.last_limit_count: + limit_count += 0x10000 + self.last_limit_count = limit_count + duration = params['query_ticks'] + if duration > self.max_query_duration: + # Skip measurement as a high query time could skew clock tracking + self.max_query_duration = max(2 * self.max_query_duration, + self.mcu.seconds_to_clock(.000005)) + return + self.max_query_duration = 2 * duration + msg_count = (sequence * SAMPLES_PER_BLOCK + + buffered // BYTES_PER_SAMPLE + fifo) + # The "chip clock" is the message counter plus .5 for average + # inaccuracy of query responses and plus .5 for assumed offset + # of lis2dw hw processing time. + chip_clock = msg_count + 1 + self.clock_sync.update(mcu_clock + duration // 2, chip_clock) + def _start_measurements(self): + if self.is_measuring(): + return + # In case of miswiring, testing LIS2DW device ID prevents treating + # noise or wrong signal as a correctly initialized device + dev_id = self.read_reg(REG_LIS2DW_WHO_AM_I_ADDR) + logging.info("lis2dw_dev_id: %x", dev_id) + if dev_id != LIS2DW_DEV_ID: + raise self.printer.command_error( + "Invalid lis2dw id (got %x vs %x).\n" + "This is generally indicative of connection problems\n" + "(e.g. faulty wiring) or a faulty lis2dw chip." + % (dev_id, LIS2DW_DEV_ID)) + # Setup chip in requested query rate + # ODR/2, +-16g, low-pass filter, Low-noise abled + self.set_reg(REG_LIS2DW_CTRL_REG6_ADDR, 0x34) + # Continuous mode: If the FIFO is full + # the new sample overwrites the older sample. + self.set_reg(REG_LIS2DW_FIFO_CTRL, 0xC0) + # High-Performance / Low-Power mode 1600/200 Hz + # High-Performance Mode (14-bit resolution) + self.set_reg(REG_LIS2DW_CTRL_REG1_ADDR, 0x94) + + # Setup samples + with self.lock: + self.raw_samples = [] + # Start bulk reading + systime = self.printer.get_reactor().monotonic() + print_time = self.mcu.estimated_print_time(systime) + MIN_MSG_TIME + reqclock = self.mcu.print_time_to_clock(print_time) + rest_ticks = self.mcu.seconds_to_clock(4. / self.data_rate) + self.query_rate = self.data_rate + self.query_lis2dw_cmd.send([self.oid, reqclock, rest_ticks], + reqclock=reqclock) + logging.info("LIS2DW starting '%s' measurements", self.name) + # Initialize clock tracking + self.last_sequence = 0 + self.last_limit_count = self.last_error_count = 0 + self.clock_sync.reset(reqclock, 0) + self.max_query_duration = 1 << 31 + self._update_clock(minclock=reqclock) + self.max_query_duration = 1 << 31 + def _finish_measurements(self): + if not self.is_measuring(): + return + # Halt bulk reading + params = self.query_lis2dw_end_cmd.send([self.oid, 0, 0]) + self.query_rate = 0 + with self.lock: + self.raw_samples = [] + logging.info("LIS2DW finished '%s' measurements", self.name) + self.set_reg(REG_LIS2DW_FIFO_CTRL, 0x00) + # API interface + def _api_update(self, eventtime): + self._update_clock() + with self.lock: + raw_samples = self.raw_samples + self.raw_samples = [] + if not raw_samples: + return {} + samples = self._extract_samples(raw_samples) + if not samples: + return {} + return {'data': samples, 'errors': self.last_error_count, + 'overflows': self.last_limit_count} + def _api_startstop(self, is_start): + if is_start: + self._start_measurements() + else: + self._finish_measurements() + def _handle_dump_lis2dw(self, web_request): + self.api_dump.add_client(web_request) + hdr = ('time', 'x_acceleration', 'y_acceleration', 'z_acceleration') + web_request.send({'header': hdr}) + def start_internal_client(self): + cconn = self.api_dump.add_internal_client() + return adxl345.AccelQueryHelper(self.printer, cconn) + + +def load_config(config): + return LIS2DW(config) + +def load_config_prefix(config): + return LIS2DW(config) diff --git a/src/Makefile b/src/Makefile index e898455494ed..0fcfacecd583 100644 --- a/src/Makefile +++ b/src/Makefile @@ -15,6 +15,6 @@ src-$(CONFIG_WANT_DISPLAYS) += lcd_st7920.c lcd_hd44780.c src-$(CONFIG_WANT_SOFTWARE_SPI) += spi_software.c src-$(CONFIG_WANT_SOFTWARE_I2C) += i2c_software.c sensors-src-$(CONFIG_HAVE_GPIO_SPI) := thermocouple.c sensor_adxl345.c \ - sensor_angle.c + sensor_angle.c sensor_lis2dw.c sensors-src-$(CONFIG_HAVE_GPIO_I2C) += sensor_mpu9250.c src-$(CONFIG_WANT_SENSORS) += $(sensors-src-y) diff --git a/src/sensor_lis2dw.c b/src/sensor_lis2dw.c new file mode 100644 index 000000000000..52612623f085 --- /dev/null +++ b/src/sensor_lis2dw.c @@ -0,0 +1,221 @@ +// Support for gathering acceleration data from LIS2DW chip +// +// Copyright (C) 2023 Zhou.XianMing +// Copyright (C) 2020 Kevin O'Connor +// +// This file may be distributed under the terms of the GNU GPLv3 license. + +#include // memcpy +#include "board/irq.h" // irq_disable +#include "board/misc.h" // timer_read_time +#include "basecmd.h" // oid_alloc +#include "command.h" // DECL_COMMAND +#include "sched.h" // DECL_TASK +#include "spicmds.h" // spidev_transfer + +#define LIS_AR_DATAX0 0x28 +#define LIS_AM_READ 0x80 +#define LIS_FIFO_CTRL 0x2E +#define LIS_FIFO_SAMPLES 0x2F + +struct lis2dw { + struct timer timer; + uint32_t rest_ticks; + struct spidev_s *spi; + uint16_t sequence, limit_count; + uint8_t flags, data_count, fifo_disable; + uint8_t data[48]; +}; + +enum { + LIS_HAVE_START = 1<<0, LIS_RUNNING = 1<<1, LIS_PENDING = 1<<2, +}; + +static struct task_wake lis2dw_wake; + +// Event handler that wakes lis2dw_task() periodically +static uint_fast8_t +lis2dw_event(struct timer *timer) +{ + struct lis2dw *ax = container_of(timer, struct lis2dw, timer); + ax->flags |= LIS_PENDING; + sched_wake_task(&lis2dw_wake); + return SF_DONE; +} + +void +command_config_lis2dw(uint32_t *args) +{ + struct lis2dw *ax = oid_alloc(args[0], command_config_lis2dw + , sizeof(*ax)); + ax->timer.func = lis2dw_event; + ax->spi = spidev_oid_lookup(args[1]); +} +DECL_COMMAND(command_config_lis2dw, "config_lis2dw oid=%c spi_oid=%c"); + +// Report local measurement buffer +static void +lis2dw_report(struct lis2dw *ax, uint8_t oid) +{ + sendf("lis2dw_data oid=%c sequence=%hu data=%*s" + , oid, ax->sequence, ax->data_count, ax->data); + ax->data_count = 0; + ax->sequence++; +} + +// Report buffer and fifo status +static void +lis2dw_status(struct lis2dw *ax, uint_fast8_t oid + , uint32_t time1, uint32_t time2, uint_fast8_t fifo) +{ + sendf("lis2dw_status oid=%c clock=%u query_ticks=%u next_sequence=%hu" + " buffered=%c fifo=%c limit_count=%hu" + , oid, time1, time2-time1, ax->sequence + , ax->data_count, fifo, ax->limit_count); +} + +// Helper code to reschedule the lis2dw_event() timer +static void +lis2dw_reschedule_timer(struct lis2dw *ax) +{ + irq_disable(); + ax->timer.waketime = timer_read_time() + ax->rest_ticks; + sched_add_timer(&ax->timer); + irq_enable(); +} + +// Query accelerometer data +static void +lis2dw_query(struct lis2dw *ax, uint8_t oid) +{ + uint8_t msg[7] = {0}; + uint8_t fifo[2] = {LIS_FIFO_SAMPLES| LIS_AM_READ , 0}; + uint8_t fifo_empty,fifo_ovrn = 0; + + msg[0] = LIS_AR_DATAX0 | LIS_AM_READ ; + uint8_t *d = &ax->data[ax->data_count]; + + spidev_transfer(ax->spi, 1, sizeof(msg), msg); + + spidev_transfer(ax->spi, 1, sizeof(fifo), fifo); + fifo_empty = fifo[1]&0x3F; + fifo_ovrn = fifo[1]&0x40; + + d[0] = msg[1]; // x low bits + d[1] = msg[2]; // x high bits + d[2] = msg[3]; // y low bits + d[3] = msg[4]; // y high bits + d[4] = msg[5]; // z low bits + d[5] = msg[6]; // z high bits + + ax->data_count += 6; + if (ax->data_count + 6 > ARRAY_SIZE(ax->data)) + lis2dw_report(ax, oid); + + // Check fifo status + if (fifo_ovrn) + ax->limit_count++; + + // check if we need to run the task again (more packets in fifo?) + if (!fifo_empty&&!(ax->fifo_disable)) { + // More data in fifo - wake this task again + sched_wake_task(&lis2dw_wake); + } else if (ax->flags & LIS_RUNNING) { + // Sleep until next check time + sched_del_timer(&ax->timer); + ax->flags &= ~LIS_PENDING; + lis2dw_reschedule_timer(ax); + } +} + +// Startup measurements +static void +lis2dw_start(struct lis2dw *ax, uint8_t oid) +{ + sched_del_timer(&ax->timer); + ax->flags = LIS_RUNNING; + ax->fifo_disable = 0; + uint8_t ctrl[2] = {LIS_FIFO_CTRL , 0xC0}; + spidev_transfer(ax->spi, 0, sizeof(ctrl), ctrl); + lis2dw_reschedule_timer(ax); +} + +// End measurements +static void +lis2dw_stop(struct lis2dw *ax, uint8_t oid) +{ + // Disable measurements + sched_del_timer(&ax->timer); + ax->flags = 0; + // Drain any measurements still in fifo + ax->fifo_disable = 1; + lis2dw_query(ax, oid); + + uint8_t ctrl[2] = {LIS_FIFO_CTRL , 0}; + uint32_t end1_time = timer_read_time(); + spidev_transfer(ax->spi, 0, sizeof(ctrl), ctrl); + uint32_t end2_time = timer_read_time(); + + uint8_t msg[2] = { LIS_FIFO_SAMPLES | LIS_AM_READ , 0}; + spidev_transfer(ax->spi, 1, sizeof(msg), msg); + uint8_t fifo_status = msg[1]&0x1f; + + //Report final data + if (ax->data_count) + lis2dw_report(ax, oid); + lis2dw_status(ax, oid, end1_time, end2_time, fifo_status); +} + +void +command_query_lis2dw(uint32_t *args) +{ + struct lis2dw *ax = oid_lookup(args[0], command_config_lis2dw); + + if (!args[2]) { + // End measurements + lis2dw_stop(ax, args[0]); + return; + } + // Start new measurements query + sched_del_timer(&ax->timer); + ax->timer.waketime = args[1]; + ax->rest_ticks = args[2]; + ax->flags = LIS_HAVE_START; + ax->sequence = ax->limit_count = 0; + ax->data_count = 0; + ax->fifo_disable = 0; + sched_add_timer(&ax->timer); +} +DECL_COMMAND(command_query_lis2dw, + "query_lis2dw oid=%c clock=%u rest_ticks=%u"); + +void +command_query_lis2dw_status(uint32_t *args) +{ + struct lis2dw *ax = oid_lookup(args[0], command_config_lis2dw); + uint8_t msg[2] = { LIS_FIFO_SAMPLES | LIS_AM_READ, 0x00 }; + uint32_t time1 = timer_read_time(); + spidev_transfer(ax->spi, 1, sizeof(msg), msg); + uint32_t time2 = timer_read_time(); + lis2dw_status(ax, args[0], time1, time2, msg[1]&0x1f); +} +DECL_COMMAND(command_query_lis2dw_status, "query_lis2dw_status oid=%c"); + +void +lis2dw_task(void) +{ + if (!sched_check_wake(&lis2dw_wake)) + return; + uint8_t oid; + struct lis2dw *ax; + foreach_oid(oid, ax, command_config_lis2dw) { + uint_fast8_t flags = ax->flags; + if (!(flags & LIS_PENDING)) + continue; + if (flags & LIS_HAVE_START) + lis2dw_start(ax, oid); + else + lis2dw_query(ax, oid); + } +} +DECL_TASK(lis2dw_task); From 5342689581fc74f3048b83e4ba6c65c2538fd8b7 Mon Sep 17 00:00:00 2001 From: bigtreetech Date: Mon, 7 Aug 2023 15:58:48 +0800 Subject: [PATCH 2/3] docs: Add how to configure LIS2DW instructions Signed-off-by: Alan.Ma from BigTreeTech tech@biqu3d.com --- docs/Config_Reference.md | 21 +++++++++++++++++++++ docs/Measuring_Resonances.md | 28 ++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md index d0b617a36e40..5b8038f7d332 100644 --- a/docs/Config_Reference.md +++ b/docs/Config_Reference.md @@ -1646,6 +1646,27 @@ cs_pin: # measurements. ``` +### [lis2dw] + +Support for LIS2DW accelerometers. + +``` +[lis2dw] +cs_pin: +# The SPI enable pin for the sensor. This parameter must be provided. +#spi_speed: 5000000 +# The SPI speed (in hz) to use when communicating with the chip. +# The default is 5000000. +#spi_bus: +#spi_software_sclk_pin: +#spi_software_mosi_pin: +#spi_software_miso_pin: +# See the "common SPI settings" section for a description of the +# above parameters. +#axes_map: x, y, z +# See the "adxl345" section for information on this parameter. +``` + ### [mpu9250] Support for MPU-9250, MPU-9255, MPU-6515, MPU-6050, and MPU-6500 diff --git a/docs/Measuring_Resonances.md b/docs/Measuring_Resonances.md index cf43e31d39b6..28cda9d0c656 100644 --- a/docs/Measuring_Resonances.md +++ b/docs/Measuring_Resonances.md @@ -1,11 +1,11 @@ # Measuring Resonances -Klipper has built-in support for the ADXL345 and MPU-9250 compatible +Klipper has built-in support for the ADXL345, MPU-9250 and LIS2DW compatible accelerometers which can be used to measure resonance frequencies of the printer for different axes, and auto-tune [input shapers](Resonance_Compensation.md) to compensate for resonances. Note that using accelerometers requires some -soldering and crimping. The ADXL345 can be connected to the SPI interface of a -Raspberry Pi or MCU board (it needs to be reasonably fast). The MPU family can +soldering and crimping. The ADXL345/LIS2DW can be connected to the SPI interface +of a Raspberry Pi or MCU board (it needs to be reasonably fast). The MPU family can be connected to the I2C interface of a Raspberry Pi directly, or to an I2C interface of an MCU board that supports 400kbit/s *fast mode* in Klipper. @@ -13,7 +13,7 @@ When sourcing accelerometers, be aware that there are a variety of different PCB board designs and different clones of them. If it is going to be connected to a 5V printer MCU ensure it has a voltage regulator and level shifters. -For ADXL345s, make sure that the board supports SPI mode (a small number of +For ADXL345s/LIS2DWs, make sure that the board supports SPI mode (a small number of boards appear to be hard-configured for I2C by pulling SDO to GND). For MPU-9250/MPU-9255/MPU-6515/MPU-6050/MPU-6500s there are also a variety of @@ -305,6 +305,26 @@ you'll also want to modify your `printer.cfg` file to include this: Restart Klipper via the `RESTART` command. +#### Configure LIS2DW series + +``` +[mcu lis] +# Change to whatever you found above. For example, +# usb-Klipper_rp2040_E661640843545B2E-if00 +serial: /dev/serial/by-id/usb-Klipper_rp2040_ + +[lis2dw] +cs_pin: lis:gpio1 +spi_bus: spi0a +axes_map: x,z,y + +[resonance_tester] +accel_chip: lis2dw +probe_points: + # Somewhere slightly above the middle of your print bed + 147,154, 20 +``` + #### Configure MPU-6000/9000 series With RPi Make sure the Linux I2C driver is enabled and the baud rate is From 428b0a0b07b4f55f8d3e8623055c451805397b60 Mon Sep 17 00:00:00 2001 From: bigtreetech Date: Thu, 10 Aug 2023 09:49:37 +0800 Subject: [PATCH 3/3] Kconfig: Support Enable/Disable lis2dw on chips with small flash size Signed-off-by: Alan.Ma from BigTreeTech tech@biqu3d.com --- src/Kconfig | 7 +++++++ src/Makefile | 3 ++- test/configs/ar100.config | 1 + test/configs/stm32f042.config | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Kconfig b/src/Kconfig index 915af2956a75..aaf506539493 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -100,6 +100,10 @@ config WANT_SENSORS bool depends on HAVE_GPIO_I2C || HAVE_GPIO_SPI default y +config WANT_LIS2DW + bool + depends on HAVE_GPIO_SPI + default y config WANT_SOFTWARE_I2C bool depends on HAVE_GPIO && HAVE_GPIO_I2C @@ -119,6 +123,9 @@ config WANT_DISPLAYS config WANT_SENSORS bool "Support external sensor devices" depends on HAVE_GPIO_I2C || HAVE_GPIO_SPI +config WANT_LIS2DW + bool "Support lis2dw 3-axis accelerometer" + depends on HAVE_GPIO_SPI config WANT_SOFTWARE_I2C bool "Support software based I2C \"bit-banging\"" depends on HAVE_GPIO && HAVE_GPIO_I2C diff --git a/src/Makefile b/src/Makefile index 0fcfacecd583..8d771f9eb484 100644 --- a/src/Makefile +++ b/src/Makefile @@ -15,6 +15,7 @@ src-$(CONFIG_WANT_DISPLAYS) += lcd_st7920.c lcd_hd44780.c src-$(CONFIG_WANT_SOFTWARE_SPI) += spi_software.c src-$(CONFIG_WANT_SOFTWARE_I2C) += i2c_software.c sensors-src-$(CONFIG_HAVE_GPIO_SPI) := thermocouple.c sensor_adxl345.c \ - sensor_angle.c sensor_lis2dw.c + sensor_angle.c +src-$(CONFIG_WANT_LIS2DW) += sensor_lis2dw.c sensors-src-$(CONFIG_HAVE_GPIO_I2C) += sensor_mpu9250.c src-$(CONFIG_WANT_SENSORS) += $(sensors-src-y) diff --git a/test/configs/ar100.config b/test/configs/ar100.config index a717fe3d0a73..6c9174824b5e 100644 --- a/test/configs/ar100.config +++ b/test/configs/ar100.config @@ -3,3 +3,4 @@ CONFIG_MACH_AR100=y CONFIG_WANT_DISPLAYS=n CONFIG_WANT_SOFTWARE_I2C=n CONFIG_WANT_SOFTWARE_SPI=n +CONFIG_WANT_LIS2DW=n diff --git a/test/configs/stm32f042.config b/test/configs/stm32f042.config index 85338f2e77f6..7f1e879fbb18 100644 --- a/test/configs/stm32f042.config +++ b/test/configs/stm32f042.config @@ -2,3 +2,4 @@ CONFIG_MACH_STM32=y CONFIG_MACH_STM32F042=y CONFIG_WANT_SOFTWARE_I2C=n +CONFIG_WANT_LIS2DW=n