In the past few days, I learned about the STM32's BKP, RTC, and PWR. My study of microcontrollers is basically coming to an end, with only the watchdog and flash left to cover.
Now, let's start today's study notes.
(1) PWR#
We will first discuss PWR, as the subsequent BKP and RTC require the PWR clock to enable some PWR functions.
What is PWR?
PWR stands for Power Control, responsible for managing the power supply part within the STM32. It can implement programmable voltage monitoring and low-power mode functions. The Programmable Voltage Detector (PVD) can monitor the VDD power voltage, and when VDD drops below or rises above the PVD threshold, the PVD will trigger an interrupt to execute an emergency shutdown task. (I haven't used the PVD function yet.)
Low-power modes include Sleep, Stop, and Standby modes, which can reduce the STM32's power consumption when the system is idle, extending the device's usage time.
The configuration process is as follows:
The PWR functions in the standard library are quite complete, and it's very simple to operate.
You just need to start the PWR clock first (the sleep mode doesn't even require starting the clock):
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
Then we call the following functions to enter the corresponding modes:
Sleep mode: __WFI();
Stop mode: PWR_EnterSTOPMode();
Standby mode: PWR_EnterSTANDBYMode();
It is important to note that after entering low-power mode, you cannot directly program the device; you need to hold down the reset button while programming and release it in time.
(2) BKP#
The video from Jiang University mentioned TAMPER, but we haven't used it in our code, so we won't discuss it here. We only need to write the BKP storage and ensure it doesn't get lost during power off.
Here is the structure of BKP:
VBAT is the backup power pin, and the reason BKP does not lose data during power off is due to this pin providing power.
The code is as follows:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
Start the clock
PWR_BackupAccessCmd(ENABLE);
Enable backup power
BKP_WriteBackupRegister();
Write data
BKP_ReadBackupRegister();
Read data
(3) RTC#
To use RTC, we first need to understand Unix timestamps and learn some functions from <time.h>.
After understanding these, we can look at RTC.
The structure of RTC is as follows:
Note the clock source here; generally, the LSE external low-speed clock is selected, which is a 32.768kHz crystal oscillator. However, this may not oscillate (it often doesn't), so I used LSI with a crystal frequency of 40kHz during my configuration.
Also, there are a few points to note regarding RTC clock configuration:
Below is the configuration code, which also completes the timestamp conversion:
So, that covers everything about BKP, RTC, and PWR. I'm getting lazier as I write (sad).