Skip to content

Commit 9df4fe0

Browse files
committed
feat(spi): add transfer api with tx/rx buffer
Fixes stm32duino#2205 Signed-off-by: Frederic Pillon <[email protected]>
1 parent 0b9c63d commit 9df4fe0

File tree

5 files changed

+62
-24
lines changed

5 files changed

+62
-24
lines changed

libraries/SPI/README.md

+23-9
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ User have 2 possibilities about the management of the CS pin:
77
* the CS pin is managed directly by the user code before to transfer the data (like the Arduino SPI library)
88
* the user uses a hardware CS pin linked to the SPI peripheral
99

10-
### New API functions
10+
## New API functions
1111

12-
* `SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)`: alternative class constructor
13-
_Params_ SPI `mosi` pin
14-
_Params_ SPI `miso` pin
15-
_Params_ SPI `sclk` pin
16-
_Params_ (optional) SPI `ssel` pin. This pin must be an hardware CS pin. If you configure this pin, the chip select will be managed by the SPI peripheral.
12+
#### Alternative class constructor
13+
* `SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)`
14+
15+
_Param_ SPI `mosi` pin
1716

18-
* `SPI_HandleTypeDef *getHandle(void)`: Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). **Use at your own risk.**
17+
_Param_ SPI `miso` pin
1918

19+
_Param_ SPI `sclk` pin
20+
21+
_Params_ (optional) SPI `ssel` pin. This pin must be an hardware CS pin. If you configure this pin, the chip select will be managed by the SPI peripheral.
2022

2123
##### Example
2224

