nr_initial_sync.c 19.3 KB
Newer Older
Hongzhi Wang's avatar
Hongzhi Wang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file PHY/LTE_TRANSPORT/initial_sync.c
* \brief Routines for initial UE synchronization procedure (PSS,SSS,PBCH and frame format detection)
* \author R. Knopp, F. Kaltenberger
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr,kaltenberger@eurecom.fr
* \note
* \warning
*/
#include "PHY/types.h"
#include "PHY/defs_nr_UE.h"
#include "PHY/phy_extern_nr_ue.h"
hongzhi wang's avatar
hongzhi wang committed
35 36 37
#include "PHY/INIT/phy_init.h"
#include "PHY/MODULATION/modulation_UE.h"
#include "nr_transport_proto_ue.h"
38
#include "PHY/NR_UE_ESTIMATION/nr_estimation.h"
39
#include "SCHED_NR_UE/defs.h"
Rakesh's avatar
Rakesh committed
40
#include "common/utils/LOG/vcd_signal_dumper.h"
41
#include "common/utils/nr/nr_common.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
42 43

#include "common_lib.h"
44
#include <math.h>
Hongzhi Wang's avatar
Hongzhi Wang committed
45 46 47

#include "PHY/NR_REFSIG/pss_nr.h"
#include "PHY/NR_REFSIG/sss_nr.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
48
#include "PHY/NR_REFSIG/refsig_defs_ue.h"
49
#include "PHY/NR_TRANSPORT/nr_dci.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
50 51

extern openair0_config_t openair0_cfg[];
hongzhi wang's avatar
hongzhi wang committed
52 53
//static  nfapi_nr_config_request_t config_t;
//static  nfapi_nr_config_request_t* config =&config_t;
54
int cnt=0;
Hongzhi Wang's avatar
Hongzhi Wang committed
55

56
#define DEBUG_INITIAL_SYNCH
Hongzhi Wang's avatar
Hongzhi Wang committed
57

Francesco Mani's avatar
Francesco Mani committed
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
// create a new node of SSB structure
NR_UE_SSB* create_ssb_node(uint8_t  i, uint8_t  h) {

  NR_UE_SSB *new_node = (NR_UE_SSB*)malloc(sizeof(NR_UE_SSB));
  new_node->i_ssb = i;
  new_node->n_hf = h;
  new_node->c_re = 0;
  new_node->c_im = 0;
  new_node->metric = 0;
  new_node->next_ssb = NULL;

  return new_node;
}


// insertion of the structure in the ordered list (highest metric first)
NR_UE_SSB* insert_into_list(NR_UE_SSB *head, NR_UE_SSB *node) {

  if (node->metric > head->metric) {
    node->next_ssb = head;
    head = node;
    return head;
  }

  NR_UE_SSB *current = head;
  while (current->next_ssb !=NULL) {
    NR_UE_SSB *temp=current->next_ssb;
86
    if(node->metric > temp->metric) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      node->next_ssb = temp;
      current->next_ssb = node;
      return head;
    }
    else
      current = temp;
  }
  current->next_ssb = node;

  return head;
}


void free_list(NR_UE_SSB *node) {
  if (node->next_ssb != NULL)
    free_list(node->next_ssb);
  free(node);
}


