forked from arceos-org/arceos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9795ff6
commit be223fd
Showing
15 changed files
with
239 additions
and
54 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,108 @@ | ||
#![cfg_attr(feature = "axstd", no_std)] | ||
#![cfg_attr(feature = "axstd", no_main)] | ||
|
||
use core::time::Duration; | ||
|
||
#[cfg(feature = "axstd")] | ||
use axstd::println; | ||
#[cfg(feature = "axstd")] | ||
use axstd::time::Instant; | ||
|
||
struct DateTime{ | ||
year: u64, | ||
mon: u64, | ||
mday: u64, | ||
hour: u64, | ||
min: u64, | ||
sec: u64, | ||
} | ||
|
||
fn convert_unix_time(unix_time: u64) -> DateTime { | ||
// UNIX 时间戳的起始时间(1970-01-01 00:00:00 UTC) | ||
//const UNIX_EPOCH_SECS: u64 = 2208988800; | ||
|
||
// 计算 UNIX 时间戳的秒数和纳秒部分 | ||
let secs = unix_time; | ||
let nsecs = 0; | ||
|
||
// 计算日期和时间 | ||
let mut t = secs; | ||
let mut tdiv = t / 86400; | ||
let mut tt = t % 86400; | ||
let mut hour = tt / 3600; | ||
println!("{},{}",tt,hour); | ||
tt %= 3600; | ||
let mut min = tt / 60; | ||
tt %= 60; | ||
let sec = tt; | ||
let mut year = 1970; | ||
let mut mon = 1; | ||
let mut mday = 0; | ||
|
||
// 计算年、月和日 | ||
while tdiv >= 365 { | ||
let days = if is_leap_year(year) { 366 } else { 365 }; | ||
if tdiv >= days { | ||
tdiv -= days; | ||
year += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
while tdiv > 0 { | ||
let days = days_in_month(mon, year); | ||
if tdiv >= days { | ||
tdiv -= days; | ||
mon += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
mday = tdiv + 1; | ||
|
||
// 格式化日期和时间为字符串 | ||
let formatted_datetime = DateTime { year, mon, mday, hour, min, sec }; | ||
|
||
formatted_datetime | ||
} | ||
|
||
fn is_leap_year(year: u64) -> bool { | ||
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) | ||
} | ||
|
||
fn days_in_month(month: u64, year: u64) -> u64 { | ||
match month { | ||
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31, | ||
4 | 6 | 9 | 11 => 30, | ||
2 => { | ||
if is_leap_year(year) { | ||
29 | ||
} else { | ||
28 | ||
} | ||
} | ||
_ => 0, | ||
} | ||
} | ||
|
||
#[cfg_attr(feature = "axstd", no_mangle)] | ||
fn main() { | ||
println!("Hello, world!"); | ||
let instant1 = Instant::now(); | ||
let time1 = instant1.current_time(); | ||
println!("time1 {:?}",time1); | ||
//task::sleep(Duration::from_secs(1)); | ||
let instant2 = Instant::now(); | ||
let time2 = instant2.current_time(); | ||
println!("time2 {:?}",time2); | ||
let instant3 = Instant::now(); | ||
let time3 = instant3.current_time().as_secs(); | ||
println!("time3 {:?}",time3); | ||
let date = convert_unix_time(time3); | ||
println!( | ||
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}", | ||
date.year, date.mon, date.mday, date.hour, date.min, date.sec | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use core::intrinsics::{volatile_load, volatile_store}; | ||
|
||
static RTC_DR: u32 = 0x000; | ||
static RTC_MR: u32 = 0x004; | ||
static RTC_LR: u32 = 0x008; | ||
static RTC_CR: u32 = 0x00c; | ||
static RTC_IMSC: u32 = 0x010; | ||
static RTC_RIS: u32 = 0x014; | ||
static RTC_MIS: u32 = 0x018; | ||
static RTC_ICR: u32 = 0x01c; | ||
|
||
pub static mut PL031_RTC: Pl031rtc = Pl031rtc { | ||
address: 0, | ||
}; | ||
|
||
pub fn init() { | ||
info!("pl031 init begin"); | ||
unsafe{ | ||
PL031_RTC.init(); | ||
//let x = PL031_RTC.time(); | ||
let x = rtc_read_time(); | ||
debug!("{}",x); | ||
rtc_write_time(10); | ||
let x = rtc_read_time(); | ||
debug!("{}",x); | ||
} | ||
} | ||
|
||
pub struct Pl031rtc { | ||
pub address: usize, | ||
} | ||
|
||
pub const PHYS_OFFSET: usize = 0xffff_0000_0000_0000; | ||
pub const PHYS_RTC: usize = PHYS_OFFSET + 0x09010000; | ||
|
||
impl Pl031rtc { | ||
fn debug(&mut self) { | ||
debug!("RTC DR: {}",unsafe { self.read(RTC_DR) } as u64); | ||
debug!("RTC MR: {}",unsafe { self.read(RTC_MR) } as u64); | ||
debug!("RTC LR: {}",unsafe { self.read(RTC_LR) } as u64); | ||
debug!("RTC CR: {}",unsafe { self.read(RTC_CR) } as u64); | ||
debug!("RTC_IMSC: {}",unsafe { self.read(RTC_IMSC) } as u64); | ||
} | ||
|
||
fn init(&mut self) { | ||
self.address = PHYS_OFFSET + 0x09010000; | ||
self.debug(); | ||
} | ||
|
||
pub unsafe fn read(&self, reg: u32) -> u32 { | ||
let val = volatile_load((PHYS_RTC + reg as usize) as *const u32); | ||
val | ||
} | ||
|
||
pub unsafe fn write(&mut self, reg: u32, value: u32) { | ||
debug!("rtc write time"); | ||
volatile_store((PHYS_RTC + reg as usize) as *mut u32, value); | ||
} | ||
|
||
pub fn time(&mut self) -> u64 { | ||
let seconds = unsafe { self.read(RTC_DR) } as u64; | ||
//let seconds = 0; | ||
seconds | ||
} | ||
} | ||
|
||
pub fn rtc_read_time() -> u64{ | ||
unsafe { | ||
//let addr = 0xffff_0000_0000_0000 as usize + 0x09010000; | ||
//let x = volatile_load(((addr) as usize) as *const u32) as u64; | ||
let x = PL031_RTC.time(); | ||
x | ||
} | ||
} | ||
|
||
pub fn rtc_write_time(seconds:u32){ | ||
|
||
unsafe { PL031_RTC.write(RTC_LR,seconds) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.