Skip to content

Commit 2169887

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

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
@@ -35,6 +35,10 @@ void setup() {
3535
}
3636
```
3737
38+
### Extended API
39+
40+
* 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`.
41+
3842
#### Change default `SPI` instance pins
3943
It is also possible to change the default pins used by the `SPI` instance using above API:
4044

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,27 @@ void SPIClass::setClockDivider(uint8_t divider)
156156
* @brief Transfer one byte on the SPI bus.
157157
* begin() or beginTransaction() must be called at least once before.
158158
* @param data: byte to send.
159+
* @param skipReceive: skip receiving data after transmit or not.
160+
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
161+
* Optional, default: SPI_TRANSMITRECEIVE.
159162
* @return byte received from the slave.
160163
*/
161-
uint8_t SPIClass::transfer(uint8_t data)
164+
uint8_t SPIClass::transfer(uint8_t data, bool skipReceive)
162165
{
163-
spi_transfer(&_spi, &data, sizeof(uint8_t), SPI_TRANSFER_TIMEOUT, false);
166+
spi_transfer(&_spi, &data, sizeof(uint8_t), SPI_TRANSFER_TIMEOUT, skipReceive);
164167
return data;
165168
}
166169

167170
/**
168171
* @brief Transfer two bytes on the SPI bus in 16 bits format.
169172
* begin() or beginTransaction() must be called at least once before.
170173
* @param data: bytes to send.
174+
* @param skipReceive: skip receiving data after transmit or not.
175+
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
176+
* Optional, default: SPI_TRANSMITRECEIVE.
171177
* @return bytes received from the slave in 16 bits format.
172178
*/
173-
uint16_t SPIClass::transfer16(uint16_t data)
179+
uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
174180
{
175181
uint16_t tmp;
176182

@@ -179,7 +185,7 @@ uint16_t SPIClass::transfer16(uint16_t data)
179185
data = tmp;
180186
}
181187
spi_transfer(&_spi, (uint8_t *)&data, sizeof(uint16_t),
182-
SPI_TRANSFER_TIMEOUT, false);
188+
SPI_TRANSFER_TIMEOUT, skipReceive);
183189

184190
if (_spiSettings.bitOrder) {
185191
tmp = ((data & 0xff00) >> 8) | ((data & 0xff) << 8);
@@ -195,12 +201,15 @@ uint16_t SPIClass::transfer16(uint16_t data)
195201
* @param buf: pointer to the bytes to send. The bytes received are copy in
196202
* this buffer.
197203
* @param count: number of bytes to send/receive.
204+
* @param skipReceive: skip receiving data after transmit or not.
205+
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
206+
* Optional, default: SPI_TRANSMITRECEIVE.
198207
*/
199-
void SPIClass::transfer(void *buf, size_t count)
208+
void SPIClass::transfer(void *buf, size_t count, bool skipReceive)
200209
{
201210
if ((count != 0) && (buf != NULL)) {
202211
spi_transfer(&_spi, ((uint8_t *)buf), count,
203-
SPI_TRANSFER_TIMEOUT, false);
212+
SPI_TRANSFER_TIMEOUT, skipReceive);
204213
}
205214
}
206215

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
@@ -131,9 +134,9 @@ class SPIClass {
131134
/* Transfer functions: must be called after initialization of the SPI
132135
* instance with begin() or beginTransaction().
133136
*/
134-
virtual uint8_t transfer(uint8_t data);
135-
virtual uint16_t transfer16(uint16_t data);
136-
virtual void transfer(void *buf, size_t count);
137+
virtual uint8_t transfer(uint8_t data, bool skipReceive = SPI_TRANSMITRECEIVE);
138+
virtual uint16_t transfer16(uint16_t data, bool skipReceive = SPI_TRANSMITRECEIVE);
139+
virtual void transfer(void *buf, size_t count, bool skipReceive = SPI_TRANSMITRECEIVE);
137140

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

0 commit comments

Comments
 (0)