diff options
| -rw-r--r-- | src/lib.rs | 10 | ||||
| -rw-r--r-- | src/tuners/fc0013.rs | 25 | ||||
| -rw-r--r-- | src/tuners/mod.rs | 5 | ||||
| -rw-r--r-- | src/tuners/r820t.rs | 33 | ||||
| -rw-r--r-- | src/usb.rs | 34 | ||||
| -rw-r--r-- | src/utils/rustl_test.rs | 4 |
6 files changed, 77 insertions, 34 deletions
@@ -22,6 +22,7 @@ pub struct RtlSdr { // ctx: rusb::Context, // device: UsbDevice, iface_id: u8, + tuner: Option<Box<dyn Tuner>>, } impl RtlSdr { @@ -30,6 +31,7 @@ impl RtlSdr { RtlSdr { // ctx: rusb::Context::new().unwrap(), iface_id: INTERFACE_ID, + tuner: None, } } @@ -69,16 +71,16 @@ impl RtlSdr { } }; - let tuner: Box<dyn Tuner> = match tuner_id { - r820t::TUNER_ID => Box::new(r820t::R820T::new(&handle)), - fc0013::TUNER_ID => Box::new(fc0013::FC0013::new(&handle)), + self.tuner = match tuner_id { + r820t::TUNER_ID => Some(Box::new(r820t::R820T::new(&handle))), + fc0013::TUNER_ID => Some(Box::new(fc0013::FC0013::new(&handle))), _ => { error!("Could not find any valid tuner, aborting."); return; } }; - info!("Found tuner {}", tuner.display()); + // info!("Found tuner {}", self.tuner.unwrap().display()); } } } diff --git a/src/tuners/fc0013.rs b/src/tuners/fc0013.rs index fed1289..14e7970 100644 --- a/src/tuners/fc0013.rs +++ b/src/tuners/fc0013.rs @@ -1,9 +1,9 @@ use super::{Tuner, TunerInfo}; use usb::RtlSdrDeviceHandle; -pub struct FC0013<'a> { +pub struct FC0013 { pub device: TunerInfo, - pub handle: &'a RtlSdrDeviceHandle, + // pub handle: &'a RtlSdrDeviceHandle, } pub const TUNER_ID: &str = "fc0013"; @@ -14,22 +14,23 @@ pub const TUNER_INFO: TunerInfo = TunerInfo { i2c_addr: 0xc6, check_addr: 0x00, check_val: 0xa3, + // gains: vec![ + // -99, -73, -65, -63, -60, -58, -54, 58, 61, 63, 65, 67, 68, 70, 71, 179, 181, 182, 184, 186, + // 188, 191, 197, + // ], }; -impl<'a> FC0013<'a> { - pub fn new(handle: &'a RtlSdrDeviceHandle) -> FC0013<'a> { - let tuner = FC0013 { - device: TUNER_INFO, - handle: handle, - }; - tuner.init(); +impl FC0013 { + pub fn new(handle: &RtlSdrDeviceHandle) -> FC0013 { + let tuner = FC0013 { device: TUNER_INFO }; + tuner.init(handle); tuner } } -impl<'a> Tuner for FC0013<'a> { - fn init(&self) { - println!("Init {}", self.device.name); +impl Tuner for FC0013 { + fn init(&self, _handle: &RtlSdrDeviceHandle) { + unimplemented!() } fn exit(&self) { diff --git a/src/tuners/mod.rs b/src/tuners/mod.rs index 0739fad..ae64384 100644 --- a/src/tuners/mod.rs +++ b/src/tuners/mod.rs @@ -1,3 +1,5 @@ +use super::RtlSdrDeviceHandle; + pub mod fc0013; pub mod r820t; @@ -7,10 +9,11 @@ pub struct TunerInfo { pub i2c_addr: u8, pub check_addr: u8, pub check_val: u8, + // pub gains: Vec<i8>, } pub trait Tuner { - fn init(&self); + fn init(&self, handle: &RtlSdrDeviceHandle); fn exit(&self); fn set_freq(&self, freq: u32); fn set_bw(&self, bw: u32); diff --git a/src/tuners/r820t.rs b/src/tuners/r820t.rs index 9844b41..14d54e8 100644 --- a/src/tuners/r820t.rs +++ b/src/tuners/r820t.rs @@ -3,9 +3,9 @@ use usb::RtlSdrDeviceHandle; const R82XX_IF_FREQ: u32 = 3570000; -pub struct R820T<'a> { +pub struct R820T { pub device: TunerInfo, - pub handle: &'a RtlSdrDeviceHandle, + // pub handle: &'a RtlSdrDeviceHandle, } pub const TUNER_ID: &str = "r820t"; @@ -16,39 +16,40 @@ pub const TUNER_INFO: TunerInfo = TunerInfo { i2c_addr: 0x34, check_addr: 0x00, check_val: 0x69, + // gains: vec![ + // 0, 9, 14, 27, 37, 77, 87, 125, 144, 157, 166, 197, 207, 229, 254, 280, 297, 328, 338, 364, + // 372, 386, 402, 421, 434, 439, 445, 480, 496, + // ], }; -impl<'a> R820T<'a> { - pub fn new(handle: &'a RtlSdrDeviceHandle) -> R820T<'a> { - let tuner = R820T { - device: TUNER_INFO, - handle: handle, - }; - tuner.init(); +impl R820T { + pub fn new(handle: &RtlSdrDeviceHandle) -> R820T { + let tuner = R820T { device: TUNER_INFO }; + tuner.init(handle); tuner } } -impl<'a> Drop for R820T<'a> { +impl Drop for R820T { fn drop(&mut self) { self.exit(); } } -impl<'a> Tuner for R820T<'a> { - fn init(&self) { +impl Tuner for R820T { + fn init(&self, handle: &RtlSdrDeviceHandle) { // disable Zero-IF mode - self.handle.demod_write_reg(1, 0xb1, 0x1a, 1); + handle.demod_write_reg(1, 0xb1, 0x1a, 1); // only enable In-phase ADC input - self.handle.demod_write_reg(0, 0x08, 0x4d, 1); + handle.demod_write_reg(0, 0x08, 0x4d, 1); // the R82XX use 3.57 MHz IF for the DVB-T 6 MHz mode, and // 4.57 MHz for the 8 MHz mode - self.handle.set_if_freq(R82XX_IF_FREQ); + handle.set_if_freq(R82XX_IF_FREQ); // enable spectrum inversion - self.handle.demod_write_reg(1, 0x15, 0x01, 1); + handle.demod_write_reg(1, 0x15, 0x01, 1); } fn exit(&self) {} @@ -1,5 +1,8 @@ +extern crate log; +extern crate pretty_env_logger; extern crate rusb; +use log::{error, info}; use rusb::{DeviceHandle, Direction, GlobalContext, Recipient, RequestType}; use std::time::Duration; @@ -43,6 +46,7 @@ pub struct RtlSdrDeviceHandle { /// various rtl-sdr specific methods impl RtlSdrDeviceHandle { pub fn new(handle: DeviceHandle<GlobalContext>, iface_id: u8) -> RtlSdrDeviceHandle { + pretty_env_logger::init(); let mut handle = RtlSdrDeviceHandle { handle, iface_id, @@ -106,7 +110,7 @@ impl RtlSdrDeviceHandle { .handle .write_control(type_vendor_in, 0, addr, index, &data, CTRL_TIMEOUT); let reg: u16 = ((data[1] as u16) << 8) | (data[0] as u16); - return reg; + reg } pub fn demod_write_reg(&self, page: u8, addr: u16, val: u16, len: u8) -> u16 { @@ -246,6 +250,34 @@ impl RtlSdrDeviceHandle { self.demod_write_reg(1, 0x1b, tmp, 1); } + pub fn set_sample_rate(&self, samp_rate: u32) { + let tmp: u16; + let real_rsamp_ratio: u32; + + // check if the rate is supported by the resampler + if (samp_rate <= 225000) + || (samp_rate > 3200000) + || ((samp_rate > 300000) && (samp_rate <= 900000)) + { + error!("Invalid sample rate: {} Hz", samp_rate); + } + + let mut rsamp_ratio: u32 = (DEF_RTL_XTAL_FREQ * (2 ^ 22)) / samp_rate; + rsamp_ratio &= 0x0ffffffc; + + real_rsamp_ratio = rsamp_ratio | ((rsamp_ratio & 0x08000000) << 1); + let real_rate: f64 = ((DEF_RTL_XTAL_FREQ * (2 ^ 22)) / real_rsamp_ratio).into(); + info!("Exact sample rate is: {} Hz", real_rate); + + self.set_i2c_repeater(true); + self.set_i2c_repeater(false); + } + + pub fn reset_buffer(&self) { + self.write_reg(BLOCK_USBB, ADDR_USB_EPA_CTL, 0x1002, 2); + self.write_reg(BLOCK_USBB, ADDR_USB_EPA_CTL, 0x0000, 2); + } + pub fn deinit_baseband(&self) { // power off demod and ADCs self.write_reg(BLOCK_SYSB, ADDR_SYS_DEMOD_CTL, 0x20, 1); diff --git a/src/utils/rustl_test.rs b/src/utils/rustl_test.rs index 669cc2a..27dc5c2 100644 --- a/src/utils/rustl_test.rs +++ b/src/utils/rustl_test.rs @@ -1,5 +1,9 @@ +const DEFAULT_SAMPLE_RATE: u32 = 2048000; +const DEFAULT_BUF_LENGTH: usize = 16 * 16384; + pub fn main() { println!("Starting rustl_test"); + let buffer: [u8; DEFAULT_BUF_LENGTH]; let mut r = rustl_sdr::RtlSdr::new(); r.open(); } |
