lte-ue.c 63.1 KB
Newer Older
1
/*******************************************************************************
2
    OpenAirInterface
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Copyright(c) 1999 - 2014 Eurecom

    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.


    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.

    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,
19 20 21 22 23
    see <http://www.gnu.org/licenses/>.

   Contact Information
   OpenAirInterface Admin: openair_admin@eurecom.fr
   OpenAirInterface Tech : openair_tech@eurecom.fr
24
   OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE

*******************************************************************************/

/*! \file lte-ue.c
 * \brief threads and support functions for real-time LTE UE target
 * \author R. Knopp, F. Kaltenberger, Navid Nikaein
 * \date 2015
 * \version 0.1
 * \company Eurecom
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr, navid.nikaein@eurecom.fr
 * \note
 * \warning
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sched.h>
#include <linux/sched.h>
#include <signal.h>
#include <execinfo.h>
#include <getopt.h>
#include <syscall.h>

#include "rt_wrapper.h"
#include "assertions.h"
#include "PHY/types.h"

#include "PHY/defs.h"
Raymond Knopp's avatar
 
Raymond Knopp committed
62
#ifdef OPENAIR2
63 64
#include "LAYER2/MAC/defs.h"
#include "RRC/LITE/extern.h"
Raymond Knopp's avatar
 
Raymond Knopp committed
65
#endif
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include "PHY_INTERFACE/extern.h"

#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all
//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all

#ifdef EXMIMO
#include "openair0_lib.h"
#else
#include "../../ARCH/COMMON/common_lib.h"
#endif

#include "PHY/extern.h"
#include "MAC_INTERFACE/extern.h"
//#include "SCHED/defs.h"
#include "SCHED/extern.h"
Raymond Knopp's avatar
 
Raymond Knopp committed
81
#ifdef OPENAIR2
82 83
#include "LAYER2/MAC/extern.h"
#include "LAYER2/MAC/proto.h"
Raymond Knopp's avatar
 
Raymond Knopp committed
84
#endif
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

#include "UTIL/LOG/log_extern.h"
#include "UTIL/OTG/otg_tx.h"
#include "UTIL/OTG/otg_externs.h"
#include "UTIL/MATH/oml.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
#include "UTIL/OPT/opt.h"

#define FRAME_PERIOD    100000000ULL
#define DAQ_PERIOD      66667ULL

typedef enum {
  pss=0,
  pbch=1,
  si=2
} sync_mode_t;

int init_dlsch_threads(void);
void cleanup_dlsch_threads(void);
int32_t init_rx_pdsch_thread(void);
void cleanup_rx_pdsch_thread(void);

extern pthread_cond_t sync_cond;
extern pthread_mutex_t sync_mutex;
extern int sync_var;

extern openair0_config_t openair0_cfg[MAX_CARDS];
extern uint32_t          downlink_frequency[MAX_NUM_CCs][4];
113
extern int32_t           uplink_frequency_offset[MAX_NUM_CCs][4];
114 115 116 117 118 119 120 121
extern openair0_rf_map rf_map[MAX_NUM_CCs];

extern openair0_device openair0;
extern int oai_exit;

extern int32_t **rxdata;
extern int32_t **txdata;

122 123
//extern unsigned int tx_forward_nsamps;
//extern int tx_delay;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

extern int rx_input_level_dBm;
extern uint8_t exit_missed_slots;
extern uint64_t num_missed_slots; // counter for the number of missed slots

extern void exit_fun(const char* s);

#ifdef EXMIMO

extern unsigned int             rxg_max[4];
extern unsigned int             rxg_med[4];
extern unsigned int             rxg_byp[4];
extern unsigned int             nf_max[4];
extern unsigned int             nf_med[4];
extern unsigned int             nf_byp[4];
extern rx_gain_t                rx_gain_mode[MAX_NUM_CCs][4];

extern double tx_gain[MAX_NUM_CCs][4];
extern double rx_gain[MAX_NUM_CCs][4];
#endif
#define KHz (1000UL)
#define MHz (1000 * KHz)

typedef struct eutra_band_s {
  int16_t band;
  uint32_t ul_min;
  uint32_t ul_max;
  uint32_t dl_min;
  uint32_t dl_max;
  lte_frame_type_t frame_type;
} eutra_band_t;

typedef struct band_info_s {
  int nbands;
  eutra_band_t band_info[100];
} band_info_t;

band_info_t bands_to_scan;

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static const eutra_band_t eutra_bands[] = {
  { 1, 1920    * MHz, 1980    * MHz, 2110    * MHz, 2170    * MHz, FDD},
  { 2, 1850    * MHz, 1910    * MHz, 1930    * MHz, 1990    * MHz, FDD},
  { 3, 1710    * MHz, 1785    * MHz, 1805    * MHz, 1880    * MHz, FDD},
  { 4, 1710    * MHz, 1755    * MHz, 2110    * MHz, 2155    * MHz, FDD},
  { 5,  824    * MHz,  849    * MHz,  869    * MHz,  894    * MHz, FDD},
  { 6,  830    * MHz,  840    * MHz,  875    * MHz,  885    * MHz, FDD},
  { 7, 2500    * MHz, 2570    * MHz, 2620    * MHz, 2690    * MHz, FDD},
  { 8,  880    * MHz,  915    * MHz,  925    * MHz,  960    * MHz, FDD},
  { 9, 1749900 * KHz, 1784900 * KHz, 1844900 * KHz, 1879900 * KHz, FDD},
  {10, 1710    * MHz, 1770    * MHz, 2110    * MHz, 2170    * MHz, FDD},
  {11, 1427900 * KHz, 1452900 * KHz, 1475900 * KHz, 1500900 * KHz, FDD},
  {12,  698    * MHz,  716    * MHz,  728    * MHz,  746    * MHz, FDD},
  {13,  777    * MHz,  787    * MHz,  746    * MHz,  756    * MHz, FDD},
  {14,  788    * MHz,  798    * MHz,  758    * MHz,  768    * MHz, FDD},
  {17,  704    * MHz,  716    * MHz,  734    * MHz,  746    * MHz, FDD},
  {20,  832    * MHz,  862    * MHz,  791    * MHz,  821    * MHz, FDD},
180
  {22, 3510    * MHz, 3590    * MHz, 3410    * MHz, 3490    * MHz, FDD},
181 182 183 184 185 186 187 188 189 190 191 192 193 194
  {33, 1900    * MHz, 1920    * MHz, 1900    * MHz, 1920    * MHz, TDD},
  {34, 2010    * MHz, 2025    * MHz, 2010    * MHz, 2025    * MHz, TDD},
  {35, 1850    * MHz, 1910    * MHz, 1850    * MHz, 1910    * MHz, TDD},
  {36, 1930    * MHz, 1990    * MHz, 1930    * MHz, 1990    * MHz, TDD},
  {37, 1910    * MHz, 1930    * MHz, 1910    * MHz, 1930    * MHz, TDD},
  {38, 2570    * MHz, 2620    * MHz, 2570    * MHz, 2630    * MHz, TDD},
  {39, 1880    * MHz, 1920    * MHz, 1880    * MHz, 1920    * MHz, TDD},
  {40, 2300    * MHz, 2400    * MHz, 2300    * MHz, 2400    * MHz, TDD},
  {41, 2496    * MHz, 2690    * MHz, 2496    * MHz, 2690    * MHz, TDD},
  {42, 3400    * MHz, 3600    * MHz, 3400    * MHz, 3600    * MHz, TDD},
  {43, 3600    * MHz, 3800    * MHz, 3600    * MHz, 3800    * MHz, TDD},
  {44, 703    * MHz, 803    * MHz, 703    * MHz, 803    * MHz, TDD},
};

195 196
/*!
 * \brief This is the UE synchronize thread.
197
 * It performs band scanning and synchonization.
198 199 200
 * \param arg is a pointer to a \ref PHY_VARS_UE structure.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
201 202
static void *UE_thread_synch(void *arg)
{
203 204 205
  static int UE_thread_synch_retval;
  int i, hw_slot_offset;
  PHY_VARS_UE *UE = (PHY_VARS_UE*) arg;
206 207
  int current_band = 0;
  int current_offset = 0;
208
  sync_mode_t sync_mode = pbch;
209
  int card;
210 211
  int ind;
  int found;
212
  int freq_offset=0;
213 214 215 216 217 218 219

  UE->is_synchronized = 0;
  printf("UE_thread_sync in with PHY_vars_UE %p\n",arg);
  printf("waiting for sync (UE_thread_synch) \n");

  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_sync_thread)\n");
220

221 222
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
223

224 225 226
  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex (UE_sync_thread)\n");

227
  printf("starting UE synch thread (IC %d)\n",UE->instance_cnt_synch);
228 229
  ind = 0;
  found = 0;
230 231


232 233
  if (UE->UE_scan == 0) {
    do  {
234
      current_band = eutra_bands[ind].band;
Raymond Knopp's avatar
 
Raymond Knopp committed
235
      printf( "Scanning band %d, dl_min %"PRIu32", ul_min %"PRIu32"\n", current_band, eutra_bands[ind].dl_min,eutra_bands[ind].ul_min);
236

237
      if ((eutra_bands[ind].dl_min <= downlink_frequency[0][0]) && (eutra_bands[ind].dl_max >= downlink_frequency[0][0])) {
238 239 240 241 242 243 244
        for (card=0; card<MAX_NUM_CCs; card++)
          for (i=0; i<4; i++)
            uplink_frequency_offset[card][i] = eutra_bands[ind].ul_min - eutra_bands[ind].dl_min;

        found = 1;
        break;
      }
245

246
      ind++;
247 248
    } while (ind < sizeof(eutra_bands) / sizeof(eutra_bands[0]));
  
249 250
    if (found == 0) {
      exit_fun("Can't find EUTRA band for frequency");
251
      return &UE_thread_synch_retval;
252
    }
253

254 255


256

257 258


Raymond Knopp's avatar
 
Raymond Knopp committed
259
    LOG_I( PHY, "[SCHED][UE] Check absolute frequency DL %"PRIu32", UL %"PRIu32" (oai_exit %d)\n", downlink_frequency[0][0], downlink_frequency[0][0]+uplink_frequency_offset[0][0],oai_exit );
260 261 262 263 264 265 266

    for (i=0;i<openair0_cfg[0].rx_num_channels;i++) {
      openair0_cfg[0].rx_freq[i] = downlink_frequency[0][i];
      openair0_cfg[0].tx_freq[i] = downlink_frequency[0][i]+uplink_frequency_offset[0][i];
      openair0_cfg[0].autocal[i] = 1;
    }

267
    sync_mode = pbch;
268

269
  } else if  (UE->UE_scan == 1) {
270
    current_band=0;
271

272
    for (card=0; card<MAX_CARDS; card++) {
273
      for (i=0; i<openair0_cfg[card].rx_num_channels; i++) {
274 275 276 277 278
        downlink_frequency[card][i] = bands_to_scan.band_info[0].dl_min;
        uplink_frequency_offset[card][i] = bands_to_scan.band_info[0].ul_min-bands_to_scan.band_info[0].dl_min;

        openair0_cfg[card].rx_freq[i] = downlink_frequency[card][i];
        openair0_cfg[card].tx_freq[i] = downlink_frequency[card][i]+uplink_frequency_offset[card][i];
279
#ifdef OAI_USRP
280
        openair0_cfg[card].rx_gain[i] = UE->rx_total_gain_dB;//-USRP_GAIN_OFFSET;
281

282
	
283 284 285 286 287 288 289 290 291 292 293 294 295
        switch(UE->lte_frame_parms.N_RB_DL) {
        case 6:
          openair0_cfg[card].rx_gain[i] -= 12;
          break;

        case 25:
          openair0_cfg[card].rx_gain[i] -= 6;
          break;

        case 50:
          openair0_cfg[card].rx_gain[i] -= 3;
          break;

296 297 298 299
        case 100:
          openair0_cfg[card].rx_gain[i] -= 0;
          break;

300
        default:
301
          printf( "Unknown number of RBs %d\n", UE->lte_frame_parms.N_RB_DL );
302 303
          break;
        }
304
	
305
        printf( "UE synch: setting RX gain (%d,%d) to %f\n", card, i, openair0_cfg[card].rx_gain[i] );
306 307
#endif
      }
308 309 310
    }

  }
311 312

  while (oai_exit==0) {
313

314
    if (pthread_mutex_lock(&UE->mutex_synch) != 0) {
315
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE initial synch thread\n" );
316
      exit_fun("noting to add");
317 318
      return &UE_thread_synch_retval;
    }
319
    
320

321
    while (UE->instance_cnt_synch < 0) {
322
      // the thread waits here most of the time
323 324
      pthread_cond_wait( &UE->cond_synch, &UE->mutex_synch );
    }
325

326 327 328 329 330
    if (pthread_mutex_unlock(&UE->mutex_synch) != 0) {
      LOG_E( PHY, "[SCHED][eNB] error unlocking mutex for UE Initial Synch thread\n" );
      exit_fun("nothing to add");
      return &UE_thread_synch_retval;
    }
331

332
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SYNCH, 1 );
333 334

    switch (sync_mode) {
335
    case pss:
336 337
      LOG_I(PHY,"[SCHED][UE] Scanning band %d (%d), freq %u\n",bands_to_scan.band_info[current_band].band, current_band,bands_to_scan.band_info[current_band].dl_min+current_offset);
      lte_sync_timefreq(UE,current_band,bands_to_scan.band_info[current_band].dl_min+current_offset);
338 339 340 341 342 343 344
      current_offset += 20000000; // increase by 20 MHz

      if (current_offset > bands_to_scan.band_info[current_band].dl_max-bands_to_scan.band_info[current_band].dl_min) {
        current_band++;
        current_offset=0;
      }

345
      if (current_band==bands_to_scan.nbands) {
346 347
        current_band=0;
        oai_exit=1;
348
      }
349 350 351 352 353 354 355

      for (card=0; card<MAX_CARDS; card++) {
        for (i=0; i<openair0_cfg[card].rx_num_channels; i++) {
          downlink_frequency[card][i] = bands_to_scan.band_info[current_band].dl_min+current_offset;
          uplink_frequency_offset[card][i] = bands_to_scan.band_info[current_band].ul_min-bands_to_scan.band_info[0].dl_min + current_offset;


356 357
          openair0_cfg[card].rx_freq[i] = downlink_frequency[card][i];
          openair0_cfg[card].tx_freq[i] = downlink_frequency[card][i]+uplink_frequency_offset[card][i];
358
#ifdef OAI_USRP
359 360
          openair0_cfg[card].rx_gain[i] = UE->rx_total_gain_dB;//-USRP_GAIN_OFFSET;  // 65 calibrated for USRP B210 @ 2.6 GHz
	  
361 362 363 364 365 366 367 368 369 370 371 372 373
          switch(UE->lte_frame_parms.N_RB_DL) {
          case 6:
            openair0_cfg[card].rx_gain[i] -= 12;
            break;

          case 25:
            openair0_cfg[card].rx_gain[i] -= 6;
            break;

          case 50:
            openair0_cfg[card].rx_gain[i] -= 3;
            break;

374 375 376 377
          case 100:
            openair0_cfg[card].rx_gain[i] -= 0;
            break;

378 379 380 381
          default:
            printf("Unknown number of RBs %d\n",UE->lte_frame_parms.N_RB_DL);
            break;
          }
382
	  
383 384

          printf("UE synch: setting RX gain (%d,%d) to %f\n",card,i,openair0_cfg[card].rx_gain[i]);
385
#endif
386

387
        }
388 389 390

      }

391
      if (UE->UE_scan_carrier) {
392

393 394 395 396
	for (i=0;i<openair0_cfg[0].rx_num_channels;i++)
	  openair0_cfg[0].autocal[i] = 1;

      }
397

398 399 400

      break;
 
401
    case pbch:
402

403
      
404
      if (initial_sync( UE, UE->mode ) == 0) {
405 406 407 408 409 410 411 412

        hw_slot_offset = (UE->rx_offset<<1) / UE->lte_frame_parms.samples_per_tti;
        LOG_I( HW, "Got synch: hw_slot_offset %d\n", hw_slot_offset );
	if (UE->UE_scan_carrier == 1) {

	  UE->UE_scan_carrier = 0;
	  // rerun with new cell parameters and frequency-offset
	  for (i=0;i<openair0_cfg[0].rx_num_channels;i++) {
413
	    openair0_cfg[0].rx_gain[i] = UE->rx_total_gain_dB;//-USRP_GAIN_OFFSET;
414 415 416 417 418 419
	    openair0_cfg[0].rx_freq[i] -= UE->lte_ue_common_vars.freq_offset;
	    openair0_cfg[0].tx_freq[i] =  openair0_cfg[0].rx_freq[i]+uplink_frequency_offset[0][i];
	    downlink_frequency[0][i] = openair0_cfg[0].rx_freq[i];
	    freq_offset=0;	    
	  }

420
	  // reconfigure for potentially different bandwidth
421 422 423
	  switch(UE->lte_frame_parms.N_RB_DL) {
	  case 6:
	    openair0_cfg[0].sample_rate =1.92e6;
424 425
	    openair0_cfg[0].rx_bw          =.96e6;
	    openair0_cfg[0].tx_bw          =.96e6;
426
            openair0_cfg[0].rx_gain[0] -= 12;
427 428
	    break;
	  case 25:
429 430 431
	    openair0_cfg[0].sample_rate =7.68e6;
	    openair0_cfg[0].rx_bw          =2.5e6;
	    openair0_cfg[0].tx_bw          =2.5e6;
432
            openair0_cfg[0].rx_gain[0] -= 6;
433 434
	    break;
	  case 50:
435 436 437
	    openair0_cfg[0].sample_rate =15.36e6;
	    openair0_cfg[0].rx_bw          =5.0e6;
	    openair0_cfg[0].tx_bw          =5.0e6;
438
            openair0_cfg[0].rx_gain[0] -= 3;
439 440 441
	    break;
	  case 100:
	    openair0_cfg[0].sample_rate=30.72e6;
442 443
	    openair0_cfg[0].rx_bw=10.0e6;
	    openair0_cfg[0].tx_bw=10.0e6;
444
            openair0_cfg[0].rx_gain[0] -= 0;
445 446
	    break;
	  }
447 448 449 450 451
#ifndef EXMIMO
	  openair0.trx_set_freq_func(&openair0,&openair0_cfg[0],0);
	  openair0.trx_set_gains_func(&openair0,&openair0_cfg[0]);
	  openair0.trx_stop_func(0);	  
#else
452 453 454
	  openair0_set_frequencies(&openair0,&openair0_cfg[0],0);
	  openair0_set_gains(&openair0,&openair0_cfg[0]);
	  openair0_stop(0);
455
#endif
456
	  sleep(1);
457 458 459 460
	  init_frame_parms(&UE->lte_frame_parms,1);
	}
	else {
	  UE->is_synchronized = 1;
461 462

	 if( UE->mode == rx_dump_frame ){
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	   FILE *fd;
	   if ((UE->frame_rx&1) == 0) {  // this guarantees SIB1 is present 
	     if (fd = fopen("rxsig_frame0.dat","w")) {
	       fwrite((void*)&UE->lte_ue_common_vars.rxdata[0][0],
		      sizeof(int32_t),
		      10*UE->lte_frame_parms.samples_per_tti,
		      fd);
	       LOG_I(PHY,"Dummping Frame ... bye bye \n");
	       fclose(fd);
	       exit(0);
	     }
	     else {
	       LOG_E(PHY,"Cannot open file for writing\n");
	       exit(0);
	     }
	   }
	   else {
	     UE->is_synchronized = 0;
	   }
482
	 }
483
	 
484

485
#ifndef EXMIMO
486 487
	  UE->slot_rx = 0;
	  UE->slot_tx = 4;
488
#else
489 490
	  UE->slot_rx = 18;
	  UE->slot_tx = 2;
491
#endif
492
	}
493 494 495
      } else {
        // initial sync failed
        // calculate new offset and try again
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
	if (UE->UE_scan_carrier == 1) {
	  if (freq_offset >= 0) {
	    freq_offset += 100;
	    freq_offset *= -1;
	  } else {
	    freq_offset *= -1;
	  }
	
	  if (abs(freq_offset) > 7500) {
	    LOG_I( PHY, "[initial_sync] No cell synchronization found, abandoning\n" );
	    mac_xface->macphy_exit("No cell synchronization found, abandoning");
	    return &UE_thread_synch_retval; // not reached
	  }
	}
	else {
	  
	}
        LOG_I( PHY, "[initial_sync] trying carrier off %d Hz, rxgain %d (DL %u, UL %u)\n", 
	       freq_offset,
515
               UE->rx_total_gain_dB,
516 517
               downlink_frequency[0][0]+freq_offset,
               downlink_frequency[0][0]+uplink_frequency_offset[0][0]+freq_offset );
518 519 520

        for (card=0; card<MAX_CARDS; card++) {
          for (i=0; i<openair0_cfg[card].rx_num_channels; i++) {
521 522
            openair0_cfg[card].rx_freq[i] = downlink_frequency[card][i]+freq_offset;
            openair0_cfg[card].tx_freq[i] = downlink_frequency[card][i]+uplink_frequency_offset[card][i]+freq_offset;
523
#ifdef OAI_USRP
524
            openair0_cfg[card].rx_gain[i] = UE->rx_total_gain_dB;//-USRP_GAIN_OFFSET;
525 526 527 528 529 530
	    
	    
#ifndef EXMIMO
	    openair0.trx_set_freq_func(&openair0,&openair0_cfg[0],0);
	    
#else
531
	    openair0_set_frequencies(&openair0,&openair0_cfg[0],0);
532
	    
533
#endif
534 535 536 537 538 539 540 541 542 543
            switch(UE->lte_frame_parms.N_RB_DL) {
            case 6:
              openair0_cfg[card].rx_gain[i] -= 12;
              break;

            case 25:
              openair0_cfg[card].rx_gain[i] -= 6;
              break;

            case 50:
544 545 546 547 548
              openair0_cfg[card].rx_gain[i] -= 0;//3;
              break;

            case 100:
              openair0_cfg[card].rx_gain[i] -= 0;
549 550 551 552 553 554
              break;

            default:
              printf("Unknown number of RBs %d\n",UE->lte_frame_parms.N_RB_DL);
              break;
            }
555
	    
556
#endif
557 558
          }
        }
559 560 561 562 563 564
	if (UE->UE_scan_carrier==1) {
	  for (i=0;i<openair0_cfg[0].rx_num_channels;i++)
	    openair0_cfg[0].autocal[i] = 1;
	  
	}
      }// initial_sync=0
565

566
      break;
567

568 569 570 571
    case si:
    default:
      break;
    }
572

573
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SYNCH, 0 );
Raymond Knopp's avatar
 
Raymond Knopp committed
574

575 576


577
    if (pthread_mutex_lock(&UE->mutex_synch) != 0) {
578 579 580 581
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE synch\n" );
      exit_fun("noting to add");
      return &UE_thread_synch_retval;
    }
582

583
    // indicate readiness
584 585 586 587 588 589
    UE->instance_cnt_synch--;

    if (pthread_mutex_unlock(&UE->mutex_synch) != 0) {
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE synch\n" );
      exit_fun("noting to add");
      return &UE_thread_synch_retval;
590
    }
591

592
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SYNCH, 0 );
593
  }  // while !oai_exit
594

595
  return &UE_thread_synch_retval;
596 597
}

598 599 600 601 602 603
/*!
 * \brief This is the UE transmit thread.
 * This thread performs the phy_procedures_UE_TX() on every transmit slot.
 * \param arg is a pointer to a \ref PHY_VARS_UE structure.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
604 605
static void *UE_thread_tx(void *arg)
{
606
  static int UE_thread_tx_retval;
607 608 609 610 611 612 613
  int ret;

  PHY_VARS_UE *UE = (PHY_VARS_UE*)arg;

  UE->instance_cnt_tx=-1;

#ifdef RTAI
614
  RT_TASK *task = rt_task_init_schmod(nam2num("UE TX Thread"), 0, 0, 0, SCHED_FIFO, 0xF);
615

616 617 618 619
  if (task==NULL) {
    LOG_E(PHY,"[SCHED][UE] Problem starting UE TX thread!!!!\n");
    return 0;
  }
620

621 622 623 624
  LOG_D(HW,"Started UE TX thread (id %p)\n",task);
#else

#ifdef LOWLATENCY
625 626 627
  struct sched_attr attr;
  unsigned int flags = 0;

628 629 630
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
631
  attr.sched_priority = 0;
632

633
  /* This creates a 1ms reservation every 10ms period*/
