spicmds: Allow inversion of CS pin for SPI busses

Signed-off-by: Martin Hierholzer <martin@hierholzer.info>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Martin Hierholzer 2021-09-25 11:52:31 +02:00 committed by Kevin O'Connor
parent a02da85101
commit 7793784640
2 changed files with 21 additions and 16 deletions

View File

@ -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 # Helper code for working with devices connected to an MCU via an SPI bus
class MCU_SPI: 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.mcu = mcu
self.bus = bus self.bus = bus
# Config SPI object (set all CS pins high before spi_set_bus commands) # Config SPI object (set all CS pins high before spi_set_bus commands)
@ -47,7 +48,8 @@ class MCU_SPI:
if pin is None: if pin is None:
mcu.add_config_cmd("config_spi_without_cs oid=%d" % (self.oid,)) mcu.add_config_cmd("config_spi_without_cs oid=%d" % (self.oid,))
else: 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 # Generate SPI bus config message
if sw_pins is not None: if sw_pins is not None:
self.config_fmt = ( self.config_fmt = (
@ -103,7 +105,8 @@ class MCU_SPI:
# Helper to setup an spi bus from settings in a config section # Helper to setup an spi bus from settings in a config section
def MCU_SPI_from_config(config, mode, pin_option="cs_pin", 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 # Determine pin from config
ppins = config.get_printer().lookup_object("pins") ppins = config.get_printer().lookup_object("pins")
cs_pin = config.get(pin_option) 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) bus = config.get('spi_bus', None)
sw_pins = None sw_pins = None
# Create MCU_SPI object # 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)
###################################################################### ######################################################################

View File

@ -23,17 +23,18 @@ struct spidev_s {
}; };
enum { 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 void
command_config_spi(uint32_t *args) command_config_spi(uint32_t *args)
{ {
struct spidev_s *spi = oid_alloc(args[0], command_config_spi, sizeof(*spi)); struct spidev_s *spi = oid_alloc(args[0], command_config_spi, sizeof(*spi));
spi->pin = gpio_out_setup(args[1], 1); uint_fast8_t cs_active_high = args[2];
spi->flags |= SF_HAVE_PIN; 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 void
command_config_spi_without_cs(uint32_t *args) command_config_spi_without_cs(uint32_t *args)
@ -86,25 +87,26 @@ void
spidev_transfer(struct spidev_s *spi, uint8_t receive_data spidev_transfer(struct spidev_s *spi, uint8_t receive_data
, uint8_t data_len, uint8_t *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 // Not yet initialized
return; return;
if (CONFIG_HAVE_GPIO_BITBANGING && spi->flags & SF_SOFTWARE) if (CONFIG_HAVE_GPIO_BITBANGING && flags & SF_SOFTWARE)
spi_software_prepare(spi->spi_software); spi_software_prepare(spi->spi_software);
else else
spi_prepare(spi->spi_config); spi_prepare(spi->spi_config);
if (spi->flags & SF_HAVE_PIN) if (flags & SF_HAVE_PIN)
gpio_out_write(spi->pin, 0); 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); spi_software_transfer(spi->spi_software, receive_data, data_len, data);
else else
spi_transfer(spi->spi_config, receive_data, data_len, data); spi_transfer(spi->spi_config, receive_data, data_len, data);
if (spi->flags & SF_HAVE_PIN) if (flags & SF_HAVE_PIN)
gpio_out_write(spi->pin, 1); gpio_out_write(spi->pin, !(flags & SF_CS_ACTIVE_HIGH));
} }
void void
@ -163,7 +165,7 @@ spidev_shutdown(void)
struct spidev_s *spi; struct spidev_s *spi;
foreach_oid(oid, spi, command_config_spi) { foreach_oid(oid, spi, command_config_spi) {
if (spi->flags & SF_HAVE_PIN) 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 // Send shutdown messages