STM32CubeL4 firmware libraries for smooth calibration
- A firmware driver API abstracting the timer features for the end-user
Refer to stm32l4xx_hal_tim.c and stm32l4xx_hal_tim_ex.c files in \STM32Cube_FW_L4_Vx.y.z\Drivers\STM32L4xx_HAL_Driver\
- A set of example projects in \STM32Cube_FW_L4_Vx.y.z\Projects\STM32L476RG-Nucleo\Examples\TIM
- A firmware driver API abstracting the RTC features for the end-user
Refer to stm32l4xx_hal_rtc.c and stm32l4xx_hal_rtc_ex.c files in \STM32Cube_FW_L4_Vx.y.z\Drivers\STM32L4xx_HAL_Driver\
- A set of example projects in \STM32Cube_FW_L4_Vx.y.z\Projects\STM32L476RG-Nucleo\Examples\RTC
X-CUBE-RTC for smooth calibration
X-CUBE-RTC shows an implementation of the smooth digital calibration feature of the RTC (using also the GPTIMER HAL API).
In this example, the RTC_CLK is smoothly calibrated based on an external 1 Hz reference (STM32CubeExpansion_AN4759_RTC_STM32L_Vx.y.z\Projects\STM32L476RG_Nucleo\RTC_SmoothCalib).
- A GPTIMER (general-purpose timer) in Master mode using channel 1 in output compare mode (and using an external trigger clock)
- A GPTIMER in Master mode using channel 1 in input capture mode and channel 2 in output compare mode
This timer is also configured to use the LSE clock thanks to the TIM option register.
- The RTC with the CALR register programming
- GPIOs to connect the input 1 Hz reference clock and the “corrected” RTC_OUT_CALIB clock
Smooth calibration application example (STM32L4)
Hardware setup

- Connect PD2 to a signal generator driving 1 Hz clock with 3.3 V amplitude. It is advised to control the generated frequency at few ppm.
- Connect PB2 to an oscilloscope (the accuracy of the frequency measurement is ideally in the range of 1 ppm).
Software setup
Open the project under the user favorite IDE. For IAR Embedded Workbench, open the RTC_SmothCalib.eww file. For MDK-ARM, open the RTC_SmoothCalib.uvprojx file. For SW4STM32, open the SW4STM32 toolchain, browse to the SW4STM32 workspace directory, select and import the project RTC_SmoothCalib.
Compile and launch the debug session. It loads the program in the internal Flash memory and executes it.
After a maximum of 64 seconds, make a frequency measurement of the CALIB_OUT clock on PB2. The user gets a frequency accurate at 1 ppm compared to the 1 Hz reference clock sent on PD2.
The user can then modify the PD2 clock by few ppm, wait 64 seconds, and check that the PB2 clock has been changed accordingly.
Under debugger, the user can monitor the evolution of the CALR register fields (CALP and CALM).
Smooth calibration application principle

