From c4ccb59e9538f527c2b8d170569ddd85fb7b89d3 Mon Sep 17 00:00:00 2001
From: Felix Schneider <felix@fx-schneider.de>
Date: Wed, 19 Aug 2020 10:02:50 +0200
Subject: [PATCH] Clean up iq sampling fix

---
 devices/plutosdr/deviceplutosdrbox.cpp        | 49 ++++++++++++++-----
 devices/plutosdr/deviceplutosdrbox.h          |  6 +--
 .../plutosdrinput/plutosdrinputthread.cpp     | 35 ++-----------
 3 files changed, 44 insertions(+), 46 deletions(-)

diff --git a/devices/plutosdr/deviceplutosdrbox.cpp b/devices/plutosdr/deviceplutosdrbox.cpp
index 2b770a5cf..cc1863e2c 100644
--- a/devices/plutosdr/deviceplutosdrbox.cpp
+++ b/devices/plutosdr/deviceplutosdrbox.cpp
@@ -40,8 +40,8 @@ DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) :
         m_devPhy(0),
         m_devRx(0),
         m_devTx(0),
-        m_chnRx0(0),
-        m_chnRxQ(0),
+        m_chnRx0i(0),
+        m_chnRx0q(0),
         m_chnTx0i(0),
         m_chnTx0q(0),
         m_rxBuf(0),
