initial_sync.c 19.5 KB
Newer Older
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3
    Copyright(c) 1999 - 2014 Eurecom
4

ghaddab's avatar
ghaddab committed
5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


ghaddab's avatar
ghaddab committed
11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

ghaddab's avatar
ghaddab committed
16
    You should have received a copy of the GNU General Public License
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19
   see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
ghaddab's avatar
ghaddab committed
22 23
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27

ghaddab's avatar
ghaddab committed
28
 *******************************************************************************/
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/*! \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.h"
#include "PHY/extern.h"
#include "SCHED/defs.h"
#include "SCHED/extern.h"
#include "defs.h"
#include "extern.h"
47

48

49 50
#include "common_lib.h"
extern openair0_config_t openair0_cfg[];
51

52
//#define DEBUG_INITIAL_SYNCH
53

54 55
int pbch_detection(PHY_VARS_UE *ue, runmode_t mode) 
{
56

57 58 59 60
  uint8_t l,pbch_decoded,frame_mod4,pbch_tx_ant,dummy;
  LTE_DL_FRAME_PARMS *frame_parms=&ue->frame_parms;
  char phich_resource[6];
  
61
#ifdef DEBUG_INITIAL_SYNCH
62 63
  LOG_I(PHY,"[UE%d] Initial sync: starting PBCH detection (rx_offset %d)\n",ue->Mod_id,
        ue->rx_offset);
64 65
#endif

66 67
  for (l=0; l<frame_parms->symbols_per_tti/2; l++) {

68
    slot_fep(ue,
69 70
	     l,
	     0,
71
	     ue->rx_offset,
72 73 74 75
	     0,
	     1);
  }  
  for (l=0; l<frame_parms->symbols_per_tti/2; l++) {
76

77
    slot_fep(ue,
78 79
	     l,
	     1,
80
	     ue->rx_offset,
81 82 83
	     0,
	     1);
  }  
84
  slot_fep(ue,
85 86
	   0,
	   2,
87
	   ue->rx_offset,
88 89
	   0,
	   1);
90

91 92
  lte_ue_measurements(ue,
		      ue->rx_offset,
93 94 95 96
		      0,
		      0);
  
  
97 98
  if (ue->frame_parms.frame_type == TDD) {
    ue_rrc_measurements(ue,
99 100 101 102
			1,
			0);
  }
  else {
103
    ue_rrc_measurements(ue,
104 105 106
			0,
			0);
  }
107
#ifdef DEBUG_INITIAL_SYNCH
108
  LOG_I(PHY,"[UE %d] RX RSSI %d dBm, digital (%d, %d) dB, linear (%d, %d), avg rx power %d dB (%d lin), RX gain %d dB\n",
109 110 111 112 113 114 115 116 117
        ue->Mod_id,
        ue->measurements.rx_rssi_dBm[0] - ((ue->frame_parms.nb_antennas_rx==2) ? 3 : 0),
        ue->measurements.rx_power_dB[0][0],
        ue->measurements.rx_power_dB[0][1],
        ue->measurements.rx_power[0][0],
        ue->measurements.rx_power[0][1],
        ue->measurements.rx_power_avg_dB[0],
        ue->measurements.rx_power_avg[0],
        ue->rx_total_gain_dB);
118

119
  LOG_I(PHY,"[UE %d] N0 %d dBm digital (%d, %d) dB, linear (%d, %d), avg noise power %d dB (%d lin)\n",
120 121 122 123 124 125 126 127
        ue->Mod_id,
        ue->measurements.n0_power_tot_dBm,
        ue->measurements.n0_power_dB[0],
        ue->measurements.n0_power_dB[1],
        ue->measurements.n0_power[0],
        ue->measurements.n0_power[1],
        ue->measurements.n0_power_avg_dB,
        ue->measurements.n0_power_avg);
128
#endif
129

130
  pbch_decoded = 0;
131 132

  for (frame_mod4=0; frame_mod4<4; frame_mod4++) {
133 134
    pbch_tx_ant = rx_pbch(&ue->common_vars,
                          ue->pbch_vars[0],
135 136 137
                          frame_parms,
                          0,
                          SISO,
138
                          ue->high_speed_flag,
139 140
                          frame_mod4);

141 142 143 144
    if ((pbch_tx_ant>0) && (pbch_tx_ant<=2)) {
      pbch_decoded = 1;
      break;
    }
145

146 147
    pbch_tx_ant = rx_pbch(&ue->common_vars,
                          ue->pbch_vars[0],
148 149 150
                          frame_parms,
                          0,
                          ALAMOUTI,
151
                          ue->high_speed_flag,
152 153
                          frame_mod4);

154 155 156 157 158
    if ((pbch_tx_ant>0) && (pbch_tx_ant<=2)) {
      pbch_decoded = 1;
      break;
    }
  }
159 160


161
  if (pbch_decoded) {
162

163
    frame_parms->nb_antennas_tx_eNB = pbch_tx_ant;
164

165 166 167
    // 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;
168

169 170

    // flip byte endian on 24-bits for MIB
171 172 173
    //    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;
174 175

    // now check for Bandwidth of Cell
176
    dummy = (ue->pbch_vars[0]->decoded_output[2]>>5)&7;
177

178
    switch (dummy) {
179 180

    case 0 :
181 182
      frame_parms->N_RB_DL = 6;
      break;
183 184

    case 1 :
185 186
      frame_parms->N_RB_DL = 15;
      break;
187 188

    case 2 :
189 190
      frame_parms->N_RB_DL = 25;
      break;
191 192

    case 3 :
193 194
      frame_parms->N_RB_DL = 50;
      break;
195 196

    case 4 :
197 198
      frame_parms->N_RB_DL = 75;
      break;
199

200 201 202
    case 5:
      frame_parms->N_RB_DL = 100;
      break;
203

204
    default:
205
      LOG_E(PHY,"[UE%d] Initial sync: PBCH decoding: Unknown N_RB_DL\n",ue->Mod_id);
206 207 208
      return -1;
      break;
    }
209

210

211
    // now check for PHICH parameters
212 213
    frame_parms->phich_config_common.phich_duration = (PHICH_DURATION_t)((ue->pbch_vars[0]->decoded_output[2]>>4)&1);
    dummy = (ue->pbch_vars[0]->decoded_output[2]>>2)&3;
214

215 216 217 218 219
    switch (dummy) {
    case 0:
      frame_parms->phich_config_common.phich_resource = oneSixth;
      sprintf(phich_resource,"1/6");
      break;
220

221 222 223 224
    case 1:
      frame_parms->phich_config_common.phich_resource = half;
      sprintf(phich_resource,"1/2");
      break;
225

226 227 228 229
    case 2:
      frame_parms->phich_config_common.phich_resource = one;
      sprintf(phich_resource,"1");
      break;
230

231 232 233 234
    case 3:
      frame_parms->phich_config_common.phich_resource = two;
      sprintf(phich_resource,"2");
      break;
235

236
    default:
237
      LOG_E(PHY,"[UE%d] Initial sync: Unknown PHICH_DURATION\n",ue->Mod_id);
238 239 240
      return -1;
      break;
    }
241

242 243 244 245 246
    ue->proc.proc_rxtx[0].frame_rx =   (((ue->pbch_vars[0]->decoded_output[2]&3)<<6) + (ue->pbch_vars[0]->decoded_output[1]>>2))<<2;
    ue->proc.proc_rxtx[0].frame_rx += frame_mod4;

    ue->proc.proc_rxtx[1].frame_rx =   (((ue->pbch_vars[0]->decoded_output[2]&3)<<6) + (ue->pbch_vars[0]->decoded_output[1]>>2))<<2;
    ue->proc.proc_rxtx[1].frame_rx += frame_mod4;
247

248 249
#ifndef USER_MODE
    // one frame delay
250 251
    ue->proc.proc_rxtx[0].frame_rx ++;
    ue->proc.proc_rxtx[1].frame_rx ++;
252
#endif
253 254
    ue->proc.proc_rxtx[0].frame_tx = ue->proc.proc_rxtx[0].frame_rx;
    ue->proc.proc_rxtx[1].frame_tx = ue->proc.proc_rxtx[1].frame_rx;
255 256
#ifdef DEBUG_INITIAL_SYNCH
    LOG_I(PHY,"[UE%d] Initial sync: pbch decoded sucessfully mode1_flag %d, tx_ant %d, frame %d, N_RB_DL %d, phich_duration %d, phich_resource %s!\n",
257
          ue->Mod_id,
258 259
          frame_parms->mode1_flag,
          pbch_tx_ant,
260
          ue->proc.proc_rxtx[0].frame_rx,
261 262 263
          frame_parms->N_RB_DL,
          frame_parms->phich_config_common.phich_duration,
          phich_resource);  //frame_parms->phich_config_common.phich_resource);
264 265
#endif
    return(0);
266
  } else {
267 268
    return(-1);
  }
269

270 271
}

272 273 274 275
char phich_string[13][4] = {"","1/6","","1/2","","","one","","","","","","two"};
char duplex_string[2][4] = {"FDD","TDD"};
char prefix_string[2][9] = {"NORMAL","EXTENDED"};

276
int initial_sync(PHY_VARS_UE *ue, runmode_t mode)
277 278
{

279
  int32_t sync_pos,sync_pos2,sync_pos_slot;
280 281 282 283
  int32_t metric_fdd_ncp=0,metric_fdd_ecp=0,metric_tdd_ncp=0,metric_tdd_ecp=0;
  uint8_t phase_fdd_ncp,phase_fdd_ecp,phase_tdd_ncp,phase_tdd_ecp;
  uint8_t flip_fdd_ncp,flip_fdd_ecp,flip_tdd_ncp,flip_tdd_ecp;
  //  uint16_t Nid_cell_fdd_ncp=0,Nid_cell_fdd_ecp=0,Nid_cell_tdd_ncp=0,Nid_cell_tdd_ecp=0;
284
  LTE_DL_FRAME_PARMS *frame_parms = &ue->frame_parms;
285 286
  int ret=-1;
  int aarx,rx_power=0;
287
  /*#ifdef OAI_USRP
288
  __m128i *rxdata128;
289
  #endif*/
290 291
  //  LOG_I(PHY,"**************************************************************\n");
  // First try FDD normal prefix
knopp's avatar
knopp committed
292 293
  frame_parms->Ncp=NORMAL;
  frame_parms->frame_type=FDD;
294
  init_frame_parms(frame_parms,1);
295
  /*
296
  write_output("rxdata0.m","rxd0",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
297 298
  exit(-1);
  */
299
  sync_pos = lte_sync_time(ue->common_vars.rxdata,
300
                           frame_parms,
301
                           (int *)&ue->common_vars.eNb_id);
302

303
  //  write_output("rxdata1.m","rxd1",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
304 305 306 307 308 309
  if (sync_pos >= frame_parms->nb_prefix_samples)
    sync_pos2 = sync_pos - frame_parms->nb_prefix_samples;
  else
    sync_pos2 = sync_pos + FRAME_LENGTH_COMPLEX_SAMPLES - frame_parms->nb_prefix_samples;

#ifdef DEBUG_INITIAL_SYNCH
310
  LOG_I(PHY,"[UE%d] Initial sync : Estimated PSS position %d, Nid2 %d\n",ue->Mod_id,sync_pos,ue->common_vars.eNb_id);
311 312 313
#endif

  // SSS detection
314

315 316 317 318
  // PSS is hypothesized in last symbol of first slot in Frame
  sync_pos_slot = (frame_parms->samples_per_tti>>1) - frame_parms->ofdm_symbol_size - frame_parms->nb_prefix_samples;

  if (sync_pos2 >= sync_pos_slot)
