2018-03-27 17:35:53 +00:00
|
|
|
/*!
|
|
|
|
* \file ad9361_manager.cc
|
|
|
|
* \brief An Analog Devices AD9361 front-end configuration library wrapper for configure some functions via iiod link.
|
|
|
|
* \author Javier Arribas, jarribas(at)cttc.es
|
|
|
|
*
|
|
|
|
* This file contains information taken from librtlsdr:
|
|
|
|
* http://git.osmocom.org/rtl-sdr/
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*
|
2019-07-26 10:38:20 +00:00
|
|
|
* Copyright (C) 2010-2019 (see AUTHORS file for a list of contributors)
|
2018-03-27 17:35:53 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR is a software defined Global Navigation
|
|
|
|
* Satellite Systems receiver
|
|
|
|
*
|
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
|
|
|
* GNSS-SDR is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GNSS-SDR is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-05-13 20:49:11 +00:00
|
|
|
* along with GNSS-SDR. If not, see <https://www.gnu.org/licenses/>.
|
2018-03-27 17:35:53 +00:00
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "ad9361_manager.h"
|
|
|
|
#include <glog/logging.h>
|
2019-10-08 17:34:18 +00:00
|
|
|
#include <ad9361.h>
|
2018-03-28 06:35:33 +00:00
|
|
|
#include <cmath>
|
2019-10-08 17:34:18 +00:00
|
|
|
#include <fstream> // for ifstream
|
2018-03-27 17:35:53 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2019-10-08 17:34:18 +00:00
|
|
|
#include <vector>
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
/* check return value of attr_write function */
|
2018-03-27 19:10:31 +00:00
|
|
|
void errchk(int v, const char *what)
|
|
|
|
{
|
2018-03-27 17:35:53 +00:00
|
|
|
if (v < 0)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
LOG(WARNING) << "Error " << v << " writing to channel " << what << " value may not be supported. ";
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-08-11 12:31:35 +00:00
|
|
|
/* write attribute: int64_t int */
|
|
|
|
void wr_ch_lli(struct iio_channel *chn, const char *what, int64_t val)
|
2018-03-27 17:35:53 +00:00
|
|
|
{
|
|
|
|
errchk(iio_channel_attr_write_longlong(chn, what, val), what);
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-03-27 17:35:53 +00:00
|
|
|
/* write attribute: string */
|
2018-03-27 19:10:31 +00:00
|
|
|
void wr_ch_str(struct iio_channel *chn, const char *what, const char *str)
|
2018-03-27 17:35:53 +00:00
|
|
|
{
|
|
|
|
errchk(iio_channel_attr_write(chn, what, str), what);
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-03-27 17:35:53 +00:00
|
|
|
/* returns ad9361 phy device */
|
2018-03-27 19:10:31 +00:00
|
|
|
struct iio_device *get_ad9361_phy(struct iio_context *ctx)
|
2018-03-27 17:35:53 +00:00
|
|
|
{
|
2018-03-27 19:10:31 +00:00
|
|
|
struct iio_device *dev = iio_context_find_device(ctx, "ad9361-phy");
|
2018-03-27 17:35:53 +00:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-03-27 17:35:53 +00:00
|
|
|
/* finds AD9361 streaming IIO devices */
|
|
|
|
bool get_ad9361_stream_dev(struct iio_context *ctx, enum iodev d, struct iio_device **dev)
|
|
|
|
{
|
2018-03-27 19:10:31 +00:00
|
|
|
switch (d)
|
|
|
|
{
|
|
|
|
case TX:
|
|
|
|
*dev = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
|
2018-12-10 18:29:00 +00:00
|
|
|
return *dev != nullptr;
|
2018-03-27 19:10:31 +00:00
|
|
|
case RX:
|
|
|
|
*dev = iio_context_find_device(ctx, "cf-ad9361-lpc");
|
2018-12-10 18:29:00 +00:00
|
|
|
return *dev != nullptr;
|
2018-03-27 19:10:31 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-03-27 17:35:53 +00:00
|
|
|
/* finds AD9361 streaming IIO channels */
|
2018-04-06 07:39:11 +00:00
|
|
|
bool get_ad9361_stream_ch(struct iio_context *ctx __attribute__((unused)), enum iodev d, struct iio_device *dev, int chid, struct iio_channel **chn)
|
2018-03-27 17:35:53 +00:00
|
|
|
{
|
|
|
|
std::stringstream name;
|
|
|
|
name.str("");
|
2018-03-27 19:10:31 +00:00
|
|
|
name << "voltage";
|
|
|
|
name << chid;
|
|
|
|
*chn = iio_device_find_channel(dev, name.str().c_str(), d == TX);
|
2018-03-27 17:35:53 +00:00
|
|
|
if (!*chn)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
name.str("");
|
|
|
|
name << "altvoltage";
|
|
|
|
name << chid;
|
|
|
|
*chn = iio_device_find_channel(dev, name.str().c_str(), d == TX);
|
|
|
|
}
|
2018-12-10 18:29:00 +00:00
|
|
|
return *chn != nullptr;
|
2018-03-27 17:35:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-03-27 17:35:53 +00:00
|
|
|
/* finds AD9361 phy IIO configuration channel with id chid */
|
|
|
|
bool get_phy_chan(struct iio_context *ctx, enum iodev d, int chid, struct iio_channel **chn)
|
|
|
|
{
|
|
|
|
std::stringstream name;
|
2018-03-27 19:10:31 +00:00
|
|
|
switch (d)
|
|
|
|
{
|
2018-03-27 17:35:53 +00:00
|
|
|
case RX:
|
|
|
|
name.str("");
|
2018-03-27 19:10:31 +00:00
|
|
|
name << "voltage";
|
|
|
|
name << chid;
|
|
|
|
*chn = iio_device_find_channel(get_ad9361_phy(ctx), name.str().c_str(), false);
|
2018-12-10 18:29:00 +00:00
|
|
|
return *chn != nullptr;
|
2018-03-27 17:35:53 +00:00
|
|
|
break;
|
|
|
|
case TX:
|
|
|
|
name.str("");
|
2018-03-27 19:10:31 +00:00
|
|
|
name << "voltage";
|
|
|
|
name << chid;
|
2018-03-27 17:35:53 +00:00
|
|
|
*chn = iio_device_find_channel(get_ad9361_phy(ctx), name.str().c_str(), true);
|
2018-12-10 18:29:00 +00:00
|
|
|
return *chn != nullptr;
|
2018-03-27 17:35:53 +00:00
|
|
|
break;
|
2018-03-27 19:10:31 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-03-27 17:35:53 +00:00
|
|
|
/* finds AD9361 local oscillator IIO configuration channels */
|
|
|
|
bool get_lo_chan(struct iio_context *ctx, enum iodev d, struct iio_channel **chn)
|
|
|
|
{
|
2018-03-27 19:10:31 +00:00
|
|
|
switch (d)
|
|
|
|
{
|
2018-03-27 17:35:53 +00:00
|
|
|
// LO chan is always output, i.e. true
|
|
|
|
case RX:
|
|
|
|
*chn = iio_device_find_channel(get_ad9361_phy(ctx), "altvoltage0", true);
|
2018-12-10 18:29:00 +00:00
|
|
|
return *chn != nullptr;
|
2018-03-27 17:35:53 +00:00
|
|
|
case TX:
|
2018-03-27 19:10:31 +00:00
|
|
|
*chn = iio_device_find_channel(get_ad9361_phy(ctx), "altvoltage1", true);
|
2018-12-10 18:29:00 +00:00
|
|
|
return *chn != nullptr;
|
2018-03-27 17:35:53 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2018-03-27 19:10:31 +00:00
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* applies streaming configuration through IIO */
|
|
|
|
bool cfg_ad9361_streaming_ch(struct iio_context *ctx, struct stream_cfg *cfg, enum iodev type, int chid)
|
|
|
|
{
|
2018-12-10 18:29:00 +00:00
|
|
|
struct iio_channel *chn = nullptr;
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
// Configure phy and lo channels
|
2019-08-18 20:16:13 +00:00
|
|
|
// LOG(INFO)<<"* Acquiring AD9361 phy channel"<<chid;
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Acquiring AD9361 phy channel" << chid << std::endl;
|
|
|
|
if (!get_phy_chan(ctx, type, chid, &chn))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wr_ch_str(chn, "rf_port_select", cfg->rfport);
|
|
|
|
wr_ch_lli(chn, "rf_bandwidth", cfg->bw_hz);
|
2018-03-27 17:35:53 +00:00
|
|
|
wr_ch_lli(chn, "sampling_frequency", cfg->fs_hz);
|
|
|
|
|
|
|
|
// Configure LO channel
|
2019-08-18 20:16:13 +00:00
|
|
|
// LOG(INFO)<<"* Acquiring AD9361 "<<type == TX ? "TX" : "RX";
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Acquiring AD9361 " << (type == TX ? "TX" : "RX") << std::endl;
|
|
|
|
if (!get_lo_chan(ctx, type, &chn))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
wr_ch_lli(chn, "frequency", cfg->lo_hz);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-11 12:31:35 +00:00
|
|
|
bool config_ad9361_rx_local(uint64_t bandwidth_,
|
|
|
|
uint64_t sample_rate_,
|
|
|
|
uint64_t freq_,
|
2018-12-11 00:56:25 +00:00
|
|
|
const std::string &rf_port_select_,
|
|
|
|
const std::string &gain_mode_rx1_,
|
|
|
|
const std::string &gain_mode_rx2_,
|
2018-03-27 19:10:31 +00:00
|
|
|
double rf_gain_rx1_,
|
2019-10-06 17:07:24 +00:00
|
|
|
double rf_gain_rx2_,
|
|
|
|
bool quadrature_,
|
|
|
|
bool rfdc_,
|
2019-10-09 18:50:06 +00:00
|
|
|
bool bbdc_,
|
2019-10-09 20:08:08 +00:00
|
|
|
std::string filter_source_,
|
2019-10-09 18:50:06 +00:00
|
|
|
std::string filter_filename_,
|
|
|
|
float Fpass_,
|
|
|
|
float Fstop_)
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// RX stream config
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "AD9361 Acquiring IIO LOCAL context\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_context *ctx;
|
|
|
|
// Streaming devices
|
|
|
|
struct iio_device *rx;
|
2019-10-09 07:18:29 +00:00
|
|
|
struct iio_channel *rx_chan1;
|
|
|
|
struct iio_channel *rx_chan2;
|
2019-10-09 18:50:06 +00:00
|
|
|
int ret;
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-10-09 20:08:08 +00:00
|
|
|
#ifndef LIBAD9361_VERSION_GREATER_THAN_01
|
|
|
|
if (filter_source_ == "Design")
|
|
|
|
{
|
|
|
|
std::cout << "Option filter_source=Design is not available in this version. Set to filter_source=Off" << std::endl;
|
|
|
|
filter_source_ = std::string("Off");
|
|
|
|
}
|
|
|
|
if (Fpass_ != 0.0 or Fstop_ != 0.0)
|
|
|
|
{
|
|
|
|
Fpass_ = 0.0;
|
|
|
|
Fstop_ = 0.0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-27 17:35:53 +00:00
|
|
|
ctx = iio_create_default_context();
|
|
|
|
if (!ctx)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "No context\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No context");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
if (iio_context_get_devices_count(ctx) <= 0)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "No devices\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No devices");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-10-09 18:50:06 +00:00
|
|
|
struct iio_device *ad9361_phy;
|
|
|
|
ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-10-09 18:50:06 +00:00
|
|
|
std::cout << "* Acquiring AD9361 streaming devices\n";
|
2018-03-27 19:10:31 +00:00
|
|
|
if (!get_ad9361_stream_dev(ctx, RX, &rx))
|
|
|
|
{
|
|
|
|
std::cout << "No rx dev found\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No rx dev found");
|
|
|
|
};
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Initializing AD9361 IIO streaming channels\n";
|
2019-10-09 07:18:29 +00:00
|
|
|
if (!get_ad9361_stream_ch(ctx, RX, rx, 0, &rx_chan1))
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
2019-10-09 07:18:29 +00:00
|
|
|
std::cout << "RX channel 1 not found\n";
|
|
|
|
throw std::runtime_error("RX channel 1 not found");
|
2018-03-27 19:10:31 +00:00
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-10-09 07:18:29 +00:00
|
|
|
if (!get_ad9361_stream_ch(ctx, RX, rx, 1, &rx_chan2))
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
2019-10-09 07:18:29 +00:00
|
|
|
std::cout << "RX channel 2 not found\n";
|
|
|
|
throw std::runtime_error("RX channel 2 not found");
|
2018-03-27 19:10:31 +00:00
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-10-09 18:50:06 +00:00
|
|
|
if (filter_source_ == "Off")
|
|
|
|
{
|
|
|
|
struct stream_cfg rxcfg;
|
|
|
|
rxcfg.bw_hz = bandwidth_;
|
|
|
|
rxcfg.fs_hz = sample_rate_;
|
|
|
|
rxcfg.lo_hz = freq_;
|
|
|
|
rxcfg.rfport = rf_port_select_.c_str();
|
|
|
|
|
|
|
|
if (!cfg_ad9361_streaming_ch(ctx, &rxcfg, RX, 0))
|
|
|
|
{
|
|
|
|
std::cout << "RX port 0 not found\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO RX port 0 not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (filter_source_ == "Auto")
|
|
|
|
{
|
|
|
|
ret = ad9361_set_bb_rate(ad9361_phy, sample_rate_);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set BB rate");
|
|
|
|
// set bw
|
|
|
|
//params.push_back("in_voltage_rf_bandwidth=" + boost::to_string(bandwidth));
|
|
|
|
}
|
|
|
|
//wr_ch_str(rx_chan1, "rf_port_select", rf_port_select_.c_str());
|
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage0_rf_port_select", rf_port_select_.c_str());
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set rf_port_select");
|
|
|
|
}
|
|
|
|
wr_ch_lli(rx_chan1, "rf_bandwidth", bandwidth_);
|
|
|
|
if (!get_lo_chan(ctx, RX, &rx_chan1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wr_ch_lli(rx_chan1, "frequency", freq_);
|
|
|
|
}
|
|
|
|
else if (filter_source_ == "File")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!load_fir_filter(filter_filename_, ad9361_phy))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to load filter file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::runtime_error &e)
|
|
|
|
{
|
|
|
|
std::cout << "Exception cached when configuring the RX FIR filter: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage0_rf_port_select", rf_port_select_.c_str());
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set rf_port_select");
|
|
|
|
}
|
|
|
|
wr_ch_lli(rx_chan1, "rf_bandwidth", bandwidth_);
|
|
|
|
if (!get_lo_chan(ctx, RX, &rx_chan1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wr_ch_lli(rx_chan1, "frequency", freq_);
|
|
|
|
}
|
2019-10-09 20:08:08 +00:00
|
|
|
#if LIBAD9361_VERSION_GREATER_THAN_01
|
2019-10-09 18:50:06 +00:00
|
|
|
else if (filter_source_ == "Design")
|
|
|
|
{
|
|
|
|
ret = ad9361_set_bb_rate_custom_filter_manual(
|
|
|
|
ad9361_phy, sample_rate_, static_cast<uint64_t>(Fpass_), static_cast<uint64_t>(Fstop_), bandwidth_, bandwidth_);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set BB rate");
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage0_rf_port_select", rf_port_select_.c_str());
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set rf_port_select");
|
|
|
|
}
|
|
|
|
wr_ch_lli(rx_chan1, "rf_bandwidth", bandwidth_);
|
|
|
|
if (!get_lo_chan(ctx, RX, &rx_chan1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wr_ch_lli(rx_chan1, "frequency", freq_);
|
|
|
|
}
|
2019-10-09 20:08:08 +00:00
|
|
|
#endif
|
2019-10-09 18:50:06 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unknown filter configuration");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filters can only be disabled after the sample rate has been set
|
|
|
|
if (filter_source_ == "Off")
|
|
|
|
{
|
|
|
|
ret = ad9361_set_trx_fir_enable(ad9361_phy, false);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to disable filters");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Enabling IIO streaming channels\n";
|
2019-10-09 07:18:29 +00:00
|
|
|
iio_channel_enable(rx_chan1);
|
|
|
|
iio_channel_enable(rx_chan2);
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
ret = iio_device_attr_write(ad9361_phy, "trx_rate_governor", "nominal");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set trx_rate_governor: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write(ad9361_phy, "ensm_mode", "fdd");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set ensm_mode: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write(ad9361_phy, "calib_mode", "auto");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set calib_mode: " << ret << std::endl;
|
|
|
|
}
|
2019-10-06 17:07:24 +00:00
|
|
|
ret = iio_device_attr_write_bool(ad9361_phy, "in_voltage_quadrature_tracking_en", quadrature_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage_quadrature_tracking_en: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write_bool(ad9361_phy, "in_voltage_rf_dc_offset_tracking_en", rfdc_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage_rf_dc_offset_tracking_en: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write_bool(ad9361_phy, "in_voltage_bb_dc_offset_tracking_en", bbdc_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage_bb_dc_offset_tracking_en: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 19:10:31 +00:00
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage0_gain_control_mode", gain_mode_rx1_.c_str());
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage0_gain_control_mode: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage1_gain_control_mode", gain_mode_rx2_.c_str());
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage1_gain_control_mode: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "in_voltage0_hardwaregain", rf_gain_rx1_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage0_hardwaregain: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "in_voltage1_hardwaregain", rf_gain_rx2_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage1_hardwaregain: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "End of AD9361 RX configuration.\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
iio_context_destroy(ctx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-12-11 00:56:25 +00:00
|
|
|
bool config_ad9361_rx_remote(const std::string &remote_host,
|
2018-08-11 12:31:35 +00:00
|
|
|
uint64_t bandwidth_,
|
|
|
|
uint64_t sample_rate_,
|
|
|
|
uint64_t freq_,
|
2018-12-11 00:56:25 +00:00
|
|
|
const std::string &rf_port_select_,
|
|
|
|
const std::string &gain_mode_rx1_,
|
|
|
|
const std::string &gain_mode_rx2_,
|
2018-03-27 19:10:31 +00:00
|
|
|
double rf_gain_rx1_,
|
2019-10-06 17:07:24 +00:00
|
|
|
double rf_gain_rx2_,
|
|
|
|
bool quadrature_,
|
|
|
|
bool rfdc_,
|
2019-10-09 18:50:06 +00:00
|
|
|
bool bbdc_,
|
2019-10-09 20:08:08 +00:00
|
|
|
std::string filter_source_,
|
2019-10-09 18:50:06 +00:00
|
|
|
std::string filter_filename_,
|
|
|
|
float Fpass_,
|
|
|
|
float Fstop_)
|
2018-03-27 17:35:53 +00:00
|
|
|
{
|
|
|
|
// RX stream config
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "AD9361 Acquiring IIO REMOTE context in host " << remote_host << std::endl;
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_context *ctx;
|
|
|
|
// Streaming devices
|
|
|
|
struct iio_device *rx;
|
2019-10-09 07:18:29 +00:00
|
|
|
struct iio_channel *rx_chan1;
|
|
|
|
struct iio_channel *rx_chan2;
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-10-09 20:08:08 +00:00
|
|
|
#ifndef LIBAD9361_VERSION_GREATER_THAN_01
|
|
|
|
if (filter_source_ == "Design")
|
|
|
|
{
|
|
|
|
std::cout << "Option filter_source=Design is not available in this version. Set to filter_source=Off" << std::endl;
|
|
|
|
filter_source_ = std::string("Off");
|
|
|
|
}
|
|
|
|
if (Fpass_ != 0.0 or Fstop_ != 0.0)
|
|
|
|
{
|
|
|
|
Fpass_ = 0.0;
|
|
|
|
Fstop_ = 0.0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-27 17:35:53 +00:00
|
|
|
ctx = iio_create_network_context(remote_host.c_str());
|
|
|
|
if (!ctx)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "No context\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No context");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
if (iio_context_get_devices_count(ctx) <= 0)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "No devices\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No devices");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Acquiring AD9361 streaming devices\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
if (!get_ad9361_stream_dev(ctx, RX, &rx))
|
|
|
|
{
|
|
|
|
std::cout << "No rx dev found\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No rx dev found");
|
|
|
|
};
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-10-08 17:34:18 +00:00
|
|
|
std::cout << "* Configuring AD9361 for streaming\n";
|
|
|
|
|
2019-10-08 12:55:16 +00:00
|
|
|
struct iio_device *ad9361_phy;
|
|
|
|
ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
|
|
|
|
int ret;
|
|
|
|
|
2019-10-08 17:34:18 +00:00
|
|
|
std::cout << "* Initializing AD9361 IIO streaming channels\n";
|
2019-10-09 07:18:29 +00:00
|
|
|
if (!get_ad9361_stream_ch(ctx, RX, rx, 0, &rx_chan1))
|
2019-10-08 17:34:18 +00:00
|
|
|
{
|
2019-10-09 17:05:31 +00:00
|
|
|
std::cout << "RX channel 1 not found\n";
|
|
|
|
throw std::runtime_error("RX channel 1 not found");
|
2019-10-08 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 07:18:29 +00:00
|
|
|
if (!get_ad9361_stream_ch(ctx, RX, rx, 1, &rx_chan2))
|
2019-10-08 17:34:18 +00:00
|
|
|
{
|
2019-10-09 17:05:31 +00:00
|
|
|
std::cout << "RX channel 2 not found\n";
|
|
|
|
throw std::runtime_error("RX channel 2 not found");
|
2019-10-08 17:34:18 +00:00
|
|
|
}
|
2019-10-08 12:55:16 +00:00
|
|
|
|
|
|
|
if (filter_source_ == "Off")
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
2019-10-08 12:55:16 +00:00
|
|
|
struct stream_cfg rxcfg;
|
2019-10-08 17:34:18 +00:00
|
|
|
rxcfg.bw_hz = bandwidth_;
|
|
|
|
rxcfg.fs_hz = sample_rate_;
|
|
|
|
rxcfg.lo_hz = freq_;
|
|
|
|
rxcfg.rfport = rf_port_select_.c_str();
|
2019-10-08 12:55:16 +00:00
|
|
|
|
|
|
|
if (!cfg_ad9361_streaming_ch(ctx, &rxcfg, RX, 0))
|
|
|
|
{
|
|
|
|
std::cout << "RX port 0 not found\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO RX port 0 not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (filter_source_ == "Auto")
|
|
|
|
{
|
|
|
|
ret = ad9361_set_bb_rate(ad9361_phy, sample_rate_);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set BB rate");
|
|
|
|
// set bw
|
|
|
|
//params.push_back("in_voltage_rf_bandwidth=" + boost::to_string(bandwidth));
|
|
|
|
}
|
2019-10-09 07:18:29 +00:00
|
|
|
//wr_ch_str(rx_chan1, "rf_port_select", rf_port_select_.c_str());
|
2019-10-08 17:34:18 +00:00
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage0_rf_port_select", rf_port_select_.c_str());
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set rf_port_select");
|
|
|
|
}
|
2019-10-09 07:18:29 +00:00
|
|
|
wr_ch_lli(rx_chan1, "rf_bandwidth", bandwidth_);
|
|
|
|
if (!get_lo_chan(ctx, RX, &rx_chan1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wr_ch_lli(rx_chan1, "frequency", freq_);
|
2019-10-08 12:55:16 +00:00
|
|
|
}
|
|
|
|
else if (filter_source_ == "File")
|
|
|
|
{
|
2019-10-08 17:34:18 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!load_fir_filter(filter_filename_, ad9361_phy))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to load filter file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::runtime_error &e)
|
2019-10-08 12:55:16 +00:00
|
|
|
{
|
2019-10-08 17:34:18 +00:00
|
|
|
std::cout << "Exception cached when configuring the RX FIR filter: " << e.what() << std::endl;
|
2019-10-08 12:55:16 +00:00
|
|
|
}
|
2019-10-08 17:34:18 +00:00
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage0_rf_port_select", rf_port_select_.c_str());
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set rf_port_select");
|
|
|
|
}
|
2019-10-09 07:18:29 +00:00
|
|
|
wr_ch_lli(rx_chan1, "rf_bandwidth", bandwidth_);
|
|
|
|
if (!get_lo_chan(ctx, RX, &rx_chan1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wr_ch_lli(rx_chan1, "frequency", freq_);
|
2019-10-08 12:55:16 +00:00
|
|
|
}
|
2019-10-09 20:08:08 +00:00
|
|
|
#if LIBAD9361_VERSION_GREATER_THAN_01
|
2019-10-08 12:55:16 +00:00
|
|
|
else if (filter_source_ == "Design")
|
|
|
|
{
|
|
|
|
ret = ad9361_set_bb_rate_custom_filter_manual(
|
2019-10-09 07:18:29 +00:00
|
|
|
ad9361_phy, sample_rate_, static_cast<uint64_t>(Fpass_), static_cast<uint64_t>(Fstop_), bandwidth_, bandwidth_);
|
2019-10-08 12:55:16 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set BB rate");
|
|
|
|
}
|
2019-10-08 17:34:18 +00:00
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage0_rf_port_select", rf_port_select_.c_str());
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to set rf_port_select");
|
|
|
|
}
|
2019-10-09 07:18:29 +00:00
|
|
|
wr_ch_lli(rx_chan1, "rf_bandwidth", bandwidth_);
|
|
|
|
if (!get_lo_chan(ctx, RX, &rx_chan1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wr_ch_lli(rx_chan1, "frequency", freq_);
|
2019-10-08 12:55:16 +00:00
|
|
|
}
|
2019-10-09 20:08:08 +00:00
|
|
|
#endif
|
2019-10-08 12:55:16 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unknown filter configuration");
|
2018-03-27 19:10:31 +00:00
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-10-08 17:34:18 +00:00
|
|
|
// Filters can only be disabled after the sample rate has been set
|
|
|
|
if (filter_source_ == "Off")
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
2019-10-08 17:34:18 +00:00
|
|
|
ret = ad9361_set_trx_fir_enable(ad9361_phy, false);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to disable filters");
|
|
|
|
}
|
2018-03-27 19:10:31 +00:00
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Enabling IIO streaming channels\n";
|
2019-10-09 07:18:29 +00:00
|
|
|
iio_channel_enable(rx_chan1);
|
|
|
|
iio_channel_enable(rx_chan2);
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
ret = iio_device_attr_write(ad9361_phy, "trx_rate_governor", "nominal");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set trx_rate_governor: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write(ad9361_phy, "ensm_mode", "fdd");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set ensm_mode: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write(ad9361_phy, "calib_mode", "auto");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set calib_mode: " << ret << std::endl;
|
|
|
|
}
|
2019-10-06 17:07:24 +00:00
|
|
|
ret = iio_device_attr_write_bool(ad9361_phy, "in_voltage_quadrature_tracking_en", quadrature_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage_quadrature_tracking_en: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write_bool(ad9361_phy, "in_voltage_rf_dc_offset_tracking_en", rfdc_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage_rf_dc_offset_tracking_en: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write_bool(ad9361_phy, "in_voltage_bb_dc_offset_tracking_en", bbdc_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage_bb_dc_offset_tracking_en: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 19:10:31 +00:00
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage0_gain_control_mode", gain_mode_rx1_.c_str());
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage0_gain_control_mode: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
ret = iio_device_attr_write(ad9361_phy, "in_voltage1_gain_control_mode", gain_mode_rx2_.c_str());
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage1_gain_control_mode: " << ret << std::endl;
|
|
|
|
}
|
2019-10-08 12:55:16 +00:00
|
|
|
if (gain_mode_rx1_ == "manual")
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
2019-10-08 12:55:16 +00:00
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "in_voltage0_hardwaregain", rf_gain_rx1_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage0_hardwaregain: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 19:10:31 +00:00
|
|
|
}
|
2019-10-08 12:55:16 +00:00
|
|
|
if (gain_mode_rx2_ == "manual")
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
2019-10-08 12:55:16 +00:00
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "in_voltage1_hardwaregain", rf_gain_rx2_);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set in_voltage1_hardwaregain: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 19:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "End of AD9361 RX configuration.\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
iio_context_destroy(ctx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-08-11 12:31:35 +00:00
|
|
|
bool config_ad9361_lo_local(uint64_t bandwidth_,
|
|
|
|
uint64_t sample_rate_,
|
|
|
|
uint64_t freq_rf_tx_hz_,
|
2018-03-27 19:10:31 +00:00
|
|
|
double tx_attenuation_db_,
|
2018-08-11 12:31:35 +00:00
|
|
|
int64_t freq_dds_tx_hz_,
|
2019-10-08 10:57:37 +00:00
|
|
|
double scale_dds_dbfs_,
|
|
|
|
double phase_dds_deg_)
|
2018-03-27 17:35:53 +00:00
|
|
|
{
|
|
|
|
// TX stream config
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "Start of AD9361 TX Local Oscillator DDS configuration\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
struct stream_cfg txcfg;
|
|
|
|
txcfg.bw_hz = bandwidth_;
|
|
|
|
txcfg.fs_hz = sample_rate_;
|
|
|
|
txcfg.lo_hz = freq_rf_tx_hz_;
|
|
|
|
txcfg.rfport = "A";
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "AD9361 Acquiring IIO LOCAL context\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_context *ctx;
|
|
|
|
ctx = iio_create_default_context();
|
|
|
|
if (!ctx)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "No context\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No context");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-08-18 20:16:13 +00:00
|
|
|
// find tx device
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_device *tx;
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Acquiring AD9361 TX streaming devices\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
if (!get_ad9361_stream_dev(ctx, TX, &tx))
|
|
|
|
{
|
|
|
|
std::cout << "No tx dev found\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No tx dev found");
|
|
|
|
};
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Configuring AD9361 for streaming TX\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
if (!cfg_ad9361_streaming_ch(ctx, &txcfg, TX, 0))
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "TX port 0 not found\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO TX port 0 not found");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-08-18 20:16:13 +00:00
|
|
|
// ENABLE DDS on TX1
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_device *ad9361_phy;
|
2018-03-27 19:10:31 +00:00
|
|
|
ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
|
2018-03-27 17:35:53 +00:00
|
|
|
int ret;
|
2019-08-18 20:16:13 +00:00
|
|
|
// set output amplifier attenuation
|
2019-10-07 20:45:09 +00:00
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage0_hardwaregain", -std::abs(tx_attenuation_db_));
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-10-07 20:45:09 +00:00
|
|
|
std::cout << "Failed to set out_voltage0_hardwaregain value " << -std::abs(tx_attenuation_db_) << ". Error " << ret << std::endl;
|
|
|
|
}
|
|
|
|
// shut down signal in TX2
|
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage1_hardwaregain", -89.75);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set out_voltage1_hardwaregain value -89.75 dB. Error " << ret << std::endl;
|
2018-03-27 19:10:31 +00:00
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
struct iio_device *dds;
|
2018-03-27 19:10:31 +00:00
|
|
|
dds = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_channel *dds_channel0_I;
|
|
|
|
dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true);
|
|
|
|
|
|
|
|
struct iio_channel *dds_channel0_Q;
|
|
|
|
dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true);
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", true);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to toggle DDS: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-08-18 20:16:13 +00:00
|
|
|
// set frequency, scale and phase
|
2018-08-11 12:31:35 +00:00
|
|
|
ret = iio_channel_attr_write_longlong(dds_channel0_I, "frequency", static_cast<int64_t>(freq_dds_tx_hz_));
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS frequency I: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-08-11 12:31:35 +00:00
|
|
|
ret = iio_channel_attr_write_longlong(dds_channel0_Q, "frequency", static_cast<int64_t>(freq_dds_tx_hz_));
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS frequency Q: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-10-08 10:57:37 +00:00
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_I, "phase", phase_dds_deg_ * 1000.0);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS phase I: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-10-08 10:57:37 +00:00
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_Q, "phase", phase_dds_deg_ * 1000.0 + 270000.0);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS phase Q: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_I, "scale", pow(10, scale_dds_dbfs_ / 20.0));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS scale I: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_Q, "scale", pow(10, scale_dds_dbfs_ / 20.0));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS scale Q: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-08-18 20:16:13 +00:00
|
|
|
// disable TX2
|
2018-03-27 17:35:53 +00:00
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage1_hardwaregain", -89.0);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set out_voltage1_hardwaregain value " << -89.0 << " error " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
struct iio_channel *dds_channel1_I;
|
|
|
|
dds_channel1_I = iio_device_find_channel(dds, "TX2_I_F1", true);
|
|
|
|
|
|
|
|
struct iio_channel *dds_channel1_Q;
|
|
|
|
dds_channel1_Q = iio_device_find_channel(dds, "TX2_Q_F1", true);
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
ret = iio_channel_attr_write_double(dds_channel1_I, "scale", 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX2 DDS scale I: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
ret = iio_channel_attr_write_double(dds_channel1_Q, "scale", 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX2 DDS scale Q: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
iio_context_destroy(ctx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-12-11 00:56:25 +00:00
|
|
|
bool config_ad9361_lo_remote(const std::string &remote_host,
|
2018-08-11 12:31:35 +00:00
|
|
|
uint64_t bandwidth_,
|
|
|
|
uint64_t sample_rate_,
|
|
|
|
uint64_t freq_rf_tx_hz_,
|
2018-03-27 19:10:31 +00:00
|
|
|
double tx_attenuation_db_,
|
2018-08-11 12:31:35 +00:00
|
|
|
int64_t freq_dds_tx_hz_,
|
2019-10-08 10:57:37 +00:00
|
|
|
double scale_dds_dbfs_,
|
|
|
|
double phase_dds_deg_)
|
2018-03-27 17:35:53 +00:00
|
|
|
{
|
|
|
|
// TX stream config
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "Start of AD9361 TX Local Oscillator DDS configuration\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
struct stream_cfg txcfg;
|
|
|
|
txcfg.bw_hz = bandwidth_;
|
|
|
|
txcfg.fs_hz = sample_rate_;
|
|
|
|
txcfg.lo_hz = freq_rf_tx_hz_;
|
|
|
|
txcfg.rfport = "A";
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "AD9361 Acquiring IIO REMOTE context in host " << remote_host << std::endl;
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_context *ctx;
|
|
|
|
ctx = iio_create_network_context(remote_host.c_str());
|
|
|
|
if (!ctx)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "No context\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No context");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-08-18 20:16:13 +00:00
|
|
|
// find tx device
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_device *tx;
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Acquiring AD9361 TX streaming devices\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
if (!get_ad9361_stream_dev(ctx, TX, &tx))
|
|
|
|
{
|
|
|
|
std::cout << "No tx dev found\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No tx dev found");
|
|
|
|
};
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "* Configuring AD9361 for streaming TX\n";
|
2018-03-27 17:35:53 +00:00
|
|
|
if (!cfg_ad9361_streaming_ch(ctx, &txcfg, TX, 0))
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "TX port 0 not found\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO TX port 0 not found");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-08-18 20:16:13 +00:00
|
|
|
// ENABLE DDS on TX1
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_device *ad9361_phy;
|
2018-03-27 19:10:31 +00:00
|
|
|
ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
|
2018-03-27 17:35:53 +00:00
|
|
|
int ret;
|
2019-08-18 20:16:13 +00:00
|
|
|
// set output amplifier attenuation
|
2019-10-07 20:45:09 +00:00
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage0_hardwaregain", -std::abs(tx_attenuation_db_));
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-10-07 20:45:09 +00:00
|
|
|
std::cout << "Failed to set out_voltage0_hardwaregain value " << -std::abs(tx_attenuation_db_) << ". Error " << ret << std::endl;
|
2018-03-27 19:10:31 +00:00
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-10-07 20:45:09 +00:00
|
|
|
// shut down signal in TX2
|
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage1_hardwaregain", -89.75);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set out_voltage1_hardwaregain value -89.75 dB. Error " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_device *dds;
|
2018-03-27 19:10:31 +00:00
|
|
|
dds = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_channel *dds_channel0_I;
|
|
|
|
dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true);
|
|
|
|
|
|
|
|
struct iio_channel *dds_channel0_Q;
|
|
|
|
dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true);
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", true);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to toggle DDS: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-08-18 20:16:13 +00:00
|
|
|
// set frequency, scale and phase
|
2018-08-11 12:31:35 +00:00
|
|
|
ret = iio_channel_attr_write_longlong(dds_channel0_I, "frequency", static_cast<int64_t>(freq_dds_tx_hz_));
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS frequency I: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-08-11 12:31:35 +00:00
|
|
|
ret = iio_channel_attr_write_longlong(dds_channel0_Q, "frequency", static_cast<int64_t>(freq_dds_tx_hz_));
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS frequency Q: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-10-08 10:57:37 +00:00
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_I, "phase", phase_dds_deg_ * 1000.0);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS phase I: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-10-08 10:57:37 +00:00
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_Q, "phase", phase_dds_deg_ * 1000.0 + 270000.0);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS phase Q: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_I, "scale", pow(10, scale_dds_dbfs_ / 20.0));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS scale I: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_Q, "scale", pow(10, scale_dds_dbfs_ / 20.0));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS scale Q: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2019-08-18 20:16:13 +00:00
|
|
|
// disable TX2
|
2018-03-27 17:35:53 +00:00
|
|
|
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage1_hardwaregain", -89.0);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set out_voltage1_hardwaregain value " << -89.0 << " error " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
struct iio_channel *dds_channel1_I;
|
|
|
|
dds_channel1_I = iio_device_find_channel(dds, "TX2_I_F1", true);
|
|
|
|
|
|
|
|
struct iio_channel *dds_channel1_Q;
|
|
|
|
dds_channel1_Q = iio_device_find_channel(dds, "TX2_Q_F1", true);
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
ret = iio_channel_attr_write_double(dds_channel1_I, "scale", 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX2 DDS scale I: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
ret = iio_channel_attr_write_double(dds_channel1_Q, "scale", 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX2 DDS scale Q: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
iio_context_destroy(ctx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-11 00:56:25 +00:00
|
|
|
bool ad9361_disable_lo_remote(const std::string &remote_host)
|
2018-03-27 17:35:53 +00:00
|
|
|
{
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "AD9361 Acquiring IIO REMOTE context in host " << remote_host << std::endl;
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_context *ctx;
|
|
|
|
ctx = iio_create_network_context(remote_host.c_str());
|
|
|
|
if (!ctx)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "No context\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No context");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_device *dds;
|
2018-03-27 19:10:31 +00:00
|
|
|
dds = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_channel *dds_channel0_I;
|
|
|
|
dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true);
|
|
|
|
|
|
|
|
struct iio_channel *dds_channel0_Q;
|
|
|
|
dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true);
|
|
|
|
int ret;
|
|
|
|
ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", false);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to toggle DDS: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_I, "scale", 0.0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS scale I: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_Q, "scale", 0.0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS scale Q: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
iio_context_destroy(ctx);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:10:31 +00:00
|
|
|
|
2018-03-27 17:35:53 +00:00
|
|
|
bool ad9361_disable_lo_local()
|
|
|
|
{
|
2018-03-27 19:10:31 +00:00
|
|
|
std::cout << "AD9361 Acquiring IIO LOCAL context" << std::endl;
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_context *ctx;
|
|
|
|
ctx = iio_create_default_context();
|
|
|
|
if (!ctx)
|
2018-03-27 19:10:31 +00:00
|
|
|
{
|
|
|
|
std::cout << "No context\n";
|
|
|
|
throw std::runtime_error("AD9361 IIO No context");
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_device *dds;
|
2018-03-27 19:10:31 +00:00
|
|
|
dds = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
|
2018-03-27 17:35:53 +00:00
|
|
|
struct iio_channel *dds_channel0_I;
|
|
|
|
dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true);
|
|
|
|
|
|
|
|
struct iio_channel *dds_channel0_Q;
|
|
|
|
dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true);
|
|
|
|
int ret;
|
|
|
|
ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", false);
|
2018-03-27 19:10:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to toggle DDS: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_I, "scale", 0.0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS scale I: " << ret << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iio_channel_attr_write_double(dds_channel0_Q, "scale", 0.0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
std::cout << "Failed to set TX DDS scale Q: " << ret << std::endl;
|
|
|
|
}
|
2018-03-27 17:35:53 +00:00
|
|
|
|
|
|
|
iio_context_destroy(ctx);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-08 17:34:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool load_fir_filter(
|
|
|
|
std::string &filter, struct iio_device *phy)
|
|
|
|
{
|
|
|
|
if (filter.empty() || !iio_device_find_attr(phy, "filter_fir_config"))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ifstream ifs(filter.c_str(), std::ifstream::binary);
|
|
|
|
if (!ifs)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-09 17:05:31 +00:00
|
|
|
// Here, we verify that the filter file contains data for both RX+TX.
|
2019-10-08 17:34:18 +00:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ifs.getline(buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
while (!(buf[0] == '-' || (buf[0] >= '0' && buf[0] <= '9')));
|
|
|
|
|
|
|
|
std::string line(buf);
|
|
|
|
if (line.find(',') == std::string::npos)
|
|
|
|
throw std::runtime_error("Incompatible filter file");
|
|
|
|
}
|
|
|
|
|
|
|
|
ifs.seekg(0, ifs.end);
|
|
|
|
int length = ifs.tellg();
|
|
|
|
ifs.seekg(0, ifs.beg);
|
|
|
|
|
|
|
|
std::vector<char> buffer(length);
|
|
|
|
|
|
|
|
ifs.read(buffer.data(), length);
|
|
|
|
ifs.close();
|
|
|
|
|
|
|
|
int ret = iio_device_attr_write_raw(phy,
|
|
|
|
"filter_fir_config", buffer.data(), length);
|
|
|
|
|
|
|
|
return ret > 0;
|
|
|
|
}
|