634 635 636 637
  attr.sched_policy   = SCHED_DEADLINE;
  attr.sched_runtime  = 900000;  // each tx thread requires .5ms to finish its job
  attr.sched_deadline = 1000000; // each tx thread will finish within 1ms
  attr.sched_period   = 1000000; // each tx thread has a period of 1ms from the starting point
638

639 640

  if (sched_setattr(0, &attr, flags) < 0 ) {
knopp's avatar
knopp committed
641
    perror("[SCHED] UE_thread_tx thread: sched_setattr failed\n");
642
    return &UE_thread_tx_retval;
643
  }
644

645 646 647 648 649
#else
  struct sched_param sp;
  sp.sched_priority = sched_get_priority_max(SCHED_FIFO)-1;
  pthread_setschedparam(pthread_self(),SCHED_FIFO,&sp);

650 651
#endif
#endif
652

653 654 655 656
  printf("waiting for sync (UE_thread_tx)\n");

  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_thread_tx)\n");
657

658 659
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
660

661 662 663 664 665
  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex, waiting (UE_thread_tx)\n");

  printf("Starting UE TX thread\n");

666
  // Lock memory from swapping. This is a process wide call (not constraint to this thread).
667 668 669 670 671
  mlockall(MCL_CURRENT | MCL_FUTURE);

  while (!oai_exit) {

    if (pthread_mutex_lock(&UE->mutex_tx) != 0) {
672
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE TX\n" );
673
      exit_fun("nothing to add");
674 675
      return &UE_thread_tx_retval;
    }
676

677 678 679
    while (UE->instance_cnt_tx < 0) {
      // most of the time, the thread is waiting here
      pthread_cond_wait( &UE->cond_tx, &UE->mutex_tx );
680
    }
681

682 683 684 685 686
    if (pthread_mutex_unlock(&UE->mutex_tx) != 0) {
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE TX\n" );
      exit_fun("nothing to add");
      return &UE_thread_tx_retval;
    }
687
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_TX, 1 );
688

