diff --git a/CMakeLists.txt b/CMakeLists.txt index 332f34e53..3a93d2cf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2286,6 +2286,14 @@ else() TYPE OPTIONAL ) endif() +find_package(LIBAD9361) +set_package_properties(LIBAD9361 PROPERTIES + PURPOSE "Used for configuring the AD9361 chipset" + TYPE OPTIONAL +) +if(NOT LIBAD9361_FOUND) + set(GRIIO_FOUND FALSE) +endif() if(ENABLE_PLUTOSDR OR ENABLE_FMCOMMS2) if(NOT GRIIO_FOUND) message(STATUS "gnuradio-iio not found, its installation is required.") diff --git a/cmake/Modules/FindLIBAD9361.cmake b/cmake/Modules/FindLIBAD9361.cmake new file mode 100644 index 000000000..a37671184 --- /dev/null +++ b/cmake/Modules/FindLIBAD9361.cmake @@ -0,0 +1,124 @@ +# Copyright (C) 2011-2019 (see AUTHORS file for a list of contributors) +# +# 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 +# along with GNSS-SDR. If not, see . + +# +# Provides the following imported target: +# Iio::iio +# + +if(NOT COMMAND feature_summary) + include(FeatureSummary) +endif() + +set(PKG_CONFIG_USE_CMAKE_PREFIX_PATH TRUE) +include(FindPkgConfig) +pkg_check_modules(PC_LIBAD9361 libad9361) + +find_path( + LIBAD9361_INCLUDE_DIRS + NAMES ad9361.h + HINTS ${PC_LIBAD9361_INCLUDEDIR} + PATHS /usr/include + /usr/local/include + /opt/local/include + ${CMAKE_INSTALL_PREFIX}/include + ${LIBAD9361_ROOT}/include + $ENV{LIBAD9361_ROOT}/include + $ENV{LIBAD9361_DIR}/include +) + +find_library( + LIBAD9361_LIBRARIES + NAMES ad9361 + HINTS ${PC_LIBAD9361_LIBDIR} + PATHS /usr/lib + /usr/lib64 + /usr/lib/x86_64-linux-gnu + /usr/lib/i386-linux-gnu + /usr/lib/alpha-linux-gnu + /usr/lib/aarch64-linux-gnu + /usr/lib/arm-linux-gnueabi + /usr/lib/arm-linux-gnueabihf + /usr/lib/hppa-linux-gnu + /usr/lib/i686-gnu + /usr/lib/i686-linux-gnu + /usr/lib/x86_64-kfreebsd-gnu + /usr/lib/i686-kfreebsd-gnu + /usr/lib/m68k-linux-gnu + /usr/lib/mips-linux-gnu + /usr/lib/mips64el-linux-gnuabi64 + /usr/lib/mipsel-linux-gnu + /usr/lib/powerpc-linux-gnu + /usr/lib/powerpc-linux-gnuspe + /usr/lib/powerpc64-linux-gnu + /usr/lib/powerpc64le-linux-gnu + /usr/lib/s390x-linux-gnu + /usr/lib/sparc64-linux-gnu + /usr/lib/x86_64-linux-gnux32 + /usr/lib/sh4-linux-gnu + /usr/lib/riscv64-linux-gnu + /usr/local/lib + /usr/local/lib64 + /opt/local/lib + /Library/Frameworks/iio.framework/ + ${CMAKE_INSTALL_PREFIX}/lib + ${CMAKE_INSTALL_PREFIX}/lib64 + ${LIBAD9361_ROOT}/lib + $ENV{LIBAD9361_ROOT}/lib + ${LIBAD9361_ROOT}/lib64 + $ENV{LIBAD9361_ROOT}/lib64 + $ENV{LIBAD9361_DIR}/lib +) + +if(LIBAD9361_LIBRARIES AND APPLE) + if(LIBAD9361_LIBRARIES MATCHES "framework") + set(LIBAD9361_LIBRARIES ${LIBAD9361_LIBRARIES}/ad9361) + endif() +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LIBAD9361 DEFAULT_MSG LIBAD9361_LIBRARIES LIBAD9361_INCLUDE_DIRS) + +if(PC_LIBAD9361_VERSION) + set(LIBAD9361_VERSION ${PC_LIBAD9361_VERSION}) +endif() + +if(LIBAD9361_FOUND AND LIBAD9361_VERSION) + set_package_properties(LIBAD9361 PROPERTIES + DESCRIPTION "A library for interfacing with AD936X transceivers (found: v${LIBAD9361_VERSION})" + ) +else() + set_package_properties(LIBAD9361 PROPERTIES + DESCRIPTION "A library for interfacing with AD936X transceivers" + ) +endif() + +set_package_properties(LIBAD9361 PROPERTIES + URL "https://github.com/analogdevicesinc/libad9361-iio" +) + +if(LIBAD9361_FOUND AND NOT TARGET Iio::ad9361) + add_library(Iio::ad9361 SHARED IMPORTED) + set_target_properties(Iio::ad9361 PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${LIBAD9361_LIBRARIES}" + INTERFACE_INCLUDE_DIRECTORIES "${LIBAD9361_INCLUDE_DIRS}" + INTERFACE_LINK_LIBRARIES "${LIBAD9361_LIBRARIES}" + ) +endif() + +mark_as_advanced(LIBAD9361_LIBRARIES LIBAD9361_INCLUDE_DIRS) diff --git a/src/algorithms/signal_source/libs/CMakeLists.txt b/src/algorithms/signal_source/libs/CMakeLists.txt index 98fdbf7cd..48f8edb74 100644 --- a/src/algorithms/signal_source/libs/CMakeLists.txt +++ b/src/algorithms/signal_source/libs/CMakeLists.txt @@ -67,6 +67,8 @@ if(ENABLE_FMCOMMS2 OR ENABLE_AD9361) target_link_libraries(signal_source_libs PUBLIC Iio::iio + PRIVATE + Iio::ad9361 ) endif() diff --git a/src/algorithms/signal_source/libs/ad9361_manager.cc b/src/algorithms/signal_source/libs/ad9361_manager.cc index c016c95ed..4a7611ef8 100644 --- a/src/algorithms/signal_source/libs/ad9361_manager.cc +++ b/src/algorithms/signal_source/libs/ad9361_manager.cc @@ -31,9 +31,12 @@ */ #include "ad9361_manager.h" #include +#include #include +#include // for ifstream #include #include +#include /* 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 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; +} diff --git a/src/algorithms/signal_source/libs/ad9361_manager.h b/src/algorithms/signal_source/libs/ad9361_manager.h index 1d9bd42d0..10353ac64 100644 --- a/src/algorithms/signal_source/libs/ad9361_manager.h +++ b/src/algorithms/signal_source/libs/ad9361_manager.h @@ -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_