drivers: spi_mcux_lpspi: Clean up next_packet func

Next packet function is way more complicated than it needs to be. Clean
it up by simplifying the code and making readable helper functions.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
This commit is contained in:
Declan Snyder 2024-09-20 18:03:34 -05:00 committed by Anas Nashif
commit 561b4347d7

View file

@ -125,53 +125,23 @@ static int spi_mcux_transfer_next_packet(const struct device *dev)
struct spi_mcux_data *data = dev->data; struct spi_mcux_data *data = dev->data;
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base); LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
struct spi_context *ctx = &data->ctx; struct spi_context *ctx = &data->ctx;
size_t max_chunk = spi_context_max_continuous_chunk(ctx);
lpspi_transfer_t transfer; lpspi_transfer_t transfer;
status_t status; status_t status;
if ((ctx->tx_len == 0) && (ctx->rx_len == 0)) { if (max_chunk == 0) {
/* nothing left to rx or tx, we're done! */ spi_context_cs_control(ctx, false);
spi_context_cs_control(&data->ctx, false); spi_context_complete(ctx, dev, 0);
spi_context_complete(&data->ctx, dev, 0);
return 0; return 0;
} }
data->transfer_len = max_chunk;
transfer.configFlags = transfer.configFlags =
kLPSPI_MasterPcsContinuous | (ctx->config->slave << LPSPI_MASTER_PCS_SHIFT); kLPSPI_MasterPcsContinuous | (ctx->config->slave << LPSPI_MASTER_PCS_SHIFT);
transfer.txData = (ctx->tx_len == 0 ? NULL : ctx->tx_buf);
if (ctx->tx_len == 0) { transfer.rxData = (ctx->rx_len == 0 ? NULL : ctx->rx_buf);
/* rx only, nothing to tx */ transfer.dataSize = max_chunk;
transfer.txData = NULL;
transfer.rxData = ctx->rx_buf;
transfer.dataSize = ctx->rx_len;
} else if (ctx->rx_len == 0) {
/* tx only, nothing to rx */
transfer.txData = (uint8_t *)ctx->tx_buf;
transfer.rxData = NULL;
transfer.dataSize = ctx->tx_len;
} else if (ctx->tx_len == ctx->rx_len) {
/* rx and tx are the same length */
transfer.txData = (uint8_t *)ctx->tx_buf;
transfer.rxData = ctx->rx_buf;
transfer.dataSize = ctx->tx_len;
} else if (ctx->tx_len > ctx->rx_len) {
/* Break up the tx into multiple transfers so we don't have to
* rx into a longer intermediate buffer. Leave chip select
* active between transfers.
*/
transfer.txData = (uint8_t *)ctx->tx_buf;
transfer.rxData = ctx->rx_buf;
transfer.dataSize = ctx->rx_len;
} else {
/* Break up the rx into multiple transfers so we don't have to
* tx from a longer intermediate buffer. Leave chip select
* active between transfers.
*/
transfer.txData = (uint8_t *)ctx->tx_buf;
transfer.rxData = ctx->rx_buf;
transfer.dataSize = ctx->tx_len;
}
data->transfer_len = transfer.dataSize;
status = LPSPI_MasterTransferNonBlocking(base, &data->handle, &transfer); status = LPSPI_MasterTransferNonBlocking(base, &data->handle, &transfer);
if (status != kStatus_Success) { if (status != kStatus_Success) {