diff --git a/include/libopencm3/stm32/common/opamp_common_all.h b/include/libopencm3/stm32/common/opamp_common_all.h
new file mode 100644
index 00000000..eb6d6f34
--- /dev/null
+++ b/include/libopencm3/stm32/common/opamp_common_all.h
@@ -0,0 +1,99 @@
+/** @addtogroup opamp_defines
+ *
+ * @date 10 Dec 2020
+ *
+ *LGPL License Terms @ref lgpl_license
+ */
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see .
+ */
+
+/** @cond */
+#if defined(LIBOPENCM3_OPAMP_H)
+/** @endcond */
+#ifndef LIBOPENCM3_OPAMP_COMMON_ALL_H
+#define LIBOPENCM3_OPAMP_COMMON_ALL_H
+/**@{*/
+
+/* OpAmp registers */
+
+/* Control and status register (OPAMPx_CSR) */
+#define OPAMP_CSR(opamp_base) MMIO32((opamp_base) + 0x00)
+
+/* OPAMPx_CSR values */
+
+#define OPAMP_CSR_LOCK (0x1 << 31)
+
+#define OPAMP_CSR_TSTREF (0x1 << 29)
+
+#define OPAMP_CSR_TRIMOFFSETN_MASK (0x1f)
+#define OPAMP_CSR_TRIMOFFSETN_SHIFT (24)
+
+#define OPAMP_CSR_TRIMOFFSETP_MASK (0x1f)
+#define OPAMP_CSR_TRIMOFFSETP_SHIFT (19)
+
+#define OPAMP_CSR_CALSEL_MASK (0x3)
+#define OPAMP_CSR_CALSEL_SHIFT (12)
+#define OPAMP_CSR_CALSEL_3P3_PERCENT (0x0)
+#define OPAMP_CSR_CALSEL_10_PERCENT (0x1)
+#define OPAMP_CSR_CALSEL_50_PERCENT (0x2)
+#define OPAMP_CSR_CALSEL_90_PERCENT (0x3)
+
+#define OPAMP_CSR_CALON (0x1 << 11)
+
+#define OPAMP_CSR_VM_SEL_MASK (0x3)
+#define OPAMP_CSR_VM_SEL_SHIFT (5)
+
+#define OPAMP_CSR_VP_SEL_MASK (0x3)
+#define OPAMP_CSR_VP_SEL_SHIFT (2)
+
+#define OPAMP_CSR_FORCE_VP (0x1 << 1)
+#define OPAMP_CSR_EN (0x1 << 0)
+
+
+BEGIN_DECLS
+
+void opamp_enable(uint32_t base);
+void opamp_disable(uint32_t base);
+void opamp_lock(uint32_t base);
+
+void opamp_set_calsel(uint32_t base, uint32_t calsel);
+void opamp_tstref_enable(uint32_t base);
+void opamp_tstref_disable(uint32_t base);
+void opamp_force_vp_enable(uint32_t base);
+void opamp_force_vp_disable(uint32_t base);
+void opamp_cal_enable(uint32_t base);
+void opamp_cal_disable(uint32_t base);
+
+void opamp_trimoffsetn_set(uint32_t base, uint32_t trim);
+void opamp_trimoffsetp_set(uint32_t base, uint32_t trim);
+void opamp_user_trim_enable(uint32_t base);
+void opamp_user_trim_disable(uint32_t base);
+
+void opamp_pga_gain_select(uint32_t base, uint32_t gain);
+
+void opamp_vp_select(uint32_t base, uint32_t vp);
+void opamp_vm_select(uint32_t base, uint32_t vm);
+
+END_DECLS
+/**@}*/
+
+#endif
+/** @cond */
+#else
+#warning "opamp_common_all.h should not be included directly, only via opamp.h"
+#endif
+/** @endcond */
diff --git a/include/libopencm3/stm32/common/opamp_common_v1.h b/include/libopencm3/stm32/common/opamp_common_v1.h
new file mode 100644
index 00000000..ee3bf267
--- /dev/null
+++ b/include/libopencm3/stm32/common/opamp_common_v1.h
@@ -0,0 +1,86 @@
+/** @addtogroup opamp_defines
+ *
+ * @date 10 Dec 2020
+ *
+ *LGPL License Terms @ref lgpl_license
+ */
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see .
+ */
+
+/** @cond */
+#if defined(LIBOPENCM3_OPAMP_H)
+/** @endcond */
+#ifndef LIBOPENCM3_OPAMP_COMMON_V1_H
+#define LIBOPENCM3_OPAMP_COMMON_V1_H
+/**@{*/
+
+#include
+
+/* OPAMPx_CSR values */
+
+#define OPAMP_CSR_OUTCAL_MASK (0x1)
+#define OPAMP_CSR_OUTCAL_SHIFT (30)
+#define OPAMP_CSR_OUTCAL_NON_LO_INV (0x0)
+#define OPAMP_CSR_OUTCAL_NON_HI_INV (0x1)
+
+#define OPAMP_CSR_USER_TRIM (0x1 << 18)
+
+#define OPAMP_CSR_PGA_GAIN_MASK (0xf)
+#define OPAMP_CSR_PGA_GAIN_SHIFT (14)
+#define OPAMP_CSR_PGA_GAIN_2 (0x0)
+#define OPAMP_CSR_PGA_GAIN_4 (0x1)
+#define OPAMP_CSR_PGA_GAIN_8 (0x2)
+#define OPAMP_CSR_PGA_GAIN_16 (0x3)
+#define OPAMP_CSR_PGA_GAIN_2_MINUS_VM0 (0x8)
+#define OPAMP_CSR_PGA_GAIN_4_MINUS_VM0 (0x9)
+#define OPAMP_CSR_PGA_GAIN_8_MINUS_VM0 (0xa)
+#define OPAMP_CSR_PGA_GAIN_16_MINUS_VM0 (0xb)
+#define OPAMP_CSR_PGA_GAIN_2_MINUS_VM1 (0xc)
+#define OPAMP_CSR_PGA_GAIN_4_MINUS_VM1 (0xd)
+#define OPAMP_CSR_PGA_GAIN_8_MINUS_VM1 (0xe)
+#define OPAMP_CSR_PGA_GAIN_16_MINUS_VM1 (0xf)
+
+#define OPAMP_CSR_VPS_SEL_MASK (0x3)
+#define OPAMP_CSR_VPS_SEL_SHIFT (9)
+
+#define OPAMP_CSR_VMS_SEL_MASK (0x1)
+#define OPAMP_CSR_VMS_SEL_SHIFT (8)
+
+#define OPAMP_CSR_TCM_EN (0x1 << 7)
+
+#define OPAMP_CSR_VM_SEL_PGA_MODE (0x2)
+#define OPAMP_CSR_VM_SEL_FOLLOWER_MODE (0x3)
+
+
+BEGIN_DECLS
+
+bool opamp_read_outcal(uint32_t base);
+
+void opamp_vps_select(uint32_t base, uint32_t vps);
+void opamp_vms_select(uint32_t base, uint32_t vms);
+void opamp_tcm_enable(uint32_t base);
+void opamp_tcm_disable(uint32_t base);
+
+END_DECLS
+/**@}*/
+
+#endif
+/** @cond */
+#else
+#warning "opamp_common_v1.h should not be included directly, only via opamp.h"
+#endif
+/** @endcond */
diff --git a/include/libopencm3/stm32/common/opamp_common_v2.h b/include/libopencm3/stm32/common/opamp_common_v2.h
new file mode 100644
index 00000000..c073fdd4
--- /dev/null
+++ b/include/libopencm3/stm32/common/opamp_common_v2.h
@@ -0,0 +1,111 @@
+/** @addtogroup opamp_defines
+ *
+ * @date 10 Dec 2020
+ *
+ *LGPL License Terms @ref lgpl_license
+ */
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see .
+ */
+
+/** @cond */
+#if defined(LIBOPENCM3_OPAMP_H)
+/** @endcond */
+#ifndef LIBOPENCM3_OPAMP_COMMON_V2_H
+#define LIBOPENCM3_OPAMP_COMMON_V2_H
+/**@{*/
+
+#include
+
+/* OpAmp registers */
+
+/* Timer controlled mode register (OPAMPx_TMCR) */
+#define OPAMP_TCMR(opamp_base) MMIO32((opamp_base) + 0x18)
+
+/* OPAMPx_CSR values */
+
+#define OPAMP_CSR_CALOUT_MASK (0x1)
+#define OPAMP_CSR_CALOUT_SHIFT (30)
+#define OPAMP_CSR_CALOUT_UNSUCC (0x0)
+#define OPAMP_CSR_CALOUT_SUCC (0x1)
+
+#define OPAMP_CSR_PGA_GAIN_MASK (0x1f)
+#define OPAMP_CSR_PGA_GAIN_SHIFT (14)
+
+/* Non-inverting gain*/
+#define OPAMP_CSR_PGA_GAIN_2 (0x00)
+#define OPAMP_CSR_PGA_GAIN_4 (0x01)
+#define OPAMP_CSR_PGA_GAIN_8 (0x02)
+#define OPAMP_CSR_PGA_GAIN_16 (0x03)
+#define OPAMP_CSR_PGA_GAIN_32 (0x04)
+#define OPAMP_CSR_PGA_GAIN_64 (0x05)
+/* Inverting gain -n/non-inverting gain y with VINM0 pin for input bias */
+#define OPAMP_CSR_PGA_INV_GAIN_MINUS_1_GAIN_2_VM0 (0x08)
+#define OPAMP_CSR_PGA_INV_GAIN_MINUS_3_GAIN_4_VM0 (0x09)
+#define OPAMP_CSR_PGA_INV_GAIN_MINUS_7_GAIN_8_VM0 (0x0A)
+#define OPAMP_CSR_PGA_INV_GAIN_MINUS_15_GAIN_16_VM0 (0x0B)
+#define OPAMP_CSR_PGA_INV_GAIN_MINUS_31_GAIN_32_VM0 (0x0C)
+#define OPAMP_CSR_PGA_INV_GAIN_MINUS_63_GAIN_64_VM0 (0x0D)
+/* Non-inverting gain with filtering on VINM0 */
+#define OPAMP_CSR_PGA_FILT_VM0_GAIN_2 (0x10)
+#define OPAMP_CSR_PGA_FILT_VM0_GAIN_4 (0x11)
+#define OPAMP_CSR_PGA_FILT_VM0_GAIN_8 (0x12)
+#define OPAMP_CSR_PGA_FILT_VM0_GAIN_16 (0x13)
+#define OPAMP_CSR_PGA_FILT_VM0_GAIN_32 (0x14)
+#define OPAMP_CSR_PGA_FILT_VM0_GAIN_64 (0x15)
+/* Inverting gain -x/non-inverting gain y with VINM0 pin for input bias
+ * VINM1 for filtering*/
+#define OPAMP_CSR_PGA_FILT_VM1_INV_GAIN_MINUS_1_GAIN_2_VM0 (0x18)
+#define OPAMP_CSR_PGA_FILT_VM1_INV_GAIN_MINUS_3_GAIN_4_VM0 (0x19)
+#define OPAMP_CSR_PGA_FILT_VM1_INV_GAIN_MINUS_7_GAIN_8_VM0 (0x1a)
+#define OPAMP_CSR_PGA_FILT_VM1_INV_GAIN_MINUS_15_GAIN_16_VM0 (0x1B)
+#define OPAMP_CSR_PGA_FILT_VM1_INV_GAIN_MINUS_31_GAIN_32_VM0 (0x1c)
+#define OPAMP_CSR_PGA_FILT_VM1_INV_GAIN_MINUS_63_GAIN_64_VM0 (0x1d)
+
+#define OPAMP_CSR_OPAINTOEN (0x1 << 8)
+
+#define OPAMP_CSR_OPAHSM (0x1 << 7)
+
+#define OPAMP_CSR_VM_SEL_VINM0_IN (0x0)
+#define OPAMP_CSR_VM_SEL_VINM1_IN (0x1)
+#define OPAMP_CSR_VM_SEL_PGA_MODE (0x2)
+#define OPAMP_CSR_VM_SEL_OUT_IN (0x3)
+
+#define OPAMP_CSR_USER_TRIM (0x1 << 4)
+
+#define OPAMP_CSR_VP_SEL_VINP0 (0x0)
+#define OPAMP_CSR_VP_SEL_VINP1 (0x1)
+#define OPAMP_CSR_VP_SEL_VINP2 (0x2)
+
+BEGIN_DECLS
+
+bool opamp_read_calout(uint32_t base);
+
+void opamp_high_speed_mode_enable(uint32_t base);
+void opamp_high_speed_mode_disable(uint32_t base);
+
+void opamp_output_set_internal(uint32_t base);
+void opamp_output_set_external(uint32_t base);
+
+END_DECLS
+/**@}*/
+
+#endif
+/** @cond */
+#else
+#warning "opamp_common_v2.h should not be included directly, only via opamp.h"
+#endif
+/** @endcond */
diff --git a/include/libopencm3/stm32/f3/opamp.h b/include/libopencm3/stm32/f3/opamp.h
index 54f2d422..ad236a11 100644
--- a/include/libopencm3/stm32/f3/opamp.h
+++ b/include/libopencm3/stm32/f3/opamp.h
@@ -32,6 +32,8 @@
#define LIBOPENCM3_OPAMP_H
/**@{*/
+#include
+
#define OPAMP1 (OPAMP_BASE + 0x38)
#define OPAMP2 (OPAMP_BASE + 0x3C)
#define OPAMP3 (OPAMP_BASE + 0x40)
@@ -40,7 +42,6 @@
/* OpAmp registers */
/* Control and status register (OPAMPx_CSR) */
-#define OPAMP_CSR(opamp_base) MMIO32((opamp_base) + 0x00)
#define OPAMP1_CSR OPAMP_CSR(OPAMP1)
#define OPAMP2_CSR OPAMP_CSR(OPAMP2)
#define OPAMP3_CSR OPAMP_CSR(OPAMP3)
@@ -48,49 +49,6 @@
/* OPAMPx_CSR values */
-#define OPAMP_CSR_LOCK (0x1 << 31)
-
-#define OPAMP_CSR_OUTCAL_MASK (0x1)
-#define OPAMP_CSR_OUTCAL_SHIFT (30)
-#define OPAMP_CSR_OUTCAL_NON_LO_INV (0x0)
-#define OPAMP_CSR_OUTCAL_NON_HI_INV (0x1)
-
-#define OPAMP_CSR_TSTREF (0x1 << 29)
-
-#define OPAMP_CSR_TRIMOFFSETN_MASK (0x1f)
-#define OPAMP_CSR_TRIMOFFSETN_SHIFT (24)
-
-#define OPAMP_CSR_TRIMOFFSETP_MASK (0x1f)
-#define OPAMP_CSR_TRIMOFFSETP_SHIFT (19)
-
-#define OPAMP_CSR_USER_TRIM (0x1 << 18)
-
-#define OPAMP_CSR_PGA_GAIN_MASK (0xf)
-#define OPAMP_CSR_PGA_GAIN_SHIFT (14)
-#define OPAMP_CSR_PGA_GAIN_2 (0x0)
-#define OPAMP_CSR_PGA_GAIN_4 (0x1)
-#define OPAMP_CSR_PGA_GAIN_8 (0x2)
-#define OPAMP_CSR_PGA_GAIN_16 (0x3)
-#define OPAMP_CSR_PGA_GAIN_2_MINUS_VM0 (0x8)
-#define OPAMP_CSR_PGA_GAIN_4_MINUS_VM0 (0x9)
-#define OPAMP_CSR_PGA_GAIN_8_MINUS_VM0 (0xa)
-#define OPAMP_CSR_PGA_GAIN_16_MINUS_VM0 (0xb)
-#define OPAMP_CSR_PGA_GAIN_2_MINUS_VM1 (0xc)
-#define OPAMP_CSR_PGA_GAIN_4_MINUS_VM1 (0xd)
-#define OPAMP_CSR_PGA_GAIN_8_MINUS_VM1 (0xe)
-#define OPAMP_CSR_PGA_GAIN_16_MINUS_VM1 (0xf)
-
-#define OPAMP_CSR_CALSEL_MASK (0x3)
-#define OPAMP_CSR_CALSEL_SHIFT (12)
-#define OPAMP_CSR_CALSEL_3P3_PERCENT (0x0)
-#define OPAMP_CSR_CALSEL_10_PERCENT (0x1)
-#define OPAMP_CSR_CALSEL_50_PERCENT (0x2)
-#define OPAMP_CSR_CALSEL_90_PERCENT (0x3)
-
-#define OPAMP_CSR_CALON (0x1 << 11)
-
-#define OPAMP_CSR_VPS_SEL_MASK (0x3)
-#define OPAMP_CSR_VPS_SEL_SHIFT (9)
#define OPAMP1_CSR_VPS_SEL_PA7 (0x0)
#define OPAMP1_CSR_VPS_SEL_PA5 (0x1)
#define OPAMP1_CSR_VPS_SEL_PA3 (0x2)
@@ -108,8 +66,6 @@
#define OPAMP4_CSR_VPS_SEL_PA4 (0x2)
#define OPAMP4_CSR_VPS_SEL_PB13 (0x3)
-#define OPAMP_CSR_VMS_SEL_MASK (0x1)
-#define OPAMP_CSR_VMS_SEL_SHIFT (8)
#define OPAMP1_CSR_VMS_SEL_PC5 (0x0)
#define OPAMP1_CSR_VMS_SEL_PA3 (0x1)
#define OPAMP2_CSR_VMS_SEL_PC5 (0x0)
@@ -119,12 +75,6 @@
#define OPAMP4_CSR_VMS_SEL_PB10 (0x0)
#define OPAMP4_CSR_VMS_SEL_PD8 (0x1)
-#define OPAMP_CSR_TCM_EN (0x1 << 7)
-
-#define OPAMP_CSR_VM_SEL_MASK (0x3)
-#define OPAMP_CSR_VM_SEL_SHIFT (5)
-#define OPAMP_CSR_VM_SEL_PGA_MODE (0x2)
-#define OPAMP_CSR_VM_SEL_FOLLOWER_MODE (0x3)
#define OPAMP1_CSR_VM_SEL_PC5 (0x0)
#define OPAMP1_CSR_VM_SEL_PA3 (0x1)
#define OPAMP2_CSR_VM_SEL_PC5 (0x0)
@@ -134,8 +84,6 @@
#define OPAMP4_CSR_VM_SEL_PB10 (0x0)
#define OPAMP4_CSR_VM_SEL_PD8 (0x1)
-#define OPAMP_CSR_VP_SEL_MASK (0x3)
-#define OPAMP_CSR_VP_SEL_SHIFT (2)
#define OPAMP1_CSR_VP_SEL_PA7 (0x0)
#define OPAMP1_CSR_VP_SEL_PA5 (0x1)
#define OPAMP1_CSR_VP_SEL_PA3 (0x2)
@@ -153,41 +101,5 @@
#define OPAMP4_CSR_VP_SEL_PA4 (0x2)
#define OPAMP4_CSR_VP_SEL_PB13 (0x3)
-#define OPAMP_CSR_FORCE_VP (0x1 << 1)
-#define OPAMP_CSR_EN (0x1 << 0)
-
-
-BEGIN_DECLS
-
-void opamp_enable(uint32_t base);
-void opamp_disable(uint32_t base);
-void opamp_lock(uint32_t base);
-
-void opamp_set_calsel(uint32_t base, uint32_t calsel);
-bool opamp_read_outcal(uint32_t base);
-void opamp_tstref_enable(uint32_t base);
-void opamp_tstref_disable(uint32_t base);
-void opamp_force_vp_enable(uint32_t base);
-void opamp_force_vp_disable(uint32_t base);
-void opamp_cal_enable(uint32_t base);
-void opamp_cal_disable(uint32_t base);
-
-void opamp_trimoffsetn_set(uint32_t base, uint32_t trim);
-void opamp_trimoffsetp_set(uint32_t base, uint32_t trim);
-void opamp_user_trim_enable(uint32_t base);
-void opamp_user_trim_disable(uint32_t base);
-
-void opamp_pga_gain_select(uint32_t base, uint32_t gain);
-
-void opamp_vp_select(uint32_t base, uint32_t vp);
-void opamp_vm_select(uint32_t base, uint32_t vm);
-
-void opamp_vps_select(uint32_t base, uint32_t vps);
-void opamp_vms_select(uint32_t base, uint32_t vms);
-void opamp_tcm_enable(uint32_t base);
-void opamp_tcm_disable(uint32_t base);
-
-END_DECLS
-
/**@}*/
#endif
diff --git a/include/libopencm3/stm32/g4/opamp.h b/include/libopencm3/stm32/g4/opamp.h
new file mode 100644
index 00000000..c3a04832
--- /dev/null
+++ b/include/libopencm3/stm32/g4/opamp.h
@@ -0,0 +1,70 @@
+/** @defgroup opamp_defines OPAMP Defines
+ *
+ * @brief libopencm3 Defined Constants and Types for the STM32G4xx
+ * Operational Amplifier module
+ *
+ * @ingroup STM32G4xx_defines
+ *
+ * @version 1.0.0
+ *
+ * @date 10 Dec 2020
+ *
+ *LGPL License Terms @ref lgpl_license
+ */
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see .
+ */
+
+#ifndef LIBOPENCM3_OPAMP_H
+#define LIBOPENCM3_OPAMP_H
+/**@{*/
+
+#include
+
+#define OPAMP1 (OPAMP_BASE + 0x00)
+#define OPAMP2 (OPAMP_BASE + 0x04)
+#define OPAMP3 (OPAMP_BASE + 0x08)
+#define OPAMP4 (OPAMP_BASE + 0x0C)
+#define OPAMP5 (OPAMP_BASE + 0x10)
+#define OPAMP6 (OPAMP_BASE + 0x14)
+
+/* OpAmp registers */
+
+/* Control and status register (OPAMPx_CSR) */
+#define OPAMP1_CSR OPAMP_CSR(OPAMP1)
+#define OPAMP2_CSR OPAMP_CSR(OPAMP2)
+#define OPAMP3_CSR OPAMP_CSR(OPAMP3)
+#define OPAMP4_CSR OPAMP_CSR(OPAMP4)
+#define OPAMP5_CSR OPAMP_CSR(OPAMP5)
+#define OPAMP6_CSR OPAMP_CSR(OPAMP6)
+
+#define OPAMP1_TCMR OPAMP_TCMR(OPAMP1)
+#define OPAMP2_TCMR OPAMP_TCMR(OPAMP2)
+#define OPAMP3_TCMR OPAMP_TCMR(OPAMP3)
+#define OPAMP4_TCMR OPAMP_TCMR(OPAMP4)
+#define OPAMP5_TCMR OPAMP_TCMR(OPAMP5)
+#define OPAMP6_TCMR OPAMP_TCMR(OPAMP6)
+
+/* OPAMPx_CSR values */
+#define OPAMP1_CSR_VP_SEL_DAC3CH1 (0x3)
+#define OPAMP2_CSR_VP_SEL_VINP3 (0x3)
+#define OPAMP3_CSR_VP_SEL_DAC3CH2 (0x3)
+#define OPAMP4_CSR_VP_SEL_DAC4CH1 (0x3)
+#define OPAMP5_CSR_VP_SEL_DAC4CH2 (0x3)
+#define OPAMP6_CSR_VP_SEL_DAC3CH1 (0x3)
+
+/**@}*/
+#endif
diff --git a/include/libopencm3/stm32/opamp.h b/include/libopencm3/stm32/opamp.h
index bde4a3a6..56e2823d 100644
--- a/include/libopencm3/stm32/opamp.h
+++ b/include/libopencm3/stm32/opamp.h
@@ -22,6 +22,8 @@
#if defined(STM32F3)
# include
+#elif defined(STM32G4)
+# include
#else
# error "stm32 family not defined."
#endif
diff --git a/lib/stm32/f3/opamp.c b/lib/stm32/common/opamp_common_all.c
similarity index 75%
rename from lib/stm32/f3/opamp.c
rename to lib/stm32/common/opamp_common_all.c
index 702e8eb0..49f24550 100644
--- a/lib/stm32/f3/opamp.c
+++ b/lib/stm32/common/opamp_common_all.c
@@ -1,14 +1,5 @@
-/** @defgroup opamp_file OPAMP
- *
- * @ingroup STM32F3xx
- *
- * @brief libopencm3 STM32F3xx OPAMP
- *
- * @version 1.0.0
- *
- * @date 22 May 2020
- *
- * LGPL License Terms @ref lgpl_license
+/** @addtogroup opamp_file OPAMP peripheral API
+ * @ingroup peripheral_apis
*/
/*
@@ -27,6 +18,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see .
*/
+
/**@{*/
#include
@@ -46,29 +38,12 @@ void opamp_lock(uint32_t base)
OPAMP_CSR(base) |= OPAMP_CSR_LOCK;
}
-
void opamp_set_calsel(uint32_t base, uint32_t calsel)
{
OPAMP_CSR(base) &= ~(OPAMP_CSR_CALSEL_MASK << OPAMP_CSR_CALSEL_SHIFT);
OPAMP_CSR(base) |= calsel << OPAMP_CSR_CALSEL_SHIFT;
}
-bool opamp_read_outcal(uint32_t base)
-{
- return (OPAMP_CSR(base) >> OPAMP_CSR_OUTCAL_SHIFT)
- & OPAMP_CSR_OUTCAL_MASK;
-}
-
-void opamp_tcm_enable(uint32_t base)
-{
- OPAMP_CSR(base) |= OPAMP_CSR_TCM_EN;
-}
-
-void opamp_tcm_disable(uint32_t base)
-{
- OPAMP_CSR(base) &= ~OPAMP_CSR_TCM_EN;
-}
-
void opamp_force_vp_enable(uint32_t base)
{
OPAMP_CSR(base) |= OPAMP_CSR_FORCE_VP;
@@ -89,7 +64,6 @@ void opamp_cal_disable(uint32_t base)
OPAMP_CSR(base) &= ~OPAMP_CSR_CALON;
}
-
void opamp_trimoffsetn_set(uint32_t base, uint32_t trim)
{
OPAMP_CSR(base) &= ~(OPAMP_CSR_TRIMOFFSETN_MASK
@@ -114,7 +88,6 @@ void opamp_user_trim_disable(uint32_t base)
OPAMP_CSR(base) &= ~OPAMP_CSR_USER_TRIM;
}
-
void opamp_pga_gain_select(uint32_t base, uint32_t gain)
{
OPAMP_CSR(base) &= ~(OPAMP_CSR_PGA_GAIN_MASK
@@ -122,7 +95,6 @@ void opamp_pga_gain_select(uint32_t base, uint32_t gain)
OPAMP_CSR(base) |= gain << OPAMP_CSR_PGA_GAIN_SHIFT;
}
-
void opamp_vp_select(uint32_t base, uint32_t vp)
{
OPAMP_CSR(base) &= ~(OPAMP_CSR_VP_SEL_MASK
@@ -137,20 +109,4 @@ void opamp_vm_select(uint32_t base, uint32_t vm)
OPAMP_CSR(base) |= vm << OPAMP_CSR_VM_SEL_SHIFT;
}
-
-void opamp_vps_select(uint32_t base, uint32_t vps)
-{
- OPAMP_CSR(base) &= ~(OPAMP_CSR_VPS_SEL_MASK
- << OPAMP_CSR_VPS_SEL_SHIFT);
- OPAMP_CSR(base) |= vps << OPAMP_CSR_VPS_SEL_SHIFT;
-}
-
-void opamp_vms_select(uint32_t base, uint32_t vms)
-{
- OPAMP_CSR(base) &= ~(OPAMP_CSR_VMS_SEL_MASK
- << OPAMP_CSR_VMS_SEL_SHIFT);
- OPAMP_CSR(base) |= vms << OPAMP_CSR_VMS_SEL_SHIFT;
-}
-
-/**@}*/
-
+/*@}*/
diff --git a/lib/stm32/common/opamp_common_v1.c b/lib/stm32/common/opamp_common_v1.c
new file mode 100644
index 00000000..3f665d4c
--- /dev/null
+++ b/lib/stm32/common/opamp_common_v1.c
@@ -0,0 +1,56 @@
+/** @addtogroup opamp_file OPAMP peripheral API
+ * @ingroup peripheral_apis
+ */
+
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see .
+ */
+
+/**@{*/
+
+#include
+
+bool opamp_read_outcal(uint32_t base)
+{
+ return (OPAMP_CSR(base) >> OPAMP_CSR_OUTCAL_SHIFT)
+ & OPAMP_CSR_OUTCAL_MASK;
+}
+
+void opamp_tcm_enable(uint32_t base)
+{
+ OPAMP_CSR(base) |= OPAMP_CSR_TCM_EN;
+}
+
+void opamp_tcm_disable(uint32_t base)
+{
+ OPAMP_CSR(base) &= ~OPAMP_CSR_TCM_EN;
+}
+
+void opamp_vps_select(uint32_t base, uint32_t vps)
+{
+ OPAMP_CSR(base) &= ~(OPAMP_CSR_VPS_SEL_MASK
+ << OPAMP_CSR_VPS_SEL_SHIFT);
+ OPAMP_CSR(base) |= vps << OPAMP_CSR_VPS_SEL_SHIFT;
+}
+
+void opamp_vms_select(uint32_t base, uint32_t vms)
+{
+ OPAMP_CSR(base) &= ~(OPAMP_CSR_VMS_SEL_MASK
+ << OPAMP_CSR_VMS_SEL_SHIFT);
+ OPAMP_CSR(base) |= vms << OPAMP_CSR_VMS_SEL_SHIFT;
+}
+
+/*@}*/
diff --git a/lib/stm32/common/opamp_common_v2.c b/lib/stm32/common/opamp_common_v2.c
new file mode 100644
index 00000000..3641edf1
--- /dev/null
+++ b/lib/stm32/common/opamp_common_v2.c
@@ -0,0 +1,52 @@
+/** @addtogroup opamp_file OPAMP peripheral API
+ * @ingroup peripheral_apis
+ */
+
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see .
+ */
+
+/**@{*/
+
+#include
+
+bool opamp_read_calout(uint32_t base)
+{
+ return (OPAMP_CSR(base) >> OPAMP_CSR_CALOUT_SHIFT) &
+ OPAMP_CSR_CALOUT_MASK;
+}
+
+void opamp_high_speed_mode_enable(uint32_t base)
+{
+ OPAMP_CSR(base) |= OPAMP_CSR_OPAHSM;
+}
+
+void opamp_high_speed_mode_disable(uint32_t base)
+{
+ OPAMP_CSR(base) &= ~OPAMP_CSR_OPAHSM;
+}
+
+void opamp_output_set_internal(uint32_t base)
+{
+ OPAMP_CSR(base) |= OPAMP_CSR_OPAINTOEN;
+}
+
+void opamp_output_set_external(uint32_t base)
+{
+ OPAMP_CSR(base) &= ~OPAMP_CSR_OPAINTOEN;
+}
+
+/*@}*/
diff --git a/lib/stm32/f3/Makefile b/lib/stm32/f3/Makefile
index a88d277d..6d6df239 100644
--- a/lib/stm32/f3/Makefile
+++ b/lib/stm32/f3/Makefile
@@ -46,7 +46,7 @@ OBJS += flash.o flash_common_all.o flash_common_f.o
OBJS += gpio_common_all.o gpio_common_f0234.o
OBJS += i2c_common_v2.o
OBJS += iwdg_common_all.o
-OBJS += opamp.o
+OBJS += opamp_common_all.o opamp_common_v1.o
OBJS += pwr_common_v1.o
OBJS += rcc.o rcc_common_all.o
OBJS += spi_common_all.o spi_common_v2.o
diff --git a/lib/stm32/g4/Makefile b/lib/stm32/g4/Makefile
index c5e4f594..cfdc585a 100644
--- a/lib/stm32/g4/Makefile
+++ b/lib/stm32/g4/Makefile
@@ -41,6 +41,7 @@ OBJS += dma_common_l1f013.o
OBJS += dmamux.o
OBJS += flash.o flash_common_all.o flash_common_f.o flash_common_idcache.o
OBJS += gpio_common_all.o gpio_common_f0234.o
+OBJS += opamp_common_all.o opamp_common_v2.o
OBJS += pwr.o
OBJS += rcc.o rcc_common_all.o
OBJS += timer_common_all.o timer_common_f0234.o