Skip to content

Commit 8091319

Browse files
committed
chore(spi): avoid SPI_TRANSFER_TIMEOUT as argument
SPI_TRANSFER_TIMEOUT is always passed as an argument while it is a constant definition. So simply function call and check. Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent cd3043f commit 8091319

File tree

4 files changed

+48
-50
lines changed

4 files changed

+48
-50
lines changed

libraries/SPI/src/SPI.cpp

Lines changed: 3 additions & 5 deletions
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), SPI_TRANSFER_TIMEOUT, skipReceive);
166+
spi_transfer(&_spi, &data, sizeof(uint8_t), skipReceive);
167167
return data;
168168
}
169169

@@ -184,8 +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),
188-
SPI_TRANSFER_TIMEOUT, skipReceive);
187+
spi_transfer(&_spi, (uint8_t *)&data, sizeof(uint16_t), skipReceive);
189188

190189
if (_spiSettings.bitOrder) {
191190
tmp = ((data & 0xff00) >> 8) | ((data & 0xff) << 8);
@@ -208,8 +207,7 @@ uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
208207
void SPIClass::transfer(void *buf, size_t count, bool skipReceive)
209208
{
210209
if ((count != 0) && (buf != NULL)) {
211-
spi_transfer(&_spi, ((uint8_t *)buf), count,
212-
SPI_TRANSFER_TIMEOUT, skipReceive);
210+
spi_transfer(&_spi, ((uint8_t *)buf), count, skipReceive);
213211
}
214212
}
215213

libraries/SPI/src/SPI.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ extern "C" {
4141
#define SPI_TRANSMITRECEIVE false
4242
#define SPI_TRANSMITONLY true
4343

44-
// Defines a default timeout delay in milliseconds for the SPI transfer
45-
#ifndef SPI_TRANSFER_TIMEOUT
46-
#define SPI_TRANSFER_TIMEOUT 1000
47-
#endif
48-
4944
class SPISettings {
5045
public:
5146
constexpr SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode)

libraries/SPI/src/utility/spi_com.c

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -502,69 +502,68 @@ void spi_deinit(spi_t *obj)
502502
* @param obj : pointer to spi_t structure
503503
* @param buffer : tx data to send before reception
504504
* @param len : length in byte of the data to send and receive
505-
* @param Timeout: Timeout duration in tick
506505
* @param skipReceive: skip receiving data after transmit or not
507506
* @retval status of the send operation (0) in case of error
508507
*/
509-
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len,
510-
uint32_t Timeout, bool skipReceive)
508+
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len, bool skipReceive)
511509
{
512510
spi_status_e ret = SPI_OK;
513511
uint32_t tickstart, size = len;
514512
SPI_TypeDef *_SPI = obj->handle.Instance;
515513
uint8_t *tx_buffer = buffer;
516514

517-
if ((len == 0) || (Timeout == 0U)) {
518-
return Timeout > 0U ? SPI_ERROR : SPI_TIMEOUT;
519-
}
520-
tickstart = HAL_GetTick();
515+
if (len == 0) {
516+
ret = SPI_ERROR;
517+
} else {
518+
tickstart = HAL_GetTick();
521519

522520
#if defined(SPI_CR2_TSIZE)
523-
/* Start transfer */
524-
LL_SPI_SetTransferSize(_SPI, size);
525-
LL_SPI_Enable(_SPI);
526-
LL_SPI_StartMasterTransfer(_SPI);
521+
/* Start transfer */
522+
LL_SPI_SetTransferSize(_SPI, size);
523+
LL_SPI_Enable(_SPI);
524+
LL_SPI_StartMasterTransfer(_SPI);
527525
#endif
528526

529-
while (size--) {
527+
while (size--) {
530528
#if defined(SPI_SR_TXP)
531-
while (!LL_SPI_IsActiveFlag_TXP(_SPI));
529+
while (!LL_SPI_IsActiveFlag_TXP(_SPI));
532530
#else
533-
while (!LL_SPI_IsActiveFlag_TXE(_SPI));
531+
while (!LL_SPI_IsActiveFlag_TXE(_SPI));
534532
#endif
535-
LL_SPI_TransmitData8(_SPI, *tx_buffer++);
533+
LL_SPI_TransmitData8(_SPI, *tx_buffer++);
536534

537-
if (!skipReceive) {
535+
if (!skipReceive) {
538536
#if defined(SPI_SR_RXP)
539-
while (!LL_SPI_IsActiveFlag_RXP(_SPI));
537+
while (!LL_SPI_IsActiveFlag_RXP(_SPI));
540538
#else
541-
while (!LL_SPI_IsActiveFlag_RXNE(_SPI));
539+
while (!LL_SPI_IsActiveFlag_RXNE(_SPI));
542540
#endif
543-
*buffer++ = LL_SPI_ReceiveData8(_SPI);
544-
}
545-
if ((Timeout != HAL_MAX_DELAY) && (HAL_GetTick() - tickstart >= Timeout)) {
546-
ret = SPI_TIMEOUT;
547-
break;
541+
*buffer++ = LL_SPI_ReceiveData8(_SPI);
542+
}
543+
if ((SPI_TRANSFER_TIMEOUT != HAL_MAX_DELAY) &&
544+
(HAL_GetTick() - tickstart >= SPI_TRANSFER_TIMEOUT)) {
545+
ret = SPI_TIMEOUT;
546+
break;
547+
}
548548
}
549-
}
550549

551550
#if defined(SPI_IFCR_EOTC)
552-
// Add a delay before disabling SPI otherwise last-bit/last-clock may be truncated
553-
// See https://github.com/stm32duino/Arduino_Core_STM32/issues/1294
554-
// Computed delay is half SPI clock
555-
delayMicroseconds(obj->disable_delay);
556-
557-
/* Close transfer */
558-
/* Clear flags */
559-
LL_SPI_ClearFlag_EOT(_SPI);
560-
LL_SPI_ClearFlag_TXTF(_SPI);
561-
/* Disable SPI peripheral */
562-
LL_SPI_Disable(_SPI);
551+
// Add a delay before disabling SPI otherwise last-bit/last-clock may be truncated
552+
// See https://github.com/stm32duino/Arduino_Core_STM32/issues/1294
553+
// Computed delay is half SPI clock
554+
delayMicroseconds(obj->disable_delay);
555+
556+
/* Close transfer */
557+
/* Clear flags */
558+
LL_SPI_ClearFlag_EOT(_SPI);
559+
LL_SPI_ClearFlag_TXTF(_SPI);
560+
/* Disable SPI peripheral */
561+
LL_SPI_Disable(_SPI);
563562
#else
564-
/* Wait for end of transfer */
565-
while (LL_SPI_IsActiveFlag_BSY(_SPI));
563+
/* Wait for end of transfer */
564+
while (LL_SPI_IsActiveFlag_BSY(_SPI));
566565
#endif
567-
566+
}
568567
return ret;
569568
}
570569

libraries/SPI/src/utility/spi_com.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ typedef struct spi_s spi_t;
7878
#define SPI_SPEED_CLOCK_DIV128_MHZ ((uint32_t)128)
7979
#define SPI_SPEED_CLOCK_DIV256_MHZ ((uint32_t)256)
8080

81+
// Defines a default timeout delay in milliseconds for the SPI transfer
82+
#ifndef SPI_TRANSFER_TIMEOUT
83+
#define SPI_TRANSFER_TIMEOUT 1000
84+
#elif SPI_TRANSFER_TIMEOUT <= 0
85+
#error "SPI_TRANSFER_TIMEOUT cannot be less or equal to 0!"
86+
#endif
87+
8188
///@brief specifies the SPI mode to use
8289
//Mode Clock Polarity (CPOL) Clock Phase (CPHA)
8390
//SPI_MODE0 0 0
@@ -103,8 +110,7 @@ typedef enum {
103110
/* Exported functions ------------------------------------------------------- */
104111
void spi_init(spi_t *obj, uint32_t speed, SPIMode mode, uint8_t msb);
105112
void spi_deinit(spi_t *obj);
106-
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len,
107-
uint32_t Timeout, bool skipReceive);
113+
spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len, bool skipReceive);
108114
uint32_t spi_getClkFreq(spi_t *obj);
109115

110116
#ifdef __cplusplus

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy