From 9014c1f1674339d4dfb920b81a25815799a4069a Mon Sep 17 00:00:00 2001 From: Within Date: Thu, 24 Oct 2019 11:24:17 -0400 Subject: [PATCH] create interface around the clock --- src/clock.rs | 20 ++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 12 +++--------- 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 src/clock.rs diff --git a/src/clock.rs b/src/clock.rs new file mode 100644 index 0000000..d4de373 --- /dev/null +++ b/src/clock.rs @@ -0,0 +1,20 @@ +use chrono::prelude::*; +use cmos::{CMOSCenturyHandler, CMOS}; +use core::convert::TryInto; +use lazy_static::lazy_static; +use spin::Mutex; + +lazy_static! { + static ref CLOCK: Mutex = Mutex::new(unsafe { CMOS::new() }); +} + +pub fn current_time() -> DateTime { + let rtc = CLOCK.lock().read_rtc(CMOSCenturyHandler::CurrentYear(2019)); + return Utc + .ymd( + rtc.year.try_into().unwrap(), + rtc.month.into(), + rtc.day.into(), + ) + .and_hms(rtc.hour.into(), rtc.minute.into(), rtc.second.into()); +} diff --git a/src/lib.rs b/src/lib.rs index ae4cd13..b15ee4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ extern crate alloc; pub mod allocator; +pub mod clock; pub mod gdt; pub mod interrupts; pub mod memory; diff --git a/src/main.rs b/src/main.rs index 3d268e4..dd2984b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate alloc; +use alloc::string::ToString; use bootloader::{entry_point, BootInfo}; use core::panic::PanicInfo; use xe_os::println; @@ -13,21 +14,14 @@ use xe_os::println; entry_point!(kernel_main); fn kernel_main(boot_info: &'static BootInfo) -> ! { - use cmos::{CMOSCenturyHandler, CMOS}; - // Create a CMOS object (unsafe due to the use of port I/O) - let mut cmos = unsafe { CMOS::new() }; - xe_os::init(&boot_info); #[cfg(test)] test_main(); // Read the rtc date time using this year - let rtc = cmos.read_rtc(CMOSCenturyHandler::CurrentYear(2019)); - println!( - "current time: {}:{}:{} {} M{} {}", - rtc.hour, rtc.minute, rtc.second, rtc.year, rtc.month, rtc.day - ); + let now = xe_os::clock::current_time(); + println!("{}", now.to_string()); println!("Running WASM:");