add: rtc support

This commit is contained in:
starnakin 2024-09-18 21:45:10 +02:00
parent ddf3cfff68
commit c7545d5fc6
2 changed files with 88 additions and 0 deletions

32
headers/rtc.h Normal file
View File

@ -0,0 +1,32 @@
#pragma once
#include <stdint.h>
#define SECOND_REGISTER 0x00
#define MINUTE_REGISTER 0x02
#define HOUR_REGISTER 0x04
#define DAY_OF_THE_MONTH_REGISTER 0x07
#define MONTH_REGISTER 0x08
#define YEAR_REGISTER 0x09
#define CENTURY_REGISTER 0x32
#define REGISTER_A 0x0A
#define REGISTER_B 0x0B
enum {
CMOS_ADDRESS = 0x70,
CMOS_DATA = 0x71
};
struct rtc_date {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t index_of_the_day;
uint8_t day;
uint8_t month;
uint32_t year;
};
struct rtc_date get_date(void);

56
src/rtc/rtc.c Normal file
View File

@ -0,0 +1,56 @@
#include "sys/io.h"
#include "rtc.h"
#include "kprintf.h"
static uint16_t raw_read_register(uint16_t cmos_register)
{
//selecting the register
outb(CMOS_ADDRESS, cmos_register);
//read value of the selectect register
return inb(CMOS_DATA);
}
static uint8_t update_is_in_progress(void)
{
return raw_read_register(REGISTER_A) & 0x80;
}
static uint16_t read_register(uint16_t cmos_register)
{
while (update_is_in_progress())
kprintf(0, "%d\n", update_is_in_progress());
return raw_read_register(cmos_register);
}
uint8_t bcd_mode_to_bin(uint8_t value)
{
return (value & 0x0F) + ((value/ 16) * 10);
}
struct rtc_date get_date(void)
{
struct rtc_date rv;
uint8_t century;
rv.second = read_register(SECOND_REGISTER);
rv.minute = read_register(MINUTE_REGISTER);
rv.hour = read_register(HOUR_REGISTER);
rv.day = read_register(DAY_OF_THE_MONTH_REGISTER);
rv.month = read_register(MONTH_REGISTER);
rv.year += read_register(YEAR_REGISTER);
century = read_register(CENTURY_REGISTER);
if (!(read_register(REGISTER_B) & 0x04)) {
rv.second = bcd_mode_to_bin(rv.second);
rv.minute = bcd_mode_to_bin(rv.minute);
rv.hour = bcd_mode_to_bin(rv.hour);
rv.day = bcd_mode_to_bin(rv.day);
rv.month = bcd_mode_to_bin(rv.month);
rv.year = bcd_mode_to_bin(rv.year);
century = bcd_mode_to_bin(century);
}
rv.year += century * 100 - 108; // -108 = bozo offset
return rv;
}