diff --git a/src/lib.rs b/src/lib.rs index 903e423..d2d177d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,7 @@ //! You can find an example application [here](https://egit.irs.uni-stuttgart.de/rust/vorago-reb1/src/branch/main/examples/max11619-adc.rs) //! using a [thin abstraction layer](https://egit.irs.uni-stuttgart.de/rust/vorago-reb1/src/branch/main/src/max11619.rs) #![no_std] +use core::convert::Infallible; use core::{marker::PhantomData, slice::IterMut}; use embedded_hal::delay::DelayNs; use embedded_hal::digital::{InputPin, OutputPin}; @@ -226,21 +227,20 @@ struct InternalCfg { // ADC implementation //================================================================================================== -pub struct Max116xx10Bit { +pub struct Max116xx10Bit { spi: Spi, - cs: Cs, cfg: InternalCfg, clocked: PhantomData, delay: PhantomData, } -pub struct Max116xx10BitEocExt { - base: Max116xx10Bit, +pub struct Max116xx10BitEocExt { + base: Max116xx10Bit, eoc: EOC, } -pub struct Max116xx10BitCnvstEocExt { - base: Max116xx10Bit, +pub struct Max116xx10BitCnvstEocExt { + base: Max116xx10Bit, eoc: EOC, cnvst: CNVST, } @@ -249,24 +249,24 @@ pub struct Max116xx10BitCnvstEocExt Max116xx10Bit { - pub fn max11618(spi: Spi, cs: Cs) -> Result> { - Self::new::(spi, cs) +impl Max116xx10Bit { + pub fn max11618(spi: Spi) -> Result> { + Self::new::(spi) } - pub fn max11619(spi: Spi, cs: Cs) -> Result> { - Self::new::(spi, cs) + pub fn max11619(spi: Spi) -> Result> { + Self::new::(spi) } - pub fn max11620(spi: Spi, cs: Cs) -> Result> { - Self::new::(spi, cs) + pub fn max11620(spi: Spi) -> Result> { + Self::new::(spi) } - pub fn max11621(spi: Spi, cs: Cs) -> Result> { - Self::new::(spi, cs) + pub fn max11621(spi: Spi) -> Result> { + Self::new::(spi) } - pub fn max11624(spi: Spi, cs: Cs) -> Result> { - Self::new::(spi, cs) + pub fn max11624(spi: Spi) -> Result> { + Self::new::(spi) } - pub fn max11625(spi: Spi, cs: Cs) -> Result> { - Self::new::(spi, cs) + pub fn max11625(spi: Spi) -> Result> { + Self::new::(spi) } /// Create a new generic MAX116xx instance. By default the generated ADC struct is configured @@ -276,10 +276,9 @@ impl Max116xx10Bit(spi: Spi, cs: Cs) -> Result> { + pub fn new(spi: Spi) -> Result> { let max_dev = Max116xx10Bit { spi, - cs, cfg: InternalCfg { clk_mode: ExtClkd::CLK_SEL, ref_mode: VoltageRefMode::ExternalSingleEndedNoWakeupDelay, @@ -300,10 +299,9 @@ impl Max116xx10Bit Max116xx10Bit { + ) -> Max116xx10Bit { Max116xx10Bit { spi: self.spi, - cs: self.cs, cfg: InternalCfg { clk_mode: ExtClkd::CLK_SEL, ref_mode: VoltageRefMode::InternalRefWithWakeupDelay, @@ -323,10 +321,9 @@ impl Max116xx10Bit Max116xx10Bit { + ) -> Max116xx10Bit { Max116xx10Bit { spi: self.spi, - cs: self.cs, cfg: InternalCfg { clk_mode: ExtClkd::CLK_SEL, ref_mode: VoltageRefMode::InternalRefWithoutWakeupDelay, @@ -347,11 +344,10 @@ impl Max116xx10Bit( self, eoc: Eoc, - ) -> Max116xx10BitEocExt { + ) -> Max116xx10BitEocExt { Max116xx10BitEocExt { base: Max116xx10Bit { spi: self.spi, - cs: self.cs, cfg: InternalCfg { clk_mode: IntClkdIntTmdSerIF::CLK_SEL, ref_mode: VoltageRefMode::InternalRefWithWakeupDelay, @@ -377,14 +373,13 @@ impl Max116xx10Bit Result, AdcError> { + ) -> Result, AdcError> { if v_ref == VoltageRefMode::InternalRefWithWakeupDelay { return Err(AdcError::InvalidRefMode); } Ok(Max116xx10BitEocExt { base: Max116xx10Bit { spi: self.spi, - cs: self.cs, cfg: InternalCfg { clk_mode: IntClkdIntTmdSerIF::CLK_SEL, ref_mode: VoltageRefMode::InternalRefWithWakeupDelay, @@ -401,20 +396,16 @@ impl Max116xx10Bit - Max116xx10Bit -{ +impl Max116xx10Bit { #[inline] - fn send_wrapper(&mut self, byte: u8) -> Result<(), Error> { - self.cs.set_low().map_err(Error::Pin)?; + fn send_wrapper(&mut self, byte: u8) -> Result<(), Error> { self.spi.write(&[byte]).map_err(Error::Spi)?; - self.cs.set_high().map_err(Error::Pin)?; Ok(()) } /// Set up the ADC depending on clock and reference configuration #[inline] - pub fn setup(&mut self) -> Result<(), Error> { + pub fn setup(&mut self) -> Result<(), Error> { self.send_wrapper(self.get_setup_byte()) } @@ -425,13 +416,13 @@ impl &mut self, avg_conv: AveragingConversions, avg_res: AveragingResults, - ) -> Result<(), Error> { + ) -> Result<(), Error> { self.cfg.results_len = Self::get_results_len(avg_res); self.send_wrapper(Self::get_averaging_byte(avg_conv, avg_res)) } #[inline] - pub fn reset(&mut self, fifo_only: bool) -> Result<(), Error> { + pub fn reset(&mut self, fifo_only: bool) -> Result<(), Error> { let mut reset_byte = 0b0001_0000; if fifo_only { reset_byte |= 1 << 3; @@ -468,10 +459,10 @@ impl /// Generic function which can be used a single result is available /// when EOC is low - fn internal_read_single_channel>( + fn internal_read_single_channel( &mut self, eoc: &mut Eoc, - ) -> nb::Result> { + ) -> nb::Result> { if self.cfg.pending_scan_mode.is_none() { return Err(nb::Error::Other(Error::Adc(AdcError::NoPendingOperation))); } else if self.cfg.pending_scan_mode != Some(ScanMode::ConvertChannelNOnce) { @@ -479,9 +470,7 @@ impl } if eoc.is_low().map_err(Error::Pin)? { let mut reply_buf: [u8; 2] = [0; 2]; - self.cs.set_low().map_err(Error::Pin)?; let transfer_result = self.spi.read(&mut reply_buf); - self.cs.set_high().map_err(Error::Pin)?; match transfer_result { Ok(_) => { self.cfg.pending_scan_mode = None; @@ -499,7 +488,7 @@ macro_rules! ext_impl { () => { /// Set up the ADC depending on clock and reference configuration #[inline] - pub fn setup(&mut self) -> Result<(), Error> { + pub fn setup(&mut self) -> Result<(), Error> { self.base.send_wrapper(self.base.get_setup_byte()) } @@ -510,16 +499,16 @@ macro_rules! ext_impl { &mut self, avg_conv: AveragingConversions, avg_res: AveragingResults, - ) -> Result<(), Error> { - self.base.cfg.results_len = Max116xx10Bit::::get_results_len(avg_res); + ) -> Result<(), Error> { + self.base.cfg.results_len = Max116xx10Bit::::get_results_len(avg_res); self.base - .send_wrapper(Max116xx10Bit::::get_averaging_byte( + .send_wrapper(Max116xx10Bit::::get_averaging_byte( avg_conv, avg_res, )) } #[inline] - pub fn reset(&mut self, fifo_only: bool) -> Result<(), Error> { + pub fn reset(&mut self, fifo_only: bool) -> Result<(), Error> { let mut reset_byte = 0b0001_0000; if fifo_only { reset_byte |= 1 << 3; @@ -528,14 +517,12 @@ macro_rules! ext_impl { } }; } -impl - Max116xx10BitEocExt -{ +impl Max116xx10BitEocExt { ext_impl!(); } -impl - Max116xx10BitCnvstEocExt +impl + Max116xx10BitCnvstEocExt { ext_impl!(); } @@ -545,21 +532,19 @@ impl //================================================================================================== /// Implementations when using the external SPI clock to time the conversions -impl Max116xx10Bit { +impl Max116xx10Bit { pub fn read_single_channel( &mut self, buf: &mut [u8], channel_num: u8, - ) -> Result> { + ) -> Result> { if buf.len() < 3 { return Err(Error::Adc(AdcError::CmdBufTooSmall)); } buf[0] = self.get_conversion_byte(ScanMode::ConvertChannelNOnce, channel_num)?; buf[1] = 0x00; buf[2] = 0x00; - self.cs.set_low().map_err(Error::Pin)?; self.spi.transfer_in_place(&mut buf[0..3]).ok().unwrap(); - self.cs.set_high().map_err(Error::Pin)?; Ok(((buf[1] as u16) << 6) | (buf[2] as u16 >> 2)) } @@ -568,7 +553,7 @@ impl Max116xx10Bit, n: u8, - ) -> Result<(), Error> { + ) -> Result<(), Error> { let mut iter = buf.iter_mut(); let mut next_byte: &mut u8; for idx in 0..n + 1 { @@ -579,11 +564,9 @@ impl Max116xx10Bit Max116xx10Bit, n: u8, - ) -> Result<(), Error> { + ) -> Result<(), Error> { let mut iter = buf.iter_mut(); let mut next_byte: &mut u8; if n > self.cfg.max_channels - 1 { @@ -617,11 +600,9 @@ impl Max116xx10Bit Max116xx10Bit Max116xx10Bit { +impl Max116xx10Bit { pub fn read_single_channel( &mut self, buf: &mut [u8], channel_num: u8, delay: &mut Delay, - ) -> Result> { + ) -> Result> { if buf.len() < 3 { return Err(Error::Adc(AdcError::CmdBufTooSmall)); } @@ -655,11 +636,9 @@ impl Max116xx10Bit> 2)) } @@ -669,7 +648,7 @@ impl Max116xx10Bit, n: u8, delay: &mut Delay, - ) -> Result<(), Error> { + ) -> Result<(), Error> { let mut iter = buf.iter_mut(); let mut next_byte: &mut u8; for idx in 0..n + 1 { @@ -684,11 +663,9 @@ impl Max116xx10Bit Max116xx10Bit, n: u8, delay: &mut Delay, - ) -> Result<(), Error> { + ) -> Result<(), Error> { let mut iter = buf.iter_mut(); let mut next_byte: &mut u8; if n > self.cfg.max_channels - 1 { @@ -725,11 +702,9 @@ impl Max116xx10Bit Max116xx10Bit, Eoc: InputPin, PinE> - Max116xx10BitEocExt -{ +impl Max116xx10BitEocExt { #[inline] fn request_wrapper( &mut self, channel_num: u8, scan_mode: ScanMode, - ) -> Result<(), Error> { + ) -> Result<(), Error> { if self.base.cfg.pending_scan_mode.is_some() { return Err(Error::Adc(AdcError::PendingOperation)); } @@ -764,7 +737,7 @@ impl, Eoc: InputPin, P .base .get_conversion_byte(scan_mode, channel_num) .map_err(Error::Adc)?; - self.base.send_wrapper(conv_byte)?; + self.base.send_wrapper(conv_byte).ok(); self.base.cfg.pending_scan_mode = Some(scan_mode); Ok(()) } @@ -772,7 +745,7 @@ impl, Eoc: InputPin, P pub fn request_single_channel( &mut self, channel_num: u8, - ) -> Result<(), Error> { + ) -> Result<(), Error> { self.request_wrapper(channel_num, ScanMode::ConvertChannelNOnce) } @@ -782,14 +755,14 @@ impl, Eoc: InputPin, P pub fn request_channel_n_repeatedly( &mut self, channel_num: u8, - ) -> Result<(), Error> { + ) -> Result<(), Error> { self.request_wrapper(channel_num, ScanMode::ScanChannelNRepeatedly) } pub fn request_multiple_channels_0_to_n( &mut self, n: u8, - ) -> Result<(), Error> { + ) -> Result<(), Error> { self.base.cfg.requested_conversions = n as usize + 1; self.request_wrapper(n, ScanMode::Scan0ToChannelN) } @@ -797,7 +770,7 @@ impl, Eoc: InputPin, P pub fn request_multiple_channels_n_to_highest( &mut self, n: u8, - ) -> Result<(), Error> { + ) -> Result<(), Error> { self.base.cfg.requested_conversions = self.base.cfg.max_channels as usize + 1 - n as usize; self.request_wrapper(n, ScanMode::ScanChannelNToHighest) } @@ -806,7 +779,7 @@ impl, Eoc: InputPin, P /// needs to be passed explicitely here. /// If no request was made, [AdcError::NoPendingOperation] is returned. /// If a request was made for multipel results, [AdcError::PendingOperation] will be returned. - pub fn get_single_channel(&mut self) -> nb::Result> { + pub fn get_single_channel(&mut self) -> nb::Result> { self.base.internal_read_single_channel(&mut self.eoc) } @@ -816,7 +789,7 @@ impl, Eoc: InputPin, P pub fn get_multi_channel( &mut self, result_iter: &mut IterMut, - ) -> nb::Result<(), Error> { + ) -> nb::Result<(), Error> { if self.base.cfg.pending_scan_mode.is_none() { return Err(nb::Error::Other(Error::Adc(AdcError::NoPendingOperation))); } else if self.base.cfg.pending_scan_mode == Some(ScanMode::ConvertChannelNOnce) { @@ -833,12 +806,10 @@ impl, Eoc: InputPin, P }; self.base.cfg.pending_scan_mode = None; self.base.cfg.requested_conversions = 0; - self.base.cs.set_low().map_err(Error::Pin)?; self.base .spi .read(&mut reply_buf[0..(num_conv * 2)]) .map_err(Error::Spi)?; - self.base.cs.set_high().map_err(Error::Pin)?; let mut reply_iter = reply_buf.iter(); for _ in 0..num_conv { let next_res = result_iter @@ -863,8 +834,8 @@ impl, Eoc: InputPin, P /// /// TODO: Implement. Unfortunately, the test board used to verify this library did not have /// the CNVST connected, so I wouldn't be able to test an implementation easily. -impl - Max116xx10BitCnvstEocExt +impl + Max116xx10BitCnvstEocExt { } @@ -873,13 +844,8 @@ impl /// /// TODO: Test. Unfortunately, the test board used to verify this library did not have /// the CNVST connected, so I wouldn't be able to test an implementation easily. -impl< - Spi: SpiDevice, - Cs: OutputPin, - Eoc: InputPin, - Cnvst: OutputPin, - PinE, - > Max116xx10BitCnvstEocExt +impl, Cnvst: OutputPin, PinE> + Max116xx10BitCnvstEocExt { /// The pulse needs to be at least 40ns. A pulse cycle value can be used to increase /// the width of the pulse @@ -905,7 +871,7 @@ impl< .base .get_conversion_byte(scan_mode, channel_num) .map_err(Error::Adc)?; - self.base.send_wrapper(conv_byte)?; + self.base.send_wrapper(conv_byte).ok(); self.cnvst.set_low().map_err(Error::Pin)?; for _ in 0..pulse_cycles {} self.cnvst.set_high().map_err(Error::Pin)?;