From 189e15aa8092f7f39857346b29dddc29d035c13c Mon Sep 17 00:00:00 2001
From: Florian Kaltenberger <kaltenbe@eurecom.fr>
Date: Fri, 11 Oct 2019 17:12:39 +0200
Subject: [PATCH] improved real-time performance:  - gNB now only sends samples
 to USRP when DL slot. (should do the same for UE)  - increased sf_ahead to 6 
 - fixed timestamp for usrp_write at UE

---
 executables/nr-gnb.c          |  4 ++--
 executables/nr-ru.c           | 13 +++++++++++--
 executables/nr-ue.c           |  2 +-
 openair2/GNB_APP/gnb_config.c |  9 ++++-----
 4 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/executables/nr-gnb.c b/executables/nr-gnb.c
index 0a3bef3355e..c665cdfb8f6 100644
--- a/executables/nr-gnb.c
+++ b/executables/nr-gnb.c
@@ -108,8 +108,8 @@ extern openair0_config_t openair0_cfg[MAX_CARDS];
 
 extern int transmission_mode;
 
-uint16_t sl_ahead=4;
-uint16_t sf_ahead=4;
+uint16_t sl_ahead=6;
+uint16_t sf_ahead=6;
 //pthread_t                       main_gNB_thread;
 
 time_stats_t softmodem_stats_mt; // main thread
diff --git a/executables/nr-ru.c b/executables/nr-ru.c
index 4f676716fa8..af4f8b8cbaa 100644
--- a/executables/nr-ru.c
+++ b/executables/nr-ru.c
@@ -732,9 +732,17 @@ void tx_rf(RU_t *ru,int frame,int slot, uint64_t timestamp) {
   T(T_ENB_PHY_OUTPUT_SIGNAL, T_INT(0), T_INT(0), T_INT(frame), T_INT(slot),
     T_INT(0), T_BUFFER(&ru->common.txdata[0][slot * fp->samples_per_slot], fp->samples_per_slot * 4));
   int sf_extension = 0;
-  //nr_subframe_t SF_type     = nr_slot_select(cfg,slot%fp->slots_per_frame);
+  nr_subframe_t SF_type     = nr_slot_select(cfg,slot%fp->slots_per_frame);
+
+  if ((slot == 0) ||
+      (slot == 1)) {
+    int siglen=fp->samples_per_slot;
+    int flags;
+    if (slot==0)
+      flags = 2;
+    else if (slot==1)
+      flags=3;
 
-    int siglen=fp->samples_per_slot,flags=1;
     /*
         if (SF_type == SF_S) {
           siglen = fp->dl_symbols_in_S_subframe*(fp->ofdm_symbol_size+fp->nb_prefix_samples0);
@@ -774,6 +782,7 @@ void tx_rf(RU_t *ru,int frame,int slot, uint64_t timestamp) {
           (long long unsigned int)timestamp,frame,proc->frame_tx_unwrap,slot);
     VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 0 );
     AssertFatal(txs ==  siglen+sf_extension,"TX : Timeout (sent %d/%d)\n",txs, siglen);
+  }
 }
 
 
diff --git a/executables/nr-ue.c b/executables/nr-ue.c
index 877dd18dff5..fe2fd6ca317 100644
--- a/executables/nr-ue.c
+++ b/executables/nr-ue.c
@@ -725,7 +725,7 @@ void *UE_thread(void *arg) {
     AssertFatal( writeBlockSize ==
                  UE->rfdevice.trx_write_func(&UE->rfdevice,
                      timestamp+
-                     (2*UE->frame_parms.samples_per_slot) -
+                     (DURATION_RX_TO_TX*UE->frame_parms.samples_per_slot) -
                      UE->frame_parms.ofdm_symbol_size-UE->frame_parms.nb_prefix_samples0 -
                      openair0_cfg[0].tx_sample_advance,
                      txp,
diff --git a/openair2/GNB_APP/gnb_config.c b/openair2/GNB_APP/gnb_config.c
index 71bec1bc58b..97f1839d507 100644
--- a/openair2/GNB_APP/gnb_config.c
+++ b/openair2/GNB_APP/gnb_config.c
@@ -808,11 +808,10 @@ void RCconfig_NRRRC(MessageDef *msg_p, uint32_t i, gNB_RRC_INST *rrc) {
             }
         
             NRRRC_CONFIGURATION_REQ (msg_p).N_RB_DL[j]= N_RB_DL;
-            if(N_RB_DL == 217)      sf_ahead = 2;
-            else if(N_RB_DL == 106) sf_ahead = 4;
-            else                    sf_ahead = 4;
-            /*else                    AssertFatal (0,"Failed to parse gNB configuration file %s, gnb %d unknown value \"%d\" for N_RB_DL choice: 106, 217 !\n",
-                                                 RC.config_file_name, i, N_RB_DL);*/
+            //if(N_RB_DL == 217)      sf_ahead = 2;
+            //else if(N_RB_DL == 106) sf_ahead = 4;
+            if ((N_RB_DL != 217) && (N_RB_DL != 106))
+	      AssertFatal (0,"Failed to parse gNB configuration file %s, gnb %d unknown value \"%d\" for N_RB_DL choice: 106, 217 !\n", RC.config_file_name, i, N_RB_DL);
             
             /*
             if ((N_RB_DL!=6) && (N_RB_DL!=15) && (N_RB_DL!=25) && (N_RB_DL!=50) && (N_RB_DL!=75) && (N_RB_DL!=100)) {
-- 
GitLab