From e433f26c2d99d5c32f0b82a98b52796ab1d1e4be Mon Sep 17 00:00:00 2001 From: jvn314 Date: Thu, 22 Oct 2026 01:17:31 -0500 Subject: [PATCH] demodmeshtastic: carry whole-chip STO wrap into FIFO advance After adding the SFO drift (4.25 * sfo_hat) to the fractional STO, the value was wrapped back into [-0.5, 0.5] but the removed whole chip was discarded, misaligning subsequent symbols by m_osFactor samples. This mostly bites at high SF with clock drift, where the drift term can reach ~0.3+ chips. - Fold the wrapped chip(s) into the returned FIFO advance (-chips * os). - Wrap via loRaRound() (half away from zero), so |sto_frac| >= 0.5 now wraps; previously exactly 0.5 did not. Equivalent timing either way. - Reset non-finite STO before rounding (float->int cast of NaN/inf is UB). - Log when the FIFO advance is clamped to 1. --- .../demodmeshtastic/meshtasticdemodsink.cpp | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp b/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp index f2ae65498..e8ba15013 100644 --- a/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp +++ b/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp @@ -1167,14 +1167,42 @@ int MeshtasticDemodSink::processLoRaFrameSyncStep() m_headerFeedbackWaitSteps = 0U; m_demodActive = true; m_loRaSTOFrac += m_loRaSFOHat * 4.25f; - if (std::abs(m_loRaSTOFrac) > 0.5f) { - m_loRaSTOFrac += (m_loRaSTOFrac > 0.0f) ? -1.0f : 1.0f; + + // Guard against NaN/inf from a degenerate STO/SFO estimate: loRaRound() + // casts float to int, which is undefined behaviour for non-finite + // values (this also protects the stoQuant computation below). + if (!std::isfinite(m_loRaSTOFrac)) + { + qWarning("MeshtasticDemodSink::processLoRaFrameSyncStep: non-finite STO (%f), resetting", m_loRaSTOFrac); + m_loRaSTOFrac = 0.0f; } + + // Fold any whole-chip wrap of the fractional STO into the FIFO advance + // instead of dropping it. One chip = m_osFactor samples. A positive + // wrap means consume fewer samples (and vice versa), the same sign + // convention as the SFO compensation step. loRaRound() rounds half away + // from zero, so |sto_frac| >= 0.5 wraps, consistent with stoQuant below. + const int stoChips = loRaRound(m_loRaSTOFrac); + m_loRaSTOFrac -= static_cast(stoChips); + const int stoCarry = -stoChips * static_cast(m_osFactor); + const float stoQuant = static_cast(loRaRound(m_loRaSTOFrac * static_cast(m_osFactor))); m_loRaSFOCum = ((m_loRaSTOFrac * static_cast(m_osFactor)) - stoQuant) / static_cast(m_osFactor); m_loRaState = LoRaStateSFOCompensation; m_loRaSyncState = LoRaSyncNetId1; - return std::max(1, static_cast(m_loRaSymbolSpan / 4U + static_cast(m_osFactor) * m_loRaCFOInt)); + + const int advance = static_cast(m_loRaSymbolSpan / 4U) + + static_cast(m_osFactor) * m_loRaCFOInt + + stoCarry; + + if (advance < 1) + { + // The CFO/STO correction is partly lost when clamped; make it visible. + qDebug("MeshtasticDemodSink::processLoRaFrameSyncStep: FIFO advance %d clamped to 1 (cfoInt=%d stoCarry=%d)", + advance, static_cast(m_loRaCFOInt), stoCarry); + } + + return std::max(1, advance); } return static_cast(m_loRaSymbolSpan);