689
    if ((subframe_select( &UE->lte_frame_parms, UE->slot_tx>>1 ) == SF_UL) ||
690
        (UE->lte_frame_parms.frame_type == FDD)) {
691
      phy_procedures_UE_TX( UE, 0, 0, UE->mode, no_relay );
692
    }
693

694 695 696
    if ((subframe_select( &UE->lte_frame_parms, UE->slot_tx>>1 ) == SF_S) &&
        ((UE->slot_tx&1) == 1)) {
      phy_procedures_UE_S_TX( UE, 0, 0, no_relay );
697
    }
698

699 700 701 702 703 704 705 706 707 708 709
    UE->slot_tx += 2;

    if (UE->slot_tx >= 20) {
      UE->slot_tx -= 20;
      UE->frame_tx++;
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX_UE, UE->frame_tx );
    }

    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX_UE, UE->slot_tx>>1 );

    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_TX, 0 );
710

711
    if (pthread_mutex_lock(&UE->mutex_tx) != 0) {
712 713 714 715
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE TX thread\n" );
      exit_fun("nothing to add");
      return &UE_thread_tx_retval;
    }
716

717
    UE->instance_cnt_tx--;
718
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_TX, UE->instance_cnt_tx);
719 720 721 722 723

    if (pthread_mutex_unlock(&UE->mutex_tx) != 0) {
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE TX thread\n" );
      exit_fun("nothing to add");
      return &UE_thread_tx_retval;
724
    }
725

726
  }
727

728
  return &UE_thread_tx_retval;
729 730
}

731 732 733 734 735 736
/*!
 * \brief This is the UE receive thread.
 * This thread performs the phy_procedures_UE_RX() on every received slot.
 * \param arg is a pointer to a \ref PHY_VARS_UE structure.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
737

738
/*
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
#ifdef OAI_USRP
void rescale(int16_t *input,int length)
{
#if defined(__x86_64__) || defined(__i386__)
  __m128i *input128 = (__m128i *)input;
#elif defined(__arm__)
  int16x8_t *input128 = (int16x8_t *)input;
#endif
  int i;

  for (i=0; i<length>>2; i++) {
#if defined(__x86_64__) || defined(__i386__)
    input128[i] = _mm_srai_epi16(input128[i],4);
#elif defined(__arm__)
    input128[i] = vshrq_n_s16(input128[i],4);
#endif
  }
}
#endif
758
*/
759

760 761
static void *UE_thread_rx(void *arg)
{
762
  static int UE_thread_rx_retval;
763 764 765
  PHY_VARS_UE *UE = (PHY_VARS_UE*)arg;
  int i;
  int ret;
766

767 768 769
  UE->instance_cnt_rx=-1;

#ifdef RTAI
770
  RT_TASK *task = rt_task_init_schmod(nam2num("UE RX Thread"), 0, 0, 0, SCHED_FIFO, 0xF);
771

772 773
  if (task==NULL) {
    LOG_E(PHY,"[SCHED][UE] Problem starting UE RX thread!!!!\n");
774
    return &UE_thread_rx_retval;
775
  }
776

777 778
  LOG_D(HW,"Started UE RX thread (id %p)\n",task);
#else
779

780
#ifdef LOWLATENCY
781 782 783
  struct sched_attr attr;
  unsigned int flags = 0;

784 785 786
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
787
  attr.sched_priority = 0;
788

789
  // This creates a .5ms reservation every 1ms period
790 791 792 793
  attr.sched_policy   = SCHED_DEADLINE;
  attr.sched_runtime  = 900000;  // each rx thread requires 1ms to finish its job
  attr.sched_deadline = 1000000; // each rx thread will finish within 1ms
  attr.sched_period   = 1000000; // each rx thread has a period of 1ms from the starting point
794 795

  if (sched_setattr(0, &attr, flags) < 0 ) {
knopp's avatar
knopp committed
796
    perror("[SCHED] UE_thread_rx : sched_setattr failed\n");
797
    return &UE_thread_rx_retval;
798 799
  }

800 801 802 803 804
#else
  struct sched_param sp;
  sp.sched_priority = sched_get_priority_max(SCHED_FIFO)-1;
  pthread_setschedparam(pthread_self(),SCHED_FIFO,&sp);

805 806
#endif
#endif
807

808
  // Lock memory from swapping. This is a process wide call (not constraint to this thread).
809
  mlockall(MCL_CURRENT | MCL_FUTURE);
810

811
  printf("waiting for sync (UE_thread_rx)\n");
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
812

813 814
  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_thread_rx)\n");
