From 28f5f0babaa6348c08985f599353e34b48c887fb Mon Sep 17 00:00:00 2001
From: Mattis Kieffer <mattis.kieffer@hotmail.de>
Date: Thu, 30 May 2024 14:07:47 +0200
Subject: [PATCH] add dac buffer function

---
 embassy-stm32/src/opamp.rs | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/embassy-stm32/src/opamp.rs b/embassy-stm32/src/opamp.rs
index 487902959..ca94a573d 100644
--- a/embassy-stm32/src/opamp.rs
+++ b/embassy-stm32/src/opamp.rs
@@ -110,6 +110,32 @@ impl<'d, T: Instance> OpAmp<'d, T> {
 
         OpAmpOutput { _inner: self }
     }
+    /// Configure the OpAmp as a buffer for the DAC it is connected to,
+    /// outputting to the provided output pin, and enable the opamp.
+    ///
+    /// The output pin is held within the returned [`OpAmpOutput`] struct,
+    /// preventing it being used elsewhere. The `OpAmpOutput` can then be
+    /// directly used as an ADC input. The opamp will be disabled when the
+    /// [`OpAmpOutput`] is dropped.
+    #[cfg(opamp_g4)]
+    pub fn buffer_dac(
+        &'d mut self,
+        out_pin: impl Peripheral<P = impl OutputPin<T> + crate::gpio::Pin> + 'd,
+    ) -> OpAmpOutput<'d, T> {
+        into_ref!(out_pin);
+        out_pin.set_as_analog();
+
+        T::regs().csr().modify(|w| {
+            use crate::pac::opamp::vals::*;
+
+            w.set_vm_sel(VmSel::OUTPUT);
+            w.set_vp_sel(VpSel::DAC3_CH1);
+            w.set_opaintoen(Opaintoen::OUTPUTPIN);
+            w.set_opampen(true);
+        });
+
+        OpAmpOutput { _inner: self }
+    }
 
     /// Configure the OpAmp as a buffer for the provided input pin,
     /// with the output only used internally, and enable the opamp.