diff --git a/srsue/hdr/phy/sync.h b/srsue/hdr/phy/sync.h index a171056ae..1ad5a19ae 100644 --- a/srsue/hdr/phy/sync.h +++ b/srsue/hdr/phy/sync.h @@ -167,6 +167,8 @@ private: bool running = false; bool is_overflow = false; + bool forced_rx_time_init = true; // Rx time sync after first receive from radio + // Objects for internal use search search_p; sfn_sync sfn_p; @@ -340,7 +342,6 @@ private: bool started = false; uint32_t tti = 0; srslte_timestamp_t tti_ts = {}; - srslte_timestamp_t radio_ts = {}; std::array mib = {}; uint32_t nof_workers = 0; @@ -352,6 +353,9 @@ private: float dl_freq = -1; float ul_freq = -1; + const static int MIN_TTI_JUMP = 1; // Time gap reported to stack after receiving subframe + const static int MAX_TTI_JUMP = 1000; // Maximum time gap tolerance in RF stream metadata + const uint8_t SYNC_CC_IDX = 0; ///< From the sync POV, the CC idx is always the first }; diff --git a/srsue/src/phy/sync.cc b/srsue/src/phy/sync.cc index 26b951eb6..ade2ae2d6 100644 --- a/srsue/src/phy/sync.cc +++ b/srsue/src/phy/sync.cc @@ -825,23 +825,40 @@ int sync::radio_recv_fnc(srslte::rf_buffer_t& data, uint32_t nsamples, srslte_ti // Receive if (radio_h->rx_now(data, nsamples, rx_time)) { - // Detect Radio Timestamp reset - if (srslte_timestamp_compare(rx_time, &radio_ts) < 0) { - srslte_timestamp_init(&radio_ts, 0, 0.0); + // check timestamp reset + if (forced_rx_time_init || srslte_timestamp_iszero(&tti_ts) || srslte_timestamp_compare(rx_time, &tti_ts) < 0) { + if (srslte_timestamp_compare(rx_time, &tti_ts) < 0) { + log_h->warning("SYNC: radio time seems to be going backwards (rx_time=%f, tti_ts=%f)\n", + srslte_timestamp_real(rx_time), + srslte_timestamp_real(&tti_ts)); + // time-stamp will be set to rx time below and run_tti() will be called with MIN_TTI_JUMP + } + + // init tti_ts with last rx time + log_h->debug("SYNC: Setting initial TTI time to %f\n", srslte_timestamp_real(rx_time)); + srslte_timestamp_copy(&tti_ts, rx_time); + forced_rx_time_init = false; } - srslte_timestamp_copy(&radio_ts, rx_time); // Advance stack in time - if (srslte_timestamp_compare(rx_time, &tti_ts) > 0) { - uint32_t tti_jump = ceil((srslte_timestamp_real(rx_time) - srslte_timestamp_real(&tti_ts)) / 1.0e-3); + if (srslte_timestamp_compare(rx_time, &tti_ts) >= 0) { + srslte_timestamp_t temp = {}; + srslte_timestamp_copy(&temp, rx_time); + srslte_timestamp_sub(&temp, tti_ts.full_secs, tti_ts.frac_secs); + int32_t tti_jump = static_cast(srslte_timestamp_uint64(&temp, 1e3)); + tti_jump = SRSLTE_MAX(tti_jump, MIN_TTI_JUMP); + if (tti_jump > MAX_TTI_JUMP) { + log_h->warning("SYNC: TTI jump of %d limited to %d\n", tti_jump, MAX_TTI_JUMP); + tti_jump = SRSLTE_MIN(tti_jump, MAX_TTI_JUMP); + } // Run stack stack->run_tti(tti, tti_jump); - - // Increase by the number of tti jumps detected - srslte_timestamp_add(&tti_ts, 0, tti_jump * 1.0e-3f); } + // update timestamp + srslte_timestamp_copy(&tti_ts, rx_time); + if (channel_emulator && rx_time) { channel_emulator->set_srate((uint32_t)current_srate); channel_emulator->run(data.to_cf_t(), data.to_cf_t(), nsamples, *rx_time);