From c377706e0e029e1eb0d8f005f0b13883fa5ef06f Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 12 Mar 2020 00:44:15 +0100 Subject: [PATCH] ad7172: move setup from main --- src/ad7172/adc.rs | 20 ++++++++++++-------- src/main.rs | 20 +++++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/ad7172/adc.rs b/src/ad7172/adc.rs index e3289e7..2bea65c 100644 --- a/src/ad7172/adc.rs +++ b/src/ad7172/adc.rs @@ -19,22 +19,26 @@ pub struct Adc, NSS: OutputPin> { } impl, NSS: OutputPin, E: fmt::Debug> Adc { - pub fn new(spi: SPI, mut nss: NSS) -> Result { + pub fn new(spi: SPI, mut nss: NSS) -> Result> { let _ = nss.set_high(); let mut adc = Adc { spi, nss, checksum_mode: ChecksumMode::Off, }; adc.reset()?; + adc.set_checksum_mode(ChecksumMode::Crc).unwrap(); - match adc.identify() { - Err(e) => - warn!("Cannot identify ADC: {:?}", e), - Ok(id) if id & 0xFFF0 == 0x00D0 => - info!("ADC id: {:04X}", id), - Ok(id) => - info!("ADC id: {:04X} (corrupt)", id), + let mut retries = 0; + let mut adc_id; + loop { + adc_id = adc.identify()?; + if adc_id & 0xFFF0 == 0x00D0 { + break; + } else { + retries += 1; + } } + info!("ADC id: {:04X} ({} retries)", adc_id, retries); Ok(adc) } diff --git a/src/main.rs b/src/main.rs index 01a182d..6b9124f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,11 +92,8 @@ fn main() -> ! { let pins = Pins::setup(clocks, dp.GPIOA, dp.GPIOB, dp.GPIOC, dp.GPIOG, dp.SPI2); - info!("ADC init"); let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap(); - adc.set_checksum_mode(ad7172::ChecksumMode::Crc).unwrap(); - info!("Timer setup"); timer::setup(cp.SYST, clocks); #[cfg(not(feature = "generate-hwaddr"))] @@ -128,15 +125,20 @@ fn main() -> ! { last_output = now; } + if let Some(channel) = adc.data_ready().unwrap() { + let data = adc.read_data().unwrap(); + info!("ADC {}: {:08X}", channel, data); + } + // Update watchdog wd.feed(); - cortex_m::interrupt::free(|cs| { - if !net::is_pending(cs) { - // Wait for interrupts - wfi(); - } - }); + // cortex_m::interrupt::free(|cs| { + // if !net::is_pending(cs) { + // // Wait for interrupts + // wfi(); + // } + // }); } }); });