cig's avatar
cig committed
107
int nr_pbch_detection(UE_nr_rxtx_proc_t * proc, PHY_VARS_NR_UE *ue, int pbch_initial_symbol)
Hongzhi Wang's avatar
Hongzhi Wang committed
108 109
{
  NR_DL_FRAME_PARMS *frame_parms=&ue->frame_parms;
110
  int ret =-1;
111

112 113
  NR_UE_SSB *best_ssb = NULL;
  NR_UE_SSB *current_ssb;
Hongzhi Wang's avatar
Hongzhi Wang committed
114 115 116 117 118 119

#ifdef DEBUG_INITIAL_SYNCH
  LOG_I(PHY,"[UE%d] Initial sync: starting PBCH detection (rx_offset %d)\n",ue->Mod_id,
        ue->rx_offset);
#endif

Francesco Mani's avatar
Francesco Mani committed
120 121
  uint8_t  N_L = (frame_parms->Lmax == 4)? 4:8;
  uint8_t  N_hf = (frame_parms->Lmax == 4)? 2:1;
hongzhi wang's avatar
hongzhi wang committed
122

Francesco Mani's avatar
Francesco Mani committed
123 124 125
  // loops over possible pbch dmrs cases to retrive best estimated i_ssb (and n_hf for Lmax=4) for multiple ssb detection
  for (int hf = 0; hf < N_hf; hf++) {
    for (int l = 0; l < N_L ; l++) {
126

127 128
      // initialization of structure
      current_ssb = create_ssb_node(l,hf);
129

130 131 132
      start_meas(&ue->dlsch_channel_estimation_stats);
      // computing correlation between received DMRS symbols and transmitted sequence for current i_ssb and n_hf
      for(int i=pbch_initial_symbol; i<pbch_initial_symbol+3;i++)
133
          nr_pbch_dmrs_correlation(ue,proc,0,0,i,i-pbch_initial_symbol,current_ssb);
134 135
      stop_meas(&ue->dlsch_channel_estimation_stats);
      
Francesco Mani's avatar
Francesco Mani committed
136
      current_ssb->metric = current_ssb->c_re*current_ssb->c_re + current_ssb->c_im*current_ssb->c_im;
137
      
138 139 140 141 142
      // generate a list of SSB structures
      if (best_ssb == NULL)
        best_ssb = current_ssb;
      else
        best_ssb = insert_into_list(best_ssb,current_ssb);
143

Francesco Mani's avatar
Francesco Mani committed
144
    }
145
  }
Raymond Knopp's avatar
Raymond Knopp committed
146

147 148 149 150
  NR_UE_SSB *temp_ptr=best_ssb;
  while (ret!=0 && temp_ptr != NULL) {

    start_meas(&ue->dlsch_channel_estimation_stats);
151
  // computing channel estimation for selected best ssb
152
    for(int i=pbch_initial_symbol; i<pbch_initial_symbol+3;i++)
153
      nr_pbch_channel_estimation(ue,proc,0,0,i,i-pbch_initial_symbol,temp_ptr->i_ssb,temp_ptr->n_hf);
154
    stop_meas(&ue->dlsch_channel_estimation_stats);
155

156
    ret = nr_rx_pbch(ue,
157 158 159 160 161 162
                     proc,
                     ue->pbch_vars[0],
                     frame_parms,
                     0,
                     temp_ptr->i_ssb,
                     SISO);
163

164
    temp_ptr=temp_ptr->next_ssb;
165
  }
166

167
  free_list(best_ssb);
168

Raymond Knopp's avatar
Raymond Knopp committed
169
  
170
  if (ret==0) {
Raymond Knopp's avatar
Raymond Knopp committed
171
    
172
    frame_parms->nb_antenna_ports_gNB = 1; //pbch_tx_ant;
Raymond Knopp's avatar
Raymond Knopp committed
173
    
Hongzhi Wang's avatar
Hongzhi Wang committed
174 175 176 177 178 179 180 181 182 183 184
    // set initial transmission mode to 1 or 2 depending on number of detected TX antennas
    //frame_parms->mode1_flag = (pbch_tx_ant==1);
    // openair_daq_vars.dlsch_transmission_mode = (pbch_tx_ant>1) ? 2 : 1;


    // flip byte endian on 24-bits for MIB
    //    dummy = ue->pbch_vars[0]->decoded_output[0];
    //    ue->pbch_vars[0]->decoded_output[0] = ue->pbch_vars[0]->decoded_output[2];
    //    ue->pbch_vars[0]->decoded_output[2] = dummy;

#ifdef DEBUG_INITIAL_SYNCH
185
    LOG_I(PHY,"[UE%d] Initial sync: pbch decoded sucessfully\n",ue->Mod_id);
Hongzhi Wang's avatar
Hongzhi Wang committed
186 187 188 189 190 191 192 193 194 195 196
#endif
    return(0);
  } else {
    return(-1);
  }

}

char duplex_string[2][4] = {"FDD","TDD"};
char prefix_string[2][9] = {"NORMAL","EXTENDED"};

197 198
int nr_initial_sync(UE_nr_rxtx_proc_t *proc,
                    PHY_VARS_NR_UE *ue,
199
                    int n_frames, int sa)
