hackrf: add support for HackRF devices through libhackrf

features:

- gain control for LNA & VGA
- frequency error correction
- automatic baseband filter
- up to 20M sampling rate

limitations:

- no DC offset correction implemented (yet)
- no RX preamplifier control (disabled by default)
- high sampling rates may not work on slow machines
gr3.6
Dimitri Stolnikov 10 years ago
parent fd56ae7640
commit f5670788fc
  1. 5
      CMakeLists.txt
  2. 24
      cmake/Modules/FindLibHackRF.cmake
  3. 2
      grc/gen_osmosdr_blocks.py
  4. 12
      lib/CMakeLists.txt
  5. 1
      lib/config.h.in
  6. 37
      lib/hackrf/CMakeLists.txt
  7. 74
      lib/hackrf/hackrf_sink_c.cc
  8. 62
      lib/hackrf/hackrf_sink_c.h
  9. 623
      lib/hackrf/hackrf_source_c.cc
  10. 154
      lib/hackrf/hackrf_source_c.h
  11. 18
      lib/osmosdr_device.cc
  12. 2
      lib/osmosdr_sink_c_impl.cc
  13. 18
      lib/osmosdr_source_c_impl.cc

@ -116,13 +116,14 @@ find_package(GnuradioFCD)
find_package(LibOsmoSDR)
find_package(LibRTLSDR)
find_package(LibMiriSDR)
find_package(LibHackRF)
if(NOT GRUEL_FOUND)
message(FATAL_ERROR "Gruel required to compile osmosdr")
message(FATAL_ERROR "Gruel required to compile gnuradio-osmosdr")
endif()
if(NOT GNURADIO_CORE_FOUND)
message(FATAL_ERROR "GnuRadio Core required to compile osmosdr")
message(FATAL_ERROR "GnuRadio Core required to compile gnuradio-osmosdr")
endif()
########################################################################

@ -0,0 +1,24 @@
INCLUDE(FindPkgConfig)
PKG_CHECK_MODULES(PC_LIBHACKRF libhackrf)
FIND_PATH(
LIBHACKRF_INCLUDE_DIRS
NAMES libhackrf/hackrf.h
HINTS $ENV{LIBHACKRF_DIR}/include
${PC_LIBHACKRF_INCLUDEDIR}
PATHS /usr/local/include
/usr/include
)
FIND_LIBRARY(
LIBHACKRF_LIBRARIES
NAMES hackrf
HINTS $ENV{LIBHACKRF_DIR}/lib
${PC_LIBHACKRF_LIBDIR}
PATHS /usr/local/lib
/usr/lib
)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBHACKRF DEFAULT_MSG LIBHACKRF_LIBRARIES LIBHACKRF_INCLUDE_DIRS)
MARK_AS_ADVANCED(LIBHACKRF_LIBRARIES LIBHACKRF_INCLUDE_DIRS)

@ -108,6 +108,7 @@ While primarily being developed for the OsmoSDR hardware, this block as well sup
* FunCube Dongle through libgnuradio-fcd
* OSMOCOM OsmoSDR Devices through libosmosdr
* Great Scott Gadgets HackRF through libhackrf
* Ettus USRP Devices through Ettus UHD library
* RTL2832U based DVB-T dongles through librtlsdr
* RTL-TCP spectrum server (see librtlsdr project)
@ -129,6 +130,7 @@ Optional arguments are placed into [] brackets, remove the brackets before using
Lines ending with ... mean it's possible to bind devices together by specifying multiple device arguments separated with a space.
fcd=0
hackrf=0
miri=0[,buffers=32] ...
rtl=serial_number ...
rtl=0[,rtl_xtal=28.8e6][,tuner_xtal=28.8e6] ...

@ -108,7 +108,7 @@ endif(ENABLE_RTL_TCP)
# Setup UHD component
########################################################################
include(GrComponent)
GR_REGISTER_COMPONENT("Ettus UHD" ENABLE_UHD UHD_FOUND GNURADIO_UHD_FOUND)
GR_REGISTER_COMPONENT("Ettus USRP Devices" ENABLE_UHD UHD_FOUND GNURADIO_UHD_FOUND)
if(ENABLE_UHD)
GR_INCLUDE_SUBDIRECTORY(uhd)
@ -124,6 +124,16 @@ if(ENABLE_MIRI)
GR_INCLUDE_SUBDIRECTORY(miri)
endif(ENABLE_MIRI)
########################################################################
# Setup HackRF component
########################################################################
include(GrComponent)
GR_REGISTER_COMPONENT("HackRF Jawbreaker" ENABLE_HACKRF LIBHACKRF_FOUND)
if(ENABLE_HACKRF)
GR_INCLUDE_SUBDIRECTORY(hackrf)
endif(ENABLE_HACKRF)
########################################################################
# Setup configuration file
########################################################################

@ -11,5 +11,6 @@
#cmakedefine ENABLE_RTL_TCP
#cmakedefine ENABLE_UHD
#cmakedefine ENABLE_MIRI
#cmakedefine ENABLE_HACKRF
#endif // CONFIG_H_IN

@ -0,0 +1,37 @@
# Copyright 2012 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio 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, or (at your option)
# any later version.
#
# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
########################################################################
# This file included, use CMake directory variables
########################################################################
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${LIBHACKRF_INCLUDE_DIRS}
)
set(hackrf_srcs
${CMAKE_CURRENT_SOURCE_DIR}/hackrf_source_c.cc
)
########################################################################
# Append gnuradio-osmosdr library sources
########################################################################
list(APPEND gr_osmosdr_srcs ${hackrf_srcs})
list(APPEND gr_osmosdr_libs ${LIBHACKRF_LIBRARIES})

@ -0,0 +1,74 @@
/* -*- c++ -*- */
/*
* Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
*
* GNU Radio 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, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
/*
* config.h is generated by configure. It contains the results
* of probing for features, options etc. It should be the first
* file included in your .cc file.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "hackrf_sink_c.h"
#include <gnuradio/gr_io_signature.h>
/*
* Create a new instance of hackrf_sink_c and return
* a boost shared_ptr. This is effectively the public constructor.
*/
hackrf_sink_c_sptr
make_hackrf_sink_c (const std::string &args)
{
return gnuradio::get_initial_sptr(new hackrf_sink_c (args));
}
/*
* Specify constraints on number of input and output streams.
* This info is used to construct the input and output signatures
* (2nd & 3rd args to gr_block's constructor). The input and
* output signatures are used by the runtime system to
* check that a valid number and type of inputs and outputs
* are connected to this block. In this case, we accept
* only 1 input and 0 output.
*/
static const int MIN_IN = 1; // mininum number of input streams
static const int MAX_IN = 1; // maximum number of input streams
static const int MIN_OUT = 0; // minimum number of output streams
static const int MAX_OUT = 0; // maximum number of output streams
/*
* The private constructor
*/
hackrf_sink_c::hackrf_sink_c (const std::string &args)
: gr_sync_block ("hackrf_sink_c",
gr_make_io_signature (MIN_IN, MAX_IN, sizeof (gr_complex)),
gr_make_io_signature (MIN_OUT, MAX_OUT, sizeof (gr_complex)))
{
}
/*
* Our virtual destructor.
*/
hackrf_sink_c::~hackrf_sink_c ()
{
}

@ -0,0 +1,62 @@
/* -*- c++ -*- */
/*
* Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
*
* GNU Radio 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, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef INCLUDED_HACKRF_SINK_C_H
#define INCLUDED_HACKRF_SINK_C_H
#include <boost/shared_ptr.hpp>
class hackrf_sink_c;
/*
* We use boost::shared_ptr's instead of raw pointers for all access
* to gr_blocks (and many other data structures). The shared_ptr gets
* us transparent reference counting, which greatly simplifies storage
* management issues. This is especially helpful in our hybrid
* C++ / Python system.
*
* See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
*
* As a convention, the _sptr suffix indicates a boost::shared_ptr
*/
typedef boost::shared_ptr<hackrf_sink_c> hackrf_sink_c_sptr;
/*!
* \brief Return a shared_ptr to a new instance of hackrf_sink_c.
*
* To avoid accidental use of raw pointers, hackrf_sink_c's
* constructor is private. make_hackrf_sink_c is the public
* interface for creating new instances.
*/
hackrf_sink_c_sptr make_hackrf_sink_c (const std::string & args = "");
class hackrf_sink_c
{
private:
// The friend declaration allows hackrf_make_sink_c to
// access the private constructor.
friend hackrf_sink_c_sptr make_hackrf_sink_c (const std::string & args);
hackrf_sink_c (const std::string & args); // private constructor
public:
~hackrf_source_c (); // public destructor
};
#endif /* INCLUDED_HACKRF_SINK_C_H */

