1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2025-01-27 09:24:52 +00:00

Apply clang-format, fix libiio detection in MacOS

This commit is contained in:
Carles Fernandez 2018-03-27 21:10:31 +02:00
parent 1d0452463f
commit b31f929655
4 changed files with 559 additions and 471 deletions

View File

@ -9,11 +9,12 @@ FIND_PATH(
PATHS ${CMAKE_INSTALL_PREFIX}/include PATHS ${CMAKE_INSTALL_PREFIX}/include
/usr/local/include /usr/local/include
/usr/include /usr/include
/opt/local/include
) )
FIND_LIBRARY( FIND_LIBRARY(
LIBIIO_LIBRARIES LIBIIO_LIBRARIES
NAMES libiio.so NAMES libiio.so iio
HINTS $ENV{LIBIIO_DIR}/lib HINTS $ENV{LIBIIO_DIR}/lib
${PC_LIBIIO_LIBDIR} ${PC_LIBIIO_LIBDIR}
PATHS ${CMAKE_INSTALL_PREFIX}/lib PATHS ${CMAKE_INSTALL_PREFIX}/lib
@ -23,10 +24,30 @@ FIND_LIBRARY(
/usr/lib /usr/lib
/usr/lib64 /usr/lib64
/usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu
/usr/lib/gcc/alpha-linux-gnu
/usr/lib/gcc/aarch64-linux-gnu
/usr/lib/gcc/arm-linux-gnueabi
/usr/lib/gcc/arm-linux-gnueabihf
/usr/lib/gcc/hppa-linux-gnu
/usr/lib/gcc/i686-gnu
/usr/lib/gcc/i686-linux-gnu
/usr/lib/gcc/x86_64-kfreebsd-gnu
/usr/lib/gcc/i686-kfreebsd-gnu
/usr/lib/gcc/m68k-linux-gnu
/usr/lib/gcc/mips-linux-gnu
/usr/lib/gcc/mips64el-linux-gnuabi64
/usr/lib/gcc/mipsel-linux-gnu
/usr/lib/gcc/powerpc-linux-gnu
/usr/lib/gcc/powerpc-linux-gnuspe
/usr/lib/gcc/powerpc64-linux-gnu
/usr/lib/gcc/powerpc64le-linux-gnu
/usr/lib/gcc/s390x-linux-gnu
/usr/lib/gcc/sparc64-linux-gnu
/usr/lib/gcc/x86_64-linux-gnux32
/usr/lib/gcc/sh4-linux-gnu
/Library/Frameworks/iio.framework/
) )
message("find libiio:")
message(${LIBIIO_LIBRARIES})
INCLUDE(FindPackageHandleStandardArgs) INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBIIO DEFAULT_MSG LIBIIO_LIBRARIES LIBIIO_INCLUDE_DIRS) FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBIIO DEFAULT_MSG LIBIIO_LIBRARIES LIBIIO_INCLUDE_DIRS)
MARK_AS_ADVANCED(LIBIIO_LIBRARIES LIBIIO_INCLUDE_DIRS) MARK_AS_ADVANCED(LIBIIO_LIBRARIES LIBIIO_INCLUDE_DIRS)

View File

