2828
2929#include < rodos_no_using_namespace.h>
3030
31+ #include < algorithm>
3132#include < array>
3233#include < bit>
3334#include < compare>
@@ -345,7 +346,8 @@ auto ExecuteWithRecovery(Args... args)
345346 return result.value ();
346347 }
347348 }
348- // Reset COBC
349+ // If all attempts failed, reset the whole COBC
350+ DEBUG_PRINT (" RF error recovery failed, resetting in %lld s\n " , errorHandlingCobcResetDelay / s);
349351 SuspendFor (errorHandlingCobcResetDelay);
350352 RODOS::hwResetAndReboot ();
351353 __builtin_unreachable (); // Tell the compiler that hwResetAndReboot() never returns
@@ -429,6 +431,7 @@ auto DoSendAndContinue(std::span<Byte const> data) -> Result<void>
429431 if (not isInTxMode)
430432 {
431433 OUTCOME_TRY (ResetFifos ());
434+ OUTCOME_TRY (ReadAndClearInterruptStatus ());
432435 DisableRfLatchupProtection ();
433436 }
434437 auto dataIndex = 0U ;
@@ -455,11 +458,21 @@ auto DoSendAndContinue(std::span<Byte const> data) -> Result<void>
455458 }
456459 return outcome_v2::success ();
457460 }();
458- OUTCOME_TRY (SetPacketHandlerInterrupts (noInterrupts));
461+ // TODO: Refactor
462+ auto setInterruptsResult = SetPacketHandlerInterrupts (noInterrupts);
463+ auto clearInterruptsResult = ReadAndClearInterruptStatus ();
459464 if (result.has_error ())
460465 {
461466 return result;
462467 }
468+ if (setInterruptsResult.has_error ())
469+ {
470+ return setInterruptsResult.error ();
471+ }
472+ if (clearInterruptsResult.has_error ())
473+ {
474+ return clearInterruptsResult.error ();
475+ }
463476 auto encodedData = convolutionalCoder.Encode (data.subspan (dataIndex), /* flush=*/ true );
464477 OUTCOME_TRY (WriteToFifo (encodedData));
465478 if (not isInTxMode)
@@ -510,6 +523,7 @@ auto DoReceive(std::span<Byte> data, Duration timeout) -> Result<std::size_t>
510523 currentDataRate = rxDataRateConfig.dataRate ;
511524 }
512525 OUTCOME_TRY (ResetFifos ());
526+ OUTCOME_TRY (ReadAndClearInterruptStatus ());
513527 OUTCOME_TRY (SetPacketHandlerInterrupts (rxFifoAlmostFullInterrupt));
514528 OUTCOME_TRY (ReadAndClearInterruptStatus ());
515529 DisableRfLatchupProtection ();
@@ -528,6 +542,17 @@ auto DoReceive(std::span<Byte> data, Duration timeout) -> Result<std::size_t>
528542 OUTCOME_TRY (ReadAndClearInterruptStatus ());
529543 ReadFromFifo (data.subspan (dataIndex, rxFifoThreshold));
530544 dataIndex += rxFifoThreshold;
545+ if (dataIndex == rxFifoThreshold)
546+ {
547+ if (std::ranges::all_of (data.subspan (0 , dataIndex),
548+ [](Byte byte) { return byte == 0xFF_b; })) // NOLINT
549+ {
550+ DEBUG_PRINT (" Received bytes are all 0xFF\n " );
551+ Reset ();
552+ (void )DoInitialize ();
553+ return ErrorCode::receivedInvalidData;
554+ }
555+ }
531556 }
532557 auto remainingData = data.subspan (dataIndex);
533558 OUTCOME_TRY (SetRxFifoThreshold (static_cast <Byte>(remainingData.size ())));
@@ -545,14 +570,27 @@ auto DoReceive(std::span<Byte> data, Duration timeout) -> Result<std::size_t>
545570 dataIndex += remainingData.size ();
546571 return dataIndex;
547572 }();
573+ // TODO: Refactor
548574 auto setInterruptsResult = SetPacketHandlerInterrupts (noInterrupts);
549- OUTCOME_TRY (DoEnterStandbyMode ());
575+ auto clearInterruptsResult = ReadAndClearInterruptStatus ();
576+ auto enterStandbyResult = DoEnterStandbyMode ();
577+ if (result.has_error ())
578+ {
579+ return result.error ();
580+ }
550581 if (setInterruptsResult.has_error ())
551582 {
552583 return setInterruptsResult.error ();
553584 }
554- OUTCOME_TRY (ReadAndClearInterruptStatus ());
555- return result;
585+ if (clearInterruptsResult.has_error ())
586+ {
587+ return clearInterruptsResult.error ();
588+ }
589+ if (enterStandbyResult.has_error ())
590+ {
591+ return enterStandbyResult.error ();
592+ }
593+ return result.value ();
556594}
557595
558596
0 commit comments