From d1dbc88b70ad64e41973c9d9467d1f1801dcff1f Mon Sep 17 00:00:00 2001 From: RudolphRiedel <31180093+RudolphRiedel@users.noreply.github.com> Date: Sat, 22 Jul 2023 17:10:54 +0200 Subject: [PATCH 1/2] - major speedup for simple SPI.transfer(data) and SPI.transfer(buffer, count) --- libraries/SPI/src/SPI.cpp | 51 +++++++++++++++++++++++++++++++++++++++ libraries/SPI/src/SPI.h | 12 +++------ 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index eab0792182..921cb9eb03 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -398,6 +398,57 @@ void SPIClass::transfer(byte _pin, void *_bufout, void *_bufin, size_t _count, S } } +/** + * @brief Transfer one byte on the SPI bus. + * begin() or beginTransaction() must be called at least once before. + * @param data: byte to send. + * @return byte received from the slave. + */ +uint8_t SPIClass::transfer(uint8_t data) +{ +#if defined(SPI_SR_TXP) + while (!LL_SPI_IsActiveFlag_TXP(_spi.handle.Instance)); +#else + while (!LL_SPI_IsActiveFlag_TXE(_spi.handle.Instance)); +#endif + LL_SPI_TransmitData8(_spi.handle.Instance, data); + +#if defined(SPI_SR_RXP) + while (!LL_SPI_IsActiveFlag_RXP(_spi.handle.Instance)); +#else + while (!LL_SPI_IsActiveFlag_RXNE(_spi.handle.Instance)); +#endif + return LL_SPI_ReceiveData8(_spi.handle.Instance); +} + +/** + * @brief Transfer several bytes. Only one buffer used to send and receive data. + * begin() or beginTransaction() must be called at least once before. + * @param buf: pointer to the bytes to send. The bytes received are copy in + * this buffer. + * @param count: number of bytes to send/receive. + */ +void SPIClass::transfer(void *buf, size_t count) +{ + uint8_t *buffer = (uint8_t *) buf; + for (size_t index = 0; index < count; index++) + { +#if defined(SPI_SR_TXP) + while (!LL_SPI_IsActiveFlag_TXP(_spi.handle.Instance)); +#else + while (!LL_SPI_IsActiveFlag_TXE(_spi.handle.Instance)); +#endif + LL_SPI_TransmitData8(_spi.handle.Instance, buffer[index]); + +#if defined(SPI_SR_RXP) + while (!LL_SPI_IsActiveFlag_RXP(_spi.handle.Instance)); +#else + while (!LL_SPI_IsActiveFlag_RXNE(_spi.handle.Instance)); +#endif + buffer[index] = LL_SPI_ReceiveData8(_spi.handle.Instance); + } +} + /** * @brief Not implemented. */ diff --git a/libraries/SPI/src/SPI.h b/libraries/SPI/src/SPI.h index 7bde790337..77a8e21b4d 100644 --- a/libraries/SPI/src/SPI.h +++ b/libraries/SPI/src/SPI.h @@ -17,6 +17,7 @@ extern "C" { #include "utility/spi_com.h" } +#include "stm32yyxx_ll_spi.h" // SPI_HAS_TRANSACTION means SPI has // - beginTransaction() @@ -178,21 +179,14 @@ class SPIClass { virtual void transfer(byte _pin, void *_bufout, void *_bufin, size_t _count, SPITransferMode _mode = SPI_LAST); // Transfer functions when user controls himself the CS pin. - byte transfer(uint8_t _data, SPITransferMode _mode = SPI_LAST) - { - return transfer(CS_PIN_CONTROLLED_BY_USER, _data, _mode); - } + virtual uint8_t transfer(uint8_t data); + virtual void transfer(void *buf, size_t count); uint16_t transfer16(uint16_t _data, SPITransferMode _mode = SPI_LAST) { return transfer16(CS_PIN_CONTROLLED_BY_USER, _data, _mode); } - void transfer(void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST) - { - transfer(CS_PIN_CONTROLLED_BY_USER, _buf, _count, _mode); - } - void transfer(void *_bufout, void *_bufin, size_t _count, SPITransferMode _mode = SPI_LAST) { transfer(CS_PIN_CONTROLLED_BY_USER, _bufout, _bufin, _count, _mode); From 8f52dccfc94900820e52a98cf1da4efbc5e540ad Mon Sep 17 00:00:00 2001 From: RudolphRiedel <31180093+RudolphRiedel@users.noreply.github.com> Date: Mon, 24 Jul 2023 16:03:18 +0200 Subject: [PATCH 2/2] - fixed indentation --- libraries/SPI/src/SPI.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index 921cb9eb03..3ed89d0cc1 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -407,18 +407,18 @@ void SPIClass::transfer(byte _pin, void *_bufout, void *_bufin, size_t _count, S uint8_t SPIClass::transfer(uint8_t data) { #if defined(SPI_SR_TXP) - while (!LL_SPI_IsActiveFlag_TXP(_spi.handle.Instance)); + while (!LL_SPI_IsActiveFlag_TXP(_spi.handle.Instance)); #else - while (!LL_SPI_IsActiveFlag_TXE(_spi.handle.Instance)); + while (!LL_SPI_IsActiveFlag_TXE(_spi.handle.Instance)); #endif - LL_SPI_TransmitData8(_spi.handle.Instance, data); + LL_SPI_TransmitData8(_spi.handle.Instance, data); #if defined(SPI_SR_RXP) - while (!LL_SPI_IsActiveFlag_RXP(_spi.handle.Instance)); + while (!LL_SPI_IsActiveFlag_RXP(_spi.handle.Instance)); #else - while (!LL_SPI_IsActiveFlag_RXNE(_spi.handle.Instance)); + while (!LL_SPI_IsActiveFlag_RXNE(_spi.handle.Instance)); #endif - return LL_SPI_ReceiveData8(_spi.handle.Instance); + return LL_SPI_ReceiveData8(_spi.handle.Instance); } /** @@ -430,23 +430,22 @@ uint8_t SPIClass::transfer(uint8_t data) */ void SPIClass::transfer(void *buf, size_t count) { - uint8_t *buffer = (uint8_t *) buf; - for (size_t index = 0; index < count; index++) - { + uint8_t *buffer = (uint8_t *) buf; + for (size_t index = 0; index < count; index++) { #if defined(SPI_SR_TXP) - while (!LL_SPI_IsActiveFlag_TXP(_spi.handle.Instance)); + while (!LL_SPI_IsActiveFlag_TXP(_spi.handle.Instance)); #else - while (!LL_SPI_IsActiveFlag_TXE(_spi.handle.Instance)); + while (!LL_SPI_IsActiveFlag_TXE(_spi.handle.Instance)); #endif - LL_SPI_TransmitData8(_spi.handle.Instance, buffer[index]); + LL_SPI_TransmitData8(_spi.handle.Instance, buffer[index]); #if defined(SPI_SR_RXP) - while (!LL_SPI_IsActiveFlag_RXP(_spi.handle.Instance)); + while (!LL_SPI_IsActiveFlag_RXP(_spi.handle.Instance)); #else - while (!LL_SPI_IsActiveFlag_RXNE(_spi.handle.Instance)); + while (!LL_SPI_IsActiveFlag_RXNE(_spi.handle.Instance)); #endif - buffer[index] = LL_SPI_ReceiveData8(_spi.handle.Instance); - } + buffer[index] = LL_SPI_ReceiveData8(_spi.handle.Instance); + } } /**