ru_procedures.c 20.6 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file ru_procedures.c
 * \brief Implementation of RU procedures
 * \author R. Knopp, F. Kaltenberger, N. Nikaein, X. Foukas
 * \date 2011
 * \version 0.1
 * \company Eurecom
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr,navid.nikaein@eurecom.fr, x.foukas@sms.ed.ac.uk
 * \note
 * \warning
 */

33 34 35
#include "PHY/defs_eNB.h"
#include "PHY/phy_extern.h"
#include "SCHED/sched_eNB.h"
knopp's avatar
knopp committed
36
#include "PHY/MODULATION/modulation_eNB.h"
37 38
#include "PHY/LTE_TRANSPORT/if4_tools.h"
#include "PHY/LTE_TRANSPORT/if5_tools.h"
knopp's avatar
knopp committed
39
#include "PHY/LTE_TRANSPORT/transport_common_proto.h"
40

41
#include "LAYER2/MAC/mac.h"
42 43
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
44 45 46 47 48 49


#include "assertions.h"
#include "msc.h"

#include <time.h>
knopp's avatar
knopp committed
50

51 52 53 54 55 56 57 58
#include "targets/RT/USER/rt_wrapper.h"

extern int oai_exit;


void feptx0(RU_t *ru,int slot) {

  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
59
  //int dummy_tx_b[7680*2] __attribute__((aligned(32)));
60 61 62 63 64 65

  unsigned int aa,slot_offset;
  int slot_sizeF = (fp->ofdm_symbol_size)*
                   ((fp->Ncp==1) ? 6 : 7);
  int subframe = ru->proc.subframe_tx;

66 67 68

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_OFDM+slot , 1 );

69 70
  slot_offset = subframe*fp->samples_per_tti + (slot*(fp->samples_per_tti>>1));

71
  //LOG_D(PHY,"SFN/SF:RU:TX:%d/%d Generating slot %d\n",ru->proc.frame_tx, ru->proc.subframe_tx,slot);
72 73 74

  for (aa=0; aa<ru->nb_tx; aa++) {
    if (fp->Ncp == EXTENDED) PHY_ofdm_mod(&ru->common.txdataF_BF[aa][slot*slot_sizeF],
75 76 77 78 79
					                      (int*)&ru->common.txdata[aa][slot_offset],
					                      fp->ofdm_symbol_size,
					                      6,
					                      fp->nb_prefix_samples,
					                      CYCLIC_PREFIX);
80
    else                     normal_prefix_mod(&ru->common.txdataF_BF[aa][slot*slot_sizeF],
81 82 83
					                           (int*)&ru->common.txdata[aa][slot_offset],
					                           7,
					                           fp);
84
    
85
   /* 
86 87
    len = fp->samples_per_tti>>1;

88
    
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    if ((slot_offset+len)>(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti)) {
      tx_offset = (int)slot_offset;
      txdata = (int16_t*)&ru->common.txdata[aa][tx_offset];
      len2 = -tx_offset+LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti;
      for (i=0; i<(len2<<1); i++) {
	txdata[i] = ((int16_t*)dummy_tx_b)[i];
      }
      txdata = (int16_t*)&ru->common.txdata[aa][0];
      for (j=0; i<(len<<1); i++,j++) {
	txdata[j++] = ((int16_t*)dummy_tx_b)[i];
      }
    }
    else {
      tx_offset = (int)slot_offset;
      txdata = (int16_t*)&ru->common.txdata[aa][tx_offset];
104
      memcpy((void*)txdata,(void*)dummy_tx_b,len<<2);   
105
    }
106
*/
107
    // TDD: turn on tx switch N_TA_offset before by setting buffer in these samples to 0    
Wang Tsu-Han's avatar
Wang Tsu-Han committed
108
/*    if ((slot == 0) &&
109 110 111 112 113 114
        (fp->frame_type == TDD) && 
        ((fp->tdd_config==0) ||
         (fp->tdd_config==1) ||
         (fp->tdd_config==2) ||
         (fp->tdd_config==6)) &&  
        ((subframe==0) || (subframe==5))) {
115 116 117 118 119 120 121 122 123 124
      for (i=0; i<ru->N_TA_offset; i++) {
	tx_offset = (int)slot_offset+i-ru->N_TA_offset/2;
	if (tx_offset<0)
	  tx_offset += LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti;
	
	if (tx_offset>=(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti))
	  tx_offset -= LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti;
	
	ru->common.txdata[aa][tx_offset] = 0x00000000;
      }
Wang Tsu-Han's avatar
Wang Tsu-Han committed
125
    }*/
126
  }
