mirror of
https://github.com/Klipper3d/klipper.git
synced 2025-08-09 14:55:09 -06:00
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:
parent
a02da85101
commit
7793784640
2 changed files with 21 additions and 16 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue