mirror of
https://github.com/vinc/moros.git
synced 2024-06-28 19:57:12 +00:00
Read RTC from CMOS
This commit is contained in:
parent
6167a69fe7
commit
451b63866c
|
@ -1,9 +1,23 @@
|
||||||
// use crate::print;
|
|
||||||
use crate::kernel::cmos::CMOS;
|
use crate::kernel::cmos::CMOS;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use spin::Mutex;
|
use spin::Mutex;
|
||||||
|
|
||||||
|
const DAYS_IN_MONTH: [u16; 12] = [
|
||||||
|
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
||||||
|
];
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
static ref DAYS_SINCE_MONTH: [u16; 12] = {
|
||||||
|
let mut days_since_month = [0; 12];
|
||||||
|
for m in 0..12 {
|
||||||
|
days_since_month[m] = DAYS_IN_MONTH[m];
|
||||||
|
if m > 0 {
|
||||||
|
days_since_month[m] += days_since_month[m - 1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
days_since_month
|
||||||
|
};
|
||||||
|
|
||||||
pub static ref TICKS: Mutex<usize> = Mutex::new(0);
|
pub static ref TICKS: Mutex<usize> = Mutex::new(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,10 +32,17 @@ pub fn clock_monotonic() -> f64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clock_realtime() -> f64 {
|
pub fn clock_realtime() -> f64 {
|
||||||
let mut cmos = CMOS::new();
|
let rtc = CMOS::new().rtc();
|
||||||
let rtc = cmos.read();
|
|
||||||
// print!("{:?}\n", rtc);
|
let days_since_year = 365.25 * (rtc.year - 1970) as f64;
|
||||||
let t = rtc.second as u64 + 60 * rtc.minute as u64 + 3600 * rtc.hour as u64;
|
let days_since_month = DAYS_SINCE_MONTH[(rtc.month as usize) - 1] as f64;
|
||||||
|
|
||||||
|
let t = 86400.0 * days_since_year
|
||||||
|
+ 86400.0 * days_since_month
|
||||||
|
+ 86400.0 * rtc.day as f64
|
||||||
|
+ 3600.0 * rtc.hour as f64
|
||||||
|
+ 60.0 * rtc.minute as f64
|
||||||
|
+ rtc.second as f64;
|
||||||
|
|
||||||
t as f64
|
t as f64
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,12 +14,12 @@ enum Register {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RTC {
|
pub struct RTC {
|
||||||
pub second: u8,
|
pub year: u16,
|
||||||
pub minute: u8,
|
|
||||||
pub hour: u8,
|
|
||||||
pub day: u8,
|
|
||||||
pub month: u8,
|
pub month: u8,
|
||||||
pub year: u8,
|
pub day: u8,
|
||||||
|
pub hour: u8,
|
||||||
|
pub minute: u8,
|
||||||
|
pub second: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CMOS {
|
pub struct CMOS {
|
||||||
|
@ -35,7 +35,7 @@ impl CMOS {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(&mut self) -> RTC {
|
pub fn rtc(&mut self) -> RTC {
|
||||||
while self.is_updating() {
|
while self.is_updating() {
|
||||||
print!(""); // TODO: sleep
|
print!(""); // TODO: sleep
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ impl CMOS {
|
||||||
let mut hour = self.read_register(Register::Hour);
|
let mut hour = self.read_register(Register::Hour);
|
||||||
let mut day = self.read_register(Register::Day);
|
let mut day = self.read_register(Register::Day);
|
||||||
let mut month = self.read_register(Register::Month);
|
let mut month = self.read_register(Register::Month);
|
||||||
let mut year = self.read_register(Register::Year);
|
let mut year = self.read_register(Register::Year) as u16;
|
||||||
|
|
||||||
let b = self.read_register(Register::B);
|
let b = self.read_register(Register::B);
|
||||||
if b & 0x04 == 0 {
|
if b & 0x04 == 0 {
|
||||||
|
@ -56,7 +56,9 @@ impl CMOS {
|
||||||
year = (year & 0x0F) + ((year / 16) * 10);
|
year = (year & 0x0F) + ((year / 16) * 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
RTC { second, minute, hour, day, month, year }
|
year += 2000; // TODO: Don't forget to change this next century
|
||||||
|
|
||||||
|
RTC { year, month, day, hour, minute, second }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_updating(&mut self) -> bool {
|
fn is_updating(&mut self) -> bool {
|
||||||
|
|
|
@ -1,23 +1,42 @@
|
||||||
use crate::print;
|
use crate::print;
|
||||||
use crate::kernel::clock;
|
use crate::kernel::clock;
|
||||||
|
use crate::kernel::cmos::CMOS;
|
||||||
|
|
||||||
pub fn main(args: &[&str]) {
|
pub fn print_time_in_seconds(time: f64) {
|
||||||
let time = clock::clock_realtime();
|
if time < 1.0e3 {
|
||||||
|
print!("{:.3} seconds\n", time);
|
||||||
if args.len() == 2 && args[1] == "--metric" {
|
} else if time < 1.0e6 {
|
||||||
if time < 1000.0 {
|
print!("{:.3} kiloseconds\n", time / 1.0e3);
|
||||||
print!("{:.2} seconds\n", time);
|
} else if time < 1.0e9 {
|
||||||
} else {
|
print!("{:.3} megaseconds\n", time / 1.0e6);
|
||||||
print!("{:.2} kiloseconds\n", time / 1000.0);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let time = 0.0864 * time;
|
print!("{:.3} gigaseconds\n", time / 1.0e9);
|
||||||
if time < 100.0 {
|
}
|
||||||
print!("{:.2} dimidays\n", time);
|
}
|
||||||
} else if time < 10_000.0 {
|
|
||||||
print!("{:.2} centidays\n", time / 100.0);
|
pub fn print_time_in_days(time: f64) {
|
||||||
} else {
|
if time < 0.01 {
|
||||||
print!("{:.2} days\n", time / 10_000.0);
|
print!("{:.2} dimidays\n", time * 10_000.0);
|
||||||
}
|
} else if time < 1.0 {
|
||||||
|
print!("{:.2} centidays\n", time * 100.0);
|
||||||
|
} else {
|
||||||
|
print!("{:.2} days\n", time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main(args: &[&str]) {
|
||||||
|
if args.len() == 2 && args[1] == "--iso-8601" {
|
||||||
|
let rtc = CMOS::new().rtc();
|
||||||
|
print!(
|
||||||
|
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}\n",
|
||||||
|
rtc.year, rtc.day, rtc.month,
|
||||||
|
rtc.hour, rtc.minute, rtc.second
|
||||||
|
);
|
||||||
|
} else if args.len() == 2 && args[1] == "--raw" {
|
||||||
|
print!("{:.6}\n", clock::clock_realtime());
|
||||||
|
} else if args.len() == 2 && args[1] == "--metric" {
|
||||||
|
print_time_in_seconds(clock::clock_realtime());
|
||||||
|
} else {
|
||||||
|
print_time_in_days(clock::clock_realtime() / 86400.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,14 @@
|
||||||
use crate::print;
|
use crate::print;
|
||||||
use crate::kernel::clock;
|
use crate::kernel::clock;
|
||||||
|
use crate::user::date::{print_time_in_seconds, print_time_in_days};
|
||||||
|
|
||||||
pub fn main(args: &[&str]) {
|
pub fn main(args: &[&str]) {
|
||||||
let time = clock::clock_monotonic();
|
let time = clock::clock_monotonic();
|
||||||
|
if args.len() == 2 && args[1] == "--raw" {
|
||||||
if args.len() == 2 && args[1] == "--metric" {
|
print!("{:.6}\n", time);
|
||||||
if time < 1000.0 {
|
} else if args.len() == 2 && args[1] == "--metric" {
|
||||||
print!("{:.2} seconds\n", time);
|
print_time_in_seconds(time);
|
||||||
} else {
|
|
||||||
print!("{:.2} kiloseconds\n", time / 1000.0);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let time = 0.0864 * time;
|
print_time_in_days(time / 86400.0);
|
||||||
if time < 100.0 {
|
|
||||||
print!("{:.2} dimidays\n", time);
|
|
||||||
} else if time < 10_000.0 {
|
|
||||||
print!("{:.2} centidays\n", time / 100.0);
|
|
||||||
} else {
|
|
||||||
print!("{:.2} days\n", time / 10_000.0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user