From 54222ff016730c16729967b261f6ac06706f542a Mon Sep 17 00:00:00 2001 From: f4exb Date: Sun, 25 Apr 2021 22:28:40 +0200 Subject: [PATCH] PlutoSDRBox: use channels registered at contruction time --- devices/plutosdr/deviceplutosdrbox.cpp | 145 ++++++++++-------- devices/plutosdr/deviceplutosdrbox.h | 28 ++-- .../plutosdrinput/plutosdrinputthread.cpp | 1 - 3 files changed, 96 insertions(+), 78 deletions(-) diff --git a/devices/plutosdr/deviceplutosdrbox.cpp b/devices/plutosdr/deviceplutosdrbox.cpp index e0af8aba5..39da90601 100644 --- a/devices/plutosdr/deviceplutosdrbox.cpp +++ b/devices/plutosdr/deviceplutosdrbox.cpp @@ -36,16 +36,12 @@ DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) : m_lpfFIRlog2Decim(0), m_lpfFIRRxGain(0), m_lpfFIRTxGain(0), - m_ctx(0), - m_devPhy(0), - m_devRx(0), - m_devTx(0), - m_chnRx0i(0), - m_chnRx0q(0), - m_chnTx0i(0), - m_chnTx0q(0), - m_rxBuf(0), - m_txBuf(0), + m_ctx(nullptr), + m_devPhy(nullptr), + m_devRx(nullptr), + m_devTx(nullptr), + m_rxBuf(nullptr), + m_txBuf(nullptr), m_xoInitial(0), m_temp(0.0f) { @@ -64,16 +60,40 @@ DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) : m_valid = m_ctx && m_devPhy && m_devRx && m_devTx; - if (m_valid) { + if (m_valid) + { + std::regex channelIdReg("voltage([0-9]+)"); + getXO(); -// int nb_channels = iio_device_get_channels_count(m_devRx); -// for (int i = 0; i < nb_channels; i++) { -// iio_channel_disable(iio_device_get_channel(m_devRx, i)); -// } -// nb_channels = iio_device_get_channels_count(m_devTx); -// for (int i = 0; i < nb_channels; i++) { -// iio_channel_disable(iio_device_get_channel(m_devTx, i)); -// } + int nbRxChannels = iio_device_get_channels_count(m_devRx); + + for (int i = 0; i < nbRxChannels; i++) + { + iio_channel *chn = iio_device_get_channel(m_devRx, i); + std::string channelId(iio_channel_get_id(chn)); + + if (std::regex_match(channelId, channelIdReg)) + { + m_rxChannelIds.append(QString(channelId.c_str())); + m_rxChannels.append(chn); + qDebug("DevicePlutoSDRBox::DevicePlutoSDRBox: Rx: %s", channelId.c_str()); + } + } + + int nbTxChannels = iio_device_get_channels_count(m_devTx); + + for (int i = 0; i < nbTxChannels; i++) + { + iio_channel *chn = iio_device_get_channel(m_devTx, i); + std::string channelId(iio_channel_get_id(chn)); + + if (std::regex_match(channelId, channelIdReg)) + { + m_txChannelIds.append(QString(channelId.c_str())); + m_txChannels.append(chn); + qDebug("DevicePlutoSDRBox::DevicePlutoSDRBox: Tx: %s", channelId.c_str()); + } + } } } @@ -259,14 +279,11 @@ bool DevicePlutoSDRBox::openRx() { if (!m_valid) { return false; } - if (!m_chnRx0i) { - m_chnRx0i = iio_device_find_channel(m_devRx, "voltage0", false); - } + if (m_rxChannels.size() > 0) + { + iio_channel_enable(m_rxChannels.at(0)); - if (m_chnRx0i) { - iio_channel_enable(m_chnRx0i); - - const struct iio_data_format *df = iio_channel_get_data_format(m_chnRx0i); + const struct iio_data_format *df = iio_channel_get_data_format(m_rxChannels.at(0)); 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, @@ -276,19 +293,18 @@ bool DevicePlutoSDRBox::openRx() df->with_scale? "true" : "false", df->scale, df->repeat); - } else { + } + 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_rxChannels.size() > 1) + { + iio_channel_enable(m_rxChannels.at(1)); - if (m_chnRx0q) { - iio_channel_enable(m_chnRx0q); - - const struct iio_data_format* df = iio_channel_get_data_format(m_chnRx0q); + const struct iio_data_format* df = iio_channel_get_data_format(m_rxChannels.at(1)); 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, @@ -300,7 +316,8 @@ bool DevicePlutoSDRBox::openRx() df->repeat); return true; } - else { + else + { std::cerr << "DevicePlutoSDRBox::openRx: failed" << std::endl; return false; } @@ -310,13 +327,10 @@ bool DevicePlutoSDRBox::openTx() { if (!m_valid) { return false; } - if (!m_chnTx0i) { - m_chnTx0i = iio_device_find_channel(m_devTx, "voltage0", true); - } - - if (m_chnTx0i) { - iio_channel_enable(m_chnTx0i); - const struct iio_data_format *df = iio_channel_get_data_format(m_chnTx0i); + if (m_txChannels.size() > 0) + { + iio_channel_enable(m_txChannels.at(0)); + const struct iio_data_format *df = iio_channel_get_data_format(m_txChannels.at(0)); qDebug("DevicePlutoSDRBox::openTx: channel I: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", df->length, df->bits, @@ -326,18 +340,17 @@ bool DevicePlutoSDRBox::openTx() df->with_scale? "true" : "false", df->scale, df->repeat); - } else { + } + else + { std::cerr << "DevicePlutoSDRBox::openTx: failed to open I channel" << std::endl; return false; } - if (!m_chnTx0q) { - m_chnTx0q = iio_device_find_channel(m_devTx, "voltage1", true); - } - - if (m_chnTx0q) { - iio_channel_enable(m_chnTx0q); - const struct iio_data_format *df = iio_channel_get_data_format(m_chnTx0q); + if (m_txChannels.size() > 1) + { + iio_channel_enable(m_txChannels.at(1)); + const struct iio_data_format *df = iio_channel_get_data_format(m_txChannels.at(1)); qDebug("DevicePlutoSDRBox::openTx: channel Q: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", df->length, df->bits, @@ -348,7 +361,9 @@ bool DevicePlutoSDRBox::openTx() df->scale, df->repeat); return true; - } else { + } + else + { std::cerr << "DevicePlutoSDRBox::openTx: failed to open Q channel" << std::endl; return false; } @@ -356,14 +371,14 @@ bool DevicePlutoSDRBox::openTx() void DevicePlutoSDRBox::closeRx() { - if (m_chnRx0i) { iio_channel_disable(m_chnRx0i); } - if (m_chnRx0q) { iio_channel_disable(m_chnRx0q); } + if (m_rxChannels.size() > 0) { iio_channel_disable(m_rxChannels.at(0)); } + if (m_rxChannels.size() > 1) { iio_channel_disable(m_rxChannels.at(1)); } } void DevicePlutoSDRBox::closeTx() { - if (m_chnTx0i) { iio_channel_disable(m_chnTx0i); } - if (m_chnTx0q) { iio_channel_disable(m_chnTx0q); } + if (m_txChannels.size() > 0) { iio_channel_disable(m_txChannels.at(0)); } + if (m_txChannels.size() > 1) { iio_channel_disable(m_txChannels.at(1)); } } struct iio_buffer *DevicePlutoSDRBox::createRxBuffer(unsigned int size, bool cyclic) @@ -454,16 +469,16 @@ char* DevicePlutoSDRBox::rxBufferEnd() if (m_rxBuf) { return (char *) iio_buffer_end(m_rxBuf); } else { - return 0; + return nullptr; } } char* DevicePlutoSDRBox::rxBufferFirst() { if (m_rxBuf) { - return (char *) iio_buffer_first(m_rxBuf, m_chnRx0i); + return (char *) iio_buffer_first(m_rxBuf, m_rxChannels.at(0)); } else { - return 0; + return nullptr; } } @@ -481,26 +496,26 @@ char* DevicePlutoSDRBox::txBufferEnd() if (m_txBuf) { return (char *) iio_buffer_end(m_txBuf); } else { - return 0; + return nullptr; } } char* DevicePlutoSDRBox::txBufferFirst() { if (m_txBuf) { - return (char *) iio_buffer_first(m_txBuf, m_chnTx0i); + return (char *) iio_buffer_first(m_txBuf, m_txChannels.at(0)); } else { - return 0; + return nullptr; } } void DevicePlutoSDRBox::txChannelConvert(int16_t *dst, int16_t *src) { - if (m_chnTx0i) { - iio_channel_convert_inverse(m_chnTx0i, &dst[0], &src[0]); + if (m_txChannels.size() > 0) { + iio_channel_convert_inverse(m_txChannels.at(0), &dst[0], &src[0]); } - if (m_chnTx0q) { - iio_channel_convert_inverse(m_chnTx0q, &dst[1], &src[1]); + if (m_txChannels.size() > 1) { + iio_channel_convert_inverse(m_txChannels.at(1), &dst[1], &src[1]); } } diff --git a/devices/plutosdr/deviceplutosdrbox.h b/devices/plutosdr/deviceplutosdrbox.h index 904b6439e..314abdd12 100644 --- a/devices/plutosdr/deviceplutosdrbox.h +++ b/devices/plutosdr/deviceplutosdrbox.h @@ -21,8 +21,11 @@ #include #include #include -#include "deviceplutosdrscan.h" +#include +#include + +#include "deviceplutosdrscan.h" #include "export.h" #if defined(_MSC_VER) @@ -30,6 +33,8 @@ typedef SSIZE_T ssize_t; #endif +struct iio_channel; + class DEVICES_API DevicePlutoSDRBox { public: @@ -75,19 +80,18 @@ public: void set_params(DeviceType devType, const std::vector ¶ms); bool get_param(DeviceType devType, const std::string ¶m, std::string &value); - bool openRx(); - bool openTx(); - void closeRx(); - void closeTx(); + bool openRx(); //!< Open first Rx (Rx0) + bool openTx(); //!< Open first Tx (Tx0) + void closeRx(); //!< Close first Rx (Rx0) + void closeTx(); //!< Close first Tx (Tx0) + int getNbRx() const { return m_rxChannels.size() / 2; } + int getNbTx() const { return m_txChannels.size() / 2; } struct iio_buffer *createRxBuffer(unsigned int size, bool cyclic); struct iio_buffer *createTxBuffer(unsigned int size, bool cyclic); void deleteRxBuffer(); void deleteTxBuffer(); ssize_t getRxSampleSize(); ssize_t getTxSampleSize(); - struct iio_channel *getRxChannel0() { return m_chnRx0i; } - struct iio_channel *getTxChannel0I() { return m_chnTx0i; } - struct iio_channel *getTxChannel0Q() { return m_chnTx0q; } ssize_t rxBufferRefill(); ssize_t txBufferPush(); std::ptrdiff_t rxBufferStep(); @@ -119,15 +123,15 @@ private: struct iio_device *m_devPhy; struct iio_device *m_devRx; struct iio_device *m_devTx; - 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; struct iio_buffer *m_txBuf; bool m_valid; int64_t m_xoInitial; float m_temp; + QList m_rxChannelIds; + QList m_rxChannels; + QList m_txChannelIds; + QList m_txChannels; bool parseSampleRates(const std::string& rateStr, SampleRates& sampleRates); void setFilter(const std::string& filterConfigStr); diff --git a/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp b/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp index 93c0cc711..86e32e557 100644 --- a/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp +++ b/plugins/samplesource/plutosdrinput/plutosdrinputthread.cpp @@ -113,7 +113,6 @@ void PlutoSDRInputThread::run() { 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]); } if (m_iqOrder) {