Skip to main content

OPAMP MCAL Module

Supported STM32 families: H5 · H7
Not supported: F4, F7, G4, L4, U3, U5

This module provides an abstraction layer for the operational amplifiers (OPAMPx) of STM32 MCUs: the standalone amplifier, the voltage follower and the programmable gain amplifier (PGA) with the non-inverting and the inverting gain, the routing of the inputs and of the output (pins, DAC, ADC, other analog blocks), the normal and the high-speed mode and the calibration of the input offset. It is a low-level MCAL module: it owns the operational amplifier hardware, the application and the other modules never touch the registers.

The public interface (OpAmp_Port.h, OpAmp_Types.h) is identical for all supported families. The implementation for a family lives in its own branch (Dev/<Family> for development, Releases/<Family> for the released versions), so a module or an application written against the interface does not change when the MCU family changes.

This README is the landing page and the specification of the interface (master). The family branches contain their own README with everything that is specific to the family: the instances of the device, their settings and the status of the implementation.

Status: design. No implementation exists yet, the interface below is a proposal. Stories and branches follow.


Intent and purpose​

  • An internal operational amplifier saves a component and a board area in the typical analog front ends of a microcontroller: the amplification of a sensor signal before the ADC (a thermocouple, a shunt, a bridge), the buffering of a high impedance source or of the DAC output, the current sense of a motor or a power converter (PGA directly connected to the ADC), a simple active filter around the amplifier with external parts.
  • The configuration of an operational amplifier is a set of connected choices (which input goes where, which gain, which internal feedback), and the offset of the input stage has to be trimmed for a precise measurement. The user chooses the function (a follower, a PGA with the gain 8, a standalone amplifier), the module chooses the selection bits, enables the amplifier in the right order, waits for the start-up and runs the calibration.
  • The module hides the differences between the families: the number of the amplifiers, the input multiplexers, the gain values, the connection to the ADC and the DAC, the speed modes and the calibration method differ, the interface stays the same.

What the module is not: it does not design the external network around a standalone amplifier, it does not measure the voltage (the Adc module) and it does not generate the signal that is amplified (the Dac module). The pins that it uses are put in the analog mode by the module; their assignment follows from the selected function.


Concept​

An instance is identified by opAmp_PeriphId_t. The function is selected by opAmp_Mode_t:

ModeBehaviour
OPAMP_MODE_STANDALONEThe amplifier with all three terminals on the pins (non-inverting input, inverting input, output); the gain is set by the external parts.
OPAMP_MODE_FOLLOWERThe output is connected to the inverting input inside the chip; the gain is 1, the inverting pin is free for another use.
OPAMP_MODE_PGA_NON_INVERTINGThe internal feedback resistors give the non-inverting gain (OPAMP_GAIN_2 ... the gains of the device).
OPAMP_MODE_PGA_INVERTINGThe internal resistors with the signal on the inverting input give the inverting gain (negative values).

Further items of opAmp_Config_t:

ItemMeaning
Non-inverting input (InputPlus)A pin, or the DAC output where the device has the connection (the DAC then feeds the amplifier directly).
Inverting input (InputMinus)A pin, the internal feedback (PGA, follower).
GainThe gain of the PGA from the list of the device.
Speed modeOPAMP_SPEED_NORMAL or OPAMP_SPEED_HIGH (a better slew rate for a higher consumption).
OutputThe pin, and/or the internal connection to the ADC (the output is converted without the pin).
TrimmingOPAMP_TRIM_FACTORY (the factory values, the default), OPAMP_TRIM_USER (the values found by OpAmp_Set_Calibration or given by the application).
LockThe configuration is frozen until the next reset, where the device has it.