@ -0,0 +1,623 @@
/* -*- c++ -*- */
/*
* Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
*
* GNU Radio 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, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
/*
* config.h is generated by configure. It contains the results
* of probing for features, options etc. It should be the first
* file included in your .cc file.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "hackrf_source_c.h"
#include <gnuradio/gr_io_signature.h>
#include <boost/assign.hpp>
#include <boost/format.hpp>
#include <boost/detail/endian.hpp>
#include <boost/algorithm/string.hpp>
#include <stdexcept>
#include <iostream>
#include <osmosdr_arg_helpers.h>
using namespace boost::assign;
#define BUF_LEN (16 * 32 * 512) /* must be multiple of 512 */
#define BUF_NUM 32
#define BUF_SKIP 1 // buffers to skip due to initial garbage
#define BYTES_PER_SAMPLE 2 // HackRF device delivers 8 bit unsigned IQ data
int hackrf_source_c::_usage = 0;
boost::mutex hackrf_source_c::_usage_mutex;
hackrf_source_c_sptr make_hackrf_source_c (const std::string & args)
{
return gnuradio::get_initial_sptr(new hackrf_source_c (args));
}
/*
* Specify constraints on number of input and output streams.
* This info is used to construct the input and output signatures
* (2nd & 3rd args to gr_block's constructor). The input and
* output signatures are used by the runtime system to
* check that a valid number and type of inputs and outputs
* are connected to this block. In this case, we accept
* only 0 input and 1 output.
*/
static const int MIN_IN = 0; // mininum number of input streams
static const int MAX_IN = 0; // maximum number of input streams
static const int MIN_OUT = 1; // minimum number of output streams
static const int MAX_OUT = 1; // maximum number of output streams
/*
* The private constructor
*/
hackrf_source_c::hackrf_source_c (const std::string &args)
: gr_sync_block ("hackrf_source_c",
gr_make_io_signature (MIN_IN, MAX_IN, sizeof (gr_complex)),
gr_make_io_signature (MIN_OUT, MAX_OUT, sizeof (gr_complex))),
_dev(NULL),
_buf(NULL),
_sample_rate(0),
_center_freq(0),
_freq_corr(0),
_auto_gain(false),
_if_gain(0),
_skipped(0)
{
int ret;
uint16_t val;
// dict_t dict = params_to_dict(args);
_buf_num = _buf_len = _buf_head = _buf_used = _buf_offset = 0;
// if (dict.count("buffers"))
// _buf_num = boost::lexical_cast< unsigned int >( dict["buffers"] );
// if (dict.count("buflen"))
// _buf_len = boost::lexical_cast< unsigned int >( dict["buflen"] );
if (0 == _buf_num)
_buf_num = BUF_NUM;
if (0 == _buf_len || _buf_len % 512 != 0) /* len must be multiple of 512 */
_buf_len = BUF_LEN;
_samp_avail = _buf_len / BYTES_PER_SAMPLE;
// create a lookup table for gr_complex values
for (unsigned int i = 0; i <= 0xffff; i++) {
#ifdef BOOST_LITTLE_ENDIAN
_lut.push_back( gr_complex( (float(i & 0xff) - 127.5f) * (1.0f/128.0f),
(float(i >> 8) - 127.5f) * (1.0f/128.0f) ) );
#else // BOOST_BIG_ENDIAN
_lut.push_back( gr_complex( (float(i >> 8) - 127.5f) * (1.0f/128.0f),
(float(i & 0xff) - 127.5f) * (1.0f/128.0f) ) );
#endif
}
{
boost::mutex::scoped_lock lock( _usage_mutex );
if ( _usage == 0 )
hackrf_init(); /* call only once before the first open */
_usage++;
}
_dev = NULL;
ret = hackrf_open( &_dev );
if (ret != HACKRF_SUCCESS)
throw std::runtime_error("Failed to open HackRF device.");
uint8_t board_id;
ret = hackrf_board_id_read( _dev, &board_id );
if (ret != HACKRF_SUCCESS)
throw std::runtime_error("Failed to get board id.");
char version[40];
memset(version, 0, sizeof(version));
ret = hackrf_version_string_read( _dev, version, sizeof(version));
if (ret != HACKRF_SUCCESS)
throw std::runtime_error("Failed to read version string.");
#if 0
read_partid_serialno_t serial_number;
ret = hackrf_board_partid_serialno_read( _dev, &serial_number );
if (ret != HACKRF_SUCCESS)
throw std::runtime_error("Failed to read serial number.");
#endif
std::cerr << "Using " << hackrf_board_id_name(hackrf_board_id(board_id)) << " "
<< "with firmware " << version << " "
<< std::endl;
if ( BUF_NUM != _buf_num || BUF_LEN != _buf_len ) {
std::cerr << "Using " << _buf_num << " buffers of size " << _buf_len << "."
<< std::endl;
}
ret = hackrf_sample_rate_set( _dev, 10000000 );
if (ret != HACKRF_SUCCESS)
throw std::runtime_error("Failed to set default samplerate.");
ret = hackrf_set_amp_enable( _dev, 0 );
if (ret != HACKRF_SUCCESS)
throw std::runtime_error("Failed to disable the RX amplifier.");
hackrf_max2837_read( _dev, 8, &val );
val |= 0x3; /* enable LNA & VGA control over SPI */
hackrf_max2837_write( _dev, 8, val );
set_if_gain( 24 ); /* preset to a reasonable default (non-GRC use case) */
set_gain( 16 ); /* preset to a reasonable default (non-GRC use case) */
_buf = (unsigned short **) malloc(_buf_num * sizeof(unsigned short *));
if (_buf) {
for(unsigned int i = 0; i < _buf_num; ++i)
_buf[i] = (unsigned short *) malloc(_buf_len);
}
ret = hackrf_start_rx( _dev, _hackrf_rx_callback, (void *)this );
if (ret != HACKRF_SUCCESS)
std::cerr << "Failed to start streaming (" << ret << ")" << std::endl;
while ( ! hackrf_is_streaming( _dev ) );
// _thread = gruel::thread(_hackrf_wait, this);
}
/*
* Our virtual destructor.
*/
hackrf_source_c::~hackrf_source_c ()
{
if (_dev) {
hackrf_stop_rx( _dev );
// _thread.join();
hackrf_close( _dev );
_dev = NULL;
{
boost::mutex::scoped_lock lock( _usage_mutex );
_usage--;
if ( _usage == 0 )
hackrf_exit(); /* call only once after last close */
}
}
if (_buf) {
for(unsigned int i = 0; i < _buf_num; ++i) {
if (_buf[i])
free(_buf[i]);
}
free(_buf);
_buf = NULL;
}
}
int hackrf_source_c::_hackrf_rx_callback(hackrf_transfer *transfer)
{
hackrf_source_c *obj = (hackrf_source_c *)transfer->rx_ctx;
return obj->hackrf_rx_callback(transfer->buffer, transfer->valid_length);
}
int hackrf_source_c::hackrf_rx_callback(unsigned char *buf, uint32_t len)
{
if (_skipped < BUF_SKIP) {
_skipped++;
return 0;
}
{
boost::mutex::scoped_lock lock( _buf_mutex );
int buf_tail = (_buf_head + _buf_used) % _buf_num;
memcpy(_buf[buf_tail], buf, len);
if (_buf_used == _buf_num) {
std::cerr << "O" << std::flush;
_buf_head = (_buf_head + 1) % _buf_num;
} else {
_buf_used++;
}
}
_buf_cond.notify_one();
return 0; // TODO: return -1 on error/stop
}
void hackrf_source_c::_hackrf_wait(hackrf_source_c *obj)
{
obj->hackrf_wait();
}
void hackrf_source_c::hackrf_wait()
{
}
int hackrf_source_c::work( int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items )
{
gr_complex *out = (gr_complex *)output_items[0];
bool running = false;
if ( _dev )
running = (bool) hackrf_is_streaming( _dev );
{
boost::mutex::scoped_lock lock( _buf_mutex );
while (_buf_used < 3 && running) // collect at least 3 buffers
_buf_cond.wait( lock );
}
if (!running)
return WORK_DONE;
unsigned short *buf = _buf[_buf_head] + _buf_offset;
if (noutput_items <= _samp_avail) {
for (int i = 0; i < noutput_items; ++i)
*out++ = _lut[ *(buf + i) ];
_buf_offset += noutput_items;
_samp_avail -= noutput_items;
} else {
for (int i = 0; i < _samp_avail; ++i)
*out++ = _lut[ *(buf + i) ];
{
boost::mutex::scoped_lock lock( _buf_mutex );
_buf_head = (_buf_head + 1) % _buf_num;
_buf_used--;
}
buf = _buf[_buf_head];
int remaining = noutput_items - _samp_avail;
for (int i = 0; i < remaining; ++i)
*out++ = _lut[ *(buf + i) ];
_buf_offset = remaining;
_samp_avail = (_buf_len / BYTES_PER_SAMPLE) - remaining;
}
return noutput_items;
}
std::vector<std::string> hackrf_source_c::get_devices()
{
std::vector<std::string> devices;
std::string label;
for (unsigned int i = 0; i < 1 /* TODO: missing libhackrf api */; i++) {
std::string args = "hackrf=" + boost::lexical_cast< std::string >( i );
label.clear();
label = "HackRF Jawbreaker"; /* TODO: missing libhackrf api */
boost::algorithm::trim(label);
args += ",label='" + label + "'";
devices.push_back( args );
}
return devices;
}
size_t hackrf_source_c::get_num_channels()
{
return 1;
}
osmosdr::meta_range_t hackrf_source_c::get_sample_rates()
{
osmosdr::meta_range_t range;
range += osmosdr::range_t( 5e6 );
range += osmosdr::range_t( 10e6 );
range += osmosdr::range_t( 12.5e6 );
range += osmosdr::range_t( 16e6 );
range += osmosdr::range_t( 20e6 ); /* confirmed to work on fast machines */
return range;
}
double hackrf_source_c::set_sample_rate(double rate)
{
int ret;
if (_dev) {
ret = hackrf_sample_rate_set( _dev, uint32_t(rate) );
if ( HACKRF_SUCCESS == ret ) {
_sample_rate = rate;
set_bandwidth( rate );
} else {
throw std::runtime_error( std::string( hackrf_error_name( (hackrf_error) ret ) ) );
}
}
return get_sample_rate();
}
double hackrf_source_c::get_sample_rate()
{
return _sample_rate;
}
osmosdr::freq_range_t hackrf_source_c::get_freq_range( size_t chan )
{
osmosdr::freq_range_t range;
range += osmosdr::range_t( 30e6, 6e9 );
return range;
}
double hackrf_source_c::set_center_freq( double freq, size_t chan )
{
int ret;
#define APPLY_PPM_CORR(val, ppm) ((val) * (1.0 + (ppm) * 0.000001))
if (_dev) {
double corr_freq = APPLY_PPM_CORR( freq, _freq_corr );
ret = hackrf_set_freq( _dev, uint64_t(corr_freq) );
if ( HACKRF_SUCCESS == ret ) {
_center_freq = freq;
} else {
throw std::runtime_error( std::string( hackrf_error_name( (hackrf_error) ret ) ) );
}
}
return get_center_freq( chan );
}
double hackrf_source_c::get_center_freq( size_t chan )
{
return _center_freq;
}
double hackrf_source_c::set_freq_corr( double ppm, size_t chan )
{
_freq_corr = ppm;
set_center_freq( _center_freq );
return get_freq_corr( chan );
}
double hackrf_source_c::get_freq_corr( size_t chan )
{
return _freq_corr;
}
std::vector<std::string> hackrf_source_c::get_gain_names( size_t chan )
{
std::vector< std::string > names;
names += "LNA";
names += "IF";
return names;
}
osmosdr::gain_range_t hackrf_source_c::get_gain_range( size_t chan )
{
osmosdr::gain_range_t range;
range += osmosdr::range_t( 0, 40, 8 );
return range;
}
osmosdr::gain_range_t hackrf_source_c::get_gain_range( const std::string & name, size_t chan )
{
if ( "IF" == name ) {
return osmosdr::gain_range_t( 0, 62, 2 );
}
return get_gain_range( chan );
}
bool hackrf_source_c::set_gain_mode( bool automatic, size_t chan )
{
_auto_gain = automatic;
return get_gain_mode(chan);
}
bool hackrf_source_c::get_gain_mode( size_t chan )
{
return _auto_gain;
}
double hackrf_source_c::set_gain( double gain, size_t chan )
{
osmosdr::gain_range_t rf_gains = get_gain_range( chan );
if (_dev) {
double clip_gain = rf_gains.clip( gain, true );
double rel_gain = fabs( rf_gains.stop() - clip_gain );
std::map<double, int> reg_vals;
reg_vals[ 0 ] = 0;
reg_vals[ 8 ] = 4;
reg_vals[ 16 ] = 2;
reg_vals[ 24 ] = 6;
reg_vals[ 32 ] = 3;
reg_vals[ 40 ] = 7;
if ( reg_vals.count( rel_gain ) ) {
int value = reg_vals[ rel_gain ];
// std::cerr << "lna gain: " << rel_gain << " value: " << value << std::endl;
uint16_t val;
hackrf_max2837_read( _dev, 1, &val );
val = (val & ~(7 << 2)) | ((value & 7) << 2);
if ( hackrf_max2837_write( _dev, 1, val ) == HACKRF_SUCCESS )
_gain = clip_gain;
}
}
return get_gain( chan );
}
double hackrf_source_c::set_gain( double gain, const std::string & name, size_t chan)
{
if ( "IF" == name ) {
return set_if_gain( gain, chan );
}
return set_gain( gain, chan );
}
double hackrf_source_c::get_gain( size_t chan )
{
return _gain;
}
double hackrf_source_c::get_gain( const std::string & name, size_t chan )
{
if ( "IF" == name ) {
return _if_gain;
}
return get_gain( chan );
}
double hackrf_source_c::set_if_gain(double gain, size_t chan)
{
osmosdr::gain_range_t if_gains = get_gain_range( "IF", chan );
if (_dev) {
double clip_gain = if_gains.clip( gain, true );
double rel_gain = fabs( if_gains.stop() - clip_gain );
std::map<double, int> reg_vals;
for ( int i = 0; i < 31; i++ )
reg_vals[ 2.0 * i ] = i;
if ( reg_vals.count( rel_gain ) ) {
int value = reg_vals[ rel_gain ];
// std::cerr << "vga gain: " << rel_gain << " value: " << value << std::endl;
uint16_t val;
hackrf_max2837_read( _dev, 5, &val );
val = (val & ~0x1f) | (value & 0x1f);
if ( hackrf_max2837_write( _dev, 5, val ) == HACKRF_SUCCESS )
_if_gain = clip_gain;
}
}
return gain;
}
std::vector< std::string > hackrf_source_c::get_antennas( size_t chan )
{
std::vector< std::string > antennas;
antennas += get_antenna( chan );
return antennas;
}
std::string hackrf_source_c::set_antenna( const std::string & antenna, size_t chan )
{
return get_antenna( chan );
}
std::string hackrf_source_c::get_antenna( size_t chan )
{
return "ANT";
}
void hackrf_source_c::set_bandwidth( double bandwidth, size_t chan )
{
int ret;
// osmosdr::meta_range_t bandwidths = get_bandwidth_range( chan );
if ( bandwidth == 0.0 )
return;
if ( _dev ) {
/* compute best default value depending on sample rate (auto filter) */
uint32_t bw = hackrf_compute_baseband_filter_bw( uint32_t(bandwidth) );
ret = hackrf_baseband_filter_bandwidth_set( _dev, bw );
if ( HACKRF_SUCCESS == ret ) {
_bandwidth = bw;
} else {
throw std::runtime_error( std::string( hackrf_error_name( (hackrf_error) ret ) ) );
}
}
}
double hackrf_source_c::get_bandwidth( size_t chan )
{
return _bandwidth;
}
osmosdr::meta_range_t hackrf_source_c::get_bandwidth_range( size_t chan )
{
osmosdr::meta_range_t bandwidths;
// TODO: read out from libhackrf when an API is available
bandwidths += osmosdr::range_t( 1750000 );
bandwidths += osmosdr::range_t( 2500000 );
bandwidths += osmosdr::range_t( 3500000 );
bandwidths += osmosdr::range_t( 5000000 );
bandwidths += osmosdr::range_t( 5500000 );
bandwidths += osmosdr::range_t( 6000000 );
bandwidths += osmosdr::range_t( 7000000 );
bandwidths += osmosdr::range_t( 8000000 );
bandwidths += osmosdr::range_t( 9000000 );
bandwidths += osmosdr::range_t( 10000000 );
bandwidths += osmosdr::range_t( 12000000 );
bandwidths += osmosdr::range_t( 14000000 );
bandwidths += osmosdr::range_t( 15000000 );
bandwidths += osmosdr::range_t( 20000000 );
bandwidths += osmosdr::range_t( 24000000 );
bandwidths += osmosdr::range_t( 28000000 );
return bandwidths;
}