319
    ue->rx_offset = sync_pos2 - sync_pos_slot;
320
  else
321
    ue->rx_offset = FRAME_LENGTH_COMPLEX_SAMPLES + sync_pos2 - sync_pos_slot;
322

323
  if (((sync_pos2 - sync_pos_slot) >=0 ) &&
324
      ((sync_pos2 - sync_pos_slot) < ((FRAME_LENGTH_COMPLEX_SAMPLES-frame_parms->samples_per_tti/2)))) {
325
#ifdef DEBUG_INITIAL_SYNCH
326 327
    LOG_I(PHY,"Calling sss detection (FDD normal CP)\n");
#endif
328
    rx_sss(ue,&metric_fdd_ncp,&flip_fdd_ncp,&phase_fdd_ncp);
329
    frame_parms->nushift  = frame_parms->Nid_cell%6;
330

331
    if (flip_fdd_ncp==1)
332
      ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
333

334 335 336 337
    init_frame_parms(&ue->frame_parms,1);
    lte_gold(frame_parms,ue->lte_gold_table[0],frame_parms->Nid_cell);
    ret = pbch_detection(ue,mode);
    //   write_output("rxdata2.m","rxd2",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
338 339 340

#ifdef DEBUG_INITIAL_SYNCH
    LOG_I(PHY,"FDD Normal prefix: CellId %d metric %d, phase %d, flip %d, pbch %d\n",
341 342 343
          frame_parms->Nid_cell,metric_fdd_ncp,phase_fdd_ncp,flip_fdd_ncp,ret);
#endif
  } else {
344
#ifdef DEBUG_INITIAL_SYNCH
345
    LOG_I(PHY,"FDD Normal prefix: SSS error condition: sync_pos %d, sync_pos_slot %d\n", sync_pos, sync_pos_slot);
346 347 348 349 350 351 352
#endif
  }


  if (ret==-1) {

    // Now FDD extended prefix
knopp's avatar
knopp committed
353 354
    frame_parms->Ncp=EXTENDED;
    frame_parms->frame_type=FDD;
355
    init_frame_parms(frame_parms,1);
356

357 358 359 360
    if (sync_pos < frame_parms->nb_prefix_samples)
      sync_pos2 = sync_pos + FRAME_LENGTH_COMPLEX_SAMPLES - frame_parms->nb_prefix_samples;
    else
      sync_pos2 = sync_pos - frame_parms->nb_prefix_samples;
361

362 363
    // PSS is hypothesized in last symbol of first slot in Frame
    sync_pos_slot = (frame_parms->samples_per_tti>>1) - frame_parms->ofdm_symbol_size - (frame_parms->nb_prefix_samples);
364

365
    if (sync_pos2 >= sync_pos_slot)
366
      ue->rx_offset = sync_pos2 - sync_pos_slot;
367
    else
368
      ue->rx_offset = FRAME_LENGTH_COMPLEX_SAMPLES + sync_pos2 - sync_pos_slot;
369

370
    //msg("nb_prefix_samples %d, rx_offset %d\n",frame_parms->nb_prefix_samples,ue->rx_offset);
371

372 373 374
    if (((sync_pos2 - sync_pos_slot) >=0 ) &&
        ((sync_pos2 - sync_pos_slot) < ((FRAME_LENGTH_COMPLEX_SAMPLES-frame_parms->samples_per_tti/2)))) {

375
      rx_sss(ue,&metric_fdd_ecp,&flip_fdd_ecp,&phase_fdd_ecp);
376
      frame_parms->nushift  = frame_parms->Nid_cell%6;
377

378
      if (flip_fdd_ecp==1)
379
        ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
380

381 382 383 384
      init_frame_parms(&ue->frame_parms,1);
      lte_gold(frame_parms,ue->lte_gold_table[0],frame_parms->Nid_cell);
      ret = pbch_detection(ue,mode);
      //     write_output("rxdata3.m","rxd3",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
385 386
#ifdef DEBUG_INITIAL_SYNCH
      LOG_I(PHY,"FDD Extended prefix: CellId %d metric %d, phase %d, flip %d, pbch %d\n",
387
            frame_parms->Nid_cell,metric_fdd_ecp,phase_fdd_ecp,flip_fdd_ecp,ret);
388
#endif
389
    } else {
390
#ifdef DEBUG_INITIAL_SYNCH
391
      LOG_I(PHY,"FDD Extended prefix: SSS error condition: sync_pos %d, sync_pos_slot %d\n", sync_pos, sync_pos_slot);
392 393 394 395 396
#endif
    }

    if (ret==-1) {
      // Now TDD normal prefix
knopp's avatar
knopp committed
397 398
      frame_parms->Ncp=NORMAL;
      frame_parms->frame_type=TDD;
399 400 401
      init_frame_parms(frame_parms,1);

      if (sync_pos >= frame_parms->nb_prefix_samples)
402
        sync_pos2 = sync_pos - frame_parms->nb_prefix_samples;
403
      else
404
        sync_pos2 = sync_pos + FRAME_LENGTH_COMPLEX_SAMPLES - frame_parms->nb_prefix_samples;
405 406

      // PSS is hypothesized in 2nd symbol of third slot in Frame (S-subframe)
407 408 409 410 411
      sync_pos_slot = frame_parms->samples_per_tti +
                      (frame_parms->ofdm_symbol_size<<1) +
                      frame_parms->nb_prefix_samples0 +
                      (frame_parms->nb_prefix_samples);

412
      if (sync_pos2 >= sync_pos_slot)
413
        ue->rx_offset = sync_pos2 - sync_pos_slot;
414
      else
415
        ue->rx_offset = (FRAME_LENGTH_COMPLEX_SAMPLES>>1) + sync_pos2 - sync_pos_slot;
416

417
      rx_sss(ue,&metric_tdd_ncp,&flip_tdd_ncp,&phase_tdd_ncp);
418 419

      if (flip_tdd_ncp==1)
420
        ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
421 422

      frame_parms->nushift  = frame_parms->Nid_cell%6;
423
      init_frame_parms(&ue->frame_parms,1);
424

425 426 427
      lte_gold(frame_parms,ue->lte_gold_table[0],frame_parms->Nid_cell);
      ret = pbch_detection(ue,mode);
      //      write_output("rxdata4.m","rxd4",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
428 429

#ifdef DEBUG_INITIAL_SYNCH
430 431
      LOG_I(PHY,"TDD Normal prefix: CellId %d metric %d, phase %d, flip %d, pbch %d\n",
            frame_parms->Nid_cell,metric_tdd_ncp,phase_tdd_ncp,flip_tdd_ncp,ret);
432
#endif
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

      if (ret==-1) {
        // Now TDD extended prefix
        frame_parms->Ncp=EXTENDED;
        frame_parms->frame_type=TDD;
        init_frame_parms(frame_parms,1);
        sync_pos2 = sync_pos - frame_parms->nb_prefix_samples;

        if (sync_pos >= frame_parms->nb_prefix_samples)
          sync_pos2 = sync_pos - frame_parms->nb_prefix_samples;
        else
          sync_pos2 = sync_pos + FRAME_LENGTH_COMPLEX_SAMPLES - frame_parms->nb_prefix_samples;

        // PSS is hypothesized in 2nd symbol of third slot in Frame (S-subframe)
        sync_pos_slot = frame_parms->samples_per_tti + (frame_parms->ofdm_symbol_size<<1) + (frame_parms->nb_prefix_samples<<1);

        if (sync_pos2 >= sync_pos_slot)
450
          ue->rx_offset = sync_pos2 - sync_pos_slot;
451
        else
452
          ue->rx_offset = (FRAME_LENGTH_COMPLEX_SAMPLES>>1) + sync_pos2 - sync_pos_slot;
453

454
        rx_sss(ue,&metric_tdd_ecp,&flip_tdd_ecp,&phase_tdd_ecp);
455 456 457
        frame_parms->nushift  = frame_parms->Nid_cell%6;

        if (flip_tdd_ecp==1)
458
          ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
459

460 461 462
        init_frame_parms(&ue->frame_parms,1);
        lte_gold(frame_parms,ue->lte_gold_table[0],frame_parms->Nid_cell);
        ret = pbch_detection(ue,mode);
463

464
	//	write_output("rxdata5.m","rxd5",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
465
#ifdef DEBUG_INITIAL_SYNCH
466 467
        LOG_I(PHY,"TDD Extended prefix: CellId %d metric %d, phase %d, flip %d, pbch %d\n",
              frame_parms->Nid_cell,metric_tdd_ecp,phase_tdd_ecp,flip_tdd_ecp,ret);
468 469 470 471
#endif
      }
    }
  }