Hongzhi Wang's avatar
Hongzhi Wang committed
200 201
{

202
  int32_t sync_pos, sync_pos_frame; // k_ssb, N_ssb_crb, sync_pos2,
Raymond Knopp's avatar
Raymond Knopp committed
203 204
  int32_t metric_tdd_ncp=0;
  uint8_t phase_tdd_ncp;
205
  double im, re;
206
  int is;
Hongzhi Wang's avatar
Hongzhi Wang committed
207

Raymond Knopp's avatar
Raymond Knopp committed
208
  NR_DL_FRAME_PARMS *fp = &ue->frame_parms;
Hongzhi Wang's avatar
Hongzhi Wang committed
209
  int ret=-1;
hongzhi wang's avatar
hongzhi wang committed
210
  int rx_power=0; //aarx,
211
  
Rakesh's avatar
Rakesh committed
212
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_NR_INITIAL_UE_SYNC, VCD_FUNCTION_IN);
213

214

Raymond Knopp's avatar
Raymond Knopp committed
215
  LOG_D(PHY,"nr_initial sync ue RB_DL %d\n", fp->N_RB_DL);
Hongzhi Wang's avatar
Hongzhi Wang committed
216 217 218 219 220 221 222 223 224 225 226 227 228

  /*   Initial synchronisation
   *
  *                                 1 radio frame = 10 ms
  *     <--------------------------------------------------------------------------->
  *     -----------------------------------------------------------------------------
  *     |                                 Received UE data buffer                    |
  *     ----------------------------------------------------------------------------
  *                     --------------------------
  *     <-------------->| pss | pbch | sss | pbch |
  *                     --------------------------
  *          sync_pos            SS/PBCH block
  */
Raymond Knopp's avatar
Raymond Knopp committed
229

230
  cnt++;
