Skip to content

Commit

Permalink
9.9
Browse files Browse the repository at this point in the history
  • Loading branch information
lhw2002426 committed Sep 9, 2023
1 parent 9795ff6 commit be223fd
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 54 deletions.
Binary file added aarch64-linux-musl-gcc
Binary file not shown.
98 changes: 98 additions & 0 deletions apps/helloworld/src/main.rs
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
);
}
1 change: 1 addition & 0 deletions modules/axhal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ paging = ["axalloc", "page_table"]
irq = []
tls = ["alloc"]
default = []
rtc = []

[dependencies]
log = "0.4"
Expand Down
1 change: 1 addition & 0 deletions modules/axhal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#![feature(const_maybe_uninit_zeroed)]
#![feature(const_option)]
#![feature(doc_auto_cfg)]
#![feature(core_intrinsics)]

#[allow(unused_imports)]
#[macro_use]
Expand Down
3 changes: 3 additions & 0 deletions modules/axhal/src/platform/aarch64_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ pub mod gic;

#[cfg(not(platform_family = "aarch64-bsta1000b"))]
pub mod pl011;

#[cfg(feature = "rtc")]
pub mod pl031;
79 changes: 79 additions & 0 deletions modules/axhal/src/platform/aarch64_common/pl031.rs
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) };
}
4 changes: 4 additions & 0 deletions modules/axhal/src/platform/aarch64_qemu_virt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub mod console {

pub mod time {
pub use crate::platform::aarch64_common::generic_timer::*;
#[cfg(feature = "rtc")]
pub use crate::platform::aarch64_common::pl031::*;
}

pub mod misc {
Expand Down Expand Up @@ -52,6 +54,8 @@ pub fn platform_init() {
#[cfg(feature = "irq")]
super::aarch64_common::gic::init_primary();
super::aarch64_common::generic_timer::init_percpu();
#[cfg(feature = "rtc")]
super::aarch64_common::pl031::init();
super::aarch64_common::pl011::init();
}

Expand Down
4 changes: 4 additions & 0 deletions modules/axhal/src/platform/aarch64_raspi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub mod console {

pub mod time {
pub use crate::platform::aarch64_common::generic_timer::*;
#[cfg(feature = "rtc")]
pub use crate::platform::aarch64_common::pl031::*;
}

pub mod misc {
Expand Down Expand Up @@ -57,6 +59,8 @@ pub fn platform_init() {
#[cfg(feature = "irq")]
super::aarch64_common::gic::init_primary();
super::aarch64_common::generic_timer::init_percpu();
#[cfg(feature = "rtc")]
super::aarch64_common::pl031::init();
super::aarch64_common::pl011::init();
}

Expand Down
6 changes: 6 additions & 0 deletions modules/axhal/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub type TimeValue = Duration;
pub use crate::platform::irq::TIMER_IRQ_NUM;
#[cfg(feature = "irq")]
pub use crate::platform::time::set_oneshot_timer;
#[cfg(feature = "rtc")]
pub use crate::platform::time::rtc_read_time;
pub use crate::platform::time::{current_ticks, nanos_to_ticks, ticks_to_nanos};

/// Number of milliseconds in a second.
Expand All @@ -32,6 +34,10 @@ pub fn current_time_nanos() -> u64 {

/// Returns the current clock time in [`TimeValue`].
pub fn current_time() -> TimeValue {
let nanos = current_time_nanos();
#[cfg(feature = "rtc")]
//let x = Duration::new(rtc_read_time(),0);
return Duration::new((nanos / (NANOS_PER_SEC as u64)) + rtc_read_time(), (nanos % (NANOS_PER_SEC as u64)) as u32);
TimeValue::from_nanos(current_time_nanos())
}

Expand Down
4 changes: 3 additions & 1 deletion modules/axruntime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ repository = "https://github.com/rcore-os/arceos/tree/main/modules/axruntime"
documentation = "https://rcore-os.github.io/arceos/axruntime/index.html"

[features]
default = []
default = ["axhal/rtc"]
#default = []

smp = ["axhal/smp"]
irq = ["axhal/irq", "axtask?/irq", "percpu", "kernel_guard"]
tls = ["axhal/tls", "axtask?/tls"]
alloc = ["axalloc"]
paging = ["axhal/paging", "lazy_init"]
rtc = ["axhal/rtc"]

multitask = ["axtask/multitask"]
fs = ["axdriver", "axfs"]
Expand Down
18 changes: 9 additions & 9 deletions modules/axruntime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
#![cfg_attr(not(test), no_std)]
#![feature(doc_auto_cfg)]
#![feature(core_intrinsics)]
use core::intrinsics::{volatile_load, volatile_store};

#[macro_use]
extern crate axlog;

#[cfg(all(target_os = "none", not(test)))]
mod lang_items;
mod trap;
mod rtc;

#[cfg(feature = "smp")]
mod mp;
Expand Down Expand Up @@ -191,17 +191,17 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! {
core::hint::spin_loop();
}

unsafe{
/*unsafe{
info!("before");
/*
let mut myrtc = rtc::Pl031rtc::new();
let x = myrtc.time();
*/
rtc::init();
let x = rtc::PL031_RTC.time();
//rtc::init();
//let mut myrtc = rtc::Pl031rtc::new();
//let x = myrtc.time();
let x = volatile_load(((0xffff_0000_0000_0000 as usize + 0x09010000) as usize) as *const u32) as u64;
info!("{}",x);
info!("after");
}
}*/

unsafe { main() };

Expand Down
44 changes: 0 additions & 44 deletions modules/axruntime/src/rtc.rs

This file was deleted.

Loading

0 comments on commit be223fd

Please sign in to comment.