AN4759

STM32L4 API and tamper detection application example

3STM32L4 API and tamper detection application example

3.1STM32CubeL4 firmware libraries for tamper detection

The RTC comes with:

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-Eval\Examples\RTC

3.2X-CUBE-RTC for tamper detection

X-CUBE-RTC shows an example where a real-time clock must be maintained alive while using as low power

as possible (STM32CubeExpansion_AN4759_RTC_STM32L_Vx.y.z\Projects\STM32L476G_EVAL\RTC_ TamperVBATT).

The firmware implements:

a few hints in order to ensure a low current consumption

a display in the debugger (date and time, timestamp date and time) and on LEDs (power-up events, reset events, tamper events, error)

a display on the TFT screen, using STemWin library

tampering detection capability, both when the main power supply (VDD) is present and when the RTC is battery powered

ability to timestamp the tampering event

ability to erase the backup registers that may contain sensitive data upon tamper detection

AN4759 - Rev 8

page 35/60

 

 

AN4759

X-CUBE-RTC for tamper detection

The application flowchart is shown in the figure below.

Figure 18. Application example flowchart

 

 

 

Initialization

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Configure RTC

 

 

 

 

Tamper interrupt

clock and

 

 

 

 

handler

prescalers

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Green LED on

 

Tamper

 

 

 

 

 

 

 

 

Green LED on

(LED1)

 

 

 

 

 

detection during

 

Yes

 

 

 

 

 

 

(LED1)

 

 

 

power-down ?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

No

Rise flag for main

program

Configure tamper detection on PA0

Return to main program

Always executed (upon system reset)

Executed upon power-on reset

Executed upon back-up domain reset

Backup

 

 

 

 

 

 

 

Write backup

 

 

registers

 

 

 

Yes

 

 

 

 

 

 

 

 

 

register

 

 

erased?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

No

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Power-oN

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Red LED on

 

 

reset

 

 

 

 

 

 

 

 

 

 

 

Yes

 

 

 

 

 

 

 

 

 

 

(LED2)

 

 

occurred ?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

No

End of initialization

Display date and time

Power-oN reset occurred ?

Yes

Configure

calendar

No

 

 

 

 

 

 

 

 

 

 

 

 

Enter low-power

 

 

 

 

 

 

No

 

 

 

 

 

Display timestamp date and time

 

mode (Stop 2)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Check backup registers are cleared

 

 

Tamper

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

detected in IT

 

Yes

 

 

Deactivate tamper

 

 

 

 

 

 

 

?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

AN4759 - Rev 8

page 36/60