From dcb8188f07fce41879d4cf8c9a666ad7c3c867f3 Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Tue, 5 Sep 2023 19:20:46 +0800 Subject: [PATCH] driver/spi: avoid calling QPOLL to change rx_length and cause data loss When the application calls poll and returns the POLLIN event, QPOLL will be called again during the read operation, causing rx_length to change and data to be lost. Therefore, read only need to actively call qpoll to collect driver data when rx length is 0. Signed-off-by: dongjiuzhu1 --- drivers/spi/spi_slave_driver.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spi_slave_driver.c b/drivers/spi/spi_slave_driver.c index ccc8f0dd3a..56ec300730 100644 --- a/drivers/spi/spi_slave_driver.c +++ b/drivers/spi/spi_slave_driver.c @@ -326,7 +326,7 @@ static ssize_t spi_slave_read(FAR struct file *filep, FAR char *buffer, return ret; } - do + while (priv->rx_length == 0) { remaining_words = SPIS_CTRLR_QPOLL(priv->ctrlr); if (remaining_words == 0) @@ -360,11 +360,11 @@ static ssize_t spi_slave_read(FAR struct file *filep, FAR char *buffer, } } } - while (priv->rx_length == 0); read_bytes = MIN(buflen, priv->rx_length); memcpy(buffer, priv->rx_buffer, read_bytes); + priv->rx_length -= read_bytes; nxmutex_unlock(&priv->lock); return (ssize_t)read_bytes;