127
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_OFDM+slot , 0);
128 129 130 131 132 133
}

static void *feptx_thread(void *param) {

  RU_t *ru = (RU_t *)param;
  RU_proc_t *proc  = &ru->proc;
134 135
  cpu_set_t cpuset;
  CPU_ZERO(&cpuset);
136 137 138 139 140 141
  
  thread_top_init("feptx_thread",1,85000,120000,500000);
  pthread_setname_np( pthread_self(),"feptx processing");
  LOG_I(PHY,"thread feptx created id=%ld\n", syscall(__NR_gettid));
  //CPU_SET(6, &cpuset);
  //pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
142
  //wait_sync("feptx_thread");
143

144
  
145 146 147 148

  while (!oai_exit) {

    if (wait_on_condition(&proc->mutex_feptx,&proc->cond_feptx,&proc->instance_cnt_feptx,"feptx thread")<0) break;  
Wang Tsu-Han's avatar
Wang Tsu-Han committed
149
    if (oai_exit) break;
150
    //stop_meas(&ru->ofdm_mod_wakeup_stats);
151 152 153 154 155 156 157 158
    feptx0(ru,1);
    if (release_thread(&proc->mutex_feptx,&proc->instance_cnt_feptx,"feptx thread")<0) break;

    if (pthread_cond_signal(&proc->cond_feptx) != 0) {
      printf("[eNB] ERROR pthread_cond_signal for feptx thread exit\n");
      exit_fun( "ERROR pthread_cond_signal" );
      return NULL;
    }
Thomas Laurent's avatar
Thomas Laurent committed
159
	/*if(opp_enabled == 1 && ru->ofdm_mod_wakeup_stats.p_time>30*3000){
160 161 162
      print_meas_now(&ru->ofdm_mod_wakeup_stats,"fep wakeup",stderr);
      printf("delay in fep wakeup in frame_tx: %d  subframe_rx: %d \n",proc->frame_tx,proc->subframe_tx);
    }*/
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  }



  return(NULL);
}

void feptx_ofdm_2thread(RU_t *ru) {

  LTE_DL_FRAME_PARMS *fp=&ru->frame_parms;
  RU_proc_t *proc = &ru->proc;
  struct timespec wait;
  int subframe = ru->proc.subframe_tx;

  wait.tv_sec=0;
  wait.tv_nsec=5000000L;
179
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_OFDM , 1 );
180
  start_meas(&ru->ofdm_mod_stats);
181 182 183

  if (subframe_select(fp,subframe) == SF_UL) return;

184 185
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_OFDM , 1 );

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  if (subframe_select(fp,subframe)==SF_DL) {
    // If this is not an S-subframe
    if (pthread_mutex_timedlock(&proc->mutex_feptx,&wait) != 0) {
      printf("[RU] ERROR pthread_mutex_lock for feptx thread (IC %d)\n", proc->instance_cnt_feptx);
      exit_fun( "error locking mutex_feptx" );
      return;
    }
    
    if (proc->instance_cnt_feptx==0) {
      printf("[RU] FEPtx thread busy\n");
      exit_fun("FEPtx thread busy");
      pthread_mutex_unlock( &proc->mutex_feptx );
      return;
    }
    
    ++proc->instance_cnt_feptx;
    
    
    if (pthread_cond_signal(&proc->cond_feptx) != 0) {
      printf("[RU] ERROR pthread_cond_signal for feptx thread\n");
      exit_fun( "ERROR pthread_cond_signal" );
      return;
    }
209
	//start_meas(&ru->ofdm_mod_wakeup_stats);
210 211 212 213 214 215
    
    pthread_mutex_unlock( &proc->mutex_feptx );
  }

  // call first slot in this thread
  feptx0(ru,0);
216
  start_meas(&ru->ofdm_mod_wait_stats);
