[PATCH] gr-osmosdr: add set_bandwidth and independent gains via extended_mode

Luigi Tarenga luigi.tarenga at gmail.com
Wed Dec 28 11:52:51 UTC 2016


Hello list,
the following patch rely on librtlsdr fork on github.  I would like to 
receive feedback on what
is the best option to backport the commit:
https://github.com/librtlsdr/librtlsdr/commit/25d0e8e6737b93b3564ad04d0fd2cd5e91cefa8b

or alternatively put a preprocessor switch in this patch in order to 
enable build against 2
different rtlsdr driver.

regards
Luigi


--- a/lib/rtl/rtl_source_c.cc
+++ b/lib/rtl/rtl_source_c.cc
@@ -88,7 +88,11 @@ rtl_source_c::rtl_source_c (const std::string &args)
      _no_tuner(false),
      _auto_gain(false),
      _if_gain(0),
-    _skipped(0)
+    _lna_gain(0),
+    _mix_gain(0),
+    _vga_gain(0),
+    _skipped(0),
+    _extended_mode(0)
  {
    int ret;
    int index;
@@ -150,6 +154,9 @@ rtl_source_c::rtl_source_c (const std::string &args)
    if (dict.count("offset_tune"))
      offset_tune = boost::lexical_cast< unsigned int >( 
dict["offset_tune"] );

+  if (dict.count("extended_mode"))
+    _extended_mode = boost::lexical_cast< bool >( dict["extended_mode"] );
+
    _buf_num = _buf_len = _buf_head = _buf_used = _buf_offset = 0;

    if (dict.count("buffers"))
@@ -528,6 +535,13 @@ std::vector<std::string> 
rtl_source_c::get_gain_names( size_t chan )
      if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_E4000 ) {
        names += "IF";
      }
+
+    if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_R820T ) {
+      if ( _extended_mode ) {
+        names += "MIX";
+        names += "IF";
+      }
+    }
    }

    return names;
@@ -553,14 +567,31 @@ osmosdr::gain_range_t 
rtl_source_c::get_gain_range( size_t chan )

  osmosdr::gain_range_t rtl_source_c::get_gain_range( const std::string 
& name, size_t chan )
  {
-  if ( "IF" == name ) {
-    if ( _dev ) {
-      if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_E4000 ) {
-        return osmosdr::gain_range_t(3, 56, 1);
-      } else {
+
+  if ( _dev ) {
+    if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_R820T ) {
+      if ( _extended_mode ) {
+        if ( "LNA" == name ) {
+          return osmosdr::gain_range_t( 0, 15, 1 );
+        }
+
+        if ( "MIX" == name ) {
+          return osmosdr::gain_range_t( 0, 15, 1 );
+        }
+
+        if ( "IF" == name ) {
+          return osmosdr::gain_range_t( 0, 15, 1 );
+        }
+
          return osmosdr::gain_range_t();
        }
      }
+
+    if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_E4000 ) {
+      if ( "IF" == name ) {
+        return osmosdr::gain_range_t(3, 56, 1);
+      }
+    }
    }

    return get_gain_range( chan );
@@ -584,21 +615,86 @@ bool rtl_source_c::get_gain_mode( size_t chan )
    return _auto_gain;
  }

+double rtl_source_c::set_bandwidth( double bandwidth, size_t chan )
+{
+  if (_dev) {
+    return (double)rtlsdr_set_tuner_bandwidth( _dev, (uint32_t)bandwidth);
+  }
+
+  return 0;
+}
+
  double rtl_source_c::set_gain( double gain, size_t chan )
  {
    osmosdr::gain_range_t rf_gains = rtl_source_c::get_gain_range( chan );

-  if (_dev) {
+  if ( _dev ) {
      rtlsdr_set_tuner_gain( _dev, int(rf_gains.clip(gain) * 10.0) );
    }

    return get_gain( chan );
  }

+double rtl_source_c::set_lna_gain( double gain, size_t chan )
+{
+  osmosdr::gain_range_t gains = rtl_source_c::get_gain_range( "LNA", 
chan );
+
+  if ( _dev ) {
+    _lna_gain = gains.clip( gain );
+    return rtlsdr_set_tuner_gain_ext( _dev, int(_lna_gain), 
int(_mix_gain), int(_vga_gain) );
+  }
+
+  return _lna_gain;
+}
+
+double rtl_source_c::set_mix_gain( double gain, size_t chan )
+{
+  osmosdr::gain_range_t gains = rtl_source_c::get_gain_range( "MIX", 
chan );
+
+  if ( _dev ) {
+    _mix_gain = gains.clip( gain );
+    return rtlsdr_set_tuner_gain_ext( _dev, int(_lna_gain), 
int(_mix_gain), int(_vga_gain) );
+  }
+
+  return _mix_gain;
+}
+
+double rtl_source_c::set_vga_gain( double gain, size_t chan )
+{
+  osmosdr::gain_range_t gains = rtl_source_c::get_gain_range( "IF", chan );
+
+  if ( _dev ) {
+    _vga_gain = gains.clip( gain );
+    return rtlsdr_set_tuner_gain_ext( _dev, int(_lna_gain), 
int(_mix_gain), int(_vga_gain) );
+  }
+
+  return _vga_gain;
+}
+
  double rtl_source_c::set_gain( double gain, const std::string & name, 
size_t chan)
  {
-  if ( "IF" == name ) {
-    return set_if_gain( gain, chan );
+  if ( _dev ) {
+    if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_R820T ) {
+      if ( _extended_mode ) {
+        if ( "LNA" == name ) {
+          return set_lna_gain( gain, chan );
+        }
+
+        if ( "MIX" == name ) {
+          return set_mix_gain( gain, chan );
+        }
+
+        if ( "IF" == name ) {
+          return set_vga_gain( gain, chan );
+        }
+      }
+    }
+
+    if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_E4000 ) {
+      if ( "IF" == name ) {
+        return set_if_gain( gain, chan );
+      }
+    }
    }

    return set_gain( gain, chan );
@@ -614,6 +710,20 @@ double rtl_source_c::get_gain( size_t chan )

  double rtl_source_c::get_gain( const std::string & name, size_t chan )
  {
+  if ( _extended_mode ) {
+    if ( "LNA" == name ) {
+      return _lna_gain;
+    }
+
+    if ( "MIX" == name ) {
+      return _mix_gain;
+    }
+
+    if ( "IF" == name ) {
+      return _vga_gain;
+    }
+  }
+
    if ( "IF" == name ) {
      return _if_gain;
    }
diff --git a/lib/rtl/rtl_source_c.h b/lib/rtl/rtl_source_c.h
index 76de400..bb20e5c 100644
--- a/lib/rtl/rtl_source_c.h
+++ b/lib/rtl/rtl_source_c.h
@@ -101,12 +101,16 @@ public:
    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_bandwidth( double bandwidth, 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 );
+  double set_lna_gain( double gain, size_t chan = 0 );
+  double set_mix_gain( double gain, size_t chan = 0 );
+  double set_vga_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 );
@@ -141,7 +145,12 @@ private:
    bool _no_tuner;
    bool _auto_gain;
    double _if_gain;
+  double _lna_gain;
+  double _mix_gain;
+  double _vga_gain;
    unsigned int _skipped;
+
+  bool _extended_mode;
  };

  #endif /* INCLUDED_RTLSDR_SOURCE_C_H */



More information about the osmocom-sdr mailing list