Skip to content

[breaking] SPI library rework #2171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(SPI): remove non arduino transfer api with 2 buffer
Signed-off-by: Frederic Pillon <[email protected]>
  • Loading branch information
fpistm committed Nov 14, 2023
commit 0ada13f60819d7be5cf5979284c8edf15da21df7
39 changes: 8 additions & 31 deletions libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ void SPIClass::setClockDivider(uint8_t _divider)
*/
byte SPIClass::transfer(uint8_t data)
{
uint8_t rx_buffer = 0;
spi_transfer(&_spi, &data, &rx_buffer, sizeof(uint8_t), SPI_TRANSFER_TIMEOUT, _spiSettings.noReceive);
return rx_buffer;
spi_transfer(&_spi, &data, sizeof(uint8_t), SPI_TRANSFER_TIMEOUT, _spiSettings.noReceive);
return data;
}

/**
Expand All @@ -178,22 +177,21 @@ byte SPIClass::transfer(uint8_t data)
*/
uint16_t SPIClass::transfer16(uint16_t data)
{
uint16_t rx_buffer = 0;
uint16_t tmp;

if (_spiSettings.bOrder) {
tmp = ((data & 0xff00) >> 8) | ((data & 0xff) << 8);
data = tmp;
}
spi_transfer(&_spi, (uint8_t *)&data, (uint8_t *)&rx_buffer, sizeof(uint16_t),
spi_transfer(&_spi, (uint8_t *)&data, sizeof(uint16_t),
SPI_TRANSFER_TIMEOUT, _spiSettings.noReceive);

if (_spiSettings.bOrder) {
tmp = ((rx_buffer & 0xff00) >> 8) | ((rx_buffer & 0xff) << 8);
rx_buffer = tmp;
tmp = ((data & 0xff00) >> 8) | ((data & 0xff) << 8);
data = tmp;
}

return rx_buffer;
return data;
}

/**
Expand All @@ -205,24 +203,8 @@ uint16_t SPIClass::transfer16(uint16_t data)
*/
void SPIClass::transfer(void *_buf, size_t _count)
{
if ((_count != 0) && (_buf != NULL)) {
spi_transfer(&_spi, ((uint8_t *)_buf), ((uint8_t *)_buf), _count,
SPI_TRANSFER_TIMEOUT, _spiSettings.noReceive);
}
}

/**
* @brief Transfer several bytes. One buffer contains the data to send and
* another one will contains the data received. begin() or
* beginTransaction() must be called at least once before.
* @param _bufout: pointer to the bytes to send.
* @param _bufin: pointer to the bytes received.
* @param _count: number of bytes to send/receive.
*/
void SPIClass::transfer(void *_bufout, void *_bufin, size_t _count)
{
if ((_count != 0) && (_bufout != NULL) && (_bufin != NULL)) {
spi_transfer(&_spi, ((uint8_t *)_bufout), ((uint8_t *)_bufin), _count,
if ((count != 0) && (buf != NULL)) {
spi_transfer(&_spi, ((uint8_t *)buf), count,
SPI_TRANSFER_TIMEOUT, _spiSettings.noReceive);
}
}
Expand Down Expand Up @@ -281,11 +263,6 @@ void SUBGHZSPIClass::transfer(void *_buf, size_t _count)
SPIClass::transfer(_buf, _count);
}

void SUBGHZSPIClass::transfer(void *_bufout, void *_bufin, size_t _count)
{
SPIClass::transfer(_bufout, _bufin, _count);
}

void SUBGHZSPIClass::enableDebugPins(uint32_t mosi, uint32_t miso, uint32_t sclk, uint32_t ssel)
{
/* Configure SPI GPIO pins */
Expand Down
2 changes: 0 additions & 2 deletions libraries/SPI/src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ class SPIClass {
virtual byte transfer(uint8_t _data);
virtual uint16_t transfer16(uint16_t _data);
virtual void transfer(void *_buf, size_t _count);
virtual void transfer(void *_bufout, void *_bufin, size_t _count);

/* These methods are deprecated and kept for compatibility.
* Use SPISettings with SPI.beginTransaction() to configure SPI parameters.
Expand Down Expand Up @@ -184,7 +183,6 @@ class SUBGHZSPIClass : public SPIClass {
byte transfer(uint8_t _data);
uint16_t transfer16(uint16_t _data);
void transfer(void *_buf, size_t _count);
void transfer(void *_bufout, void *_bufin, size_t _count);
void enableDebugPins(uint32_t mosi = DEBUG_SUBGHZSPI_MOSI, uint32_t miso = DEBUG_SUBGHZSPI_MISO, uint32_t sclk = DEBUG_SUBGHZSPI_SCLK, uint32_t ssel = DEBUG_SUBGHZSPI_SS);

using SPIClass::beginTransaction;
Expand Down
25 changes: 6 additions & 19 deletions libraries/SPI/src/utility/spi_com.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,38 +496,25 @@ void spi_deinit(spi_t *obj)
#endif
}

/**
* @brief This function is implemented by user to send data over SPI interface
* @param obj : pointer to spi_t structure
* @param Data : data to be sent
* @param len : length in bytes of the data to be sent
* @param Timeout: Timeout duration in tick
* @retval status of the send operation (0) in case of error
*/
spi_status_e spi_send(spi_t *obj, uint8_t *Data, uint16_t len, uint32_t Timeout)
{
return spi_transfer(obj, Data, Data, len, Timeout, 1 /* SPI_TRANSMITONLY */);
}

/**
* @brief This function is implemented by user to send/receive data over
* SPI interface
* @param obj : pointer to spi_t structure
* @param tx_buffer : tx data to send before reception
* @param rx_buffer : data to receive
* @param buffer : tx data to send before reception
* @param len : length in byte of the data to send and receive
* @param Timeout: Timeout duration in tick
* @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 *tx_buffer, uint8_t *rx_buffer,
uint16_t len, uint32_t Timeout, bool skipReceive)
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len,
uint32_t Timeout, bool skipReceive)
{
spi_status_e ret = SPI_OK;
uint32_t tickstart, size = len;
SPI_TypeDef *_SPI = obj->handle.Instance;
uint8_t *tx_buffer = buffer;

if ((obj == NULL) || (len == 0) || (Timeout == 0U)) {
if ((len == 0) || (Timeout == 0U)) {
return Timeout > 0U ? SPI_ERROR : SPI_TIMEOUT;
}
tickstart = HAL_GetTick();
Expand All @@ -553,7 +540,7 @@ spi_status_e spi_transfer(spi_t *obj, uint8_t *tx_buffer, uint8_t *rx_buffer,
#else
while (!LL_SPI_IsActiveFlag_RXNE(_SPI));
#endif
*rx_buffer++ = LL_SPI_ReceiveData8(_SPI);
*buffer++ = LL_SPI_ReceiveData8(_SPI);
}
if ((Timeout != HAL_MAX_DELAY) && (HAL_GetTick() - tickstart >= Timeout)) {
ret = SPI_TIMEOUT;
Expand Down
5 changes: 2 additions & 3 deletions libraries/SPI/src/utility/spi_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ typedef enum {
/* Exported functions ------------------------------------------------------- */
void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb);
void spi_deinit(spi_t *obj);
spi_status_e spi_send(spi_t *obj, uint8_t *Data, uint16_t len, uint32_t Timeout);
spi_status_e spi_transfer(spi_t *obj, uint8_t *tx_buffer,
uint8_t *rx_buffer, uint16_t len, uint32_t Timeout, bool skipReceive);
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len,
uint32_t Timeout, bool skipReceive);
uint32_t spi_getClkFreq(spi_t *obj);

#ifdef __cplusplus
Expand Down