Skip to content

Commit bd1390e

Browse files
committed
feat(spi): allow to skip receive during transfer
Linked to stm32duino#912. Signed-off-by: Frederic Pillon <[email protected]>
1 parent 8ab04de commit bd1390e

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

Diff for: libraries/SPI/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ void setup() {
3434
}
3535
```
3636
37+
### Extended API
38+
39+
* 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`.
40+
3741
#### Change default `SPI` instance pins
3842
It is also possible to change the default pins used by the `SPI` instance using above API:
3943

Diff for: libraries/SPI/src/SPI.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,27 @@ void SPIClass::setClockDivider(uint8_t _divider)
158158
* @brief Transfer one byte on the SPI bus.
159159
* begin() or beginTransaction() must be called at least once before.
160160
* @param data: byte to send.
161+
* @param skipReceive: skip receiving data after transmit or not.
162+
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
163+
* Optional, default: SPI_TRANSMITRECEIVE.
161164
* @return byte received from the slave.
162165
*/
163-
uint8_t SPIClass::transfer(uint8_t data)
166+
uint8_t SPIClass::transfer(uint8_t data, bool skipReceive)
164167
{
165-
spi_transfer(&_spi, &data, sizeof(uint8_t), SPI_TRANSFER_TIMEOUT, false);
168+
spi_transfer(&_spi, &data, sizeof(uint8_t), SPI_TRANSFER_TIMEOUT, skipReceive);
166169
return data;
167170
}
168171

169172
/**
170173
* @brief Transfer two bytes on the SPI bus in 16 bits format.
171174
* begin() or beginTransaction() must be called at least once before.
172175
* @param data: bytes to send.
176+
* @param skipReceive: skip receiving data after transmit or not.
177+
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
178+
* Optional, default: SPI_TRANSMITRECEIVE.
173179
* @return bytes received from the slave in 16 bits format.
174180
*/
175-
uint16_t SPIClass::transfer16(uint16_t data)
181+
uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
176182
{
177183
uint16_t tmp;
178184

@@ -181,7 +187,7 @@ uint16_t SPIClass::transfer16(uint16_t data)
181187
data = tmp;
182188
}
183189
spi_transfer(&_spi, (uint8_t *)&data, sizeof(uint16_t),
184-
SPI_TRANSFER_TIMEOUT, false);
190+
SPI_TRANSFER_TIMEOUT, skipReceive);
185191

186192
if (_spiSettings.getBitOrder()) {
187193
tmp = ((data & 0xff00) >> 8) | ((data & 0xff) << 8);
@@ -197,12 +203,15 @@ uint16_t SPIClass::transfer16(uint16_t data)
197203
* @param buf: pointer to the bytes to send. The bytes received are copy in
198204
* this buffer.
199205
* @param count: number of bytes to send/receive.
206+
* @param skipReceive: skip receiving data after transmit or not.
207+
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
208+
* Optional, default: SPI_TRANSMITRECEIVE.
200209
*/
201-
void SPIClass::transfer(void *buf, size_t count)
210+
void SPIClass::transfer(void *buf, size_t count, bool skipReceive)
202211
{
203212
if ((count != 0) && (buf != NULL)) {
204213
spi_transfer(&_spi, ((uint8_t *)buf), count,
205-
SPI_TRANSFER_TIMEOUT, false);
214+
SPI_TRANSFER_TIMEOUT, skipReceive);
206215
}
207216
}
208217

Diff for: libraries/SPI/src/SPI.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ extern "C" {
3838
#define SPI_CLOCK_DIV64 64
3939
#define SPI_CLOCK_DIV128 128
4040

41+
#define SPI_TRANSMITRECEIVE false
42+
#define SPI_TRANSMITONLY true
43+
4144
// Defines a default timeout delay in milliseconds for the SPI transfer
4245
#ifndef SPI_TRANSFER_TIMEOUT
4346
#define SPI_TRANSFER_TIMEOUT 1000
@@ -184,9 +187,9 @@ class SPIClass {
184187
/* Transfer functions: must be called after initialization of the SPI
185188
* instance with begin() or beginTransaction().
186189
*/
187-
virtual uint8_t transfer(uint8_t _data);
188-
virtual uint16_t transfer16(uint16_t _data);
189-
virtual void transfer(void *buf, size_t count);
190+
virtual uint8_t transfer(uint8_t _data, bool skipReceive = SPI_TRANSMITRECEIVE);
191+
virtual uint16_t transfer16(uint16_t _data, bool skipReceive = SPI_TRANSMITRECEIVE);
192+
virtual void transfer(void *buf, size_t count, bool skipReceive = SPI_TRANSMITRECEIVE);
190193

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

0 commit comments

Comments
 (0)