From 72b7ec8a0d6af45171e505815f1376f4888fb59f Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 25 Jun 2020 03:07:53 +0200 Subject: Base: added timer Also added usleep and sleep functions --- src/pOS/arch/x86/kernel/timer.cpp | 50 +++++++++++++++++++++++++++++++++++++++ src/pOS/arch/x86/libc/unistd.cpp | 11 +++++++++ src/pOS/include/kernel/kernel.h | 2 ++ src/pOS/include/kernel/timer.h | 27 +++++++++++++++++++++ src/pOS/include/libc/unistd.h | 9 +++++++ src/pOS/kernel/kernel.cpp | 3 +++ 6 files changed, 102 insertions(+) create mode 100644 src/pOS/arch/x86/kernel/timer.cpp create mode 100644 src/pOS/arch/x86/libc/unistd.cpp create mode 100644 src/pOS/include/kernel/timer.h create mode 100644 src/pOS/include/libc/unistd.h (limited to 'src') diff --git a/src/pOS/arch/x86/kernel/timer.cpp b/src/pOS/arch/x86/kernel/timer.cpp new file mode 100644 index 0000000..113dbbf --- /dev/null +++ b/src/pOS/arch/x86/kernel/timer.cpp @@ -0,0 +1,50 @@ +#include + +uint16_t Timer::hz = 0; +volatile uint32_t Timer::ms = 0; + +#include + +int Timer::init(void) +{ + set_frequency(1000); + IRQ::install_handler(0, Timer::timer_handler); + + return 0; +} + +int Timer::set_frequency(uint16_t frec) +{ + hz = frec; + uint16_t divisor = INPUT_CLOCK_FREQUENCY / frec; + + /* Init, Square Wave Mode, non-BCD, first transfer LSB then MSB */ + System::outb(TIMER_COMMAND, TIMER_ICW); + System::outb(TIMER_DATA, divisor & 0xFF); + System::outb(TIMER_DATA, (divisor >> 8) & 0xFF); + + return 0; +} + +uint32_t Timer::get_ms_from_boot(void) +{ + return ms; +} + +void Timer::sleep(uint32_t sec) +{ + uint32_t end = ms + sec * hz; + while(ms < end); +} + +void Timer::usleep(uint32_t usec) +{ + uint32_t end = ms + usec; + while(ms < end); +} + +void Timer::timer_handler(regs* reg) +{ + UNUSED_VARIABLE(reg); + ms++; +} diff --git a/src/pOS/arch/x86/libc/unistd.cpp b/src/pOS/arch/x86/libc/unistd.cpp new file mode 100644 index 0000000..eb8e748 --- /dev/null +++ b/src/pOS/arch/x86/libc/unistd.cpp @@ -0,0 +1,11 @@ +#include + +void sleep(uint32_t sec) +{ + Timer::sleep(sec); +} + +void usleep(uint32_t usec) +{ + Timer::usleep(usec); +} diff --git a/src/pOS/include/kernel/kernel.h b/src/pOS/include/kernel/kernel.h index cbe5f3d..2c22263 100644 --- a/src/pOS/include/kernel/kernel.h +++ b/src/pOS/include/kernel/kernel.h @@ -2,11 +2,13 @@ #define _KERNEL_H_ #include +#include #include #include #include #include +#include #include #include #include diff --git a/src/pOS/include/kernel/timer.h b/src/pOS/include/kernel/timer.h new file mode 100644 index 0000000..4d202db --- /dev/null +++ b/src/pOS/include/kernel/timer.h @@ -0,0 +1,27 @@ +#ifndef _TIMER_H_ +#define _TIMER_H_ + +#include +#include +#include + +#define INPUT_CLOCK_FREQUENCY 1193180 +#define TIMER_COMMAND 0x43 +#define TIMER_DATA 0x40 +#define TIMER_ICW 0x36 + +class Timer +{ +public: + static int init(void); + static uint32_t get_ms_from_boot(void); + static void sleep(uint32_t sec); + static void usleep(uint32_t usec); +private: + static int set_frequency(uint16_t frec); + static void timer_handler(regs* reg); + static uint16_t hz; + static volatile uint32_t ms; +}; + +#endif diff --git a/src/pOS/include/libc/unistd.h b/src/pOS/include/libc/unistd.h new file mode 100644 index 0000000..b8283d7 --- /dev/null +++ b/src/pOS/include/libc/unistd.h @@ -0,0 +1,9 @@ +#ifndef _UNISTD_H_ +#define _UNISTD_H_ + +#include + +void sleep(uint32_t sec); +void usleep(uint32_t usec); + +#endif diff --git a/src/pOS/kernel/kernel.cpp b/src/pOS/kernel/kernel.cpp index 1de0818..742c205 100644 --- a/src/pOS/kernel/kernel.cpp +++ b/src/pOS/kernel/kernel.cpp @@ -6,6 +6,7 @@ extern "C" int kmain(void) Debug::set_serial_debug(true); #endif + Timer::init(); TTY::tty_initialize(); GDT::init(); @@ -23,6 +24,8 @@ extern "C" int kmain(void) printf("Current time: %s (%s-%d-%d%d)\n", Time::get_date_formatted(), Time::get_month_string(), Time::get_day(), Time::get_century() - 1, Time::get_year()); + usleep(200); + Shell::init(); System::idle_loop(); -- cgit 1.4.1