Implement more conf options for AD9361 FIR filters, work in progress

This commit is contained in:
Carles Fernandez
2019-10-08 19:34:18 +02:00
parent 0dbba0500d
commit 09078b6486
5 changed files with 247 additions and 19 deletions
@@ -67,6 +67,8 @@ if(ENABLE_FMCOMMS2 OR ENABLE_AD9361)
target_link_libraries(signal_source_libs
PUBLIC
Iio::iio
PRIVATE
Iio::ad9361
)
endif()
@@ -31,9 +31,12 @@
*/
#include "ad9361_manager.h"
#include <glog/logging.h>
#include <ad9361.h>
#include <cmath>
#include <fstream> // for ifstream
#include <iostream>
#include <sstream>
#include <vector>
/* check return value of attr_write function */
@@ -322,9 +325,10 @@ bool config_ad9361_rx_remote(const std::string &remote_host,
bool rfdc_,
bool bbdc_)
{
std::string filter_source_("Off");
float Fpass_ = 0.0, Fstop_ = 0.0;
std::string filter_filename_;
// RX stream config
// Stream configurations
struct stream_cfg rxcfg;
std::cout << "AD9361 Acquiring IIO REMOTE context in host " << remote_host << std::endl;
struct iio_context *ctx;
@@ -354,19 +358,32 @@ bool config_ad9361_rx_remote(const std::string &remote_host,
throw std::runtime_error("AD9361 IIO No rx dev found");
};
std::cout << "* Configuring AD9361 for streaming\n";
struct iio_device *ad9361_phy;
ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
int ret;
std::cout << "* Configuring AD9361 for streaming\n";
std::cout << "* Initializing AD9361 IIO streaming channels\n";
if (!get_ad9361_stream_ch(ctx, RX, rx, 0, &rx0_i))
{
std::cout << "RX chan i not found\n";
throw std::runtime_error("RX chan i not found");
}
if (!get_ad9361_stream_ch(ctx, RX, rx, 1, &rx0_q))
{
std::cout << "RX chan q not found\n";
throw std::runtime_error("RX chan q not found");
}
if (filter_source_ == "Off")
{
struct stream_cfg rxcfg;
rxcfg.bw_hz = bandwidth_; // 2 MHz rf bandwidth
rxcfg.fs_hz = sample_rate_; // 2.5 MS/s rx sample rate
rxcfg.lo_hz = freq_; // 2.5 GHz rf frequency
rxcfg.rfport = rf_port_select_.c_str(); // port A (select for rf freq.)
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))
{
@@ -383,14 +400,41 @@ bool config_ad9361_rx_remote(const std::string &remote_host,
// set bw
//params.push_back("in_voltage_rf_bandwidth=" + boost::to_string(bandwidth));
}
//wr_ch_str(rx0_i, "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(rx0_i, "rf_bandwidth", bandwidth_);
//if (!get_lo_chan(ctx, "RX", &chn))
// {
// return false;
// }
//wr_ch_lli(chn, "frequency", freq_);
// in_voltage0_rf_port_select
}
else if (filter_source_ == "File")
{
if (!load_fir_filter(filter_filename_, ad9361_phy))
try
{
throw std::runtime_error("Unable to load filter file");
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(rx0_i, "rf_bandwidth", bandwidth_);
wr_ch_lli(rx0_i, "frequency", freq_);
}
else if (filter_source_ == "Design")
{
@@ -400,23 +444,27 @@ bool config_ad9361_rx_remote(const std::string &remote_host,
{
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(rx0_i, "rf_bandwidth", bandwidth_);
wr_ch_lli(rx0_i, "frequency", freq_);
}
else
{
throw std::runtime_error("Unknown filter configuration");
}
std::cout << "* Initializing AD9361 IIO streaming channels\n";
if (!get_ad9361_stream_ch(ctx, RX, rx, 0, &rx0_i))
// Filters can only be disabled after the sample rate has been set
if (filter_source_ == "Off")
{
std::cout << "RX chan i not found\n";
throw std::runtime_error("RX chan i not found");
}
if (!get_ad9361_stream_ch(ctx, RX, rx, 1, &rx0_q))
{
std::cout << "RX chan q not found\n";
throw std::runtime_error("RX chan q not found");
ret = ad9361_set_trx_fir_enable(ad9361_phy, false);
if (ret)
{
throw std::runtime_error("Unable to disable filters");
}
}
std::cout << "* Enabling IIO streaming channels\n";
@@ -853,3 +901,47 @@ bool ad9361_disable_lo_local()
return true;
}
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;
}
/* Here, we verify that the filter file contains data for both RX+TX. */
{
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;
}
@@ -132,4 +132,6 @@ bool ad9361_disable_lo_remote(const std::string &remote_host);
bool ad9361_disable_lo_local();
bool load_fir_filter(std::string &filter, struct iio_device *phy);
#endif // GNSS_SDR_AD9361_MANAGER_H_