Skip to content

Commit

Permalink
feat(spi): allow to skip receive during transfer
Browse files Browse the repository at this point in the history
Linked to stm32duino#912.

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
  • Loading branch information
fpistm committed Nov 23, 2023
1 parent 49d1c30 commit 2169887
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 4 additions & 0 deletions libraries/SPI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ void setup() {
}
```
### Extended API
* All `transfer()` API's have a new bool argument `skipReceive`. It allows to skip receive data after transmitting. Value can be `SPI_TRANSMITRECEIVE` or `SPI_TRANSMITONLY`. Default `SPI_TRANSMITRECEIVE`.
#### Change default `SPI` instance pins
It is also possible to change the default pins used by the `SPI` instance using above API:
Expand Down
21 changes: 15 additions & 6 deletions libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,27 @@ void SPIClass::setClockDivider(uint8_t divider)
* @brief Transfer one byte on the SPI bus.
* begin() or beginTransaction() must be called at least once before.
* @param data: byte to send.
* @param skipReceive: skip receiving data after transmit or not.
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
* Optional, default: SPI_TRANSMITRECEIVE.
* @return byte received from the slave.
*/
uint8_t SPIClass::transfer(uint8_t data)
uint8_t SPIClass::transfer(uint8_t data, bool skipReceive)
{
spi_transfer(&_spi, &data, sizeof(uint8_t), SPI_TRANSFER_TIMEOUT, false);
spi_transfer(&_spi, &data, sizeof(uint8_t), SPI_TRANSFER_TIMEOUT, skipReceive);
return data;
}

/**
* @brief Transfer two bytes on the SPI bus in 16 bits format.
* begin() or beginTransaction() must be called at least once before.
* @param data: bytes to send.
* @param skipReceive: skip receiving data after transmit or not.
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
* Optional, default: SPI_TRANSMITRECEIVE.
* @return bytes received from the slave in 16 bits format.
*/
uint16_t SPIClass::transfer16(uint16_t data)
uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
{
uint16_t tmp;

Expand All @@ -179,7 +185,7 @@ uint16_t SPIClass::transfer16(uint16_t data)
data = tmp;
}
spi_transfer(&_spi, (uint8_t *)&data, sizeof(uint16_t),
SPI_TRANSFER_TIMEOUT, false);
SPI_TRANSFER_TIMEOUT, skipReceive);

if (_spiSettings.bitOrder) {
tmp = ((data & 0xff00) >> 8) | ((data & 0xff) << 8);
Expand All @@ -195,12 +201,15 @@ uint16_t SPIClass::transfer16(uint16_t data)
* @param buf: pointer to the bytes to send. The bytes received are copy in
* this buffer.
* @param count: number of bytes to send/receive.
* @param skipReceive: skip receiving data after transmit or not.
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
* Optional, default: SPI_TRANSMITRECEIVE.
*/
void SPIClass::transfer(void *buf, size_t count)
void SPIClass::transfer(void *buf, size_t count, bool skipReceive)
{
if ((count != 0) && (buf != NULL)) {
spi_transfer(&_spi, ((uint8_t *)buf), count,
SPI_TRANSFER_TIMEOUT, false);
SPI_TRANSFER_TIMEOUT, skipReceive);
}
}

Expand Down
9 changes: 6 additions & 3 deletions libraries/SPI/src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ extern "C" {
#define SPI_CLOCK_DIV64 64
#define SPI_CLOCK_DIV128 128

#define SPI_TRANSMITRECEIVE false
#define SPI_TRANSMITONLY true

// Defines a default timeout delay in milliseconds for the SPI transfer
#ifndef SPI_TRANSFER_TIMEOUT
#define SPI_TRANSFER_TIMEOUT 1000
Expand Down Expand Up @@ -131,9 +134,9 @@ class SPIClass {
/* Transfer functions: must be called after initialization of the SPI
* instance with begin() or beginTransaction().
*/
virtual uint8_t transfer(uint8_t data);
virtual uint16_t transfer16(uint16_t data);
virtual void transfer(void *buf, size_t count);
virtual uint8_t transfer(uint8_t data, bool skipReceive = SPI_TRANSMITRECEIVE);
virtual uint16_t transfer16(uint16_t data, bool skipReceive = SPI_TRANSMITRECEIVE);
virtual void transfer(void *buf, size_t count, bool skipReceive = SPI_TRANSMITRECEIVE);

/* These methods are deprecated and kept for compatibility.
* Use SPISettings with SPI.beginTransaction() to configure SPI parameters.
Expand Down

0 comments on commit 2169887

Please sign in to comment.