diff options
| author | Yuval Adam <_@yuv.al> | 2021-04-23 16:25:17 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2021-04-23 16:25:17 +0300 |
| commit | ee2b91fb66486013802c9880bafdcb498af25919 (patch) | |
| tree | cab67722d44af0fc482aee70b55fd8c23061eeaa | |
| parent | 75e330a0bdac57523550ac9fe1aa7ea8aa0e5def (diff) | |
Reorg handle and tuner references
| -rw-r--r-- | src/lib.rs | 37 | ||||
| -rw-r--r-- | src/tuners/r820t.rs | 149 | ||||
| -rw-r--r-- | src/usb.rs | 34 |
3 files changed, 123 insertions, 97 deletions
@@ -16,17 +16,17 @@ const KNOWN_DEVICES: [(u16, u16, &str); 2] = [ ]; pub struct RtlSdr { - handle: RtlSdrDeviceHandle, + // handle: RtlSdrDeviceHandle, } impl RtlSdr { pub fn new() -> RtlSdr { pretty_env_logger::init(); - let handle = Self::open_device().unwrap(); - RtlSdr { handle: handle } + let handle = Self::open_device(); + RtlSdr {} } - pub fn open_device() -> Option<RtlSdrDeviceHandle> { + pub fn open_device() { for dev in rusb::devices().unwrap().iter() { let desc = dev.device_descriptor().unwrap(); let vid = desc.vendor_id(); @@ -50,29 +50,26 @@ impl RtlSdr { handle.set_i2c_repeater(true); handle.init_tuner(); - - return Some(handle); } } } - return None; } - pub fn set_sample_rate(&self, samp_rate: u32) { - self.handle.set_sample_rate(samp_rate); - } + // pub fn set_sample_rate(&self, samp_rate: u32) { + // self.handle.set_sample_rate(samp_rate); + // } - pub fn set_test_mode(&self, on: bool) { - let val = match on { - true => 0x03, - false => 0x05, - }; - self.handle.demod_write_reg(0, 0x19, val, 1); - } + // pub fn set_test_mode(&self, on: bool) { + // let val = match on { + // true => 0x03, + // false => 0x05, + // }; + // self.handle.demod_write_reg(0, 0x19, val, 1); + // } - pub fn reset_buffer(&self) { - self.handle.reset_buffer(); - } + // pub fn reset_buffer(&self) { + // self.handle.reset_buffer(); + // } pub fn read_sync(&self, len: usize) -> Vec<u8> { return Vec::with_capacity(len); diff --git a/src/tuners/r820t.rs b/src/tuners/r820t.rs index eb9bea0..e39bcfd 100644 --- a/src/tuners/r820t.rs +++ b/src/tuners/r820t.rs @@ -1,4 +1,5 @@ -use super::{Tuner, TunerInfo}; +use super::TunerInfo; +use log::info; use usb::RtlSdrDeviceHandle; pub const TUNER_ID: &str = "r820t"; @@ -55,19 +56,20 @@ pub struct Config { } pub struct R820T { + pub handle: RtlSdrDeviceHandle, pub device: TunerInfo, regs: [u8; NUM_REGS], buf: [u8; NUM_REGS + 1], - xtal_cal_sel: XtalCapValue, - pll: u16, // kHz - int_freq: u32, - fil_cal_code: u8, - input: u8, - has_lock: i16, // bool? - init_done: i16, // bool? - delsys: u32, - tuner_type: TunerType, - bw: u32, // MHz + // xtal_cal_sel: XtalCapValue, + // pll: u16, // kHz + // int_freq: u32, + // fil_cal_code: u8, + // input: u8, + // has_lock: i16, // bool? + // init_done: i16, // bool? + // delsys: u32, + // tuner_type: TunerType, + // bw: u32, // MHz } pub const TUNER_INFO: TunerInfo = TunerInfo { @@ -121,26 +123,42 @@ const XTAL_CAPS: [(u8, XtalCapValue); 5] = [ ]; impl R820T { - pub fn new(handle: &RtlSdrDeviceHandle) -> R820T { + pub fn new(handle: RtlSdrDeviceHandle) -> R820T { let tuner = R820T { + handle: handle, device: TUNER_INFO, - regs: [u8; NUM_REGS], - buf: [u8; NUM_REGS + 1], - xtal_cal_sel: XtalCapValue::XtalHighCap0P, - pll: u16, // kHz - int_freq: u32, - fil_cal_code: u8, - input: u8, - has_lock: i16, // bool? - init_done: i16, // bool? - delsys: u32, - tuner_type: TunerType, - bw: u32, // MHz + regs: [0; NUM_REGS], + buf: [0; NUM_REGS + 1], + // xtal_cal_sel: XtalCapValue::XtalHighCap0P, + // pll: u16, // kHz + // int_freq: u32, + // fil_cal_code: u8, + // input: u8, + // has_lock: i16, // bool? + // init_done: i16, // bool? + // delsys: u32, + // tuner_type: TunerType, + // bw: u32, // MHz }; - tuner.init(handle); + tuner.init(); tuner } + fn init(&self) { + // disable Zero-IF mode + self.handle.demod_write_reg(1, 0xb1, 0x1a, 1); + + // only enable In-phase ADC input + self.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(IF_FREQ); + + // enable spectrum inversion + self.handle.demod_write_reg(1, 0x15, 0x01, 1); + } + fn shadow_store(&self, reg: u8, val: u8, len: usize) { let r = reg as usize - REG_SHADOW_START; if r < 0 { @@ -148,7 +166,9 @@ impl R820T { } } - fn write(&self, reg: u8, val: u8) -> u8 {} + fn write(&self, reg: u8, val: u8) -> u8 { + 1 + } fn write_reg() {} @@ -176,6 +196,11 @@ impl R820T { fn read(&self, reg: u8, val: u8, len: usize) { let p: u8 = self.buf[1]; self.buf[0] = reg; + // self.handle.i2c_write(self.device.i2c_addr, &p); + } + + pub fn exit(&self) { + info!("Tuner r820t exiting..."); } } @@ -185,50 +210,50 @@ impl Drop for R820T { } } -impl Tuner for R820T { - fn init(&self, handle: &RtlSdrDeviceHandle) { - // disable Zero-IF mode - handle.demod_write_reg(1, 0xb1, 0x1a, 1); +// impl Tuner for R820T { +// fn init(&self, handle: &RtlSdrDeviceHandle) { +// // disable Zero-IF mode +// handle.demod_write_reg(1, 0xb1, 0x1a, 1); - // only enable In-phase ADC input - handle.demod_write_reg(0, 0x08, 0x4d, 1); +// // only enable In-phase ADC input +// 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 - handle.set_if_freq(IF_FREQ); +// // the R82XX use 3.57 MHz IF for the DVB-T 6 MHz mode, and +// // 4.57 MHz for the 8 MHz mode +// handle.set_if_freq(IF_FREQ); - // enable spectrum inversion - handle.demod_write_reg(1, 0x15, 0x01, 1); - } +// // enable spectrum inversion +// handle.demod_write_reg(1, 0x15, 0x01, 1); +// } - fn exit(&self) {} +// fn exit(&self) {} - fn set_freq(&self, _freq: u32) { - unimplemented!() - } +// fn set_freq(&self, _freq: u32) { +// unimplemented!() +// } - fn set_bandwidth(&self, bw: u16, rate: u32, handle: &RtlSdrDeviceHandle) { - let mut rc: u16; - let mut real_bw: u16 = 0; - let mut reg_0a: u8; - let mut reg_0b: u8; +// fn set_bandwidth(&self, bw: u16, rate: u32, handle: &RtlSdrDeviceHandle) { +// let mut rc: u16; +// let mut real_bw: u16 = 0; +// let mut reg_0a: u8; +// let mut reg_0b: u8; - if bw > 7000000 {} - } +// if bw > 7000000 {} +// } - fn set_gain(&self, _gain: u32) { - unimplemented!() - } +// fn set_gain(&self, _gain: u32) { +// unimplemented!() +// } - fn set_if_gain(&self, _if_gain: u32) { - unimplemented!() - } +// fn set_if_gain(&self, _if_gain: u32) { +// unimplemented!() +// } - fn set_gain_mode(&self, _mode: bool) { - unimplemented!() - } +// fn set_gain_mode(&self, _mode: bool) { +// unimplemented!() +// } - fn display(&self) -> &str { - self.device.name - } -} +// fn display(&self) -> &str { +// self.device.name +// } +// } @@ -40,7 +40,7 @@ const CTRL_TIMEOUT: Duration = Duration::from_millis(300); pub struct RtlSdrDeviceHandle { handle: DeviceHandle<GlobalContext>, - tuner: Option<Box<dyn Tuner>>, + // tuner: Option<r820t::R820T>, // when we go abstract: Option<Box<dyn Tuner>>, iface_id: u8, kernel_driver_active: bool, } @@ -51,7 +51,7 @@ impl RtlSdrDeviceHandle { pub fn new(handle: DeviceHandle<GlobalContext>, iface_id: u8) -> RtlSdrDeviceHandle { let mut handle = RtlSdrDeviceHandle { handle, - tuner: None, + // tuner: None, iface_id, kernel_driver_active: false, }; @@ -60,7 +60,7 @@ impl RtlSdrDeviceHandle { handle } - pub fn init_tuner(&mut self) { + pub fn init_tuner(self) { let tuner_id: &str = match self.search_tuner() { Some(tid) => { trace!("Found tuner ID {}", tid); @@ -69,18 +69,18 @@ impl RtlSdrDeviceHandle { None => "", }; - let tuner: Option<Box<dyn Tuner>> = match tuner_id { - r820t::TUNER_ID => Some(Box::new(r820t::R820T::new(&self))), - fc0013::TUNER_ID => Some(Box::new(fc0013::FC0013::new(&self))), + let tuner: Option<r820t::R820T> = match tuner_id { + r820t::TUNER_ID => Some(r820t::R820T::new(self)), + // fc0013::TUNER_ID => Some(Box::new(fc0013::FC0013::new(&self))), _ => { error!("Could not find any valid tuner."); None } }; - self.tuner = tuner; + // self.tuner = tuner; - info!("Found tuner {}", self.tuner.as_ref().unwrap().display()); + info!("Found tuner r820t"); } /// Probe all known tuners at their I2C addresses @@ -199,6 +199,10 @@ impl RtlSdrDeviceHandle { .unwrap() } + pub fn i2c_read(&self, i2c_addr: u8, buf: &mut [u8]) -> usize { + self.read_array(BLOCK_IICB, i2c_addr as u16, buf) + } + pub fn write_array(&self, block: u8, addr: u16, arr: &[u8]) -> Result<usize, rusb::Error> { let type_vendor_out = rusb::request_type(Direction::Out, RequestType::Vendor, Recipient::Device); @@ -221,7 +225,7 @@ impl RtlSdrDeviceHandle { } } - pub fn i2c_write(&self, i2c_addr: u8, buf: &[u8], len: usize) { + pub fn i2c_write(&self, i2c_addr: u8, buf: &[u8]) { self.write_array(BLOCK_IICB, i2c_addr as u16, buf); } @@ -356,9 +360,9 @@ impl RtlSdrDeviceHandle { } } -impl Drop for RtlSdrDeviceHandle { - fn drop(&mut self) { - self.deinit_baseband(); - self.attach_kernel_driver(); - } -} +// impl Drop for RtlSdrDeviceHandle { +// fn drop(self) { +// self.deinit_baseband(); +// self.attach_kernel_driver(); +// } +// } |
