Skip to content

Commit b72ef52

Browse files
committed
spi_nxp_lpspi: Add maximum wait time of fifo empty
Instead of waiting forever and potentially allowing infinite loop on ISR, wait some arbitrary amount of cycles to error out if it isn't happening. Still make this configurable for debugging purposes. Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
1 parent 72719c4 commit b72ef52

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

drivers/spi/spi_nxp_lpspi/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@ config SPI_MCUX_LPSPI_CPU
3636
This has lower latency than DMA-based driver but over the
3737
longer transfers will likely have less bandwidth and use more CPU time.
3838

39+
config SPI_NXP_LPSPI_TXFIFO_WAIT_CYCLES
40+
int "Number of CPU cycles to wait on TX fifo empty"
41+
default 0 if DEBUG
42+
default 10000
43+
help
44+
This option most likely does not need changed.
45+
The drivers tend to need to wait on confirming the transmit command
46+
is consumed by the hardware by checking of the TX fifo is emptied.
47+
Usually this should not be a problem and should happen immediately,
48+
but in the rare case of some bug, this option is available to avoid
49+
infinite loop locking the system in ISR. So this option gives a maximum
50+
number of CPU cycles to wait on that. The special value of 0 means infinite.
51+
The default of 10000 is arbitrary. The default of 0 if DEBUG is enabled
52+
is because getting stuck on that infinite loop can be useful for debugging
53+
the rare case of why the hardware is not consuming a command during development.
54+
3955
endif # SPI_MCUX_LPSPI

drivers/spi/spi_nxp_lpspi/spi_nxp_lpspi.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,10 @@ static int transceive(const struct device *dev, const struct spi_config *spi_cfg
335335
base->TCR |= LPSPI_TCR_CONT_MASK;
336336
}
337337
/* tcr is written to tx fifo */
338-
lpspi_wait_tx_fifo_empty(dev);
338+
ret = lpspi_wait_tx_fifo_empty(dev);
339+
if (ret) {
340+
return ret;
341+
}
339342

340343
/* start the transfer sequence which are handled by irqs */
341344
lpspi_next_tx_fill(dev);

drivers/spi/spi_nxp_lpspi/spi_nxp_lpspi_common.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,20 @@ static inline clock_ip_name_t lpspi_get_clock(LPSPI_Type *const base)
6060
}
6161
#endif
6262

63-
void lpspi_wait_tx_fifo_empty(const struct device *dev)
63+
int lpspi_wait_tx_fifo_empty(const struct device *dev)
6464
{
6565
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
66+
int arbitrary_cycle_limit = CONFIG_SPI_NXP_LPSPI_TXFIFO_WAIT_CYCLES;
67+
bool limit_wait = arbitrary_cycle_limit > 0;
6668

67-
while (LPSPI_GetTxFifoCount(base) != 0) {
69+
while (FIELD_GET(LPSPI_FSR_TXCOUNT_MASK, base->FSR) != 0) {
70+
if (limit_wait && (arbitrary_cycle_limit-- < 0)) {
71+
LOG_WRN("Failed waiting for TX fifo empty");
72+
return -EIO;
73+
}
6874
}
75+
76+
return 0;
6977
}
7078

7179
int spi_lpspi_release(const struct device *dev, const struct spi_config *spi_cfg)

drivers/spi/spi_nxp_lpspi/spi_nxp_lpspi_priv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ int spi_nxp_init_common(const struct device *dev);
6868
/* common api function for now */
6969
int spi_lpspi_release(const struct device *dev, const struct spi_config *spi_cfg);
7070

71-
void lpspi_wait_tx_fifo_empty(const struct device *dev);
71+
int lpspi_wait_tx_fifo_empty(const struct device *dev);
7272

7373
#define SPI_LPSPI_IRQ_FUNC_LP_FLEXCOMM(n) \
7474
nxp_lp_flexcomm_setirqhandler(DEVICE_DT_GET(DT_INST_PARENT(n)), DEVICE_DT_INST_GET(n), \

0 commit comments

Comments
 (0)