Skip to content

Commit 2c884a9

Browse files
decsnynashif
authored andcommitted
drivers: spi_mcux_lpspi: Optimize dma callback
Optimize DMA callback by cleaning up the code and storing less debug logging strings. Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
1 parent 5dbe322 commit 2c884a9

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

drivers/spi/spi_mcux_lpspi.c

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -244,44 +244,46 @@ static void spi_mcux_dma_callback(const struct device *dev, void *arg, uint32_t
244244
/* arg directly holds the spi device */
245245
const struct device *spi_dev = arg;
246246
struct spi_mcux_data *data = (struct spi_mcux_data *)spi_dev->data;
247+
char debug_char;
247248

248249
if (status < 0) {
249-
LOG_ERR("DMA callback error with channel %d.", channel);
250-
data->status_flags |= LPSPI_DMA_ERROR_FLAG;
250+
goto error;
251+
}
252+
253+
/* identify the origin of this callback */
254+
if (channel == data->dma_tx.channel) {
255+
/* this part of the transfer ends */
256+
data->status_flags |= LPSPI_DMA_TX_DONE_FLAG;
257+
debug_char = 'T';
258+
} else if (channel == data->dma_rx.channel) {
259+
/* this part of the transfer ends */
260+
data->status_flags |= LPSPI_DMA_RX_DONE_FLAG;
261+
debug_char = 'R';
251262
} else {
252-
/* identify the origin of this callback */
253-
if (channel == data->dma_tx.channel) {
254-
/* this part of the transfer ends */
255-
data->status_flags |= LPSPI_DMA_TX_DONE_FLAG;
256-
LOG_DBG("DMA TX Block Complete");
257-
} else if (channel == data->dma_rx.channel) {
258-
/* this part of the transfer ends */
259-
data->status_flags |= LPSPI_DMA_RX_DONE_FLAG;
260-
LOG_DBG("DMA RX Block Complete");
261-
} else {
262-
LOG_ERR("DMA callback channel %d is not valid.", channel);
263-
data->status_flags |= LPSPI_DMA_ERROR_FLAG;
264-
}
263+
goto error;
265264
}
265+
266+
LOG_DBG("DMA %cX Block Complete", debug_char);
267+
266268
#if CONFIG_SPI_ASYNC
267-
if (data->ctx.asynchronous &&
268-
((data->status_flags & LPSPI_DMA_DONE_FLAG) == LPSPI_DMA_DONE_FLAG)) {
269+
if (data->ctx.asynchronous && (data->status_flags & LPSPI_DMA_DONE_FLAG)) {
269270
/* Load dma blocks of equal length */
270-
size_t dma_size = MIN(data->ctx.tx_len, data->ctx.rx_len);
271+
size_t dma_size = spi_context_max_continuous_chunk(data->ctx);
271272

272-
if (dma_size == 0) {
273-
dma_size = MAX(data->ctx.tx_len, data->ctx.rx_len);
273+
if (dma_size != 0) {
274+
return;
274275
}
275276

276277
spi_context_update_tx(&data->ctx, 1, dma_size);
277278
spi_context_update_rx(&data->ctx, 1, dma_size);
278-
279-
if (data->ctx.tx_len == 0 && data->ctx.rx_len == 0) {
280-
spi_context_complete(&data->ctx, spi_dev, 0);
281-
}
282-
return;
283279
}
284280
#endif
281+
282+
goto done;
283+
error:
284+
LOG_ERR("DMA callback error with channel %d.", channel);
285+
data->status_flags |= LPSPI_DMA_ERROR_FLAG;
286+
done:
285287
spi_context_complete(&data->ctx, spi_dev, 0);
286288
}
287289

0 commit comments

Comments
 (0)