217
  wait_on_busy_condition(&proc->mutex_feptx,&proc->cond_feptx,&proc->instance_cnt_feptx,"feptx thread");  
218
  stop_meas(&ru->ofdm_mod_wait_stats);
Thomas Laurent's avatar
Thomas Laurent committed
219
  /*if(opp_enabled == 1 && ru->ofdm_mod_wait_stats.p_time>30*3000){
220 221
    print_meas_now(&ru->ofdm_mod_wait_stats,"fep wakeup",stderr);
    printf("delay in feptx wait on codition in frame_rx: %d  subframe_rx: %d \n",proc->frame_tx,proc->subframe_tx);
222
  }*/
223

224 225
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_OFDM , 0 );

226
  stop_meas(&ru->ofdm_mod_stats);
227
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_OFDM , 0 );
228 229 230

}

231
void feptx_ofdm(RU_t *ru) {
knopp's avatar
knopp committed
232 233 234 235 236 237 238 239 240 241
     
  LTE_DL_FRAME_PARMS *fp=&ru->frame_parms;

  unsigned int aa,slot_offset, slot_offset_F;
  int dummy_tx_b[7680*4] __attribute__((aligned(32)));
  int i,j, tx_offset;
  int slot_sizeF = (fp->ofdm_symbol_size)*
                   ((fp->Ncp==1) ? 6 : 7);
  int len,len2;
  int16_t *txdata;
242 243
  int subframe = ru->proc.subframe_tx;

knopp's avatar
knopp committed
244 245
//  int CC_id = ru->proc.CC_id;

246
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_OFDM , 1 );
Cedric Roux's avatar
Cedric Roux committed
247
  slot_offset_F = 0;
