#include
#include
#include
const char* months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const char* weekDays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const u16 daysAtStartOfMonthLUT[12] =
{
0 %7, //januari 31
31 %7, //februari 28+1(leap year)
59 %7, //maart 31
90 %7, //april 30
120 %7, //mei 31
151 %7, //juni 30
181 %7, //juli 31
212 %7, //augustus 31
243 %7, //september 30
273 %7, //oktober 31
304 %7, //november 30
334 %7 //december 31
};
#define isLeapYear(year) (((year)%4) == 0)
uint getDayOfWeek(uint day, uint month, uint year)
{
//http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week
day += 2*(3-((year/100)%4));
year %= 100;
day += year + (year/4);
day += daysAtStartOfMonthLUT[month] - (isLeapYear(year) && (month <= 1));
return day % 7;
}
void update_clock()
{
int hours, seconds, minutes, day, month, year;
time_t unixTime = time(NULL);
time(&unixTime);
struct tm* timeStruct = gmtime((const time_t *)&unixTime);
hours = timeStruct->tm_hour;
minutes = timeStruct->tm_min;
seconds = timeStruct->tm_sec;
day = timeStruct->tm_mday;
month = timeStruct->tm_mon;
year = timeStruct->tm_year +1900;
printf("\x1b[2J%02i:%02i:%02i", hours, minutes, seconds);
printf("\n%s %s %i %i", weekDays[getDayOfWeek(day, month, year)], months[month], day, year);
}
int main(void)
{
timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(1), update_clock);
consoleDemoInit(); //setup the sub screen for printing
while(1) {
swiWaitForVBlank();
}
return 0;
}
在libnds的arm9部分的头文件中,没找到获取自带的实时时钟相关的内容,相反在arm7目录中找到了clock.h,其中有这么个函数:
//---------------------------------------------------------------------------------
void rtcGetTimeAndDate(uint8 * time) {
//---------------------------------------------------------------------------------
uint8 command, status;
command = READ_TIME_AND_DATE;
rtcTransaction(&command, 1, time, 7);
command = READ_STATUS_REG1;
rtcTransaction(&command, 1, &status, 1);
if ( status & STATUS_24HRS ) {
time[4] &= 0x3f;
} else {
}
BCDToInteger(time,7);
}
//---------------------------------------------------------------------------------
void resyncClock() {
//---------------------------------------------------------------------------------
RTCtime dstime;
rtcGetTimeAndDate((uint8 *)&dstime);
__transferRegion()->unixTime = __mktime(&dstime);
}
clock.c中的该函数就实现了同步时钟的功能,而resync函数在初始化时钟中断的时候被调用了一次:
//---------------------------------------------------------------------------------
void initClockIRQ() {
//---------------------------------------------------------------------------------
REG_RCNT = 0x8100;
irqSet(IRQ_NETWORK, syncRTC);
// Reset the clock if needed
rtcReset();
uint8 command[4];
command[0] = READ_STATUS_REG2;
rtcTransaction(command, 1, &command[1], 1);
command[0] = WRITE_STATUS_REG2;
command[1] = 0x41;
rtcTransaction(command, 2, 0, 0);
command[0] = WRITE_INT_REG1;
command[1] = 0x01;
rtcTransaction(command, 2, 0, 0);
command[0] = WRITE_INT_REG2;
command[1] = 0x00;
command[2] = 0x21;
command[3] = 0x35;
rtcTransaction(command, 4, 0, 0);
// Read all time settings on first start
resyncClock();
}
void powerValueHandler(u32 value, void* user_data)
// Keep the ARM7 mostly idle
while (!exitflag) {
if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R))) {
exitflag = true;
}
rtcGetTimeAndDate((uint8 *)0x02fff800);
swiWaitForVBlank();
}
#include
#include
void update_clock()
{
vu8* data = (vu8*)0x02fff800;
iprintf("%d %d %d %d %d %d %d\n",data[0],data[1],data[2],data[3],data[4],data[5],data[6]);
}
int main(void)
{
timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(1), update_clock);
consoleDemoInit(); //setup the sub screen for printing
while(1) {
swiWaitForVBlank();
}
return 0;
}