initial_sync.c 21.1 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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
 */

22 23 24 25 26 27 28 29 30 31 32
/*! \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"
33 34 35
#include "PHY/defs_UE.h"
#include "PHY/phy_extern_ue.h"
#include "SCHED_UE/sched_UE.h"
36 37 38 39 40
#include "transport_proto_ue.h"
#include "PHY/MODULATION/modulation_UE.h"
#include "PHY/LTE_ESTIMATION/lte_estimation.h"
#include "PHY/LTE_REFSIG/lte_refsig.h"
#include "openair2/LAYER2/MAC/mac_proto.h"
41
#include "common_lib.h"
42 43
#include "PHY/INIT/phy_init.h"

44
extern openair0_config_t openair0_cfg[];
45

46
//#define DEBUG_INITIAL_SYNCH
47

48
int pbch_detection(PHY_VARS_UE *ue, runmode_t mode)
49
{
50

51 52 53
  uint8_t l,pbch_decoded,frame_mod4,pbch_tx_ant,dummy;
  LTE_DL_FRAME_PARMS *frame_parms=&ue->frame_parms;
  char phich_resource[6];
54

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

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

62
    slot_fep(ue,
63 64
	     l,
	     0,
65
	     ue->rx_offset,
66 67
	     0,
	     1);
68
  }
69
  for (l=0; l<frame_parms->symbols_per_tti/2; l++) {
70

71
    slot_fep(ue,
72 73
	     l,
	     1,
74
	     ue->rx_offset,
75 76
	     0,
	     1);
77
  }
78
  slot_fep(ue,
79 80
	   0,
	   2,
81
	   ue->rx_offset,
82 83
	   0,
	   1);
84

85 86
  lte_ue_measurements(ue,
		      ue->rx_offset,
87
		      0,
88 89 90 91 92
                      0,
		      0,
                      0);


93 94
  if (ue->frame_parms.frame_type == TDD) {
    ue_rrc_measurements(ue,
95
			2,
96 97 98
			0);
  }
  else {
99
    ue_rrc_measurements(ue,
100 101 102
			0,
			0);
  }
103
#ifdef DEBUG_INITIAL_SYNCH
104
  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",
105 106 107 108 109 110 111 112 113
        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);
114

115
  LOG_I(PHY,"[UE %d] N0 %d dBm digital (%d, %d) dB, linear (%d, %d), avg noise power %d dB (%d lin)\n",
116 117 118 119 120 121 122 123
        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);
124
#endif
125

126
  pbch_decoded = 0;
127 128

  for (frame_mod4=0; frame_mod4<4; frame_mod4++) {
129 130
    pbch_tx_ant = rx_pbch(&ue->common_vars,
                          ue->pbch_vars[0],
131 132 133
                          frame_parms,
                          0,
                          SISO,
134
                          ue->high_speed_flag,
135 136
                          frame_mod4);

137 138 139 140
    if ((pbch_tx_ant>0) && (pbch_tx_ant<=2)) {
      pbch_decoded = 1;
      break;
    }
141

142 143
    pbch_tx_ant = rx_pbch(&ue->common_vars,
                          ue->pbch_vars[0],
144 145 146
                          frame_parms,
                          0,
                          ALAMOUTI,
147
                          ue->high_speed_flag,
148 149
                          frame_mod4);

150 151 152 153 154
    if ((pbch_tx_ant>0) && (pbch_tx_ant<=2)) {
      pbch_decoded = 1;
      break;
    }
  }
155 156


157
  if (pbch_decoded) {
158

Xiwen JIANG's avatar
Xiwen JIANG committed
159
    frame_parms->nb_antenna_ports_eNB = pbch_tx_ant;
160

161 162
    // set initial transmission mode to 1 or 2 depending on number of detected TX antennas
    // openair_daq_vars.dlsch_transmission_mode = (pbch_tx_ant>1) ? 2 : 1;
163

164 165

    // flip byte endian on 24-bits for MIB
166 167 168
    //    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;
169 170

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

173
    switch (dummy) {
174 175

    case 0 :
176 177
      frame_parms->N_RB_DL = 6;
      break;
178 179

    case 1 :
180 181
      frame_parms->N_RB_DL = 15;
      break;
182 183

    case 2 :
184 185
      frame_parms->N_RB_DL = 25;
      break;
186 187

    case 3 :
188 189
      frame_parms->N_RB_DL = 50;
      break;
190 191

    case 4 :
192 193
      frame_parms->N_RB_DL = 75;
      break;
194

195 196 197
    case 5:
      frame_parms->N_RB_DL = 100;
      break;
198

199
    default:
200
      LOG_E(PHY,"[UE%d] Initial sync: PBCH decoding: Unknown N_RB_DL\n",ue->Mod_id);
201 202 203
      return -1;
      break;
    }
204

205

206
    // now check for PHICH parameters
207 208
    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;
209

210 211 212 213 214
    switch (dummy) {
    case 0:
      frame_parms->phich_config_common.phich_resource = oneSixth;
      sprintf(phich_resource,"1/6");
      break;
215

216 217 218 219
    case 1:
      frame_parms->phich_config_common.phich_resource = half;
      sprintf(phich_resource,"1/2");
      break;
220

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

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

231
    default:
232
      LOG_E(PHY,"[UE%d] Initial sync: Unknown PHICH_DURATION\n",ue->Mod_id);
233 234 235
      return -1;
      break;
    }
236

237 238 239 240
    for(int i=0; i<RX_NB_TH;i++)
    {
        ue->proc.proc_rxtx[i].frame_rx =   (((ue->pbch_vars[0]->decoded_output[2]&3)<<6) + (ue->pbch_vars[0]->decoded_output[1]>>2))<<2;
        ue->proc.proc_rxtx[i].frame_rx =   (((ue->pbch_vars[0]->decoded_output[2]&3)<<6) + (ue->pbch_vars[0]->decoded_output[1]>>2))<<2;
241

242 243
        ue->proc.proc_rxtx[i].frame_tx = ue->proc.proc_rxtx[0].frame_rx;
    }
244
#ifdef DEBUG_INITIAL_SYNCH
245
    LOG_I(PHY,"[UE%d] Initial sync: pbch decoded sucessfully p %d, tx_ant %d, frame %d, N_RB_DL %d, phich_duration %d, phich_resource %s!\n",
246
          ue->Mod_id,
247
          frame_parms->nb_antenna_ports_eNB,
248
          pbch_tx_ant,
249
          ue->proc.proc_rxtx[0].frame_rx,
250 251 252
          frame_parms->N_RB_DL,
          frame_parms->phich_config_common.phich_duration,
          phich_resource);  //frame_parms->phich_config_common.phich_resource);
253 254
#endif
    return(0);
255
  } else {
256 257
    return(-1);
  }
258

259 260
}

261 262 263 264
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"};

265
int initial_sync(PHY_VARS_UE *ue, runmode_t mode)
266 267
{

268
  int32_t sync_pos,sync_pos2,sync_pos_slot;
269 270 271 272
  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;
273
  LTE_DL_FRAME_PARMS *frame_parms = &ue->frame_parms;
274 275
  int ret=-1;
  int aarx,rx_power=0;
276
  /*#ifdef OAI_USRP
277
  __m128i *rxdata128;
278
  #endif*/
