Skip to content

Commit d1156a7

Browse files
committed
feat(timer): 允许配置定时器间隔
1 parent 19d5108 commit d1156a7

File tree

12 files changed

+72
-21
lines changed

12 files changed

+72
-21
lines changed

kernel/src/drivers/char/serial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate alloc;
22

3-
use super::super::{CharDevice, OldDevice, DeviceError, DeviceInner, DeviceType, SharedDeviceOps};
3+
use super::super::{CharDevice, DeviceError, DeviceInner, DeviceType, OldDevice, SharedDeviceOps};
44
use alloc::format;
55
use alloc::string::{String, ToString};
66
use alloc::sync::Arc;

kernel/src/drivers/input/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub mod ps2;
1+
pub mod ps2;

kernel/src/drivers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pub mod char;
44
pub mod device;
55
pub mod input;
66
pub mod ops;
7-
pub mod usb;
87
pub mod resource;
8+
pub mod usb;
99

1010
pub use device::*;
1111
pub use ops::*;

kernel/src/drivers/usb.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
//! The USB driver (xHCI).
2-
//!
2+
//!
33
//! Copyright (C) RainSTR Studio 2025-2026, All Rights Reserved.
44
55
extern crate alloc;
6+
use crate::libs::pci::scan_all_pci_devices;
67
use alloc::vec::Vec;
78
use log::warn;
8-
use crate::libs::pci::scan_all_pci_devices;
99

1010
pub fn init() {
1111
let all_pci_devices = scan_all_pci_devices();
1212

1313
// Get xHCI devices
14-
let usb_devices = all_pci_devices.iter()
14+
let usb_devices = all_pci_devices
15+
.iter()
1516
.filter(|(_, class)| class.class == 0x0C && class.subclass == 0x03 && class.prog_if == 0x30)
1617
.collect::<Vec<_>>();
1718

1819
if usb_devices.is_empty() {
1920
warn!("No USB devices found.");
2021
return;
2122
}
22-
}
23+
}

kernel/src/fs/fs_impl/kernfs.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ impl Inode for KernInode {
192192
}
193193
}
194194

195-
fn create_device(&self, name: &str, device: Arc<OldDevice>) -> Result<Arc<dyn Inode>, VfsError> {
195+
fn create_device(
196+
&self,
197+
name: &str,
198+
device: Arc<OldDevice>,
199+
) -> Result<Arc<dyn Inode>, VfsError> {
196200
match &self.content {
197201
KernNodeContent::Dir(entries) => {
198202
let mut map = entries.write();

kernel/src/fs/fs_impl/memfs.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ impl Inode for MemVNode {
281281
}
282282
}
283283

284-
fn create_device(&self, name: &str, device: Arc<OldDevice>) -> Result<Arc<dyn Inode>, VfsError> {
284+
fn create_device(
285+
&self,
286+
name: &str,
287+
device: Arc<OldDevice>,
288+
) -> Result<Arc<dyn Inode>, VfsError> {
285289
match &self.content {
286290
MemNodeContent::Dir { entries } => {
287291
let mut entries = entries.write();

kernel/src/fs/vfs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::drivers::{OldDevice, DeviceError, DEVICE_MANAGER};
1+
use crate::drivers::{DeviceError, OldDevice, DEVICE_MANAGER};
22
extern crate alloc;
33
use super::fs_impl;
44
use alloc::format;
@@ -110,7 +110,11 @@ pub trait Inode: Send + Sync + core::fmt::Debug {
110110
Err(VfsError::NotImplemented)
111111
}
112112

113-
fn create_device(&self, name: &str, device: Arc<OldDevice>) -> Result<Arc<dyn Inode>, VfsError> {
113+
fn create_device(
114+
&self,
115+
name: &str,
116+
device: Arc<OldDevice>,
117+
) -> Result<Arc<dyn Inode>, VfsError> {
114118
let _ = (name, device);
115119
Err(VfsError::NotImplemented)
116120
}

kernel/src/libs/Kconfig.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ default = +8
1515
desc = "Timezone"
1616
help = "Timezone offset from UTC (in hours)."
1717
rust_type = "i8"
18+
19+
[[config]]
20+
name = "TIMER_PERIOD_MS"
21+
type = "int"
22+
default = 10
23+
desc = "Timer Period (ms)"
24+
help = "The period of the system timer in milliseconds."
25+
rust_type = "u64"

kernel/src/libs/time/apic.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ pub const XAPIC_TIMER_DIV_CONF_OFFSET: u32 = 0x3E0;
1313
/// * `timer_vector` - The interrupt vector to use for the timer.
1414
///
1515
/// # Returns
16-
/// The number of ticks per 10ms.
16+
/// The number of ticks per the configured period.
1717
pub unsafe fn calibrate_timer<F1, F2>(read_reg: F1, write_reg: F2, timer_vector: u8) -> u64
1818
where
1919
F1: Fn(u32) -> u32,
2020
F2: Fn(u32, u32),
2121
{
22+
let period_ms = crate::config::TIMER_PERIOD_MS;
23+
let period_us = period_ms * 1000;
24+
2225
// Stop timer
2326
write_reg(XAPIC_TIMER_INIT_COUNT_OFFSET, 0);
2427
// Set divider to 16
2528
write_reg(XAPIC_TIMER_DIV_CONF_OFFSET, 0x3);
2629

27-
let (ticks_per_10ms, _) = super::calibrate_with_pit(10000, || {
30+
let (ticks_per_period, _) = super::calibrate_with_pit(period_us, || {
2831
// Set APIC timer to max
2932
write_reg(XAPIC_TIMER_INIT_COUNT_OFFSET, 0xFFFFFFFF);
3033
move || {
@@ -33,11 +36,14 @@ where
3336
}
3437
});
3538

36-
info!("APIC Timer calibrated: {} ticks per 10ms", ticks_per_10ms);
39+
info!(
40+
"APIC Timer calibrated: {} ticks per {}ms",
41+
ticks_per_period, period_ms
42+
);
3743

38-
// Set timer for periodic interrupt at 100Hz (10ms)
44+
// Set timer for periodic interrupt at the configured frequency
3945
write_reg(XAPIC_LVT_TIMER_OFFSET, 0x20000 | timer_vector as u32); // Periodic mode
40-
write_reg(XAPIC_TIMER_INIT_COUNT_OFFSET, ticks_per_10ms as u32);
46+
write_reg(XAPIC_TIMER_INIT_COUNT_OFFSET, ticks_per_period as u32);
4147

42-
ticks_per_10ms
48+
ticks_per_period
4349
}

kernel/src/libs/time/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod pit;
33
pub mod rtc;
44
pub mod tsc;
55

6-
pub use tsc::{init, sleep_us, time_since_boot};
6+
pub use tsc::{init, sleep_us, time_since_boot, uptime_ms, uptime_us};
77

88
/// Calibrate a timer using the PIT (Programmable Interval Timer).
99
///

0 commit comments

Comments
 (0)