📄 rtc_au1000.c
字号:
*
*
*
************************************************************************/
static INT32
rtc_au1000_init(
UINT32 major, /* IN: major device number */
UINT32 minor, /* IN: minor device number */
void *p_param ) /* INOUT: device parameter block */
{
int rcode;
bool start12;
UINT8 hour;
UINT32 value;
unsigned int x,y;
/*
* For boards that do not use the 32khz oscillator, must USE_CP0_COUNTER
*/
#ifndef USE_CP0_COUNTER
/* Enable RTC,
bit13 - 1 to enable PC1
bit 8 - 1 to enable 32.768kHz Osc
*/
/*** This is a blind enable of the TOY and OSC, this will speed bringup ****/
#if 1
REG32(AU1000_TOY_CONTROL) = 0x00000100;
/* Spin on write status */
while((REG32(AU1000_TOY_CONTROL) & 0x00000080));
/* Check to see if OSC is running */
x=0;
while(!(REG32(AU1000_TOY_CONTROL) & 0x00000020));
{
x++;
if(x>100000)
{
printf("Osc not running\n");
return (ERROR_RTC);
}
}
#else
REG32(AU1000_TOY_CONTROL) = 0x4100;
for(x=0;x<100000;x++)
y = REG32(AU1000_TOY_CONTROL);
#endif
/* Trim = 0x7fff. Trim will add 1 for effective divide of 32768 - the RTC will be at
1 Hz */
SETREG( AU1000_TOY_TRIM, 0x00007fff );
#endif /* USE_CP0_COUNTER */
return( OK );
}
/************************************************************************
*
* rtc_au1000_read
* Description :
* -------------
* This service reads the current value of the Real Time Clock.
*
*
* Parameters :
* ------------
*
* 'major', IN, major device number
* 'minor', IN, minor device number for multi device drivers
* 'p_param', INOUT, RTC variable of type, t_RTC_calendar.
*
*
* Return values :
* ---------------
*
* 'OK'(=0)
*
*
************************************************************************/
static INT32
rtc_au1000_read(
UINT32 major, /* IN: major device number */
UINT32 minor, /* IN: minor device number */
t_RTC_calendar *p_param ) /* INOUT: current RTC value */
{
#ifndef USE_CP0_COUNTER
UINT32 TOY_value;
UINT32 year;
UINT32 month = 1;
INT32 days;
bool leapyear;
UINT32 i;
/* Read TOY value */
/* TOY_value represents number of ticks since 2000 */
TOY_value = REG32(AU1000_TOY_READ);
days = TOY_value/(60 * 60 * 24);
p_param->hour = (TOY_value - (days * 60 * 60 * 24)) / (60 * 60);
p_param->minute = (TOY_value - (days * 60 * 60 * 24) - (p_param->hour * 60 * 60)) / 60;
p_param->second = TOY_value - (days * 60 * 60 * 24) - (p_param->hour * 60 * 60) - (p_param->minute * 60);
year = 2000;
while(days>365)
{
leapyear= ((year % 4) == 0 &&
((year % 100) != 0 || (year % 400) == 0));
if(leapyear && days > 366)
{
year++;
days -= 366;
}
else if (!leapyear && days > 365)
{
year++;
days -= 365;
}
else
break;
}
leapyear= ((year % 4) == 0 &&
((year % 100) != 0 || (year % 400) == 0));
if(leapyear)
while(days > leapyeardays[month])
month++;
else
while(days > monthdays[month])
month++;
if(leapyear)
p_param->dayofmonth = days - leapyeardays[month - 1];
else
p_param->dayofmonth = days - monthdays[month - 1];
year--;
days = year*365 + year/4 - year/100 + year/400;
year++;
if (leapyear)
days += leapyeardays[month-1];
else
days += monthdays[month-1];
days += p_param->dayofmonth;
p_param->dayofweek = days % 7; /* unix: sunday=0, saturday=6) */
p_param->dayofweek++; /* TOY: sunday=1, saturday=7) */
p_param->month = month;
p_param->year = year;
#else
UINT32 seconds;
asm("mfc0 %0,$9" : "=r" (seconds));
seconds = seconds / 396000000;
p_param->hour = 0;
p_param->minute = 0;
p_param->second = seconds;
p_param->dayofmonth = 1;
p_param->dayofweek = 1;
p_param->month = 1;
p_param->year = 2002;
#endif /* USE_CP0_COUNTER */
return( OK );
}
/************************************************************************
*
* TOY_write
* Description :
* -------------
* This service sets the current value of the Real Time Clock.
*
*
* Parameters :
* ------------
*
* 'major', IN, major device number
* 'minor', IN, minor device number for multi device drivers
* 'p_param', INOUT, TOY variable of type, t_TOY_calendar.
*
*
* Return values :
* ---------------
*
* 'OK'(=0)
*
*
************************************************************************/
static INT32
rtc_au1000_write(
UINT32 major, /* IN: major device number */
UINT32 minor, /* IN: minor device number */
t_RTC_calendar *p_param ) /* INOUT: current RTC value */
{
#ifndef USE_CP0_COUNTER
UINT32 year;
UINT32 days;
UINT32 seconds;
bool leapyear;
/* Set TOY value */
/* find year */
year = p_param->year;
leapyear = ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));
/* adjust for TOY counting from 2000 */
year = year - 2000;
/* Calculate days */
days = year * 365 + year/4 - year/100 + year/400;
if(leapyear)
days += leapyeardays[p_param->month - 1];
else
days += monthdays[p_param->month - 1];
days += p_param->dayofmonth;
seconds = days * 24 * 60 * 60;
seconds += (p_param->hour * 60 * 60) + (p_param->minute * 60) + p_param->second;
/* Make sure a write is not in process */
while(REG32( AU1000_TOY_CONTROL ) & 0x00000001);
REG32( AU1000_TOY_WRITE) = seconds;
while(REG32( AU1000_TOY_CONTROL ) & 0x00000001);
#endif /* USE_CP0_COUNTER */
return( OK );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -