aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/Cargo.toml4
-rw-r--r--embassy-stm32/src/opamp.rs32
-rw-r--r--examples/stm32f334/src/bin/opamp.rs4
3 files changed, 33 insertions, 7 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index d43252ada..1b688eca9 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -59,7 +59,7 @@ sdio-host = "0.5.0"
59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } 59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true }
60critical-section = "1.1" 60critical-section = "1.1"
61atomic-polyfill = "1.0.1" 61atomic-polyfill = "1.0.1"
62stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-06d13dfd245cc9bf86fd88c35b401bdb84c079c4" } 62stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-172c5ea18824d7cd38decb210e4af441fa3816cb" }
63vcell = "0.1.3" 63vcell = "0.1.3"
64bxcan = "0.7.0" 64bxcan = "0.7.0"
65nb = "1.0.0" 65nb = "1.0.0"
@@ -78,7 +78,7 @@ critical-section = { version = "1.1", features = ["std"] }
78[build-dependencies] 78[build-dependencies]
79proc-macro2 = "1.0.36" 79proc-macro2 = "1.0.36"
80quote = "1.0.15" 80quote = "1.0.15"
81stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-06d13dfd245cc9bf86fd88c35b401bdb84c079c4", default-features = false, features = ["metadata"]} 81stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-172c5ea18824d7cd38decb210e4af441fa3816cb", default-features = false, features = ["metadata"]}
82 82
83 83
84[features] 84[features]
diff --git a/embassy-stm32/src/opamp.rs b/embassy-stm32/src/opamp.rs
index 7b388aefe..e0fad26eb 100644
--- a/embassy-stm32/src/opamp.rs
+++ b/embassy-stm32/src/opamp.rs
@@ -4,7 +4,15 @@ use embassy_hal_internal::{into_ref, PeripheralRef};
4 4
5use crate::Peripheral; 5use crate::Peripheral;
6 6
7#[cfg(opamp_f3)] 7#[derive(Clone, Copy)]
8pub enum OpAmpGain {
9 Mul1,
10 Mul2,
11 Mul4,
12 Mul8,
13 Mul16,
14}
15
8pub struct OpAmpOutput<'d, 'p, T: Instance, P: NonInvertingPin<T>> { 16pub struct OpAmpOutput<'d, 'p, T: Instance, P: NonInvertingPin<T>> {
9 _inner: &'d OpAmp<'d, T>, 17 _inner: &'d OpAmp<'d, T>,
10 _input: &'p mut P, 18 _input: &'p mut P,
@@ -35,14 +43,32 @@ impl<'d, T: Instance> OpAmp<'d, T> {
35 Self { _inner: opamp } 43 Self { _inner: opamp }
36 } 44 }
37 45
38 #[cfg(opamp_f3)] 46 pub fn buffer_for<'a, 'b, P>(&'a mut self, pin: &'b mut P, gain: OpAmpGain) -> OpAmpOutput<'a, 'b, T, P>
39 pub fn buffer_for<'a, 'b, P>(&'a mut self, pin: &'b mut P) -> OpAmpOutput<'a, 'b, T, P>
40 where 47 where
41 P: NonInvertingPin<T>, 48 P: NonInvertingPin<T>,
42 { 49 {
50 let (vm_sel, pga_gain) = match gain {
51 OpAmpGain::Mul1 => (0b11, 0b00),
52 OpAmpGain::Mul2 => (0b10, 0b00),
53 OpAmpGain::Mul4 => (0b10, 0b01),
54 OpAmpGain::Mul8 => (0b10, 0b10),
55 OpAmpGain::Mul16 => (0b10, 0b11),
56 };
57
43 #[cfg(opamp_f3)] 58 #[cfg(opamp_f3)]
44 T::regs().opampcsr().modify(|w| { 59 T::regs().opampcsr().modify(|w| {
45 w.set_vp_sel(pin.channel()); 60 w.set_vp_sel(pin.channel());
61 w.set_vm_sel(vm_sel);
62 w.set_pga_gain(pga_gain);
63 });
64
65 #[cfg(opamp_g4)]
66 T::regs().opamp_csr().modify(|w| {
67 use crate::pac::opamp::vals::*;
68
69 w.set_vp_sel(OpampCsrVpSel::from_bits(pin.channel()));
70 w.set_vm_sel(OpampCsrVmSel::from_bits(vm_sel));
71 w.set_pga_gain(OpampCsrPgaGain::from_bits(pga_gain));
46 }); 72 });
47 73
48 OpAmpOutput { 74 OpAmpOutput {
diff --git a/examples/stm32f334/src/bin/opamp.rs b/examples/stm32f334/src/bin/opamp.rs
index 72263bab8..3fffcfb1f 100644
--- a/examples/stm32f334/src/bin/opamp.rs
+++ b/examples/stm32f334/src/bin/opamp.rs
@@ -5,7 +5,7 @@
5use defmt::info; 5use defmt::info;
6use embassy_executor::Spawner; 6use embassy_executor::Spawner;
7use embassy_stm32::adc::{Adc, SampleTime}; 7use embassy_stm32::adc::{Adc, SampleTime};
8use embassy_stm32::opamp::OpAmp; 8use embassy_stm32::opamp::{OpAmp, OpAmpGain};
9use embassy_stm32::peripherals::ADC2; 9use embassy_stm32::peripherals::ADC2;
10use embassy_stm32::rcc::AdcClockSource; 10use embassy_stm32::rcc::AdcClockSource;
11use embassy_stm32::time::mhz; 11use embassy_stm32::time::mhz;
@@ -39,7 +39,7 @@ async fn main(_spawner: Spawner) -> ! {
39 39
40 let mut vrefint = adc.enable_vref(&mut Delay); 40 let mut vrefint = adc.enable_vref(&mut Delay);
41 let mut temperature = adc.enable_temperature(); 41 let mut temperature = adc.enable_temperature();
42 let mut buffer = opamp.buffer_for(&mut p.PA7); 42 let mut buffer = opamp.buffer_for(&mut p.PA7, OpAmpGain::Mul1);
43 43
44 loop { 44 loop {
45 let vref = adc.read(&mut vrefint).await; 45 let vref = adc.read(&mut vrefint).await;