diff --git a/include/kernel/iomisc.h b/include/kernel/iomisc.h index 5c9da8e..edea848 100644 --- a/include/kernel/iomisc.h +++ b/include/kernel/iomisc.h @@ -66,6 +66,20 @@ extern void IoEnableNMI(void); extern void IoDisableNMI(void); extern void IdtRegisterIrq(void (*isr)(void), uchar irq, uchar flags); +typedef struct +{ + uchar sec; + uchar min; + uchar hour; + uchar weekday; + uchar day; + uchar month; + uchar year; + uchar century; +} Time_t; + +extern void IoPrintRtcTime(void); + // // Restore IRQ flag to its state before KePauseIRQs // diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index fb67032..eb160c9 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -96,6 +96,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) IoEnableRtc(); IoEnableKeyb(); + IoPrintRtcTime(); while (1) { } diff --git a/kaleid/kernel/io/rtc.c b/kaleid/kernel/io/rtc.c index 984a465..376b6bf 100644 --- a/kaleid/kernel/io/rtc.c +++ b/kaleid/kernel/io/rtc.c @@ -30,11 +30,111 @@ extern void RtcIsr(void); ulong IoRtcTicks = 0; +Time_t IoRtcTime; + +char* WeekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; + + void IoSetupRtc(void) { IdtRegisterIrq(RtcIsr, 0x28, 0x8E); } +void IoGetTimeFromRtc(void) +{ + char time24or12Mode; + Time_t lastTime; + + char updateInProgress = 1; + + while(updateInProgress) { // wait while the RTC updates its value + IoWriteByteOnPort(0x70, 0x0A); + updateInProgress = (IoReadByteFromPort(0x71) & 0x80); + } + + IoWriteByteOnPort(0x70, 0x0); + IoRtcTime.sec = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x02); + IoRtcTime.min = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x04); + IoRtcTime.hour = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x06); + IoRtcTime.weekday = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x07); + IoRtcTime.day = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x08); + IoRtcTime.month = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x09); + IoRtcTime.year = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x32); + IoRtcTime.century = IoReadByteFromPort(0x71); + + // Now while we don't get the same value, read the registers (ensure data are valid) + do { + lastTime.sec = IoRtcTime.sec; + lastTime.min = IoRtcTime.min; + lastTime.hour = IoRtcTime.hour; + lastTime.weekday = IoRtcTime.weekday; + lastTime.day = IoRtcTime.day; + lastTime.month = IoRtcTime.month; + lastTime.year = IoRtcTime.year; + lastTime.century = IoRtcTime.century; + + while(updateInProgress) { // wait while the RTC updates its value + IoWriteByteOnPort(0x70, 0x0A); + updateInProgress = (IoReadByteFromPort(0x71) & 0x80); + } + + IoWriteByteOnPort(0x70, 0x0); + IoRtcTime.sec = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x02); + IoRtcTime.min = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x04); + IoRtcTime.hour = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x06); + IoRtcTime.weekday = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x07); + IoRtcTime.day = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x08); + IoRtcTime.month = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x09); + IoRtcTime.year = IoReadByteFromPort(0x71); + IoWriteByteOnPort(0x70, 0x32); + IoRtcTime.century = IoReadByteFromPort(0x71); + + } while ( (lastTime.sec != IoRtcTime.sec) || (lastTime.min != IoRtcTime.min) || + (lastTime.hour != IoRtcTime.hour) || (lastTime.weekday != IoRtcTime.weekday) || + (lastTime.day != IoRtcTime.day) || (lastTime.month != IoRtcTime.month) || + (lastTime.year != IoRtcTime.year) || (lastTime.century != IoRtcTime.century) + ); + + IoWriteByteOnPort(0x70, 0x32); + time24or12Mode = IoReadByteFromPort(0x71); + + // Convert to binary if it is necessary + if (!(time24or12Mode & 0x04)) { + IoRtcTime.sec = (IoRtcTime.sec & 0x0F) + + ((IoRtcTime.sec / 16) * 10); + IoRtcTime.min = (IoRtcTime.min & 0x0F) + + ((IoRtcTime.min / 16) * 10); + IoRtcTime.hour = ( (IoRtcTime.hour & 0x0F) + + (((IoRtcTime.hour & 0x70) / 16) * 10) ) + | (IoRtcTime.hour & 0x80); + IoRtcTime.day = (IoRtcTime.day & 0x0F) + ((IoRtcTime.day / 16) * 10); + IoRtcTime.month = (IoRtcTime.month & 0x0F) + + ((IoRtcTime.month / 16) * 10); + IoRtcTime.year = (IoRtcTime.year & 0x0F) + + ((IoRtcTime.year / 16) * 10); + IoRtcTime.century = (IoRtcTime.century & 0x0F) + + ((IoRtcTime.century / 16) * 10); + } + + // Convert 12 to 24 hour if necessary + if (!(time24or12Mode & 0x02) && (IoRtcTime.hour & 0x80)) { + IoRtcTime.hour = ((IoRtcTime.hour & 0x7F) + 12) % 24; + } +} + void IoEnableRtc(void) { ulong flags = KePauseIRQs(); @@ -67,6 +167,8 @@ void IoEnableRtc(void) // clean-up IoWriteByteOnPort(0x70, 0x0C); // Select status reg C IoReadByteFromPort(0x71); // Flush + + IoGetTimeFromRtc(); KeRestoreIRQs(flags); IoEnableNMI(); } @@ -80,3 +182,16 @@ void RtcHandler(void) IoSendEOItoPIC(0x28); //bprintf(BStdOut, " - EOI* "); } + +void IoPrintRtcTime(void) +{ + KernLog("[RTC Time] %s %d/%d/%d ; %d:%d:%d\n", + WeekDays[IoRtcTime.weekday], + IoRtcTime.day, + IoRtcTime.month, + IoRtcTime.year + IoRtcTime.century*100, + IoRtcTime.hour, + IoRtcTime.min, + IoRtcTime.sec + ); +}