diff --git a/klippy/extras/bus.py b/klippy/extras/bus.py index 9b3e476a..f37897d8 100644 --- a/klippy/extras/bus.py +++ b/klippy/extras/bus.py @@ -39,7 +39,8 @@ def resolve_bus_name(mcu, param, bus): # Helper code for working with devices connected to an MCU via an SPI bus class MCU_SPI: - def __init__(self, mcu, bus, pin, mode, speed, sw_pins=None): + def __init__(self, mcu, bus, pin, mode, speed, sw_pins=None, + cs_active_high=False): self.mcu = mcu self.bus = bus # Config SPI object (set all CS pins high before spi_set_bus commands) @@ -47,7 +48,8 @@ class MCU_SPI: if pin is None: mcu.add_config_cmd("config_spi_without_cs oid=%d" % (self.oid,)) else: - mcu.add_config_cmd("config_spi oid=%d pin=%s" % (self.oid, pin)) + mcu.add_config_cmd("config_spi oid=%d pin=%s cs_active_high=%d" + % (self.oid, pin, cs_active_high)) # Generate SPI bus config message if sw_pins is not None: self.config_fmt = ( @@ -103,7 +105,8 @@ class MCU_SPI: # Helper to setup an spi bus from settings in a config section def MCU_SPI_from_config(config, mode, pin_option="cs_pin", - default_speed=100000, share_type=None): + default_speed=100000, share_type=None, + cs_active_high=False): # Determine pin from config ppins = config.get_printer().lookup_object("pins") cs_pin = config.get(pin_option) @@ -130,7 +133,7 @@ def MCU_SPI_from_config(config, mode, pin_option="cs_pin", bus = config.get('spi_bus', None) sw_pins = None # Create MCU_SPI object - return MCU_SPI(mcu, bus, pin, mode, speed, sw_pins) + return MCU_SPI(mcu, bus, pin, mode, speed, sw_pins, cs_active_high) ###################################################################### diff --git a/src/spicmds.c b/src/spicmds.c index 8bdefbbf..2c3f3ed6 100644 --- a/src/spicmds.c +++ b/src/spicmds.c @@ -23,17 +23,18 @@ struct spidev_s { }; enum { - SF_HAVE_PIN = 1, SF_SOFTWARE = 2, SF_HARDWARE = 4, + SF_HAVE_PIN = 1, SF_SOFTWARE = 2, SF_HARDWARE = 4, SF_CS_ACTIVE_HIGH = 8 }; void command_config_spi(uint32_t *args) { struct spidev_s *spi = oid_alloc(args[0], command_config_spi, sizeof(*spi)); - spi->pin = gpio_out_setup(args[1], 1); - spi->flags |= SF_HAVE_PIN; + uint_fast8_t cs_active_high = args[2]; + spi->pin = gpio_out_setup(args[1], !cs_active_high); + spi->flags |= SF_HAVE_PIN | (cs_active_high ? SF_CS_ACTIVE_HIGH : 0); } -DECL_COMMAND(command_config_spi, "config_spi oid=%c pin=%u"); +DECL_COMMAND(command_config_spi, "config_spi oid=%c pin=%u cs_active_high=%c"); void command_config_spi_without_cs(uint32_t *args) @@ -86,25 +87,26 @@ void spidev_transfer(struct spidev_s *spi, uint8_t receive_data , uint8_t data_len, uint8_t *data) { - if (!(spi->flags & (SF_SOFTWARE|SF_HARDWARE))) + uint_fast8_t flags = spi->flags; + if (!(flags & (SF_SOFTWARE|SF_HARDWARE))) // Not yet initialized return; - if (CONFIG_HAVE_GPIO_BITBANGING && spi->flags & SF_SOFTWARE) + if (CONFIG_HAVE_GPIO_BITBANGING && flags & SF_SOFTWARE) spi_software_prepare(spi->spi_software); else spi_prepare(spi->spi_config); - if (spi->flags & SF_HAVE_PIN) - gpio_out_write(spi->pin, 0); + if (flags & SF_HAVE_PIN) + gpio_out_write(spi->pin, !!(flags & SF_CS_ACTIVE_HIGH)); - if (CONFIG_HAVE_GPIO_BITBANGING && spi->flags & SF_SOFTWARE) + if (CONFIG_HAVE_GPIO_BITBANGING && flags & SF_SOFTWARE) spi_software_transfer(spi->spi_software, receive_data, data_len, data); else spi_transfer(spi->spi_config, receive_data, data_len, data); - if (spi->flags & SF_HAVE_PIN) - gpio_out_write(spi->pin, 1); + if (flags & SF_HAVE_PIN) + gpio_out_write(spi->pin, !(flags & SF_CS_ACTIVE_HIGH)); } void @@ -163,7 +165,7 @@ spidev_shutdown(void) struct spidev_s *spi; foreach_oid(oid, spi, command_config_spi) { if (spi->flags & SF_HAVE_PIN) - gpio_out_write(spi->pin, 1); + gpio_out_write(spi->pin, !(spi->flags & SF_CS_ACTIVE_HIGH)); } // Send shutdown messages