231
  if (1){ // (cnt>100)
232
   cnt =0;
233

234
   // initial sync performed on two successive frames, if pbch passes on first frame, no need to process second frame 
235 236
   // only one frame is used for symulation tools
   for(is=0; is<n_frames;is++) {
Hongzhi Wang's avatar
Hongzhi Wang committed
237

238 239
    /* process pss search on received buffer */
    sync_pos = pss_synchro_nr(ue, is, NO_RATE_CHANGE);
240

241 242 243 244
    if (sync_pos >= fp->nb_prefix_samples)
      ue->ssb_offset = sync_pos - fp->nb_prefix_samples;
    else
      ue->ssb_offset = sync_pos + (fp->samples_per_subframe * 10) - fp->nb_prefix_samples;
245

Hongzhi Wang's avatar
Hongzhi Wang committed
246
#ifdef DEBUG_INITIAL_SYNCH
247 248
    LOG_I(PHY,"[UE%d] Initial sync : Estimated PSS position %d, Nid2 %d\n", ue->Mod_id, sync_pos,ue->common_vars.eNb_id);
    LOG_I(PHY,"sync_pos %d ssb_offset %d \n",sync_pos,ue->ssb_offset);
Hongzhi Wang's avatar
Hongzhi Wang committed
249 250
#endif

251 252
    // digital compensation of FFO for SSB symbols
    if (ue->UE_fo_compensation){  
253 254
	double s_time = 1/(1.0e3*fp->samples_per_subframe);  // sampling time
	double off_angle = -2*M_PI*s_time*(ue->common_vars.freq_offset);  // offset rotation angle compensation per sample
255

256
	int start = is*fp->samples_per_frame+ue->ssb_offset;  // start for offset correction is at ssb_offset (pss time position)
257
  	int end = start + 4*(fp->ofdm_symbol_size + fp->nb_prefix_samples);  // loop over samples in 4 symbols (ssb size), including prefix  
258

259 260
	for(int n=start; n<end; n++){  	
	  for (int ar=0; ar<fp->nb_antennas_rx; ar++) {
261 262 263 264
		re = ((double)(((short *)ue->common_vars.rxdata[ar]))[2*n]);
		im = ((double)(((short *)ue->common_vars.rxdata[ar]))[2*n+1]);
		((short *)ue->common_vars.rxdata[ar])[2*n] = (short)(round(re*cos(n*off_angle) - im*sin(n*off_angle))); 
		((short *)ue->common_vars.rxdata[ar])[2*n+1] = (short)(round(re*sin(n*off_angle) + im*cos(n*off_angle)));
265
	  }
266
	}
267
    }
268

269 270
    /* check that SSS/PBCH block is continuous inside the received buffer */
    if (sync_pos < (NR_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_subframe - (NB_SYMBOLS_PBCH * fp->ofdm_symbol_size))) {
Hongzhi Wang's avatar
Hongzhi Wang committed
271

Francesco Mani's avatar
Francesco Mani committed
272 273 274 275 276 277
    /* slop_fep function works for lte and takes into account begining of frame with prefix for subframe 0 */
    /* for NR this is not the case but slot_fep is still used for computing FFT of samples */
    /* in order to achieve correct processing for NR prefix samples is forced to 0 and then restored after function call */
    /* symbol number are from beginning of SS/PBCH blocks as below:  */
    /*    Signal            PSS  PBCH  SSS  PBCH                     */
    /*    symbol number      0     1    2    3                       */
278
    /* time samples in buffer rxdata are used as input of FFT -> FFT results are stored in the frequency buffer rxdataF */
Francesco Mani's avatar
Francesco Mani committed
279
    /* rxdataF stores SS/PBCH from beginning of buffers in the same symbol order as in time domain */
280

281
      for(int i=0; i<4;i++)
282
        nr_slot_fep_init_sync(ue,
283 284 285
                              proc,
                              i,
                              0,
286
                              is*fp->samples_per_frame+ue->ssb_offset);
287

Hongzhi Wang's avatar
Hongzhi Wang committed
288
#ifdef DEBUG_INITIAL_SYNCH
289
      LOG_I(PHY,"Calling sss detection (normal CP)\n");
Hongzhi Wang's avatar
Hongzhi Wang committed
290 291
#endif

292
      rx_sss_nr(ue, proc, &metric_tdd_ncp, &phase_tdd_ncp);
Hongzhi Wang's avatar
Hongzhi Wang committed
293

294
      nr_gold_pbch(ue);
cig's avatar
cig committed
295
      ret = nr_pbch_detection(proc, ue, 1);  // start pbch detection at first symbol after pss
296

297 298 299
      if (ret == 0) {
        // sync at symbol ue->symbol_offset
        // computing the offset wrt the beginning of the frame
300 301 302 303 304
        int mu = fp->numerology_index;
        // number of symbols with different prefix length
        // every 7*(1<<mu) symbols there is a different prefix length (38.211 5.3.1)
        int n_symb_prefix0 = (ue->symbol_offset/(7*(1<<mu)))+1;
        sync_pos_frame = n_symb_prefix0*(fp->ofdm_symbol_size + fp->nb_prefix_samples0)+(ue->symbol_offset-n_symb_prefix0)*(fp->ofdm_symbol_size + fp->nb_prefix_samples);
305 306 307
        // for a correct computation of frame number to sync with the one decoded at MIB we need to take into account in which of the n_frames we got sync
        ue->init_sync_frame = n_frames - 1 - is;
        // we also need to take into account the shift by samples_per_frame in case the if is true
308
        if (ue->ssb_offset < sync_pos_frame){
309
          ue->rx_offset = fp->samples_per_frame - sync_pos_frame + ue->ssb_offset;
310
          ue->init_sync_frame += 1;
311
        }
312 313
        else
          ue->rx_offset = ue->ssb_offset - sync_pos_frame;
314
      }   
315

Raymond Knopp's avatar
Raymond Knopp committed
316
    /*
317 318
    int nb_prefix_samples0 = fp->nb_prefix_samples0;
    fp->nb_prefix_samples0 = fp->nb_prefix_samples;
Raymond Knopp's avatar
Raymond Knopp committed
319
	  
320 321
    nr_slot_fep(ue, proc, 0, 0, ue->ssb_offset, 0, NR_PDCCH_EST);
    nr_slot_fep(ue, proc, 1, 0, ue->ssb_offset, 0, NR_PDCCH_EST);
322
    fp->nb_prefix_samples0 = nb_prefix_samples0;	
323

324
    LOG_I(PHY,"[UE  %d] AUTOTEST Cell Sync : frame = %d, rx_offset %d, freq_offset %d \n",
325 326 327 328
              ue->Mod_id,
              ue->proc.proc_rxtx[0].frame_rx,
              ue->rx_offset,
              ue->common_vars.freq_offset );
Raymond Knopp's avatar
Raymond Knopp committed
329
    */
Hongzhi Wang's avatar
Hongzhi Wang committed
330

331

Hongzhi Wang's avatar
Hongzhi Wang committed
332
#ifdef DEBUG_INITIAL_SYNCH
333 334
      LOG_I(PHY,"TDD Normal prefix: CellId %d metric %d, phase %d, pbch %d\n",
            fp->Nid_cell,metric_tdd_ncp,phase_tdd_ncp,ret);
Hongzhi Wang's avatar
Hongzhi Wang committed
335
#endif
336 337 338

      }
      else {
Hongzhi Wang's avatar
Hongzhi Wang committed
339
#ifdef DEBUG_INITIAL_SYNCH
340
       LOG_I(PHY,"TDD Normal prefix: SSS error condition: sync_pos %d\n", sync_pos);
Hongzhi Wang's avatar
Hongzhi Wang committed
341
#endif
342 343 344
      }
      if (ret == 0) break;
    }
345 346
  }
  else {
347
    ret = -1;
348
  }