815

816 817
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
818

819 820
  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex, waiting (UE_thread_rx)\n");
821

822
  printf("Starting UE RX thread\n");
823 824

  while (!oai_exit) {
825
    if (pthread_mutex_lock(&UE->mutex_rx) != 0) {
826
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE RX\n" );
827
      exit_fun("nothing to add");
828
      return &UE_thread_rx_retval;
829
    }
830 831

    while (UE->instance_cnt_rx < 0) {
832 833
      // most of the time, the thread is waiting here
      pthread_cond_wait( &UE->cond_rx, &UE->mutex_rx );
834 835 836
    }

    if (pthread_mutex_unlock(&UE->mutex_rx) != 0) {
837
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE RX\n" );
838
      exit_fun("nothing to add");
839
      return &UE_thread_rx_retval;
840 841
    }

842
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_RX, 1 );
843
    for (i=0; i<2; i++) {
844
      if ((subframe_select( &UE->lte_frame_parms, UE->slot_rx>>1 ) == SF_DL) ||
845
          (UE->lte_frame_parms.frame_type == FDD)) {
846
	/*
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
#ifdef OAI_USRP
	// this does the adjustments of RX signal amplitude to bring into least 12 significant bits
	int slot_length = UE->lte_frame_parms.samples_per_tti>>1;
	int rx_offset = (UE->slot_rx)*slot_length + UE->rx_offset;
	int frame_length = UE->lte_frame_parms.samples_per_tti*10;
	int aa;
	if (rx_offset > frame_length)
	  rx_offset-=frame_length;


	if (rx_offset >= 0) {
	  if (rx_offset + slot_length < frame_length)
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++)
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][rx_offset&(~0x3)],
		      slot_length);
	  else {
	    int diff = rx_offset + slot_length - frame_length;
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++){
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][rx_offset&(~0x3)],
		      slot_length-diff);
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][0],
		      diff);
	    }
	  }
	}
	else {
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++){
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][(frame_length+rx_offset)&(~0x3)],
		      -rx_offset);
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][0],
		      slot_length+rx_offset);
	    }
	}
#endif
881
	*/
882
        phy_procedures_UE_RX( UE, 0, 0, UE->mode, no_relay, NULL );
883
      }
884

885 886
      if ((subframe_select( &UE->lte_frame_parms, UE->slot_rx>>1 ) == SF_S) &&
          ((UE->slot_rx&1) == 0)) {
887
	/*
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
#ifdef OAI_USRP
	// this does the adjustments of RX signal amplitude to bring into least 12 significant bits
	int slot_length = UE->lte_frame_parms.samples_per_tti>>1;
	int rx_offset = (UE->slot_rx)*slot_length + UE->rx_offset;
	int frame_length = UE->lte_frame_parms.samples_per_tti*10;
	if (rx_offset > frame_length)
	  rx_offset-=frame_length;
	int aa;

	if (rx_offset >= 0) {
	  if (rx_offset + slot_length < frame_length)
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++)
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][rx_offset&(~0x3)],
		      slot_length);
	  else {
	    int diff = rx_offset + slot_length - frame_length;
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++){
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][rx_offset&(~0x3)],
		      slot_length-diff);
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][0],
		      diff);
	    }
	  }
	}
	else {
	  for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++){
	    rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][(frame_length+rx_offset)&(~0x3)],
		    -rx_offset);
	    rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][0],
		    slot_length+rx_offset);
	  }
	}
#endif
921
	*/
922
        phy_procedures_UE_RX( UE, 0, 0, UE->mode, no_relay, NULL );
923 924 925 926 927 928 929 930 931 932 933 934 935
      }

#ifdef OPENAIR2

      if (i==0) {
        ret = mac_xface->ue_scheduler(UE->Mod_id,
                                      UE->frame_tx,
                                      UE->slot_rx>>1,
                                      subframe_select(&UE->lte_frame_parms,UE->slot_tx>>1),
                                      0,
                                      0/*FIXME CC_id*/);

        if (ret == CONNECTION_LOST) {
936
          LOG_E( PHY, "[UE %"PRIu8"] Frame %"PRIu32", subframe %u RRC Connection lost, returning to PRACH\n",
937
                 UE->Mod_id, UE->frame_rx, UE->slot_tx>>1 );
938 939
          UE->UE_mode[0] = PRACH;
        } else if (ret == PHY_RESYNCH) {
940
          LOG_E( PHY, "[UE %"PRIu8"] Frame %"PRIu32", subframe %u RRC Connection lost, trying to resynch\n",
941
                 UE->Mod_id, UE->frame_rx, UE->slot_tx>>1 );
942 943
          UE->UE_mode[0] = RESYNCH;
        } else if (ret == PHY_HO_PRACH) {
944
          LOG_I( PHY, "[UE %"PRIu8"] Frame %"PRIu32", subframe %u, return to PRACH and perform a contention-free access\n",
945
                 UE->Mod_id, UE->frame_rx, UE->slot_tx>>1 );
946 947
          UE->UE_mode[0] = PRACH;
        }
948
      }
949 950 951 952

#endif
      UE->slot_rx++;

953 954
      if (UE->slot_rx == 20) {
        UE->slot_rx = 0;
955
        UE->frame_rx++;
956
        VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX_UE, UE->frame_rx );
957 958
      }

959
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_RX_UE, UE->slot_rx>>1 );
960
    }
961 962
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_RX, 0 );

963
    if (pthread_mutex_lock(&UE->mutex_rx) != 0) {
964 965 966
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE RX\n" );
      exit_fun("noting to add");
      return &UE_thread_rx_retval;
967 968
    }

969
    UE->instance_cnt_rx--;
970
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_RX, UE->instance_cnt_rx);
971 972 973 974 975 976

    if (pthread_mutex_unlock(&UE->mutex_rx) != 0) {
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE RX\n" );
      exit_fun("noting to add");
      return &UE_thread_rx_retval;
    }
977 978 979
  }

  // thread finished
980
  return &UE_thread_rx_retval;
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
981
}
982

983 984 985 986 987 988 989 990



#ifndef EXMIMO
#define RX_OFF_MAX 10
#define RX_OFF_MIN 5
#define RX_OFF_MID ((RX_OFF_MAX+RX_OFF_MIN)/2)

991 992 993 994 995 996 997 998 999
/*!
 * \brief This is the main UE thread.
 * This thread controls the other three UE threads:
 * - UE_thread_rx
 * - UE_thread_tx
 * - UE_thread_synch
 * \param arg unused
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
1000 1001
void *UE_thread(void *arg)
{
1002 1003 1004
  UNUSED(arg)
  static int UE_thread_retval;
  PHY_VARS_UE *UE = PHY_vars_UE_g[0][0];
1005
  int spp = openair0_cfg[0].samples_per_packet;
1006
  int slot=1, frame=0, hw_subframe=0, rxpos=0, txpos=openair0_cfg[0].tx_scheduling_advance;
1007 1008 1009 1010 1011
#ifdef __AVX2__
  int dummy[2][spp] __attribute__((aligned(32)));
#else
  int dummy[2][spp] __attribute__((aligned(16)));
#endif
1012
  int dummy_dump = 0;
1013 1014
  int tx_enabled = 0;
  int start_rx_stream = 0;
1015 1016
  int rx_off_diff = 0;
  int rx_correction_timer = 0;
1017
  int first_rx = 0;
1018
  RTIME T0;
1019
  unsigned int rxs;
1020 1021 1022

  openair0_timestamp timestamp;

1023 1024 1025 1026
#ifdef NAS_UE
  MessageDef *message_p;
#endif

1027
#ifdef RTAI
1028
  RT_TASK *task = rt_task_init_schmod(nam2num("UE thread"), 0, 0, 0, SCHED_FIFO, 0xF);
1029

1030 1031 1032 1033
  if (task==NULL) {
    LOG_E(PHY,"[SCHED][UE] Problem starting UE thread!!!!\n");
    return 0;
  }
1034

1035
#else
1036

1037
#ifdef LOWLATENCY
1038 1039 1040
  struct sched_attr attr;
  unsigned int flags = 0;

1041 1042 1043
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
1044
  attr.sched_priority = 0;//sched_get_priority_max(SCHED_DEADLINE);
1045

1046 1047
  // This creates a .5 ms  reservation
  attr.sched_policy = SCHED_DEADLINE;
1048 1049 1050
  attr.sched_runtime  = 100000;
  attr.sched_deadline = 500000;
  attr.sched_period   = 500000;
1051 1052

  if (sched_setattr(0, &attr, flags) < 0 ) {
1053 1054
    perror("[SCHED] main eNB thread: sched_setattr failed\n");
    exit_fun("Nothing to add");
1055
    return &UE_thread_retval;
1056
  }
1057 1058 1059
  LOG_I(HW,"[SCHED][eNB] eNB main deadline thread %lu started on CPU %d\n",
        (unsigned long)gettid(), sched_getcpu());

1060 1061 1062 1063
#else
  struct sched_param sp;
  sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
  pthread_setschedparam(pthread_self(),SCHED_FIFO,&sp);
1064 1065 1066
#endif
#endif

1067
  // Lock memory from swapping. This is a process wide call (not constraint to this thread).
1068 1069 1070 1071 1072
  mlockall(MCL_CURRENT | MCL_FUTURE);

  printf("waiting for sync (UE_thread)\n");
  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_thread)\n");
1073

1074 1075
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
1076

1077 1078 1079 1080 1081
  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex, waiting (UE_thread)\n");

  printf("starting UE thread\n");

1082 1083 1084 1085 1086
#ifdef NAS_UE
  message_p = itti_alloc_new_message(TASK_NAS_UE, INITIALIZE_MESSAGE);
  itti_send_msg_to_task (TASK_NAS_UE, INSTANCE_DEFAULT, message_p);
#endif

1087 1088
  T0 = rt_get_time_ns();
  first_rx = 1;
1089
  rxpos=0;
1090

1091
  while (!oai_exit) {
1092 1093
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_HW_SUBFRAME, hw_subframe );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_HW_FRAME, frame );
1094
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_DUMMY_DUMP, dummy_dump );
1095

1096

1097
    while (rxpos < (1+hw_subframe)*UE->lte_frame_parms.samples_per_tti) {
1098
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 1 );
1099

1100 1101
#ifndef USRP_DEBUG

1102 1103
      DevAssert( UE->lte_frame_parms.nb_antennas_rx <= 2 );
      void* rxp[2];
1104

1105
      for (int i=0; i<UE->lte_frame_parms.nb_antennas_rx; i++)
1106
        rxp[i] = (dummy_dump==0) ? (void*)&rxdata[i][rxpos] : (void*)dummy[i];
1107 1108
      /*
      if (dummy_dump == 0)
1109 1110
      	printf("writing %d samples to %d (first_rx %d)\n",spp - ((first_rx==1) ? rx_off_diff : 0),rxpos,first_rx);
      */
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
      if (UE->mode != loop_through_memory) {
	rxs = openair0.trx_read_func(&openair0,
				     &timestamp,
				     rxp,
				     spp - ((first_rx==1) ? rx_off_diff : 0),
				     UE->lte_frame_parms.nb_antennas_rx);

	if (rxs != (spp- ((first_rx==1) ? rx_off_diff : 0))) {
	  exit_fun("problem in rx");
	  return &UE_thread_retval;
	}
1122
      }