knopp's avatar
knopp committed
248 249 250 251 252 253 254

  slot_offset = subframe*fp->samples_per_tti;

  if ((subframe_select(fp,subframe)==SF_DL)||
      ((subframe_select(fp,subframe)==SF_S))) {
    //    LOG_D(HW,"Frame %d: Generating slot %d\n",frame,next_slot);

255 256
    start_meas(&ru->ofdm_mod_stats);

knopp's avatar
knopp committed
257 258
    for (aa=0; aa<ru->nb_tx; aa++) {
      if (fp->Ncp == EXTENDED) {
259
        PHY_ofdm_mod(&ru->common.txdataF_BF[aa][0],
knopp's avatar
knopp committed
260 261 262 263 264
                     dummy_tx_b,
                     fp->ofdm_symbol_size,
                     6,
                     fp->nb_prefix_samples,
                     CYCLIC_PREFIX);
265
        PHY_ofdm_mod(&ru->common.txdataF_BF[aa][slot_sizeF],
knopp's avatar
knopp committed
266 267 268 269 270 271
                     dummy_tx_b+(fp->samples_per_tti>>1),
                     fp->ofdm_symbol_size,
                     6,
                     fp->nb_prefix_samples,
                     CYCLIC_PREFIX);
      } else {
272
        normal_prefix_mod(&ru->common.txdataF_BF[aa][slot_offset_F],
knopp's avatar
knopp committed
273 274 275 276 277
                          dummy_tx_b,
                          7,
                          fp);
	// if S-subframe generate first slot only
	if (subframe_select(fp,subframe) == SF_DL) 
278
	  normal_prefix_mod(&ru->common.txdataF_BF[aa][slot_offset_F+slot_sizeF],
knopp's avatar
knopp committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
			    dummy_tx_b+(fp->samples_per_tti>>1),
			    7,
			    fp);
      }

      // if S-subframe generate first slot only
      if (subframe_select(fp,subframe) == SF_S)
	len = fp->samples_per_tti>>1;
      else
	len = fp->samples_per_tti;
      /*
      for (i=0;i<len;i+=4) {
	dummy_tx_b[i] = 0x100;
	dummy_tx_b[i+1] = 0x01000000;
	dummy_tx_b[i+2] = 0xff00;
	dummy_tx_b[i+3] = 0xff000000;
	}*/
      
297 298 299
      if (slot_offset<0) {
	txdata = (int16_t*)&ru->common.txdata[aa][(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti)+tx_offset];
        len2 = -(slot_offset);
knopp's avatar
knopp committed
300 301
	len2 = (len2>len) ? len : len2;
	for (i=0; i<(len2<<1); i++) {
302
	  txdata[i] = ((int16_t*)dummy_tx_b)[i];
knopp's avatar
knopp committed
303 304
	}
	if (len2<len) {
305
	  txdata = (int16_t*)&ru->common.txdata[aa][0];
knopp's avatar
knopp committed
306
	  for (j=0; i<(len<<1); i++,j++) {
307
	    txdata[j++] = ((int16_t*)dummy_tx_b)[i];
knopp's avatar
knopp committed
308 309 310
	  }
	}
      }  
311 312 313
      else if ((slot_offset+len)>(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti)) {
	tx_offset = (int)slot_offset;
	txdata = (int16_t*)&ru->common.txdata[aa][tx_offset];
knopp's avatar
knopp committed
314 315
	len2 = -tx_offset+LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti;
	for (i=0; i<(len2<<1); i++) {
316
	  txdata[i] = ((int16_t*)dummy_tx_b)[i];
knopp's avatar
knopp committed
317
	}
318
	txdata = (int16_t*)&ru->common.txdata[aa][0];
knopp's avatar
knopp committed
319
	for (j=0; i<(len<<1); i++,j++) {
320
          txdata[j++] = ((int16_t*)dummy_tx_b)[i];
knopp's avatar
knopp committed
321 322 323
	}
      }
      else {
324
	//LOG_D(PHY,"feptx_ofdm: Writing to position %d\n",slot_offset);
325 326
	tx_offset = (int)slot_offset;
	txdata = (int16_t*)&ru->common.txdata[aa][tx_offset];
knopp's avatar
knopp committed
327 328

	for (i=0; i<(len<<1); i++) {
329
	  txdata[i] = ((int16_t*)dummy_tx_b)[i];
knopp's avatar
knopp committed
330 331 332 333 334 335 336 337 338 339 340
	}
      }
      
     // if S-subframe switch to RX in second subframe
      /*
     if (subframe_select(fp,subframe) == SF_S) {
       for (i=0; i<len; i++) {
	 ru->common_vars.txdata[0][aa][tx_offset++] = 0x00010001;
       }
     }
      */
Wang Tsu-Han's avatar
Wang Tsu-Han committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

//     if ((fp->frame_type == TDD) &&
//         ((fp->tdd_config==0) ||
//	   (fp->tdd_config==1) ||
//	   (fp->tdd_config==2) ||
//	   (fp->tdd_config==6)) &&
//	     ((subframe==0) || (subframe==5))) {
//       // turn on tx switch N_TA_offset before
//       //LOG_D(HW,"subframe %d, time to switch to tx (N_TA_offset %d, slot_offset %d) \n",subframe,ru->N_TA_offset,slot_offset);
//       for (i=0; i<ru->N_TA_offset; i++) {
//         tx_offset = (int)slot_offset+i-ru->N_TA_offset/2;
//         if (tx_offset<0)
//           tx_offset += LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti;
//
//         if (tx_offset>=(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti))
//           tx_offset -= LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_tti;
//
//         ru->common.txdata[aa][tx_offset] = 0x00000000;
//       }
//     }

362
     stop_meas(&ru->ofdm_mod_stats);
363
     LOG_D(PHY,"feptx_ofdm (TXPATH): frame %d, subframe %d: txp (time %p) %d dB, txp (freq) %d dB\n",
knopp's avatar
knopp committed
364
	   ru->proc.frame_tx,subframe,txdata,dB_fixed(signal_energy((int32_t*)txdata,fp->samples_per_tti)),
365
	   dB_fixed(signal_energy_nodc(ru->common.txdataF_BF[aa],2*slot_sizeF)));
knopp's avatar
knopp committed
366 367
    }
  }
368
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_OFDM , 0 );
knopp's avatar
knopp committed
369 370
}