@ -71,13 +71,12 @@ Fmcomms2SignalSource::Fmcomms2SignalSource(ConfigurationInterface* configuration
dump_filename_ = configuration->property(role + ".dump_filename", default_dump_file); dump_filename_ = configuration->property(role + ".dump_filename", default_dump_file);
//AD9361 Local Oscillator generation for dual band operation //AD9361 Local Oscillator generation for dual band operation
enable_dds_lo_=configuration->property(role + ".enable_dds_lo", false); enable_dds_lo_ = configuration->property(role + ".enable_dds_lo", false);
freq_rf_tx_hz_=configuration->property(role + ".freq_rf_tx_hz", GPS_L1_FREQ_HZ-GPS_L2_FREQ_HZ-1000); freq_rf_tx_hz_ = configuration->property(role + ".freq_rf_tx_hz", GPS_L1_FREQ_HZ - GPS_L2_FREQ_HZ - 1000);
freq_dds_tx_hz_=configuration->property(role + ".freq_dds_tx_hz", 1000); freq_dds_tx_hz_ = configuration->property(role + ".freq_dds_tx_hz", 1000);
scale_dds_dbfs_=configuration->property(role + ".scale_dds_dbfs", 0.0); scale_dds_dbfs_ = configuration->property(role + ".scale_dds_dbfs", 0.0);
phase_dds_deg_=configuration->property(role + ".phase_dds_deg", 0.0); phase_dds_deg_ = configuration->property(role + ".phase_dds_deg", 0.0);
tx_attenuation_db_=configuration->property(role + ".tx_attenuation_db", 0.0); tx_attenuation_db_ = configuration->property(role + ".tx_attenuation_db", 0.0);
item_size_ = sizeof(gr_complex); item_size_ = sizeof(gr_complex);
@ -87,7 +86,7 @@ Fmcomms2SignalSource::Fmcomms2SignalSource(ConfigurationInterface* configuration
if (item_type_.compare("gr_complex") == 0) if (item_type_.compare("gr_complex") == 0)
{ {
if (RF_channels_==1) if (RF_channels_ == 1)
{ {
if (rx1_en_ and rx2_en_) if (rx1_en_ and rx2_en_)
{ {
@ -106,9 +105,9 @@ Fmcomms2SignalSource::Fmcomms2SignalSource(ConfigurationInterface* configuration
filter_auto_); filter_auto_);
//configure LO //configure LO
if (enable_dds_lo_==true) if (enable_dds_lo_ == true)
{ {
std::cout<<"Enabling Local Oscillator generator in FMCOMMS2\n"; std::cout << "Enabling Local Oscillator generator in FMCOMMS2\n";
config_ad9361_lo_remote(uri_, config_ad9361_lo_remote(uri_,
bandwidth_, bandwidth_,
sample_rate_, sample_rate_,
@ -119,7 +118,7 @@ Fmcomms2SignalSource::Fmcomms2SignalSource(ConfigurationInterface* configuration
} }
} }
} }
else if(RF_channels_==2) else if (RF_channels_ == 2)
{ {
if (!(rx1_en_ and rx2_en_)) if (!(rx1_en_ and rx2_en_))
{ {
@ -137,9 +136,9 @@ Fmcomms2SignalSource::Fmcomms2SignalSource(ConfigurationInterface* configuration
rf_port_select_.c_str(), filter_file_.c_str(), rf_port_select_.c_str(), filter_file_.c_str(),
filter_auto_); filter_auto_);
//configure LO //configure LO
if (enable_dds_lo_==true) if (enable_dds_lo_ == true)
{ {
std::cout<<"Enabling Local Oscillator generator in FMCOMMS2\n"; std::cout << "Enabling Local Oscillator generator in FMCOMMS2\n";
config_ad9361_lo_remote(uri_, config_ad9361_lo_remote(uri_,
bandwidth_, bandwidth_,
sample_rate_, sample_rate_,
@ -149,10 +148,11 @@ Fmcomms2SignalSource::Fmcomms2SignalSource(ConfigurationInterface* configuration
scale_dds_dbfs_); scale_dds_dbfs_);
} }
} }
}else{ }
else
{
LOG(FATAL) << "Configuration error: Unsupported number of RF_channels !"; LOG(FATAL) << "Configuration error: Unsupported number of RF_channels !";
} }
} }
else else
{ {
@ -177,7 +177,7 @@ Fmcomms2SignalSource::Fmcomms2SignalSource(ConfigurationInterface* configuration
Fmcomms2SignalSource::~Fmcomms2SignalSource() Fmcomms2SignalSource::~Fmcomms2SignalSource()
{ {
if (enable_dds_lo_==true) if (enable_dds_lo_ == true)
{ {
ad9361_disable_lo_remote(uri_); ad9361_disable_lo_remote(uri_);
} }

View File

@ -35,94 +35,112 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
/* check return value of attr_write function */ /* check return value of attr_write function */
void errchk(int v, const char* what) { void errchk(int v, const char *what)
{
if (v < 0) if (v < 0)
{ {
LOG(WARNING)<<"Error "<<v<<" writing to channel "<<what<<" value may not be supported. "; LOG(WARNING) << "Error " << v << " writing to channel " << what << " value may not be supported. ";
} }
} }
/* write attribute: long long int */ /* write attribute: long long int */
void wr_ch_lli(struct iio_channel *chn, const char* what, long long val) void wr_ch_lli(struct iio_channel *chn, const char *what, long long val)
{ {
errchk(iio_channel_attr_write_longlong(chn, what, val), what); errchk(iio_channel_attr_write_longlong(chn, what, val), what);
} }
/* write attribute: string */ /* write attribute: string */
void wr_ch_str(struct iio_channel *chn, const char* what, const char* str) void wr_ch_str(struct iio_channel *chn, const char *what, const char *str)
{ {
errchk(iio_channel_attr_write(chn, what, str), what); errchk(iio_channel_attr_write(chn, what, str), what);
} }
/* returns ad9361 phy device */ /* returns ad9361 phy device */
struct iio_device* get_ad9361_phy(struct iio_context *ctx) struct iio_device *get_ad9361_phy(struct iio_context *ctx)
{ {
struct iio_device *dev = iio_context_find_device(ctx, "ad9361-phy"); struct iio_device *dev = iio_context_find_device(ctx, "ad9361-phy");
return dev; return dev;
} }
/* finds AD9361 streaming IIO devices */ /* finds AD9361 streaming IIO devices */
bool get_ad9361_stream_dev(struct iio_context *ctx, enum iodev d, struct iio_device **dev) bool get_ad9361_stream_dev(struct iio_context *ctx, enum iodev d, struct iio_device **dev)
{ {
switch (d) { switch (d)
case TX: *dev = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc"); return *dev != NULL; {
case RX: *dev = iio_context_find_device(ctx, "cf-ad9361-lpc"); return *dev != NULL; case TX:
default: return false; *dev = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
return *dev != NULL;
case RX:
*dev = iio_context_find_device(ctx, "cf-ad9361-lpc");
return *dev != NULL;
default:
return false;
} }
} }
/* finds AD9361 streaming IIO channels */ /* finds AD9361 streaming IIO channels */
bool get_ad9361_stream_ch(struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, struct iio_channel **chn) bool get_ad9361_stream_ch(struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, struct iio_channel **chn)
{ {
std::stringstream name; std::stringstream name;
name.str(""); name.str("");
name<<"voltage"; name << "voltage";
name<<chid; name << chid;
*chn = iio_device_find_channel(dev,name.str().c_str(), d == TX); *chn = iio_device_find_channel(dev, name.str().c_str(), d == TX);
if (!*chn) if (!*chn)
{ {
name.str(""); name.str("");
name<<"altvoltage"; name << "altvoltage";
name<<chid; name << chid;
*chn = iio_device_find_channel(dev, name.str().c_str(), d == TX); *chn = iio_device_find_channel(dev, name.str().c_str(), d == TX);
} }
return *chn != NULL; return *chn != NULL;
} }
/* finds AD9361 phy IIO configuration channel with id chid */ /* 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) bool get_phy_chan(struct iio_context *ctx, enum iodev d, int chid, struct iio_channel **chn)
{ {
std::stringstream name; std::stringstream name;
switch (d) { switch (d)
{
case RX: case RX:
name.str(""); name.str("");
name<<"voltage"; name << "voltage";
name<<chid; name << chid;
*chn = iio_device_find_channel(get_ad9361_phy(ctx),name.str().c_str() , false); *chn = iio_device_find_channel(get_ad9361_phy(ctx), name.str().c_str(), false);
return *chn != NULL; return *chn != NULL;
break; break;
case TX: case TX:
name.str(""); name.str("");
name<<"voltage"; name << "voltage";
name<<chid; name << chid;
*chn = iio_device_find_channel(get_ad9361_phy(ctx), name.str().c_str(), true); *chn = iio_device_find_channel(get_ad9361_phy(ctx), name.str().c_str(), true);
return *chn != NULL; return *chn != NULL;
break; break;
default: return false; default:
return false;
} }
} }
/* finds AD9361 local oscillator IIO configuration channels */ /* finds AD9361 local oscillator IIO configuration channels */
bool get_lo_chan(struct iio_context *ctx, enum iodev d, struct iio_channel **chn) bool get_lo_chan(struct iio_context *ctx, enum iodev d, struct iio_channel **chn)
{ {
switch (d) { switch (d)
{
// LO chan is always output, i.e. true // LO chan is always output, i.e. true
case RX: case RX:
*chn = iio_device_find_channel(get_ad9361_phy(ctx), "altvoltage0", true); *chn = iio_device_find_channel(get_ad9361_phy(ctx), "altvoltage0", true);
return *chn != NULL; return *chn != NULL;
case TX: case TX:
*chn = iio_device_find_channel(get_ad9361_phy(ctx), "altvoltage1" , true); *chn = iio_device_find_channel(get_ad9361_phy(ctx), "altvoltage1", true);
return *chn != NULL; return *chn != NULL;
default: default:
return false; return false;
@ -137,16 +155,22 @@ bool cfg_ad9361_streaming_ch(struct iio_context *ctx, struct stream_cfg *cfg, en
// Configure phy and lo channels // Configure phy and lo channels
//LOG(INFO)<<"* Acquiring AD9361 phy channel"<<chid; //LOG(INFO)<<"* Acquiring AD9361 phy channel"<<chid;
std::cout<<"* Acquiring AD9361 phy channel"<<chid<<std::endl; std::cout << "* Acquiring AD9361 phy channel" << chid << std::endl;
if (!get_phy_chan(ctx, type, chid, &chn)) { return false; } if (!get_phy_chan(ctx, type, chid, &chn))
{
return false;
}
wr_ch_str(chn, "rf_port_select", cfg->rfport); wr_ch_str(chn, "rf_port_select", cfg->rfport);
wr_ch_lli(chn, "rf_bandwidth", cfg->bw_hz); wr_ch_lli(chn, "rf_bandwidth", cfg->bw_hz);
wr_ch_lli(chn, "sampling_frequency", cfg->fs_hz); wr_ch_lli(chn, "sampling_frequency", cfg->fs_hz);
// Configure LO channel // Configure LO channel
//LOG(INFO)<<"* Acquiring AD9361 "<<type == TX ? "TX" : "RX"; //LOG(INFO)<<"* Acquiring AD9361 "<<type == TX ? "TX" : "RX";
std::cout<<"* Acquiring AD9361 "<<(type == TX ? "TX" : "RX")<<std::endl; std::cout << "* Acquiring AD9361 " << (type == TX ? "TX" : "RX") << std::endl;
if (!get_lo_chan(ctx, type, &chn)) { return false; } if (!get_lo_chan(ctx, type, &chn))
{
return false;
}
wr_ch_lli(chn, "frequency", cfg->lo_hz); wr_ch_lli(chn, "frequency", cfg->lo_hz);
return true; return true;
} }
@ -170,7 +194,7 @@ bool config_ad9361_rx_local(unsigned long bandwidth_,
rxcfg.lo_hz = freq_; // 2.5 GHz rf frequency rxcfg.lo_hz = freq_; // 2.5 GHz rf frequency
rxcfg.rfport = rf_port_select_.c_str(); // port A (select for rf freq.) rxcfg.rfport = rf_port_select_.c_str(); // port A (select for rf freq.)
std::cout<<"AD9361 Acquiring IIO LOCAL context\n"; std::cout << "AD9361 Acquiring IIO LOCAL context\n";
struct iio_context *ctx; struct iio_context *ctx;
// Streaming devices // Streaming devices
struct iio_device *rx; struct iio_device *rx;
@ -180,85 +204,93 @@ bool config_ad9361_rx_local(unsigned long bandwidth_,
ctx = iio_create_default_context(); ctx = iio_create_default_context();
if (!ctx) if (!ctx)
{ {
std::cout<<"No context\n"; std::cout << "No context\n";
throw std::runtime_error("AD9361 IIO No context"); throw std::runtime_error("AD9361 IIO No context");
} }
if (iio_context_get_devices_count(ctx) <= 0) if (iio_context_get_devices_count(ctx) <= 0)
{ {
std::cout<<"No devices\n"; std::cout << "No devices\n";
throw std::runtime_error("AD9361 IIO No devices"); throw std::runtime_error("AD9361 IIO No devices");
} }
std::cout<<"* Acquiring AD9361 streaming devices\n"; std::cout << "* Acquiring AD9361 streaming devices\n";
if(!get_ad9361_stream_dev(ctx, RX, &rx)) if (!get_ad9361_stream_dev(ctx, RX, &rx))
{ {
std::cout<<"No rx dev found\n"; std::cout << "No rx dev found\n";
throw std::runtime_error("AD9361 IIO No rx dev found"); throw std::runtime_error("AD9361 IIO No rx dev found");
}; };
std::cout<<"* Configuring AD9361 for streaming\n"; std::cout << "* Configuring AD9361 for streaming\n";
if (!cfg_ad9361_streaming_ch(ctx, &rxcfg, RX, 0)) if (!cfg_ad9361_streaming_ch(ctx, &rxcfg, RX, 0))
{ {
std::cout<<"RX port 0 not found\n"; std::cout << "RX port 0 not found\n";
throw std::runtime_error("AD9361 IIO RX port 0 not found"); throw std::runtime_error("AD9361 IIO RX port 0 not found");
} }
std::cout<<"* Initializing AD9361 IIO streaming channels\n"; std::cout << "* Initializing AD9361 IIO streaming channels\n";
if (!get_ad9361_stream_ch(ctx, RX, rx, 0, &rx0_i)) if (!get_ad9361_stream_ch(ctx, RX, rx, 0, &rx0_i))
{ {
std::cout<<"RX chan i not found\n"; std::cout << "RX chan i not found\n";
throw std::runtime_error("RX chan i not found"); throw std::runtime_error("RX chan i not found");
} }
if (!get_ad9361_stream_ch(ctx, RX, rx, 1, &rx0_q)) if (!get_ad9361_stream_ch(ctx, RX, rx, 1, &rx0_q))
{ {
std::cout<<"RX chan q not found\n"; std::cout << "RX chan q not found\n";
throw std::runtime_error("RX chan q not found"); throw std::runtime_error("RX chan q not found");
} }
std::cout<<"* Enabling IIO streaming channels\n"; std::cout << "* Enabling IIO streaming channels\n";
iio_channel_enable(rx0_i); iio_channel_enable(rx0_i);
iio_channel_enable(rx0_q); iio_channel_enable(rx0_q);
struct iio_device *ad9361_phy; struct iio_device *ad9361_phy;
ad9361_phy= iio_context_find_device(ctx, "ad9361-phy"); ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
int ret; int ret;
ret=iio_device_attr_write(ad9361_phy,"trx_rate_governor","nominal"); ret = iio_device_attr_write(ad9361_phy, "trx_rate_governor", "nominal");
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set trx_rate_governor: "<<ret<<std::endl; {
std::cout << "Failed to set trx_rate_governor: " << ret << std::endl;
} }
ret=iio_device_attr_write(ad9361_phy,"ensm_mode","fdd"); ret = iio_device_attr_write(ad9361_phy, "ensm_mode", "fdd");
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set ensm_mode: "<<ret<<std::endl; {
std::cout << "Failed to set ensm_mode: " << ret << std::endl;
} }
ret=iio_device_attr_write(ad9361_phy,"calib_mode","auto"); ret = iio_device_attr_write(ad9361_phy, "calib_mode", "auto");
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set calib_mode: "<<ret<<std::endl; {
std::cout << "Failed to set calib_mode: " << ret << std::endl;
} }
ret=iio_device_attr_write(ad9361_phy,"in_voltage0_gain_control_mode",gain_mode_rx1_.c_str()); ret = iio_device_attr_write(ad9361_phy, "in_voltage0_gain_control_mode", gain_mode_rx1_.c_str());
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set in_voltage0_gain_control_mode: "<<ret<<std::endl; {
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()); ret = iio_device_attr_write(ad9361_phy, "in_voltage1_gain_control_mode", gain_mode_rx2_.c_str());
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set in_voltage1_gain_control_mode: "<<ret<<std::endl; {
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_); ret = iio_device_attr_write_double(ad9361_phy, "in_voltage0_hardwaregain", rf_gain_rx1_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set in_voltage0_hardwaregain: "<<ret<<std::endl; {
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_); ret = iio_device_attr_write_double(ad9361_phy, "in_voltage1_hardwaregain", rf_gain_rx2_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set in_voltage1_hardwaregain: "<<ret<<std::endl; {
std::cout << "Failed to set in_voltage1_hardwaregain: " << ret << std::endl;
} }
std::cout<<"End of AD9361 RX configuration.\n"; std::cout << "End of AD9361 RX configuration.\n";
iio_context_destroy(ctx); iio_context_destroy(ctx);
return true; return true;
} }
bool config_ad9361_rx_remote(std::string remote_host, bool config_ad9361_rx_remote(std::string remote_host,
unsigned long bandwidth_, unsigned long bandwidth_,
unsigned long sample_rate_, unsigned long sample_rate_,
@ -278,7 +310,7 @@ bool config_ad9361_rx_remote(std::string remote_host,
rxcfg.rfport = rf_port_select_.c_str(); // port A (select for rf freq.) rxcfg.rfport = rf_port_select_.c_str(); // port A (select for rf freq.)
std::cout<<"AD9361 Acquiring IIO REMOTE context in host "<<remote_host<<std::endl; std::cout << "AD9361 Acquiring IIO REMOTE context in host " << remote_host << std::endl;
struct iio_context *ctx; struct iio_context *ctx;
// Streaming devices // Streaming devices
struct iio_device *rx; struct iio_device *rx;
@ -288,86 +320,94 @@ bool config_ad9361_rx_remote(std::string remote_host,
ctx = iio_create_network_context(remote_host.c_str()); ctx = iio_create_network_context(remote_host.c_str());
if (!ctx) if (!ctx)
{ {
std::cout<<"No context\n"; std::cout << "No context\n";
throw std::runtime_error("AD9361 IIO No context"); throw std::runtime_error("AD9361 IIO No context");
} }
if (iio_context_get_devices_count(ctx) <= 0) if (iio_context_get_devices_count(ctx) <= 0)
{ {
std::cout<<"No devices\n"; std::cout << "No devices\n";
throw std::runtime_error("AD9361 IIO No devices"); throw std::runtime_error("AD9361 IIO No devices");
} }
std::cout<<"* Acquiring AD9361 streaming devices\n"; std::cout << "* Acquiring AD9361 streaming devices\n";
if(!get_ad9361_stream_dev(ctx, RX, &rx)) if (!get_ad9361_stream_dev(ctx, RX, &rx))
{ {
std::cout<<"No rx dev found\n"; std::cout << "No rx dev found\n";
throw std::runtime_error("AD9361 IIO No rx dev found"); throw std::runtime_error("AD9361 IIO No rx dev found");
}; };
std::cout<<"* Configuring AD9361 for streaming\n"; std::cout << "* Configuring AD9361 for streaming\n";
if (!cfg_ad9361_streaming_ch(ctx, &rxcfg, RX, 0)) if (!cfg_ad9361_streaming_ch(ctx, &rxcfg, RX, 0))
{ {
std::cout<<"RX port 0 not found\n"; std::cout << "RX port 0 not found\n";
throw std::runtime_error("AD9361 IIO RX port 0 not found"); throw std::runtime_error("AD9361 IIO RX port 0 not found");
} }
std::cout<<"* Initializing AD9361 IIO streaming channels\n"; std::cout << "* Initializing AD9361 IIO streaming channels\n";
if (!get_ad9361_stream_ch(ctx, RX, rx, 0, &rx0_i)) if (!get_ad9361_stream_ch(ctx, RX, rx, 0, &rx0_i))
{ {
std::cout<<"RX chan i not found\n"; std::cout << "RX chan i not found\n";
throw std::runtime_error("RX chan i not found"); throw std::runtime_error("RX chan i not found");
} }
if (!get_ad9361_stream_ch(ctx, RX, rx, 1, &rx0_q)) if (!get_ad9361_stream_ch(ctx, RX, rx, 1, &rx0_q))
{ {
std::cout<<"RX chan q not found\n"; std::cout << "RX chan q not found\n";
throw std::runtime_error("RX chan q not found"); throw std::runtime_error("RX chan q not found");
} }
std::cout<<"* Enabling IIO streaming channels\n"; std::cout << "* Enabling IIO streaming channels\n";
iio_channel_enable(rx0_i); iio_channel_enable(rx0_i);
iio_channel_enable(rx0_q); iio_channel_enable(rx0_q);
struct iio_device *ad9361_phy; struct iio_device *ad9361_phy;
ad9361_phy= iio_context_find_device(ctx, "ad9361-phy"); ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
int ret; int ret;
ret=iio_device_attr_write(ad9361_phy,"trx_rate_governor","nominal"); ret = iio_device_attr_write(ad9361_phy, "trx_rate_governor", "nominal");
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set trx_rate_governor: "<<ret<<std::endl; {
std::cout << "Failed to set trx_rate_governor: " << ret << std::endl;
} }
ret=iio_device_attr_write(ad9361_phy,"ensm_mode","fdd"); ret = iio_device_attr_write(ad9361_phy, "ensm_mode", "fdd");
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set ensm_mode: "<<ret<<std::endl; {
std::cout << "Failed to set ensm_mode: " << ret << std::endl;
} }
ret=iio_device_attr_write(ad9361_phy,"calib_mode","auto"); ret = iio_device_attr_write(ad9361_phy, "calib_mode", "auto");
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set calib_mode: "<<ret<<std::endl; {
std::cout << "Failed to set calib_mode: " << ret << std::endl;
} }
ret=iio_device_attr_write(ad9361_phy,"in_voltage0_gain_control_mode",gain_mode_rx1_.c_str()); ret = iio_device_attr_write(ad9361_phy, "in_voltage0_gain_control_mode", gain_mode_rx1_.c_str());
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set in_voltage0_gain_control_mode: "<<ret<<std::endl; {
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()); ret = iio_device_attr_write(ad9361_phy, "in_voltage1_gain_control_mode", gain_mode_rx2_.c_str());
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set in_voltage1_gain_control_mode: "<<ret<<std::endl; {
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_); ret = iio_device_attr_write_double(ad9361_phy, "in_voltage0_hardwaregain", rf_gain_rx1_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set in_voltage0_hardwaregain: "<<ret<<std::endl; {
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_); ret = iio_device_attr_write_double(ad9361_phy, "in_voltage1_hardwaregain", rf_gain_rx2_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set in_voltage1_hardwaregain: "<<ret<<std::endl; {
std::cout << "Failed to set in_voltage1_hardwaregain: " << ret << std::endl;
} }
std::cout<<"End of AD9361 RX configuration.\n"; std::cout << "End of AD9361 RX configuration.\n";
iio_context_destroy(ctx); iio_context_destroy(ctx);
return true; return true;
} }
bool config_ad9361_lo_local(unsigned long bandwidth_, bool config_ad9361_lo_local(unsigned long bandwidth_,
unsigned long sample_rate_, unsigned long sample_rate_,
unsigned long freq_rf_tx_hz_, unsigned long freq_rf_tx_hz_,
@ -375,54 +415,54 @@ bool config_ad9361_lo_local(unsigned long bandwidth_,
long long freq_dds_tx_hz_, long long freq_dds_tx_hz_,
double scale_dds_dbfs_) double scale_dds_dbfs_)
{ {
// TX stream config // TX stream config
std::cout<<"Start of AD9361 TX Local Oscillator DDS configuration\n"; std::cout << "Start of AD9361 TX Local Oscillator DDS configuration\n";
struct stream_cfg txcfg; struct stream_cfg txcfg;
txcfg.bw_hz = bandwidth_; txcfg.bw_hz = bandwidth_;
txcfg.fs_hz = sample_rate_; txcfg.fs_hz = sample_rate_;
txcfg.lo_hz = freq_rf_tx_hz_; txcfg.lo_hz = freq_rf_tx_hz_;
txcfg.rfport = "A"; txcfg.rfport = "A";
std::cout<<"AD9361 Acquiring IIO LOCAL context\n"; std::cout << "AD9361 Acquiring IIO LOCAL context\n";
struct iio_context *ctx; struct iio_context *ctx;
ctx = iio_create_default_context(); ctx = iio_create_default_context();
if (!ctx) if (!ctx)
{ {
std::cout<<"No context\n"; std::cout << "No context\n";
throw std::runtime_error("AD9361 IIO No context"); throw std::runtime_error("AD9361 IIO No context");
} }
//find tx device //find tx device
struct iio_device *tx; struct iio_device *tx;
std::cout<<"* Acquiring AD9361 TX streaming devices\n"; std::cout << "* Acquiring AD9361 TX streaming devices\n";
if(!get_ad9361_stream_dev(ctx, TX, &tx)) if (!get_ad9361_stream_dev(ctx, TX, &tx))
{ {
std::cout<<"No tx dev found\n"; std::cout << "No tx dev found\n";
throw std::runtime_error("AD9361 IIO No tx dev found"); throw std::runtime_error("AD9361 IIO No tx dev found");
}; };
std::cout<<"* Configuring AD9361 for streaming TX\n"; std::cout << "* Configuring AD9361 for streaming TX\n";
if (!cfg_ad9361_streaming_ch(ctx, &txcfg, TX, 0)) if (!cfg_ad9361_streaming_ch(ctx, &txcfg, TX, 0))
{ {
std::cout<<"TX port 0 not found\n"; std::cout << "TX port 0 not found\n";
throw std::runtime_error("AD9361 IIO TX port 0 not found"); throw std::runtime_error("AD9361 IIO TX port 0 not found");
} }
//ENABLE DDS on TX1 //ENABLE DDS on TX1
struct iio_device *ad9361_phy; struct iio_device *ad9361_phy;
ad9361_phy= iio_context_find_device(ctx, "ad9361-phy"); ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
int ret; int ret;
//set output amplifier attenuation //set output amplifier attenuation
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage0_hardwaregain", -tx_attenuation_db_); ret = iio_device_attr_write_double(ad9361_phy, "out_voltage0_hardwaregain", -tx_attenuation_db_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set out_voltage0_hardwaregain value "<<-tx_attenuation_db_<<" error "<<ret<<std::endl; {
std::cout << "Failed to set out_voltage0_hardwaregain value " << -tx_attenuation_db_ << " error " << ret << std::endl;
} }
struct iio_device *dds; struct iio_device *dds;
dds= iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc"); dds = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
struct iio_channel *dds_channel0_I; struct iio_channel *dds_channel0_I;
dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true); dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true);
@ -430,49 +470,57 @@ bool config_ad9361_lo_local(unsigned long bandwidth_,
dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true); dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true);
ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", true); ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", true);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to toggle DDS: "<<ret<<std::endl; {
std::cout << "Failed to toggle DDS: " << ret << std::endl;
} }
//set frequency, scale and phase //set frequency, scale and phase
ret=iio_channel_attr_write_longlong(dds_channel0_I, "frequency",(long long) freq_dds_tx_hz_); ret = iio_channel_attr_write_longlong(dds_channel0_I, "frequency", (long long)freq_dds_tx_hz_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS frequency I: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS frequency I: " << ret << std::endl;
} }
ret=iio_channel_attr_write_longlong(dds_channel0_Q, "frequency",(long long) freq_dds_tx_hz_); ret = iio_channel_attr_write_longlong(dds_channel0_Q, "frequency", (long long)freq_dds_tx_hz_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS frequency Q: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS frequency Q: " << ret << std::endl;
} }
ret=iio_channel_attr_write_double(dds_channel0_I, "phase",0.0); ret = iio_channel_attr_write_double(dds_channel0_I, "phase", 0.0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS phase I: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS phase I: " << ret << std::endl;
} }
ret=iio_channel_attr_write_double(dds_channel0_Q, "phase",270000.0); ret = iio_channel_attr_write_double(dds_channel0_Q, "phase", 270000.0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS phase Q: "<<ret<<std::endl; {
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)); ret = iio_channel_attr_write_double(dds_channel0_I, "scale", pow(10, scale_dds_dbfs_ / 20.0));
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS scale I: "<<ret<<std::endl; {
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)); ret = iio_channel_attr_write_double(dds_channel0_Q, "scale", pow(10, scale_dds_dbfs_ / 20.0));
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS scale Q: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS scale Q: " << ret << std::endl;
} }
//disable TX2 //disable TX2
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage1_hardwaregain", -89.0); ret = iio_device_attr_write_double(ad9361_phy, "out_voltage1_hardwaregain", -89.0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set out_voltage1_hardwaregain value "<<-89.0<<" error "<<ret<<std::endl; {
std::cout << "Failed to set out_voltage1_hardwaregain value " << -89.0 << " error " << ret << std::endl;
} }
struct iio_channel *dds_channel1_I; struct iio_channel *dds_channel1_I;
@ -482,20 +530,23 @@ bool config_ad9361_lo_local(unsigned long bandwidth_,
dds_channel1_Q = iio_device_find_channel(dds, "TX2_Q_F1", true); dds_channel1_Q = iio_device_find_channel(dds, "TX2_Q_F1", true);
ret=iio_channel_attr_write_double(dds_channel1_I, "scale",0); ret = iio_channel_attr_write_double(dds_channel1_I, "scale", 0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX2 DDS scale I: "<<ret<<std::endl; {
std::cout << "Failed to set TX2 DDS scale I: " << ret << std::endl;
} }
ret=iio_channel_attr_write_double(dds_channel1_Q, "scale",0); ret = iio_channel_attr_write_double(dds_channel1_Q, "scale", 0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX2 DDS scale Q: "<<ret<<std::endl; {
std::cout << "Failed to set TX2 DDS scale Q: " << ret << std::endl;
} }
iio_context_destroy(ctx); iio_context_destroy(ctx);
return true; return true;
} }
bool config_ad9361_lo_remote(std::string remote_host, bool config_ad9361_lo_remote(std::string remote_host,
unsigned long bandwidth_, unsigned long bandwidth_,
unsigned long sample_rate_, unsigned long sample_rate_,
@ -504,54 +555,54 @@ bool config_ad9361_lo_remote(std::string remote_host,
long long freq_dds_tx_hz_, long long freq_dds_tx_hz_,
double scale_dds_dbfs_) double scale_dds_dbfs_)
{ {
// TX stream config // TX stream config
std::cout<<"Start of AD9361 TX Local Oscillator DDS configuration\n"; std::cout << "Start of AD9361 TX Local Oscillator DDS configuration\n";
struct stream_cfg txcfg; struct stream_cfg txcfg;
txcfg.bw_hz = bandwidth_; txcfg.bw_hz = bandwidth_;
txcfg.fs_hz = sample_rate_; txcfg.fs_hz = sample_rate_;
txcfg.lo_hz = freq_rf_tx_hz_; txcfg.lo_hz = freq_rf_tx_hz_;
txcfg.rfport = "A"; txcfg.rfport = "A";
std::cout<<"AD9361 Acquiring IIO REMOTE context in host "<<remote_host<<std::endl; std::cout << "AD9361 Acquiring IIO REMOTE context in host " << remote_host << std::endl;
struct iio_context *ctx; struct iio_context *ctx;
ctx = iio_create_network_context(remote_host.c_str()); ctx = iio_create_network_context(remote_host.c_str());
if (!ctx) if (!ctx)
{ {
std::cout<<"No context\n"; std::cout << "No context\n";
throw std::runtime_error("AD9361 IIO No context"); throw std::runtime_error("AD9361 IIO No context");
} }
//find tx device //find tx device
struct iio_device *tx; struct iio_device *tx;
std::cout<<"* Acquiring AD9361 TX streaming devices\n"; std::cout << "* Acquiring AD9361 TX streaming devices\n";
if(!get_ad9361_stream_dev(ctx, TX, &tx)) if (!get_ad9361_stream_dev(ctx, TX, &tx))
{ {
std::cout<<"No tx dev found\n"; std::cout << "No tx dev found\n";
throw std::runtime_error("AD9361 IIO No tx dev found"); throw std::runtime_error("AD9361 IIO No tx dev found");
}; };
std::cout<<"* Configuring AD9361 for streaming TX\n"; std::cout << "* Configuring AD9361 for streaming TX\n";
if (!cfg_ad9361_streaming_ch(ctx, &txcfg, TX, 0)) if (!cfg_ad9361_streaming_ch(ctx, &txcfg, TX, 0))
{ {
std::cout<<"TX port 0 not found\n"; std::cout << "TX port 0 not found\n";
throw std::runtime_error("AD9361 IIO TX port 0 not found"); throw std::runtime_error("AD9361 IIO TX port 0 not found");
} }
//ENABLE DDS on TX1 //ENABLE DDS on TX1
struct iio_device *ad9361_phy; struct iio_device *ad9361_phy;
ad9361_phy= iio_context_find_device(ctx, "ad9361-phy"); ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
int ret; int ret;
//set output amplifier attenuation //set output amplifier attenuation
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage0_hardwaregain", -tx_attenuation_db_); ret = iio_device_attr_write_double(ad9361_phy, "out_voltage0_hardwaregain", -tx_attenuation_db_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set out_voltage0_hardwaregain value "<<-tx_attenuation_db_<<" error "<<ret<<std::endl; {
std::cout << "Failed to set out_voltage0_hardwaregain value " << -tx_attenuation_db_ << " error " << ret << std::endl;
} }
struct iio_device *dds; struct iio_device *dds;
dds= iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc"); dds = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
struct iio_channel *dds_channel0_I; struct iio_channel *dds_channel0_I;
dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true); dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true);
@ -559,49 +610,55 @@ bool config_ad9361_lo_remote(std::string remote_host,
dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true); dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true);
ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", true); ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", true);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to toggle DDS: "<<ret<<std::endl; {
std::cout << "Failed to toggle DDS: " << ret << std::endl;
} }
//set frequency, scale and phase //set frequency, scale and phase
ret=iio_channel_attr_write_longlong(dds_channel0_I, "frequency",(long long) freq_dds_tx_hz_); ret = iio_channel_attr_write_longlong(dds_channel0_I, "frequency", (long long)freq_dds_tx_hz_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS frequency I: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS frequency I: " << ret << std::endl;
} }
ret=iio_channel_attr_write_longlong(dds_channel0_Q, "frequency",(long long) freq_dds_tx_hz_); ret = iio_channel_attr_write_longlong(dds_channel0_Q, "frequency", (long long)freq_dds_tx_hz_);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS frequency Q: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS frequency Q: " << ret << std::endl;
} }
ret = iio_channel_attr_write_double(dds_channel0_I, "phase", 0.0);
ret=iio_channel_attr_write_double(dds_channel0_I, "phase",0.0); if (ret < 0)
if (ret < 0) { {
std::cout<<"Failed to set TX DDS phase I: "<<ret<<std::endl; std::cout << "Failed to set TX DDS phase I: " << ret << std::endl;
} }
ret=iio_channel_attr_write_double(dds_channel0_Q, "phase",270000.0); ret = iio_channel_attr_write_double(dds_channel0_Q, "phase", 270000.0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS phase Q: "<<ret<<std::endl; {
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)); ret = iio_channel_attr_write_double(dds_channel0_I, "scale", pow(10, scale_dds_dbfs_ / 20.0));
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS scale I: "<<ret<<std::endl; {
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)); ret = iio_channel_attr_write_double(dds_channel0_Q, "scale", pow(10, scale_dds_dbfs_ / 20.0));
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS scale Q: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS scale Q: " << ret << std::endl;
} }
//disable TX2 //disable TX2
ret = iio_device_attr_write_double(ad9361_phy, "out_voltage1_hardwaregain", -89.0); ret = iio_device_attr_write_double(ad9361_phy, "out_voltage1_hardwaregain", -89.0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set out_voltage1_hardwaregain value "<<-89.0<<" error "<<ret<<std::endl; {
std::cout << "Failed to set out_voltage1_hardwaregain value " << -89.0 << " error " << ret << std::endl;
} }
struct iio_channel *dds_channel1_I; struct iio_channel *dds_channel1_I;
@ -610,15 +667,16 @@ bool config_ad9361_lo_remote(std::string remote_host,
struct iio_channel *dds_channel1_Q; struct iio_channel *dds_channel1_Q;
dds_channel1_Q = iio_device_find_channel(dds, "TX2_Q_F1", true); dds_channel1_Q = iio_device_find_channel(dds, "TX2_Q_F1", true);
ret = iio_channel_attr_write_double(dds_channel1_I, "scale", 0);
ret=iio_channel_attr_write_double(dds_channel1_I, "scale",0); if (ret < 0)
if (ret < 0) { {
std::cout<<"Failed to set TX2 DDS scale I: "<<ret<<std::endl; std::cout << "Failed to set TX2 DDS scale I: " << ret << std::endl;
} }
ret=iio_channel_attr_write_double(dds_channel1_Q, "scale",0); ret = iio_channel_attr_write_double(dds_channel1_Q, "scale", 0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX2 DDS scale Q: "<<ret<<std::endl; {
std::cout << "Failed to set TX2 DDS scale Q: " << ret << std::endl;
} }
iio_context_destroy(ctx); iio_context_destroy(ctx);
@ -628,16 +686,16 @@ bool config_ad9361_lo_remote(std::string remote_host,
bool ad9361_disable_lo_remote(std::string remote_host) bool ad9361_disable_lo_remote(std::string remote_host)
{ {
std::cout<<"AD9361 Acquiring IIO REMOTE context in host "<<remote_host<<std::endl; std::cout << "AD9361 Acquiring IIO REMOTE context in host " << remote_host << std::endl;
struct iio_context *ctx; struct iio_context *ctx;
ctx = iio_create_network_context(remote_host.c_str()); ctx = iio_create_network_context(remote_host.c_str());
if (!ctx) if (!ctx)
{ {
std::cout<<"No context\n"; std::cout << "No context\n";
throw std::runtime_error("AD9361 IIO No context"); throw std::runtime_error("AD9361 IIO No context");
} }
struct iio_device *dds; struct iio_device *dds;
dds= iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc"); dds = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
struct iio_channel *dds_channel0_I; struct iio_channel *dds_channel0_I;
dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true); dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true);
@ -645,18 +703,21 @@ bool ad9361_disable_lo_remote(std::string remote_host)
dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true); dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true);
int ret; int ret;
ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", false); ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", false);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to toggle DDS: "<<ret<<std::endl; {
std::cout << "Failed to toggle DDS: " << ret << std::endl;
} }
ret=iio_channel_attr_write_double(dds_channel0_I, "scale",0.0); ret = iio_channel_attr_write_double(dds_channel0_I, "scale", 0.0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS scale I: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS scale I: " << ret << std::endl;
} }
ret=iio_channel_attr_write_double(dds_channel0_Q, "scale",0.0); ret = iio_channel_attr_write_double(dds_channel0_Q, "scale", 0.0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS scale Q: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS scale Q: " << ret << std::endl;
} }
iio_context_destroy(ctx); iio_context_destroy(ctx);
@ -664,18 +725,19 @@ bool ad9361_disable_lo_remote(std::string remote_host)
return true; return true;
} }
bool ad9361_disable_lo_local() bool ad9361_disable_lo_local()
{ {
std::cout<<"AD9361 Acquiring IIO LOCAL context"<<std::endl; std::cout << "AD9361 Acquiring IIO LOCAL context" << std::endl;
struct iio_context *ctx; struct iio_context *ctx;
ctx = iio_create_default_context(); ctx = iio_create_default_context();
if (!ctx) if (!ctx)
{ {
std::cout<<"No context\n"; std::cout << "No context\n";
throw std::runtime_error("AD9361 IIO No context"); throw std::runtime_error("AD9361 IIO No context");
} }
struct iio_device *dds; struct iio_device *dds;
dds= iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc"); dds = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
struct iio_channel *dds_channel0_I; struct iio_channel *dds_channel0_I;
dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true); dds_channel0_I = iio_device_find_channel(dds, "TX1_I_F1", true);
@ -683,22 +745,24 @@ bool ad9361_disable_lo_local()
dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true); dds_channel0_Q = iio_device_find_channel(dds, "TX1_Q_F1", true);
int ret; int ret;
ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", false); ret = iio_channel_attr_write_bool(dds_channel0_I, "raw", false);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to toggle DDS: "<<ret<<std::endl; {
std::cout << "Failed to toggle DDS: " << ret << std::endl;
} }
ret=iio_channel_attr_write_double(dds_channel0_I, "scale",0.0); ret = iio_channel_attr_write_double(dds_channel0_I, "scale", 0.0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS scale I: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS scale I: " << ret << std::endl;
} }
ret=iio_channel_attr_write_double(dds_channel0_Q, "scale",0.0); ret = iio_channel_attr_write_double(dds_channel0_Q, "scale", 0.0);
if (ret < 0) { if (ret < 0)
std::cout<<"Failed to set TX DDS scale Q: "<<ret<<std::endl; {
std::cout << "Failed to set TX DDS scale Q: " << ret << std::endl;
} }
iio_context_destroy(ctx); iio_context_destroy(ctx);
return true; return true;
} }

