Skip to content

[chore] Speedup for simple SPI.transfer #2082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,56 @@ 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.
*/
Expand Down
12 changes: 3 additions & 9 deletions libraries/SPI/src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
extern "C" {
#include "utility/spi_com.h"
}
#include "stm32yyxx_ll_spi.h"

// SPI_HAS_TRANSACTION means SPI has
// - beginTransaction()
Expand Down Expand Up @@ -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);
Expand Down