Rules:

  1. OpAmp_Init configures the instance and leaves it disabled; the output of a disabled amplifier is in the high impedance state. OpAmp_Set_PeriphActive enables it and waits for the start-up time; OpAmp_Get_PeriphState tells OPAMP_STATE_DISABLED, OPAMP_STATE_STARTING, OPAMP_STATE_READY, OPAMP_STATE_CALIBRATING.
  2. The mode, the inputs, the gain and the speed mode are changed on a disabled amplifier (hardware rule of most families); the call on an enabled amplifier returns an error and nothing changes. Where the hardware allows a gain change on the fly the family README says so.
  3. OpAmp_Set_Calibration runs the calibration of the input offset (the amplifier is disconnected from the pins during it, the output is undefined), finds the trimming values for both input pairs of the stage and stores them in the module so that OPAMP_TRIM_USER uses them; OpAmp_Get_Trimming returns them (the application can store them in the flash and write them back by OpAmp_Set_Trimming after the next reset).
  4. The input signal of a PGA must stay below the supply divided by the gain, otherwise the output saturates; the module cannot check it.
  5. All parameters are checked before a register is written; an amplifier of a lock configuration refuses every change.

Supported families​

Every family gets its own story, its own tasks and its own branches Dev/<Family> and Releases/<Family> cut from this master.

FamilyBranchAmplifiersStatus
STM32H5Dev/STM32H5OPAMP1 on STM32H503 and the H5E / H5F lines: rail-to-rail inputs, standalone, follower, PGA with the non-inverting gain 2 / 4 / 8 / 16 and the inverting gain -1 ... -15, factory and user trimming, high-speed mode, the non-inverting input from the pin or the DAC output, the output to the ADCplanned (first)
STM32G4Dev/STM32G4OPAMP1 ... OPAMP6, PGA with the higher gains, the connection to the ADC and the filtering inputsplanned
STM32L4Dev/STM32L4OPAMP1, OPAMP2, PGAplanned
STM32H7Dev/STM32H7OPAMP1, OPAMP2planned
STM32U5Dev/STM32U5OPAMP1 (and further instances on the lines that have them)planned
STM32U3Dev/STM32U3device dependentplanned
STM32F4, STM32F7Dev/STM32F4, Dev/STM32F7no operational amplifier on the device - the module is a stub that returns OPAMP_REQUEST_ERRORplanned

A function without a meaning in a family stays in the interface, returns OPAMP_REQUEST_ERROR and suppresses the unused parameters (for example a gain or a mode that the device does not have, the second amplifier of a device that has one, the lock on a family without it).


Public interface in short (proposal)​

GroupFunctions
Module managementOpAmp_Get_ModuleVersion, OpAmp_Init, OpAmp_Deinit, OpAmp_Task, OpAmp_Get_DefaultConfig
ControlOpAmp_Set_PeriphActive, OpAmp_Set_PeriphInactive, OpAmp_Get_PeriphState
FunctionOpAmp_Set_Mode / Get, OpAmp_Set_Gain / Get, OpAmp_Set_InputPlus / Get, OpAmp_Set_InputMinus / Get, OpAmp_Set_SpeedMode / Get, OpAmp_Set_AdcConnection / Get
CalibrationOpAmp_Set_Calibration, OpAmp_Get_Trimming, OpAmp_Set_Trimming, OpAmp_Set_TrimmingSource
LockOpAmp_Set_Lock, OpAmp_Get_LockState
PinsOpAmp_InitInputPlusGpio, OpAmp_InitInputMinusGpio, OpAmp_InitOutputGpio
opAmp_ModuleVersion_t OpAmp_Get_ModuleVersion ( void );

opAmp_RequestState_t OpAmp_Init ( const opAmp_Config_t * const opAmpConfig );
opAmp_RequestState_t OpAmp_Deinit ( opAmp_PeriphId_t periphId );
void OpAmp_Task ( void );
opAmp_RequestState_t OpAmp_Get_DefaultConfig ( opAmp_Config_t * const opAmpConfig );

opAmp_RequestState_t OpAmp_Set_PeriphActive ( opAmp_PeriphId_t periphId );
opAmp_RequestState_t OpAmp_Set_PeriphInactive ( opAmp_PeriphId_t periphId );
opAmp_RequestState_t OpAmp_Get_PeriphState ( opAmp_PeriphId_t periphId, opAmp_State_t * const opAmpState );

