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
- 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
-
Open STM32CubeIDE
-
Create a new project using the NUCLEO-L496ZG board
-
Give a name to the project
-
Initialize all peripherals with their default settings
-
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.
-
Generate Code
Project -> Generate Code
-
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 2 3 4 5 6 7 8 9 10 11 |
/* USER CODE BEGIN 2 */ // Check if Data stored in BackUp register1: No Need to write it and then turn LED1 // Read the Back Up Register 1 Data if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0xBEBE) { // Write Back Up Register 1 Data HAL_PWR_EnableBkUpAccess(); // Writes a data in a RTC Backup data Register 1 HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xBEBE); <code>HAL_PWR_DisableBkUpAccess();</code> //Turn LED2 HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET); } else { // data register already written so turn LED3 HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_SET); } /* USER CODE END 2 */ |
-
Build the project, enter debug mode and run the code
After the project is built, enter a debug session in order to flash the code.
We can now terminate the debug session by clicking on the red square icon.
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.