应用笔记 · 2023年4月4日

STM32 BKP register read/write accesss demo, CubeMX and HAL driver

1. Introduction

Some of the STM32 microcontrollers have backup registers. These registers can be written/read and protected and have the option of being preserved in VBAT mode when the VDD domain is powered off. In this article we will learn how write, read and protect these registers.

2. Prerequisites

  • Hardware:
    • Micro USB cable used to power the Nucleo board from a host machine and to load the code into the STM32.
    • Nucleo-L496

Graphical user interface, application  Description automatically generated

  • Software: STM32CubeIDE

 

3. Theory

In this Article we will be using an STM32L496 included on the Nucleo-L496 Nucleo. The STM32L496 includes 32 backup registers (each 32-bit) that can be written/read and protected and are included in the backup domain. This domain also includes the RTC and remains powered by VBAT when VDD power is switched off (provided that VBAT remains powered). After a system reset, the backup and RTC registers are protected against parasitic writes. We will learn how to write, read and protect the backup registers using the STM32Cube HAL drivers.

In this code example, it reads the first backup data register to see if it was written previously and if not, it will unprotect the backup domain and write to it. Once the code is executed the first time, we will do a reset of the STM32 using the button connected to the reset input (NRST). Since the backup domain is preserved by hardware through a reset, the backup register value will remain unchanged.

4. Steps

  1. Open STM32CubeIDE

  2. Create a new project using the NUCLEO-L496ZG board

Graphical user interface, text, application  Description automatically generated

  1. Give a name to the project

Graphical user interface, text, application, email  Description automatically generated

  1. Initialize all peripherals with their default settings

Graphical user interface, text, application  Description automatically generated

  1. Enable the RTC

The BackUp Registers are part of the RTC peripheral so we will need to enable the RTC to be able to access them. In Pinout & Configuration Tab, go to Timers, and select RTC and then in the RTC Mode and Configuration. Activate the clock source to enable the RTC.
Graphical user interface, text, application  Description automatically generated

  1. Generate Code

Project -> Generate Code
Graphical user interface, text, application  Description automatically generated

  1. Add code

We will add the code that checks if the first backup register was already written. If it was previously written with the correct data we read back (0xBEBE) we will turn on LED3 and if not we will un-protect the backup register and write a data to it, protect it and then turn on LED2.

We need to enable the RTC clock as the Backup Registers are part of the RTC. This is done on RTC Configuration Function.

To read a Backup Register use the HAL function call, HAL_RTCEx_BKUPRead.
To be able to write to the Backup register:

  • Enable the PWR Clock (this is already done in the HAL Init function)
  • Enable access to the backup domain
  • Make a HAL function call to write the data: HAL_RTCEx_BKUPWrite
  • Re-Protect the Backup domain

Here is the code to add in main.c in the user code section 2:

  1. Build the project, enter debug mode and run the code

Graphical user interface, text, application, Word  Description automatically generated

After the project is built, enter a debug session in order to flash the code.
Graphical user interface, text, application  Description automatically generated

We can now terminate the debug session by clicking on the red square icon.
Graphical user interface, text, application  Description automatically generated

This will exit the debug session and then the code will be executed. You should see the blue LED2 that is turned on indicating that the check of the backup register 1 did not pass and that we then wrote a data to it.
Now if you press on the black reset button of the Nulceo, this will re-execute the code but this time the red LED3 will be turned on indicating that the check of the backup register passed because it was previously written.

Now if you unplug and re-plug the Nucleo board, the code will execute again and this time the blue LED2 will be turned on because the backup register was not preserved as the backup domain lost its power (there was no power provided to the VBAT pin). To be able to preserve the backup registers through a power cycle, VBAT must remain powered when VDD is removed, this is called the VBAT mode.