- A 1 Hz reference clock is connected to TIM3 through the PD2 GPIO.
- TIM3 is configured to generate a rising edge after 32 x 1 Hz reference rising edges. This gives an event every 32 s.
- TIM2 is configured to increment its counter on CLK_RTC, to get the counter value on the rising edge generated by TIM3, and to store it in TIM2_CCR1 register. Then TIM2 counter is reset.
- The Cortex-M4 core gets the TIM2_CCR1 value, compares it with the number of CLK_RTC cycles expected in 32 s and processes the comparison results to update the CALP and CALM[8:0] in RTC_CALR.
The calibration is continuous.
Run time observations
- Modify the 1 Hz reference clock (for instance by forcing 1.0001 Hz).
- Wait 2 x 32 s.
- Measure the output frequency. Depending on the accuracy of the generator and the measurement device used, the user can see around 1 ppm accuracy.
- CALP and CALM[8:0] in RTC_CALR
- TIM2_CCR1 register (contains the number of CLK_RTC cycles within a 32 s window)
Porting suggestions
The software example expects that the TIM2 is more than 20 bit, 32 bit in case of L476RB. With products featuring only 16-bit timers it is recommended to keep track of 16-bit timer overflows using the software and perform the calibration with 16-th overflow to compensate for the missing 4 bits.
STM32L0 API and tampering detection application example
STM32CubeL0 firmware libraries
- A firmware driver API abstracting RTC features for the end-user
Refer to stm32l0xx_hal_rtc.c and stm32l0xx_hal_rtc_ex.c files in \STM32Cube_FW_L0_Vx.y.z\Drivers\STM32L0xx_HAL_Driver\
- A set of example projects in \STM32Cube_FW_L0_Vx.y.z\Projects\STM32L053R8-Nucleo\Examples\RTC
X-CUBE-RTC for tamper detection
X-CUBE-RTC shows an example where a real-time clock must be maintained alive while using as less power as possible (STM32CubeExpansion_AN4759_RTC_STM32L_Vx.y.z\Projects\STM32L053R8-Nucleo\RTC_Tamper.
- A few hints order to ensure a low current consumption
- A display in the debugger (date and time, timestamp date and time) and on a single LED (power-up events, reset events, tamper events, error)
- Tampering detection capability
- The ability to timestamp the tampering event
- The ability to erase the backup registers that may contain sensitive data upon tamper detection
The application flowchart is shown in Figure 18 Application example flowchart.
Tamper detection application example (STM32L0)
Hardware setup


Software setup
Open the project under the user favorite IDE (integrated development environment). For IAR Embedded Workbench, open the project.eww file. For Keil MDK-ARM, open the project.uvprojx file, compile and launch the debug session. For SW4STM32, open the SW4STM32 toolchain, browse to the SW4STM32 workspace directory, select and import the project RTC_Tamper.
Compile and launch the debug session. This loads the program in the internal Flash memory and executes it.
- aShowTime
- aShowDate
- aShowTimeStampTime
- aShowTimeStampDate
- LED1_Green_Tamper_event_detected
- LED2_Red_Power_On_Reset_occurred
- LED3_Red_Error
- LED4_Blue_Reset_occurred
The same observation can be done using MDK-ARM development tools. To open the Watch1 window, do View\Watch Windows\Watch1.
LED meaning
uint32_t LED1_Green_Tamper_event_detected: set when tamper1 event is detected (LED1 equivalent)uint32_t LED2_Red_Power_On_Reset_occurred: set when power-on reset is detected (LED2 equivalent)uint32_t LED3_Red_Error: set in errors cases (LED3 equivalent)uint32_t LED4_Blue_Reset_occurred: set when reset is detected (B2 black push button, LED4 equivalent)
The user can observe these private variables using the live watch feature in the debugger, or on LED LD2 and CN5 pin 6 (SCK/D13). Regarding the LED LD2 behavior, see details in main.c file.
Tampering detection during normal operation
Step 1
| Meaning | Status | LD2 blink |
|---|---|---|
| LED1_Green_Tamper_event_detected | 0 | OFF |
| LED2_Red_Power_On_Reset_occurred | 1 | ON |
| LED3_Red_Error | 0 | OFF |
| LED4_Blue_Reset_occurred | 1 | ON |
Step 2
| Meaning | Status | LD2 blink |
|---|---|---|
| LED1_Green_Tamper_event_detected | 1 | ON |
| LED2_Red_Power_On_Reset_occurred | 1 | ON |
| LED3_Red_Error | 0 | OFF |
| LED4_Blue_Reset_occurred | 1 | ON |
Step 3
| Meaning | Status | LD2 blink |
|---|---|---|
| LED1_Green_Tamper_event_detected | 0 | OFF |
| LED2_Red_Power_On_Reset_occurred | 0 | OFF |
| LED3_Red_Error | 0 | OFF |
| LED4_Blue_Reset_occurred | 1 | ON |
Step 4
| Meaning | Status | LD2 blink |
|---|---|---|
| LED1_Green_Tamper_event_detected | 1 | ON |
| LED2_Red_Power_On_Reset_occurred | 0 | OFF |
| LED3_Red_Error | 0 | OFF |
| LED4_Blue_Reset_occurred | 1 | ON |