From da65ea21a45505cc5094f469f54ec78c6ce16f55 Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 21 Mar 2020 00:10:12 +0100 Subject: [PATCH] ad7172: eliminate superfluous AdcError --- src/ad7172/adc.rs | 28 ++++++++++++++-------------- src/ad7172/mod.rs | 13 ------------- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/ad7172/adc.rs b/src/ad7172/adc.rs index b17d007..890bcc4 100644 --- a/src/ad7172/adc.rs +++ b/src/ad7172/adc.rs @@ -5,7 +5,7 @@ use log::{info, warn}; use super::{ regs::{self, Register, RegisterData}, checksum::{ChecksumMode, Checksum}, - AdcError, Mode, Input, RefSource, PostFilter, DigitalFilterOrder, + Mode, Input, RefSource, PostFilter, DigitalFilterOrder, }; /// AD7172-2 implementation @@ -18,7 +18,7 @@ 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, @@ -48,12 +48,12 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc } /// `0x00DX` for AD7172-2 - pub fn identify(&mut self) -> Result> { + pub fn identify(&mut self) -> Result { self.read_reg(®s::Id) .map(|id| id.id()) } - pub fn set_checksum_mode(&mut self, mode: ChecksumMode) -> Result<(), AdcError> { + pub fn set_checksum_mode(&mut self, mode: ChecksumMode) -> Result<(), SPI::Error> { // Cannot use update_reg() here because checksum_mode is // updated between read_reg() and write_reg(). let mut ifmode = self.read_reg(®s::IfMode)?; @@ -63,7 +63,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc Ok(()) } - pub fn set_sync_enable(&mut self, enable: bool) -> Result<(), AdcError> { + pub fn set_sync_enable(&mut self, enable: bool) -> Result<(), SPI::Error> { self.update_reg(®s::GpioCon, |data| { data.set_sync_en(enable); }) @@ -71,7 +71,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc pub fn setup_channel( &mut self, index: u8, in_pos: Input, in_neg: Input - ) -> Result<(), AdcError> { + ) -> Result<(), SPI::Error> { self.update_reg(®s::SetupCon { index }, |data| { data.set_bipolar(false); data.set_refbuf_pos(true); @@ -95,7 +95,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc } /// Calibrates offset registers - pub fn calibrate_offset(&mut self) -> Result<(), AdcError> { + pub fn calibrate_offset(&mut self) -> Result<(), SPI::Error> { self.update_reg(®s::AdcMode, |adc_mode| { adc_mode.set_mode(Mode::SystemOffsetCalibration); })?; @@ -108,7 +108,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc Ok(()) } - pub fn get_postfilter(&mut self, index: u8) -> Result, AdcError> { + pub fn get_postfilter(&mut self, index: u8) -> Result, SPI::Error> { self.read_reg(®s::FiltCon { index }) .map(|data| { if data.enh_filt_en() { @@ -119,7 +119,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc }) } - pub fn set_postfilter(&mut self, index: u8, filter: Option) -> Result<(), AdcError> { + pub fn set_postfilter(&mut self, index: u8, filter: Option) -> Result<(), SPI::Error> { self.update_reg(®s::FiltCon { index }, |data| { match filter { None => data.set_enh_filt_en(false), @@ -132,7 +132,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc } /// Returns the channel the data is from - pub fn data_ready(&mut self) -> Result, AdcError> { + pub fn data_ready(&mut self) -> Result, SPI::Error> { self.read_reg(®s::Status) .map(|status| { if status.ready() { @@ -144,12 +144,12 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc } /// Get data - pub fn read_data(&mut self) -> Result> { + pub fn read_data(&mut self) -> Result { self.read_reg(®s::Data) .map(|data| data.data()) } - fn read_reg(&mut self, reg: &R) -> Result> { + fn read_reg(&mut self, reg: &R) -> Result { let mut reg_data = R::Data::empty(); let address = 0x40 | reg.address(); let mut checksum = Checksum::new(self.checksum_mode); @@ -170,7 +170,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc Ok(reg_data) } - fn write_reg(&mut self, reg: &R, reg_data: &mut R::Data) -> Result<(), AdcError> { + fn write_reg(&mut self, reg: &R, reg_data: &mut R::Data) -> Result<(), SPI::Error> { loop { let address = reg.address(); let mut checksum = Checksum::new(match self.checksum_mode { @@ -194,7 +194,7 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc } } - fn update_reg(&mut self, reg: &R, f: F) -> Result> + fn update_reg(&mut self, reg: &R, f: F) -> Result where R: regs::Register, F: FnOnce(&mut R::Data) -> A, diff --git a/src/ad7172/mod.rs b/src/ad7172/mod.rs index 53f4c11..6e6921d 100644 --- a/src/ad7172/mod.rs +++ b/src/ad7172/mod.rs @@ -21,19 +21,6 @@ pub const SPI_CLOCK: MegaHertz = MegaHertz(2); pub const MAX_VALUE: u32 = 0xFF_FFFF; -#[derive(Clone, Debug, PartialEq)] -pub enum AdcError { - SPI(SPI), - ChecksumMismatch(Option, Option), -} - -impl From for AdcError { - fn from(e: SPI) -> Self { - AdcError::SPI(e) - } -} - - #[derive(Clone, Copy, Debug)] #[repr(u8)]