rt-thread-official/bsp/synwit/swm320/libraries/SWM320_StdPeriph_Driver/SWM320_rtc.c

414 lines
19 KiB
C
Raw Normal View History

2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:10:29 +08:00
* : SWM320_rtc.c
2021-09-13 21:04:42 +08:00
* : SWM320单片机的RTC驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
2021-02-18 13:29:12 +08:00
*
*
*******************************************************************************************************************************************
* @attention
*
2021-09-13 21:04:42 +08:00
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
2021-02-18 13:29:12 +08:00
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
#include "SWM320.h"
#include "SWM320_rtc.h"
2021-02-18 13:29:12 +08:00
static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date);
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_Init()
* : RTC初始化
* : RTC_TypeDef * RTCx RTCRTC
* RTC_InitStructure * initStruct RTC相关设定值的结构体
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_Init(RTC_TypeDef * RTCx, RTC_InitStructure * initStruct)
2021-05-06 10:42:41 +08:00
{
SYS->CLKEN |= (1 << SYS_CLKEN_RTCBKP_Pos);
SYS->LRCCR &= ~(1 << SYS_LRCCR_OFF_Pos); //RTC使用32KHz RC时钟
2021-05-06 10:42:41 +08:00
SYS->CLKEN |= (1 << SYS_CLKEN_RTC_Pos) |
((uint32_t)1 << SYS_CLKEN_ALIVE_Pos);
RTC_Stop(RTCx);
while(RTCx->CFGABLE == 0);
2021-05-06 10:42:41 +08:00
RTCx->MINSEC = (initStruct->Second << RTC_MINSEC_SEC_Pos) |
(initStruct->Minute << RTC_MINSEC_MIN_Pos);
RTCx->DATHUR = (initStruct->Hour << RTC_DATHUR_HOUR_Pos) |
(initStruct->Date << RTC_DATHUR_DATE_Pos);
RTCx->MONDAY = (calcWeekDay(initStruct->Year, initStruct->Month, initStruct->Date) << RTC_MONDAY_DAY_Pos) |
(initStruct->Month << RTC_MONDAY_MON_Pos);
2021-09-13 20:19:14 +08:00
RTCx->YEAR = initStruct->Year;
2021-05-06 10:42:41 +08:00
RTCx->LOAD = 1 << RTC_LOAD_TIME_Pos;
RTCx->IF = 0x1F;
RTCx->IE = (initStruct->SecondIEn << RTC_IE_SEC_Pos) |
(initStruct->MinuteIEn << RTC_IE_MIN_Pos);
if(initStruct->SecondIEn | initStruct->MinuteIEn)
2021-05-06 10:42:41 +08:00
{
NVIC_EnableIRQ(RTC_IRQn);
}
else
{
NVIC_DisableIRQ(RTC_IRQn);
}
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_Start()
* : RTC
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_Start(RTC_TypeDef * RTCx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
RTCx->EN = 1;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_Stop()
* : RTC
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_Stop(RTC_TypeDef * RTCx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
RTCx->EN = 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_GetDateTime()
* :
* : RTC_TypeDef * RTCx RTCRTC
* RTC_DateTime * dateTime
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_GetDateTime(RTC_TypeDef * RTCx, RTC_DateTime * dateTime)
2021-02-18 13:29:12 +08:00
{
2021-09-13 20:19:14 +08:00
dateTime->Year = RTCx->YEAR;
2021-05-06 10:42:41 +08:00
dateTime->Month = (RTCx->MONDAY & RTC_MONDAY_MON_Msk) >> RTC_MONDAY_MON_Pos;
dateTime->Date = (RTCx->DATHUR & RTC_DATHUR_DATE_Msk) >> RTC_DATHUR_DATE_Pos;
dateTime->Day = 1 << ((RTCx->MONDAY & RTC_MONDAY_DAY_Msk) >> RTC_MONDAY_DAY_Pos);
dateTime->Hour = (RTCx->DATHUR & RTC_DATHUR_HOUR_Msk) >> RTC_DATHUR_HOUR_Pos;
dateTime->Minute = (RTCx->MINSEC & RTC_MINSEC_MIN_Msk) >> RTC_MINSEC_MIN_Pos;
dateTime->Second = (RTCx->MINSEC & RTC_MINSEC_SEC_Msk) >> RTC_MINSEC_SEC_Pos;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_AlarmSetup()
* : RTC闹钟设定
* : RTC_TypeDef * RTCx RTCRTC
* RTC_AlarmStructure * alarmStruct RTC闹钟设定值的结构体
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_AlarmSetup(RTC_TypeDef * RTCx, RTC_AlarmStructure * alarmStruct)
2021-02-18 13:29:12 +08:00
{
while(RTCx->CFGABLE == 0);
2021-05-06 10:42:41 +08:00
RTCx->MINSECAL = (alarmStruct->Second << RTC_MINSECAL_SEC_Pos) |
(alarmStruct->Minute << RTC_MINSECAL_MIN_Pos);
RTCx->DAYHURAL = (alarmStruct->Hour << RTC_DAYHURAL_HOUR_Pos) |
(alarmStruct->Days << RTC_DAYHURAL_SUN_Pos);
RTCx->LOAD = 1 << RTC_LOAD_ALARM_Pos;
while(RTCx->LOAD & RTC_LOAD_ALARM_Msk);
2021-05-06 10:42:41 +08:00
RTCx->IF = (1 << RTC_IF_ALARM_Pos);
RTCx->IE &= ~RTC_IE_ALARM_Msk;
RTCx->IE |= (alarmStruct->AlarmIEn << RTC_IE_ALARM_Pos);
if(alarmStruct->AlarmIEn) NVIC_EnableIRQ(RTC_IRQn);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : calcWeekDay()
* :
* : uint32_t year
* uint32_t month
* uint32_t date
* : uint32_t 0 1 ... ... 6
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date)
{
uint32_t i, cnt = 0;
const uint32_t daysOfMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
2021-05-06 10:42:41 +08:00
for(i = 1; i < month; i++) cnt += daysOfMonth[i];
2021-05-06 10:42:41 +08:00
2021-02-18 13:29:12 +08:00
cnt += date;
2021-05-06 10:42:41 +08:00
if((year%4 == 0) && ((year%100 != 0) || (year%400 == 0)) && (month >= 3)) cnt += 1;
2021-05-06 10:42:41 +08:00
2021-02-18 13:29:12 +08:00
cnt += (year - 1901) * 365;
2021-05-06 10:42:41 +08:00
for(i = 1901; i < year; i++)
2021-02-18 13:29:12 +08:00
{
if((i%4 == 0) && ((i%100 != 0) || (i%400 == 0))) cnt += 1;
2021-02-18 13:29:12 +08:00
}
2021-05-06 10:42:41 +08:00
return (cnt+1) % 7;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntSecondEn()
* : 使
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntSecondEn(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE |= (1 << RTC_IE_SEC_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntSecondDis()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntSecondDis(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE &= ~(1 << RTC_IE_SEC_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntSecondClr()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntSecondClr(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IF = (1 << RTC_IF_SEC_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntSecondStat()
* :
* : RTC_TypeDef * RTCx RTCRTC
* : uint32_t 1 0
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t RTC_IntSecondStat(RTC_TypeDef * RTCx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (RTCx->IF & RTC_IF_SEC_Msk) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntMinuteEn()
* : 使
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntMinuteEn(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE |= (1 << RTC_IE_MIN_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntMinuteDis()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntMinuteDis(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE &= ~(1 << RTC_IE_MIN_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntMinuteClr()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntMinuteClr(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IF = (1 << RTC_IF_MIN_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntMinuteStat()
* :
* : RTC_TypeDef * RTCx RTCRTC
* : uint32_t 1 0
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t RTC_IntMinuteStat(RTC_TypeDef * RTCx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (RTCx->IF & RTC_IF_MIN_Msk) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntHourEn()
* : 使
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntHourEn(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE |= (1 << RTC_IE_HOUR_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntHourDis()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntHourDis(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE &= ~(1 << RTC_IE_HOUR_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntHourClr()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntHourClr(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IF = (1 << RTC_IF_HOUR_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntHourStat()
* :
* : RTC_TypeDef * RTCx RTCRTC
* : uint32_t 1 0
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t RTC_IntHourStat(RTC_TypeDef * RTCx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (RTCx->IF & RTC_IF_HOUR_Msk) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntDateEn()
* : 使
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntDateEn(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE |= (1 << RTC_IE_DATE_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntDateDis()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntDateDis(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE &= ~(1 << RTC_IE_DATE_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntDateClr()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntDateClr(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IF = (1 << RTC_IF_DATE_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntDateStat()
* :
* : RTC_TypeDef * RTCx RTCRTC
* : uint32_t 1 0
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t RTC_IntDateStat(RTC_TypeDef * RTCx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (RTCx->IF & RTC_IF_DATE_Msk) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntAlarmEn()
* : 使
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntAlarmEn(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE |= (1 << RTC_IE_ALARM_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntAlarmDis()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntAlarmDis(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IE &= ~(1 << RTC_IE_ALARM_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntAlarmClr()
* :
* : RTC_TypeDef * RTCx RTCRTC
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void RTC_IntAlarmClr(RTC_TypeDef * RTCx)
2021-05-06 10:42:41 +08:00
{
RTCx->IF = (1 << RTC_IF_ALARM_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : RTC_IntAlarmStat()
* :
* : RTC_TypeDef * RTCx RTCRTC
* : uint32_t 1 0
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t RTC_IntAlarmStat(RTC_TypeDef * RTCx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (RTCx->IF & RTC_IF_ALARM_Msk) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}