1 Introduction
After a system reset, access to the backup registers and RTC is disabled to prevent accidental writes to the backup area (BKP). The following operations will enable access to the backup registers and RTC:
l Set the PWREN and BKPEN bits of register RCC_APB1ENR to enable the power supply and backup interface clock (call: RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE));
l Set the DBP bit in register PWR_CR to enable access to the backup registers and RTC (call: PWR_BackupAccessCmd(ENABLE)).
2. RTC precautions
The RTC_PRL, RTC_ALR, RTC_CNT and RTC_DIV registers can only be reset by the backup domain reset signal; a system reset or power reset will not affect their values;
l RTC provides APB1 interface to read the value of RTC register through ABP1, but it must wait until the RSF (synchronization flag) bit in the RTC_CRL register is set to "1" by hardware;
The configuration of RTC must be completed after the previous write operation (determine whether RTOFF in the RTC_CR register is 1, which means the update is complete), and set the CNF bit in the RTC_CRL register to make the RTC enter the configuration mode before writing to the RTC_PRL, RTC_CNT, and RTC_ALR registers. When the CNF flag is cleared, the write operation is actually effective (indicating that the RTC is dynamically configured, that is, the configuration is performed after the RTC is running);
Any flag bit in the RTC will remain pending (because OWF, ALRF, SECF and RSF can only be set by hardware and cleared by software) until the appropriate RTC_CR request bit is reset by software, indicating that all requested interrupts have been accepted;
If ALRF=1 and ALRIE=1, RTC global interrupt is allowed. If EXTI line 17 interrupt is allowed in EXTI controller, RTC global interrupt and RTC alarm interrupt are allowed. In this case, the alarm interrupt priority is generally set higher than the global interrupt. If the global interrupt priority is higher than the alarm interrupt, the alarm interrupt flag must be cleared in the global interrupt before entering the alarm interrupt processing function for further processing (because if the flag is not cleared, the interrupt will continue to be triggered, and the global interrupt priority is high, it will always be in the global interrupt and cannot jump out);
If ALRF = 1, if the interrupt mode of EXTI line 17 is set in the EXTI controller, the RTC alarm interrupt is allowed; if the event mode of EXTI line 17 is set in the EXTI controller, a pulse will be generated on this line (no RTC alarm interrupt will be generated);
When the APB1 clock is not running, the OWF, ALRF, SECF, and RSF bits are not updated;
l When the system is reset, all interrupts are disabled, there is no pending interrupt request, and the RTC register can be written;
Write operations to the RTC must be synchronized with the RTC seconds flag using one of the following procedures:
Use the RTC alarm interrupt and modify the RTC alarm and/or RTC counter in the interrupt handler;
Wait for the seconds flag SECF in the RTC control register to be set before changing the RTC alarm and/or RTC counter.
Figure 1 Simplified RTC block diagram (see manual for details)
3. RTC register description
l RTC control register high RTC_CRH/low RTC_CRL
l RTC prescaler loading register (RTC_PRLH/RTC_PRLL)
l RTC prescaler remainder register (RTC_DIVH/RTC_DIVL)
l RTC counter register (RTC_CNTH/RTC_CNTL)
l RTC alarm register (RTC_ALRH/RTC_ALRL)
The registers related to RTC are:
l PWREN and BKPEN of APB1 peripheral clock enable register RCC_APB1ENR enable power and backup clock
l Backup area protection bit of the power control register PWR_CR: DBP
4. RTC configuration process
? Configure RCC: select system clock, configure bus clock, enable peripheral device clock, etc.;
? Call RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE) function to enable the power supply and backup domain clock;
? Call PWR_BackupAccessCmd(ENABLE) to obtain the backup area access rights;
? Call the BKP_DeInit() function to reset all registers of the peripheral BKP to default values;
? Configure and select the RTC clock: call RCC_RTCCLKConfig(RCC_RTCCLKSource_XXX) to select LSE, HSE divided by 128 or LSI;
? RTC configuration:
n Call RCC_RTCCLKCmd(ENABLE) to enable the RTC clock;
n Call RTC_WaitForSynchro() to wait for the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL) to synchronize with the APB clock of the RTC (wait for the RTOFF position to be 1);
n Call the RTC_WaitForLastTask() function to wait for the most recent write operation to the RTC register to complete;
n Call the RTC configuration function (such as RTC_SetPrescaler(40000)) to configure the RTC (Note: the RTC control registers can be read and written directly; the write operation of RTC_PRL, RTC_CNT, RTC_ALR needs to enter the configuration mode, and reading them only needs to wait for the synchronization to complete (RSF is set to 1) and read through the APB1 interface);
n Each time after calling the RTC configuration function, you need to call RTC_WaitForLastTask() to wait for the current configuration to succeed.
? EXTI configuration: If you need to connect the RTC to EXTI line 17, configure EXTI line 17 to interrupt/event mode;
? NVIC configuration: If an interrupt is to be generated, configure the interrupt vector controller, enable the EXTI15_10_IRQHandler interrupt, or enable the RTC_IRQHandler interrupt;
? Write an interrupt handling function: Note that you must call the RTC_ClearITPendingBit() function in the interrupt handling function to clear the corresponding interrupt flag bit;
5. RTC configuration example
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x00);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x00);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Set the alarm interrupt priority to be higher than the global interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void RTC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSICmd(ENABLE);
while(RESET == RCC_GetFlagStatus(RCC_FLAG_LSIRDY))
{
}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_ALR,ENABLE);
RTC_ITConfig(RTC_IT_SEC,ENABLE);
RTC_SetPrescaler(6000);
RTC_WaitForLastTask();
RTC_SetAlarm(29);
RTC_WaitForLastTask();
BKP_TamperPinCmd(DISABLE);
BKP_RTCOutputConfig(BKP_RTCOutputSource_Second);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
/* Interrupt handling function */
void RTC_IRQHandler(void)
{
if(SET == RTC_GetITStatus(RTC_IT_SEC))
{
RTC_ClearITPendingBit(RTC_IT_SEC);
GPIO_WriteBit(GPIOB,GPIO_Pin_13,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_13)));
}
}
void RTCAlarm_IRQHandler(void)
{
if(SET == RTC_GetFlagStatus(RTC_IT_ALR))
{
RTC_ClearFlag(RTC_IT_ALR); /* Clear interrupt flag, including external interrupt line flag */
if(EXTI_GetITStatus(EXTI_Line17));
{
EXTI_ClearITPendingBit(EXTI_Line17);
GPIO_WriteBit(GPIOB,GPIO_Pin_8,(BitAction)(0));
}
}
}
Previous article:A brief discussion on the SysTick system clock timer of the STM32F10X chip
Next article:Summary of STM32F10x chip GPIO/AFIO port configuration
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Very generous! These boards, which cost 500 yuan, are given away for free this month!
- The characteristics of low-power Bluetooth determine that it is worth looking forward to~
- EEWORLD University ---- Industrial Motor Driver - Introduction to Texas Instruments System Solutions
- Updated PYBCN_V2 and PYBNANO_V2 firmware
- IIR filter software implementation (Matlab+C++)
- Invite you to join Microchip's online seminar - Designing energy-efficient solutions for your IoT sensors
- FPGA-based CPU design
- [Synopsys IP Resources] Effectively protect data in PCIe and CXL in cloud computing
- Recruiting part-time DSP and other related professional lecturers or technical support personnel
- [LSM6DSOX finite state machine routine learning 5] - Free Fall