diff --git a/src/shared/drivers/usart/USART.cpp b/src/shared/drivers/usart/USART.cpp index 171edb219ac19b73147b84d101ed9c0945d35b5a..6f416be182f32243940d5880499a7dbcd7199c80 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 eb52fa565da67bbba723059ccbdd889a1facf929..06502eb2e900eba82ea9a0069287c95c38221ce6 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)