on 2024-12-09 08:00 AM
The goal of this article is to help readers make sense out of the RTC_SSR register, the RTC’s sub second value (SS), and the second fraction equation. This article uses the STM32L476xx MCU and its reference manual RM0351 as an example, but in general, the same methodology can be applied to any other STM32 MCU. Refer to the reference manual for your specific STM32 part number when following along with this article.
The RTC’s calendar time is stored in two separate registers: RTC_TR and RTC_SSR. The RTC time register (RTC_TR) holds the hours, minutes, and seconds information for the current time-of-day. The RTC sub second register (RTC_SSR) holds the sub seconds information for the current time-of-day. The RTC_SSR register is the focus of this article.
The RTC_SSR register contains the 16-bit value SS, which represents the number of sub seconds in the current time-of-day. More specifically, the value of SS equals the number of divisions in one second at the current time-of-day.
As stated in the reference manual, the RTC has a resolution of 1 / (PREDIV_S + 1) seconds. This means that the maximum number of divisions that are possible in one second is PREDIV_S + 1.
So, in simpler terms, we say:
Maximum # of divisions in 1 second = PREDIV_S + 1
PREDIV_S is a 15-bit user-programmed value that we set in the RTC_PRER register.
For this article, we have arbitrarily chosen to use PREDIV_S = 4096. For your application, compute PREDIV_S appropriately based on your application’s requirements and the equations given in the RTC_PRER register bits description.
By our example, if PREDIV_S = 4096, then we can divide 1 second a maximum of 4096 + 1 times.
For example: 1 second = (4097 divisions / 4097 divisions per second)
By this logic, (PREDIV_S + 1) is the maximum number of sub seconds we can have in 1 second. Therefore, SS will typically be a value between 0 and PREDIV_S + 1.
As noted in the reference manual, SS can be a value greater than PREDIV_S only when a shift operation is performed to try and synchronize the RTC with a remote clock.
For more details on RTC synchronization, refer to the reference manual for your specific STM32 part number. For STM32L476xx MCUs, RM0351 section 38.3.10 discusses RTC synchronization in detail.
The Second fraction is an equation given in the reference manual and can be found in the section for the RTC_SSR register.
The Second fraction equation uses the RTC’s sub second value (SS) to compute the number of fractions of a second in the current time-of-day.
In simpler terms, the Second fraction equation converts the RTC’s raw sub second value (SS) into fractions of a second, which is generally a more meaningful way to interpret the value.
Let us look at some examples to see how SS affects the computed value of the second fraction equation, and what the result means in a real-world context.
Compute the second fraction equation using our example value of PREDIV_S = 4096, and a value of SS = 2048 chosen for the purposes of this example.
Second fraction = (PREDIV_S - SS) / (PREDIV_S + 1)
= (4096 - 2048) / (4096 + 1)
= 2048 / 4097
= 0.49987… seconds
This means that the current time-of-day is HH:MM:SS plus 0.49987… seconds, or in plain English, half a second.
Compute the second fraction equation using our example value of PREDIV_S = 4096, and a value of SS = 8192 chosen for the purposes of this example.
Second fraction = (PREDIV_S - SS) / (PREDIV_S + 1)
= (4096 - 8192) / (4096 + 1)
= -4096 / 4097
= -0.9997… seconds
This means that the RTC is ahead of the remote clock by 0.9997… seconds and the correct time of day is HH:MM:SS minus 0.9997… seconds.