From f36c15e2e3d6e6baffb3dc91f30c38b2f84a4f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20G=C5=82=C4=85bek?= Date: Thu, 19 Jan 2023 11:40:42 +0100 Subject: [PATCH] drivers: spi_context: Use total transfer length in timeout calculation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When estimating the time that a given SPI transfer will take, whole buffer sets for TX and RX need to be taken into account, not only their first parts. Correct `spi_context_wait_for_completion()` accordingly. Signed-off-by: Andrzej Głąbek --- drivers/spi/spi_context.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/spi/spi_context.h b/drivers/spi/spi_context.h index 44f8f601aa6..e7e611f02ed 100644 --- a/drivers/spi/spi_context.h +++ b/drivers/spi/spi_context.h @@ -129,6 +129,9 @@ static inline void spi_context_release(struct spi_context *ctx, int status) #endif /* CONFIG_SPI_ASYNC */ } +static inline size_t spi_context_total_tx_len(struct spi_context *ctx); +static inline size_t spi_context_total_rx_len(struct spi_context *ctx); + static inline int spi_context_wait_for_completion(struct spi_context *ctx) { int status = 0; @@ -141,9 +144,11 @@ static inline int spi_context_wait_for_completion(struct spi_context *ctx) if (IS_ENABLED(CONFIG_SPI_SLAVE) && spi_context_is_slave(ctx)) { timeout = K_FOREVER; } else { + uint32_t tx_len = spi_context_total_tx_len(ctx); + uint32_t rx_len = spi_context_total_rx_len(ctx); uint32_t timeout_ms; - timeout_ms = MAX(ctx->tx_len, ctx->rx_len) * 8 * 1000 / + timeout_ms = MAX(tx_len, rx_len) * 8 * 1000 / ctx->config->frequency; timeout_ms += CONFIG_SPI_COMPLETION_TIMEOUT_TOLERANCE;