@ -0,0 +1,154 @@
/* -*- c++ -*- */
/*
* Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
*
* This file is part of GNU Radio
*
* GNU Radio 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, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef INCLUDED_HACKRF_SOURCE_C_H
#define INCLUDED_HACKRF_SOURCE_C_H
#include <gruel/thread.h>
#include <gnuradio/gr_sync_block.h>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <libhackrf/hackrf.h>
#include "osmosdr_src_iface.h"
class hackrf_source_c;
/*
* We use boost::shared_ptr's instead of raw pointers for all access
* to gr_blocks (and many other data structures). The shared_ptr gets
* us transparent reference counting, which greatly simplifies storage
* management issues. This is especially helpful in our hybrid
* C++ / Python system.
*
* See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
*
* As a convention, the _sptr suffix indicates a boost::shared_ptr
*/
typedef boost::shared_ptr<hackrf_source_c> hackrf_source_c_sptr;
/*!
* \brief Return a shared_ptr to a new instance of hackrf_source_c.
*
* To avoid accidental use of raw pointers, hackrf_source_c's
* constructor is private. make_hackrf_source_c is the public
* interface for creating new instances.
*/
hackrf_source_c_sptr make_hackrf_source_c (const std::string & args = "");
/*!
* \brief Provides a stream of complex samples.
* \ingroup block
*/
class hackrf_source_c :
public gr_sync_block,
public osmosdr_src_iface
{
private:
// The friend declaration allows make_hackrf_source_c to
// access the private constructor.
friend hackrf_source_c_sptr make_hackrf_source_c (const std::string & args);
/*!
* \brief Provides a stream of complex samples.
*/
hackrf_source_c (const std::string & args); // private constructor
public:
~hackrf_source_c (); // public destructor
int work( int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items );
static std::vector< std::string > get_devices();
size_t get_num_channels( void );
osmosdr::meta_range_t get_sample_rates( void );
double set_sample_rate( double rate );
double get_sample_rate( void );
osmosdr::freq_range_t get_freq_range( size_t chan = 0 );
double set_center_freq( double freq, size_t chan = 0 );
double get_center_freq( size_t chan = 0 );
double set_freq_corr( double ppm, size_t chan = 0 );
double get_freq_corr( size_t chan = 0 );
std::vector<std::string> get_gain_names( size_t chan = 0 );
osmosdr::gain_range_t get_gain_range( size_t chan = 0 );
osmosdr::gain_range_t get_gain_range( const std::string & name, size_t chan = 0 );
bool set_gain_mode( bool automatic, size_t chan = 0 );
bool get_gain_mode( size_t chan = 0 );
double set_gain( double gain, size_t chan = 0 );
double set_gain( double gain, const std::string & name, size_t chan = 0 );
double get_gain( size_t chan = 0 );
double get_gain( const std::string & name, size_t chan = 0 );
double set_if_gain( double gain, size_t chan = 0 );
std::vector< std::string > get_antennas( size_t chan = 0 );
std::string set_antenna( const std::string & antenna, size_t chan = 0 );
std::string get_antenna( size_t chan = 0 );
void set_bandwidth( double bandwidth, size_t chan = 0 );
double get_bandwidth( size_t chan = 0 );
osmosdr::meta_range_t get_bandwidth_range( size_t chan = 0 );
private:
static int _hackrf_rx_callback(hackrf_transfer* transfer);
int hackrf_rx_callback(unsigned char *buf, uint32_t len);
static void _hackrf_wait(hackrf_source_c *obj);
void hackrf_wait();
static int _usage;
static boost::mutex _usage_mutex;
std::vector<gr_complex> _lut;
hackrf_device *_dev;
gruel::thread _thread;
unsigned short **_buf;
unsigned int _buf_num;
unsigned int _buf_len;
unsigned int _buf_head;
unsigned int _buf_used;
boost::mutex _buf_mutex;
boost::condition_variable _buf_cond;
bool _running;
unsigned int _buf_offset;
int _samp_avail;
double _sample_rate;
double _center_freq;
double _freq_corr;
bool _auto_gain;
double _gain;
double _if_gain;
unsigned int _skipped;
double _bandwidth;
};
#endif /* INCLUDED_HACKRF_SOURCE_C_H */

