diff options
| author | Adam Greig <[email protected]> | 2023-11-20 21:35:05 +0000 |
|---|---|---|
| committer | Adam Greig <[email protected]> | 2023-11-20 21:35:05 +0000 |
| commit | d1af6966051486fd605aff902b347b0346d2d889 (patch) | |
| tree | cf26fc73bb31ef5731322b81b527ba5056497923 | |
| parent | 2386619f1f8b9630166b0b55c275bb74125e4b9d (diff) | |
STM32 opamp: use impl Peripheral instead of directly taking pins
| -rw-r--r-- | embassy-stm32/src/opamp.rs | 46 | ||||
| -rw-r--r-- | examples/stm32f334/src/bin/opamp.rs | 2 |
2 files changed, 21 insertions, 27 deletions
diff --git a/embassy-stm32/src/opamp.rs b/embassy-stm32/src/opamp.rs index 4a607e8ab..e1eb031d1 100644 --- a/embassy-stm32/src/opamp.rs +++ b/embassy-stm32/src/opamp.rs | |||
| @@ -31,12 +31,9 @@ impl From<OpAmpSpeed> for crate::pac::opamp::vals::OpampCsrOpahsm { | |||
| 31 | 31 | ||
| 32 | /// OpAmp external outputs, wired to a GPIO pad. | 32 | /// OpAmp external outputs, wired to a GPIO pad. |
| 33 | /// | 33 | /// |
| 34 | /// The GPIO output pad is held by this struct to ensure it cannot be used elsewhere. | ||
| 35 | /// | ||
| 36 | /// This struct can also be used as an ADC input. | 34 | /// This struct can also be used as an ADC input. |
| 37 | pub struct OpAmpOutput<'d, 'p, T: Instance, P: OutputPin<T>> { | 35 | pub struct OpAmpOutput<'d, T: Instance> { |
| 38 | _inner: &'d OpAmp<'d, T>, | 36 | _inner: &'d OpAmp<'d, T>, |
| 39 | _output: &'p mut P, | ||
| 40 | } | 37 | } |
| 41 | 38 | ||
| 42 | /// OpAmp internal outputs, wired directly to ADC inputs. | 39 | /// OpAmp internal outputs, wired directly to ADC inputs. |
| @@ -76,16 +73,14 @@ impl<'d, T: Instance> OpAmp<'d, T> { | |||
| 76 | /// preventing it being used elsewhere. The `OpAmpOutput` can then be | 73 | /// preventing it being used elsewhere. The `OpAmpOutput` can then be |
| 77 | /// directly used as an ADC input. The opamp will be disabled when the | 74 | /// directly used as an ADC input. The opamp will be disabled when the |
| 78 | /// [`OpAmpOutput`] is dropped. | 75 | /// [`OpAmpOutput`] is dropped. |
| 79 | pub fn buffer_ext<'a, 'b, IP, OP>( | 76 | pub fn buffer_ext( |
| 80 | &'a mut self, | 77 | &'d mut self, |
| 81 | in_pin: &IP, | 78 | in_pin: impl Peripheral<P = impl NonInvertingPin<T> + crate::gpio::sealed::Pin>, |
| 82 | out_pin: &'b mut OP, | 79 | out_pin: impl Peripheral<P = impl OutputPin<T> + crate::gpio::sealed::Pin> + 'd, |
| 83 | gain: OpAmpGain, | 80 | gain: OpAmpGain, |
| 84 | ) -> OpAmpOutput<'a, 'b, T, OP> | 81 | ) -> OpAmpOutput<'d, T> { |
| 85 | where | 82 | into_ref!(in_pin); |
| 86 | IP: NonInvertingPin<T> + crate::gpio::sealed::Pin, | 83 | into_ref!(out_pin); |
| 87 | OP: OutputPin<T> + crate::gpio::sealed::Pin, | ||
| 88 | { | ||
| 89 | in_pin.set_as_analog(); | 84 | in_pin.set_as_analog(); |
| 90 | out_pin.set_as_analog(); | 85 | out_pin.set_as_analog(); |
| 91 | 86 | ||
| @@ -116,10 +111,7 @@ impl<'d, T: Instance> OpAmp<'d, T> { | |||
| 116 | w.set_opaen(true); | 111 | w.set_opaen(true); |
| 117 | }); | 112 | }); |
| 118 | 113 | ||
| 119 | OpAmpOutput { | 114 | OpAmpOutput { _inner: self } |
| 120 | _inner: self, | ||
| 121 | _output: out_pin, | ||
| 122 | } | ||
| 123 | } | 115 | } |
| 124 | 116 | ||
| 125 | /// Configure the OpAmp as a buffer for the provided input pin, | 117 | /// Configure the OpAmp as a buffer for the provided input pin, |
| @@ -131,10 +123,12 @@ impl<'d, T: Instance> OpAmp<'d, T> { | |||
| 131 | /// The returned `OpAmpInternalOutput` struct may be used as an ADC input. | 123 | /// The returned `OpAmpInternalOutput` struct may be used as an ADC input. |
| 132 | /// The opamp output will be disabled when it is dropped. | 124 | /// The opamp output will be disabled when it is dropped. |
| 133 | #[cfg(opamp_g4)] | 125 | #[cfg(opamp_g4)] |
| 134 | pub fn buffer_int<'a, P>(&'a mut self, pin: &P, gain: OpAmpGain) -> OpAmpInternalOutput<'a, T> | 126 | pub fn buffer_int( |
| 135 | where | 127 | &'d mut self, |
| 136 | P: NonInvertingPin<T> + crate::gpio::sealed::Pin, | 128 | pin: impl Peripheral<P = impl NonInvertingPin<T> + crate::gpio::sealed::Pin>, |
| 137 | { | 129 | gain: OpAmpGain, |
| 130 | ) -> OpAmpInternalOutput<'d, T> { | ||
| 131 | into_ref!(pin); | ||
| 138 | pin.set_as_analog(); | 132 | pin.set_as_analog(); |
| 139 | 133 | ||
| 140 | let (vm_sel, pga_gain) = match gain { | 134 | let (vm_sel, pga_gain) = match gain { |
| @@ -158,7 +152,7 @@ impl<'d, T: Instance> OpAmp<'d, T> { | |||
| 158 | } | 152 | } |
| 159 | } | 153 | } |
| 160 | 154 | ||
| 161 | impl<'d, 'p, T: Instance, P: OutputPin<T>> Drop for OpAmpOutput<'d, 'p, T, P> { | 155 | impl<'d, T: Instance> Drop for OpAmpOutput<'d, T> { |
| 162 | fn drop(&mut self) { | 156 | fn drop(&mut self) { |
| 163 | #[cfg(opamp_f3)] | 157 | #[cfg(opamp_f3)] |
| 164 | T::regs().opampcsr().modify(|w| { | 158 | T::regs().opampcsr().modify(|w| { |
| @@ -212,16 +206,16 @@ macro_rules! impl_opamp_external_output { | |||
| 212 | ($inst:ident, $adc:ident, $ch:expr) => { | 206 | ($inst:ident, $adc:ident, $ch:expr) => { |
| 213 | foreach_adc!( | 207 | foreach_adc!( |
| 214 | ($adc, $common_inst:ident, $adc_clock:ident) => { | 208 | ($adc, $common_inst:ident, $adc_clock:ident) => { |
| 215 | impl<'d, 'p, P: OutputPin<crate::peripherals::$inst>> crate::adc::sealed::AdcPin<crate::peripherals::$adc> | 209 | impl<'d> crate::adc::sealed::AdcPin<crate::peripherals::$adc> |
| 216 | for OpAmpOutput<'d, 'p, crate::peripherals::$inst, P> | 210 | for OpAmpOutput<'d, crate::peripherals::$inst> |
| 217 | { | 211 | { |
| 218 | fn channel(&self) -> u8 { | 212 | fn channel(&self) -> u8 { |
| 219 | $ch | 213 | $ch |
| 220 | } | 214 | } |
| 221 | } | 215 | } |
| 222 | 216 | ||
| 223 | impl<'d, 'p, P: OutputPin<crate::peripherals::$inst>> crate::adc::AdcPin<crate::peripherals::$adc> | 217 | impl<'d> crate::adc::AdcPin<crate::peripherals::$adc> |
| 224 | for OpAmpOutput<'d, 'p, crate::peripherals::$inst, P> | 218 | for OpAmpOutput<'d, crate::peripherals::$inst> |
| 225 | { | 219 | { |
| 226 | } | 220 | } |
| 227 | }; | 221 | }; |
diff --git a/examples/stm32f334/src/bin/opamp.rs b/examples/stm32f334/src/bin/opamp.rs index 137fc9e66..10e7b3543 100644 --- a/examples/stm32f334/src/bin/opamp.rs +++ b/examples/stm32f334/src/bin/opamp.rs | |||
| @@ -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_ext(&p.PA7, &mut p.PA6, OpAmpGain::Mul1); | 42 | let mut buffer = opamp.buffer_ext(&mut p.PA7, &mut p.PA6, OpAmpGain::Mul1); |
| 43 | 43 | ||
| 44 | loop { | 44 | loop { |
| 45 | let vref = adc.read(&mut vrefint).await; | 45 | let vref = adc.read(&mut vrefint).await; |
