diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index d43252ad..1b688eca 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml @@ -59,7 +59,7 @@ sdio-host = "0.5.0" embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } critical-section = "1.1" atomic-polyfill = "1.0.1" -stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-06d13dfd245cc9bf86fd88c35b401bdb84c079c4" } +stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-172c5ea18824d7cd38decb210e4af441fa3816cb" } vcell = "0.1.3" bxcan = "0.7.0" nb = "1.0.0" @@ -78,7 +78,7 @@ critical-section = { version = "1.1", features = ["std"] } [build-dependencies] proc-macro2 = "1.0.36" quote = "1.0.15" -stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-06d13dfd245cc9bf86fd88c35b401bdb84c079c4", default-features = false, features = ["metadata"]} +stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-172c5ea18824d7cd38decb210e4af441fa3816cb", default-features = false, features = ["metadata"]} [features] diff --git a/embassy-stm32/src/opamp.rs b/embassy-stm32/src/opamp.rs index 7b388aef..e0fad26e 100644 --- a/embassy-stm32/src/opamp.rs +++ b/embassy-stm32/src/opamp.rs @@ -4,7 +4,15 @@ use embassy_hal_internal::{into_ref, PeripheralRef}; use crate::Peripheral; -#[cfg(opamp_f3)] +#[derive(Clone, Copy)] +pub enum OpAmpGain { + Mul1, + Mul2, + Mul4, + Mul8, + Mul16, +} + pub struct OpAmpOutput<'d, 'p, T: Instance, P: NonInvertingPin> { _inner: &'d OpAmp<'d, T>, _input: &'p mut P, @@ -35,14 +43,32 @@ impl<'d, T: Instance> OpAmp<'d, T> { Self { _inner: opamp } } - #[cfg(opamp_f3)] - pub fn buffer_for<'a, 'b, P>(&'a mut self, pin: &'b mut P) -> OpAmpOutput<'a, 'b, T, P> + pub fn buffer_for<'a, 'b, P>(&'a mut self, pin: &'b mut P, gain: OpAmpGain) -> OpAmpOutput<'a, 'b, T, P> where P: NonInvertingPin, { + let (vm_sel, pga_gain) = match gain { + OpAmpGain::Mul1 => (0b11, 0b00), + OpAmpGain::Mul2 => (0b10, 0b00), + OpAmpGain::Mul4 => (0b10, 0b01), + OpAmpGain::Mul8 => (0b10, 0b10), + OpAmpGain::Mul16 => (0b10, 0b11), + }; + #[cfg(opamp_f3)] T::regs().opampcsr().modify(|w| { w.set_vp_sel(pin.channel()); + w.set_vm_sel(vm_sel); + w.set_pga_gain(pga_gain); + }); + + #[cfg(opamp_g4)] + T::regs().opamp_csr().modify(|w| { + use crate::pac::opamp::vals::*; + + w.set_vp_sel(OpampCsrVpSel::from_bits(pin.channel())); + w.set_vm_sel(OpampCsrVmSel::from_bits(vm_sel)); + w.set_pga_gain(OpampCsrPgaGain::from_bits(pga_gain)); }); OpAmpOutput { diff --git a/examples/stm32f334/src/bin/opamp.rs b/examples/stm32f334/src/bin/opamp.rs index 72263bab..3fffcfb1 100644 --- a/examples/stm32f334/src/bin/opamp.rs +++ b/examples/stm32f334/src/bin/opamp.rs @@ -5,7 +5,7 @@ use defmt::info; use embassy_executor::Spawner; use embassy_stm32::adc::{Adc, SampleTime}; -use embassy_stm32::opamp::OpAmp; +use embassy_stm32::opamp::{OpAmp, OpAmpGain}; use embassy_stm32::peripherals::ADC2; use embassy_stm32::rcc::AdcClockSource; use embassy_stm32::time::mhz; @@ -39,7 +39,7 @@ async fn main(_spawner: Spawner) -> ! { let mut vrefint = adc.enable_vref(&mut Delay); let mut temperature = adc.enable_temperature(); - let mut buffer = opamp.buffer_for(&mut p.PA7); + let mut buffer = opamp.buffer_for(&mut p.PA7, OpAmpGain::Mul1); loop { let vref = adc.read(&mut vrefint).await;