/*******************************************************************************
|
* File Name : RTC.c
|
* Description :
|
* Author : hido.ltd
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Include Files *
|
*******************************************************************************/
|
#include "RTC.h"
|
#include "stm32l0xx_hal.h"
|
|
/*******************************************************************************
|
* Macro *
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Type Definition *
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Local Variable *
|
*******************************************************************************/
|
static RTC_HandleTypeDef *l_pstRTC = NULL;
|
|
/*******************************************************************************
|
* Local Function Declaration *
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Local Function *
|
*******************************************************************************/
|
/*******************************************************************************
|
* Function Name :
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : hido.ltd
|
* Modified Date: : 2018Äê8ÔÂ2ÈÕ
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Global Function *
|
*******************************************************************************/
|
/*******************************************************************************
|
* Function Name : RTC_Init
|
* Description : RTC³õʼ»¯
|
* Input : _pRTC RTCÃèÊöÖ¸Õë
|
* Output : None
|
* Return : None
|
* Author : hido.ltd
|
*******************************************************************************/
|
void RTC_Init(void *_pRTC)
|
{
|
l_pstRTC = (RTC_HandleTypeDef *)_pRTC;
|
}
|
|
/*******************************************************************************
|
* Function Name : RTC_GetDateTime
|
* Description : ´ÓRTCÖлñÈ¡ÈÕÆÚ¼°Ê±¼ä
|
* Input : None
|
* Output : _pstDateTime ÈÕÆÚʱ¼ä
|
* Return : HIDO_ERR ʧ°Ü
|
* HIDO_OK ³É¹¦
|
* Author : hido.ltd
|
*******************************************************************************/
|
HIDO_INT32 RTC_GetDateTime(ST_RTCDateTime *_pstDateTime)
|
{
|
RTC_TimeTypeDef stTime;
|
RTC_DateTypeDef stDate;
|
|
/* ÐèÒªÏÈ»ñȡʱ¼ä */
|
if (HAL_RTC_GetTime(l_pstRTC, &stTime, RTC_FORMAT_BIN) != HAL_OK)
|
{
|
return HIDO_ERR;
|
}
|
|
/* ÔÙ»ñÈ¡ÈÕÆÚ */
|
if (HAL_RTC_GetDate(l_pstRTC, &stDate, RTC_FORMAT_BIN) != HAL_OK)
|
{
|
return HIDO_ERR;
|
}
|
|
_pstDateTime->m_u32Year = stDate.Year + 2000;
|
_pstDateTime->m_u8Month = RTC_Bcd2ToByte(stDate.Month);
|
_pstDateTime->m_u8Day = stDate.Date;
|
|
_pstDateTime->m_u8Hour = stTime.Hours;
|
_pstDateTime->m_u8Min = stTime.Minutes;
|
_pstDateTime->m_u8Sec = stTime.Seconds;
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name : RTC_SetDateTime
|
* Description : ÉèÖÃÈÕÆÚ¼°Ê±¼ä
|
* Input : _pstDateTime ÈÕÆÚʱ¼ä
|
* Output : None
|
* Return : HIDO_ERR ʧ°Ü
|
* HIDO_OK ³É¹¦
|
* Author : hido.ltd
|
*******************************************************************************/
|
HIDO_INT32 RTC_SetDateTime(ST_RTCDateTime *_pstDateTime)
|
{
|
RTC_TimeTypeDef stTime;
|
RTC_DateTypeDef stDate;
|
|
stDate.Year = _pstDateTime->m_u32Year - 2000;
|
stDate.Month = RTC_ByteToBcd2(_pstDateTime->m_u8Month);
|
stDate.Date = _pstDateTime->m_u8Day;
|
stDate.WeekDay = RTC_WEEKDAY_MONDAY;
|
|
stTime.Hours = _pstDateTime->m_u8Hour;
|
stTime.Minutes = _pstDateTime->m_u8Min;
|
stTime.Seconds = _pstDateTime->m_u8Sec;
|
stTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
|
stTime.StoreOperation = RTC_STOREOPERATION_RESET;
|
|
/* ÉèÖÃʱ¼ä */
|
if (HAL_RTC_SetTime(l_pstRTC, &stTime, RTC_FORMAT_BIN) != HAL_OK)
|
{
|
return HIDO_ERR;
|
}
|
|
/* ÉèÖÃÈÕÆÚ */
|
if (HAL_RTC_SetDate(l_pstRTC, &stDate, RTC_FORMAT_BIN) != HAL_OK)
|
{
|
return HIDO_ERR;
|
}
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name : RTC_MakeTime
|
* Description : ½«ÄêÔÂÈÕʱ·ÖÃëת³ÉUTCÃëÊý£¬²ÉÓÃLINUXÄÚºËËã·¨
|
* Input : _pstDateTime ÈÕÆÚʱ¼ä
|
* Output : None
|
* Return : UTCÃëÊý
|
* Author : hido.ltd
|
* Modified Date: : 2017-09-22
|
*******************************************************************************/
|
HIDO_UINT32 RTC_MakeTime(ST_RTCDateTime *_pstDateTime)
|
{
|
HIDO_UINT32 u32Mon = _pstDateTime->m_u8Month;
|
HIDO_UINT32 u32Year = _pstDateTime->m_u32Year;
|
|
/* 1..12 -> 11,12,1..10 */
|
if (0 >= (HIDO_INT32) (u32Mon -= 2)) {
|
u32Mon += 12; /* Puts Feb last since it has leap day */
|
u32Year -= 1;
|
}
|
|
return ((((HIDO_UINT32)
|
(u32Year/4 - u32Year/100 + u32Year/400 + 367*u32Mon/12 + _pstDateTime->m_u8Day) +
|
u32Year*365 - 719499
|
)*24 + _pstDateTime->m_u8Hour /* now have hours */
|
)*60 + _pstDateTime->m_u8Min /* now have minutes */
|
)*60 + _pstDateTime->m_u8Sec; /* finally seconds */
|
}
|
|
/*******************************************************************************
|
* Function Name : RTC_GetDiffSeconds
|
* Description : ¼ÆËãʱ¼ä_pstDateTime0ºÍʱ¼ä_pstDateTime1µÄÃëÊý²îÖµ
|
* Input : _pstDateTime0 ÈÕÆÚʱ¼ä0
|
* : _pstDateTime1 ÈÕÆÚʱ¼ä1
|
* Output : None
|
* Return : ÃëÊý
|
* Author : hido.ltd
|
* Modified Date: : 2017-09-22
|
*******************************************************************************/
|
HIDO_UINT32 RTC_GetDiffSeconds(ST_RTCDateTime *_pstDateTime0, ST_RTCDateTime *_pstDateTime1)
|
{
|
HIDO_UINT32 u32Sec0 = RTC_MakeTime(_pstDateTime0);
|
HIDO_UINT32 u32Sec1 = RTC_MakeTime(_pstDateTime1);
|
|
if (u32Sec0 > u32Sec1)
|
{
|
return u32Sec0 - u32Sec1;
|
}
|
else
|
{
|
return u32Sec1 - u32Sec0;
|
}
|
}
|
|
/*******************************************************************************
|
* Function Name : RTC_GetDiffSeconds
|
* Description : ¼ÆËãʱ¼ä_pstDateTime0ºÍʱ¼ä_pstDateTime1µÄÃëÊý²îÖµ
|
* Input : _pstDateTime0 ÈÕÆÚʱ¼ä0
|
* : _pstDateTime1 ÈÕÆÚʱ¼ä1
|
* Output : None
|
* Return : ÃëÊý
|
* Author : hido.ltd
|
*******************************************************************************/
|
HIDO_UINT32 RTC_GetTimeStamp(void)
|
{
|
ST_RTCDateTime stDateTime;
|
|
RTC_GetDateTime(&stDateTime);
|
|
return RTC_MakeTime(&stDateTime) - (8 * 3600);
|
}
|
|
/*******************************************************************************
|
* Function Name : RTC_SetWakeUp
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : hido.ltd
|
*******************************************************************************/
|
HIDO_INT32 RTC_SetWakeUp(HIDO_UINT32 _u32Ms)
|
{
|
/*
|
* DIV(16) / LSI(32.768K);
|
* 60s / (DVI / LSI) = 122880
|
*/
|
|
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(l_pstRTC, RTC_FLAG_WUTF);
|
|
if (HAL_RTCEx_SetWakeUpTimer_IT(l_pstRTC, _u32Ms / (16 / 32.768), RTC_WAKEUPCLOCK_RTCCLK_DIV16) != HAL_OK)
|
{
|
return HIDO_ERR;
|
}
|
|
return HIDO_OK;
|
}
|