Hongzhi Wang's avatar
Hongzhi Wang committed
349

350 351
  /* Consider this is a false detection if the offset is > 1000 Hz 
     Not to be used now that offest estimation is in place
Hongzhi Wang's avatar
Hongzhi Wang committed
352 353 354 355
  if( (abs(ue->common_vars.freq_offset) > 150) && (ret == 0) )
  {
	  ret=-1;
	  LOG_E(HW, "Ignore MIB with high freq offset [%d Hz] estimation \n",ue->common_vars.freq_offset);
356
  }*/
Hongzhi Wang's avatar
Hongzhi Wang committed
357 358 359 360

  if (ret==0) {  // PBCH found so indicate sync to higher layers and configure frame parameters

    //#ifdef DEBUG_INITIAL_SYNCH
361

Hongzhi Wang's avatar
Hongzhi Wang committed
362
    LOG_I(PHY, "[UE%d] In synch, rx_offset %d samples\n",ue->Mod_id, ue->rx_offset);
363

Hongzhi Wang's avatar
Hongzhi Wang committed
364 365 366 367 368
    //#endif

    if (ue->UE_scan_carrier == 0) {

    #if UE_AUTOTEST_TRACE
laurent's avatar
laurent committed
369
      LOG_I(PHY,"[UE  %d] AUTOTEST Cell Sync : rx_offset %d, freq_offset %d \n",
Hongzhi Wang's avatar
Hongzhi Wang committed
370 371 372 373 374 375 376 377 378 379 380
              ue->Mod_id,
              ue->rx_offset,
              ue->common_vars.freq_offset );
    #endif

// send sync status to higher layers later when timing offset converge to target timing

      ue->pbch_vars[0]->pdu_errors_conseq=0;

    }

laurent's avatar
laurent committed
381
    LOG_I(PHY, "[UE %d] RRC Measurements => rssi %3.1f dBm (dig %3.1f dB, gain %d), N0 %d dBm,  rsrp %3.1f dBm/RE, rsrq %3.1f dB\n",ue->Mod_id,
Hongzhi Wang's avatar
Hongzhi Wang committed
382 383 384 385 386 387 388 389 390 391
	  10*log10(ue->measurements.rssi)-ue->rx_total_gain_dB,
	  10*log10(ue->measurements.rssi),
	  ue->rx_total_gain_dB,
	  ue->measurements.n0_power_tot_dBm,
	  10*log10(ue->measurements.rsrp[0])-ue->rx_total_gain_dB,
	  (10*log10(ue->measurements.rsrq[0])));

/*    LOG_I(PHY, "[UE %d] Frame %d MIB Information => %s, %s, NidCell %d, N_RB_DL %d, PHICH DURATION %d, PHICH RESOURCE %s, TX_ANT %d\n",
	  ue->Mod_id,
	  ue->proc.proc_rxtx[0].frame_rx,
Raymond Knopp's avatar
Raymond Knopp committed
392 393 394 395 396 397
	  duplex_string[fp->frame_type],
	  prefix_string[fp->Ncp],
	  fp->Nid_cell,
	  fp->N_RB_DL,
	  fp->phich_config_common.phich_duration,
	  phich_string[fp->phich_config_common.phich_resource],
398
	  fp->nb_antenna_ports_gNB);*/
Hongzhi Wang's avatar
Hongzhi Wang committed
399 400

#if defined(OAI_USRP) || defined(EXMIMO) || defined(OAI_BLADERF) || defined(OAI_LMSSDR) || defined(OAI_ADRV9371_ZC706)
laurent's avatar
laurent committed
401
    LOG_I(PHY, "[UE %d] Measured Carrier Frequency %.0f Hz (offset %d Hz)\n",
Hongzhi Wang's avatar
Hongzhi Wang committed
402
	  ue->Mod_id,
403
	  openair0_cfg[0].rx_freq[0]+ue->common_vars.freq_offset,
Hongzhi Wang's avatar
Hongzhi Wang committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	  ue->common_vars.freq_offset);
#endif
  } else {
#ifdef DEBUG_INITIAL_SYNC
    LOG_I(PHY,"[UE%d] Initial sync : PBCH not ok\n",ue->Mod_id);
    LOG_I(PHY,"[UE%d] Initial sync : Estimated PSS position %d, Nid2 %d\n",ue->Mod_id,sync_pos,ue->common_vars.eNb_id);
    LOG_I(PHY,"[UE%d] Initial sync : Estimated Nid_cell %d, Frame_type %d\n",ue->Mod_id,
          frame_parms->Nid_cell,frame_parms->frame_type);
#endif

    ue->UE_mode[0] = NOT_SYNCHED;
    ue->pbch_vars[0]->pdu_errors_last=ue->pbch_vars[0]->pdu_errors;
    ue->pbch_vars[0]->pdu_errors++;
    ue->pbch_vars[0]->pdu_errors_conseq++;

  }

  // gain control
  if (ret!=0) { //we are not synched, so we cannot use rssi measurement (which is based on channel estimates)
    rx_power = 0;

    // do a measurement on the best guess of the PSS
426
    //for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
427
    //  rx_power += signal_energy(&ue->common_vars.rxdata[aarx][sync_pos2],
428
	//			frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples);
Hongzhi Wang's avatar
Hongzhi Wang committed
429 430 431 432

    /*
    // do a measurement on the full frame
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
433
      rx_power += signal_energy(&ue->common_vars.rxdata[aarx][0],
Hongzhi Wang's avatar
Hongzhi Wang committed
434 435 436 437
				frame_parms->samples_per_subframe*10);
    */

    // we might add a low-pass filter here later
Raymond Knopp's avatar
Raymond Knopp committed
438
    ue->measurements.rx_power_avg[0] = rx_power/fp->nb_antennas_rx;
Hongzhi Wang's avatar
Hongzhi Wang committed
439 440 441 442 443 444 445 446 447 448 449

    ue->measurements.rx_power_avg_dB[0] = dB_fixed(ue->measurements.rx_power_avg[0]);

#ifdef DEBUG_INITIAL_SYNCH
  LOG_I(PHY,"[UE%d] Initial sync : Estimated power: %d dB\n",ue->Mod_id,ue->measurements.rx_power_avg_dB[0] );
#endif

#ifndef OAI_USRP
#ifndef OAI_BLADERF
#ifndef OAI_LMSSDR
#ifndef OAI_ADRV9371_ZC706
450
  //phy_adjust_gain(ue,ue->measurements.rx_power_avg_dB[0],0);
Hongzhi Wang's avatar
Hongzhi Wang committed
451 452 453 454 455 456 457 458 459 460 461 462
#endif
#endif
#endif
#endif

  }
  else {

#ifndef OAI_USRP
#ifndef OAI_BLADERF
#ifndef OAI_LMSSDR
#ifndef OAI_ADRV9371_ZC706
463
  //phy_adjust_gain(ue,dB_fixed(ue->measurements.rssi),0);
Hongzhi Wang's avatar
Hongzhi Wang committed
464 465 466 467 468
#endif
#endif
#endif
#endif

469
  }
