initial_sync.c 22.2 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
#ifdef EXMIMO
#include "gain_control.h"
#endif
50

Florian Kaltenberger's avatar
Florian Kaltenberger committed
51
#if defined(OAI_USRP) || defined(EXMIMO) || defined(OAI_LMSSDR)
52 53 54
#include "common_lib.h"
extern openair0_config_t openair0_cfg[];
#endif
55
#define DEBUG_INITIAL_SYNCH
56

57
int pbch_detection(PHY_VARS_UE *phy_vars_ue, runmode_t mode) 
58
{
59

60 61 62
uint8_t l,pbch_decoded,frame_mod4,pbch_tx_ant,dummy;
LTE_DL_FRAME_PARMS *frame_parms=&phy_vars_ue->lte_frame_parms;
char phich_resource[6];
63 64 65

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

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

71
    slot_fep(phy_vars_ue,
72 73 74 75 76 77 78
	     l,
	     0,
	     phy_vars_ue->rx_offset,
	     0,
	     1);
  }  
  for (l=0; l<frame_parms->symbols_per_tti/2; l++) {
79

80 81 82 83 84 85 86
    slot_fep(phy_vars_ue,
	     l,
	     1,
	     phy_vars_ue->rx_offset,
	     0,
	     1);
  }  
87
  slot_fep(phy_vars_ue,
88 89 90 91 92
	   0,
	   2,
	   phy_vars_ue->rx_offset,
	   0,
	   1);
93 94

  lte_ue_measurements(phy_vars_ue,
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		      phy_vars_ue->rx_offset,
		      0,
		      0);
  
  
  if (phy_vars_ue->lte_frame_parms.frame_type == TDD) {
    ue_rrc_measurements(phy_vars_ue,
			1,
			0);
  }
  else {
    ue_rrc_measurements(phy_vars_ue,
			0,
			0);
  }
110
#ifdef DEBUG_INITIAL_SYNCH
111
  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",
112 113 114 115 116 117 118 119 120 121
        phy_vars_ue->Mod_id,
        phy_vars_ue->PHY_measurements.rx_rssi_dBm[0] - ((phy_vars_ue->lte_frame_parms.nb_antennas_rx==2) ? 3 : 0),
        phy_vars_ue->PHY_measurements.rx_power_dB[0][0],
        phy_vars_ue->PHY_measurements.rx_power_dB[0][1],
        phy_vars_ue->PHY_measurements.rx_power[0][0],
        phy_vars_ue->PHY_measurements.rx_power[0][1],
        phy_vars_ue->PHY_measurements.rx_power_avg_dB[0],
        phy_vars_ue->PHY_measurements.rx_power_avg[0],
        phy_vars_ue->rx_total_gain_dB);

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

133
  pbch_decoded = 0;
134 135

  for (frame_mod4=0; frame_mod4<4; frame_mod4++) {
136
    pbch_tx_ant = rx_pbch(&phy_vars_ue->lte_ue_common_vars,
137 138 139 140 141 142 143
                          phy_vars_ue->lte_ue_pbch_vars[0],
                          frame_parms,
                          0,
                          SISO,
                          phy_vars_ue->high_speed_flag,
                          frame_mod4);

144 145 146 147
    if ((pbch_tx_ant>0) && (pbch_tx_ant<=2)) {
      pbch_decoded = 1;
      break;
    }
148

149
    pbch_tx_ant = rx_pbch(&phy_vars_ue->lte_ue_common_vars,
150 151 152 153 154 155 156
                          phy_vars_ue->lte_ue_pbch_vars[0],
                          frame_parms,
                          0,
                          ALAMOUTI,
                          phy_vars_ue->high_speed_flag,
                          frame_mod4);

157 158 159 160 161
    if ((pbch_tx_ant>0) && (pbch_tx_ant<=2)) {
      pbch_decoded = 1;
      break;
    }
  }
162 163


164
  if (pbch_decoded) {
165

166
    frame_parms->nb_antennas_tx_eNB = pbch_tx_ant;
167

168 169 170
    // 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;
171

172 173 174 175 176 177 178 179

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

    // now check for Bandwidth of Cell
    dummy = (phy_vars_ue->lte_ue_pbch_vars[0]->decoded_output[2]>>5)&7;
180

181
    switch (dummy) {
182 183

    case 0 :
184 185
      frame_parms->N_RB_DL = 6;
      break;
186 187

    case 1 :
188 189
      frame_parms->N_RB_DL = 15;
      break;
190 191

    case 2 :
192 193
      frame_parms->N_RB_DL = 25;
      break;
194 195

    case 3 :
196 197
      frame_parms->N_RB_DL = 50;
      break;
198 199

    case 4 :
200 201
      frame_parms->N_RB_DL = 75;
      break;
202

203 204 205
    case 5:
      frame_parms->N_RB_DL = 100;
      break;
206

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

213

214 215 216
    // now check for PHICH parameters
    frame_parms->phich_config_common.phich_duration = (PHICH_DURATION_t)((phy_vars_ue->lte_ue_pbch_vars[0]->decoded_output[2]>>4)&1);
    dummy = (phy_vars_ue->lte_ue_pbch_vars[0]->decoded_output[2]>>2)&3;
217

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

224 225 226 227
    case 1:
      frame_parms->phich_config_common.phich_resource = half;
      sprintf(phich_resource,"1/2");
      break;
228

229 230 231 232
    case 2:
      frame_parms->phich_config_common.phich_resource = one;
      sprintf(phich_resource,"1");
      break;
233

234 235 236 237
    case 3:
      frame_parms->phich_config_common.phich_resource = two;
      sprintf(phich_resource,"2");
      break;
238

239
    default:
240
      LOG_E(PHY,"[UE%d] Initial sync: Unknown PHICH_DURATION\n",phy_vars_ue->Mod_id);
241 242 243
      return -1;
      break;
    }
244 245

    phy_vars_ue->frame_rx =   (((phy_vars_ue->lte_ue_pbch_vars[0]->decoded_output[2]&3)<<6) + (phy_vars_ue->lte_ue_pbch_vars[0]->decoded_output[1]>>2))<<2;
knopp's avatar
 
knopp committed
246
    phy_vars_ue->frame_rx += frame_mod4;
247

248 249
#ifndef USER_MODE
    // one frame delay
knopp's avatar
 
knopp committed
250
    phy_vars_ue->frame_rx ++;
251
#endif
knopp's avatar
 
knopp committed
252
    phy_vars_ue->frame_tx = phy_vars_ue->frame_rx;
253 254
#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",
255 256 257 258 259 260 261
          phy_vars_ue->Mod_id,
          frame_parms->mode1_flag,
          pbch_tx_ant,
          phy_vars_ue->frame_rx,
          frame_parms->N_RB_DL,
          frame_parms->phich_config_common.phich_duration,
          phich_resource);  //frame_parms->phich_config_common.phich_resource);
262 263
#endif
    return(0);
264
  } else {
265 266
    return(-1);
  }
267

268 269
}

270 271 272 273
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"};

274 275 276
int initial_sync(PHY_VARS_UE *phy_vars_ue, runmode_t mode)
{

277
  int32_t sync_pos,sync_pos2,sync_pos_slot;
gauthier's avatar
gauthier committed
278 279 280 281
  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;
282 283 284
  LTE_DL_FRAME_PARMS *frame_parms = &phy_vars_ue->lte_frame_parms;
  int ret=-1;
  int aarx,rx_power=0;
285
  /*#ifdef OAI_USRP
286
  __m128i *rxdata128;
287
  #endif*/
288 289
  //  LOG_I(PHY,"**************************************************************\n");
  // First try FDD normal prefix
knopp's avatar
 
knopp committed
290 291
  frame_parms->Ncp=NORMAL;
  frame_parms->frame_type=FDD;
292
  init_frame_parms(frame_parms,1);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
293 294 295 296
  /*
  write_output("rxdata0.m","rxd0",phy_vars_ue->lte_ue_common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
  exit(-1);
  */
297 298 299
  sync_pos = lte_sync_time(phy_vars_ue->lte_ue_common_vars.rxdata,
                           frame_parms,
                           (int *)&phy_vars_ue->lte_ue_common_vars.eNb_id);
300

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

  // SSS detection
312

313 314 315 316
  // 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)
317
    phy_vars_ue->rx_offset = sync_pos2 - sync_pos_slot;
318 319 320
  else
    phy_vars_ue->rx_offset = FRAME_LENGTH_COMPLEX_SAMPLES + sync_pos2 - sync_pos_slot;

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

329 330
    if (flip_fdd_ncp==1)
      phy_vars_ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);
331

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

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


  if (ret==-1) {

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

355 356 357 358
    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;
359

360 361
    // 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);
362

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

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

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

373 374
      rx_sss(phy_vars_ue,&metric_fdd_ecp,&flip_fdd_ecp,&phase_fdd_ecp);
      frame_parms->nushift  = frame_parms->Nid_cell%6;
375

376
      if (flip_fdd_ecp==1)
377 378
        phy_vars_ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);

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

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

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

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

410
      if (sync_pos2 >= sync_pos_slot)
411
        phy_vars_ue->rx_offset = sync_pos2 - sync_pos_slot;