1123

1124
      if (rx_off_diff !=0)
1125
	LOG_D(PHY,"frame %d, rx_offset %d, rx_off_diff %d\n",UE->frame_rx,UE->rx_offset,rx_off_diff);
1126

1127
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 0 );
1128

1129
      // Transmit TX buffer based on timestamp from RX
1130
      if ((tx_enabled==1) && (UE->mode!=loop_through_memory)) {
1131
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 1 );
1132

1133 1134
        DevAssert( UE->lte_frame_parms.nb_antennas_tx <= 2 );
        void* txp[2];
1135

1136
        for (int i=0; i<UE->lte_frame_parms.nb_antennas_tx; i++)
1137 1138 1139
          txp[i] = (void*)&txdata[i][txpos];

        openair0.trx_write_func(&openair0,
Raymond Knopp's avatar
Raymond Knopp committed
1140
                                (timestamp+openair0_cfg[0].tx_scheduling_advance-openair0_cfg[0].tx_sample_advance),
1141
                                txp,
1142
				spp - ((first_rx==1) ? rx_off_diff : 0),
1143 1144 1145
                                UE->lte_frame_parms.nb_antennas_tx,
                                1);

1146
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 0 );
1147
      }
1148 1149
      else if (UE->mode == loop_through_memory)
	rt_sleep_ns(1000000);
1150
#else
1151
      // define USRP_DEBUG is active
1152 1153
      rt_sleep_ns(1000000);
#endif
1154

1155 1156 1157
      rx_off_diff = 0;
      first_rx = 0;

1158 1159 1160 1161
      rxpos += spp;
      txpos += spp;

      if (txpos >= 10*PHY_vars_UE_g[0][0]->lte_frame_parms.samples_per_tti)
1162
        txpos -= 10*PHY_vars_UE_g[0][0]->lte_frame_parms.samples_per_tti;
1163
    }
1164

1165
    if (rxpos >= 10*PHY_vars_UE_g[0][0]->lte_frame_parms.samples_per_tti)
1166 1167
      rxpos -= 10*PHY_vars_UE_g[0][0]->lte_frame_parms.samples_per_tti;

1168 1169
    if (UE->is_synchronized == 1)  {
      LOG_D( HW, "UE_thread: hw_frame %d, hw_subframe %d (time %lli)\n", frame, hw_subframe, rt_get_time_ns()-T0 );
1170

1171
      if (start_rx_stream == 1) {
1172
	LOG_D(PHY,"Locking mutex_rx (IC %d)\n",UE->instance_cnt_rx);
1173
        if (pthread_mutex_lock(&UE->mutex_rx) != 0) {
1174
          LOG_E( PHY, "[SCHED][UE] error locking mutex for UE RX thread\n" );
1175
          exit_fun("nothing to add");
1176 1177
          return &UE_thread_retval;
        }
1178

1179
        int instance_cnt_rx = ++UE->instance_cnt_rx;
1180

1181
	LOG_D(PHY,"Unlocking mutex_rx (IC %d)\n",instance_cnt_rx);
1182 1183 1184 1185 1186
        if (pthread_mutex_unlock(&UE->mutex_rx) != 0) {
          LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE RX thread\n" );
          exit_fun("nothing to add");
          return &UE_thread_retval;
        }
1187

1188 1189 1190
	VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_RX, instance_cnt_rx);


1191
        if (instance_cnt_rx == 0) {
1192
	  LOG_D(HW,"signalling rx thread to wake up, hw_frame %d, hw_subframe %d (time %lli)\n", frame, hw_subframe, rt_get_time_ns()-T0 );
1193 1194 1195 1196 1197
          if (pthread_cond_signal(&UE->cond_rx) != 0) {
            LOG_E( PHY, "[SCHED][UE] ERROR pthread_cond_signal for UE RX thread\n" );
            exit_fun("nothing to add");
            return &UE_thread_retval;
          }
1198
	  
1199
	  LOG_D(HW,"signalled rx thread to wake up, hw_frame %d, hw_subframe %d (time %lli)\n", frame, hw_subframe, rt_get_time_ns()-T0 );
1200 1201 1202 1203
	  if (UE->mode == loop_through_memory) {
	    printf("Processing subframe %d",UE->slot_rx>>1);
	    getchar();
	  }
1204

1205 1206 1207
          if (UE->mode == rx_calib_ue) {
            if (frame == 10) {
              LOG_D(PHY,
1208
                    "[SCHED][UE] Found cell with N_RB_DL %"PRIu8", PHICH CONFIG (%d,%d), Nid_cell %"PRIu16", NB_ANTENNAS_TX %"PRIu8", frequency offset "PRIi32" Hz, RSSI (digital) %hu dB, measured Gain %d dB, total_rx_gain %"PRIu32" dB, USRP rx gain %f dB\n",
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
                    UE->lte_frame_parms.N_RB_DL,
                    UE->lte_frame_parms.phich_config_common.phich_duration,
                    UE->lte_frame_parms.phich_config_common.phich_resource,
                    UE->lte_frame_parms.Nid_cell,
                    UE->lte_frame_parms.nb_antennas_tx_eNB,
                    UE->lte_ue_common_vars.freq_offset,
                    UE->PHY_measurements.rx_power_avg_dB[0],
                    UE->PHY_measurements.rx_power_avg_dB[0] - rx_input_level_dBm,
                    UE->rx_total_gain_dB,
                    openair0_cfg[0].rx_gain[0]
1219
                   );
1220 1221
              exit_fun("[HW][UE] UE in RX calibration mode, exiting");
              return &UE_thread_retval;
1222 1223
            }
          }
1224
        } else {
1225 1226 1227 1228 1229
          LOG_E( PHY, "[SCHED][UE] UE RX thread busy (IC %d)!!\n", instance_cnt_rx);
	  if (instance_cnt_rx > 1) {
	    exit_fun("instance_cnt_rx > 1");
	    return &UE_thread_retval;
	  }
1230 1231
        }

1232 1233
       
        if ((tx_enabled==1)&&(UE->mode != loop_through_memory)) {
1234 1235 1236 1237 1238 1239 1240

	  if (pthread_mutex_lock(&UE->mutex_tx) != 0) {
	    LOG_E( PHY, "[SCHED][UE] error locking mutex for UE TX thread\n" );
	    exit_fun("nothing to add");
	    return &UE_thread_retval;
	  }

1241

1242 1243 1244 1245 1246 1247 1248
          int instance_cnt_tx = ++UE->instance_cnt_tx;

          if (pthread_mutex_unlock(&UE->mutex_tx) != 0) {
            LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE TX thread\n" );
            exit_fun("nothing to add");
            return &UE_thread_retval;
          }
1249 1250
	  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_TX, instance_cnt_tx);

1251 1252 1253 1254

          if (instance_cnt_tx == 0) {
            if (pthread_cond_signal(&UE->cond_tx) != 0) {
              LOG_E( PHY, "[SCHED][UE] ERROR pthread_cond_signal for UE TX thread\n" );
1255
              exit_fun("nothing to add");
1256
              return &UE_thread_retval;
1257
            }
1258
	    LOG_D(HW,"signalled tx thread to wake up, hw_frame %d, hw_subframe %d (time %lli)\n", frame, hw_subframe, rt_get_time_ns()-T0 );
1259

1260
          } else {
1261 1262 1263 1264 1265
            LOG_E( PHY, "[SCHED][UE] UE TX thread busy (IC %d)!!\n" );
	    if (instance_cnt_tx>1) {
	      exit_fun("instance_cnt_tx > 1");
	      return &UE_thread_retval;
	    }
1266 1267
          }
        }
1268

1269
      }
1270 1271 1272
    } else {
      // we are not yet synchronized
      if ((hw_subframe == 9) && (dummy_dump == 0)) {
1273 1274
        // Wake up initial synch thread
        if (pthread_mutex_lock(&UE->mutex_synch) != 0) {
1275
          LOG_E( PHY, "[SCHED][UE] error locking mutex for UE initial synch thread\n" );
1276
          exit_fun("nothing to add");
1277 1278
          return &UE_thread_retval;
        }
1279

1280
        int instance_cnt_synch = ++UE->instance_cnt_synch;
1281

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
        if (pthread_mutex_unlock(&UE->mutex_synch) != 0) {
          LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE initial synch thread\n" );
          exit_fun("nothing to add");
          return &UE_thread_retval;
        }

        dummy_dump = 1;

        if (instance_cnt_synch == 0) {
          if (pthread_cond_signal(&UE->cond_synch) != 0) {
            LOG_E( PHY, "[SCHED][UE] ERROR pthread_cond_signal for UE sync thread\n" );
1293
            exit_fun("nothing to add");
1294
            return &UE_thread_retval;
1295
          }
1296 1297 1298 1299
        } else {
          LOG_E( PHY, "[SCHED][UE] UE sync thread busy!!\n" );
          exit_fun("nothing to add");
          return &UE_thread_retval;
1300
        }
1301 1302
      }
    }