@ -29,15 +29,7 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/*
#define ENABLE_OSMOSDR
#define ENABLE_FCD
#define ENABLE_FILE
#define ENABLE_RTL
#define ENABLE_RTL_TCP
#define ENABLE_UHD
#define ENABLE_MIRI
*/
#ifdef ENABLE_OSMOSDR
#include <osmosdr_src_c.h>
#endif
@ -66,6 +58,10 @@
#include <miri_source_c.h>
#endif
#ifdef ENABLE_HACKRF
#include <hackrf_source_c.h>
#endif
#include "osmosdr_arg_helpers.h"
using namespace osmosdr;
@ -145,6 +141,10 @@ devices_t device::find(const device_t &hint)
BOOST_FOREACH( std::string dev, miri_source_c::get_devices() )
devices.push_back( device_t(dev) );
#endif
#ifdef ENABLE_HACKRF
BOOST_FOREACH( std::string dev, hackrf_source_c::get_devices() )
devices.push_back( device_t(dev) );
#endif
return devices;
}

@ -62,5 +62,5 @@ osmosdr_sink_c_impl::osmosdr_sink_c_impl (const std::string &args)
gr_make_io_signature (MIN_IN, MAX_IN, sizeof (gr_complex)),
gr_make_io_signature (MIN_OUT, MAX_OUT, sizeof (gr_complex)))
{
//connect(_src, 0, self(), 0);
}