279 280
  //  LOG_I(PHY,"**************************************************************\n");
  // First try FDD normal prefix
knopp's avatar
knopp committed
281 282
  frame_parms->Ncp=NORMAL;
  frame_parms->frame_type=FDD;
283 284
  frame_parms->nb_antenna_ports_eNB = 2;

285
  init_frame_parms(frame_parms,1);
286
  /*
Mongazon's avatar
Mongazon committed
287
  LOG_M("rxdata0.m","rxd0",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
knopp's avatar
knopp committed
288

289 290
  exit(-1);
  */
291
  sync_pos = lte_sync_time(ue->common_vars.rxdata,
292
                           frame_parms,
293
                           (int *)&ue->common_vars.eNb_id);
294

Mongazon's avatar
Mongazon committed
295
  //  LOG_M("rxdata1.m","rxd1",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
296 297 298 299 300 301
  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
302
  LOG_I(PHY,"[UE%d] Initial sync : Estimated PSS position %d, Nid2 %d\n",ue->Mod_id,sync_pos,ue->common_vars.eNb_id);
303 304 305
#endif

  // SSS detection
306

307 308 309 310
  // 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)
311
    ue->rx_offset = sync_pos2 - sync_pos_slot;
312
  else
313
    ue->rx_offset = FRAME_LENGTH_COMPLEX_SAMPLES + sync_pos2 - sync_pos_slot;
