From 95a876738382d0c03f062d995bc81603b17862e2 Mon Sep 17 00:00:00 2001 From: Raghavendra Dinavahi Date: Wed, 3 Jul 2024 20:40:28 +0200 Subject: [PATCH] Automatic update of USRP RX gain during initial search using --agc option - if search fails, keeps increasing the rx gain (set by --ue-rxgain) by 3 db until USRP max rx gain - if initial sync is success, sets the rxgain to the value of (TARGET_RX_POWER (50) - pbch digital power in dB/RE) --- executables/nr-softmodem-common.h | 2 +- executables/nr-ue.c | 43 ++++++++++++++++--- executables/nr-uesoftmodem.h | 2 + openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h | 14 ++++-- .../PHY/NR_UE_TRANSPORT/nr_initial_sync.c | 6 +++ .../PHY/NR_UE_TRANSPORT/nr_initial_sync_sl.c | 2 +- openair1/PHY/defs_nr_UE.h | 4 ++ openair1/PHY/impl_defs_top.h | 3 ++ .../SCHED_NR_UE/phy_procedures_nr_ue_sl.c | 2 +- radio/USRP/usrp_lib.cpp | 31 +++++++------ 10 files changed, 83 insertions(+), 26 deletions(-) diff --git a/executables/nr-softmodem-common.h b/executables/nr-softmodem-common.h index e84ca015cc..1d90b259df 100644 --- a/executables/nr-softmodem-common.h +++ b/executables/nr-softmodem-common.h @@ -106,7 +106,7 @@ #define CONFIG_HLP_SNR "Set average SNR in dB (for --siml1 option)\n" #define CONFIG_HLP_NOS1 "Disable s1 interface\n" #define CONFIG_HLP_NOKRNMOD "(noS1 only): Use tun instead of namesh module \n" - +#define CONFIG_HLP_AGC "Rx Gain control used for UE" /*--------------------------------------------------------------------------------------------------------------------------------*/ /* command line parameters for LOG utility */ /* optname helpstr paramflags XXXptr defXXXval type numelt */ diff --git a/executables/nr-ue.c b/executables/nr-ue.c index 447c99182b..26dbbb2d85 100644 --- a/executables/nr-ue.c +++ b/executables/nr-ue.c @@ -351,10 +351,31 @@ typedef struct { int rx_offset; } syncData_t; +static int nr_ue_adjust_rx_gain(PHY_VARS_NR_UE *UE, openair0_config_t *cfg0, int gain_change) +{ + // Increase the RX gain by the value determined by adjust_rxgain + cfg0->rx_gain[0] += gain_change; + + // Set new RX gain. + int ret_gain = UE->rfdevice.trx_set_gains_func(&UE->rfdevice, cfg0); + // APPLY RX gain again if crossed the MAX RX gain threshold + if (ret_gain < 0) { + gain_change += ret_gain; + cfg0->rx_gain[0] += ret_gain; + ret_gain = UE->rfdevice.trx_set_gains_func(&UE->rfdevice, cfg0); + } + + int applied_rxgain = cfg0->rx_gain[0] - cfg0->rx_gain_offset[0]; + LOG_I(PHY, "Rxgain adjusted by %d dB, RX gain: %d dB \n", gain_change, applied_rxgain); + + return gain_change; +} + static void UE_synch(void *arg) { syncData_t *syncD = (syncData_t *)arg; PHY_VARS_NR_UE *UE = syncD->UE; UE->is_synchronized = 0; + openair0_config_t *cfg0 = &openair0_cfg[UE->rf_map.card]; if (UE->target_Nid_cell != -1) { LOG_W(NR_PHY, "Starting re-sync detection for target Nid_cell %i\n", UE->target_Nid_cell); @@ -386,18 +407,30 @@ static void UE_synch(void *arg) { // rerun with new cell parameters and frequency-offset // todo: the freq_offset computed on DL shall be scaled before being applied to UL - nr_rf_card_config_freq(&openair0_cfg[UE->rf_map.card], ul_carrier, dl_carrier, freq_offset); + nr_rf_card_config_freq(cfg0, ul_carrier, dl_carrier, freq_offset); + + if (get_nrUE_params()->agc) { + nr_ue_adjust_rx_gain(UE, cfg0, UE->adjust_rxgain); + } LOG_I(PHY, "Got synch: hw_slot_offset %d, carrier off %d Hz, rxgain %f (DL %f Hz, UL %f Hz)\n", hw_slot_offset, freq_offset, - openair0_cfg[UE->rf_map.card].rx_gain[0], - openair0_cfg[UE->rf_map.card].rx_freq[0], - openair0_cfg[UE->rf_map.card].tx_freq[0]); + cfg0->rx_gain[0] - cfg0->rx_gain_offset[0], + cfg0->rx_freq[0], + cfg0->tx_freq[0]); - UE->rfdevice.trx_set_freq_func(&UE->rfdevice, &openair0_cfg[0]); + UE->rfdevice.trx_set_freq_func(&UE->rfdevice, cfg0); UE->is_synchronized = 1; + } else { + int gain_change = 0; + if (get_nrUE_params()->agc) + gain_change = nr_ue_adjust_rx_gain(UE, cfg0, INCREASE_IN_RXGAIN); + if (gain_change) + LOG_I(PHY, "synch retry: Rx gain increased \n"); + else + LOG_E(PHY, "synch Failed: \n"); } } diff --git a/executables/nr-uesoftmodem.h b/executables/nr-uesoftmodem.h index 8e0650b4e4..3793e6b3a0 100644 --- a/executables/nr-uesoftmodem.h +++ b/executables/nr-uesoftmodem.h @@ -68,6 +68,7 @@ {"num-ues", NULL, 0, .iptr=&(NB_UE_INST), .defuintval=1, TYPE_INT, 0}, \ {"ntn-koffset", CONFIG_HLP_NTN_KOFFSET, 0, .uptr=&(nrUE_params.ntn_koffset), .defuintval=0, TYPE_UINT, 0}, \ {"ntn-ta-common", CONFIG_HLP_NTN_TA_COMMON, 0, .dblptr=&(nrUE_params.ntn_ta_common), .defdblval=0.0, TYPE_DOUBLE, 0}, \ + {"agc", CONFIG_HLP_AGC, PARAMFLAG_BOOL, .iptr=&(nrUE_params.agc), .defintval=0, TYPE_INT, 0}, \ } // clang-format on @@ -90,6 +91,7 @@ typedef struct { int ldpc_offload_flag; unsigned int ntn_koffset; double ntn_ta_common; + int agc; } nrUE_params_t; extern uint64_t get_nrUE_optmask(void); extern uint64_t set_nrUE_optmask(uint64_t bitmask); diff --git a/openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h b/openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h index fc2e2bba62..390775fee7 100644 --- a/openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h +++ b/openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h @@ -115,6 +115,12 @@ void nr_ue_measurements(PHY_VARS_NR_UE *ue, uint32_t pdsch_est_size, int32_t dl_ch_estimates[][pdsch_est_size]); +int nr_ue_calculate_ssb_rsrp(const NR_DL_FRAME_PARMS *fp, + const UE_nr_rxtx_proc_t *proc, + const c16_t rxdataF[][fp->samples_per_slot_wCP], + int symbol_offset, + int ssb_start_subcarrier); + void nr_ue_ssb_rsrp_measurements(PHY_VARS_NR_UE *ue, uint8_t gNB_index, const UE_nr_rxtx_proc_t *proc, @@ -146,9 +152,9 @@ void nr_pdsch_ptrs_processing(PHY_VARS_NR_UE *ue, float_t get_nr_RSRP(module_id_t Mod_id,uint8_t CC_id,uint8_t gNB_index); -void nr_sl_psbch_rsrp_measurements(sl_nr_ue_phy_params_t *sl_phy_params, - NR_DL_FRAME_PARMS *fp, - c16_t rxdataF[][fp->samples_per_slot_wCP], - bool use_SSS); +int nr_sl_psbch_rsrp_measurements(sl_nr_ue_phy_params_t *sl_phy_params, + NR_DL_FRAME_PARMS *fp, + c16_t rxdataF[][fp->samples_per_slot_wCP], + bool use_SSS); /** @}*/ #endif diff --git a/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c b/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c index 4788c9ed9d..341459832f 100644 --- a/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c +++ b/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c @@ -291,6 +291,11 @@ void nr_scan_ssb(void *arg) &ssbInfo->pbchResult, nr_gold_pbch_ref, rxdataF); // start pbch detection at first symbol after pss + if (ssbInfo->syncRes.cell_detected) { + int rsrp_db_per_re = nr_ue_calculate_ssb_rsrp(ssbInfo->fp, ssbInfo->proc, rxdataF, 0, ssbInfo->gscnInfo.ssbFirstSC); + ssbInfo->adjust_rxgain = TARGET_RX_POWER - rsrp_db_per_re; + LOG_I(PHY, "pbch rx ok. rsrp:%d dB/RE, adjust_rxgain:%d dB\n", rsrp_db_per_re, ssbInfo->adjust_rxgain); + } } } } @@ -370,6 +375,7 @@ nr_initial_sync_t nr_initial_sync(UE_nr_rxtx_proc_t *proc, fp->ssb_index = res.ssbIndex; ue->symbol_offset = res.symbolOffset; ue->common_vars.freq_offset = res.freqOffset; + ue->adjust_rxgain = res.adjust_rxgain; } // In initial sync, we indicate PBCH to MAC after the scan is complete. diff --git a/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync_sl.c b/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync_sl.c index 98433ea8b2..1d92bf99aa 100644 --- a/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync_sl.c +++ b/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync_sl.c @@ -512,7 +512,7 @@ nr_initial_sync_t sl_nr_slss_search(PHY_VARS_NR_UE *UE, UE_nr_rxtx_proc_t *proc, sync_params->DFN, sync_params->slot_offset); - nr_sl_psbch_rsrp_measurements(sl_ue, frame_parms, rxdataF, false); + UE->adjust_rxgain = nr_sl_psbch_rsrp_measurements(sl_ue, frame_parms, rxdataF, false); UE->init_sync_frame = sync_params->remaining_frames; result.rx_offset = sync_params->rx_offset; diff --git a/openair1/PHY/defs_nr_UE.h b/openair1/PHY/defs_nr_UE.h index 70b0dc041f..5178bc6320 100644 --- a/openair1/PHY/defs_nr_UE.h +++ b/openair1/PHY/defs_nr_UE.h @@ -600,6 +600,9 @@ typedef struct PHY_VARS_NR_UE_s { notifiedFIFO_t tx_resume_ind_fifo[NR_MAX_SLOTS_PER_FRAME]; + // Gain change required for automation RX gain change + int adjust_rxgain; + // Sidelink parameters sl_nr_sidelink_mode_t sl_mode; sl_nr_ue_phy_params_t SL_UE_PHY_PARAMS; @@ -645,6 +648,7 @@ typedef struct { fapiPbch_t pbchResult; int pssCorrPeakPower; int pssCorrAvgPower; + int adjust_rxgain; } nr_ue_ssb_scan_t; typedef struct nr_phy_data_tx_s { diff --git a/openair1/PHY/impl_defs_top.h b/openair1/PHY/impl_defs_top.h index 618a7e6c58..24f1599873 100644 --- a/openair1/PHY/impl_defs_top.h +++ b/openair1/PHY/impl_defs_top.h @@ -181,6 +181,9 @@ #define TARGET_RX_POWER_MAX 65 // Maximum digital power, such that signal does not saturate (value found by simulation) #define TARGET_RX_POWER_MIN 35 // Minimum digital power, anything below will be discarded (value found by simulation) +// Increase USRP rx gain in steps of 3dB during Initial search +#define INCREASE_IN_RXGAIN 3 + //the min and max gains have to match the calibrated gain table //#define MAX_RF_GAIN 160 //#define MIN_RF_GAIN 96 diff --git a/openair1/SCHED_NR_UE/phy_procedures_nr_ue_sl.c b/openair1/SCHED_NR_UE/phy_procedures_nr_ue_sl.c index b4335c7480..dac44e0f56 100644 --- a/openair1/SCHED_NR_UE/phy_procedures_nr_ue_sl.c +++ b/openair1/SCHED_NR_UE/phy_procedures_nr_ue_sl.c @@ -223,7 +223,7 @@ int psbch_pscch_processing(PHY_VARS_NR_UE *ue, const UE_nr_rxtx_proc_t *proc, nr sym = (sym == 0) ? 5 : sym + 1; } - nr_sl_psbch_rsrp_measurements(sl_phy_params, fp, rxdataF, false); + ue->adjust_rxgain = nr_sl_psbch_rsrp_measurements(sl_phy_params, fp, rxdataF, false); LOG_D(NR_PHY, " ------ Decode SL-MIB: frame.slot %d.%d ------ \n", frame_rx % 1024, nr_slot_rx); diff --git a/radio/USRP/usrp_lib.cpp b/radio/USRP/usrp_lib.cpp index 8cc4a5b2ee..71da4da167 100644 --- a/radio/USRP/usrp_lib.cpp +++ b/radio/USRP/usrp_lib.cpp @@ -869,7 +869,8 @@ int trx_usrp_set_gains(openair0_device *device, if (openair0_cfg[0].rx_gain[0]-openair0_cfg[0].rx_gain_offset[0] > gain_range.stop()) { LOG_E(HW,"RX Gain 0 too high, reduce by %f dB\n", openair0_cfg[0].rx_gain[0]-openair0_cfg[0].rx_gain_offset[0] - gain_range.stop()); - exit(-1); + int gain_diff = gain_range.stop() - (openair0_cfg[0].rx_gain[0] - openair0_cfg[0].rx_gain_offset[0]); + return gain_diff; } s->usrp->set_rx_gain(openair0_cfg[0].rx_gain[0]-openair0_cfg[0].rx_gain_offset[0]); @@ -1424,25 +1425,27 @@ extern "C" { } for(int i=0; i<((int) s->usrp->get_rx_num_channels()); i++) { - if (iusrp->set_rx_rate(openair0_cfg[0].sample_rate,i+choffset); - uhd::tune_request_t rx_tune_req(openair0_cfg[0].rx_freq[i], - openair0_cfg[0].tune_offset); + openair0_config_t *cfg = &openair0_cfg[0]; + if (i < cfg->rx_num_channels) { + s->usrp->set_rx_rate(cfg->sample_rate, i + choffset); + uhd::tune_request_t rx_tune_req(cfg->rx_freq[i], cfg->tune_offset); s->usrp->set_rx_freq(rx_tune_req, i+choffset); - set_rx_gain_offset(&openair0_cfg[0],i,bw_gain_adjust); + set_rx_gain_offset(cfg, i, bw_gain_adjust); ::uhd::gain_range_t gain_range = s->usrp->get_rx_gain_range(i+choffset); // limit to maximum gain - double gain=openair0_cfg[0].rx_gain[i]-openair0_cfg[0].rx_gain_offset[i]; + double gain = cfg->rx_gain[i] - cfg->rx_gain_offset[i]; if ( gain > gain_range.stop()) { - LOG_E(HW,"RX Gain too high, lower by %f dB\n", - gain - gain_range.stop()); - gain=gain_range.stop(); + LOG_E(HW, "RX Gain too high, lower by %f dB\n", gain - gain_range.stop()); + gain = gain_range.stop(); } - s->usrp->set_rx_gain(gain,i+choffset); - LOG_I(HW,"RX Gain %d %f (%f) => %f (max %f)\n",i, - openair0_cfg[0].rx_gain[i],openair0_cfg[0].rx_gain_offset[i], - openair0_cfg[0].rx_gain[i]-openair0_cfg[0].rx_gain_offset[i],gain_range.stop()); + LOG_I(HW, + "RX Gain %d %f (%f) => %f (max %f)\n", + i, + cfg->rx_gain[i], + cfg->rx_gain_offset[i], + cfg->rx_gain[i] - cfg->rx_gain_offset[i], + gain_range.stop()); } }