From ebd9035325fc3f4ca8e40b06804f3834bbea5a28 Mon Sep 17 00:00:00 2001 From: Grigori Goronzy Date: Sun, 8 Jul 2018 00:03:00 +0200 Subject: [PATCH] SPI: introduce spi_prepare function The SPI interface needs to be enabled and configured to the correct settings of a given oid before CS is asserted. The new function spi_prepare() allows ports to do that. This port only introduces the new function in all ports with no implementation and adds the call to the Klipper generic firmware code. That means everything still works as before. Ports need to be changed to fix the underlying issue. Discussion about the motivation here: https://github.com/KevinOConnor/klipper/pull/453#issuecomment-403131149 Signed-off-by: Grigori Goronzy --- src/avr/gpio.c | 4 ++++ src/avr/gpio.h | 1 + src/generic/gpio.h | 1 + src/linux/gpio.h | 1 + src/linux/spidev.c | 5 +++++ src/sam3x8e/gpio.h | 1 + src/sam3x8e/spi.c | 5 +++++ src/simulator/gpio.c | 4 ++++ src/spicmds.c | 2 ++ src/stm32f1/gpio.c | 5 +++++ src/stm32f1/gpio.h | 1 + 11 files changed, 30 insertions(+) diff --git a/src/avr/gpio.c b/src/avr/gpio.c index a6b7aa41..fdb18562 100644 --- a/src/avr/gpio.c +++ b/src/avr/gpio.c @@ -438,6 +438,10 @@ spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) return config; } +void spi_prepare(struct spi_config config) +{ +} + void spi_transfer(struct spi_config config, uint8_t receive_data , uint8_t len, uint8_t *data) diff --git a/src/avr/gpio.h b/src/avr/gpio.h index 515e4c9a..2f769cb9 100644 --- a/src/avr/gpio.h +++ b/src/avr/gpio.h @@ -39,6 +39,7 @@ struct spi_config { uint8_t spcr, spsr; }; struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate); +void spi_prepare(struct spi_config config); void spi_transfer(struct spi_config config, uint8_t receive_data , uint8_t len, uint8_t *data); diff --git a/src/generic/gpio.h b/src/generic/gpio.h index e6ef1280..11f2f394 100644 --- a/src/generic/gpio.h +++ b/src/generic/gpio.h @@ -35,6 +35,7 @@ struct spi_config { uint32_t cfg; }; struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate); +void spi_prepare(struct spi_config config); void spi_transfer(struct spi_config config, uint8_t receive_data , uint8_t len, uint8_t *data); diff --git a/src/linux/gpio.h b/src/linux/gpio.h index b18e5d3d..8d878597 100644 --- a/src/linux/gpio.h +++ b/src/linux/gpio.h @@ -23,6 +23,7 @@ struct spi_config { int fd; }; struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate); +void spi_prepare(struct spi_config config); void spi_transfer(struct spi_config config, uint8_t receive_data , uint8_t len, uint8_t *data); diff --git a/src/linux/spidev.c b/src/linux/spidev.c index cb511205..2d6f55ac 100644 --- a/src/linux/spidev.c +++ b/src/linux/spidev.c @@ -56,6 +56,11 @@ spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) return (struct spi_config) { fd }; } +void +spi_prepare(struct spi_config config) +{ +} + void spi_transfer(struct spi_config config, uint8_t receive_data , uint8_t len, uint8_t *data) diff --git a/src/sam3x8e/gpio.h b/src/sam3x8e/gpio.h index e7016665..d40eec21 100644 --- a/src/sam3x8e/gpio.h +++ b/src/sam3x8e/gpio.h @@ -33,6 +33,7 @@ struct spi_config { uint32_t cfg; }; struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate); +void spi_prepare(struct spi_config config); void spi_transfer(struct spi_config config, uint8_t receive_data , uint8_t len, uint8_t *data); diff --git a/src/sam3x8e/spi.c b/src/sam3x8e/spi.c index 3bbf6548..c1137318 100644 --- a/src/sam3x8e/spi.c +++ b/src/sam3x8e/spi.c @@ -95,6 +95,11 @@ spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) return (struct spi_config){.cfg = config}; } +void +spi_prepare(struct spi_config config) +{ +} + void spi_transfer(struct spi_config config, uint8_t receive_data , uint8_t len, uint8_t *data) diff --git a/src/simulator/gpio.c b/src/simulator/gpio.c index 327c05ce..095fc384 100644 --- a/src/simulator/gpio.c +++ b/src/simulator/gpio.c @@ -44,6 +44,10 @@ spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) return (struct spi_config){ }; } void +spi_prepare(struct spi_config config) +{ +} +void spi_transfer(struct spi_config config, uint8_t receive_data , uint8_t len, uint8_t *data) { diff --git a/src/spicmds.c b/src/spicmds.c index f87972a0..3c9c2928 100644 --- a/src/spicmds.c +++ b/src/spicmds.c @@ -58,10 +58,12 @@ spidev_transfer(struct spidev_s *spi, uint8_t receive_data , uint8_t data_len, uint8_t *data) { if (spi->flags & SF_HAVE_PIN) { + spi_prepare(spi->spi_config); gpio_out_write(spi->pin, 0); spi_transfer(spi->spi_config, receive_data, data_len, data); gpio_out_write(spi->pin, 1); } else { + spi_prepare(spi->spi_config); spi_transfer(spi->spi_config, receive_data, data_len, data); } } diff --git a/src/stm32f1/gpio.c b/src/stm32f1/gpio.c index ed507496..832febd8 100644 --- a/src/stm32f1/gpio.c +++ b/src/stm32f1/gpio.c @@ -300,6 +300,11 @@ spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) return config; } +void +spi_prepare(struct spi_config config) +{ +} + void spi_transfer(struct spi_config config, uint8_t receive_data, uint8_t len, uint8_t *data) diff --git a/src/stm32f1/gpio.h b/src/stm32f1/gpio.h index 8c2a00ca..c5f0775e 100644 --- a/src/stm32f1/gpio.h +++ b/src/stm32f1/gpio.h @@ -34,6 +34,7 @@ struct spi_config { SPI_TypeDef config; }; struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate); +void spi_prepare(struct spi_config config); void spi_transfer(struct spi_config config, uint8_t receive_data, uint8_t len, uint8_t *data);