From 41635e3c68386cbaaf6006d073e3767a4971c962 Mon Sep 17 00:00:00 2001 From: Christian Riggenbach Date: Mon, 2 May 2022 22:35:38 +0200 Subject: [PATCH 1/3] beginTransactionWithAssertingCS() and endTransactionWithDeassertingCS() These two methods are similar to beginTransaction() and endTransaction(), but, as the name implies, with CS management. They are both public, as beginTransaction() and endTransaction() are too. --- Adafruit_SPIDevice.cpp | 20 ++++++++++++++++++++ Adafruit_SPIDevice.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/Adafruit_SPIDevice.cpp b/Adafruit_SPIDevice.cpp index 3cca367..45a2e65 100644 --- a/Adafruit_SPIDevice.cpp +++ b/Adafruit_SPIDevice.cpp @@ -282,6 +282,26 @@ void Adafruit_SPIDevice::endTransaction(void) { } } +/*! + * @brief Write a buffer or two to the SPI device, with transaction + * management. + * @brief Manually begin a transaction (calls beginTransaction if hardware + * SPI) with asserting the CS pin + */ +void Adafruit_SPIDevice::beginTransactionWithAssertingCS() { + beginTransaction(); + setChipSelect(LOW); +} + +/*! + * @brief Manually end a transaction (calls endTransaction if hardware SPI) + * with deasserting the CS pin + */ +void Adafruit_SPIDevice::endTransactionWithDeassertingCS() { + setChipSelect(HIGH); + endTransaction(); +} + /*! * @brief Write a buffer or two to the SPI device, with transaction * management. diff --git a/Adafruit_SPIDevice.h b/Adafruit_SPIDevice.h index 4c8d7bf..7639a9b 100644 --- a/Adafruit_SPIDevice.h +++ b/Adafruit_SPIDevice.h @@ -88,6 +88,8 @@ public: void transfer(uint8_t *buffer, size_t len); void beginTransaction(void); void endTransaction(void); + void beginTransactionWithAssertingCS(); + void endTransactionWithDeassertingCS(); private: SPIClass *_spi; From c00ec13218669062731270f0cdf79ed4d65b3a00 Mon Sep 17 00:00:00 2001 From: Christian Riggenbach Date: Mon, 2 May 2022 22:37:17 +0200 Subject: [PATCH 2/3] moved setChipSelect() in front of beginTransactionWithAssertingCS() This is a more logical order, as beginTransactionWithAssertingCS() and endTransactionWithDeassertingCS() both call it. --- Adafruit_SPIDevice.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Adafruit_SPIDevice.cpp b/Adafruit_SPIDevice.cpp index 45a2e65..00a7c6b 100644 --- a/Adafruit_SPIDevice.cpp +++ b/Adafruit_SPIDevice.cpp @@ -282,6 +282,16 @@ void Adafruit_SPIDevice::endTransaction(void) { } } +/*! + * @brief Assert/Deassert the CS pin if it is defined + * @param value The state the CS is set to + */ +void Adafruit_SPIDevice::setChipSelect(int value) { + if (_cs != -1) { + digitalWrite(_cs, value); + } +} + /*! * @brief Write a buffer or two to the SPI device, with transaction * management. @@ -510,14 +520,4 @@ bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) { return true; } -/*! - * @brief Assert/Deassert the CS pin if it is defined - * @param value The state the CS is set to - */ -void Adafruit_SPIDevice::setChipSelect(int value) { - if (_cs == -1) - return; - digitalWrite(_cs, value); -} - #endif // SPI exists From f82f699c64306c89b6106f7e2197d420d6922dbd Mon Sep 17 00:00:00 2001 From: Christian Riggenbach Date: Mon, 2 May 2022 20:37:06 +0200 Subject: [PATCH 3/3] reuse beginTransactionWithAssertingCS()/endTransactionWithDeassertingCS() --- Adafruit_SPIDevice.cpp | 46 ++++++++---------------------------------- 1 file changed, 8 insertions(+), 38 deletions(-) diff --git a/Adafruit_SPIDevice.cpp b/Adafruit_SPIDevice.cpp index 00a7c6b..44a8f55 100644 --- a/Adafruit_SPIDevice.cpp +++ b/Adafruit_SPIDevice.cpp @@ -326,11 +326,8 @@ void Adafruit_SPIDevice::endTransactionWithDeassertingCS() { bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len, const uint8_t *prefix_buffer, size_t prefix_len) { - if (_spi) { - _spi->beginTransaction(*_spiSetting); - } + beginTransactionWithAssertingCS(); - setChipSelect(LOW); // do the writing #if defined(ARDUINO_ARCH_ESP32) if (_spi) { @@ -350,11 +347,7 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len, transfer(buffer[i]); } } - setChipSelect(HIGH); - - if (_spi) { - _spi->endTransaction(); - } + endTransactionWithDeassertingCS(); #ifdef DEBUG_SERIAL DEBUG_SERIAL.print(F("\tSPIDevice Wrote: ")); @@ -391,17 +384,10 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len, */ bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) { memset(buffer, sendvalue, len); // clear out existing buffer - if (_spi) { - _spi->beginTransaction(*_spiSetting); - } - setChipSelect(LOW); + beginTransactionWithAssertingCS(); transfer(buffer, len); - setChipSelect(HIGH); - - if (_spi) { - _spi->endTransaction(); - } + endTransactionWithDeassertingCS(); #ifdef DEBUG_SERIAL DEBUG_SERIAL.print(F("\tSPIDevice Read: ")); @@ -435,11 +421,7 @@ bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) { bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, uint8_t sendvalue) { - if (_spi) { - _spi->beginTransaction(*_spiSetting); - } - - setChipSelect(LOW); + beginTransactionWithAssertingCS(); // do the writing #if defined(ARDUINO_ARCH_ESP32) if (_spi) { @@ -485,11 +467,7 @@ bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer, DEBUG_SERIAL.println(); #endif - setChipSelect(HIGH); - - if (_spi) { - _spi->endTransaction(); - } + endTransactionWithDeassertingCS(); return true; } @@ -505,17 +483,9 @@ bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer, * writes */ bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) { - if (_spi) { - _spi->beginTransaction(*_spiSetting); - } - - setChipSelect(LOW); + beginTransactionWithAssertingCS(); transfer(buffer, len); - setChipSelect(HIGH); - - if (_spi) { - _spi->endTransaction(); - } + endTransactionWithDeassertingCS(); return true; }