@@ -35,9 +37,15 @@ void setup() {
3537
}
3638
```
3739
38-
### Extended API
40+
#### Transfer with Tx/Rx buffer
41+
42+
* `void transfer(const void *tx_buf, void *rx_buf, size_t count)` :Transfer several bytes. One constant buffer used to send and one to receive data.
3943
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`.
44+
_Param_ `tx_buf`: constant array of Tx bytes that is filled by the user before starting the SPI transfer. If NULL, default dummy 0xFF bytes will be clocked out.
45+
46+
_Param_ `rx_buf`: array of Rx bytes that will be filled by the slave during the SPI transfer. If NULL, the received data will be discarded.
47+
48+
_Param_ `count`: number of bytes to send/receive.
4149
4250
#### Change default `SPI` instance pins
4351
It is also possible to change the default pins used by the `SPI` instance using above API:
@@ -63,3 +71,9 @@ It is also possible to change the default pins used by the `SPI` instance using
6371
SPI.setMOSI(PC2); // using pin number PYn
6472
SPI.begin(2);
6573
```
74+
75+
* `SPI_HandleTypeDef *getHandle(void)`: Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). **Use at your own risk.**
76+
77+
## Extended API
78+
79+
* All defaustatndard `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`.

libraries/SPI/src/SPI.cpp

+21-5
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void SPIClass::setClockDivider(uint8_t divider)
163163
*/
164164
uint8_t SPIClass::transfer(uint8_t data, bool skipReceive)
165165
{
166-
spi_transfer(&_spi, &data, sizeof(uint8_t), skipReceive);
166+
spi_transfer(&_spi, &data, (!skipReceive) ? &data : NULL, sizeof(uint8_t));
167167
return data;
168168
}
169169

@@ -184,7 +184,7 @@ uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
184184
tmp = ((data & 0xff00) >> 8) | ((data & 0xff) << 8);
185185
data = tmp;
186186
}
187-
spi_transfer(&_spi, (uint8_t *)&data, sizeof(uint16_t), skipReceive);
187+
spi_transfer(&_spi, (uint8_t *)&data, (!skipReceive) ? (uint8_t *)&data : NULL, sizeof(uint16_t));
188188

189189
if (_spiSettings.bitOrder) {
190190
tmp = ((data & 0xff00) >> 8) | ((data & 0xff) << 8);
@@ -206,11 +206,27 @@ uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
206206
*/
207207
void SPIClass::transfer(void *buf, size_t count, bool skipReceive)
208208
{
209-
if ((count != 0) && (buf != NULL)) {
210-
spi_transfer(&_spi, ((uint8_t *)buf), count, skipReceive);
211-
}
209+
spi_transfer(&_spi, (uint8_t *)buf, (!skipReceive) ? (uint8_t *)buf : NULL, count);
210+
211+
}
212+
213+
/**
214+
* @brief Transfer several bytes. One constant buffer used to send and
215+
* one to receive data.
216+
* begin() or beginTransaction() must be called at least once before.
217+
* @param tx_buf: array of Tx bytes that is filled by the user before starting
218+
* the SPI transfer. If NULL, default dummy 0xFF bytes will be
219+
* clocked out.
220+
* @param rx_buf: array of Rx bytes that will be filled by the slave during
221+
* the SPI transfer. If NULL, the received data will be discarded.
222+
* @param count: number of bytes to send/receive.
223+
*/
224+
void SPIClass::transfer(const void *tx_buf, void *rx_buf, size_t count)
225+
{
226+
spi_transfer(&_spi, ((const uint8_t *)tx_buf), ((uint8_t *)rx_buf), count);
212227
}
213228

229+
214230
/**
215231
* @brief Not implemented.
216232
*/

libraries/SPI/src/SPI.h

+5
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ class SPIClass {
138138
uint16_t transfer16(uint16_t data, bool skipReceive = SPI_TRANSMITRECEIVE);
139139
void transfer(void *buf, size_t count, bool skipReceive = SPI_TRANSMITRECEIVE);
140140

141+
/* Expand SPI API
142+
* https://github.com/arduino/ArduinoCore-API/discussions/189
143+
*/
144+
void transfer(const void *tx_buf, void *rx_buf, size_t count);
145+
141146
/* These methods are deprecated and kept for compatibility.
142147
* Use SPISettings with SPI.beginTransaction() to configure SPI parameters.
143148
*/

libraries/SPI/src/utility/spi_com.c

+12-9
Original file line numberDiff line numberDiff line change
@@ -500,17 +500,18 @@ void spi_deinit(spi_t *obj)
500500
* @brief This function is implemented by user to send/receive data over
501501
* SPI interface
502502
* @param obj : pointer to spi_t structure
503-
* @param buffer : tx data to send before reception
503+
* @param tx_buffer : tx data to send before reception
504+
* @param rx_buffer : rx data to receive if not numm
504505
* @param len : length in byte of the data to send and receive
505-
* @param skipReceive: skip receiving data after transmit or not
506506
* @retval status of the send operation (0) in case of error
507507
*/
508-
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len, bool skipReceive)
508+
spi_status_e spi_transfer(spi_t *obj, const uint8_t *tx_buffer, uint8_t *rx_buffer,
509+
uint16_t len)
509510
{
510511
spi_status_e ret = SPI_OK;
511512
uint32_t tickstart, size = len;
512513
SPI_TypeDef *_SPI = obj->handle.Instance;
513-
uint8_t *tx_buffer = buffer;
514+
uint8_t *tx_buf = (uint8_t *)tx_buffer;
514515

515516
if (len == 0) {
516517
ret = SPI_ERROR;
@@ -530,15 +531,17 @@ spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len, bool skipRe
530531
#else
531532
while (!LL_SPI_IsActiveFlag_TXE(_SPI));
532533
#endif
533-
LL_SPI_TransmitData8(_SPI, *tx_buffer++);
534+
LL_SPI_TransmitData8(_SPI, tx_buf ? *tx_buf++ : 0XFF);
534535

535-
if (!skipReceive) {
536536
#if defined(SPI_SR_RXP)
537-
while (!LL_SPI_IsActiveFlag_RXP(_SPI));
537+
while (!LL_SPI_IsActiveFlag_RXP(_SPI));
538538
#else
539-
while (!LL_SPI_IsActiveFlag_RXNE(_SPI));
539+
while (!LL_SPI_IsActiveFlag_RXNE(_SPI));
540540
#endif
541-
*buffer++ = LL_SPI_ReceiveData8(_SPI);
541+
if (rx_buffer) {
542+
*rx_buffer++ = LL_SPI_ReceiveData8(_SPI);
543+
} else {
544+
LL_SPI_ReceiveData8(_SPI);
542545
}
543546
if ((SPI_TRANSFER_TIMEOUT != HAL_MAX_DELAY) &&
544547
(HAL_GetTick() - tickstart >= SPI_TRANSFER_TIMEOUT)) {

libraries/SPI/src/utility/spi_com.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ typedef enum {
110110
/* Exported functions ------------------------------------------------------- */
111111
void spi_init(spi_t *obj, uint32_t speed, SPIMode mode, uint8_t msb);
112112
void spi_deinit(spi_t *obj);
113-
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len, bool skipReceive);
113+
spi_status_e spi_transfer(spi_t *obj, const uint8_t *tx_buffer, uint8_t *rx_buffer, uint16_t len);
114114
uint32_t spi_getClkFreq(spi_t *obj);
115115

116116
#ifdef __cplusplus

0 commit comments

Comments
 (0)