371 372 373 374 375 376 377 378
void feptx_prec(RU_t *ru) {

  int l,i,aa;
  PHY_VARS_eNB **eNB_list = ru->eNB_list,*eNB;
  LTE_DL_FRAME_PARMS *fp;
  int32_t ***bw;
  int subframe = ru->proc.subframe_tx;

379 380
  if (ru->num_eNB == 1) {
    eNB = eNB_list[0];
381
    fp  = &eNB->frame_parms;
382
    
383 384
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_PREC , 1);

385 386 387 388
    for (aa=0;aa<ru->nb_tx;aa++)
      memcpy((void*)ru->common.txdataF_BF[aa],
	     (void*)&eNB->common_vars.txdataF[aa][subframe*fp->symbols_per_tti*fp->ofdm_symbol_size],
	     fp->symbols_per_tti*fp->ofdm_symbol_size*sizeof(int32_t));
389 390

    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPTX_PREC , 0);
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  }
  else {
    for (i=0;i<ru->num_eNB;i++) {
      eNB = eNB_list[i];
      fp  = &eNB->frame_parms;
      bw  = ru->beam_weights[i];
      
      for (l=0;l<fp->symbols_per_tti;l++) {
	for (aa=0;aa<ru->nb_tx;aa++) {
	  beam_precoding(eNB->common_vars.txdataF,
			 ru->common.txdataF_BF,
			 fp,
			 bw,
			 subframe<<1,
			 l,
			 aa);
	}
408 409 410 411
      }
    }
  }
}
knopp's avatar
knopp committed
412 413 414

void fep0(RU_t *ru,int slot) {

415 416
  RU_proc_t *proc       = &ru->proc;
  LTE_DL_FRAME_PARMS *fp = &ru->frame_parms;
knopp's avatar
knopp committed
417 418 419 420
  int l;

  //  printf("fep0: slot %d\n",slot);

421 422
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPRX+slot, 1);

knopp's avatar
knopp committed
423 424
  remove_7_5_kHz(ru,(slot&1)+(proc->subframe_rx<<1));
  for (l=0; l<fp->symbols_per_tti/2; l++) {
425
    slot_fep_ul(ru,
knopp's avatar
knopp committed
426
		l,
427
		(slot&1),
knopp's avatar
knopp committed
428 429 430
		0
		);
  }
431
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPRX+slot, 0);
knopp's avatar
knopp committed
432 433 434 435 436 437 438
}



static void *fep_thread(void *param) {

  RU_t *ru = (RU_t *)param;
439
  RU_proc_t *proc  = &ru->proc;
440

441 442 443
  thread_top_init("fep_thread",1,100000,120000,5000000);
  pthread_setname_np( pthread_self(),"fep processing");
  LOG_I(PHY,"thread fep created id=%ld\n", syscall(__NR_gettid));
444

445 446
  cpu_set_t cpuset;
  CPU_ZERO(&cpuset);
447 448
  //CPU_SET(2, &cpuset);
  //pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
449
  //wait_sync("fep_thread");
450

knopp's avatar
knopp committed
451 452
  while (!oai_exit) {

453
    if (wait_on_condition(&proc->mutex_fep,&proc->cond_fep,&proc->instance_cnt_fep,"fep thread")<0) break; 
Wang Tsu-Han's avatar
Wang Tsu-Han committed
454
    if (oai_exit) break;
455
	//stop_meas(&ru->ofdm_demod_wakeup_stats);
456
	VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPRX1, 1 ); 
knopp's avatar
knopp committed
457
    fep0(ru,0);
458
	VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPRX1, 0 ); 
knopp's avatar
knopp committed
459 460 461 462 463 464 465
    if (release_thread(&proc->mutex_fep,&proc->instance_cnt_fep,"fep thread")<0) break;

    if (pthread_cond_signal(&proc->cond_fep) != 0) {
      printf("[eNB] ERROR pthread_cond_signal for fep thread exit\n");
      exit_fun( "ERROR pthread_cond_signal" );
      return NULL;
    }
Thomas Laurent's avatar
Thomas Laurent committed
466
    /*if(opp_enabled == 1 && ru->ofdm_demod_wakeup_stats.p_time>30*3000){
467 468 469
      print_meas_now(&ru->ofdm_demod_wakeup_stats,"fep wakeup",stderr);
      printf("delay in fep wakeup in frame_rx: %d  subframe_rx: %d \n",proc->frame_rx,proc->subframe_rx);
    }*/
