Added time RTC library

This commit is contained in:
Cetica Marco
2021-01-02 17:49:15 +01:00
parent 5880c473fc
commit 21f017f13c
29 changed files with 157 additions and 67 deletions

View File

@@ -1,4 +1,4 @@
OBJS = stdio.o string.o assert.o
OBJS = stdio.o string.o assert.o time.o
CC = i686-elf-gcc # cross-compiler
CFLAGS = -m32 -fno-stack-protector -ffreestanding -Wall -Wextra -Werror -g -c
@@ -7,4 +7,4 @@ CFLAGS = -m32 -fno-stack-protector -ffreestanding -Wall -Wextra -Werror -g -c
all:${OBJS}
%.o: %.c
$(CC) $(CFLAGS) $< -o $@
$(CC) $(CFLAGS) $< -o $@

View File

@@ -1,7 +1,7 @@
/**************************************
* iceOS Kernel *
* VulcanOS Kernel *
* Developed by Marco 'icebit' Cetica *
* (c) 2019 *
* (c) 2019-2021 *
* Released under GPLv3 *
* https://github.com/ice-bit/iceOS *
***************************************/
@@ -18,4 +18,4 @@
extern void panic(const char *message, const char *file, uint32_t line);
extern void panic_assert(const char *file, uint32_t line, const char *desc);
#endif
#endif

View File

@@ -1,11 +1,10 @@
/**************************************
* iceOS Kernel *
* VulcanOS Kernel *
* Developed by Marco 'icebit' Cetica *
* (c) 2019 *
* (c) 2019-2021 *
* Released under GPLv3 *
* https://github.com/ice-bit/iceOS *
***************************************/
#ifndef _STDIO_H_
#define _STDIO_H_
@@ -17,4 +16,4 @@ int printf(const char *format, ...);
int printf_color(const char *format, uint8_t fg, uint8_t bg); // Only for string for now
void puts(const char *buf);
#endif
#endif

View File

@@ -1,11 +1,10 @@
/**************************************
* iceOS Kernel *
* VulcanOS Kernel *
* Developed by Marco 'icebit' Cetica *
* (c) 2019 *
* (c) 2019-2021 *
* Released under GPLv3 *
* https://github.com/ice-bit/iceOS *
***************************************/
#ifndef _STRING_H_
#define _STRING_H_

38
kernel/libc/time.c Normal file
View File

@@ -0,0 +1,38 @@
#include "time.h"
#include "../drivers/ports.h"
// Check whether CMOS is updated or not
static uint8_t is_cmos_updated() {
outb(CMOS_ADDRESS, 0x0A);
return (inb(CMOS_DATA) & 0x80);
}
// Get CMOS register's status
static uint8_t reg_status(int32_t reg) {
outb(CMOS_ADDRESS, reg);
return inb(CMOS_DATA);
}
time_t cmos_reader() {
while(is_cmos_updated()); // Wait until the CMOS is being updated
time_t tm;
tm.second = BCD_CONVERTER(reg_status(TIME_R_SECOND));
tm.minute = BCD_CONVERTER(reg_status(TIME_R_MINUTE));
tm.hour = BCD_CONVERTER(reg_status(TIME_R_HOUR));
tm.day = BCD_CONVERTER(reg_status(TIME_R_DAY));
tm.month = BCD_CONVERTER(reg_status(TIME_R_MONTH));
tm.year = BCD_CONVERTER(reg_status(TIME_R_YEAR));
return tm;
}
uint32_t get_time(uint32_t field) {
while(is_cmos_updated()); // Wait the CMOS is being updated
return BCD_CONVERTER(reg_status(field));
}

42
kernel/libc/time.h Normal file
View File

@@ -0,0 +1,42 @@
/**************************************
* VulcanOS Kernel *
* Developed by Marco 'icebit' Cetica *
* (c) 2019-2021 *
* Released under GPLv3 *
* https://github.com/ice-bit/iceOS *
***************************************/
/* Get time reading the RTC(real time clock) CMOS on the motherboard */
#ifndef TIME_H
#define TIME_H
#include <stdint.h>
// Define RTC field registers
#define TIME_R_YEAR 0x09
#define TIME_R_MONTH 0x08
#define TIME_R_DAY 0x07
#define TIME_R_HOUR 0x06
#define TIME_R_MINUTE 0x05
#define TIME_R_SECOND 0x04
//#define TIME_R_CENTURY 0x32
// Define RTC address
#define CMOS_ADDRESS 0x70
#define CMOS_DATA 0x71
// Convert BCD encoed values to binary
#define BCD_CONVERTER(n) ((n / 16) * 10 + (n & 0xF))
typedef struct {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day;
uint8_t month;
uint8_t year;
} time_t;
time_t cmos_reader();
uint32_t get_time(uint32_t field);
#endif