@ -61,6 +61,10 @@
#include <miri_source_c.h>
#endif
#ifdef ENABLE_HACKRF
#include <hackrf_source_c.h>
#endif
#include <osmosdr_arg_helpers.h>
/* This avoids throws in ctor of gr_hier_block2, as gnuradio is unable to deal
@ -113,6 +117,9 @@ osmosdr_source_c_impl::osmosdr_source_c_impl (const std::string &args)
#ifdef ENABLE_MIRI
dev_types.push_back("miri");
#endif
#ifdef ENABLE_HACKRF
dev_types.push_back("hackrf");
#endif
std::cerr << "gr-osmosdr "
<< GR_OSMOSDR_VERSION " (" GR_OSMOSDR_LIBVER ") "
@ -155,6 +162,10 @@ osmosdr_source_c_impl::osmosdr_source_c_impl (const std::string &args)
BOOST_FOREACH( std::string dev, miri_source_c::get_devices() )
dev_list.push_back( dev );
#endif
#ifdef ENABLE_HACKRF
BOOST_FOREACH( std::string dev, hackrf_source_c::get_devices() )
dev_list.push_back( dev );
#endif
// std::cerr << std::endl;
// BOOST_FOREACH( std::string dev, dev_list )
// std::cerr << "'" << dev << "'" << std::endl;
@ -226,6 +237,13 @@ osmosdr_source_c_impl::osmosdr_source_c_impl (const std::string &args)
}
#endif
#ifdef ENABLE_HACKRF
if ( dict.count("hackrf") ) {
hackrf_source_c_sptr src = make_hackrf_source_c( arg );
block = src; iface = src.get();
}
#endif
if ( iface != NULL && long(block.get()) != 0 ) {
_devs.push_back( iface );

Loading…
Cancel
Save