knopp's avatar
knopp committed
470 471 472 473 474 475 476
  }



  return(NULL);
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490
void init_feptx_thread(RU_t *ru,pthread_attr_t *attr_feptx) {

  RU_proc_t *proc = &ru->proc;

  proc->instance_cnt_feptx         = -1;
    
  pthread_mutex_init( &proc->mutex_feptx, NULL);
  pthread_cond_init( &proc->cond_feptx, NULL);

  pthread_create(&proc->pthread_feptx, attr_feptx, feptx_thread, (void*)ru);


}

491
void init_fep_thread(RU_t *ru,pthread_attr_t *attr_fep) {
knopp's avatar
knopp committed
492

493
  RU_proc_t *proc = &ru->proc;
knopp's avatar
knopp committed
494 495 496 497 498 499

  proc->instance_cnt_fep         = -1;
    
  pthread_mutex_init( &proc->mutex_fep, NULL);
  pthread_cond_init( &proc->cond_fep, NULL);

500
  pthread_create(&proc->pthread_fep, attr_fep, fep_thread, (void*)ru);
knopp's avatar
knopp committed
501 502 503


}
Wang Tsu-Han's avatar
Wang Tsu-Han committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

extern void kill_fep_thread(RU_t *ru)
{
  RU_proc_t *proc = &ru->proc;
  pthread_mutex_lock( &proc->mutex_fep );
  proc->instance_cnt_fep         = 0;
  pthread_cond_signal(&proc->cond_fep);
  pthread_mutex_unlock( &proc->mutex_fep );
  LOG_D(PHY, "Joining pthread_fep\n");
  pthread_join(proc->pthread_fep, NULL);
  pthread_mutex_destroy( &proc->mutex_fep );
  pthread_cond_destroy( &proc->cond_fep );
}

extern void kill_feptx_thread(RU_t *ru)
{
  RU_proc_t *proc = &ru->proc;
  pthread_mutex_lock( &proc->mutex_feptx );
  proc->instance_cnt_feptx         = 0;
  pthread_cond_signal(&proc->cond_feptx);
  pthread_mutex_unlock( &proc->mutex_feptx );
  LOG_D(PHY, "Joining pthread_feptx\n");
  pthread_join(proc->pthread_feptx, NULL);
  pthread_mutex_destroy( &proc->mutex_feptx );
  pthread_cond_destroy( &proc->cond_feptx );
}

knopp's avatar
knopp committed
531 532
void ru_fep_full_2thread(RU_t *ru) {

533
  RU_proc_t *proc = &ru->proc;
knopp's avatar
knopp committed
534 535 536

  struct timespec wait;

537 538 539 540 541 542 543
  LTE_DL_FRAME_PARMS *fp=&ru->frame_parms;

  if ((fp->frame_type == TDD) &&
     (subframe_select(fp,proc->subframe_rx) != SF_UL)) return;

  if (ru->idx == 0) VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPRX, 1 );

knopp's avatar
knopp committed
544 545 546
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;

547
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPRX, 1 );
knopp's avatar
knopp committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  start_meas(&ru->ofdm_demod_stats);

  if (pthread_mutex_timedlock(&proc->mutex_fep,&wait) != 0) {
    printf("[RU] ERROR pthread_mutex_lock for fep thread (IC %d)\n", proc->instance_cnt_fep);
    exit_fun( "error locking mutex_fep" );
    return;
  }

  if (proc->instance_cnt_fep==0) {
    printf("[RU] FEP thread busy\n");
    exit_fun("FEP thread busy");
    pthread_mutex_unlock( &proc->mutex_fep );
    return;
  }
  
  ++proc->instance_cnt_fep;


  if (pthread_cond_signal(&proc->cond_fep) != 0) {
    printf("[RU] ERROR pthread_cond_signal for fep thread\n");
    exit_fun( "ERROR pthread_cond_signal" );
    return;
  }
571
  //start_meas(&ru->ofdm_demod_wakeup_stats);
knopp's avatar
knopp committed
572 573 574 575 576 577
  
  pthread_mutex_unlock( &proc->mutex_fep );

  // call second slot in this symbol
  fep0(ru,1);

578
  start_meas(&ru->ofdm_demod_wait_stats);
