Skip to content

Commit 5db25d9

Browse files
ananglcfriedt
authored andcommitted
drivers: spi_context: Do not use transfer timeout in slave mode
Do not use any timeout in the slave mode, as in this case it is not known when the transfer will actually start and what the frequency will be. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent 525c112 commit 5db25d9

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

drivers/spi/spi_context.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,34 @@ static inline void spi_context_release(struct spi_context *ctx, int status)
112112
static inline int spi_context_wait_for_completion(struct spi_context *ctx)
113113
{
114114
int status = 0;
115-
uint32_t timeout_ms;
115+
k_timeout_t timeout;
116+
117+
/* Do not use any timeout in the slave mode, as in this case it is not
118+
* known when the transfer will actually start and what the frequency
119+
* will be.
120+
*/
121+
if (IS_ENABLED(CONFIG_SPI_SLAVE) && spi_context_is_slave(ctx)) {
122+
timeout = K_FOREVER;
123+
} else {
124+
uint32_t timeout_ms;
125+
126+
timeout_ms = MAX(ctx->tx_len, ctx->rx_len) * 8 * 1000 /
127+
ctx->config->frequency;
128+
timeout_ms += CONFIG_SPI_COMPLETION_TIMEOUT_TOLERANCE;
116129

117-
timeout_ms = MAX(ctx->tx_len, ctx->rx_len) * 8 * 1000 /
118-
ctx->config->frequency;
119-
timeout_ms += CONFIG_SPI_COMPLETION_TIMEOUT_TOLERANCE;
130+
timeout = K_MSEC(timeout_ms);
131+
}
120132

121133
#ifdef CONFIG_SPI_ASYNC
122134
if (!ctx->asynchronous) {
123-
if (k_sem_take(&ctx->sync, K_MSEC(timeout_ms))) {
135+
if (k_sem_take(&ctx->sync, timeout)) {
124136
LOG_ERR("Timeout waiting for transfer complete");
125137
return -ETIMEDOUT;
126138
}
127139
status = ctx->sync_status;
128140
}
129141
#else
130-
if (k_sem_take(&ctx->sync, K_MSEC(timeout_ms))) {
142+
if (k_sem_take(&ctx->sync, timeout)) {
131143
LOG_ERR("Timeout waiting for transfer complete");
132144
return -ETIMEDOUT;
133145
}

0 commit comments

Comments
 (0)