STM32 gotchas
218.Backup battery discharges, if its measurement in ADC is kept switched on

Except for the 'L0 family and STM32 models in very pin-limited packages, STM32 have a VBAT pin, which supplies the RTC and a couple of related functionalities (LSE oscillator, backup registers/memory, tamper, etc.) Usually, a battery (such as the ubiquitous 2032 coin cell) is connected to this pin, supplying these functionalities while the "main" VDD is switched off.

When VDD is on, an internal switch automatically disconnects the backup domain from VBAT and connects it to VDD, so there's no current consumption from the battery when VDD is on. The backup domain consumption is at around 1μA, so even the 2032 coin cell can provide years of service.

However, users may want to know the state of charge on the battery. For this, the ADC has a dedicated input, where VBAT voltage can be measured. As battery voltage may be higher than the ADC reference (VREF+), VBAT is not connected directly to the given ADC input, instead, it is connected to a resistive divider, so the input voltage to ADC is 1/2 or 1/3 of VBAT (depending on particular STM32 model).

While resistance of this divider is relatively high - e.g. in STM32F407 it is 50kOhm - it represents a significant current draw from the battery (here around 60μA, which is almost two orders of magnitude higher than the backup-mode current), and it can unpleasantly shorten the service life of the battery.

It is therefore necessary to switch on the VBAT measurement in ADC only momentarily during actually measuring the VBAT voltage, and use this feature sparingly.

The ADC subchapters dealing with VBAT in the STM32 reference manuals warn about this problem, too.