This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit d2e6e4f89fce204c1195da9cf005feac505c561c
Author: raiden00pl <[email protected]>
AuthorDate: Thu Oct 22 18:17:02 2026 +0200

    drivers/can/can.c: Reset rx_sem and the error report on RX flush.
    
    CANIOC_IFLUSH and CANIOC_IOFLUSH reset only the RX FIFO indexes.  The
    next read() then took the stale rx_sem count and failed with EIO, or
    returned a pending RX overflow error report (CONFIG_CAN_ERRORS).
    include/nuttx/can/can.h defines CANIOC_IFLUSH as "Flush data received
    but not read", so reset rx_sem and rx_error with the FIFO.
    
    Assisted-by: Claude:claude-opus-5-5
    Signed-off-by: raiden00pl <[email protected]>
---
 drivers/can/can.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/drivers/can/can.c b/drivers/can/can.c
index 887318bd440..87215c72e5a 100644
--- a/drivers/can/can.c
+++ b/drivers/can/can.c
@@ -880,6 +880,29 @@ return_with_irqdisabled:
   return ret;
 }
 
+/****************************************************************************
+ * Name: can_rxfifo_flush
+ *
+ * Description:
+ *   Discard all messages in a reader's RX FIFO.  rx_sem counts a non-empty
+ *   FIFO, so it is reset too.  A pending internal error report is also
+ *   returned by read(), so it is discarded as well.
+ *
+ * Assumptions:
+ *   Called with interrupts disabled
+ *
+ ****************************************************************************/
+
+static void can_rxfifo_flush(FAR struct can_rxfifo_s *fifo)
+{
+  fifo->rx_head = 0;
+  fifo->rx_tail = 0;
+#ifdef CONFIG_CAN_ERRORS
+  fifo->rx_error = 0;
+#endif
+  nxsem_reset(&fifo->rx_sem, 0);
+}
+
 /****************************************************************************
  * Name: can_rtrread
  *
@@ -1016,8 +1039,7 @@ static int can_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
 
       case CANIOC_IFLUSH:
         {
-          reader->fifo.rx_head = 0;
-          reader->fifo.rx_tail = 0;
+          can_rxfifo_flush(&reader->fifo);
 
           /* invoke lower half ioctl */
 
@@ -1044,9 +1066,7 @@ static int can_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
       case CANIOC_IOFLUSH:
         {
           can_sender_init(&dev->cd_sender);
-
-          reader->fifo.rx_head = 0;
-          reader->fifo.rx_tail = 0;
+          can_rxfifo_flush(&reader->fifo);
 
           /* invoke lower half ioctl */
 

Reply via email to