knopp's avatar
knopp committed
579
  wait_on_busy_condition(&proc->mutex_fep,&proc->cond_fep,&proc->instance_cnt_fep,"fep thread");  
580
  stop_meas(&ru->ofdm_demod_wait_stats);
Thomas Laurent's avatar
Thomas Laurent committed
581
  if(opp_enabled == 1 && ru->ofdm_demod_wakeup_stats.p_time>30*3000){
582 583 584
    print_meas_now(&ru->ofdm_demod_wakeup_stats,"fep wakeup",stderr);
    printf("delay in fep wait on codition in frame_rx: %d  subframe_rx: %d \n",proc->frame_rx,proc->subframe_rx);
  }
knopp's avatar
knopp committed
585 586

  stop_meas(&ru->ofdm_demod_stats);
587
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPRX, 0 );
knopp's avatar
knopp committed
588 589 590 591
}



592
void fep_full(RU_t *ru) {
knopp's avatar
knopp committed
593

594
  RU_proc_t *proc = &ru->proc;
knopp's avatar
knopp committed
595
  int l;
596
  LTE_DL_FRAME_PARMS *fp=&ru->frame_parms;
knopp's avatar
knopp committed
597

598 599
  if ((fp->frame_type == TDD) && 
     (subframe_select(fp,proc->subframe_rx) != SF_UL)) return;
knopp's avatar
knopp committed
600

601 602
  start_meas(&ru->ofdm_demod_stats);
  if (ru->idx == 0) VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPRX, 1 );
knopp's avatar
knopp committed
603 604 605

  remove_7_5_kHz(ru,proc->subframe_rx<<1);
  remove_7_5_kHz(ru,1+(proc->subframe_rx<<1));
606

knopp's avatar
knopp committed
607
  for (l=0; l<fp->symbols_per_tti/2; l++) {
608
    slot_fep_ul(ru,
knopp's avatar
knopp committed
609
		l,
610
		0,
knopp's avatar
knopp committed
611 612
		0
		);
613
    slot_fep_ul(ru,
knopp's avatar
knopp committed
614
		l,
615
		1,
knopp's avatar
knopp committed
616 617 618
		0
		);
  }
619
  if (ru->idx == 0) VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_RU_FEPRX, 0 );
knopp's avatar
knopp committed
620 621 622 623 624 625 626 627
  stop_meas(&ru->ofdm_demod_stats);
  
  
}


void do_prach_ru(RU_t *ru) {

628 629
  RU_proc_t *proc = &ru->proc;
  LTE_DL_FRAME_PARMS *fp=&ru->frame_parms;
knopp's avatar
knopp committed
630 631

  // check if we have to detect PRACH first
632
  if (is_prach_subframe(fp,proc->frame_prach,proc->subframe_prach)>0) { 
633
    //accept some delay in processing - up to 5ms
knopp's avatar
knopp committed
634 635
    int i;
    for (i = 0; i < 10 && proc->instance_cnt_prach == 0; i++) {
636 637
      LOG_W(PHY,"Frame %d Subframe %d, PRACH thread busy (IC %d)!!\n", proc->frame_prach,proc->subframe_prach,
	    proc->instance_cnt_prach);
knopp's avatar
knopp committed
638 639 640 641 642 643 644 645 646
      usleep(500);
    }
    if (proc->instance_cnt_prach == 0) {
      exit_fun( "PRACH thread busy" );
      return;
    }
    
    // wake up thread for PRACH RX
    if (pthread_mutex_lock(&proc->mutex_prach) != 0) {
647
      LOG_E( PHY, "ERROR pthread_mutex_lock for PRACH thread (IC %d)\n", proc->instance_cnt_prach );
knopp's avatar
knopp committed
648 649 650 651 652 653 654 655
      exit_fun( "error locking mutex_prach" );
      return;
    }
    
    ++proc->instance_cnt_prach;
    
    // the thread can now be woken up
    if (pthread_cond_signal(&proc->cond_prach) != 0) {
656
      LOG_E( PHY, "ERROR pthread_cond_signal for PRACH thread\n");
knopp's avatar
knopp committed
657 658 659 660 661 662 663 664
      exit_fun( "ERROR pthread_cond_signal" );
      return;
    }
    
    pthread_mutex_unlock( &proc->mutex_prach );
  }

}