470 471

  // if stand alone and sync on ssb do sib1 detection as part of initial sync
472
  if (sa==1 && ret==0) {
473
    bool dec = false;
474
    NR_UE_PDCCH *pdcch_vars  = ue->pdcch_vars[proc->thread_id][0];
475
    int gnb_id = 0; //FIXME
476 477 478
    int coreset_nb_rb=0;
    int coreset_start_rb=0;

479
    for(int n_ss = 0; n_ss<pdcch_vars->nb_search_space; n_ss++) {
francescomani's avatar
francescomani committed
480
      uint8_t nb_symb_pdcch = pdcch_vars->pdcch_config[n_ss].coreset.duration;
481
      int start_symb = pdcch_vars->pdcch_config[n_ss].coreset.StartSymbolIndex;
482
      get_coreset_rballoc(pdcch_vars->pdcch_config[n_ss].coreset.frequency_domain_resource,&coreset_nb_rb,&coreset_start_rb);
483
      for (uint16_t l=start_symb; l<start_symb+nb_symb_pdcch; l++) {
484 485 486 487
        nr_slot_fep_init_sync(ue,
                              proc,
                              l, // the UE PHY has no notion of the symbols to be monitored in the search space
                              pdcch_vars->slot,
488
                              is*fp->samples_per_frame+pdcch_vars->sfn*fp->samples_per_frame+ue->rx_offset);
489 490 491 492 493 494 495 496 497

        if (coreset_nb_rb > 0)
          nr_pdcch_channel_estimation(ue,
                                      proc,
                                      0,
                                      pdcch_vars->slot,
                                      l,
                                      fp->first_carrier_offset+(pdcch_vars->pdcch_config[n_ss].BWPStart + coreset_start_rb)*12,
                                      coreset_nb_rb);
498

499
      }
500
      int  dci_cnt = nr_ue_pdcch_procedures(gnb_id, ue, proc, n_ss);
501
      if (dci_cnt>0){
502 503 504 505 506 507 508 509 510 511 512 513
        NR_UE_DLSCH_t *dlsch = ue->dlsch_SI[gnb_id];
        if (dlsch && (dlsch->active == 1)) {
          uint8_t harq_pid = dlsch->current_harq_pid;
          NR_DL_UE_HARQ_t *dlsch0_harq = dlsch->harq_processes[harq_pid];
          uint16_t nb_symb_sch = dlsch0_harq->nb_symbols;
          uint16_t start_symb_sch = dlsch0_harq->start_symbol;

          for (uint16_t m=start_symb_sch;m<(nb_symb_sch+start_symb_sch) ; m++){
            nr_slot_fep_init_sync(ue,
                                  proc,
                                  m,
                                  pdcch_vars->slot,  // same slot and offset as pdcch
514
                                  is*fp->samples_per_frame+pdcch_vars->sfn*fp->samples_per_frame+ue->rx_offset);
515 516 517 518 519 520 521 522 523
          }

          int ret = nr_ue_pdsch_procedures(ue,
                                           proc,
                                           gnb_id,
                                           SI_PDSCH,
                                           ue->dlsch_SI[gnb_id],
                                           NULL);
          if (ret >= 0)
524 525 526 527 528 529
            dec = nr_ue_dlsch_procedures(ue,
                                         proc,
                                         gnb_id,
                                         SI_PDSCH,
                                         ue->dlsch_SI[gnb_id],
                                         NULL,
530
                                         &ue->dlsch_SI_errors[gnb_id]);
531 532 533 534

          // deactivate dlsch once dlsch proc is done
          ue->dlsch_SI[gnb_id]->active = 0;
        }
535
      }
536
    }
537 538
    if (dec == false) // sib1 not decoded
      ret = -1;
Hongzhi Wang's avatar
Hongzhi Wang committed
539 540
  }
  //  exit_fun("debug exit");
Rakesh's avatar
Rakesh committed
541
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_NR_INITIAL_UE_SYNC, VCD_FUNCTION_OUT);
Hongzhi Wang's avatar
Hongzhi Wang committed
542 543 544
  return ret;
}