1303

1304 1305
    hw_subframe++;
    slot+=2;
1306

1307
    if (hw_subframe==10) {
1308 1309 1310 1311
      hw_subframe = 0;
      first_rx = 1;
      frame++;
      slot = 1;
1312

1313 1314 1315
      int fail = pthread_mutex_lock(&UE->mutex_synch);
      int instance_cnt_synch = UE->instance_cnt_synch;
      fail = fail || pthread_mutex_unlock(&UE->mutex_synch);
1316

1317 1318 1319 1320 1321 1322 1323 1324
      if (fail) {
        LOG_E( PHY, "[SCHED][UE] error (un-)locking mutex for UE synch\n" );
        exit_fun("noting to add");
        return &UE_thread_retval;
      }

      if (instance_cnt_synch < 0) {
        // the UE_thread_synch is ready
1325 1326
        if (UE->is_synchronized == 1) {
          rx_off_diff = 0;
1327
          LTE_DL_FRAME_PARMS *frame_parms = &UE->lte_frame_parms; // for macro FRAME_LENGTH_COMPLEX_SAMPLES
1328

1329
	  //	  LOG_I(PHY,"UE->rx_offset %d\n",UE->rx_offset);
1330
          if ((UE->rx_offset > RX_OFF_MAX) && (start_rx_stream == 0)) {
1331 1332 1333
            start_rx_stream=1;
            frame=0;
            // dump ahead in time to start of frame
1334 1335

#ifndef USRP_DEBUG
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
	    if (UE->mode != loop_through_memory) {
	      rxs = openair0.trx_read_func(&openair0,
					   &timestamp,
					   (void**)rxdata,
					   UE->rx_offset,
					   UE->lte_frame_parms.nb_antennas_rx);
	      if (rxs != UE->rx_offset) {
		exit_fun("problem in rx");
		return &UE_thread_retval;
	      }
	      UE->rx_offset=0;
	      tx_enabled = 1;
1348
	    }
1349 1350
	    else
	      rt_sleep_ns(1000000);
1351
#else
1352
            rt_sleep_ns(10000000);
1353
#endif
1354

1355 1356 1357 1358
          } else if ((UE->rx_offset<(FRAME_LENGTH_COMPLEX_SAMPLES/2)) &&
		     (UE->rx_offset > RX_OFF_MIN) && 
		     (start_rx_stream==1) && 
		     (rx_correction_timer == 0)) {
1359
            rx_off_diff = -UE->rx_offset + RX_OFF_MIN;
1360
	    LOG_D(PHY,"UE->rx_offset %d > %d, diff %d\n",UE->rx_offset,RX_OFF_MIN,rx_off_diff);
1361
            rx_correction_timer = 5;
1362 1363 1364 1365 1366
          } else if ((UE->rx_offset>(FRAME_LENGTH_COMPLEX_SAMPLES/2)) && 
		     (UE->rx_offset < (FRAME_LENGTH_COMPLEX_SAMPLES-RX_OFF_MIN)) &&
		     (start_rx_stream==1) && 
		     (rx_correction_timer == 0)) {   // moving to the left so drop rx_off_diff samples
            rx_off_diff = FRAME_LENGTH_COMPLEX_SAMPLES - RX_OFF_MIN - UE->rx_offset;
1367
	    LOG_D(PHY,"UE->rx_offset %d < %d, diff %d\n",UE->rx_offset,FRAME_LENGTH_COMPLEX_SAMPLES-RX_OFF_MIN,rx_off_diff);
1368

1369 1370 1371 1372 1373 1374 1375 1376
            rx_correction_timer = 5;
          }

          if (rx_correction_timer>0)
            rx_correction_timer--;
        }

        dummy_dump=0;
1377 1378
      }
    }
1379

1380 1381 1382 1383
#if defined(ENABLE_ITTI)
    itti_update_lte_time(frame, slot);
#endif
  }
1384

1385
  return &UE_thread_retval;
1386 1387 1388 1389 1390 1391 1392
}
#endif



#ifdef EXMIMO
/* This is the main UE thread. Initially it is doing a periodic get_frame. One synchronized it gets woken up by the kernel driver using the RTAI message mechanism (rt_send and rt_receive). */
1393 1394
void *UE_thread(void *arg)
{
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
  PHY_VARS_UE *UE=PHY_vars_UE_g[0][0];
#ifdef RTAI
  RT_TASK *task;
#endif
  // RTIME in, out, diff;
  int slot=0,frame=0,hw_slot,last_slot,next_slot;
  // unsigned int aa;
  int delay_cnt;
  RTIME time_in;
  int hw_slot_offset=0,rx_offset_mbox=0,mbox_target=0,mbox_current=0;
  int diff2;
  int i, ret;
  int CC_id,card;
  volatile unsigned int *DAQ_MBOX = openair0_daq_cnt();

  int wait_sync_cnt = 0;
  int first_synch = 1;
#ifdef LOWLATENCY
  struct sched_attr attr;
  unsigned int flags = 0;
1415
  //  unsigned long mask = 1; // processor 0
1416
#endif
1417
  int freq_offset;
1418 1419 1420 1421


#ifdef RTAI
  task = rt_task_init_schmod(nam2num("UE thread"), 0, 0, 0, SCHED_FIFO, 0xF);
1422

1423 1424 1425 1426
  if (task==NULL) {
    LOG_E(PHY,"[SCHED][UE] Problem starting UE thread!!!!\n");
    return 0;
  }
1427

1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
#endif


#ifdef HARD_RT
  rt_make_hard_real_time();
#endif


#ifdef LOWLATENCY
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
1440
  attr.sched_priority = 0;
1441

1442 1443
  // This creates a .25 ms  reservation
  attr.sched_policy = SCHED_DEADLINE;
1444 1445 1446
  attr.sched_runtime  = (0.1  * 100) * 10000;
  attr.sched_deadline = (0.25 * 100) * 10000;
  attr.sched_period   = (0.5  * 100) * 10000;
1447

1448 1449 1450 1451
  // pin the UE main thread to CPU0
  // if (pthread_setaffinity_np(pthread_self(), sizeof(mask),&mask) <0) {
  //   perror("[MAIN_ENB_THREAD] pthread_setaffinity_np failed\n");
  //   }
1452 1453

  if (sched_setattr(0, &attr, flags) < 0 ) {
1454 1455 1456 1457
    perror("[SCHED] main UE thread: sched_setattr failed\n");
    exit_fun("Nothing to add");
  } else {
    LOG_I(HW,"[SCHED][eNB] eNB main deadline thread %ld started on CPU %d\n",
1458
          gettid(),sched_getcpu());
1459
  }
1460

1461
#endif
1462

1463 1464 1465 1466 1467 1468 1469

  mlockall(MCL_CURRENT | MCL_FUTURE);

  printf("waiting for sync (UE_thread)\n");

  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_thread)\n");
1470

1471 1472
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
1473

1474 1475 1476 1477 1478
  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex, waiting (UE_thread)\n");

  printf("starting UE thread\n");

1479
  freq_offset = 0; //-7500;
1480 1481 1482 1483 1484 1485 1486

  first_synch = 1;

  while (!oai_exit)  {

    hw_slot = (((((volatile unsigned int *)DAQ_MBOX)[0]+1)%150)<<1)/15; //the slot the hw is about to store

1487

1488 1489 1490
    if (UE->is_synchronized) {

      if (first_synch == 1) {
1491 1492 1493
        first_synch = 0;

        for (card=0; card<openair0_num_detected_cards; card++)
1494
          openair0_start_rt_acquisition(card);
1495 1496

        rt_sleep_ns(FRAME_PERIOD/10);
1497 1498 1499 1500
      }

      //this is the mbox counter that indicates the start of the frame
      rx_offset_mbox = (UE->rx_offset * 150) / (10*UE->lte_frame_parms.samples_per_tti);
1501 1502
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_RX_OFFSET, UE->rx_offset);
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_OFFSET_MBOX, rx_offset_mbox);
1503 1504 1505 1506 1507 1508
      //this is the mbox counter where we should be
      mbox_target = (((((slot+1)%20)*15+1)>>1) + rx_offset_mbox + 1)%150;
      // round up to the next multiple of two (mbox counter from express MIMO gives only even numbers)
      mbox_target = ((mbox_target+1)-((mbox_target-1)%2))%150;
      //this is the mbox counter where we are
      mbox_current = ((volatile unsigned int *)DAQ_MBOX)[0];
1509

1510 1511
      //this is the time we need to sleep in order to synchronize with the hw (in multiples of DAQ_PERIOD)
      if ((mbox_current>=120) && (mbox_target<30)) //handle the frame wrap-arround
1512
        diff2 = 150-mbox_current+mbox_target;
1513
      else if ((mbox_current<30) && (mbox_target>=120))
1514
        diff2 = -150+mbox_target-mbox_current;
1515
      else
1516 1517
        diff2 = mbox_target - mbox_current;

1518
      if (diff2 <(-7)) {
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
        LOG_D(HW,"UE Frame %d: missed slot, proceeding with next one (slot %d, hw_slot %d, diff %d)\n",frame, slot, hw_slot, diff2);

        if (frame>0) {
          if (exit_missed_slots==1)
            exit_fun("[HW][UE] missed slot");
          else {
            num_missed_slots++;
            LOG_W(HW,"[UE] just missed slot (total missed slots %ld)\n", num_missed_slots);
          }
        }

1530
        slot++;
1531

1532 1533 1534 1535
        if (slot==20) {
          slot=0;
          frame++;
        }
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551

        // update thread slot/frame counters because of skipped slot
        UE->slot_rx++;
        UE->slot_tx++;

        if (UE->slot_rx == 20) {
          UE->slot_rx = 0;
          UE->frame_rx++;
        }

        if (UE->slot_tx == 20) {
          UE->slot_tx = 0;
          UE->frame_tx++;
        }

        continue;
1552
      }
1553

1554
      if (diff2>8)