opAmp_RequestState_t OpAmp_Set_Mode ( opAmp_PeriphId_t periphId, opAmp_Mode_t mode );
opAmp_RequestState_t OpAmp_Set_Gain ( opAmp_PeriphId_t periphId, opAmp_Gain_t gain );
opAmp_RequestState_t OpAmp_Set_InputPlus ( opAmp_PeriphId_t periphId, opAmp_InputPlus_t inputPlus );
opAmp_RequestState_t OpAmp_Set_InputMinus ( opAmp_PeriphId_t periphId, opAmp_InputMinus_t inputMinus );
opAmp_RequestState_t OpAmp_Set_SpeedMode ( opAmp_PeriphId_t periphId, opAmp_SpeedMode_t speedMode );

opAmp_RequestState_t OpAmp_Set_Calibration ( opAmp_PeriphId_t periphId );
opAmp_RequestState_t OpAmp_Get_Trimming ( opAmp_PeriphId_t periphId, opAmp_Trimming_t * const trimming );
opAmp_RequestState_t OpAmp_Set_Trimming ( opAmp_PeriphId_t periphId, const opAmp_Trimming_t * const trimming );

opAmp_RequestState_t OpAmp_Set_Lock ( opAmp_PeriphId_t periphId );

Every function returns OPAMP_REQUEST_OK / OPAMP_REQUEST_ERROR and checks its parameters and the state of the module before it touches a register.

TypeMeaning
opAmp_Config_tInstance, mode, gain, inputs, speed mode, ADC connection, trimming source, output pin, lock request
opAmp_Gain_tOPAMP_GAIN_2 ... for the non-inverting PGA, OPAMP_GAIN_MINUS_1 ... for the inverting one - only the gains of the device are defined
opAmp_Trimming_tThe trimming values of the two input pairs of the stage (opaque numbers of the family)
opAmp_State_tOPAMP_STATE_DISABLED, OPAMP_STATE_STARTING, OPAMP_STATE_READY, OPAMP_STATE_CALIBRATING

Usage​

Sensor amplifier: PGA with the gain 8, the output converted by the ADC​

#include "OpAmp_Port.h"

opAmp_Config_t opAmpConfig;

(void)OpAmp_Get_DefaultConfig( &opAmpConfig );

opAmpConfig.PeriphId = OPAMP_PERIPH_1;
opAmpConfig.Mode = OPAMP_MODE_PGA_NON_INVERTING;
opAmpConfig.Gain = OPAMP_GAIN_8;
opAmpConfig.InputPlus = OPAMP_INPUT_PLUS_OPAMP1_PIN0; /* the sensor signal */
opAmpConfig.AdcConnection = OPAMP_FUNCTION_ACTIVE; /* the output goes to the ADC */

(void)OpAmp_Init( &opAmpConfig );
(void)OpAmp_Set_PeriphActive( OPAMP_PERIPH_1 );

/* the Adc module converts the internal channel of the amplifier output */

Buffer for the DAC output with the offset calibration​

opAmpConfig.Mode = OPAMP_MODE_FOLLOWER;
opAmpConfig.InputPlus = OPAMP_INPUT_PLUS_DAC_OUT1; /* the DAC output feeds the amplifier */
(void)OpAmp_Init( &opAmpConfig );

(void)OpAmp_Set_Calibration( OPAMP_PERIPH_1 ); /* user trimming, the values stay in the module */
(void)OpAmp_Set_PeriphActive( OPAMP_PERIPH_1 );

Behaviour and limits​

  • Disabled output. A disabled amplifier has a high impedance output; an enabled one drives the pin even when the rest of the firmware is not running - the pin must not be loaded by another driver.
  • Offset. The factory trimming gives the offset of the order of 10 mV, the calibration of the application improves it to the order of 1 mV (the values are in the datasheet). The calibration depends on the temperature and the supply; repeat it when the conditions change. The calibration disconnects the pins - do not run it while the amplifier is a part of an active measurement.
  • Gain and saturation. The output range is limited by the supply (rail-to-rail); with the gain G the input must stay below VDDA / G. The bandwidth (gain-bandwidth product) falls with the gain; the high-speed mode improves the slew rate.
  • Start-up. The amplifier needs a start-up time after the enable; the module waits for it (the state OPAMP_STATE_STARTING), the output is valid in OPAMP_STATE_READY only.
  • Not part of the first version: the dynamic switching between the input pairs (the timer controlled multiplexer of some families), the filtering inputs of the PGA of STM32G4 beyond the plain selection, the low-power mode in the Stop mode.

