53 lines
1.4 KiB
C
53 lines
1.4 KiB
C
#include "rtc.h"
|
|
#include "base.h"
|
|
#include "stm32f4xx.h"
|
|
|
|
int RTC_InitNormal(RTC_InitStruct *init) {
|
|
RTC_InitTypeDef RTC_InitStructure;
|
|
uint16_t retry = 0X1FFF;
|
|
// 使能PWR时钟
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
|
// 使能后备寄存器访问
|
|
PWR_BackupAccessCmd(ENABLE);
|
|
// 是否第一次配置?
|
|
if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x5050) {
|
|
// LSE 开启
|
|
RCC_LSEConfig(RCC_LSE_ON);
|
|
// 检查指定的RCC标志位设置与否,等待低速晶振就绪
|
|
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {
|
|
retry++;
|
|
if (init->delay_ms)
|
|
init->delay_ms(10);
|
|
}
|
|
// LSE 开启失败.
|
|
if (retry == 0)
|
|
return -1;
|
|
// 设置RTC时钟(RTCCLK),选择LSE作为RTC时钟
|
|
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
|
|
// 使能RTC时钟
|
|
RCC_RTCCLKCmd(ENABLE);
|
|
|
|
// RTC异步分频系数(1~0X7F)
|
|
RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
|
|
// RTC同步分频系数(0~7FFF)
|
|
RTC_InitStructure.RTC_SynchPrediv = 0xFF;
|
|
// RTC设置为,24小时格式
|
|
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
|
|
RTC_Init(&RTC_InitStructure);
|
|
|
|
RTC_TimeTypeDef time = {0};
|
|
time.RTC_Hours = 12;
|
|
time.RTC_H12 = RTC_H12_AM;
|
|
RTC_SetTime(RTC_Format_BIN, &time);
|
|
RTC_DateTypeDef data = {0};
|
|
data.RTC_Year = 19;
|
|
data.RTC_Month = 12;
|
|
data.RTC_Date = 3;
|
|
data.RTC_WeekDay = 1;
|
|
RTC_SetDate(RTC_Format_BIN, &data);
|
|
// 标记已经初始化过了
|
|
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x5050);
|
|
}
|
|
return 0;
|
|
}
|