412
      else
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
        phy_vars_ue->rx_offset = (FRAME_LENGTH_COMPLEX_SAMPLES>>1) + sync_pos2 - sync_pos_slot;

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


      rx_sss(phy_vars_ue,&metric_tdd_ncp,&flip_tdd_ncp,&phase_tdd_ncp);

      if (flip_tdd_ncp==1)
        phy_vars_ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);

      frame_parms->nushift  = frame_parms->Nid_cell%6;
      init_frame_parms(&phy_vars_ue->lte_frame_parms,1);

      lte_gold(frame_parms,phy_vars_ue->lte_gold_table[0],frame_parms->Nid_cell);
      ret = pbch_detection(phy_vars_ue,mode);
429
      //      write_output("rxdata4.m","rxd4",phy_vars_ue->lte_ue_common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
430 431

#ifdef DEBUG_INITIAL_SYNCH
432 433
      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);
434 435
#endif
      /*}
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
          else {
      #ifdef DEBUG_INITIAL_SYNCH
              LOG_I(PHY,"TDD Normal prefix: SSS error condition: sync_pos %d, sync_pos_slot %d\n", sync_pos, sync_pos_slot);
      #endif
      }*/


      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)
          phy_vars_ue->rx_offset = sync_pos2 - sync_pos_slot;
        else
          phy_vars_ue->rx_offset = (FRAME_LENGTH_COMPLEX_SAMPLES>>1) + sync_pos2 - sync_pos_slot;

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

        rx_sss(phy_vars_ue,&metric_tdd_ecp,&flip_tdd_ecp,&phase_tdd_ecp);
        frame_parms->nushift  = frame_parms->Nid_cell%6;

        if (flip_tdd_ecp==1)
          phy_vars_ue->rx_offset += (FRAME_LENGTH_COMPLEX_SAMPLES>>1);

        init_frame_parms(&phy_vars_ue->lte_frame_parms,1);
        lte_gold(frame_parms,phy_vars_ue->lte_gold_table[0],frame_parms->Nid_cell);
        ret = pbch_detection(phy_vars_ue,mode);

476
	//	write_output("rxdata5.m","rxd5",phy_vars_ue->lte_ue_common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
477
#ifdef DEBUG_INITIAL_SYNCH
478 479
        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);
480
#endif
481 482 483 484 485 486 487
        /*}
        else {
        #ifdef DEBUG_INITIAL_SYNCH
          LOG_I(PHY,"TDD Extended prefix: SSS error condition: sync_pos %d, sync_pos_slot %d\n", sync_pos, sync_pos_slot);
        #endif
        }*/

488 489 490
      }
    }
  }
491

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

#ifdef DEBUG_INITIAL_SYNCH
495
    LOG_I(PHY,"[UE%d] In synch, rx_offset %d samples\n",phy_vars_ue->Mod_id, phy_vars_ue->rx_offset);
496
#endif
497 498

    if (phy_vars_ue->UE_scan_carrier == 0) {
Florian Kaltenberger's avatar
Florian Kaltenberger committed
499 500 501 502 503 504 505 506 507 508
      if (phy_vars_ue->mac_enabled==1) {
	LOG_I(PHY,"[UE%d] Sending synch status to higher layers\n",phy_vars_ue->Mod_id);
	//mac_resynch();
	mac_xface->dl_phy_sync_success(phy_vars_ue->Mod_id,phy_vars_ue->frame_rx,0,1);//phy_vars_ue->lte_ue_common_vars.eNb_id);
	phy_vars_ue->UE_mode[0] = PRACH;
      }
      else {
	phy_vars_ue->UE_mode[0] = PUSCH;
      }

509 510 511
      generate_pcfich_reg_mapping(frame_parms);
      generate_phich_reg_mapping(frame_parms);
      //    init_prach625(frame_parms);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
512

513 514
      //phy_vars_ue->lte_ue_pbch_vars[0]->pdu_errors=0;
      phy_vars_ue->lte_ue_pbch_vars[0]->pdu_errors_conseq=0;
515
    //phy_vars_ue->lte_ue_pbch_vars[0]->pdu_errors_last=0;
516
    }
