os-k/kaleid/kernel/ke/rtc.c

319 lines
9.9 KiB
C
Raw Normal View History

2019-04-24 18:43:05 +02:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
2019-05-07 18:35:46 +02:00
// Desc: RTC Time related functions //
2019-04-24 18:43:05 +02:00
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
2019-05-13 23:22:27 +02:00
#include <lib/buf.h>
#include <ke/time.h>
2019-05-14 11:48:07 +02:00
#include <ke/idt.h>
2019-04-24 18:43:05 +02:00
2019-05-14 11:48:07 +02:00
static ulong Ticks = 0;
static Time_t OriginTime;
static Time_t CurTime;
// TODO asnprintf()
static char TimeFmtBuf[22] = { 0 };
static uchar RTC_RATE = 0x05; //2048Hz
2019-04-25 16:31:06 +02:00
static char time24or12Mode;
2019-04-24 21:08:44 +02:00
2019-05-14 11:48:07 +02:00
static void GetTimeFromRTC(void)
2019-04-25 12:23:45 +02:00
{
Time_t lastTime;
char updateInProgress = 1;
2019-05-14 11:48:07 +02:00
// Wait while the RTC updates its value
while (updateInProgress) {
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x0A);
updateInProgress = (IoReadByteFromPort(0x71) & 0x80);
}
IoWriteByteOnPort(0x70, 0x0);
2019-05-14 11:48:07 +02:00
OriginTime.sec = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x02);
2019-05-14 11:48:07 +02:00
OriginTime.min = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x04);
2019-05-14 11:48:07 +02:00
OriginTime.hour = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x06);
2019-05-14 11:48:07 +02:00
OriginTime.weekday = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x07);
2019-05-14 11:48:07 +02:00
OriginTime.day = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x08);
2019-05-14 11:48:07 +02:00
OriginTime.month = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x09);
2019-05-14 11:48:07 +02:00
OriginTime.year = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x32);
2019-05-14 11:48:07 +02:00
OriginTime.century = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
// Now while we don't get the same value, read the registers (ensure data are valid)
do {
2019-05-14 11:48:07 +02:00
lastTime.sec = OriginTime.sec;
lastTime.min = OriginTime.min;
lastTime.hour = OriginTime.hour;
lastTime.weekday = OriginTime.weekday;
lastTime.day = OriginTime.day;
lastTime.month = OriginTime.month;
lastTime.year = OriginTime.year;
lastTime.century = OriginTime.century;
while (updateInProgress) {
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x0A);
updateInProgress = (IoReadByteFromPort(0x71) & 0x80);
}
IoWriteByteOnPort(0x70, 0x0);
2019-05-14 11:48:07 +02:00
OriginTime.sec = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x02);
2019-05-14 11:48:07 +02:00
OriginTime.min = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x04);
2019-05-14 11:48:07 +02:00
OriginTime.hour = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x06);
2019-05-14 11:48:07 +02:00
OriginTime.weekday = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x07);
2019-05-14 11:48:07 +02:00
OriginTime.day = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x08);
2019-05-14 11:48:07 +02:00
OriginTime.month = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
IoWriteByteOnPort(0x70, 0x09);
2019-05-14 11:48:07 +02:00
OriginTime.year = IoReadByteFromPort(0x71);
2019-04-25 12:23:45 +02:00
2019-05-14 11:48:07 +02:00
IoWriteByteOnPort(0x70, 0x32);
OriginTime.century = IoReadByteFromPort(0x71);
} while ((lastTime.sec != OriginTime.sec)
|| (lastTime.min != OriginTime.min)
|| (lastTime.hour != OriginTime.hour)
|| (lastTime.weekday != OriginTime.weekday)
|| (lastTime.day != OriginTime.day)
|| (lastTime.month != OriginTime.month)
|| (lastTime.year != OriginTime.year)
|| (lastTime.century != OriginTime.century)
2019-04-25 12:23:45 +02:00
);
2019-04-25 16:31:06 +02:00
IoWriteByteOnPort(0x70, 0x0B);
2019-04-25 12:23:45 +02:00
time24or12Mode = IoReadByteFromPort(0x71);
2019-05-14 11:48:07 +02:00
2019-04-25 12:23:45 +02:00
// Convert to binary if it is necessary
if (!(time24or12Mode & 0x04)) {
2019-05-14 11:48:07 +02:00
OriginTime.sec = (OriginTime.sec & 0x0F)
+ ((OriginTime.sec / 16) * 10);
OriginTime.min = (OriginTime.min & 0x0F)
+ ((OriginTime.min / 16) * 10);
OriginTime.hour = ( (OriginTime.hour & 0x0F)
+ (((OriginTime.hour & 0x70) / 16) * 10) )
| (OriginTime.hour & 0x80);
OriginTime.day = (OriginTime.day & 0x0F)
+ ((OriginTime.day / 16) * 10);
OriginTime.month = (OriginTime.month & 0x0F)
+ ((OriginTime.month / 16) * 10);
OriginTime.year = (OriginTime.year & 0x0F)
+ ((OriginTime.year / 16) * 10);
OriginTime.century = (OriginTime.century & 0x0F)
+ ((OriginTime.century / 16) * 10);
OriginTime.weekday = (OriginTime.weekday & 0x0F)
+ ((OriginTime.weekday / 16) * 10);
2019-04-25 12:23:45 +02:00
}
// Convert 12 to 24 hour if necessary
2019-05-14 11:48:07 +02:00
if (!(time24or12Mode & 0x02) && (OriginTime.hour & 0x80)) {
OriginTime.hour = ((OriginTime.hour & 0x7)+ 10) % 24;
2019-04-25 12:23:45 +02:00
}
2019-04-25 16:31:06 +02:00
2019-05-14 11:48:07 +02:00
CurTime.sec = OriginTime.sec;
CurTime.min = OriginTime.min;
CurTime.hour = OriginTime.hour;
CurTime.weekday = OriginTime.weekday;
CurTime.day = OriginTime.day;
CurTime.month = OriginTime.month;
CurTime.year = OriginTime.year;
CurTime.century = OriginTime.century;
2019-04-25 12:23:45 +02:00
}
2019-11-11 23:57:31 +01:00
//
// ISR handler for the real time clock
//
2019-05-14 11:48:07 +02:00
static void HandleRTC(ISRFrame_t *regs)
2019-04-24 21:08:44 +02:00
{
IoWriteByteOnPort(0x70, 0x0C); // Selects status reg C
IoReadByteFromPort(0x71); // Flush
2019-05-14 11:48:07 +02:00
Ticks++;
KeSendEOItoPIC(0x28);
2019-04-24 21:08:44 +02:00
}
2019-04-25 12:23:45 +02:00
2019-05-14 11:48:07 +02:00
char *KeFormatCurTime(void)
2019-04-25 12:23:45 +02:00
{
2019-05-14 11:48:07 +02:00
Time_t *RtcTime = KeGetCurTime();
snprintf(TimeFmtBuf, sizeof(TimeFmtBuf),
2019-05-14 11:56:42 +02:00
"%02d/%02d/%04d - %02d:%02d:%02d",
2019-04-25 16:31:06 +02:00
RtcTime->day,
RtcTime->month,
RtcTime->year + RtcTime->century*100,
RtcTime->hour,
RtcTime->min,
RtcTime->sec
);
2019-05-14 11:48:07 +02:00
return TimeFmtBuf;
2019-04-25 16:31:06 +02:00
}
2019-05-14 11:48:07 +02:00
static void UpdateCurTime(void)
2019-04-25 16:31:06 +02:00
{
2019-05-14 11:48:07 +02:00
ulong frequency = 32768 >> (RTC_RATE - 1);
2019-04-25 16:31:06 +02:00
uchar minRemain, hourRemain, dayRemain;
2019-05-14 11:48:07 +02:00
CurTime.sec =
(uchar)(((ulong)OriginTime.sec + (Ticks / frequency)) % 60);
2019-04-25 16:31:06 +02:00
minRemain =
2019-05-14 11:48:07 +02:00
(uchar)(((ulong)OriginTime.sec + (Ticks / frequency)) / 60);
CurTime.min =
(uchar)(((ulong)OriginTime.min + minRemain) % 60);
2019-04-25 16:31:06 +02:00
hourRemain =
2019-05-14 11:48:07 +02:00
(uchar)(((ulong)OriginTime.min + minRemain) / 60);
CurTime.hour =
(uchar)(((ulong)OriginTime.hour + hourRemain) % 24);
2019-04-25 16:31:06 +02:00
dayRemain =
2019-05-14 11:48:07 +02:00
(uchar)(((ulong)OriginTime.hour + hourRemain) / 24);
2019-04-25 16:31:06 +02:00
2019-11-14 00:54:34 +01:00
CurTime.day =
(uchar)(((ulong)OriginTime.day + dayRemain) % 30);
2019-04-25 16:31:06 +02:00
}
2019-05-14 11:48:07 +02:00
Time_t* KeGetCurTime(void)
2019-04-25 13:08:54 +02:00
{
2019-05-14 11:48:07 +02:00
UpdateCurTime();
return &CurTime;
2019-04-25 13:08:54 +02:00
}
2019-05-13 20:34:41 +02:00
static uint IsLeapYear(uint year)
{
if (!(year % 4)) {
return 0;
}
return year % 100 == 0
? (year % 400 == 0)
: 1;
}
static uint DaysInMonth(uint month, uint year)
{
return (month == 2)
? (28 + IsLeapYear(year))
: 31 - (month - 1) % 7 % 2;
}
2019-05-14 11:48:07 +02:00
ulong KeGetTimeStamp(void)
2019-05-13 20:34:41 +02:00
{
2019-05-14 11:48:07 +02:00
Time_t *time = KeGetCurTime();
2019-05-13 20:34:41 +02:00
uint dpy = 365 + IsLeapYear(time->year);
uint dim = DaysInMonth(time->month, time->year + time->century * 100);
return time->sec
+ time->min * 60
+ time->hour * 60 * 60
+ time->day * 24 * 60 * 60
+ time->month * dim * 24 * 60 * 60
+ (time->year + time->century * 100)
* dpy * 24 * 60 * 60;
}
2019-05-14 11:48:07 +02:00
ulong KeGetClockTicks(void)
2019-04-25 13:08:54 +02:00
{
2019-05-14 11:48:07 +02:00
return Ticks;
2019-04-25 13:08:54 +02:00
}
2019-05-14 11:48:07 +02:00
void KeEnableRTC(void)
2019-04-27 00:04:27 +02:00
{
ulong flags = KePauseIRQs();
2019-05-05 18:55:00 +02:00
char readInterruptConfig;
char readRegister;
2019-04-27 00:04:27 +02:00
2019-05-14 11:48:07 +02:00
KeRegisterISR(HandleRTC, 0x28);
2019-04-27 00:04:27 +02:00
// Setting up the register control and interrupt rates
2019-05-17 21:10:22 +02:00
DebugLog("\tRTC interrupt frequency set to %d Hz\n",
2019-05-14 11:48:07 +02:00
32768 >> (RTC_RATE - 1));
2019-04-27 00:04:27 +02:00
IoWriteByteOnPort(0x70, 0x8B);
2019-05-05 18:55:00 +02:00
readRegister = IoReadByteFromPort(0x71);
2019-04-27 00:04:27 +02:00
IoWriteByteOnPort(0x70, 0x8B); // Because reading flushes
2019-05-05 18:55:00 +02:00
IoWriteByteOnPort(0x71, readRegister | 0x40);
2019-04-27 00:04:27 +02:00
IoWriteByteOnPort(0x70, 0x8A);
2019-05-05 18:55:00 +02:00
readInterruptConfig = IoReadByteFromPort(0x71);
2019-04-27 00:04:27 +02:00
IoWriteByteOnPort(0x70, 0x8A); // Because reading flushes
2019-05-14 11:48:07 +02:00
IoWriteByteOnPort(0x71, (readInterruptConfig & 0xF0) | RTC_RATE);
2019-04-27 00:04:27 +02:00
IoWriteByteOnPort(0x70, 0x0C);
IoReadByteFromPort(0x71); // Flush
// Setting up the IRQs
2019-11-14 00:54:34 +01:00
KeUnmaskIRQ(8);
2019-04-27 00:04:27 +02:00
// Clean-up
IoWriteByteOnPort(0x70, 0x0C); // Select status reg C
IoReadByteFromPort(0x71); // Flush
2019-05-14 11:48:07 +02:00
GetTimeFromRTC();
2019-04-27 00:04:27 +02:00
KeRestoreIRQs(flags);
2019-05-14 11:48:07 +02:00
KeEnableNMI();
2019-11-14 00:54:34 +01:00
srand(KeGetTimeStamp()); // Initializes the kernel number generator
2019-04-27 00:04:27 +02:00
}
2019-05-14 11:48:07 +02:00
void KeDelayExecution(uint time)
2019-05-07 17:02:25 +02:00
{
2019-05-14 11:48:07 +02:00
ulong frequency = 32768 >> (RTC_RATE - 1);
ulong beginTick = KeGetClockTicks();
while (KeGetClockTicks() < beginTick + (frequency/1000) * time) {
KeRelaxCPU();
2019-05-07 17:02:25 +02:00
}
}