From 7848b019873bbe56c07d406bf37422c57c819c92 Mon Sep 17 00:00:00 2001
From: Felix Schneider <felix@fx-schneider.de>
Date: Wed, 19 Aug 2020 09:05:09 +0200
Subject: [PATCH] Open both I and Q channels on plutosdr and change the
 inputthread accordingly

---
 devices/plutosdr/deviceplutosdrbox.cpp                |  7 +++++--
 devices/plutosdr/deviceplutosdrbox.h                  |  1 +
 .../plutosdrinput/plutosdrinputthread.cpp             | 11 ++++++-----
 3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/devices/plutosdr/deviceplutosdrbox.cpp b/devices/plutosdr/deviceplutosdrbox.cpp
index 07edd34be..2b770a5cf 100644
--- a/devices/plutosdr/deviceplutosdrbox.cpp
+++ b/devices/plutosdr/deviceplutosdrbox.cpp
@@ -41,6 +41,7 @@ DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) :
         m_devRx(0),
         m_devTx(0),
         m_chnRx0(0),
+        m_chnRxQ(0),
         m_chnTx0i(0),
         m_chnTx0q(0),
         m_rxBuf(0),
@@ -254,12 +255,14 @@ bool DevicePlutoSDRBox::openRx()
 {
     if (!m_valid) { return false; }
 
-    if (!m_chnRx0) {
+    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_chnRx0) {
+    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",
                 df->length,
diff --git a/devices/plutosdr/deviceplutosdrbox.h b/devices/plutosdr/deviceplutosdrbox.h
index ae7021e5a..bff534b2e 100644
--- a/devices/plutosdr/deviceplutosdrbox.h
+++ b/devices/plutosdr/deviceplutosdrbox.h
@@ -120,6 +120,7 @@ private:
     struct iio_device  *m_devRx;
     struct iio_device  *m_devTx;
     struct iio_channel *m_chnRx0;
+    struct iio_channel* m_chnRxQ;
     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 74c5ecfba..5050d7620 100644
--- a/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp
+++ b/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp
@@ -94,7 +94,7 @@ void PlutoSDRInputThread::run()
         // Refill RX buffer
         nbytes_rx = m_plutoBox->rxBufferRefill();
 
-        if (nbytes_rx != m_blockSizeSamples*2)
+        if (nbytes_rx != m_blockSizeSamples*4)
         {
             qWarning("PlutoSDRInputThread::run: error refilling buf (1) %d / %d",(int) nbytes_rx, (int)  m_blockSizeSamples*2);
             usleep(200000);
@@ -111,13 +111,14 @@ void PlutoSDRInputThread::run()
 
         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);
+            m_buf[ihs++] = *(((int16_t*)p_dat)+1);
 //            iio_channel_convert(m_plutoBox->getRxChannel0(), (void *) &m_bufConv[ihs], (const void *) &m_buf[ihs]);
-            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();
+        /*nbytes_rx = m_plutoBox->rxBufferRefill();
 
         if (nbytes_rx != m_blockSizeSamples*2)
         {
@@ -139,7 +140,7 @@ void PlutoSDRInputThread::run()
 //            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 {