517

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    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",phy_vars_ue->Mod_id,
	  phy_vars_ue->frame_rx,
	  10*log10(phy_vars_ue->PHY_measurements.rssi)-phy_vars_ue->rx_total_gain_dB,
	  10*log10(phy_vars_ue->PHY_measurements.rssi),
	  phy_vars_ue->rx_total_gain_dB,
	  phy_vars_ue->PHY_measurements.n0_power_tot_dBm,
	  10*log10(phy_vars_ue->PHY_measurements.rsrp[0])-phy_vars_ue->rx_total_gain_dB,
	  (10*log10(phy_vars_ue->PHY_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",
	  phy_vars_ue->Mod_id,
	  phy_vars_ue->frame_rx,
	  duplex_string[phy_vars_ue->lte_frame_parms.frame_type],
	  prefix_string[phy_vars_ue->lte_frame_parms.Ncp],
	  phy_vars_ue->lte_frame_parms.Nid_cell,
	  phy_vars_ue->lte_frame_parms.N_RB_DL,
	  phy_vars_ue->lte_frame_parms.phich_config_common.phich_duration,
	  phich_string[phy_vars_ue->lte_frame_parms.phich_config_common.phich_resource],
	  phy_vars_ue->lte_frame_parms.nb_antennas_tx_eNB);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
538
#if defined(OAI_USRP) || defined(EXMIMO) || defined(OAI_LMSSDR)
539 540 541 542 543 544
    LOG_I(PHY,"[UE %d] Frame %d Measured Carrier Frequency %.0f Hz (offset %d Hz)\n",
	  phy_vars_ue->Mod_id,
	  phy_vars_ue->frame_rx,
	  openair0_cfg[0].rx_freq[0]-phy_vars_ue->lte_ue_common_vars.freq_offset,
	  phy_vars_ue->lte_ue_common_vars.freq_offset);
#endif
545
  } else {
546
#ifdef DEBUG_INITIAL_SYNC
547 548 549 550 551 552 553 554 555 556
    LOG_I(PHY,"[UE%d] Initial sync : PBCH not ok\n",phy_vars_ue->Mod_id);
    LOG_I(PHY,"[UE%d] Initial sync : Estimated PSS position %d, Nid2 %d\n",phy_vars_ue->Mod_id,sync_pos,phy_vars_ue->lte_ue_common_vars.eNb_id);
    /*      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",
          phy_vars_ue->Mod_id,
          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);*/
    LOG_I(PHY,"[UE%d] Initial sync : Estimated Nid_cell %d, Frame_type %d\n",phy_vars_ue->Mod_id,
          frame_parms->Nid_cell,frame_parms->frame_type);
557
#endif
558 559 560 561 562 563

    phy_vars_ue->UE_mode[0] = NOT_SYNCHED;
    phy_vars_ue->lte_ue_pbch_vars[0]->pdu_errors_last=phy_vars_ue->lte_ue_pbch_vars[0]->pdu_errors;
    phy_vars_ue->lte_ue_pbch_vars[0]->pdu_errors++;
    phy_vars_ue->lte_ue_pbch_vars[0]->pdu_errors_conseq++;

564 565
  }

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

570 571 572 573 574 575 576
    // do a measurement on the best guess of the PSS
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
      rx_power += signal_energy(&phy_vars_ue->lte_ue_common_vars.rxdata[aarx][sync_pos2],
				frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples);
    
    /*
    // do a measurement on the full frame
577 578 579
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++)
      rx_power += signal_energy(&phy_vars_ue->lte_ue_common_vars.rxdata[aarx][0],
				frame_parms->samples_per_tti*10);
580
    */
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

    // we might add a low-pass filter here later
    phy_vars_ue->PHY_measurements.rx_power_avg[0] = rx_power/frame_parms->nb_antennas_rx; 

    phy_vars_ue->PHY_measurements.rx_power_avg_dB[0] = dB_fixed(phy_vars_ue->PHY_measurements.rx_power_avg[0]);

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

#ifdef EXMIMO
  if ((openair_daq_vars.rx_gain_mode == DAQ_AGC_ON) &&
      (mode != rx_calib_ue) && (mode != rx_calib_ue_med) && (mode != rx_calib_ue_byp) )
    //phy_adjust_gain(phy_vars_ue,0);
    gain_control_all(phy_vars_ue->PHY_measurements.rx_power_avg_dB[0],0);

#else
#ifndef OAI_USRP
599 600
#ifndef OAI_BLADERF 
#ifndef OAI_LMSSDR
601
  phy_adjust_gain(phy_vars_ue,phy_vars_ue->PHY_measurements.rx_power_avg_dB[0],0);
602 603
#endif
#endif
604 605 606 607 608 609 610 611 612 613 614 615
#endif
#endif
  }
  else {
#ifdef EXMIMO
  if ((openair_daq_vars.rx_gain_mode == DAQ_AGC_ON) &&
      (mode != rx_calib_ue) && (mode != rx_calib_ue_med) && (mode != rx_calib_ue_byp) )
    //phy_adjust_gain(phy_vars_ue,0);
    gain_control_all(dB_fixed(phy_vars_ue->PHY_measurements.rssi),0);

#else
#ifndef OAI_USRP
616 617
#ifndef OAI_BLADERF 
#ifndef OAI_LMSSDR
618
  phy_adjust_gain(phy_vars_ue,dB_fixed(phy_vars_ue->PHY_measurements.rssi),0);
619 620
#endif
#endif
621 622 623 624
#endif
#endif
  }

625
  //  exit_fun("debug exit");
626 627
  return ret;
}
628