314

315
  if (((sync_pos2 - sync_pos_slot) >=0 ) &&
316
      ((sync_pos2 - sync_pos_slot) < ((FRAME_LENGTH_COMPLEX_SAMPLES-frame_parms->samples_per_tti/2)))) {
317
#ifdef DEBUG_INITIAL_SYNCH
318 319
    LOG_I(PHY,"Calling sss detection (FDD normal CP)\n");
#endif
320
    rx_sss(ue,&metric_fdd_ncp,&flip_fdd_ncp,&phase_fdd_ncp);
321
    frame_parms->nushift  = frame_parms->Nid_cell%6;
322

323
    if (flip_fdd_ncp==1)
324
      ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
325

326 327 328
    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);
Mongazon's avatar
Mongazon committed
329
    //   LOG_M("rxdata2.m","rxd2",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
330 331 332

#ifdef DEBUG_INITIAL_SYNCH
    LOG_I(PHY,"FDD Normal prefix: CellId %d metric %d, phase %d, flip %d, pbch %d\n",
333 334 335
          frame_parms->Nid_cell,metric_fdd_ncp,phase_fdd_ncp,flip_fdd_ncp,ret);
#endif
  } else {
336
#ifdef DEBUG_INITIAL_SYNCH
337
    LOG_I(PHY,"FDD Normal prefix: SSS error condition: sync_pos %d, sync_pos_slot %d\n", sync_pos, sync_pos_slot);
338 339 340 341 342 343 344
#endif
  }


  if (ret==-1) {

    // Now FDD extended prefix
knopp's avatar
knopp committed
345 346
    frame_parms->Ncp=EXTENDED;
    frame_parms->frame_type=FDD;
347
    init_frame_parms(frame_parms,1);
348

349 350 351 352
    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;
353

354 355
    // 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);
356

357
    if (sync_pos2 >= sync_pos_slot)
358
      ue->rx_offset = sync_pos2 - sync_pos_slot;
359
    else
360
      ue->rx_offset = FRAME_LENGTH_COMPLEX_SAMPLES + sync_pos2 - sync_pos_slot;
361

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

364 365 366
    if (((sync_pos2 - sync_pos_slot) >=0 ) &&
        ((sync_pos2 - sync_pos_slot) < ((FRAME_LENGTH_COMPLEX_SAMPLES-frame_parms->samples_per_tti/2)))) {

367
      rx_sss(ue,&metric_fdd_ecp,&flip_fdd_ecp,&phase_fdd_ecp);
368
      frame_parms->nushift  = frame_parms->Nid_cell%6;
369

370
      if (flip_fdd_ecp==1)
371
        ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
372

373 374 375
      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);
Mongazon's avatar
Mongazon committed
376
      //     LOG_M("rxdata3.m","rxd3",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
377 378
#ifdef DEBUG_INITIAL_SYNCH
      LOG_I(PHY,"FDD Extended prefix: CellId %d metric %d, phase %d, flip %d, pbch %d\n",
379
            frame_parms->Nid_cell,metric_fdd_ecp,phase_fdd_ecp,flip_fdd_ecp,ret);
380
#endif
381
    } else {
382
#ifdef DEBUG_INITIAL_SYNCH
383
      LOG_I(PHY,"FDD Extended prefix: SSS error condition: sync_pos %d, sync_pos_slot %d\n", sync_pos, sync_pos_slot);
384 385 386 387 388
#endif
    }

    if (ret==-1) {
      // Now TDD normal prefix
knopp's avatar
knopp committed
389 390
      frame_parms->Ncp=NORMAL;
      frame_parms->frame_type=TDD;
391
      frame_parms->tdd_config=1;
392 393 394
      init_frame_parms(frame_parms,1);

      if (sync_pos >= frame_parms->nb_prefix_samples)
395
        sync_pos2 = sync_pos - frame_parms->nb_prefix_samples;
396
      else
397
        sync_pos2 = sync_pos + FRAME_LENGTH_COMPLEX_SAMPLES - frame_parms->nb_prefix_samples;
398 399

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

405
      if (sync_pos2 >= sync_pos_slot)
406
        ue->rx_offset = sync_pos2 - sync_pos_slot;
407
      else
408
        ue->rx_offset = (FRAME_LENGTH_COMPLEX_SAMPLES>>1) + sync_pos2 - sync_pos_slot;
409

410
      rx_sss(ue,&metric_tdd_ncp,&flip_tdd_ncp,&phase_tdd_ncp);
411 412

      if (flip_tdd_ncp==1)
413
        ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
414 415

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

418 419
      lte_gold(frame_parms,ue->lte_gold_table[0],frame_parms->Nid_cell);
      ret = pbch_detection(ue,mode);
Mongazon's avatar
Mongazon committed
420
      //      LOG_M("rxdata4.m","rxd4",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
421 422

#ifdef DEBUG_INITIAL_SYNCH
423 424
      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);
