From 8f0ba722f63ded97821de794bba723bb9b6f84f5 Mon Sep 17 00:00:00 2001
From: Raymond Knopp <raymond.knopp@eurecom.fr>
Date: Thu, 17 Aug 2017 16:22:04 +0200
Subject: [PATCH] first half of RA procedure. PRACH reception functional for
 CElevel0. Up to RAR generation.

---
 openair1/PHY/LTE_TRANSPORT/prach.c            | 103 ++++++++++--------
 openair1/SCHED/phy_procedures_lte_eNb.c       |  72 ++++++++----
 openair2/LAYER2/MAC/config.c                  |   1 +
 openair2/LAYER2/MAC/eNB_scheduler.c           |   2 +-
 openair2/LAYER2/MAC/eNB_scheduler_RA.c        |  78 +++++++++----
 .../LAYER2/MAC/eNB_scheduler_primitives.c     |  24 ++--
 openair2/PHY_INTERFACE/IF_Module.c            |   1 +
 .../CONF/enb.band13.tm1.50PRB.emtc.conf       |   4 +-
 8 files changed, 181 insertions(+), 104 deletions(-)

diff --git a/openair1/PHY/LTE_TRANSPORT/prach.c b/openair1/PHY/LTE_TRANSPORT/prach.c
index b6564845d0..27663c6698 100755
--- a/openair1/PHY/LTE_TRANSPORT/prach.c
+++ b/openair1/PHY/LTE_TRANSPORT/prach.c
@@ -1153,10 +1153,10 @@ void rx_prach0(PHY_VARS_eNB *eNB,
 #ifdef Rel14
   int prach_ifft_cnt=0;
 #endif
-  //#ifdef PRACH_DEBUG
-  int en,en0=0;
-  //#endif
-
+#ifdef PRACH_DEBUG
+  int en0=0;
+#endif
+  int en;
   if (ru) { 
     fp    = &ru->frame_parms;
     nb_rx = ru->nb_rx;
@@ -1209,12 +1209,14 @@ void rx_prach0(PHY_VARS_eNB *eNB,
       subframe            = eNB->proc.subframe_prach_br;
       prachF              = eNB->prach_vars_br.prachF;
       rxsigF              = eNB->prach_vars_br.rxsigF;
+#ifdef PRACH_DEBUG
       if ((frame&1023) < 20) LOG_I(PHY,"PRACH (eNB) : running rx_prach (br_flag %d, ce_level %d) for frame %d subframe %d, prach_FreqOffset %d, prach_ConfigIndex %d, rootSequenceIndex %d, repetition number %d,numRepetitionsPrePreambleAttempt %d\n",
 				   br_flag,ce_level,frame,subframe,
 				   fp->prach_emtc_config_common.prach_ConfigInfo.prach_FreqOffset[ce_level],
 				   prach_ConfigIndex,rootSequenceIndex,
 				   eNB->prach_vars_br.repetition_number[ce_level],
 				   fp->prach_emtc_config_common.prach_ConfigInfo.prach_numRepetitionPerPreambleAttempt[ce_level]);
+#endif
     }
     else
 #endif
@@ -1224,8 +1226,10 @@ void rx_prach0(PHY_VARS_eNB *eNB,
         subframe          = eNB->proc.subframe_prach;
         prachF            = eNB->prach_vars.prachF;
         rxsigF            = eNB->prach_vars.rxsigF;
+#ifdef PRACH_DEBUG
         if ((frame&1023) < 20) LOG_I(PHY,"PRACH (eNB) : running rx_prach for subframe %d, prach_FreqOffset %d, prach_ConfigIndex %d , rootSequenceIndex %d\n",
 				     subframe,fp->prach_config_common.prach_ConfigInfo.prach_FreqOffset,prach_ConfigIndex,rootSequenceIndex);
+#endif
       }
   }
   else {
@@ -1233,17 +1237,20 @@ void rx_prach0(PHY_VARS_eNB *eNB,
     if (br_flag == 1) {
         subframe          = ru->proc.subframe_prach_br;
         rxsigF            = ru->prach_rxsigF_br[ce_level];
+#ifdef PRACH_DEBUG
         if ((frame&1023) < 20) LOG_I(PHY,"PRACH (RU) : running rx_prach (br_flag %d, ce_level %d) for frame %d subframe %d, prach_FreqOffset %d, prach_ConfigIndex %d\n",
 				     br_flag,ce_level,frame,subframe,fp->prach_emtc_config_common.prach_ConfigInfo.prach_FreqOffset[ce_level],prach_ConfigIndex);
+#endif
     }
     else
 #endif
       {
         subframe          = ru->proc.subframe_prach;
         rxsigF            = ru->prach_rxsigF;
+#ifdef PRACH_DEBUG
         if ((frame&1023) < 20) LOG_I(PHY,"PRACH (RU) : running rx_prach for subframe %d, prach_FreqOffset %d, prach_ConfigIndex %d\n",
 	      subframe,fp->prach_config_common.prach_ConfigInfo.prach_FreqOffset,prach_ConfigIndex);
-
+#endif
       }
 
   }
@@ -1253,9 +1260,9 @@ void rx_prach0(PHY_VARS_eNB *eNB,
   for (aa=0; aa<nb_rx; aa++) {
     if (ru->if_south == LOCAL_RF) { // set the time-domain signal if we have to use it in this node
       prach[aa] = (int16_t*)&ru->common.rxdata[aa][(subframe*fp->samples_per_tti)-ru->N_TA_offset];
-      //#ifdef PRACH_DEBUG
+#ifdef PRACH_DEBUG
 	if ((frame&1023) < 20) LOG_I(PHY,"RU %d, br_flag %d ce_level %d frame %d subframe %d, : prach %p (energy %d)\n",ru->idx,br_flag,ce_level,frame,subframe,prach[aa],dB_fixed(en0=signal_energy(prach[aa],fp->samples_per_tti))); 
-      //#endif
+#endif
     }
   }
 
@@ -1465,25 +1472,13 @@ void rx_prach0(PHY_VARS_eNB *eNB,
 #endif
       send_IF4p5(ru, ru->proc.frame_prach, ru->proc.subframe_prach, IF4p5_PRACH);
     
-#if 0
-    if (dB_fixed(en0)>30) {
-      en = dB_fixed(signal_energy(&rxsigF[0][k],840));
-      //    if (en>60)
-      printf("PRACH (if4p5), k %d, n_ra_prb %d: Frame %d, Subframe %d => %d dB\n",k,n_ra_prb,ru->proc.frame_rx,ru->proc.subframe_rx,en);
-      write_output("rxsigF.m","prach_rxF",rxsigF[0],12288,1,1);
-      exit(-1);
-    }
-#endif
-
     return;
   } else if (eNB!=NULL) {
 
-
-    //#if 0
     en = dB_fixed(signal_energy(&rxsigF[0][0],840));
-    /*if (en>60)*/
-    if ((frame&1023) < 20) LOG_I(PHY,"PRACH (br_flag %d,ce_level %d): Frame %d, Subframe %d => %d dB\n",br_flag,ce_level,eNB->proc.frame_rx,eNB->proc.subframe_rx,en);
-    //#endif
+#ifdef PRACH_DEBUG
+    if ((en > 60)&&(br_flag==1)) LOG_I(PHY,"PRACH (br_flag %d,ce_level %d, n_ra_prb %d, k %d): Frame %d, Subframe %d => %d dB\n",br_flag,ce_level,n_ra_prb,k,eNB->proc.frame_rx,eNB->proc.subframe_rx,en);
+#endif
 
   }
   
@@ -1519,8 +1514,9 @@ void rx_prach0(PHY_VARS_eNB *eNB,
   *max_preamble_energy=0;
   for (preamble_index=0 ; preamble_index<64 ; preamble_index++) {
 
+#ifdef PRACH_DEBUG
     if (en>60) LOG_I(PHY,"frame %d, subframe %d : Trying preamble %d (br_flag %d)\n",frame,subframe,preamble_index,br_flag);
- 
+#endif
     if (restricted_set == 0) {
       // This is the relative offset in the root sequence table (5.7.2-4 from 36.211) for the given preamble index
       preamble_offset = ((NCS==0)? preamble_index : (preamble_index/(N_ZC/NCS)));
@@ -1603,10 +1599,10 @@ void rx_prach0(PHY_VARS_eNB *eNB,
     }
 
     // Compute DFT of RX signal (conjugate input, results in conjugate output) for each new rootSequenceIndex
-    //#ifdef PRACH_DEBUG
+#ifdef PRACH_DEBUG
     if (en>60) LOG_I(PHY,"frame %d, subframe %d : preamble index %d: offset %d, preamble shift %d (br_flag %d, en %d)\n",
 		     frame,subframe,preamble_index,preamble_offset,preamble_shift,br_flag,en);
-    //#endif
+#endif
     log2_ifft_size = 10;
     fft_size = 6144;
 
@@ -1667,25 +1663,6 @@ void rx_prach0(PHY_VARS_eNB *eNB,
 #endif
       // if (aa=1) write_output("prach_rxF_comp1.m","prach_rxF_comp1",prachF,1024,1,1);
       }// antennas_rx
-
-#ifdef PRACH_DEBUG
-
-      if (en>40) {
-	k = (12*n_ra_prb) - 6*fp->N_RB_UL;
-	
-	if (k<0) k+=fp->ofdm_symbol_size;
-	
-	k*=12;
-	k+=13;
-	k*=2;
-	printf("Dumping prach, k = %d (n_ra_prb %d)\n",k,n_ra_prb);
-	write_output("rxsigF.m","prach_rxF",&rxsigF[0][0],12288,1,1);
-	write_output("prach_rxF_comp0.m","prach_rxF_comp0",prachF,1024,1,1);
-	write_output("Xu.m","xu",Xu,N_ZC,1,1);
-	write_output("prach_ifft0.m","prach_t0",prach_ifft[0][0],1024,1,1);
-	exit(-1);
-      }
-#endif
     } // new dft
     
     // check energy in nth time shift, for 
@@ -1695,26 +1672,58 @@ void rx_prach0(PHY_VARS_eNB *eNB,
 	 eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_numRepetitionPerPreambleAttempt[ce_level]))
 #endif
       {
+#ifdef PRACH_DEBUG
 	if (en>60) LOG_I(PHY,"frame %d, subframe %d: Checking for peak in time-domain (br_flag %d, en %d)\n",frame,subframe,br_flag,en);
-
+#endif
 	preamble_shift2 = ((preamble_shift==0) ? 0 : ((preamble_shift<<log2_ifft_size)/N_ZC));
 
     
 	for (i=0; i<NCS2; i++) {
-	  lev = (int32_t)prach_ifft[(preamble_shift2+i)<<1];
+	  lev = (int32_t)prach_ifft[(preamble_shift2+i)];
 	  levdB = dB_fixed_times10(lev);
 	  
 	  if (levdB>*max_preamble_energy) {
 	    *max_preamble_energy  = levdB;
 	    *max_preamble_delay   = ((i*fft_size)>>log2_ifft_size)*update_TA/update_TA2;
 	    *max_preamble         = preamble_index;
-	    if (en>60) LOG_I(PHY,"frame %d, subframe %d : max_preamble_energy %d, max_preamble_delay %d, max_preamble %d (br_flag %d, en %d)\n",frame,subframe,*max_preamble_energy,*max_preamble_delay,*max_preamble,br_flag,en);
+	    //#ifdef PRACH_DEBUG
+	    if ((en>60) && (br_flag==1)) LOG_I(PHY,"frame %d, subframe %d : max_preamble_energy %d, max_preamble_delay %d, max_preamble %d (br_flag %d,ce_level %d, levdB %d, lev %d)\n",frame,subframe,*max_preamble_energy,*max_preamble_delay,*max_preamble,br_flag,ce_level,levdB,lev);
+	    //#endif
 	  }
 	}
 
       }
   }// preamble_index
 
+#ifdef PRACH_DEBUG
+  
+  if (en>60) {
+    k = (12*n_ra_prb) - 6*fp->N_RB_UL;
+    
+    if (k<0) k+=fp->ofdm_symbol_size;
+    
+    k*=12;
+    k+=13;
+    k*=2;
+    
+    if (br_flag == 0) {
+      /*
+	write_output("rxsigF.m","prach_rxF",&rxsigF[0][0],12288,1,1);
+	write_output("prach_rxF_comp0.m","prach_rxF_comp0",prachF,1024,1,1);
+	write_output("Xu.m","xu",Xu,N_ZC,1,1);
+	write_output("prach_ifft0.m","prach_t0",prach_ifft,1024,1,1);*/
+    }
+    else {
+      printf("Dumping prach (br_flag %d), k = %d (n_ra_prb %d)\n",br_flag,k,n_ra_prb);
+      write_output("rxsigF_br.m","prach_rxF_br",&rxsigF[0][0],12288,1,1);
+      write_output("prach_rxF_comp0_br.m","prach_rxF_comp0_br",prachF,1024,1,1);
+      write_output("Xu_br.m","xu_br",Xu,N_ZC,1,1);
+      write_output("prach_ifft0_br.m","prach_t0_br",prach_ifft,1024,1,1);
+      exit(-1);      
+    }
+
+  }
+#endif
   if (eNB) stop_meas(&eNB->rx_prach);
 
 }
diff --git a/openair1/SCHED/phy_procedures_lte_eNb.c b/openair1/SCHED/phy_procedures_lte_eNb.c
index 1a784a306f..0d2d4e8d50 100755
--- a/openair1/SCHED/phy_procedures_lte_eNb.c
+++ b/openair1/SCHED/phy_procedures_lte_eNb.c
@@ -1889,10 +1889,16 @@ void prach_procedures(PHY_VARS_eNB *eNB,
   if (br_flag==1) {
     subframe = eNB->proc.subframe_prach_br;
     frame = eNB->proc.frame_prach_br;
+    pthread_mutex_lock(&eNB->UL_INFO_mutex);
+    eNB->UL_INFO.rach_ind_br.number_of_preambles=0;
+    pthread_mutex_unlock(&eNB->UL_INFO_mutex);
   }
   else
 #endif
     {
+      pthread_mutex_lock(&eNB->UL_INFO_mutex);
+      eNB->UL_INFO.rach_ind.number_of_preambles=0;
+      pthread_mutex_unlock(&eNB->UL_INFO_mutex);
       subframe = eNB->proc.subframe_prach;
       frame = eNB->proc.frame_prach;
     }
@@ -1905,6 +1911,8 @@ void prach_procedures(PHY_VARS_eNB *eNB,
  
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PRACH_RX,1);
 
+
+
   for (i=0;i<eNB->num_RU;i++) {
     ru=eNB->RU_list[i];
     for (ru_aa=0,aa=0;ru_aa<ru->nb_rx;ru_aa++,aa++) {
@@ -1918,8 +1926,6 @@ void prach_procedures(PHY_VARS_eNB *eNB,
     }
   }
 
-  if ((frame&1023) < 20) LOG_I(PHY,"Frame %d, subframe %d: Running rx_prach (br_flag %d)\n",
-			       frame,subframe,br_flag);
   rx_prach(eNB,
 	   eNB->RU_list[0],
 	   &max_preamble[0],
@@ -1932,13 +1938,13 @@ void prach_procedures(PHY_VARS_eNB *eNB,
 #endif
 	   );
 
-  //#ifdef DEBUG_PHY_PROC
-  LOG_D(PHY,"[RAPROC] Frame %d, subframe %d : Most likely preamble %d, energy %d dB delay %d\n",
+#ifdef DEBUG_PHY_PROC
+  LOG_I(PHY,"[RAPROC] Frame %d, subframe %d : Most likely preamble %d, energy %d dB delay %d\n",
         frame,subframe,
 	max_preamble[0],
-        max_preamble_energy[0],
+        max_preamble_energy[0]/10,
         max_preamble_delay[0]);
-  //#endif
+#endif
 
 #ifdef Rel14
   if (br_flag==1) {
@@ -1949,30 +1955,46 @@ void prach_procedures(PHY_VARS_eNB *eNB,
     prach_mask = is_prach_subframe(&eNB->frame_parms,eNB->proc.frame_prach_br,eNB->proc.subframe_prach_br);
     
     eNB->UL_INFO.rach_ind_br.preamble_list                              = eNB->preamble_list_br;
-    
+    int ind=0;
+    int ce_level=0;
+    /* Save for later, it doesn't work    
     for (int ind=0,ce_level=0;ce_level<4;ce_level++) {
-      if ((prach_mask&(1<<(1+ce_level)) > 0) && // prach is active and CE level has finished its repetitions
+      
+      if ((eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_CElevel_enable[ce_level]==1)&&
+	  (prach_mask&(1<<(1+ce_level)) > 0) && // prach is active and CE level has finished its repetitions
 	  (eNB->prach_vars_br.repetition_number[ce_level]==
 	   eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_numRepetitionPerPreambleAttempt[ce_level])) {
-	if (max_preamble_energy[ind] > 580) {
-	  eNB->UL_INFO.rach_ind_br.number_of_preambles++;
-	  
-	  eNB->preamble_list_br[ind].preamble_rel8.timing_advance        = max_preamble_delay[ind];//
-	  eNB->preamble_list_br[ind].preamble_rel8.preamble              = max_preamble[ind];
-	  // note: fid is implicitly 0 here, this is the rule for eMTC RA-RNTI from 36.321, Section 5.1.4
-	  eNB->preamble_list_br[ind].preamble_rel8.rnti                  = 1+subframe+(eNB->prach_vars_br.first_frame[ce_level]%40);  
-	  eNB->preamble_list_br[ind].instance_length                     = 0; //don't know exactly what this is
-	  eNB->preamble_list_br[ind].preamble_rel13.rach_resource_type   = 1+ce_level;  // CE Level
-	}
+    */
+    if (eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_CElevel_enable[0]==1){ 
+      if (max_preamble_energy[0] > 350) {
+	eNB->UL_INFO.rach_ind_br.number_of_preambles++;
+	
+	eNB->preamble_list_br[ind].preamble_rel8.timing_advance        = max_preamble_delay[ind];//
+	eNB->preamble_list_br[ind].preamble_rel8.preamble              = max_preamble[ind];
+	// note: fid is implicitly 0 here, this is the rule for eMTC RA-RNTI from 36.321, Section 5.1.4
+	eNB->preamble_list_br[ind].preamble_rel8.rnti                  = 1+subframe+(eNB->prach_vars_br.first_frame[ce_level]%40);  
+	eNB->preamble_list_br[ind].instance_length                     = 0; //don't know exactly what this is
+	eNB->preamble_list_br[ind].preamble_rel13.rach_resource_type   = 1+ce_level;  // CE Level
+	LOG_I(PHY,"Filling NFAPI indication for RACH %d CELevel %d (mask %x) : TA %d, Preamble %d, rnti %x, rach_resource_type %d\n",
+	      ind,
+	      ce_level,
+	      prach_mask,
+	      eNB->preamble_list_br[ind].preamble_rel8.timing_advance,
+	      eNB->preamble_list_br[ind].preamble_rel8.preamble,
+	      eNB->preamble_list_br[ind].preamble_rel8.rnti,
+	      eNB->preamble_list_br[ind].preamble_rel13.rach_resource_type);
+      }
+      /*
 	ind++;
       }
-    } // ce_level
+      } */// ce_level
+    }
   }
   else
 #endif
 
     {
-      if (max_preamble_energy[0] > 580) {
+      if (max_preamble_energy[0] > 10000) {
 
 	LOG_D(PHY,"[eNB %d/%d][RAPROC] Frame %d, subframe %d Initiating RA procedure with preamble %d, energy %d.%d dB, delay %d\n",
 	      eNB->Mod_id,
@@ -2002,10 +2024,15 @@ void prach_procedures(PHY_VARS_eNB *eNB,
 	      eNB->preamble_list[0].preamble_rel8.rnti                  = 1+subframe;  // note: fid is implicitly 0 here
 	      eNB->preamble_list[0].preamble_rel13.rach_resource_type   = 0;
 	      eNB->preamble_list[0].instance_length                     = 0; //don't know exactly what this is
-	    
+
+	      LOG_I(PHY,"Filling NFAPI indication for RACH : TA %d, Preamble %d, rnti %x, rach_resource_type %d\n",
+		    eNB->preamble_list[0].preamble_rel8.timing_advance,
+		    eNB->preamble_list[0].preamble_rel8.preamble,
+		    eNB->preamble_list[0].preamble_rel8.rnti,
+		    eNB->preamble_list[0].preamble_rel13.rach_resource_type);	    
 	      pthread_mutex_unlock(&eNB->UL_INFO_mutex);
 	    }
-      } // max_preamble_energy > 580
+      } // max_preamble_energy > 350
     } // else br_flag
       /*
 	mac_xface->initiate_ra_proc(eNB->Mod_id,
@@ -2494,7 +2521,6 @@ void phy_procedures_eNB_uespec_RX(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,const
   LOG_D(PHY,"[eNB %d] Frame %d: Doing phy_procedures_eNB_uespec_RX(%d)\n",eNB->Mod_id,frame, subframe);
 #endif
 
-
   eNB->rb_mask_ul[0]=0;
   eNB->rb_mask_ul[1]=0;
   eNB->rb_mask_ul[2]=0;
diff --git a/openair2/LAYER2/MAC/config.c b/openair2/LAYER2/MAC/config.c
index 2084a6046b..50b5531598 100755
--- a/openair2/LAYER2/MAC/config.c
+++ b/openair2/LAYER2/MAC/config.c
@@ -556,6 +556,7 @@ int rrc_mac_config_req_eNB(module_id_t                      Mod_idP,
 		  (int)radioResourceConfigCommon->rach_ConfigCommon.maxHARQ_Msg3Tx);
 
       RC.mac[Mod_idP]->common_channels[CC_idP].radioResourceConfigCommon = radioResourceConfigCommon;
+      RC.mac[Mod_idP]->common_channels[CC_idP].radioResourceConfigCommon_BR = radioResourceConfigCommon_BR;
       if (ul_CarrierFreq>0) RC.mac[Mod_idP]->common_channels[CC_idP].ul_CarrierFreq          = ul_CarrierFreq;
       if (ul_Bandwidth) RC.mac[Mod_idP]->common_channels[CC_idP].ul_Bandwidth                = *ul_Bandwidth;
       else RC.mac[Mod_idP]->common_channels[CC_idP].ul_Bandwidth                             = RC.mac[Mod_idP]->common_channels[CC_idP].mib->message.dl_Bandwidth;
diff --git a/openair2/LAYER2/MAC/eNB_scheduler.c b/openair2/LAYER2/MAC/eNB_scheduler.c
index 7e6e2b67c9..c3f405ad99 100755
--- a/openair2/LAYER2/MAC/eNB_scheduler.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler.c
@@ -180,7 +180,7 @@ void eNB_dlsch_ulsch_scheduler(module_id_t module_idP, frame_t frameP, sub_frame
 #endif
 
   
-  //  LOG_I(MAC,"[eNB %d] Frame %d, Subframe %d, entering MAC scheduler (UE_list->head %d)\n",module_idP, frameP, subframeP,UE_list->head);
+  LOG_D(MAC,"[eNB %d] Frame %d, Subframe %d, entering MAC scheduler (UE_list->head %d)\n",module_idP, frameP, subframeP,UE_list->head);
 
   start_meas(&RC.mac[module_idP]->eNB_scheduler);
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ULSCH_SCHEDULER,VCD_FUNCTION_IN);
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_RA.c b/openair2/LAYER2/MAC/eNB_scheduler_RA.c
index 3488f7c20b..c588540c27 100755
--- a/openair2/LAYER2/MAC/eNB_scheduler_RA.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_RA.c
@@ -90,7 +90,7 @@ void check_and_add_msg3(module_id_t module_idP,frame_t frameP, sub_frame_t subfr
 	
 	if (RA_template->Msg3_subframe<4) msg3_prog_frame=(RA_template->Msg3_frame+1023)&1023;
 	else                              msg3_prog_frame=RA_template->Msg3_frame;
-        LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d CC_id %d RA %d is active, Msg3 in (%d,%d), programmed in (%d,%d)\n",
+        LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : CC_id %d RA %d is active, Msg3 in (%d,%d), programmed in (%d,%d)\n",
               module_idP,frameP,subframeP,CC_id,i,RA_template->Msg3_frame,RA_template->Msg3_subframe,
 	      msg3_prog_frame,msg3_prog_subframe);
 
@@ -171,6 +171,11 @@ void generate_Msg2(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t
   PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13;
   PRACH_ParametersCE_r13_t *p[4]={NULL,NULL,NULL,NULL};
 
+  uint16_t absSF      = (10*frameP)+subframeP;
+  uint16_t absSF_Msg2 = (10*RA_template->Msg2_frame)+RA_template->Msg2_subframe;
+
+  if (absSF>absSF_Msg2) return; // we're not ready yet, need to be to start ==  
+  
   if (cc[CC_idP].radioResourceConfigCommon_BR) {
 
     ext4_prach                 = cc[CC_idP].radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
@@ -185,8 +190,10 @@ void generate_Msg2(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t
       p[1]=prach_ParametersListCE_r13->list.array[1];
     case 1:
       p[0]=prach_ParametersListCE_r13->list.array[0];
+      break;
     default:
-      AssertFatal(1==0,"Illegal count for prach_ParametersListCE_r13 %d\n",prach_ParametersListCE_r13->list.count);
+      AssertFatal(1==0,"Illegal count for prach_ParametersListCE_r13 %d\n",(int)prach_ParametersListCE_r13->list.count);
+      break;
     }
   }
 
@@ -201,9 +208,10 @@ void generate_Msg2(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t
     //    distributed transmission
     
     // rmax from SIB2 information
-    rmax           = p[RA_template->rach_resource_type-1]->mpdcch_NumRepetition_RA_r13;
-    // choose r3 by default for RAR (Table 9.1.5-5)
-    rep            = 2; 
+    AssertFatal(rmax<9,"rmax>8!\n");
+    rmax           = 1<<p[RA_template->rach_resource_type-1]->mpdcch_NumRepetition_RA_r13;
+    // choose r1 by default for RAR (Table 9.1.5-5)
+    rep            = 0; 
     // get actual repetition count from Table 9.1.5-3
     reps           = (rmax<=8)?(1<<rep):(rmax>>(3-rep));
     // get narrowband according to higher-layer config 
@@ -212,9 +220,10 @@ void generate_Msg2(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t
     first_rb = narrowband_to_first_rb(&cc[CC_idP],RA_template->msg2_narrowband);
     
     if ((RA_template->msg2_mpdcch_repetition_cnt == 0) &&
-    (mpdcch_sf_condition(eNB,CC_idP,frameP,subframeP,rmax,TYPE2,-1)>0)){
+	(mpdcch_sf_condition(eNB,CC_idP,frameP,subframeP,rmax,TYPE2,-1)>0)){
       // MPDCCH configuration for RAR
-      
+      LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : In generate_Msg2, Programming MPDCCH %d repetitions\n",
+	    module_idP,frameP,subframeP,reps);
       
       
       memset((void*)dl_config_pdu,0,sizeof(nfapi_dl_config_request_pdu_t));
@@ -265,10 +274,13 @@ void generate_Msg2(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t
       dl_config_pdu->mpdcch_pdu.mpdcch_pdu_rel13.number_of_tx_antenna_ports                    = 1;
       RA_template->msg2_mpdcch_repetition_cnt++;
       dl_req->number_pdu++;
-      
+      RA_template->Msg2_subframe = (RA_template->Msg2_subframe+9)%10;
+
     } //repetition_count==0 && SF condition met
-    else if (RA_template->msg2_mpdcch_repetition_cnt>0) { // we're in a stream of repetitions
-      RA_template->msg2_mpdcch_repetition_cnt++;	      
+    if (RA_template->msg2_mpdcch_repetition_cnt>0) { // we're in a stream of repetitions
+      LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : In generate_Msg2, MPDCCH repetition %d\n",
+	    module_idP,frameP,subframeP,RA_template->msg2_mpdcch_repetition_cnt);
+
       if (RA_template->msg2_mpdcch_repetition_cnt==reps) { // this is the last mpdcch repetition
 	if (cc[CC_idP].tdd_Config==NULL) { // FDD case
 	  // wait 2 subframes for PDSCH transmission
@@ -279,9 +291,13 @@ void generate_Msg2(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t
 	else {
 	  AssertFatal(1==0,"TDD case not done yet\n");
 	}
-      } // mpdcch_repetition_count == reps
+      }// mpdcch_repetition_count == reps
+      RA_template->msg2_mpdcch_repetition_cnt++;	      
+
       if ((RA_template->Msg2_frame == frameP) && (RA_template->Msg2_subframe == subframeP)) {
 	// Program PDSCH
+	LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : In generate_Msg2, Programming PDSCH %d\n",
+	      module_idP,frameP,subframeP);
 	RA_template->generate_rar = 0;	      
 	
 	dl_config_pdu                                                                  = &dl_req->dl_config_pdu_list[dl_req->number_pdu]; 
@@ -341,8 +357,9 @@ void generate_Msg2(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t
   else
 #endif
     {
+
       if ((RA_template->Msg2_frame == frameP) && (RA_template->Msg2_subframe == subframeP)) {
-	LOG_D(MAC,"[eNB %d] CC_id %d Frame %d, subframeP %d: Generating RAR DCI, RA_active %d format 1A (%d,%d))\n",
+	LOG_I(MAC,"[eNB %d] CC_id %d Frame %d, subframeP %d: Generating RAR DCI, RA_active %d format 1A (%d,%d))\n",
 	      module_idP, CC_idP, frameP, subframeP,
 	      RA_template->RA_active,
 	      
@@ -409,7 +426,8 @@ void generate_Msg2(module_id_t module_idP,int CC_idP,frame_t frameP,sub_frame_t
 	  
 	  // Program UL processing for Msg3
 	  get_Msg3alloc(&cc[CC_idP],subframeP,frameP,&RA_template->Msg3_frame,&RA_template->Msg3_subframe);
-	  
+	  LOG_I(MAC,"Frame %d, Subframe %d: Setting Msg3 reception for Frame %d Subframe %d\n",
+		frameP,subframeP,RA_template->Msg3_frame,RA_template->Msg3_subframe);
 	  
 	  fill_rar(module_idP,CC_idP,frameP,cc[CC_idP].RAR_pdu.payload,N_RB_DL,7);
 	  // DL request
@@ -1024,7 +1042,7 @@ void schedule_RA(module_id_t module_idP,frame_t frameP, sub_frame_t subframeP)
 
   start_meas(&eNB->schedule_ra);
 
- 
+
   for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
     // skip UL component carriers
     if (is_UL_sf(&cc[CC_id],subframeP)==1) continue;
@@ -1036,8 +1054,8 @@ void schedule_RA(module_id_t module_idP,frame_t frameP, sub_frame_t subframeP)
 
       if (RA_template->RA_active == TRUE) {
 
-        LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d RA %d is active (generate RAR %d, generate_Msg4 %d, wait_ack_Msg4 %d, rnti %x)\n",
-              module_idP,CC_id,i,RA_template->generate_rar,RA_template->generate_Msg4,RA_template->wait_ack_Msg4, RA_template->rnti);
+        LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Subframe %d : CC_id %d RA %d is active (generate RAR %d, generate_Msg4 %d, wait_ack_Msg4 %d, rnti %x)\n",
+              module_idP,frameP,subframeP,CC_id,i,RA_template->generate_rar,RA_template->generate_Msg4,RA_template->wait_ack_Msg4, RA_template->rnti);
 
         if      (RA_template->generate_rar == 1)  generate_Msg2(module_idP,CC_id,frameP,subframeP,RA_template);
 	else if (RA_template->generate_Msg4 == 1) generate_Msg4(module_idP,CC_id,frameP,subframeP,RA_template);
@@ -1068,13 +1086,26 @@ void initiate_ra_proc(module_id_t module_idP,
 {
 
   uint8_t i;
-  RA_TEMPLATE *RA_template = (RA_TEMPLATE *)&RC.mac[module_idP]->common_channels[CC_id].RA_template[0];
 
-  LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d Initiating RA procedure for preamble index %d\n",module_idP,CC_id,frameP,preamble_index);
+  COMMON_channels_t   *cc  = &RC.mac[module_idP]->common_channels[CC_id];
+  RA_TEMPLATE *RA_template = &cc->RA_template[0];
+
+  struct PRACH_ConfigSIB_v1310 *ext4_prach=cc->radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
+  PRACH_ParametersListCE_r13_t *prach_ParametersListCE_r13= &ext4_prach->prach_ParametersListCE_r13;
+
+  LOG_I(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, Subframe %d  Initiating RA procedure for preamble index %d\n",module_idP,CC_id,frameP,subframeP,preamble_index);
 #ifdef Rel14
-  LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d PRACH resource type %d\n",module_idP,CC_id,frameP,rach_resource_type);
+  LOG_I(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d, Subframe %d  PRACH resource type %d\n",module_idP,CC_id,frameP,subframeP,rach_resource_type);
 #endif
 
+  if (prach_ParametersListCE_r13->list.count<rach_resource_type) {
+    LOG_E(MAC,"[eNB %d][RAPROC] CC_id %d Received impossible PRACH resource type %d, only %d CE levels configured\n",
+	  module_idP,CC_id,
+	  rach_resource_type,
+	  (int)prach_ParametersListCE_r13->list.count);
+    return;
+  }
+
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_INITIATE_RA_PROC,1);
   VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_INITIATE_RA_PROC,0);
 
@@ -1093,6 +1124,8 @@ void initiate_ra_proc(module_id_t module_idP,
       RA_template[i].msg2_mpdcch_repetition_cnt = 0;		      
       RA_template[i].msg4_mpdcch_repetition_cnt = 0;		      
 #endif
+      RA_template[i].Msg2_frame         = frameP+((subframeP>5)?1:0);
+      RA_template[i].Msg2_subframe      = (subframeP+4)%10;
       /* TODO: find better procedure to allocate RNTI */
       do {
         RA_template[i].rnti = taus();
@@ -1107,8 +1140,11 @@ void initiate_ra_proc(module_id_t module_idP,
       if (loop == 100) { printf("%s:%d:%s: FATAL ERROR! contact the authors\n", __FILE__, __LINE__, __FUNCTION__); abort(); }
       RA_template[i].RA_rnti        = ra_rnti;
       RA_template[i].preamble_index = preamble_index;
-      LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d Activating RAR generation for process %d, rnti %x, RA_active %d\n",
-            module_idP,CC_id,frameP,i,RA_template[i].rnti,
+      LOG_I(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d Activating RAR generation in Frame %d, subframe %d for process %d, rnti %x, RA_active %d\n",
+            module_idP,CC_id,frameP,
+	    RA_template[i].Msg2_frame,
+	    RA_template[i].Msg2_subframe,
+	    i,RA_template[i].rnti,
             RA_template[i].RA_active);
 
       return;
diff --git a/openair2/LAYER2/MAC/eNB_scheduler_primitives.c b/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
index 4b4949c949..45cb7082f9 100755
--- a/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
+++ b/openair2/LAYER2/MAC/eNB_scheduler_primitives.c
@@ -490,10 +490,9 @@ int mpdcch_sf_condition(eNB_MAC_INST *eNB,int CC_id, frame_t frameP,sub_frame_t
 
   struct PRACH_ConfigSIB_v1310 *ext4_prach = eNB->common_channels[CC_id].radioResourceConfigCommon_BR->ext4->prach_ConfigCommon_v1310;
 
-  EPDCCH_SetConfig_r11_t *epdcch_setconfig_r11= &eNB->UE_list.UE_template[CC_id][UE_id].physicalConfigDedicated->ext4->epdcch_Config_r11->config_r11.choice.setup.setConfigToAddModList_r11->list.array[0] ;
-
 
   int T;
+  EPDCCH_SetConfig_r11_t *epdcch_setconfig_r11;
 
   switch (mpdcch_type) {
   case TYPE0:
@@ -508,6 +507,7 @@ int mpdcch_sf_condition(eNB_MAC_INST *eNB,int CC_id, frame_t frameP,sub_frame_t
   case TYPE2: // RAR
     AssertFatal(ext4_prach->mpdcch_startSF_CSS_RA_r13!=NULL,
 		"mpdcch_startSF_CSS_RA_r13 is null\n");
+    AssertFatal(rmax>0,"rmax is 0!\b");
     if (eNB->common_channels[CC_id].tdd_Config==NULL) //FDD
       T = rmax*startSF_fdd_RA_times2[ext4_prach->mpdcch_startSF_CSS_RA_r13->choice.fdd_r13]>>1;
     else //TDD
@@ -517,19 +517,23 @@ int mpdcch_sf_condition(eNB_MAC_INST *eNB,int CC_id, frame_t frameP,sub_frame_t
     AssertFatal(1==0,"MPDCCH Type 2A not handled yet\n");
     break;
   case TYPEUESPEC:
-     AssertFatal(epdcch_setconfig_r11 != NULL," epdcch_setconfig_r11 is null for UE specific \n");
-     AssertFatal(epdcch_setconfig_r11->ext2 != NULL," ext2 doesn't exist in epdcch config ' \n");
-
-      if (eNB->common_channels[CC_id].tdd_Config==NULL) //FDD
-        T = rmax*startSF_fdd_RA_times2[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.fdd_r13]>>1;
-      else //TDD
-        T = rmax*startSF_tdd_RA[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.tdd_r13];
-
+    epdcch_setconfig_r11= &eNB->UE_list.UE_template[CC_id][UE_id].physicalConfigDedicated->ext4->epdcch_Config_r11->config_r11.choice.setup.setConfigToAddModList_r11->list.array[0] ;
+    
+    
+    AssertFatal(epdcch_setconfig_r11 != NULL," epdcch_setconfig_r11 is null for UE specific \n");
+    AssertFatal(epdcch_setconfig_r11->ext2 != NULL," ext2 doesn't exist in epdcch config ' \n");
+    
+    if (eNB->common_channels[CC_id].tdd_Config==NULL) //FDD
+      T = rmax*startSF_fdd_RA_times2[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.fdd_r13]>>1;
+    else //TDD
+      T = rmax*startSF_tdd_RA[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.tdd_r13];
+    
     break;
   default:
     return(0);
   }
 
+  AssertFatal(T>0,"T is 0!\n");
   if (((10*frameP) + subframeP)%T == 0) return(1);
   else return(0);
 
diff --git a/openair2/PHY_INTERFACE/IF_Module.c b/openair2/PHY_INTERFACE/IF_Module.c
index 70104e8331..03ce7d4a0b 100755
--- a/openair2/PHY_INTERFACE/IF_Module.c
+++ b/openair2/PHY_INTERFACE/IF_Module.c
@@ -40,6 +40,7 @@ void handle_rach(UL_IND_t *UL_info) {
 		  "Got regular PRACH preamble, not BL/CE\n");
       LOG_D(MAC,"Frame %d, Subframe %d Calling initiate_ra_proc (CE_level %d)\n",UL_info->frame,UL_info->subframe,
 	    UL_info->rach_ind_br.preamble_list[i].preamble_rel13.rach_resource_type-1);
+      UL_info->rach_ind_br.number_of_preambles=0;
       initiate_ra_proc(UL_info->module_id,
 		       UL_info->CC_id,
 		       UL_info->frame,
diff --git a/targets/PROJECTS/GENERIC-LTE-EPC/CONF/enb.band13.tm1.50PRB.emtc.conf b/targets/PROJECTS/GENERIC-LTE-EPC/CONF/enb.band13.tm1.50PRB.emtc.conf
index 6ba66784e4..133bcc9caa 100755
--- a/targets/PROJECTS/GENERIC-LTE-EPC/CONF/enb.band13.tm1.50PRB.emtc.conf
+++ b/targets/PROJECTS/GENERIC-LTE-EPC/CONF/enb.band13.tm1.50PRB.emtc.conf
@@ -179,7 +179,7 @@ eNBs =
           (
               {
                   prach_config_index_br                     = 3;
-                  prach_freq_offset_br                      = 1;
+                  prach_freq_offset_br                      = 2;
                   prach_StartingSubframe_r13                = 0;
                   maxNumPreambleAttemptCE_r13               = 6;
                   numRepetitionPerPreambleAttempt_r13       = 0;
@@ -235,7 +235,7 @@ eNBs =
           prach_config_index                        = 3;
           prach_high_speed                          = "DISABLE";
           prach_zero_correlation                    = 1;
-          prach_freq_offset                         = 1;
+          prach_freq_offset                         = 2;
           pucch_delta_shift                         = 1;
           pucch_nRB_CQI                             = 0;
           pucch_nCS_AN                              = 0;
-- 
2.26.2