Merge pull request #184 from JurajSadel/refactor/adc

ADC: Add compile time check for using unconfigured PIN, add AdcPin wr…
This commit is contained in:
Björn Quentin 2022-09-13 13:31:10 +02:00 committed by GitHub
commit cfcadc882b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 103 additions and 41 deletions

View File

@ -16,7 +16,6 @@ pub enum Resolution {
Resolution12Bit = 0b11, Resolution12Bit = 0b11,
} }
/// The attenuation of the ADC pin
#[derive(PartialEq, Eq, Clone, Copy)] #[derive(PartialEq, Eq, Clone, Copy)]
pub enum Attenuation { pub enum Attenuation {
Attenuation0dB = 0b00, Attenuation0dB = 0b00,
@ -25,6 +24,19 @@ pub enum Attenuation {
Attenuation11dB = 0b11, Attenuation11dB = 0b11,
} }
pub struct AdcPin<PIN, ADCI> {
pub pin: PIN,
_phantom: PhantomData<ADCI>,
}
impl<PIN: Channel<ADCI, ID = u8>, ADCI> Channel<ADCI> for AdcPin<PIN, ADCI> {
type ID = u8;
fn channel() -> Self::ID {
PIN::channel()
}
}
pub struct AdcConfig<ADCI> { pub struct AdcConfig<ADCI> {
pub resolution: Resolution, pub resolution: Resolution,
pub attenuations: [Option<Attenuation>; 10], pub attenuations: [Option<Attenuation>; 10],
@ -41,10 +53,15 @@ where
pub fn enable_pin<PIN: Channel<ADCI, ID = u8>>( pub fn enable_pin<PIN: Channel<ADCI, ID = u8>>(
&mut self, &mut self,
_pin: &PIN, pin: PIN,
attenuation: Attenuation, attenuation: Attenuation,
) { ) -> AdcPin<PIN, ADCI> {
self.attenuations[PIN::channel() as usize] = Some(attenuation); self.attenuations[PIN::channel() as usize] = Some(attenuation);
AdcPin {
pin,
_phantom: PhantomData::default(),
}
} }
} }
@ -330,7 +347,7 @@ impl<ADC1> ADC<ADC1> {
} }
} }
impl<ADCI, WORD, PIN> OneShot<ADCI, WORD, PIN> for ADC<ADCI> impl<ADCI, WORD, PIN> OneShot<ADCI, WORD, AdcPin<PIN, ADCI>> for ADC<ADCI>
where where
WORD: From<u16>, WORD: From<u16>,
PIN: Channel<ADCI, ID = u8>, PIN: Channel<ADCI, ID = u8>,
@ -338,23 +355,26 @@ where
{ {
type Error = (); type Error = ();
fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> { fn read(&mut self, _pin: &mut AdcPin<PIN, ADCI>) -> nb::Result<WORD, Self::Error> {
if self.attenuations[PIN::channel() as usize] == None { if self.attenuations[AdcPin::<PIN, ADCI>::channel() as usize] == None {
panic!("Channel {} is not configured reading!", PIN::channel()); panic!(
"Channel {} is not configured reading!",
AdcPin::<PIN, ADCI>::channel()
);
} }
if let Some(active_channel) = self.active_channel { if let Some(active_channel) = self.active_channel {
// There is conversion in progress: // There is conversion in progress:
// - if it's for a different channel try again later // - if it's for a different channel try again later
// - if it's for the given channel, go ahaid and check progress // - if it's for the given channel, go ahead and check progress
if active_channel != PIN::channel() { if active_channel != AdcPin::<PIN, ADCI>::channel() {
return Err(nb::Error::WouldBlock); return Err(nb::Error::WouldBlock);
} }
} else { } else {
// If no conversions are in progress, start a new one for given channel // If no conversions are in progress, start a new one for given channel
self.active_channel = Some(PIN::channel()); self.active_channel = Some(AdcPin::<PIN, ADCI>::channel());
ADCI::set_en_pad(PIN::channel() as u8); ADCI::set_en_pad(AdcPin::<PIN, ADCI>::channel() as u8);
ADCI::clear_start_sar(); ADCI::clear_start_sar();
ADCI::set_start_sar(); ADCI::set_start_sar();

View File

@ -23,6 +23,18 @@ pub enum Attenuation {
Attenuation11dB = 0b11, Attenuation11dB = 0b11,
} }
pub struct AdcPin<PIN, ADCI> {
pub pin: PIN,
_phantom: PhantomData<ADCI>,
}
impl<PIN: Channel<ADCI, ID = u8>, ADCI> Channel<ADCI> for AdcPin<PIN, ADCI> {
type ID = u8;
fn channel() -> Self::ID {
PIN::channel()
}
}
pub struct AdcConfig<ADCI> { pub struct AdcConfig<ADCI> {
pub resolution: Resolution, pub resolution: Resolution,
pub attenuations: [Option<Attenuation>; 5], pub attenuations: [Option<Attenuation>; 5],
@ -39,10 +51,15 @@ where
pub fn enable_pin<PIN: Channel<ADCI, ID = u8>>( pub fn enable_pin<PIN: Channel<ADCI, ID = u8>>(
&mut self, &mut self,
_pin: &PIN, pin: PIN,
attenuation: Attenuation, attenuation: Attenuation,
) { ) -> AdcPin<PIN, ADCI> {
self.attenuations[PIN::channel() as usize] = Some(attenuation); self.attenuations[PIN::channel() as usize] = Some(attenuation);
AdcPin {
pin,
_phantom: PhantomData::default(),
}
} }
} }
@ -187,7 +204,7 @@ where
} }
} }
impl<ADCI, WORD, PIN> OneShot<ADCI, WORD, PIN> for ADC<ADCI> impl<ADCI, WORD, PIN> OneShot<ADCI, WORD, AdcPin<PIN, ADCI>> for ADC<ADCI>
where where
WORD: From<u16>, WORD: From<u16>,
PIN: Channel<ADCI, ID = u8>, PIN: Channel<ADCI, ID = u8>,
@ -195,21 +212,24 @@ where
{ {
type Error = (); type Error = ();
fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> { fn read(&mut self, _pin: &mut AdcPin<PIN, ADCI>) -> nb::Result<WORD, Self::Error> {
if self.attenuations[PIN::channel() as usize] == None { if self.attenuations[AdcPin::<PIN, ADCI>::channel() as usize] == None {
panic!("Channel {} is not configured reading!", PIN::channel()); panic!(
"Channel {} is not configured reading!",
AdcPin::<PIN, ADCI>::channel()
);
} }
if let Some(active_channel) = self.active_channel { if let Some(active_channel) = self.active_channel {
// There is conversion in progress: // There is conversion in progress:
// - if it's for a different channel try again later // - if it's for a different channel try again later
// - if it's for the given channel, go ahaid and check progress // - if it's for the given channel, go ahead and check progress
if active_channel != PIN::channel() { if active_channel != AdcPin::<PIN, ADCI>::channel() {
return Err(nb::Error::WouldBlock); return Err(nb::Error::WouldBlock);
} }
} else { } else {
// If no conversions are in progress, start a new one for given channel // If no conversions are in progress, start a new one for given channel
self.active_channel = Some(PIN::channel()); self.active_channel = Some(AdcPin::<PIN, ADCI>::channel());
let channel = self.active_channel.unwrap(); let channel = self.active_channel.unwrap();
let attenuation = self.attenuations[channel as usize].unwrap() as u8; let attenuation = self.attenuations[channel as usize].unwrap() as u8;

View File

@ -22,6 +22,19 @@ pub enum Attenuation {
Attenuation11dB = 0b11, Attenuation11dB = 0b11,
} }
pub struct AdcPin<PIN, ADCI> {
pub pin: PIN,
_phantom: PhantomData<ADCI>,
}
impl<PIN: Channel<ADCI, ID = u8>, ADCI> Channel<ADCI> for AdcPin<PIN, ADCI> {
type ID = u8;
fn channel() -> Self::ID {
PIN::channel()
}
}
pub struct AdcConfig<ADCI> { pub struct AdcConfig<ADCI> {
pub resolution: Resolution, pub resolution: Resolution,
pub attenuations: [Option<Attenuation>; 10], pub attenuations: [Option<Attenuation>; 10],
@ -38,10 +51,15 @@ where
pub fn enable_pin<PIN: Channel<ADCI, ID = u8>>( pub fn enable_pin<PIN: Channel<ADCI, ID = u8>>(
&mut self, &mut self,
_pin: &PIN, pin: PIN,
attenuation: Attenuation, attenuation: Attenuation,
) { ) -> AdcPin<PIN, ADCI> {
self.attenuations[PIN::channel() as usize] = Some(attenuation); self.attenuations[PIN::channel() as usize] = Some(attenuation);
AdcPin {
pin,
_phantom: PhantomData::default(),
}
} }
} }
@ -314,7 +332,7 @@ where
} }
} }
impl<ADCI, WORD, PIN> OneShot<ADCI, WORD, PIN> for ADC<ADCI> impl<ADCI, WORD, PIN> OneShot<ADCI, WORD, AdcPin<PIN, ADCI>> for ADC<ADCI>
where where
WORD: From<u16>, WORD: From<u16>,
PIN: Channel<ADCI, ID = u8>, PIN: Channel<ADCI, ID = u8>,
@ -322,23 +340,26 @@ where
{ {
type Error = (); type Error = ();
fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> { fn read(&mut self, _pin: &mut AdcPin<PIN, ADCI>) -> nb::Result<WORD, Self::Error> {
if self.attenuations[PIN::channel() as usize] == None { if self.attenuations[AdcPin::<PIN, ADCI>::channel() as usize] == None {
panic!("Channel {} is not configured reading!", PIN::channel()); panic!(
"Channel {} is not configured reading!",
AdcPin::<PIN, ADCI>::channel()
);
} }
if let Some(active_channel) = self.active_channel { if let Some(active_channel) = self.active_channel {
// There is conversion in progress: // There is conversion in progress:
// - if it's for a different channel try again later // - if it's for a different channel try again later
// - if it's for the given channel, go ahaid and check progress // - if it's for the given channel, go ahead and check progress
if active_channel != PIN::channel() { if active_channel != AdcPin::<PIN, ADCI>::channel() {
return Err(nb::Error::WouldBlock); return Err(nb::Error::WouldBlock);
} }
} else { } else {
// If no conversions are in progress, start a new one for given channel // If no conversions are in progress, start a new one for given channel
self.active_channel = Some(PIN::channel()); self.active_channel = Some(AdcPin::<PIN, ADCI>::channel());
ADCI::set_en_pad(PIN::channel() as u8); ADCI::set_en_pad(AdcPin::<PIN, ADCI>::channel() as u8);
ADCI::clear_start_sar(); ADCI::clear_start_sar();
ADCI::set_start_sar(); ADCI::set_start_sar();

View File

@ -34,13 +34,12 @@ fn main() -> ! {
rtc.rwdt.disable(); rtc.rwdt.disable();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let mut pin25 = io.pins.gpio25.into_analog();
// Create ADC instances // Create ADC instances
let analog = peripherals.SENS.split(); let analog = peripherals.SENS.split();
let mut adc2_config = AdcConfig::new(); let mut adc2_config = AdcConfig::new();
adc2_config.enable_pin(&pin25, Attenuation::Attenuation11dB); let mut pin25 = adc2_config.enable_pin(io.pins.gpio25.into_analog(), Attenuation::Attenuation11dB);
let mut adc2 = ADC::<ADC2>::adc(analog.adc2, adc2_config).unwrap(); let mut adc2 = ADC::<ADC2>::adc(analog.adc2, adc2_config).unwrap();
let mut delay = Delay::new(&clocks); let mut delay = Delay::new(&clocks);

View File

@ -41,24 +41,25 @@ fn main() -> ! {
wdt1.disable(); wdt1.disable();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let mut pin = io.pins.gpio2.into_analog();
// Create ADC instances // Create ADC instances
let analog = peripherals.APB_SARADC.split(); let analog = peripherals.APB_SARADC.split();
let mut adc2_config = AdcConfig::new(); let mut adc1_config = AdcConfig::new();
adc2_config.enable_pin(&pin, Attenuation::Attenuation11dB);
let mut adc2 = ADC::<ADC1>::adc( let mut pin = adc1_config.enable_pin(io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB);
let mut adc1 = ADC::<ADC1>::adc(
&mut system.peripheral_clock_control, &mut system.peripheral_clock_control,
analog.adc1, analog.adc1,
adc2_config, adc1_config,
) )
.unwrap(); .unwrap();
let mut delay = Delay::new(&clocks); let mut delay = Delay::new(&clocks);
loop { loop {
let pin_value: u16 = nb::block!(adc2.read(&mut pin)).unwrap(); let pin_value: u16 = nb::block!(adc1.read(&mut pin)).unwrap();
println!("PIN ADC reading = {}", pin_value); println!("PIN ADC reading = {}", pin_value);
delay.delay_ms(1500u32); delay.delay_ms(1500u32);
} }

View File

@ -34,19 +34,20 @@ fn main() -> ! {
rtc.rwdt.disable(); rtc.rwdt.disable();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let mut pin3 = io.pins.gpio3.into_analog(); //let pin3 = io.pins.gpio3.into_analog();
// Create ADC instances // Create ADC instances
let analog = peripherals.SENS.split(); let analog = peripherals.SENS.split();
let mut adc1_config = AdcConfig::new(); let mut adc1_config = AdcConfig::new();
adc1_config.enable_pin(&pin3, Attenuation::Attenuation11dB);
let mut pin3 = adc1_config.enable_pin(io.pins.gpio3.into_analog(), Attenuation::Attenuation11dB);
let mut adc1 = ADC::<ADC1>::adc(analog.adc1, adc1_config).unwrap(); let mut adc1 = ADC::<ADC1>::adc(analog.adc1, adc1_config).unwrap();
let mut delay = Delay::new(&clocks); let mut delay = Delay::new(&clocks);
loop { loop {
println!("About to read from ADC");
let pin3_value: u16 = nb::block!(adc1.read(&mut pin3)).unwrap(); let pin3_value: u16 = nb::block!(adc1.read(&mut pin3)).unwrap();
println!("PIN3 ADC reading = {}", pin3_value); println!("PIN3 ADC reading = {}", pin3_value);
delay.delay_ms(1500u32); delay.delay_ms(1500u32);