Skip to content

Commit 37665b5

Browse files
ananglstephanosio
authored andcommitted
drivers: spi_context: Refactor spi_context_wait_for_completion()
Refactor the code of this function to make it a bit easier to read. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent f36c15e commit 37665b5

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

drivers/spi/spi_context.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,41 +135,41 @@ static inline size_t spi_context_total_rx_len(struct spi_context *ctx);
135135
static inline int spi_context_wait_for_completion(struct spi_context *ctx)
136136
{
137137
int status = 0;
138-
k_timeout_t timeout;
139-
140-
/* Do not use any timeout in the slave mode, as in this case it is not
141-
* known when the transfer will actually start and what the frequency
142-
* will be.
143-
*/
144-
if (IS_ENABLED(CONFIG_SPI_SLAVE) && spi_context_is_slave(ctx)) {
145-
timeout = K_FOREVER;
146-
} else {
147-
uint32_t tx_len = spi_context_total_tx_len(ctx);
148-
uint32_t rx_len = spi_context_total_rx_len(ctx);
149-
uint32_t timeout_ms;
138+
bool wait;
150139

151-
timeout_ms = MAX(tx_len, rx_len) * 8 * 1000 /
152-
ctx->config->frequency;
153-
timeout_ms += CONFIG_SPI_COMPLETION_TIMEOUT_TOLERANCE;
140+
#ifdef CONFIG_SPI_ASYNC
141+
wait = !ctx->asynchronous;
142+
#else
143+
wait = true;
144+
#endif
154145

155-
timeout = K_MSEC(timeout_ms);
156-
}
146+
if (wait) {
147+
k_timeout_t timeout;
148+
149+
/* Do not use any timeout in the slave mode, as in this case
150+
* it is not known when the transfer will actually start and
151+
* what the frequency will be.
152+
*/
153+
if (IS_ENABLED(CONFIG_SPI_SLAVE) && spi_context_is_slave(ctx)) {
154+
timeout = K_FOREVER;
155+
} else {
156+
uint32_t tx_len = spi_context_total_tx_len(ctx);
157+
uint32_t rx_len = spi_context_total_rx_len(ctx);
158+
uint32_t timeout_ms;
159+
160+
timeout_ms = MAX(tx_len, rx_len) * 8 * 1000 /
161+
ctx->config->frequency;
162+
timeout_ms += CONFIG_SPI_COMPLETION_TIMEOUT_TOLERANCE;
163+
164+
timeout = K_MSEC(timeout_ms);
165+
}
157166

158-
#ifdef CONFIG_SPI_ASYNC
159-
if (!ctx->asynchronous) {
160167
if (k_sem_take(&ctx->sync, timeout)) {
161168
LOG_ERR("Timeout waiting for transfer complete");
162169
return -ETIMEDOUT;
163170
}
164171
status = ctx->sync_status;
165172
}
166-
#else
167-
if (k_sem_take(&ctx->sync, timeout)) {
168-
LOG_ERR("Timeout waiting for transfer complete");
169-
return -ETIMEDOUT;
170-
}
171-
status = ctx->sync_status;
172-
#endif /* CONFIG_SPI_ASYNC */
173173

174174
#ifdef CONFIG_SPI_SLAVE
175175
if (spi_context_is_slave(ctx) && !status) {

0 commit comments

Comments
 (0)