472

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

475
    //#ifdef DEBUG_INITIAL_SYNCH
476
    LOG_I(PHY,"[UE%d] In synch, rx_offset %d samples\n",ue->Mod_id, ue->rx_offset);
477
    //#endif
478

479 480 481
    if (ue->UE_scan_carrier == 0) {
      if (ue->mac_enabled==1) {
	LOG_I(PHY,"[UE%d] Sending synch status to higher layers\n",ue->Mod_id);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
482
	//mac_resynch();
483
	mac_xface->dl_phy_sync_success(ue->Mod_id,ue->proc.proc_rxtx[0].frame_rx,0,1);//ue->common_vars.eNb_id);
484
	ue->UE_mode[0] = PRACH;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
485 486
      }
      else {
487
	ue->UE_mode[0] = PUSCH;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
488 489
      }

490 491
      generate_pcfich_reg_mapping(frame_parms);
      generate_phich_reg_mapping(frame_parms);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
492

493 494 495

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

496
    }
497

498
    LOG_I(PHY,"[UE %d] Frame %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,
499
	  ue->proc.proc_rxtx[0].frame_rx,
500 501 502 503 504 505
	  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])));
506 507 508
    
    
    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",
509
	  ue->Mod_id,
510
	  ue->proc.proc_rxtx[0].frame_rx,
