Files
FireAlarmCtrlCn/FW/Core/Src/rtc.c
2026-04-06 19:02:09 +08:00

150 lines
3.9 KiB
C

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file rtc.c
* @brief This file provides code for the configuration
* of the RTC instances.
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "rtc.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* RTC init function */
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
LL_RTC_InitTypeDef RTC_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/* Peripheral clock enable */
LL_RCC_EnableRTC();
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
RTC_InitStruct.AsynchPrescaler = 127;
RTC_InitStruct.SynchPrescaler = 255;
LL_RTC_Init(RTC, &RTC_InitStruct);
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
/* USER CODE BEGIN 1 */
LL_RTC_TimeTypeDef RTC_TimeStruct = {0};
LL_RTC_DateTypeDef RTC_DateStruct = {0};
void RTC_Config(void)
{
LL_RTC_InitTypeDef RTC_InitStruct = {0};
LL_RTC_WaitForSynchro(RTC);
RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
RTC_InitStruct.AsynchPrescaler = 127;
RTC_InitStruct.SynchPrescaler = 255;
LL_RTC_Init(RTC, &RTC_InitStruct);
RTC_TimeStruct.TimeFormat = LL_RTC_HOURFORMAT_24HOUR;
RTC_TimeStruct.Hours = 0x10;
RTC_TimeStruct.Minutes = 0x25;
RTC_TimeStruct.Seconds = 0x02;
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_TimeStruct);
RTC_DateStruct.Year = 0x24;
RTC_DateStruct.Month = LL_RTC_MONTH_NOVEMBER;
RTC_DateStruct.Day = 0x05;
RTC_DateStruct.WeekDay = LL_RTC_WEEKDAY_TUESDAY;
LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_DateStruct);
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, 0x32F2);
}
void RTC_TimeShow(void)
{
/* Get the current Time */
RTC_TimeStruct.Hours = LL_RTC_TIME_GetHour(RTC);
RTC_TimeStruct.Minutes = LL_RTC_TIME_GetMinute(RTC);
RTC_TimeStruct.Seconds = LL_RTC_TIME_GetSecond(RTC);
RTC_DateStruct.Year = LL_RTC_DATE_GetYear(RTC);
RTC_DateStruct.Month = LL_RTC_DATE_GetMonth(RTC);
RTC_DateStruct.Day = LL_RTC_DATE_GetDay(RTC);
RTC_DateStruct.WeekDay = LL_RTC_DATE_GetWeekDay(RTC);
SystemDataTimeUpdata( RTC_DateStruct.Year,
RTC_DateStruct.Month,
RTC_DateStruct.Day,
RTC_TimeStruct.Hours,
RTC_TimeStruct.Minutes,
RTC_TimeStruct.Seconds
);
}
void rtc_init()
{
if(0x32F2 != (LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0)))
{
RTC_Config();
RTC_TimeShow();
}
else
{
RTC_TimeShow();
}
}
void RTC_DataTimeSet(unsigned char aY, unsigned char aM, unsigned char aD, unsigned char aH, unsigned char aMin, unsigned char aSec, unsigned char aWk)
{
if((!aY)||(!aM)||(!aD))return;
LL_RTC_WaitForSynchro(RTC);
RTC_TimeStruct.Hours = aH;
RTC_TimeStruct.Minutes = aMin;
RTC_TimeStruct.Seconds = aSec;
RTC_DateStruct.Year = aY;
RTC_DateStruct.Month = aM;
RTC_DateStruct.Day = aD;
RTC_DateStruct.WeekDay = aWk;
LL_RTC_DATE_Init(RTC,LL_RTC_FORMAT_BCD, &RTC_DateStruct);
LL_RTC_TIME_Init(RTC,LL_RTC_FORMAT_BCD, &RTC_TimeStruct);
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, 0x32F2);
}
/* USER CODE END 1 */