From 5679e4a3b32beeadd220169aea3c3c1d59e4bee8 Mon Sep 17 00:00:00 2001 From: Florian Kaltenberger Date: Tue, 22 Mar 2022 11:10:07 +0100 Subject: [PATCH] making SFN dependent on GPS time (when available) --- executables/nr-ru.c | 9 ++++++++- targets/ARCH/USRP/USERSPACE/LIB/usrp_lib.cpp | 12 ++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/executables/nr-ru.c b/executables/nr-ru.c index 0240de87bc..394118d5a9 100644 --- a/executables/nr-ru.c +++ b/executables/nr-ru.c @@ -604,6 +604,7 @@ void *emulatedRF_thread(void *param) { void rx_rf(RU_t *ru,int *frame,int *slot) { RU_proc_t *proc = &ru->proc; NR_DL_FRAME_PARMS *fp = ru->nr_frame_parms; + openair0_config_t *cfg = &ru->openair0_cfg; void *rxp[ru->nb_rx]; unsigned int rxs; int i; @@ -653,7 +654,13 @@ void rx_rf(RU_t *ru,int *frame,int *slot) { } } - proc->frame_rx = (proc->timestamp_rx / (fp->samples_per_subframe*10))&1023; + //compute system frame number (SFN) according to O-RAN-WG4-CUS.0-v02.00 (using alpha=beta=0) + // this assumes that the USRP has been synchronized to the GPS time + // OAI uses timestamps in sample time stored in int64_t, but it will fit in double precision for many years to come. + double gps_sec = ((double) ts)/cfg->sample_rate; + proc->frame_rx = ((int64_t) (gps_sec/0.01)) & 1023; + + //proc->frame_rx = (proc->timestamp_rx / (fp->samples_per_subframe*10))&1023; proc->tti_rx = fp->get_slot_from_timestamp(proc->timestamp_rx,fp); // synchronize first reception to frame 0 subframe 0 LOG_D(PHY,"RU %d/%d TS %llu , frame %d, slot %d.%d / %d\n", diff --git a/targets/ARCH/USRP/USERSPACE/LIB/usrp_lib.cpp b/targets/ARCH/USRP/USERSPACE/LIB/usrp_lib.cpp index fdc1f4289e..2c880ed919 100644 --- a/targets/ARCH/USRP/USERSPACE/LIB/usrp_lib.cpp +++ b/targets/ARCH/USRP/USERSPACE/LIB/usrp_lib.cpp @@ -203,8 +203,8 @@ static int sync_to_gps(openair0_device *device) { //Set to GPS time uhd::time_spec_t gps_time = uhd::time_spec_t(time_t(s->usrp->get_mboard_sensor("gps_time", mboard).to_int())); - //s->usrp->set_time_next_pps(gps_time+1.0, mboard); - s->usrp->set_time_next_pps(uhd::time_spec_t(0.0)); + s->usrp->set_time_next_pps(gps_time+1.0, mboard); + //s->usrp->set_time_next_pps(uhd::time_spec_t(0.0)); //Wait for it to apply //The wait is 2 seconds because N-Series has a known issue where //the time at the last PPS does not properly update at the PPS edge @@ -215,10 +215,10 @@ static int sync_to_gps(openair0_device *device) { uhd::time_spec_t time_last_pps = s->usrp->get_time_last_pps(mboard); std::cout << "USRP time: " << (boost::format("%0.9f") % time_last_pps.get_real_secs()) << std::endl; std::cout << "GPSDO time: " << (boost::format("%0.9f") % gps_time.get_real_secs()) << std::endl; - //if (gps_time.get_real_secs() == time_last_pps.get_real_secs()) - // std::cout << std::endl << "SUCCESS: USRP time synchronized to GPS time" << std::endl << std::endl; - //else - // std::cerr << std::endl << "ERROR: Failed to synchronize USRP time to GPS time" << std::endl << std::endl; + if (gps_time.get_real_secs() == time_last_pps.get_real_secs()) + std::cout << std::endl << "SUCCESS: USRP time synchronized to GPS time" << std::endl << std::endl; + else + std::cerr << std::endl << "ERROR: Failed to synchronize USRP time to GPS time" << std::endl << std::endl; } if (num_gps_locked == num_mboards and num_mboards > 1) {