425
#endif
426 427 428 429 430

      if (ret==-1) {
        // Now TDD extended prefix
        frame_parms->Ncp=EXTENDED;
        frame_parms->frame_type=TDD;
431
	frame_parms->tdd_config=1;
432 433 434 435 436 437 438 439 440 441 442 443
        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)
444
          ue->rx_offset = sync_pos2 - sync_pos_slot;
445
        else
446
          ue->rx_offset = (FRAME_LENGTH_COMPLEX_SAMPLES>>1) + sync_pos2 - sync_pos_slot;
447

448
        rx_sss(ue,&metric_tdd_ecp,&flip_tdd_ecp,&phase_tdd_ecp);
449 450 451
        frame_parms->nushift  = frame_parms->Nid_cell%6;

        if (flip_tdd_ecp==1)
452
          ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
453

454 455 456
        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);
457

Mongazon's avatar
Mongazon committed
458
	//	LOG_M("rxdata5.m","rxd5",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
459
#ifdef DEBUG_INITIAL_SYNCH
460 461
        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);
462 463 464 465
#endif
      }
    }
  }
466

467
  /* Consider this is a false detection if the offset is > 1000 Hz */
468
  if( (abs(ue->common_vars.freq_offset) > 150) && (ret == 0) )
469 470
  {
	  ret=-1;
Cedric Roux's avatar
Cedric Roux committed
471
#if DISABLE_LOG_X
472
	  printf("Ignore MIB with high freq offset [%d Hz] estimation \n",ue->common_vars.freq_offset);
Cedric Roux's avatar
Cedric Roux committed
473 474 475
#else
	  LOG_E(HW, "Ignore MIB with high freq offset [%d Hz] estimation \n",ue->common_vars.freq_offset);
#endif
476 477
  }

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

480
    //#ifdef DEBUG_INITIAL_SYNCH
Cedric Roux's avatar
Cedric Roux committed
481
#if DISABLE_LOG_X
482
    printf("[UE%d] In synch, rx_offset %d samples\n",ue->Mod_id, ue->rx_offset);
Cedric Roux's avatar
Cedric Roux committed
483 484 485
#else
    LOG_I(PHY, "[UE%d] In synch, rx_offset %d samples\n",ue->Mod_id, ue->rx_offset);
#endif
486
    //#endif
487

488
    if (ue->UE_scan_carrier == 0) {
489 490 491 492 493 494 495 496 497

    #if UE_AUTOTEST_TRACE
      LOG_I(PHY,"[UE  %d] AUTOTEST Cell Sync : frame = %d, rx_offset %d, freq_offset %d \n",
              ue->Mod_id,
              ue->proc.proc_rxtx[0].frame_rx,
              ue->rx_offset,
              ue->common_vars.freq_offset );
    #endif

498
// send sync status to higher layers later when timing offset converge to target timing
Florian Kaltenberger's avatar
Florian Kaltenberger committed
499

500 501
      generate_pcfich_reg_mapping(frame_parms);
      generate_phich_reg_mapping(frame_parms);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
502

503 504 505

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

506
    }
