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 <andrzej.glabek@nordicsemi.no>
This commit is contained in:
parent
f36c15e2e3
commit
37665b5e95
1 changed files with 27 additions and 27 deletions
|
@ -135,41 +135,41 @@ 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)
|
static inline int spi_context_wait_for_completion(struct spi_context *ctx)
|
||||||
{
|
{
|
||||||
int status = 0;
|
int status = 0;
|
||||||
k_timeout_t timeout;
|
bool wait;
|
||||||
|
|
||||||
/* 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.
|
|
||||||
*/
|
|
||||||
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(tx_len, rx_len) * 8 * 1000 /
|
|
||||||
ctx->config->frequency;
|
|
||||||
timeout_ms += CONFIG_SPI_COMPLETION_TIMEOUT_TOLERANCE;
|
|
||||||
|
|
||||||
timeout = K_MSEC(timeout_ms);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_SPI_ASYNC
|
#ifdef CONFIG_SPI_ASYNC
|
||||||
if (!ctx->asynchronous) {
|
wait = !ctx->asynchronous;
|
||||||
|
#else
|
||||||
|
wait = true;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (wait) {
|
||||||
|
k_timeout_t timeout;
|
||||||
|
|
||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
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(tx_len, rx_len) * 8 * 1000 /
|
||||||
|
ctx->config->frequency;
|
||||||
|
timeout_ms += CONFIG_SPI_COMPLETION_TIMEOUT_TOLERANCE;
|
||||||
|
|
||||||
|
timeout = K_MSEC(timeout_ms);
|
||||||
|
}
|
||||||
|
|
||||||
if (k_sem_take(&ctx->sync, timeout)) {
|
if (k_sem_take(&ctx->sync, timeout)) {
|
||||||
LOG_ERR("Timeout waiting for transfer complete");
|
LOG_ERR("Timeout waiting for transfer complete");
|
||||||
return -ETIMEDOUT;
|
return -ETIMEDOUT;
|
||||||
}
|
}
|
||||||
status = ctx->sync_status;
|
status = ctx->sync_status;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
if (k_sem_take(&ctx->sync, timeout)) {
|
|
||||||
LOG_ERR("Timeout waiting for transfer complete");
|
|
||||||
return -ETIMEDOUT;
|
|
||||||
}
|
|
||||||
status = ctx->sync_status;
|
|
||||||
#endif /* CONFIG_SPI_ASYNC */
|
|
||||||
|
|
||||||
#ifdef CONFIG_SPI_SLAVE
|
#ifdef CONFIG_SPI_SLAVE
|
||||||
if (spi_context_is_slave(ctx) && !status) {
|
if (spi_context_is_slave(ctx) && !status) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue