initial_sync.c 21.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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
 */

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*! \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"
39

40

41 42
#include "common_lib.h"
extern openair0_config_t openair0_cfg[];
43

44
//#define DEBUG_INITIAL_SYNCH
45

46
int pbch_detection(PHY_VARS_UE *ue, runmode_t mode)
47
{
48

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

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

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

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

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

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


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

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

124
  pbch_decoded = 0;
125 126

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

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

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

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


155
  if (pbch_decoded) {
156

Xiwen JIANG's avatar
Xiwen JIANG committed
157
    frame_parms->nb_antenna_ports_eNB = pbch_tx_ant;
158

159 160 161
    // 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;
162

163 164

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

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

172
    switch (dummy) {
173 174

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

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

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

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

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

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

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

204

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

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

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

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

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

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

236 237 238 239 240
    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;
241

242 243
#ifndef USER_MODE
    // one frame delay
244 245
    ue->proc.proc_rxtx[0].frame_rx ++;
    ue->proc.proc_rxtx[1].frame_rx ++;
246
#endif
247 248
    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;
249 250
#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",
251
          ue->Mod_id,
252 253
          frame_parms->mode1_flag,
          pbch_tx_ant,
254
          ue->proc.proc_rxtx[0].frame_rx,
255 256 257
          frame_parms->N_RB_DL,
          frame_parms->phich_config_common.phich_duration,
          phich_resource);  //frame_parms->phich_config_common.phich_resource);
258 259
#endif
    return(0);
260
  } else {
261 262
    return(-1);
  }
263

264 265
}

266 267 268 269
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"};

270
int initial_sync(PHY_VARS_UE *ue, runmode_t mode)
271 272
{

273
  int32_t sync_pos,sync_pos2,sync_pos_slot;
274 275 276 277
  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;
278
  LTE_DL_FRAME_PARMS *frame_parms = &ue->frame_parms;
279 280
  int ret=-1;
  int aarx,rx_power=0;
281
  /*#ifdef OAI_USRP
282
  __m128i *rxdata128;
283
  #endif*/
284 285
  //  LOG_I(PHY,"**************************************************************\n");
  // First try FDD normal prefix
286 287
  frame_parms->Ncp=NORMAL;
  frame_parms->frame_type=FDD;
288
  init_frame_parms(frame_parms,1);
289
  /*
290
  write_output("rxdata0.m","rxd0",ue->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
291 292
  exit(-1);
  */
293
  sync_pos = lte_sync_time(ue->common_vars.rxdata,
294
                           frame_parms,
295
                           (int *)&ue->common_vars.eNb_id);
296

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

  // SSS detection
308

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

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

325
    if (flip_fdd_ncp==1)
326
      ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
327

328 329 330 331
    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);
332 333 334

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


  if (ret==-1) {

    // Now FDD extended prefix
347 348
    frame_parms->Ncp=EXTENDED;
    frame_parms->frame_type=FDD;
349
    init_frame_parms(frame_parms,1);
350

351 352 353 354
    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;
355

356 357
    // 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);
358

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

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

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

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

372
      if (flip_fdd_ecp==1)
373
        ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
374

375 376 377 378
      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);
379 380
#ifdef DEBUG_INITIAL_SYNCH
      LOG_I(PHY,"FDD Extended prefix: CellId %d metric %d, phase %d, flip %d, pbch %d\n",
381
            frame_parms->Nid_cell,metric_fdd_ecp,phase_fdd_ecp,flip_fdd_ecp,ret);
382
#endif
383
    } else {
384
#ifdef DEBUG_INITIAL_SYNCH
385
      LOG_I(PHY,"FDD Extended prefix: SSS error condition: sync_pos %d, sync_pos_slot %d\n", sync_pos, sync_pos_slot);
386 387 388 389 390
#endif
    }

    if (ret==-1) {
      // Now TDD normal prefix
391 392
      frame_parms->Ncp=NORMAL;
      frame_parms->frame_type=TDD;
393 394 395
      init_frame_parms(frame_parms,1);

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

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

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

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

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

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

419 420 421
      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);
422 423

#ifdef DEBUG_INITIAL_SYNCH
424 425
      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);
426
#endif
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

      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)
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

458
	//	write_output("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 499
      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
500
	//mac_resynch();
501
	mac_xface->dl_phy_sync_success(ue->Mod_id,ue->proc.proc_rxtx[0].frame_rx,0,1);//ue->common_vars.eNb_id);
502
	ue->UE_mode[0] = PRACH;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
503 504
      }
      else {
505
	ue->UE_mode[0] = PUSCH;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
506 507
      }

508 509
      generate_pcfich_reg_mapping(frame_parms);
      generate_phich_reg_mapping(frame_parms);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
510

511 512 513

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

514
    }
515

Cedric Roux's avatar
Cedric Roux committed
516
#if DISABLE_LOG_X
517
    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,
518
	  ue->proc.proc_rxtx[0].frame_rx,
519 520 521 522 523 524
	  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])));
525 526


Cedric Roux's avatar
Cedric Roux committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    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,
	  ue->proc.proc_rxtx[0].frame_rx,
	  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])));
546

Cedric Roux's avatar
Cedric Roux committed
547
    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",
548
	  ue->Mod_id,
549
	  ue->proc.proc_rxtx[0].frame_rx,
550 551 552 553 554 555
	  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],
556
	  ue->frame_parms.nb_antenna_ports_eNB);
Cedric Roux's avatar
Cedric Roux committed
557
#endif
558

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

588 589 590 591
    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++;
592

593 594
  }

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

599 600
    // do a measurement on the best guess of the PSS
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
601
      rx_power += signal_energy(&ue->common_vars.rxdata[aarx][sync_pos2],
602
				frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples);
603

604 605
    /*
    // do a measurement on the full frame
606
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
607
      rx_power += signal_energy(&ue->common_vars.rxdata[aarx][0],
608
				frame_parms->samples_per_tti*10);
609
    */
610 611

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

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

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

#ifndef OAI_USRP
621
#ifndef OAI_BLADERF
622
#ifndef OAI_LMSSDR
623
  phy_adjust_gain(ue,ue->measurements.rx_power_avg_dB[0],0);
624
#endif
625 626
#endif
#endif
627

628 629 630 631
  }
  else {

#ifndef OAI_USRP
632
#ifndef OAI_BLADERF
633
#ifndef OAI_LMSSDR
634
  phy_adjust_gain(ue,dB_fixed(ue->measurements.rssi),0);
635
#endif
636 637
#endif
#endif
638

639 640
  }

641
  //  exit_fun("debug exit");
642 643
  return ret;
}
644