From 4d5bc87f829ad953d1f52583b2289ec2c2565231 Mon Sep 17 00:00:00 2001
From: Emilio Corigliano <emilio.corigliano@skywarder.eu>
Date: Mon, 11 Sep 2023 11:10:53 +0000
Subject: [PATCH] [USART] Improved clearQueue method for flushing the internal
 receiver queue

---
 src/shared/drivers/usart/USART.cpp     |  9 +++++-
 src/tests/drivers/usart/test-usart.cpp | 42 ++++++++++++++++++++++++--
 2 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/src/shared/drivers/usart/USART.cpp b/src/shared/drivers/usart/USART.cpp
index 171edb219..6f416be18 100644
--- a/src/shared/drivers/usart/USART.cpp
+++ b/src/shared/drivers/usart/USART.cpp
@@ -595,7 +595,14 @@ void USART::writeString(const char *buffer)
     };
 }
 
-void USART::clearQueue() { rxQueue.reset(); }
+void USART::clearQueue()
+{
+    char buf[usart_queue_default_capacity];
+    rxQueue.reset();
+    while (read(buf, usart_queue_default_capacity))
+        ;
+    rxQueue.reset();
+}
 
 STM32SerialWrapper::STM32SerialWrapper(USARTType *usart, int baudrate)
     : USARTInterface(usart, baudrate)
diff --git a/src/tests/drivers/usart/test-usart.cpp b/src/tests/drivers/usart/test-usart.cpp
index eb52fa565..06502eb2e 100644
--- a/src/tests/drivers/usart/test-usart.cpp
+++ b/src/tests/drivers/usart/test-usart.cpp
@@ -203,6 +203,40 @@ bool testCommunicationSequential(USARTInterface *src, USARTInterface *dst)
     return passed;
 }
 
+bool testClearQueue(USART *src, USART *dst)
+{
+    char buf[128];
+    size_t nReads{0};
+    src->writeString("Transmitting useless stuff!");
+    // miosix::delayUs(1000);
+    dst->clearQueue();
+
+    // Can be commented to test without read
+    if (dst->read(buf, 128, nReads))
+    {
+        printf("### read something after the clearQueue: %s (%zu bytes)\n", buf,
+               nReads);
+        // Shouldn't read anything
+        return false;
+    }
+
+    src->writeString("Now transmitting the juicy stuff :P");
+    dst->readBlocking(buf, 128, nReads);
+
+    // After the clearQueue we should only read the things written after
+    if (strcmp(buf, "Now transmitting the juicy stuff :P") != 0)
+    {
+        printf(
+            "### read something different than the things sent: %s (%zu "
+            "bytes)\n",
+            buf, nReads);
+        return false;
+    }
+
+    printf("*** clearQueue test passed\n");
+    return true;
+}
+
 /* Available default pins:
  * - USART1: tx=PA9  rx=PA10
  * - USART2: tx=PA2  rx=PA3
@@ -242,17 +276,19 @@ int main()
             // usartx.setWordLength(USART::WordLength::BIT8);
             // usartx.setParity(USART::ParityBit::NO_PARITY);
 
-            // USART usarty(UART4, baudrate);
-            STM32SerialWrapper usarty(UART4, baudrate, u4rx2::getPin(),
-                                      u4tx2::getPin());
+            USART usarty(UART4, baudrate);
+            // STM32SerialWrapper usarty(UART4, baudrate, u4rx2::getPin(),
+            //                           u4tx2::getPin());
 
             // testing transmission (both char and binary) "serial 1 <- serial
             // 2"
             testPassed &= testCommunicationSequential(&usartx, &usarty);
+            testPassed &= testClearQueue(&usartx, &usarty);
 
             // testing transmission (both char and binary) "serial 1 -> serial
             // 2"
             testPassed &= testCommunicationSequential(&usarty, &usartx);
+            testPassed &= testClearQueue(&usarty, &usartx);
         }
 
         if (testPassed)
-- 
GitLab