diff --git a/rp2040-hal/src/adc.rs b/rp2040-hal/src/adc.rs index 30c9fd457..1e0a6a3e5 100644 --- a/rp2040-hal/src/adc.rs +++ b/rp2040-hal/src/adc.rs @@ -155,10 +155,11 @@ use crate::{ dma, gpio::{ bank0::{Gpio26, Gpio27, Gpio28, Gpio29}, - AnyPin, DynPinId, Function, OutputEnableOverride, Pin, PullType, ValidFunction, DynBankId, + AnyPin, DynBankId, DynPinId, Function, OutputEnableOverride, Pin, PullType, ValidFunction, }, pac::{dma::ch::ch_ctrl_trig::TREQ_SEL_A, ADC, RESETS}, resets::SubsystemReset, + typelevel::Sealed, }; const TEMPERATURE_SENSOR_CHANNEL: u8 = 4; @@ -190,13 +191,11 @@ where let (od, ie) = (p.get_output_disable(), p.get_input_enable()); p.set_output_enable_override(OutputEnableOverride::Disable); p.set_input_enable(false); - Ok( - Self { + Ok(Self { pin: P::from(p), saved_output_disable: od, saved_input_enable: ie, - } - ) + }) } else { Err(InvalidPinError) } @@ -218,19 +217,23 @@ where } } -/// Trait for GPIO pins (concrete or dynamic) that can be -/// used as ADC channels. Also implemented for [`TempSense`]. -pub trait AdcChannel { +/// Trait for entities that can be used as ADC channels. +/// +/// This is implemented by [`AdcPin`] and by [`TempSense`]. +/// The trait is sealed and can't be implemented in other crates. +pub trait AdcChannel: Sealed { /// Get the channel id used to configure the ADC peripheral. fn channel(&self) -> u8; } +impl Sealed for AdcPin

{} impl AdcChannel for AdcPin

{ fn channel(&self) -> u8 { self.channel() } } +impl Sealed for TempSense {} impl AdcChannel for TempSense { fn channel(&self) -> u8 { TEMPERATURE_SENSOR_CHANNEL @@ -370,19 +373,15 @@ impl Adc { } /// Enable free-running mode by setting the start_many flag. - pub fn free_running(&mut self, pin: &mut AdcPin) - { + pub fn free_running(&mut self, pin: &mut AdcPin) { self.device .cs - .modify(|_, w| w.ainsel().variant(pin.channel()).start_many().set_bit() ); + .modify(|_, w| w.ainsel().variant(pin.channel()).start_many().set_bit()); } /// Disable free-running mode by unsetting the start_many flag. - pub fn stop(&mut self) - { - self.device - .cs - .modify(|_, w| w.start_many().clear_bit() ); + pub fn stop(&mut self) { + self.device.cs.modify(|_, w| w.start_many().clear_bit()); } fn inner_read(&mut self, chan: u8) -> u16 { @@ -883,7 +882,12 @@ where D: AdcChannel, { fn from(pins: (&A, &B, &C, &D)) -> Self { - Self(1 << pins.0.channel() | 1 << pins.1.channel() | 1 << pins.2.channel() | 1 << pins.3.channel()) + Self( + 1 << pins.0.channel() + | 1 << pins.1.channel() + | 1 << pins.2.channel() + | 1 << pins.3.channel(), + ) } }