511 512 513 514 515 516 517 518
	  duplex_string[ue->frame_parms.frame_type],
	  prefix_string[ue->frame_parms.Ncp],
	  ue->frame_parms.Nid_cell,
	  ue->frame_parms.N_RB_DL,
	  ue->frame_parms.phich_config_common.phich_duration,
	  phich_string[ue->frame_parms.phich_config_common.phich_resource],
	  ue->frame_parms.nb_antennas_tx_eNB);

519
#if defined(OAI_USRP) || defined(EXMIMO) || defined(OAI_BLADERF) || defined(OAI_LMSSDR)
520
    LOG_I(PHY,"[UE %d] Frame %d Measured Carrier Frequency %.0f Hz (offset %d Hz)\n",
521
	  ue->Mod_id,
522
	  ue->proc.proc_rxtx[0].frame_rx,
523 524
	  openair0_cfg[0].rx_freq[0]-ue->common_vars.freq_offset,
	  ue->common_vars.freq_offset);
525
#endif
526
  } else {
527
#ifdef DEBUG_INITIAL_SYNC
528 529
    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);
530
    /*      LOG_I(PHY,"[UE%d] Initial sync: (metric fdd_ncp %d (%d), metric fdd_ecp %d (%d), metric_tdd_ncp %d (%d), metric_tdd_ecp %d (%d))\n",
531
          ue->Mod_id,
532 533 534 535
          metric_fdd_ncp,Nid_cell_fdd_ncp,
          metric_fdd_ecp,Nid_cell_fdd_ecp,
          metric_tdd_ncp,Nid_cell_tdd_ncp,
          metric_tdd_ecp,Nid_cell_tdd_ecp);*/
