From 982af827b4c973ec14aeb23cd7618750ec5a99d5 Mon Sep 17 00:00:00 2001 From: Javier Arribas Date: Tue, 27 Mar 2018 19:00:25 +0200 Subject: [PATCH] Group all ad9361 configuration calls in one library --- .../adapters/ad9361_fpga_signal_source.cc | 371 +-------- .../signal_source/libs/CMakeLists.txt | 47 +- .../signal_source/libs/ad9361_manager.cc | 704 ++++++++++++++++++ .../signal_source/libs/ad9361_manager.h | 127 ++++ 4 files changed, 898 insertions(+), 351 deletions(-) create mode 100644 src/algorithms/signal_source/libs/ad9361_manager.cc create mode 100644 src/algorithms/signal_source/libs/ad9361_manager.h diff --git a/src/algorithms/signal_source/adapters/ad9361_fpga_signal_source.cc b/src/algorithms/signal_source/adapters/ad9361_fpga_signal_source.cc index ff6860499..cf9130767 100644 --- a/src/algorithms/signal_source/adapters/ad9361_fpga_signal_source.cc +++ b/src/algorithms/signal_source/adapters/ad9361_fpga_signal_source.cc @@ -32,6 +32,7 @@ #include "ad9361_fpga_signal_source.h" #include "configuration_interface.h" +#include "ad9361_manager.h" #include "GPS_L1_CA.h" #include "GPS_L2C.h" #include @@ -45,123 +46,6 @@ #include #endif - -/* RX is input, TX is output */ -enum iodev { RX, TX }; - -/* common RX and TX streaming params */ -struct stream_cfg { - long long bw_hz; // Analog banwidth in Hz - long long fs_hz; // Baseband sample rate in Hz - long long lo_hz; // Local oscillator frequency in Hz - const char* rfport; // Port name -}; - - -using google::LogMessage; - - -/* static scratch mem for strings */ -static char tmpstr[64]; - -/* IIO structs required for streaming */ -static struct iio_context *ctx = NULL; -static struct iio_channel *rx0_i = NULL; -static struct iio_channel *rx0_q = NULL; - -/* check return value of attr_write function */ -static void errchk(int v, const char* what) { - if (v < 0) { fprintf(stderr, "Error %d writing to channel \"%s\"\nvalue may not be supported.\n", v, what); } -} - -/* write attribute: long long int */ -static void wr_ch_lli(struct iio_channel *chn, const char* what, long long val) -{ - errchk(iio_channel_attr_write_longlong(chn, what, val), what); -} - -/* write attribute: string */ -static void wr_ch_str(struct iio_channel *chn, const char* what, const char* str) -{ - errchk(iio_channel_attr_write(chn, what, str), what); -} - - -/* helper function generating channel names */ -static char* get_ch_name(const char* type, int id) -{ - snprintf(tmpstr, sizeof(tmpstr), "%s%d", type, id); - return tmpstr; -} - -/* returns ad9361 phy device */ -static struct iio_device* get_ad9361_phy(struct iio_context *ctx) -{ - struct iio_device *dev = iio_context_find_device(ctx, "ad9361-phy"); - return dev; -} - -/* finds AD9361 streaming IIO devices */ -static bool get_ad9361_stream_dev(struct iio_context *ctx, enum iodev d, struct iio_device **dev) -{ - 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; - default: return false; - } -} - -/* finds AD9361 streaming IIO channels */ -static bool get_ad9361_stream_ch(struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, struct iio_channel **chn) -{ - *chn = iio_device_find_channel(dev, get_ch_name("voltage", chid), d == TX); - if (!*chn) - *chn = iio_device_find_channel(dev, get_ch_name("altvoltage", chid), d == TX); - return *chn != NULL; -} - -/* finds AD9361 phy IIO configuration channel with id chid */ -static bool get_phy_chan(struct iio_context *ctx, enum iodev d, int chid, struct iio_channel **chn) -{ - switch (d) { - case RX: *chn = iio_device_find_channel(get_ad9361_phy(ctx), get_ch_name("voltage", chid), false); return *chn != NULL; - case TX: *chn = iio_device_find_channel(get_ad9361_phy(ctx), get_ch_name("voltage", chid), true); return *chn != NULL; - default: return false; - } -} - -/* finds AD9361 local oscillator IIO configuration channels */ -static bool get_lo_chan(struct iio_context *ctx, enum iodev d, struct iio_channel **chn) -{ - switch (d) { - // LO chan is always output, i.e. true - case RX: *chn = iio_device_find_channel(get_ad9361_phy(ctx), get_ch_name("altvoltage", 0), true); return *chn != NULL; - case TX: *chn = iio_device_find_channel(get_ad9361_phy(ctx), get_ch_name("altvoltage", 1), true); return *chn != NULL; - default: return false; - } -} - - -/* applies streaming configuration through IIO */ -bool cfg_ad9361_streaming_ch(struct iio_context *ctx, struct stream_cfg *cfg, enum iodev type, int chid) -{ - struct iio_channel *chn = NULL; - - // Configure phy and lo channels - printf("* Acquiring AD9361 phy channel %d\n", chid); - 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); - wr_ch_lli(chn, "sampling_frequency", cfg->fs_hz); - - // Configure LO channel - printf("* Acquiring AD9361 %s lo channel\n", type == TX ? "TX" : "RX"); - if (!get_lo_chan(ctx, type, &chn)) { return false; } - wr_ch_lli(chn, "frequency", cfg->lo_hz); - return true; -} - - Ad9361FpgaSignalSource::Ad9361FpgaSignalSource(ConfigurationInterface* configuration, std::string role, unsigned int in_stream, unsigned int out_stream, boost::shared_ptr queue) : @@ -170,7 +54,6 @@ Ad9361FpgaSignalSource::Ad9361FpgaSignalSource(ConfigurationInterface* configura { std::string default_item_type = "gr_complex"; std::string default_dump_file = "./data/signal_source.dat"; - uri_ = configuration->property(role + ".device_address", std::string("192.168.2.1")); freq_ = configuration->property(role + ".freq", GPS_L1_FREQ_HZ); sample_rate_ = configuration->property(role + ".sampling_frequency", 2600000); bandwidth_ = configuration->property(role + ".bandwidth", 2000000); @@ -205,212 +88,24 @@ Ad9361FpgaSignalSource::Ad9361FpgaSignalSource(ConfigurationInterface* configura std::cout << "LO frequency : " << freq_ << " Hz" << std::endl; std::cout << "sample rate: " << sample_rate_ << " Hz" << std::endl; - - // AD9361 Frontend IC device operation - int ret; - - - // Streaming devices - struct iio_device *rx; - - // RX stream config - // Stream configurations - 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.) - - - std::cout<<"AD9361 Acquiring IIO context\n"; - ctx = iio_create_default_context(); - if (!ctx) - { - std::cout<<"No context\n"; - throw std::runtime_error("AD9361 IIO No context"); - } - - if (iio_context_get_devices_count(ctx) <= 0) - { - std::cout<<"No devices\n"; - throw std::runtime_error("AD9361 IIO No devices"); - } - - std::cout<<"* Acquiring AD9361 streaming devices\n"; - - 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"); - }; - - std::cout<<"* Configuring AD9361 for streaming\n"; - 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"); - } - - 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"); - } - - std::cout<<"* Enabling IIO streaming channels\n"; - iio_channel_enable(rx0_i); - iio_channel_enable(rx0_q); - - - - struct iio_device *ad9361_phy; - ad9361_phy= iio_context_find_device(ctx, "ad9361-phy"); - ret=iio_device_attr_write(ad9361_phy,"trx_rate_governor","nominal"); - if (ret < 0) { - std::cout<<"Failed to set trx_rate_governor: "<. # +if(ENABLE_PLUTOSDR OR ENABLE_FMCOMMS2) + find_package(iio REQUIRED) + if(NOT IIO_FOUND) + message(STATUS "gnuradio-iio not found, its installation is required.") + message(STATUS "Please build and install the following projects:") + message(STATUS " * libiio from https://github.com/analogdevicesinc/libiio") + message(STATUS " * libad9361-iio from https://github.com/analogdevicesinc/libad9361-iio") + message(STATUS " * gnuradio-iio from https://github.com/analogdevicesinc/gr-iio") + message(FATAL_ERROR "gnuradio-iio required for building gnss-sdr with this option enabled") + endif(NOT IIO_FOUND) + set(OPT_LIBRARIES ${OPT_LIBRARIES} ${IIO_LIBRARIES}) + set(OPT_DRIVER_INCLUDE_DIRS ${OPT_DRIVER_INCLUDE_DIRS} ${IIO_INCLUDE_DIRS}) + +endif(ENABLE_PLUTOSDR OR ENABLE_FMCOMMS2) + +if(ENABLE_FMCOMMS2 OR ENABLE_AD9361) + find_package(libiio REQUIRED) + if(NOT LIBIIO_FOUND) + message(STATUS "gnuradio-iio not found, its installation is required.") + message(STATUS "Please build and install the following projects:") + message(STATUS " * libiio from https://github.com/analogdevicesinc/libiio") + message(STATUS " * libad9361-iio from https://github.com/analogdevicesinc/libad9361-iio") + message(STATUS " * gnuradio-iio from https://github.com/analogdevicesinc/gr-iio") + message(FATAL_ERROR "gnuradio-iio required for building gnss-sdr with this option enabled") + endif(NOT LIBIIO_FOUND) + set(OPT_LIBRARIES ${OPT_LIBRARIES} ${LIBIIO_LIBRARIES}) + set(OPT_DRIVER_INCLUDE_DIRS ${OPT_DRIVER_INCLUDE_DIRS} ${LIBIIO_INCLUDE_DIRS}) + + ############################################### + # FMCOMMS2 based SDR Hardware + ############################################### + if(IIO_FOUND) + set(OPT_SIGNAL_SOURCE_LIB_SOURCES ad9361_manager.cc) + endif(IIO_FOUND) + +endif(ENABLE_FMCOMMS2 OR ENABLE_AD9361) + set (SIGNAL_SOURCE_LIB_SOURCES rtl_tcp_commands.cc - rtl_tcp_dongle_info.cc) + rtl_tcp_dongle_info.cc + ${OPT_SIGNAL_SOURCE_LIB_SOURCES}) include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDE_DIRS} + ${OPT_DRIVER_INCLUDE_DIRS} ) + file(GLOB SIGNAL_SOURCE_LIB_HEADERS "*.h") list(SORT SIGNAL_SOURCE_LIB_HEADERS) add_library(signal_source_lib ${SIGNAL_SOURCE_LIB_SOURCES} ${SIGNAL_SOURCE_LIB_HEADERS}) -source_group(Headers FILES ${SIGNAL_SOURCE_LIB_HEADERS}) \ No newline at end of file +source_group(Headers FILES ${SIGNAL_SOURCE_LIB_HEADERS}) + +target_link_libraries(signal_source_lib ${OPT_LIBRARIES}) + diff --git a/src/algorithms/signal_source/libs/ad9361_manager.cc b/src/algorithms/signal_source/libs/ad9361_manager.cc new file mode 100644 index 000000000..66a95ee14 --- /dev/null +++ b/src/algorithms/signal_source/libs/ad9361_manager.cc @@ -0,0 +1,704 @@ +/*! + * \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/ + * ------------------------------------------------------------------------- + * + * Copyright (C) 2010-2018 (see AUTHORS file for a list of contributors) + * + * 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 + * along with GNSS-SDR. If not, see . + * + * ------------------------------------------------------------------------- + */ +#include "ad9361_manager.h" +#include +#include +#include +#include + +/* check return value of attr_write function */ +void errchk(int v, const char* what) { + if (v < 0) + { + LOG(WARNING)<<"Error "<rfport); + wr_ch_lli(chn, "rf_bandwidth", cfg->bw_hz); + wr_ch_lli(chn, "sampling_frequency", cfg->fs_hz); + + // Configure LO channel + //LOG(INFO)<<"* Acquiring AD9361 "<lo_hz); + return true; +} + + +bool config_ad9361_rx_local(unsigned long bandwidth_, + unsigned long sample_rate_, + unsigned long freq_, + std::string rf_port_select_, + std::string gain_mode_rx1_, + std::string gain_mode_rx2_, + double rf_gain_rx1_, + double rf_gain_rx2_) + +{ + // RX stream config + // Stream configurations + 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.) + + std::cout<<"AD9361 Acquiring IIO LOCAL context\n"; + struct iio_context *ctx; + // Streaming devices + struct iio_device *rx; + struct iio_channel *rx0_i; + struct iio_channel *rx0_q; + + ctx = iio_create_default_context(); + if (!ctx) + { + std::cout<<"No context\n"; + throw std::runtime_error("AD9361 IIO No context"); + } + + if (iio_context_get_devices_count(ctx) <= 0) + { + std::cout<<"No devices\n"; + throw std::runtime_error("AD9361 IIO No devices"); + } + + std::cout<<"* Acquiring AD9361 streaming devices\n"; + + 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"); + }; + + std::cout<<"* Configuring AD9361 for streaming\n"; + 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"); + } + + 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"); + } + + std::cout<<"* Enabling IIO streaming channels\n"; + iio_channel_enable(rx0_i); + iio_channel_enable(rx0_q); + + struct iio_device *ad9361_phy; + ad9361_phy= iio_context_find_device(ctx, "ad9361-phy"); + int ret; + ret=iio_device_attr_write(ad9361_phy,"trx_rate_governor","nominal"); + if (ret < 0) { + std::cout<<"Failed to set trx_rate_governor: "<. + * + * ------------------------------------------------------------------------- + */ + +#ifndef __AD9361_MANAGER__ +#define __AD9361_MANAGER__ + +#include + +#ifdef __APPLE__ +#include +#else +#include +#endif + +/* RX is input, TX is output */ +enum iodev { RX, TX }; + +/* common RX and TX streaming params */ +struct stream_cfg { + long long bw_hz; // Analog banwidth in Hz + long long fs_hz; // Baseband sample rate in Hz + long long lo_hz; // Local oscillator frequency in Hz + const char* rfport; // Port name +}; + + +/* check return value of attr_write function */ +void errchk(int v, const char* what); + +/* write attribute: long long int */ +void wr_ch_lli(struct iio_channel *chn, const char* what, long long val); + +/* write attribute: string */ +void wr_ch_str(struct iio_channel *chn, const char* what, const char* str); + +/* helper function generating channel names */ +char* get_ch_name(const char* type, int id, char* tmpstr); + +/* returns ad9361 phy device */ +struct iio_device* get_ad9361_phy(struct iio_context *ctx); + +/* finds AD9361 streaming IIO devices */ +bool get_ad9361_stream_dev(struct iio_context *ctx, enum iodev d, struct iio_device **dev); + +/* 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); + +/* 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); + +/* finds AD9361 local oscillator IIO configuration channels */ +bool get_lo_chan(struct iio_context *ctx, enum iodev d, struct iio_channel **chn); + +/* applies streaming configuration through IIO */ +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_, + unsigned long sample_rate_, + unsigned long freq_, + std::string rf_port_select_, + std::string gain_mode_rx1_, + std::string gain_mode_rx2_, + double rf_gain_rx1_, + double rf_gain_rx2_); + +bool config_ad9361_rx_remote(std::string remote_host, + unsigned long bandwidth_, + unsigned long sample_rate_, + unsigned long freq_, + std::string rf_port_select_, + std::string gain_mode_rx1_, + std::string gain_mode_rx2_, + double rf_gain_rx1_, + double rf_gain_rx2_); + +bool config_ad9361_lo_local(unsigned long bandwidth_, + unsigned long sample_rate_, + unsigned long freq_rf_tx_hz_, + double tx_attenuation_db_, + long long freq_dds_tx_hz_, + double scale_dds_dbfs_); + +bool config_ad9361_lo_remote(std::string remote_host, + unsigned long bandwidth_, + unsigned long sample_rate_, + unsigned long freq_rf_tx_hz_, + double tx_attenuation_db_, + long long freq_dds_tx_hz_, + double scale_dds_dbfs_); + + +bool ad9361_disable_lo_remote(std::string remote_host); + +bool ad9361_disable_lo_local(); + + +#endif