@@ -255,16 +255,15 @@ bool DevicePlutoSDRBox::openRx()
 {
     if (!m_valid) { return false; }
 
-    if (!m_chnRx0 || !m_chnRxQ) {
-        m_chnRx0 = iio_device_find_channel(m_devRx, "voltage0", false);
-        m_chnRxQ = iio_device_find_channel(m_devRx, "voltage1", false);
+    if (!m_chnRx0i) {
+        m_chnRx0i = iio_device_find_channel(m_devRx, "voltage0", false);
     }
 
-    if (m_chnRx0 && m_chnRxQ) {
-        iio_channel_enable(m_chnRx0);
-        iio_channel_enable(m_chnRxQ);
-        const struct iio_data_format *df = iio_channel_get_data_format(m_chnRx0);
-        qDebug("DevicePlutoSDRBox::openRx: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u",
+    if (m_chnRx0i) {
+        iio_channel_enable(m_chnRx0i);
+        
+        const struct iio_data_format *df = iio_channel_get_data_format(m_chnRx0i);
+        qDebug("DevicePlutoSDRBox::openRx channel I: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u",
                 df->length,
                 df->bits,
                 df->shift,
@@ -273,11 +272,34 @@ bool DevicePlutoSDRBox::openRx()
                 df->with_scale? "true" : "false",
                 df->scale,
                 df->repeat);
-        return true;
     } else {
         std::cerr << "DevicePlutoSDRBox::openRx: failed" << std::endl;
         return false;
     }
+
+    if (!m_chnRx0q) {
+        m_chnRx0q = iio_device_find_channel(m_devRx, "voltage1", false);
+    }
+
+    if (m_chnRx0q) {
+        iio_channel_enable(m_chnRx0q);
+
+        const struct iio_data_format* df = iio_channel_get_data_format(m_chnRx0q);
+        qDebug("DevicePlutoSDRBox::openRx channel Q: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u",
+            df->length,
+            df->bits,
+            df->shift,
+            df->is_signed ? "true" : "false",
+            df->is_be ? "true" : "false",
+            df->with_scale ? "true" : "false",
+            df->scale,
+            df->repeat);
+        return true;
+    }
+    else {
+        std::cerr << "DevicePlutoSDRBox::openRx: failed" << std::endl;
+        return false;
+    }
 }
 
 bool DevicePlutoSDRBox::openTx()
@@ -330,7 +352,8 @@ bool DevicePlutoSDRBox::openTx()
 
 void DevicePlutoSDRBox::closeRx()
 {
-    if (m_chnRx0) { iio_channel_disable(m_chnRx0); }
+    if (m_chnRx0i) { iio_channel_disable(m_chnRx0i); }
+    if (m_chnRx0q) { iio_channel_disable(m_chnRx0q); }
 }
 
 void DevicePlutoSDRBox::closeTx()
@@ -434,7 +457,7 @@ char* DevicePlutoSDRBox::rxBufferEnd()
 char* DevicePlutoSDRBox::rxBufferFirst()
 {
     if (m_rxBuf) {
-        return (char *) iio_buffer_first(m_rxBuf, m_chnRx0);
+        return (char *) iio_buffer_first(m_rxBuf, m_chnRx0i);
     } else {
         return 0;
     }
diff --git a/devices/plutosdr/deviceplutosdrbox.h b/devices/plutosdr/deviceplutosdrbox.h
index bff534b2e..904b6439e 100644
--- a/devices/plutosdr/deviceplutosdrbox.h
+++ b/devices/plutosdr/deviceplutosdrbox.h
@@ -85,7 +85,7 @@ public:
     void deleteTxBuffer();
     ssize_t getRxSampleSize();
     ssize_t getTxSampleSize();
-    struct iio_channel *getRxChannel0() { return m_chnRx0; }
+    struct iio_channel *getRxChannel0() { return m_chnRx0i; }
     struct iio_channel *getTxChannel0I() { return m_chnTx0i; }
     struct iio_channel *getTxChannel0Q() { return m_chnTx0q; }
     ssize_t rxBufferRefill();
@@ -119,8 +119,8 @@ private:
     struct iio_device  *m_devPhy;
     struct iio_device  *m_devRx;
     struct iio_device  *m_devTx;
-    struct iio_channel *m_chnRx0;
-    struct iio_channel* m_chnRxQ;
+    struct iio_channel *m_chnRx0i;
+    struct iio_channel* m_chnRx0q;
     struct iio_channel *m_chnTx0i;
     struct iio_channel *m_chnTx0q;
     struct iio_buffer  *m_rxBuf;
diff --git a/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp b/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp
index 5050d7620..93c0cc711 100644
--- a/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp
+++ b/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp
@@ -80,7 +80,7 @@ void PlutoSDRInputThread::run()
     qDebug("PlutoSDRInputThread::run: rxBufferStep: %ld bytes", p_inc);
     qDebug("PlutoSDRInputThread::run: Rx sample size is %ld bytes", m_plutoBox->getRxSampleSize());
     qDebug("PlutoSDRInputThread::run: Tx sample size is %ld bytes", m_plutoBox->getTxSampleSize());
-    qDebug("PlutoSDRInputThread::run: nominal nbytes_rx is %d bytes with 2 refills", m_blockSizeSamples*2);
+    qDebug("PlutoSDRInputThread::run: nominal nbytes_rx is %d bytes with 1 refill", m_blockSizeSamples*4);
 
     m_running = true;
     m_startWaiter.wakeAll();
@@ -96,7 +96,7 @@ void PlutoSDRInputThread::run()
 
         if (nbytes_rx != m_blockSizeSamples*4)
         {
-            qWarning("PlutoSDRInputThread::run: error refilling buf (1) %d / %d",(int) nbytes_rx, (int)  m_blockSizeSamples*2);
+            qWarning("PlutoSDRInputThread::run: error refilling buf %d / %d",(int) nbytes_rx, (int)  m_blockSizeSamples*4);
             usleep(200000);
             continue;
         }
@@ -105,42 +105,17 @@ void PlutoSDRInputThread::run()
         p_end = m_plutoBox->rxBufferEnd();
         ihs = 0;
 
-        // p_inc is 2 on a char* buffer therefore each iteration processes only the I or Q sample
-        // I and Q samples are processed one after the other
+        // p_inc is 4 on a char* buffer therefore each iteration processes a single IQ sample,
+        // I and Q each being two bytes
         // conversion is not needed as samples are little endian
 
         for (p_dat = m_plutoBox->rxBufferFirst(); p_dat < p_end; p_dat += p_inc)
         {
             m_buf[ihs++] = *((int16_t *) p_dat);
-            m_buf[ihs++] = *(((int16_t*)p_dat)+1);
+            m_buf[ihs++] = *(((int16_t *) p_dat) + 1);
 //            iio_channel_convert(m_plutoBox->getRxChannel0(), (void *) &m_bufConv[ihs], (const void *) &m_buf[ihs]);
-            //ihs++;
         }
 
-        // Refill RX buffer again - we still need twice more samples to complete since they come as I followed by Q
-        /*nbytes_rx = m_plutoBox->rxBufferRefill();
-
-        if (nbytes_rx != m_blockSizeSamples*2)
-        {
-            qWarning("PlutoSDRInputThread::run: error refilling buf (2) %d / %d",(int) nbytes_rx, (int)  m_blockSizeSamples*2);
-            usleep(200000);
-            continue;
-        }
-
-        // READ: Get pointers to RX buf and read IQ from RX buf port 0
-        p_end = m_plutoBox->rxBufferEnd();
-
-        // p_inc is 2 on a char* buffer therefore each iteration processes only the I or Q sample
-        // I and Q samples are processed one after the other
-        // conversion is not needed as samples are little endian
-
-        for (p_dat = m_plutoBox->rxBufferFirst(); p_dat < p_end; p_dat += p_inc)
-        {
-            m_buf[ihs] = *((int16_t *) p_dat);
-//            iio_channel_convert(m_plutoBox->getRxChannel0(), (void *) &m_bufConv[ihs], (const void *) &m_buf[ihs]);
-            ihs++;
-        }
-        */
         if (m_iqOrder) {
             convertIQ(m_buf, 2*m_blockSizeSamples); // size given in number of int16_t (I and Q interleaved)
         } else {