Dependencies​

  • Rcc - peripheral clock and reset
  • Gpio - input and output pins in the analog mode
  • Dac - the DAC output as the non-inverting input (configured by its own module)
  • Adc - the converter of the internal output channel (configured by its own module)
  • RAL (Register Abstraction Layer) - used internally to access the registers

All mandatory RCC and GPIO configurations are handled internally.


Testing​

  • Unit tests (host, Unity + CMock + RegMem): the combinations of the mode, the inputs and the gain (accepted and rejected), the order of the writes at the enable and at the disable, the calibration algorithm against a simulated comparison flag (the binary search of the trimming values, the failing calibration), the trimming storage, the rejected changes of an enabled and of a locked amplifier.
  • Integration tests (target, probe-rs): the gain error of every PGA setting measured by the ADC with a DAC stimulus, the offset before and after the calibration, the follower with the DAC input, the high-speed mode slew rate.

Interface conventions​

The module follows the common rules of the MCAL layer, so that it behaves like the other modules:

  • Lists instead of free members. Everything that the hardware restricts (the pins, the kernel clock sources, the DMA channels, the triggers and the other selectable sources) is a typedef enum list with exactly the items that are valid for the device line; a pin is an item OPAMP_<SIGNAL>_PIN_<INSTANCE>_P<port><pin> and there is an *_UNUSED item. A choice that is not in the list cannot be written.
  • Own peripheral only. The module calls only the registers / LL functions of its own peripheral. The clocks, the interrupts, the pins, the DMA and the EXTI lines go through the API of the Rcc, Nvic, Gpio, Gpdma and Exti modules; a missing function is added to that module, it is not bypassed.
  • Init verifies itself. OpAmp_Init checks the result at the end; when it is not as required, the module is deinitialized and an error is returned. The waiting for the hardware is a loop on the register state with a timeout, never a blind delay.
  • Transfer modes. Where the module moves data, the POLL (served by OpAmp_Task), ISR and DMA modes have the same configuration structure, the same request and the same callbacks (error, half transfer, transfer complete).
  • Flags and states are enums of the module (opAmp_FunctionState_t, opAmp_FlagState_t), the limits and the constants are named, there are no magic numbers in the interface.
  • Unsupported functions stay. A function without a meaning in a family keeps its signature, suppresses the unused parameters and returns OPAMP_REQUEST_ERROR.
  • Documentation. Every function and type has a Doxygen description, the main header has the \defgroup.

Repository layout and branches​

The master holds this README and the license. The family branches (Dev/<Family>, Releases/<Family>) hold:

PathContent
README.mdREADME of the family: the instances of the device, specifics, errata, status
OpAmp_Port.h, OpAmp_Types.hPublic interface (identical in all families)
OpAmp*.c, OpAmp.hImplementation of the family
CMakeLists.txtLibrary OpAmp_Lib of the EmBi platform
Tests/UnitTestsUnit tests (Unity + CMock + RegMem)
Tests/IntegrationTestsIntegration tests on the target (probe-rs)

A change of the public interface is made in all families.


CMake integration​

target_link_libraries( User_Lib PRIVATE OpAmp_Lib )
  1. Link OpAmp_Lib to your CMake library.
  2. Include OpAmp_Port.h in your project.
  3. Configure the module as needed for your hardware.

License​

This project is licensed under the Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0).

You are free to use, modify, and share this work for non-commercial purposes, provided appropriate credit is given.

See LICENSE.md for full terms or visit creativecommons.org/licenses/by-nc/4.0.


Authors​

Contributions are welcome! Please open a pull request.