📄 rtc.c
字号:
else if (days == 366)
break;
}
else
{
days -= 365;
year += 1;
}
}
// Determine whether it is a leap year
month_tab = (UINT8 *)((IsLeapYear(year)) ? monthtable_leap : monthtable);
for (month=0; month<12; month++)
{
if (days <= month_tab[month])
break;
days -= month_tab[month];
}
month += 1;
lpTime->wDay = days;
lpTime->wDayOfWeek = dayofweek;
lpTime->wMonth = month;
lpTime->wYear = year;
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: ConvertSeconds
//
// Local helper function that converts time of day in seconds to hour,
// minute and seconds.
//
// Parameters:
//
// Returns:
// Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL ConvertSeconds(UINT32 seconds, SYSTEMTIME* lpTime)
{
int minutes = 0, hours = 0;
if(seconds < 86400)
{
if (seconds >= 60)
{
minutes = (int) seconds / 60;
seconds -= (minutes * 60);
if (minutes >= 60)
{
hours = (int) minutes / 60;
minutes -= (hours * 60);
}
}
}
else
{
ERRORMSG(TRUE, (_T("TOD in sec is wrong(seconds > 86399) %d"),
seconds));
return FALSE;
}
lpTime->wMilliseconds = 0;
lpTime->wHour = hours;
lpTime->wMinute = minutes;
lpTime->wSecond = seconds;
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: SetTime
//
// This function sets the given time & day into the register pair indicated
// by type.
//
// Parameters:
//
// Returns:
// Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL SetTime(UINT32 type, SYSTEMTIME* lpTime)
{
UINT32 days, seconds, addr;
if(!CheckRealTime(lpTime))
return FALSE;
// calculate time of day in seconds
seconds = CalculateSeconds(lpTime);
if(seconds > 86399)
{
return FALSE;
}
// Set Reg TimeOftheDay TOD -> Hours, Min , Sec : a 17 bit time of day (TOD)
addr = (type == TYPE_TIME) ? MC13783_RTC_TM_ADDR : MC13783_RTC_ALM_ADDR;
OALPmicWrite(addr, seconds);
// Calculate days.
days = CalculateDays(lpTime);
// Set Reg Day -> years, months , days :the 15 bit DAY counter.
addr = (type == TYPE_TIME) ? MC13783_RTC_DAY_ADDR :
MC13783_RTC_DAY_ALM_ADDR;
//OALMSG(1, (TEXT("SetTime:set day (%d), seconds (%d)\r\n"), days, seconds));
OALPmicWrite(addr, days);
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: GetTime
//
// This function gets the time and day from the register pair indicated by type
//
// Parameters:
//
// Returns:
// Returns TRUE if successful, otherwise returns FALSE.
//
//------------------------------------------------------------------------------
BOOL GetTime(UINT32 type, SYSTEMTIME* lpTime)
{
UINT32 seconds, days, addr;
//OALMSG(1, (TEXT("+GetTime\r\n")));
addr = (type == TYPE_TIME) ? MC13783_RTC_TM_ADDR : MC13783_RTC_ALM_ADDR;
OALPmicRead(addr, &seconds);
addr = (type == TYPE_TIME) ? MC13783_RTC_DAY_ADDR :
MC13783_RTC_DAY_ALM_ADDR;
OALPmicRead(addr, &days);
// Convert seconds to hours , minutes and seconds of the day.
if (ConvertSeconds(seconds, lpTime) != TRUE)
{
RETAILMSG(1, (TEXT("Convert Second wrong\r\n")));
return FALSE;
}
// convert days to year, month and day
if (ConvertDays(days, lpTime) != TRUE)
{
RETAILMSG(1, (TEXT("Convert Day wrong\r\n")));
return FALSE;
}
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: OEMGetRealTime
//
// This function is called by the kernel to retrieve the time from
// the real-time clock.
//
//------------------------------------------------------------------------------
BOOL OEMGetRealTime(LPSYSTEMTIME lpst)
{
return GetTime(TYPE_TIME, lpst);
}
//------------------------------------------------------------------------------
//
// Function: OEMSetRealTime
//
// This function is called by the kernel to set the real-time clock.
//
//------------------------------------------------------------------------------
BOOL OEMSetRealTime(LPSYSTEMTIME lpst)
{
return SetTime(TYPE_TIME, lpst);
}
//------------------------------------------------------------------------------
//
// FUNCTION: CheckRealTime
//
// DESCRIPTION: Helper function is used to check if the input
// time is valid.
//
// PARAMETERS:
// lpst -
// Long pointer to the buffer containing
// the time to be checked in SYSTEMTIME format.
//
// RETURNS:
// TRUE - If time is valid.
//
// FALSE - If time is invalid.
//
//------------------------------------------------------------------------------
BOOL CheckRealTime(LPSYSTEMTIME lpst)
{
WORD isleap;
UINT8 *month_tab;
isleap = IsLeapYear(lpst->wYear);
month_tab = (UINT8 *)(isleap? monthtable_leap : monthtable);
if ((lpst->wYear < ORIGINYEAR) || (lpst->wYear > gdwMaxYear))
return FALSE;
if ((lpst->wMonth < 1) ||(lpst->wMonth > 12))
return FALSE;
if((lpst->wDay < 1) ||(lpst->wDay > month_tab[lpst->wMonth-1]))
return FALSE;
if ((lpst->wHour > 23) ||(lpst->wMinute > 59) ||(lpst->wSecond > 59))
return FALSE;
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: OEMSetAlarmTime
//
// Set the RTC alarm time.
//
//------------------------------------------------------------------------------
BOOL OEMSetAlarmTime(LPSYSTEMTIME pTime)
{
BOOL rc;
UINT32 irq;
OALMSG(OAL_RTC&&OAL_FUNC, ( L"+OEMSetAlarmTime(%d/%d/%d %d:%d:%d.%03d)\r\n",
pTime->wMonth, pTime->wDay, pTime->wYear,
pTime->wHour, pTime->wMinute, pTime->wSecond,
pTime->wMilliseconds ));
if (pTime == NULL)
goto cleanUp;
// Set seconds, minutes, hours and day in RTC day alarm register.
if(!SetTime(TYPE_ALRM, pTime))
goto cleanUp;
// Enable alarm IRQ.
OALMSG(OAL_RTC&&OAL_INFO, (TEXT("RTC Alarm Interrupt enabled.\r\n")));
// Enable/clear RTC interrupt
irq = g_IRQ_RTC;
OALIoCtlHalUnforceIrq(0, &irq, sizeof(irq), NULL, 0, NULL);
OALIntrDoneIrqs(1, &irq);
// Done
rc = TRUE;
cleanUp:
OALMSG(OAL_RTC&&OAL_FUNC, (L"-OEMSetAlarmTime(rc = %d)\r\n", rc));
return rc;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -