#include "r_typedefs.h"
#include "dr7f701684.dvf.h"
#include "RTC.h"

/*-----------------use for RH850_F1KM_S1 --------------------*/

volatile uint8_t g_RTCLeapYear; /*1:LeapYear , 0:Not a leap year*/
volatile RTC_Information_st_t g_stRTCInformation;

/* BCD Convert   decimalism*/
/*The valid range of parameters is not judged*/
static uint8_t RTC_BCD_To_Dec(uint8_t u8RTCBCD)
{
	uint8_t u8RTCData = ((u8RTCBCD >> 4U) * 10U) + (u8RTCBCD & 0X0FU);
	return u8RTCData;
}
/* decimalism Convert  BCD */
static uint8_t RTC_Dec_To_BCD(uint8_t u8RTCDec)
{
	uint8_t u8RTCData = (((u8RTCDec / 10U) % 10U) << 4U) + (u8RTCDec % 10U);
	return u8RTCData;
}

/**************************************************************************/ /**
  * \brief      Determine if a year is leap year
  * \param      Year: the year to be determined
  * \retval     \arg 0: Not leap year
  *             \arg 1: Leap year
******************************************************************************/
static uint8_t RTC_Determine_Leap_Year(uint16_t Year)
{
	uint16_t u16RTCLeapYear = Year;

	/*u16RTCLeapYear = ((Year >> 4) & 0x0F) * 10 + (Year & 0x0F);*/

	if (u16RTCLeapYear & 0x0003U)
	{
		u16RTCLeapYear = 0U;
	}
	else
	{
		u16RTCLeapYear = 1U;
	}

	return (uint8_t)u16RTCLeapYear;
}

static void RTC_Stop(void)
{
	/*Stop sub-counter */
	RTCA0CE = 0U;
	/*Wait  sub-counter  stop*/
	while (RTCA0CEST != 0)
	{
		;
	}
}
void RTC_Pre_Init(void)
{
	RTC_Stop(); /* Stop RTCA*/

#if (RTC_MODE_SELECT == RTC_MODE_MAIN)
	RTCA0SLSB = 1U;			   /*Frequency selection mode*/
	RTCA0SCMP = 240000U - 1U; /*4M  RTCATCKI. */
#else
	RTCA0SLSB = 0U; /*32.768 kHz mode*/
	RTCA0SUBU = 0U; /*Reserved Later modified,Error Correction*/
#endif

	RTCA0AMPM = 1U; /*fix 24Hour Format*/

	/*Write start values*/
	RTCA0YEAR = RTC_Dec_To_BCD(RTC_DEFAULT_YEAR);
	RTCA0MONTH = RTC_Dec_To_BCD(RTC_DEFAULT_MONTH);
	RTCA0DAY = RTC_Dec_To_BCD(RTC_DEFAULT_DATE);
	RTCA0WEEK = RTC_Dec_To_BCD(RTC_DEFAULT_WEEK);
	RTCA0HOUR = RTC_Dec_To_BCD(RTC_DEFAULT_HOUR);
	RTCA0MIN = RTC_Dec_To_BCD(RTC_DEFAULT_MINUTE);
	RTCA0SEC = RTC_Dec_To_BCD(RTC_DEFAULT_SECOND);

	/*Starts sub-counter*/
	RTCA0CE = 1U;

	/*Wait  sub-counter  enable*/
	while (RTCA0CEST != 1U)
	{
		;
	}
}

void RTC_Set_Time(RTC_Information_st_t *pstRTCTime)
{
	while (RTCA0WST != 0U) /*Check that all clock counters are running.*/
	{
		;
	}
	RTCA0WAIT = 1U;		   /*Stop all clock counters*/
	while (RTCA0WST != 1U) /*Wait  all clock counters  stop*/
	{
		;
	}
	/*Write start values*/
	RTCA0YEAR = RTC_Dec_To_BCD(pstRTCTime->u8RTCYear);
	RTCA0MONTH = RTC_Dec_To_BCD(pstRTCTime->u8RTCMonth);
	RTCA0DAY = RTC_Dec_To_BCD(pstRTCTime->u8RTCDayOfMonth);
	RTCA0WEEK = RTC_Dec_To_BCD(pstRTCTime->u8RTCDayOfWeek);
	RTCA0HOUR = RTC_Dec_To_BCD(pstRTCTime->u8RTCHour);
	RTCA0MIN = RTC_Dec_To_BCD(pstRTCTime->u8RTCMinute);
	RTCA0SEC = RTC_Dec_To_BCD(pstRTCTime->u8RTCSecond);

	/*Start all clock counters*/
	RTCA0WAIT = 0U;
	while (RTCA0WST != 0U) /*Check that all clock counters are running.*/
	{
		;
	}
}
/**************************************************************************/ /**
  * \brief      Rreal time clock timing control
  * \attention  Call this function every 100 ms.
  * \retval     None
******************************************************************************/
void RTC_Timing_Service(void)
{
	while (RTCA0WST != 0U) /*Check that all clock counters are running.*/
	{
		;
	}
	RTCA0WAIT = 1U;		   /*Stop all clock counters*/
	while (RTCA0WST != 1U) /*Wait  all clock counters  stop*/
	{
		;
	}
	/*Read  data*/
	g_stRTCInformation.u8RTCYear = RTC_BCD_To_Dec(RTCA0YEAR);
	g_stRTCInformation.u8RTCMonth = RTC_BCD_To_Dec(RTCA0MONTH);
	g_stRTCInformation.u8RTCDayOfMonth = RTC_BCD_To_Dec(RTCA0DAY);
	g_stRTCInformation.u8RTCDayOfWeek = RTC_BCD_To_Dec(RTCA0WEEK);
	g_stRTCInformation.u8RTCHour = RTC_BCD_To_Dec(RTCA0HOUR);
	g_stRTCInformation.u8RTCMinute = RTC_BCD_To_Dec(RTCA0MIN);
	g_stRTCInformation.u8RTCSecond = RTC_BCD_To_Dec(RTCA0SEC);
	g_RTCLeapYear = RTC_Determine_Leap_Year(g_stRTCInformation.u8RTCYear);
	/*Start all clock counters*/
	RTCA0WAIT = 0U;
	while (RTCA0WST != 0U) /*Check that all clock counters are running.*/
	{
		;
	}
}
void RTC_Init(void)
{
	RTCA0ENALM = 0U; /*Disables alarm interrupt */
	RTC_Timing_Service();
}
/*
RTC_Alarm_Days_en_t:报警的星期，多选,多选,多选
u8RTCHour：报警的小时  DEC
u8RTCMinute：报警的分钟   DEC
*/
void RTC_Alarm_Set(RTC_Alarm_Days_en_t enAlarmDays, uint8_t u8RTCHour, uint8_t u8RTCMinute)
{
	RTCA0ENALM = 0U; /*Disables alarm interrupt */
	RTCA0ALW = enAlarmDays;
	RTCA0ALH = RTC_Dec_To_BCD(u8RTCHour);
	RTCA0ALM = RTC_Dec_To_BCD(u8RTCMinute);

	RTCA0ENALM = 1U; /*Enables alarm interrupt */
	STBC_WUF0WUFMSK0 &= 0xDFFFFFFFUL;
}