1555 1556 1557
        LOG_D(HW,"UE Frame %d: skipped slot, waiting for hw to catch up (slot %d, hw_slot %d, mbox_current %d, mbox_target %d, diff %d)\n",frame, slot, hw_slot, mbox_current, mbox_target, diff2);


1558 1559
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX, *DAQ_MBOX);
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DIFF, diff2);
1560

1561 1562 1563 1564


      // This loop implements the delay of 1 slot to allow for processing
      delay_cnt = 0;
1565

1566 1567 1568
      while ((diff2>0) && (!oai_exit) )  {
        time_in = rt_get_time_ns();
        //LOG_D(HW,"eNB Frame %d delaycnt %d : hw_slot %d (%d), slot %d (%d), diff %d, time %llu\n",frame,delay_cnt,hw_slot,((volatile unsigned int *)DAQ_MBOX)[0],slot,mbox_target,diff2,time_in);
1569 1570
        VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX, *DAQ_MBOX);
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RT_SLEEP,1);
1571
        ret = rt_sleep_ns(diff2*DAQ_PERIOD);
1572 1573
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RT_SLEEP,0);
        VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX, *DAQ_MBOX);
1574

1575 1576
        if (ret)
          LOG_D(HW,"eNB Frame %d, time %llu: rt_sleep_ns returned %d\n",frame, time_in);
1577

1578 1579 1580
        hw_slot = (((((volatile unsigned int *)DAQ_MBOX)[0]+1)%150)<<1)/15;
        //LOG_D(HW,"eNB Frame %d : hw_slot %d, time %llu\n",frame,hw_slot,rt_get_time_ns());
        delay_cnt++;
1581

1582 1583 1584 1585
        if (delay_cnt == 30)  {
          LOG_D(HW,"UE frame %d: HW stopped ... \n",frame);
          exit_fun("[HW][UE] HW stopped");
        }
1586

1587
        mbox_current = ((volatile unsigned int *)DAQ_MBOX)[0];
1588

1589 1590 1591 1592 1593 1594
        if ((mbox_current>=135) && (mbox_target<15)) //handle the frame wrap-arround
          diff2 = 150-mbox_current+mbox_target;
        else if ((mbox_current<15) && (mbox_target>=135))
          diff2 = -150+mbox_target-mbox_current;
        else
          diff2 = mbox_target - mbox_current;
1595

1596 1597
        VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX, *DAQ_MBOX);
        VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DIFF, diff2);
1598 1599 1600 1601
      }

      // on even slots, schedule processing of entire subframe
      if ((slot&1) == 0)  {
1602 1603 1604 1605 1606 1607

        if (pthread_mutex_lock(&UE->mutex_rx) != 0) {
          LOG_E(PHY,"[SCHED][UE] error locking mutex for UE RX thread\n");
          exit_fun("nothing to add");
        } else {

1608 1609
	  int instance_cnt_rx = ++UE->instance_cnt_rx;

1610
          pthread_mutex_unlock(&UE->mutex_rx);
1611 1612
	  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_RX, instance_cnt_rx);

1613

1614
          if (instance_cnt_rx == 0) {
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
            LOG_D(HW,"Scheduling UE RX for frame %d (hw frame %d), subframe %d (%d), mode %d\n",UE->frame_rx,frame,slot>>1,UE->slot_rx>>1,UE->mode);

            if (pthread_cond_signal(&UE->cond_rx) != 0) {
              LOG_E(PHY,"[SCHED][UE] ERROR pthread_cond_signal for UE RX thread\n");
              exit_fun("nothing to add");
            } else {
              //        printf("UE_thread: cond_signal for RX ok (%p) @ %llu\n",(void*)&UE->cond_rx,rt_get_time_ns()-T0);
            }

            if (UE->mode == rx_calib_ue) {
              if (frame == 10) {
                LOG_D(PHY,
                      "[SCHED][UE] Found cell with N_RB_DL %d, PHICH CONFIG (%d,%d), Nid_cell %d, NB_ANTENNAS_TX %d, initial frequency offset %d Hz, frequency offset %d Hz, RSSI (digital) %d dB, measured Gain %d dB, total_rx_gain %d dB, USRP rx gain %f dB\n",
                      UE->lte_frame_parms.N_RB_DL,
                      UE->lte_frame_parms.phich_config_common.phich_duration,
                      UE->lte_frame_parms.phich_config_common.phich_resource,
                      UE->lte_frame_parms.Nid_cell,
                      UE->lte_frame_parms.nb_antennas_tx_eNB,
1633
                      freq_offset,
1634 1635 1636 1637 1638
                      UE->lte_ue_common_vars.freq_offset,
                      UE->PHY_measurements.rx_power_avg_dB[0],
                      UE->PHY_measurements.rx_power_avg_dB[0] - rx_input_level_dBm,
                      UE->rx_total_gain_dB,
                      openair0_cfg[0].rx_gain[0]
1639
                     );
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
                exit_fun("[HW][UE] UE in RX calibration mode, exiting");
              }
            }
          } else {
            LOG_E(PHY,"[SCHED][UE] UE RX thread busy!!\n");
            exit_fun("nothing to add");
          }
        }

        if (pthread_mutex_lock(&UE->mutex_tx) != 0) {
          LOG_E(PHY,"[SCHED][UE] error locking mutex for UE TX thread\n");
          exit_fun("nothing to add");
        } else {

1654 1655
          int instance_cnt_tx = ++UE->instance_cnt_tx;

1656
          pthread_mutex_unlock(&UE->mutex_tx);
1657
	  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_TX, instance_cnt_tx);
1658

1659
          if (instance_cnt_tx == 0) {
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
            LOG_D(HW,"Scheduling UE TX for frame %d (hw frame %d), subframe %d (%d), mode %d\n",UE->frame_tx,frame,slot>>1,UE->slot_tx>>1,UE->mode);

            if (pthread_cond_signal(&UE->cond_tx) != 0) {
              LOG_E(PHY,"[SCHED][UE] ERROR pthread_cond_signal for UE TX thread\n");
              exit_fun("nothing to add");
            } else {
              //        printf("UE_thread: cond_signal for RX ok (%p) @ %llu\n",(void*)&UE->cond_rx,rt_get_time_ns()-T0);
            }
          } else {
            LOG_E(PHY,"[SCHED][UE] UE TX thread busy!!\n");
            exit_fun("nothing to add");
          }
        }
1673 1674 1675
      }

      /*
1676 1677
      if ((slot%2000)<10)
      LOG_D(HW,"fun0: doing very hard work\n");
1678 1679 1680
      */
      // now increment slot and frame counters
      slot++;
1681

1682
      if (slot==20) {
1683 1684
        slot=0;
        frame++;
1685
      }
1686
    } else if (UE->is_synchronized == 0) { // we are not yet synchronized
1687 1688 1689 1690 1691 1692
      hw_slot_offset = 0;
      first_synch = 1;
      slot = 0;


      // wait until we can lock mutex_synch
jiangx's avatar
jiangx committed
1693
      //printf("Locking mutex_synch (UE_thread)\n");
1694
      if (pthread_mutex_lock(&UE->mutex_synch) != 0) {
1695 1696 1697
        LOG_E(PHY,"[SCHED][UE] error locking mutex for UE initial synch thread\n");
        exit_fun("noting to add");
      } else {
1698

1699 1700 1701
        if (UE->instance_cnt_synch < 0) {

          wait_sync_cnt=0;
1702 1703
	  openair0_config(&openair0_cfg[0],1);
      //  openair0_set_gains(&openair0,&openair0_cfg[0]);
1704

1705
	  printf("Getting frame\n");
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
          openair0_get_frame(0);
          rt_sleep_ns(FRAME_PERIOD);
          // increment instance count for sync thread
          UE->instance_cnt_synch++;
          pthread_mutex_unlock(&UE->mutex_synch);

          if (pthread_cond_signal(&UE->cond_synch) != 0) {
            LOG_E(PHY,"[SCHED][UE] ERROR pthread_cond_signal for UE sync thread\n");
            exit_fun("nothing to add");
          }
        } else {
          wait_sync_cnt++;
          pthread_mutex_unlock(&UE->mutex_synch);

          if (wait_sync_cnt>1000)
            exit_fun("waiting to long for synch thread");
          else
            rt_sleep_ns(FRAME_PERIOD);
        }
1725
      }
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743


      /*
        if (initial_sync(UE,mode)==0) {

        if (mode == rx_calib_ue) {
        exit_fun("[HW][UE] UE in RX calibration mode");
        }
        else {
        is_synchronized = 1;
        //start the streaming DMA transfers
        for (card=0;card<openair0_num_detected_cards;card++)
        openair0_start_rt_acquisition(card);

        hw_slot_offset = (UE->rx_offset<<1) / UE->lte_frame_parms.samples_per_tti;
        }
        }
        else {
1744 1745 1746
        if (freq_offset >= 0) {
        freq_offset += 100;
        freq_offset *= -1;
1747 1748
        }
        else {
1749
        freq_offset *= -1;
1750
        }
1751
        if (abs(freq_offset) > 7500) {
1752 1753 1754 1755
        LOG_I(PHY,"[initial_sync] No cell synchronization found, abondoning\n");
        mac_xface->macphy_exit("No cell synchronization found, abondoning");
        }
        else {
1756
        //    LOG_I(PHY,"[initial_sync] trying carrier off %d Hz\n",freq_offset);
1757 1758 1759
        #ifndef USRP
        for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
        for (i=0; i<openair0_cfg[rf_map[CC_id].card].rx_num_channels; i++)
1760
        openair0_cfg[rf_map[CC_id].card].rx_freq[rf_map[CC_id].chain+i] = downlink_frequency[CC_id][i]+freq_offset;
1761
        for (i=0; i<openair0_cfg[rf_map[CC_id].card].tx_num_channels; i++)
1762
        openair0_cfg[rf_map[CC_id].card].tx_freq[rf_map[CC_id].chain+i] = downlink_frequency[CC_id][i]+freq_offset;
1763 1764 1765 1766 1767 1768
        }
        openair0_config(&openair0_cfg[0],UE_flag);
        #endif
        rt_sleep_ns(FRAME_PERIOD);
        }
        }
1769 1770 1771 1772 1773
      */
    }
  }

  LOG_D(HW,"UE_thread: finished, ran %d times.\n",frame);