507

Cedric Roux's avatar
Cedric Roux committed
508
#if DISABLE_LOG_X
509
    printf("[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,
510
	  ue->proc.proc_rxtx[0].frame_rx,
511 512 513 514 515 516
	  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])));
517 518


Cedric Roux's avatar
Cedric Roux committed
519 520 521 522 523 524 525 526 527 528 529 530
    printf("[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,
	  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_antenna_ports_eNB);
#else
    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,
531
	  ue->proc.proc_rxtx[0].frame_rx,
532 533 534 535 536 537
	  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])));
538

Cedric Roux's avatar
Cedric Roux committed
539
    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",
540
	  ue->Mod_id,
541
	  ue->proc.proc_rxtx[0].frame_rx,
542 543 544 545 546 547
	  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],
548
	  ue->frame_parms.nb_antenna_ports_eNB);
Cedric Roux's avatar
Cedric Roux committed
549
#endif
550

Wang's avatar
Wang committed
551
#if defined(OAI_USRP) || defined(EXMIMO) || defined(OAI_BLADERF) || defined(OAI_LMSSDR) || defined(OAI_ADRV9371_ZC706)
Cedric Roux's avatar
Cedric Roux committed
552
#  if DISABLE_LOG_X
553
    printf("[UE %d] Frame %d Measured Carrier Frequency %.0f Hz (offset %d Hz)\n",
554
	  ue->Mod_id,
555
	  ue->proc.proc_rxtx[0].frame_rx,
556 557
	  openair0_cfg[0].rx_freq[0]-ue->common_vars.freq_offset,
	  ue->common_vars.freq_offset);
Cedric Roux's avatar
Cedric Roux committed
558 559
#  else
    LOG_I(PHY, "[UE %d] Frame %d Measured Carrier Frequency %.0f Hz (offset %d Hz)\n",
560
	  ue->Mod_id,
561
	  ue->proc.proc_rxtx[0].frame_rx,
562 563
	  openair0_cfg[0].rx_freq[0]-ue->common_vars.freq_offset,
	  ue->common_vars.freq_offset);
Cedric Roux's avatar
Cedric Roux committed
564
#  endif
565
#endif
566
  } else {
567
#ifdef DEBUG_INITIAL_SYNC
568 569
    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);
570
    /*      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",
571
          ue->Mod_id,
572 573 574 575
          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);*/
576
    LOG_I(PHY,"[UE%d] Initial sync : Estimated Nid_cell %d, Frame_type %d\n",ue->Mod_id,
577
          frame_parms->Nid_cell,frame_parms->frame_type);
578
#endif
579

580 581 582 583
    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++;
584

585 586
  }

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

591 592
    // do a measurement on the best guess of the PSS
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
593
      rx_power += signal_energy(&ue->common_vars.rxdata[aarx][sync_pos2],
594
				frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples);
595

596 597
    /*
    // do a measurement on the full frame
598
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
599
      rx_power += signal_energy(&ue->common_vars.rxdata[aarx][0],
600
				frame_parms->samples_per_tti*10);
601
    */
602 603

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

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

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

#ifndef OAI_USRP
613
#ifndef OAI_BLADERF
614
#ifndef OAI_LMSSDR
Wang's avatar
Wang committed
615
#ifndef OAI_ADRV9371_ZC706
616
  phy_adjust_gain(ue,ue->measurements.rx_power_avg_dB[0],0);
617
#endif
618
#endif
Wang's avatar
Wang committed
619
#endif
620
#endif
621

622 623 624 625
  }
  else {

#ifndef OAI_USRP
626
#ifndef OAI_BLADERF
627
#ifndef OAI_LMSSDR
Wang's avatar
Wang committed
628
#ifndef OAI_ADRV9371_ZC706
629
  phy_adjust_gain(ue,dB_fixed(ue->measurements.rssi),0);
630
#endif
631
#endif
Wang's avatar
Wang committed
632
#endif
633
#endif
634

635 636
  }

637
  //  exit_fun("debug exit");
638 639
  return ret;
}
640