STM32G4: Using PC13 for calibrating RTC – how bad can it be, really?
Hello fellow STM32 users.
Only after making a prototype where I use PC13 for calibrating the RTC, I stumbled upon the PC13 issue. Or maybe I had seen it before, and forgotten about it.
For those unfamiliar with this issue, using PC13 for anything I/O related may affect the RTC clock frequency, in case you use an external RTC crystal (LSE oscillator). This is copied from the STM32G4 errata sheet:

But how bad can it be, really? After (re)discovering this, I decided to put it to the test. I made a firmware version where I could select which I/O port to use for calibration. I use the 1 Hz calibration output, and have a frequency counter measure the average pulse period over 32 consecutive pulses. The frequency counter uses an external GPSDO as frequency reference, so the result should be pretty accurate.
Using PC13 for the 1 Hz calibration signal, I measured a 243.7 ppm deviation. Pretty big, actually, but still possible to compensate for in the RTC_CALR register.
However, when I used PB2 for the exact same purpose, I measured a 182.9 ppm deviation. Still significant, and I probably need to adjust the crystal capacitors a bit. But also a big difference from the PC13 measurement.
Then I entered -256 into RTC_CALR, to calibrate the RTC based on the PC13 measurement. Measured once more to verify (using PC13) and got -0.5 ppm. That's fine.
After this, I again turned on the 1 Hz output on the PB2 port, and made a measurement. Not surprisingly, the result was -61.4 ppm, which is very close to the difference between the first two measurements. (243.7 - 182.9 = 60.8. (-0.5) - (-61.4) = 60.9. Very close indeed.)
I imagine that using the 512 Hz output for calibrating, would make the difference even bigger. I haven't made any experiments with this, though, so it is pure guesswork.
My conclusion
On this particular board, with this microcontroller (an STM32G474), the difference between calibrating with PC13 and PB2 (at 1 Hz) is about 61 ppm. Over a year, this amounts to about 32 minutes. In some applications, this discrepancy is unacceptable.
So when using the LSE, it would indeed be wise to not use PC13 at all, or to use it for something that rarely changes level, such as a DIP switch input. And never ever for RTC calibration.