536
    LOG_I(PHY,"[UE%d] Initial sync : Estimated Nid_cell %d, Frame_type %d\n",ue->Mod_id,
537
          frame_parms->Nid_cell,frame_parms->frame_type);
538
#endif
539

540 541 542 543
    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++;
544

545 546
  }

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

551 552
    // do a measurement on the best guess of the PSS
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
553
      rx_power += signal_energy(&ue->common_vars.rxdata[aarx][sync_pos2],
554 555 556 557
				frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples);
    
    /*
    // do a measurement on the full frame
558
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
559
      rx_power += signal_energy(&ue->common_vars.rxdata[aarx][0],
560
				frame_parms->samples_per_tti*10);
561
    */
562 563

    // we might add a low-pass filter here later
564
    ue->measurements.rx_power_avg[0] = rx_power/frame_parms->nb_antennas_rx; 
565

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

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

#ifndef OAI_USRP
573 574
#ifndef OAI_BLADERF 
#ifndef OAI_LMSSDR
575
  phy_adjust_gain(ue,ue->measurements.rx_power_avg_dB[0],0);
576
#endif
577 578
#endif
#endif
579

580 581 582 583
  }
  else {

#ifndef OAI_USRP
584 585
#ifndef OAI_BLADERF 
#ifndef OAI_LMSSDR
586
  phy_adjust_gain(ue,dB_fixed(ue->measurements.rssi),0);
587
#endif
588 589
#endif
#endif
590

591 592
  }

593
  //  exit_fun("debug exit");
594 595
  return ret;
}
596