STM32 gotchas
116. RTC loses half a second per chip reset (Cube bug, fixed)

This issue has been reported/discussed on the STM32 forum many times during the last years; but this thread probably sums it the best, and also contains the report of the fix (below "More answers", if it's not there in the 10th post thanks to ST using salesforce instead of proper forum software, reload the page).

In the unmistaken logic of Cube/HAL/CubeMX, CubeMX-generated code unconditionally calls HAL_RTC_Init(), which in turn enters the INIT mode of RTC and rewrites its basic setup registers (the clock dividers). As the values written are always the same, and the date/time registers are not overwritten in HAL_RTC_Init(), the RTC seemingly continues to keep time, but the effect of entering INIT mode is, that the subseconds register gets cleared.

The net effect is, that with resets (including power cycles) occuring at random times, in average, half a second is lost, i.e. time kept by RTC lags half a second per every reset/powerup experienced.

To unaware user, this can mimic LSE frequency deviation, the user may mistakenly go the path of attempting to correct it.

In 2022, ST posted a fix to CubeF4 (which is to be propagated to other families' Cubes), where in HAL_RTC_Init(), the RTC INIT mode is entered only if INITS flag is not set. However, this may be not the best solution either, given the INITS flag only indicates that the year field in RTC_DR is different from 0, and not all applications wish to set date. It may be a better idea to use RCC_BDCR.RTCEN as basis for this test (or equivalent in STM32 families where the register controlling backup-domain clocks are named differently, e.g. RCC_CSR in 'L0).