1774

1775 1776 1777
#ifdef HARD_RT
  rt_make_soft_real_time();
#endif
1778

1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
  // clean task
#ifdef RTAI
  rt_task_delete(task);
#endif
  LOG_D(HW,"Task deleted. returning\n");
  return 0;
}

#else  // This is for USRP or ETHERNET targets

#endif


1792 1793 1794 1795 1796 1797 1798 1799
/*!
 * \brief Initialize the UE theads.
 * Creates the UE threads:
 * - UE_thread_tx
 * - UE_thread_rx
 * - UE_thread_synch
 * and the locking between them.
 */
1800 1801
void init_UE_threads(void)
{
1802
  PHY_VARS_UE *UE = PHY_vars_UE_g[0][0];
1803

1804 1805 1806 1807
  // the threads are not yet active, therefore access is allowed without locking
  UE->instance_cnt_tx = -1;
  UE->instance_cnt_rx = -1;
  UE->instance_cnt_synch = -1;
1808 1809 1810 1811 1812 1813 1814
  pthread_mutex_init(&UE->mutex_tx,NULL);
  pthread_mutex_init(&UE->mutex_rx,NULL);
  pthread_mutex_init(&UE->mutex_synch,NULL);
  pthread_cond_init(&UE->cond_tx,NULL);
  pthread_cond_init(&UE->cond_rx,NULL);
  pthread_cond_init(&UE->cond_synch,NULL);
  pthread_create(&UE->thread_tx,NULL,UE_thread_tx,(void*)UE);
1815
  pthread_setname_np( UE->thread_tx, "UE_thread_tx" );
1816
  pthread_create(&UE->thread_rx,NULL,UE_thread_rx,(void*)UE);
1817 1818 1819
  pthread_setname_np( UE->thread_rx, "UE_thread_rx" );
  pthread_create(&UE->thread_synch,NULL,UE_thread_synch,(void*)UE);
  pthread_setname_np( UE->thread_synch, "UE_thread_synch" );
1820 1821 1822 1823 1824
  UE->frame_tx = 0;
  UE->frame_rx = 0;
}


Raymond Knopp's avatar
 
Raymond Knopp committed
1825
#ifdef OPENAIR2
1826 1827
void fill_ue_band_info(void)
{
1828 1829 1830 1831 1832 1833

  UE_EUTRA_Capability_t *UE_EUTRA_Capability = UE_rrc_inst[0].UECap->UE_EUTRA_Capability;
  int i,j;

  bands_to_scan.nbands = UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.count;

1834 1835 1836
  for (i=0; i<bands_to_scan.nbands; i++) {

    for (j=0; j<sizeof (eutra_bands) / sizeof (eutra_bands[0]); j++)
1837
      if (eutra_bands[j].band == UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.array[i]->bandEUTRA) {
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
        memcpy(&bands_to_scan.band_info[i],
               &eutra_bands[j],
               sizeof(eutra_band_t));

        printf("Band %d (%lu) : DL %u..%u Hz, UL %u..%u Hz, Duplex %s \n",
               bands_to_scan.band_info[i].band,
               UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.array[i]->bandEUTRA,
               bands_to_scan.band_info[i].dl_min,
               bands_to_scan.band_info[i].dl_max,
               bands_to_scan.band_info[i].ul_min,
               bands_to_scan.band_info[i].ul_max,
               (bands_to_scan.band_info[i].frame_type==FDD) ? "FDD" : "TDD");
        break;
1851 1852 1853
      }
  }
}
Raymond Knopp's avatar
 
Raymond Knopp committed
1854
#endif
1855 1856 1857 1858

int setup_ue_buffers(PHY_VARS_UE **phy_vars_ue, openair0_config_t *openair0_cfg, openair0_rf_map rf_map[MAX_NUM_CCs])
{

1859 1860 1861
  //#ifndef EXMIMO
  //  uint16_t N_TA_offset = 0;
  //#endif
1862 1863 1864

  int i, CC_id;
  LTE_DL_FRAME_PARMS *frame_parms;
1865 1866

  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
1867
    if (phy_vars_ue[CC_id]) {
1868 1869
      frame_parms = &(phy_vars_ue[CC_id]->lte_frame_parms);
    } else {
1870
      printf("phy_vars_UE[%d] not initialized\n", CC_id);
1871 1872 1873 1874
      return(-1);
    }


1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
    //#ifndef EXMIMO
    //    if (frame_parms->frame_type == TDD) {
    //      if (frame_parms->N_RB_DL == 100)
    //  N_TA_offset = 624;
    //      else if (frame_parms->N_RB_DL == 50)
    //  N_TA_offset = 624/2;
    //      else if (frame_parms->N_RB_DL == 25)
    //  N_TA_offset = 624/4;
    //    }
    //#endif

1886 1887 1888 1889 1890
#ifdef EXMIMO
    openair0_cfg[CC_id].tx_num_channels = 0;
    openair0_cfg[CC_id].rx_num_channels = 0;

    // replace RX signal buffers with mmaped HW versions
1891
    for (i=0; i<frame_parms->nb_antennas_rx; i++) {
1892 1893 1894
      printf("Mapping UE CC_id %d, rx_ant %d, freq %u on card %d, chain %d\n",CC_id,i,downlink_frequency[CC_id][i],rf_map[CC_id].card,rf_map[CC_id].chain+i);
      free(phy_vars_ue[CC_id]->lte_ue_common_vars.rxdata[i]);
      phy_vars_ue[CC_id]->lte_ue_common_vars.rxdata[i] = (int32_t*) openair0_exmimo_pci[rf_map[CC_id].card].adc_head[rf_map[CC_id].chain+i];
1895

1896
      if (openair0_cfg[rf_map[CC_id].card].rx_freq[rf_map[CC_id].chain+i]) {
1897 1898 1899 1900 1901 1902 1903
        printf("Error with rf_map! A channel has already been allocated!\n");
        return(-1);
      } else {
        openair0_cfg[rf_map[CC_id].card].rx_freq[rf_map[CC_id].chain+i] = downlink_frequency[CC_id][i];
        openair0_cfg[rf_map[CC_id].card].rx_gain[rf_map[CC_id].chain+i] = rx_gain[CC_id][i];
        openair0_cfg[rf_map[CC_id].card].rxg_mode[rf_map[CC_id].chain+i] = rx_gain_mode[CC_id][i];
        openair0_cfg[rf_map[CC_id].card].rx_num_channels++;
1904 1905 1906 1907
      }

      printf("rxdata[%d] @ %p\n",i,phy_vars_ue[CC_id]->lte_ue_common_vars.rxdata[i]);
    }
1908 1909

    for (i=0; i<frame_parms->nb_antennas_tx; i++) {
1910 1911 1912
      printf("Mapping UE CC_id %d, tx_ant %d, freq %u on card %d, chain %d\n",CC_id,i,downlink_frequency[CC_id][i],rf_map[CC_id].card,rf_map[CC_id].chain+i);
      free(phy_vars_ue[CC_id]->lte_ue_common_vars.txdata[i]);
      phy_vars_ue[CC_id]->lte_ue_common_vars.txdata[i] = (int32_t*) openair0_exmimo_pci[rf_map[CC_id].card].dac_head[rf_map[CC_id].chain+i];
1913

1914
      if (openair0_cfg[rf_map[CC_id].card].tx_freq[rf_map[CC_id].chain+i]) {
1915 1916 1917 1918 1919 1920
        printf("Error with rf_map! A channel has already been allocated!\n");
        return(-1);
      } else {
        openair0_cfg[rf_map[CC_id].card].tx_freq[rf_map[CC_id].chain+i] = downlink_frequency[CC_id][i]+uplink_frequency_offset[CC_id][i];
        openair0_cfg[rf_map[CC_id].card].tx_gain[rf_map[CC_id].chain+i] = tx_gain[CC_id][i];
        openair0_cfg[rf_map[CC_id].card].tx_num_channels++;
1921 1922 1923 1924
      }

      printf("txdata[%d] @ %p\n",i,phy_vars_ue[CC_id]->lte_ue_common_vars.txdata[i]);
    }
1925

1926 1927
#else
    // replace RX signal buffers with mmaped HW versions
1928 1929
    rxdata = (int32_t**)malloc16( frame_parms->nb_antennas_rx*sizeof(int32_t*) );
    txdata = (int32_t**)malloc16( frame_parms->nb_antennas_tx*sizeof(int32_t*) );
1930

1931 1932 1933
    for (i=0; i<frame_parms->nb_antennas_rx; i++) {
      printf( "Mapping UE CC_id %d, rx_ant %d, freq %u on card %d, chain %d\n", CC_id, i, downlink_frequency[CC_id][i], rf_map[CC_id].card, rf_map[CC_id].chain+i );
      free( phy_vars_ue[CC_id]->lte_ue_common_vars.rxdata[i] );
1934
      rxdata[i] = (int32_t*)malloc16_clear( 307200*sizeof(int32_t) );
1935
      phy_vars_ue[CC_id]->lte_ue_common_vars.rxdata[i] = rxdata[i]; // what about the "-N_TA_offset" ? // N_TA offset for TDD
1936
    }
1937

1938 1939 1940
    for (i=0; i<frame_parms->nb_antennas_tx; i++) {
      printf( "Mapping UE CC_id %d, tx_ant %d, freq %u on card %d, chain %d\n", CC_id, i, downlink_frequency[CC_id][i], rf_map[CC_id].card, rf_map[CC_id].chain+i );
      free( phy_vars_ue[CC_id]->lte_ue_common_vars.txdata[i] );
1941
      txdata[i] = (int32_t*)malloc16_clear( 307200*sizeof(int32_t) );
1942 1943
      phy_vars_ue[CC_id]->lte_ue_common_vars.txdata[i] = txdata[i];
    }
1944

1945 1946 1947 1948
    // rxdata[x] points now to the same memory region as phy_vars_ue[CC_id]->lte_ue_common_vars.rxdata[x]
    // txdata[x] points now to the same memory region as phy_vars_ue[CC_id]->lte_ue_common_vars.txdata[x]
    // be careful when releasing memory!
    // because no "release_ue_buffers"-function is available, at least rxdata and txdata memory will leak (only some bytes)
1949
#endif
1950

1951
  }
1952

1953
  return 0;
1954
}