1#![allow(dead_code)]
3use alloc::string::String;
4use core::sync::atomic::{AtomicUsize, Ordering};
5
6static SYSTEM_TICKS: AtomicUsize = AtomicUsize::new(0);
27static mut TIMER_PERIOD_TICKS: u64 = 0;
28
29struct WallClock {
42 base_unix: u64,
43 base_ms: u64,
44 set: bool,
45}
46static WALL_CLOCK: spin::Mutex<WallClock> = spin::Mutex::new(WallClock {
47 base_unix: 0,
48 base_ms: 0,
49 set: false,
50});
51
52const JST_OFFSET_SECS: u64 = 9 * 3600;
54pub const NTP_UNIX_OFFSET: u64 = 2_208_988_800;
56
57pub fn init() {
59 unsafe {
60 let freq: u64;
61 core::arch::asm!("mrs {}, cntfrq_el0", out(reg) freq);
62 TIMER_PERIOD_TICKS = freq / 100;
63 core::arch::asm!("msr cntp_tval_el0, {}", in(reg) TIMER_PERIOD_TICKS);
64 let ctrl: u64 = 1;
65 core::arch::asm!("msr cntp_ctl_el0, {}", in(reg) ctrl);
66 }
67 SYSTEM_TICKS.store(0, Ordering::Relaxed);
68 crate::debug!("[TIMER] ARM Generic Timer enabled at 10ms intervals");
69}
70
71pub fn init_secondary() {
72 unsafe {
73 core::arch::asm!("msr cntp_tval_el0, {}", in(reg) TIMER_PERIOD_TICKS);
74 let ctrl: u64 = 1;
75 core::arch::asm!("msr cntp_ctl_el0, {}", in(reg) ctrl);
76 }
77}
78
79pub fn handle_timer_irq() {
81 unsafe {
82 core::arch::asm!("msr cntp_tval_el0, {}", in(reg) TIMER_PERIOD_TICKS);
84 }
85
86 if crate::kernel::scheduler::core_id() == 0 {
91 SYSTEM_TICKS.fetch_add(1, Ordering::Relaxed);
92 }
93
94 crate::kernel::watchdog::check_from_irq();
97 crate::kernel::watchdog::check_os_loop_from_irq();
99}
100
101pub fn get_ticks() -> usize {
105 (get_system_time_ms() / 10) as usize
106}
107
108pub fn get_raw_ticks() -> usize {
110 SYSTEM_TICKS.load(Ordering::Relaxed)
111}
112
113pub fn get_system_time_us() -> u64 {
118 let count: u64;
119 let freq: u64;
120 unsafe {
121 core::arch::asm!("mrs {}, cntpct_el0", out(reg) count);
122 core::arch::asm!("mrs {}, cntfrq_el0", out(reg) freq);
123 }
124 if freq == 0 {
125 return 0;
126 }
127 let secs = count / freq;
128 let rem = count % freq;
129 secs.wrapping_mul(1_000_000).wrapping_add(rem * 1_000_000 / freq)
130}
131
132pub fn get_system_time_ms() -> u64 {
134 get_system_time_us() / 1000
135}
136
137pub fn set_wall_clock(unix_secs: u64) {
143 let now_ms = get_system_time_ms();
144 let mut w = WALL_CLOCK.lock();
145 w.base_unix = unix_secs;
146 w.base_ms = now_ms;
147 w.set = true;
148}
149
150pub fn is_wall_clock_set() -> bool {
152 WALL_CLOCK.lock().set
153}
154
155pub fn get_unix_time() -> Option<u64> {
157 let w = WALL_CLOCK.lock();
158 if !w.set {
159 return None;
160 }
161 let elapsed_ms = get_system_time_ms().wrapping_sub(w.base_ms);
162 Some(w.base_unix + elapsed_ms / 1000)
163}
164
165pub fn get_unix_time_ms() -> Option<u64> {
169 let w = WALL_CLOCK.lock();
170 if !w.set {
171 return None;
172 }
173 let elapsed_ms = get_system_time_ms().wrapping_sub(w.base_ms);
174 Some(w.base_unix.saturating_mul(1000).saturating_add(elapsed_ms))
175}
176
177fn civil_from_days(z: i64) -> (i64, u32, u32) {
180 let z = z + 719468;
181 let era = (if z >= 0 { z } else { z - 146096 }) / 146097;
182 let doe = z - era * 146097; let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; let y = yoe + era * 400;
185 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let d = (doy - (153 * mp + 2) / 5 + 1) as u32; let m = (if mp < 10 { mp + 3 } else { mp - 9 }) as u32; (if m <= 2 { y + 1 } else { y }, m, d)
190}
191
192pub fn get_datetime_jst() -> Option<(i64, u32, u32, u32, u32, u32)> {
194 let unix = get_unix_time()?;
195 let local = unix + JST_OFFSET_SECS;
196 let days = (local / 86400) as i64;
197 let secs_of_day = local % 86400;
198 let (y, mo, d) = civil_from_days(days);
199 let hh = (secs_of_day / 3600) as u32;
200 let mm = ((secs_of_day % 3600) / 60) as u32;
201 let ss = (secs_of_day % 60) as u32;
202 Some((y, mo, d, hh, mm, ss))
203}
204
205pub fn format_datetime_jst() -> Option<String> {
207 let (y, mo, d, hh, mm, _ss) = get_datetime_jst()?;
208 Some(alloc::format!(
209 "{:04}-{:02}-{:02} {:02}:{:02}",
210 y,
211 mo,
212 d,
213 hh,
214 mm
215 ))
216}
217
218pub fn format_time_hms_jst() -> Option<String> {
220 let (_y, _mo, _d, hh, mm, ss) = get_datetime_jst()?;
221 Some(alloc::format!("{:02}:{:02}:{:02}", hh, mm, ss))
222}