From fa0b8668fb49f1577f5bea990b04a95e1b9d9124 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Mon, 27 Mar 2017 16:27:56 +0200
Subject: [PATCH] bugfix: workaround a problem with timestamp and LMSSDR.

We have a strange problem with the timestamp of the very
first packets received from the LimeSDR that leads to
a complete failure of the eNB.

A simple workaround is to discard the first RX packet
just after a "start" of the device.

We also change the value of "tx_sample_advance" to a more
reasonable value. The problem of timestamp was "solved"
differently in the past and at the time we needed to put
a much higher value.

This may have to be refined at some point.
---
 targets/ARCH/LMSSDR/USERSPACE/LIB/lms_lib.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/targets/ARCH/LMSSDR/USERSPACE/LIB/lms_lib.cpp b/targets/ARCH/LMSSDR/USERSPACE/LIB/lms_lib.cpp
index f2d2af55c7..2dec4ff22d 100644
--- a/targets/ARCH/LMSSDR/USERSPACE/LIB/lms_lib.cpp
+++ b/targets/ARCH/LMSSDR/USERSPACE/LIB/lms_lib.cpp
@@ -45,6 +45,14 @@ lms_device_t* lms_device;
 lms_stream_t rx_stream;
 lms_stream_t tx_stream;
 
+/* We have a strange behavior when we just start reading
+ * from the device (inconsistent values of the timestamp).
+ * A quick solution is to discard the very first read packet
+ * after a "start".
+ * The following global variable "first_rx" serves that purpose.
+ */
+static int first_rx = 0;
+
 #define RXDCLENGTH 4096
 #define NUMBUFF 32
 
@@ -94,6 +102,10 @@ int trx_lms_read(openair0_device *device, openair0_timestamp *ptimestamp, void *
     meta.flushPartialPacket = false;
 
     int ret;
+    if (first_rx == 1) {
+      first_rx = 0;
+      ret = LMS_RecvStream(&rx_stream,buff[0],nsamps,&meta,50);
+    }
     ret = LMS_RecvStream(&rx_stream,buff[0],nsamps,&meta,50);
     *ptimestamp = meta.timestamp;
     return ret;
@@ -223,6 +235,8 @@ int trx_lms_start(openair0_device *device){
     }
 
 
+    first_rx = 1;
+
     rx_stream.channel = 0;
     rx_stream.fifoSize = 256*1024;
     rx_stream.throughputVsLatency = 0.1;
@@ -355,7 +369,7 @@ int device_init(openair0_device *device, openair0_config_t *openair0_cfg){
     break;
   case 15360000:
     openair0_cfg[0].samples_per_packet    = 2048;
-    openair0_cfg[0].tx_sample_advance     = 450;
+    openair0_cfg[0].tx_sample_advance     = 50;   /* TODO: to be refined */
     openair0_cfg[0].tx_bw                 = 15.36e6;
     openair0_cfg[0].rx_bw                 = 15.36e6;
     break;
-- 
GitLab