STM32-RTC Configuration Commentary
STM32 RTC Clock Configuration because of the many registers involved, such as: BKP, PWR, RTC, messy, here is a summary of the RTC configuration.
RTC Introduction: RTC is a real-time clock is a separate timer, RTC module has a set of continuous counting counters, in the corresponding software configuration, can provide the function of clock, calendar. Modify the value of the counter to reset the current time and date of the system.
Before you configure RTC, you need to know:
BKP:
The registers for the RTC module and the clock configuration system are in the fallback area (i.e. BKP), and the data in the RTC configuration is stored in the BKP fallback area to allow the RTC configuration to remain intact after the system reset or Standby mode is awakened.
PWR:
PWR is the register of the power supply, we need to use a Power control register (PWR_CR), by enabling the PWR_CR of the DBP bit to cancel the backup area BKP write protection.
Rtc:
Consists of a set of programmable counters, divided into two main modules. The first module is the RTC's Prescaler module, which can be programmed to produce up to 1 seconds of RTC Time Reference TR_CLK. The RTC Prescaler module contains a 20-bit programmable divider (RTC Prescaler). If the appropriate allowable bit is set in the RTC_CR register, the RTC generates an interrupt (second interrupt) in each real-time clock (RTC) TR_CLK cycle. The second module is a 32-bit programmable counter that can be initialized to the current system time. The system time is incremented by the TR_CLK cycle and compared to the programmable time stored in the RTC_ALR register, if the corresponding allow bit is set in the RTC_CR control register, an alarm interrupt is generated when the match is matched.
If you want to know more about it, you can open your Stm32 user manual to view it.
The following explains the overall process of configuration:
First step: Open the clock Call library function for the power and fallback interface by setting the Pwren and Bkpen bits of the register RCC_APB1ENR:
Rcc_apb1periphclockcmd (rcc_apb1periph_pwr| rcc_apb1periph_bkp,enable);
Step two: The DBP bit of the Power Control Register (PWR_CR) enables access to the backing register and RTC to call the library function:
Call Library functions:
Pwr_backupaccesscmd (ENABLE);
Step three: Initialize reset BKP Register
Call Library functions:
Bkp_deinit ();
Fourth step: Set RTCCLK, as shown below:
We need to set the RTCCLK to Lseosc this 32.768KHZ crystal oscillator.
Called Library functions:
Rcc_lseconfig (rcc_lse_on);
while (! Rcc_getflagstatus (Rcc_flag_hserdy));//wait to start after Setup
Fifth step: Select the RTC input clock as the LSE clock input and enable RTC, waiting for the RTC and APB clocks to synchronize
Call Library functions:
Rcc_rtcclkconfig (Rcc_rtcclksource_lse);//select LSE for RTC device clock
Rcc_rtcclkcmd (enable);//Enable RTC
Rtc_waitforsynchro ();//wait for synchronization
Sixth step: Configure the RTC clock parameters.
1. Query the Rtoff bit until the Rtoff value changes to ' 1 '
2. Set CNF value is 1, enter configuration mode
3. Write to one or more RTC registers
4. Remove the CNF flag and exit configuration mode
5. Query Rtoff until the Rtoff bit becomes ' 1 ' to confirm that the write operation is complete.
The write operation is performed only when the CNF flag bit is cleared, which requires a minimum of 3 rtcclk cycles.
Good. Follow these steps to configure with library functions:
/* 1. Query the Rtoff bit until the value of Rtoff changes to ' 1 ' */
Rtc_waitforlasttask ();//You can open the library to see the code inside the function, that is, query the value of Rtoff
/*
2. Set CNF value is 1, enter configuration mode
3. Write to one or more RTC registers
4. Remove the CNF flag and exit configuration mode
*/
Rtc_setprescaler (32767); The Prescaler value is configured here, so you can open the library to see the function
Internal code, which contains 2, 3, 4 of the operation described.
Supplement: These bits are used to define the clock frequency of the counter according to the following formula:
FTR_CLK = frtcclk/(prl[19:0]+1), our LSE is
32.768khz=32768hz, from the above formula, we can know the final ftr_clk =1hz so it just happens to be 1 seconds.
Remember that every operation that is done is typically queried rtoff to determine if RTC is updating the data, and if so, wait for him to complete!!!
Rtc_waitforlasttask ();//wait for the update to end
Rtc_itconfig (rtc_it_sec,enable);//Configure seconds Interrupt
Rtc_waitforlasttask ();//wait for the update to end
}
OK, the entire RTC configuration is configured to follow this step, you have a good understanding of it, a lot of open your STM32 manuals and STM32 official library manual, see a few times several registers several functions internal code is more helpful to understand.
Configure the RTC code as a whole:
Voidrtc_configuration (void)
{
/*enablepwrandbkpclocks*/
Rcc_apb1periphclockcmd (RCC_APB1PERIPH_PWR | RCC_APB1PERIPH_BKP,
ENABLE);
/*allowaccesstobkpdomain*/
Pwr_backupaccesscmd (ENABLE);
/*resetbackupdomain*/
Bkp_deinit ();
/*enablelse*/
Rcc_lseconfig (rcc_lse_on);
/*waittilllseisready*/
while (Rcc_getflagstatus (Rcc_flag_lserdy) ==reset)
{}
/*selectlseasrtcclocksource*/
Rcc_rtcclkconfig (Rcc_rtcclksource_lse);
/*enablertcclock*/
Rcc_rtcclkcmd (ENABLE);
/*waitforrtcregisterssynchronization*/
Rtc_waitforsynchro ();
/*waituntillastwriteoperationonrtcregistershasfinished*/
Rtc_waitforlasttask ();
/*setrtcprescaler:setrtcperiodto1sec*/
Rtc_setprescaler (32767);/*rtcperiod=rtcclk/rtc_pr= (32.768KHz)/(32767+1) */
/*waituntillastwriteoperationonrtcregistershasfinished*/
STM32RTC (real-time clock) configuration commentary author: Lan Tianxiang
Rtc_waitforlasttask ();
/*enablethertcsecond*/
Rtc_itconfig (rtc_it_sec,enable);
/*waituntillastwriteoperationonrtcregistershasfinished*/
Rtc_waitforlasttask ();