/*
** ===================================================================
** Method : RTC1_GetTime (component RTC_LDD)
*/
/*!
** @brief
** Returns actual time and date.
** Note: Fields not supported by HW are calculated in software.
** @param
** DeviceDataPtr - Pointer to device data
** structure pointer returned by [Init] method.
** @param
** TimePtr - Pointer to the time structure to
** fill with current time.
*/
/* ===================================================================*/
void RTC1_GetTime(LDD_TDeviceData *DeviceDataPtr, LDD_RTC_TTime *TimePtr)
{
uint32_t x;
uint32_t Seconds, Days;
(void)DeviceDataPtr; /* Parameter is not used, suppress unused argument warning */
Seconds = RTC_PDD_ReadTimeSecondsReg(RTC_BASE_PTR); /* Seconds since 2000-01-01 */
Seconds--;
Days = Seconds / 86400U; /* Days */
Seconds = Seconds % 86400U; /* Seconds left */
TimePtr->Hour = Seconds / 3600U; /* Hours */
Seconds = Seconds % 3600u; /* Seconds left */
TimePtr->Minute = Seconds / 60U; /* Minutes */
TimePtr->Second = Seconds % 60U; /* Seconds */
TimePtr->DayOfWeek = (Days + 6U) % 7U; /* Day of week */
TimePtr->Year = (4U * (Days / ((4U * 365U) + 1U))) + 2000U; /* Year */
Days = Days % ((4U * 365U) + 1U);
if (Days == ((0U * 365U) + 59U)) { /* 59 */
TimePtr->Day = 29U;
TimePtr->Month = 2U;
return;
} else if (Days > ((0U * 365U) + 59U)) {
Days--;
} else {
}
x = Days / 365U;
TimePtr->Year += x;
Days -= x * 365U;
for (x=1U; x <= 12U; x++) {
if (Days < ULY[x]) {
TimePtr->Month = x;
break;
} else {
Days -= ULY[x];
}
}
TimePtr->Day = Days + 1U;
}
/*
** ===================================================================
** Method : RTC1_SetTime (component RTC_LDD)
*/
/*!
** @brief
** Sets new time and date.
** Note: All LDD_RTC_TTime structure members must be correctly
** filled in.
** @param
** DeviceDataPtr - Pointer to device data
** structure pointer returned by [Init] method.
** @param
** TimePtr - Pointer to the time structure with
** new time to set.
** @return
** - Error code, possible codes:
** - ERR_OK - OK.
** - ERR_DISABLED - Component is disabled.
** - ERR_SPEED - Component does not work in
** the active clock configuration.
** - ERR_RANGE - Parameter out of range.
*/
/* ===================================================================*/
LDD_TError RTC1_SetTime(LDD_TDeviceData *DeviceDataPtr, LDD_RTC_TTime *TimePtr)
{
uint32_t Seconds;
(void)DeviceDataPtr; /* Parameter is not used, suppress unused argument warning */
if ((TimePtr->Year < 2000U) || (TimePtr->Year > 2099U) || (TimePtr->Month > 12U) || (TimePtr->Month == 0U) || (TimePtr->Day > 31U) || (TimePtr->Day == 0U)) { /* Test correctness of given parameters */
return ERR_RANGE; /* If not correct then error */
}
if (TimePtr->Year & 3U) { /* Is given year non-leap-one? */
if (ULY[TimePtr->Month] < TimePtr->Day) { /* Does the obtained number of days exceed number of days in the appropriate month & year? */
return ERR_RANGE; /* If yes (incorrect date inserted) then error */
}
} else { /* Is given year leap-one? */
if (LY[TimePtr->Month] < TimePtr->Day) { /* Does the obtained number of days exceed number of days in the appropriate month & year? */
return ERR_RANGE; /* If yes (incorrect date inserted) then error */
}
}
Seconds = ((TimePtr->Year - 2000U) * 365U) + (((TimePtr->Year - 2000U) + 3U) / 4U); /* Compute number of days from 2000 till given year */
Seconds += MONTH_DAYS[TimePtr->Month]; /* Add number of days till given month */
Seconds += TimePtr->Day; /* Add days in given month */
if ((TimePtr->Year & 3U) || (TimePtr->Month <= 2U)) { /* For non-leap year or month <= 2, decrement day counter */
Seconds--;
}
Seconds = (Seconds * 86400U) + (TimePtr->Hour * 3600U) + (TimePtr->Minute * 60U) + TimePtr->Second;
Seconds++;
RTC_PDD_EnableCounter(RTC_BASE_PTR, PDD_DISABLE); /* Disable counter */
RTC_PDD_WriteTimePrescalerReg(RTC_BASE_PTR, 0x00U); /* Clear prescaler */
RTC_PDD_WriteTimeSecondsReg(RTC_BASE_PTR, Seconds); /* Set seconds counter */
RTC_PDD_EnableCounter(RTC_BASE_PTR, PDD_ENABLE); /* Enable counter */
return ERR_OK;
}