diff options
author | Your Name <you@example.com> | 2020-06-25 03:07:53 +0200 |
---|---|---|
committer | Your Name <you@example.com> | 2020-06-25 03:07:53 +0200 |
commit | 72b7ec8a0d6af45171e505815f1376f4888fb59f (patch) | |
tree | b9a5714c67230c0ade51ddddb5ba169230d91e54 | |
parent | Base: added support for signals. (diff) | |
download | pOS-72b7ec8a0d6af45171e505815f1376f4888fb59f.tar.gz pOS-72b7ec8a0d6af45171e505815f1376f4888fb59f.tar.bz2 pOS-72b7ec8a0d6af45171e505815f1376f4888fb59f.zip |
Base: added timer
Also added usleep and sleep functions
-rw-r--r-- | src/pOS/arch/x86/kernel/timer.cpp | 50 | ||||
-rw-r--r-- | src/pOS/arch/x86/libc/unistd.cpp | 11 | ||||
-rw-r--r-- | src/pOS/include/kernel/kernel.h | 2 | ||||
-rw-r--r-- | src/pOS/include/kernel/timer.h | 27 | ||||
-rw-r--r-- | src/pOS/include/libc/unistd.h | 9 | ||||
-rw-r--r-- | src/pOS/kernel/kernel.cpp | 3 |
6 files changed, 102 insertions, 0 deletions
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 <kernel/timer.h> + +uint16_t Timer::hz = 0; +volatile uint32_t Timer::ms = 0; + +#include <kernel/debug.h> + +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 <unistd.h> + +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 <stdio.h> +#include <unistd.h> #include <math.h> #include <assert.h> #include <kernel/shell/shell.h> #include <kernel/time.h> +#include <kernel/timer.h> #include <kernel/debug.h> #include <kernel/drivers.h> #include <kernel/acpi.h> 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 <stdint.h> +#include <kernel/system.h> +#include <kernel/interrupts/irq.h> + +#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 <kernel/timer.h> + +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(); |