Skip to content

Commit

Permalink
feat(spi): add transfer api with tx/rx buffer
Browse files Browse the repository at this point in the history
Fixes #2205

Signed-off-by: Frederic Pillon <[email protected]>
  • Loading branch information
fpistm committed Nov 28, 2023
1 parent 0b9c63d commit 9df4fe0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
32 changes: 23 additions & 9 deletions libraries/SPI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ User have 2 possibilities about the management of the CS pin:
* the CS pin is managed directly by the user code before to transfer the data (like the Arduino SPI library)
* the user uses a hardware CS pin linked to the SPI peripheral

### New API functions
## New API functions

* `SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)`: alternative class constructor
_Params_ SPI `mosi` pin
_Params_ SPI `miso` pin
_Params_ SPI `sclk` pin
_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.
#### Alternative class constructor
* `SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)`

_Param_ SPI `mosi` pin

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

_Param_ SPI `sclk` pin

_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.

##### Example

Expand All @@ -35,9 +37,15 @@ void setup() {
}
```
### Extended API
#### Transfer with Tx/Rx buffer
* `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.
* 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`.
_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.
_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.
_Param_ `count`: number of bytes to send/receive.
#### Change default `SPI` instance pins
It is also possible to change the default pins used by the `SPI` instance using above API:
Expand All @@ -63,3 +71,9 @@ It is also possible to change the default pins used by the `SPI` instance using
SPI.setMOSI(PC2); // using pin number PYn
SPI.begin(2);
```

* `SPI_HandleTypeDef *getHandle(void)`: Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). **Use at your own risk.**

## Extended API

* 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`.
26 changes: 21 additions & 5 deletions libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void SPIClass::setClockDivider(uint8_t divider)
*/
uint8_t SPIClass::transfer(uint8_t data, bool skipReceive)
{
spi_transfer(&_spi, &data, sizeof(uint8_t), skipReceive);
spi_transfer(&_spi, &data, (!skipReceive) ? &data : NULL, sizeof(uint8_t));
return data;
}

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

if (_spiSettings.bitOrder) {
tmp = ((data & 0xff00) >> 8) | ((data & 0xff) << 8);
Expand All @@ -206,11 +206,27 @@ uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
*/
void SPIClass::transfer(void *buf, size_t count, bool skipReceive)
{
if ((count != 0) && (buf != NULL)) {
spi_transfer(&_spi, ((uint8_t *)buf), count, skipReceive);
}
spi_transfer(&_spi, (uint8_t *)buf, (!skipReceive) ? (uint8_t *)buf : NULL, count);

}

/**
* @brief Transfer several bytes. One constant buffer used to send and
* one to receive data.
* begin() or beginTransaction() must be called at least once before.
* @param tx_buf: 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.
* @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.
* @param count: number of bytes to send/receive.
*/
void SPIClass::transfer(const void *tx_buf, void *rx_buf, size_t count)
{
spi_transfer(&_spi, ((const uint8_t *)tx_buf), ((uint8_t *)rx_buf), count);
}


/**
* @brief Not implemented.
*/
Expand Down
5 changes: 5 additions & 0 deletions libraries/SPI/src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ class SPIClass {
uint16_t transfer16(uint16_t data, bool skipReceive = SPI_TRANSMITRECEIVE);
void transfer(void *buf, size_t count, bool skipReceive = SPI_TRANSMITRECEIVE);

/* Expand SPI API
* https://github.com/arduino/ArduinoCore-API/discussions/189
*/
void transfer(const void *tx_buf, void *rx_buf, size_t count);

/* These methods are deprecated and kept for compatibility.
* Use SPISettings with SPI.beginTransaction() to configure SPI parameters.
*/
Expand Down
21 changes: 12 additions & 9 deletions libraries/SPI/src/utility/spi_com.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,18 @@ void spi_deinit(spi_t *obj)
* @brief This function is implemented by user to send/receive data over
* SPI interface
* @param obj : pointer to spi_t structure
* @param buffer : tx data to send before reception
* @param tx_buffer : tx data to send before reception
* @param rx_buffer : rx data to receive if not numm
* @param len : length in byte of the data to send and receive
* @param skipReceive: skip receiving data after transmit or not
* @retval status of the send operation (0) in case of error
*/
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len, bool skipReceive)
spi_status_e spi_transfer(spi_t *obj, const uint8_t *tx_buffer, uint8_t *rx_buffer,
uint16_t len)
{
spi_status_e ret = SPI_OK;
uint32_t tickstart, size = len;
SPI_TypeDef *_SPI = obj->handle.Instance;
uint8_t *tx_buffer = buffer;
uint8_t *tx_buf = (uint8_t *)tx_buffer;

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

if (!skipReceive) {
#if defined(SPI_SR_RXP)
while (!LL_SPI_IsActiveFlag_RXP(_SPI));
while (!LL_SPI_IsActiveFlag_RXP(_SPI));
#else
while (!LL_SPI_IsActiveFlag_RXNE(_SPI));
while (!LL_SPI_IsActiveFlag_RXNE(_SPI));
#endif
*buffer++ = LL_SPI_ReceiveData8(_SPI);
if (rx_buffer) {
*rx_buffer++ = LL_SPI_ReceiveData8(_SPI);
} else {
LL_SPI_ReceiveData8(_SPI);
}
if ((SPI_TRANSFER_TIMEOUT != HAL_MAX_DELAY) &&
(HAL_GetTick() - tickstart >= SPI_TRANSFER_TIMEOUT)) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/SPI/src/utility/spi_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ typedef enum {
/* Exported functions ------------------------------------------------------- */
void spi_init(spi_t *obj, uint32_t speed, SPIMode mode, uint8_t msb);
void spi_deinit(spi_t *obj);
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len, bool skipReceive);
spi_status_e spi_transfer(spi_t *obj, const uint8_t *tx_buffer, uint8_t *rx_buffer, uint16_t len);
uint32_t spi_getClkFreq(spi_t *obj);

#ifdef __cplusplus
Expand Down

0 comments on commit 9df4fe0

Please sign in to comment.