Skip to content

Commit 0db15ac

Browse files
authored
Flush UART RX buffer before receiving answer from EDU (#482)
Fixes #478 At least, I hope if finally fixes that issue. A quick test with and without redirecting RF over UCI was successful.
2 parents 697132c + b321590 commit 0db15ac

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

Sts1CobcSw/Edu/Edu.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ template<>
111111

112112
template<typename T>
113113
[[nodiscard]] auto Retry(auto (*communicationFunction)()->Result<T>, int nTries) -> Result<T>;
114-
auto FlushUartReceiveBuffer() -> void;
115114
}
116115

117116

@@ -493,6 +492,7 @@ auto SendDataPacket(std::span<Byte const> data) -> Result<void>
493492
OUTCOME_TRY(Send(data));
494493
OUTCOME_TRY(Send(Span(checksum)));
495494

495+
hal::FlushReceiveBuffer(&uart);
496496
OUTCOME_TRY(auto answer, Receive<Byte>());
497497
switch(answer)
498498
{
@@ -628,7 +628,7 @@ auto Retry(auto (*communicationFunction)()->Result<T>, int nTries) -> Result<T>
628628
// No ACK is sent here. The caller is responsible for that.
629629
return result;
630630
}
631-
FlushUartReceiveBuffer();
631+
hal::FlushReceiveBuffer(&uart);
632632
OUTCOME_TRY(SendCommand(cepNack));
633633
iTries++;
634634
if(iTries >= nTries)
@@ -638,22 +638,5 @@ auto Retry(auto (*communicationFunction)()->Result<T>, int nTries) -> Result<T>
638638
}
639639
}
640640
}
641-
642-
643-
//! @brief Flush the EDU UART read buffer.
644-
//!
645-
//! This can be used to clear all buffer data after an error to request a resend.
646-
auto FlushUartReceiveBuffer() -> void
647-
{
648-
auto garbageBuffer = std::array<Byte, 32>{}; // NOLINT(*magic-numbers)
649-
while(true)
650-
{
651-
auto readFromResult = hal::ReadFrom(&uart, Span(&garbageBuffer), flushReceiveBufferTimeout);
652-
if(readFromResult.has_error())
653-
{
654-
break;
655-
}
656-
}
657-
}
658641
}
659642
}

Sts1CobcSw/Hal/Uart.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ auto Initialize(RODOS::HAL_UART * uart, std::uint32_t baudRate) -> void
1313
// report. Also, the UART buffer size is capped at RODOS::UART_BUF_SIZE.
1414
uart->config(RODOS::UART_PARAMETER_ENABLE_DMA, RODOS::UART_BUF_SIZE);
1515
}
16+
17+
18+
auto FlushReceiveBuffer(RODOS::HAL_UART * uart) -> void
19+
{
20+
while(uart->isDataReady())
21+
{
22+
std::uint8_t dummy; // NOLINT(*init-variables)
23+
uart->read(&dummy, 1);
24+
}
25+
}
1626
}

Sts1CobcSw/Hal/Uart.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ auto ReadFrom(RODOS::HAL_UART * uart, std::span<T, extent> data) -> void;
3030
template<typename T, std::size_t extent>
3131
[[nodiscard]] auto ReadFrom(RODOS::HAL_UART * uart, std::span<T, extent> data, Duration timeout)
3232
-> Result<void>;
33+
34+
auto FlushReceiveBuffer(RODOS::HAL_UART * uart) -> void;
3335
}
3436

3537

0 commit comments

Comments
 (0)