drivers: spi_nrfx_spis: Refactor prepare_for_transfer()

Refactor the function to make the execution flow in transceive()
clearer. In particular, return error codes directly, not through
spi_context_complete() which is unnecessary in this case.

Signed-off-by: Andrzej Głąbek <andrzej.glabek@nordicsemi.no>
This commit is contained in:
Andrzej Głąbek 2023-07-27 11:07:02 +02:00 committed by Carles Cufí
commit f132f55e32

View file

@ -106,36 +106,30 @@ static int configure(const struct device *dev,
return 0; return 0;
} }
static void prepare_for_transfer(const struct device *dev, static int prepare_for_transfer(const struct device *dev,
const uint8_t *tx_buf, size_t tx_buf_len, const uint8_t *tx_buf, size_t tx_buf_len,
uint8_t *rx_buf, size_t rx_buf_len) uint8_t *rx_buf, size_t rx_buf_len)
{ {
struct spi_nrfx_data *dev_data = dev->data;
const struct spi_nrfx_config *dev_config = dev->config; const struct spi_nrfx_config *dev_config = dev->config;
int status; nrfx_err_t result;
if (tx_buf_len > dev_config->max_buf_len || if (tx_buf_len > dev_config->max_buf_len ||
rx_buf_len > dev_config->max_buf_len) { rx_buf_len > dev_config->max_buf_len) {
LOG_ERR("Invalid buffer sizes: Tx %d/Rx %d", LOG_ERR("Invalid buffer sizes: Tx %d/Rx %d",
tx_buf_len, rx_buf_len); tx_buf_len, rx_buf_len);
status = -EINVAL; return -EINVAL;
} else { }
nrfx_err_t result;
result = nrfx_spis_buffers_set(&dev_config->spis, result = nrfx_spis_buffers_set(&dev_config->spis,
tx_buf, tx_buf_len, tx_buf, tx_buf_len,
rx_buf, rx_buf_len); rx_buf, rx_buf_len);
if (result == NRFX_SUCCESS) { if (result != NRFX_SUCCESS) {
return; return -EIO;
} }
status = -EIO; return 0;
}
spi_context_complete(&dev_data->ctx, dev, status);
} }
static int transceive(const struct device *dev, static int transceive(const struct device *dev,
const struct spi_config *spi_cfg, const struct spi_config *spi_cfg,
const struct spi_buf_set *tx_bufs, const struct spi_buf_set *tx_bufs,
@ -161,14 +155,15 @@ static int transceive(const struct device *dev,
LOG_ERR("Only buffers located in RAM are supported"); LOG_ERR("Only buffers located in RAM are supported");
error = -ENOTSUP; error = -ENOTSUP;
} else { } else {
prepare_for_transfer(dev, error = prepare_for_transfer(dev,
tx_bufs ? tx_bufs->buffers[0].buf : NULL, tx_bufs ? tx_bufs->buffers[0].buf : NULL,
tx_bufs ? tx_bufs->buffers[0].len : 0, tx_bufs ? tx_bufs->buffers[0].len : 0,
rx_bufs ? rx_bufs->buffers[0].buf : NULL, rx_bufs ? rx_bufs->buffers[0].buf : NULL,
rx_bufs ? rx_bufs->buffers[0].len : 0); rx_bufs ? rx_bufs->buffers[0].len : 0);
if (error == 0) {
error = spi_context_wait_for_completion(&dev_data->ctx); error = spi_context_wait_for_completion(&dev_data->ctx);
} }
}
spi_context_release(&dev_data->ctx, error); spi_context_release(&dev_data->ctx, error);