View File

@ -30,8 +30,8 @@
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
*/ */
#ifndef __AD9361_MANAGER__ #ifndef GNSS_SDR_AD9361_MANAGER_H
#define __AD9361_MANAGER__ #define GNSS_SDR_AD9361_MANAGER_H
#include <string> #include <string>
@ -42,31 +42,36 @@
#endif #endif
/* RX is input, TX is output */ /* RX is input, TX is output */
enum iodev { RX, TX }; enum iodev
{
RX,
TX
};
/* common RX and TX streaming params */ /* common RX and TX streaming params */
struct stream_cfg { struct stream_cfg
{
long long bw_hz; // Analog banwidth in Hz long long bw_hz; // Analog banwidth in Hz
long long fs_hz; // Baseband sample rate in Hz long long fs_hz; // Baseband sample rate in Hz
long long lo_hz; // Local oscillator frequency in Hz long long lo_hz; // Local oscillator frequency in Hz
const char* rfport; // Port name const char *rfport; // Port name
}; };
/* check return value of attr_write function */ /* check return value of attr_write function */
void errchk(int v, const char* what); void errchk(int v, const char *what);
/* write attribute: long long int */ /* write attribute: long long int */
void wr_ch_lli(struct iio_channel *chn, const char* what, long long val); void wr_ch_lli(struct iio_channel *chn, const char *what, long long val);
/* write attribute: string */ /* write attribute: string */
void wr_ch_str(struct iio_channel *chn, const char* what, const char* str); void wr_ch_str(struct iio_channel *chn, const char *what, const char *str);
/* helper function generating channel names */ /* helper function generating channel names */
char* get_ch_name(const char* type, int id, char* tmpstr); char *get_ch_name(const char *type, int id, char *tmpstr);
/* returns ad9361 phy device */ /* returns ad9361 phy device */
struct iio_device* get_ad9361_phy(struct iio_context *ctx); struct iio_device *get_ad9361_phy(struct iio_context *ctx);
/* finds AD9361 streaming IIO devices */ /* finds AD9361 streaming IIO devices */
bool get_ad9361_stream_dev(struct iio_context *ctx, enum iodev d, struct iio_device **dev); bool get_ad9361_stream_dev(struct iio_context *ctx, enum iodev d, struct iio_device **dev);
@ -83,7 +88,6 @@ bool get_lo_chan(struct iio_context *ctx, enum iodev d, struct iio_channel **chn
/* applies streaming configuration through IIO */ /* applies streaming configuration through IIO */
bool cfg_ad9361_streaming_ch(struct iio_context *ctx, struct stream_cfg *cfg, enum iodev type, int chid); bool cfg_ad9361_streaming_ch(struct iio_context *ctx, struct stream_cfg *cfg, enum iodev type, int chid);
bool config_ad9361_rx_local(unsigned long bandwidth_, bool config_ad9361_rx_local(unsigned long bandwidth_,
unsigned long sample_rate_, unsigned long sample_rate_,
unsigned long freq_, unsigned long freq_,
@ -123,5 +127,4 @@ bool ad9361_disable_lo_remote(std::string remote_host);
bool ad9361_disable_lo_local(); bool ad9361_disable_lo_local();
#endif #endif