Skip to content

Commit

Permalink
timer
Browse files Browse the repository at this point in the history
  • Loading branch information
ZR233 committed Jan 17, 2025
1 parent c76d9c2 commit 6daf756
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/driver-interface/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait Interface: Send {
fn name(&self) -> String;
}

pub trait InterfacePerCPU: DriverGeneric {
pub trait InterfacePerCPU: DriverGeneric + Sync {
fn set_interval(&mut self, ticks: u64);
fn current_ticks(&self) -> u64;
fn tick_hz(&self) -> u64;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{string::String, vec::Vec};
use alloc::string::String;
use core::fmt::Debug;

use super::irq::IrqInfo;
Expand Down
18 changes: 17 additions & 1 deletion crates/sparreal-kernel/src/driver_manager/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,27 @@ impl<T> Device<T> {
})
}

pub fn spin_use(&self, who: impl ToString) -> BorrowGuard<T> {
loop {
if let Ok(g) = self.try_use_by(who.to_string()) {
return g;
}
}
}

/// 强制获取设备
/// #Safety
/// 一般用于中断处理中
pub unsafe fn force_use(&self) -> *mut T {
unsafe { self.data.get() }
self.data.get()
}
}

impl<T: Sync> Deref for Device<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
unsafe { &*self.data.get() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use core::ptr::NonNull;
use alloc::{format, string::String, vec::Vec};
use driver_interface::{DriverRegister, ProbeFnKind, timer::*};
use fdt_parser::Fdt;
use log::debug;

use crate::prelude::GetIrqConfig;

use super::{Descriptor, Device, DeviceId};
use super::{Descriptor, Device};

pub struct Container {
data: Option<Device<Driver>>,
Expand Down Expand Up @@ -56,6 +57,7 @@ pub fn init_by_fdt(
};

let timer = probe(irq.cfgs.clone());
debug!("[{}] ok", timer.name());
let dev = Device::new(
Descriptor {
name: timer.name(),
Expand Down
3 changes: 2 additions & 1 deletion crates/sparreal-kernel/src/irq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use driver_interface::interrupt_controller::*;
use log::debug;
use spin::Mutex;

use crate::platform::cpu_id;
use crate::{
driver_manager::{self, device::DriverId},
globals::{self, cpu_global},
platform::{self, cpu_id},
platform::{self},
platform_if::PlatformImpl,
};
pub use driver_manager::device::irq::IrqInfo;
Expand Down
13 changes: 10 additions & 3 deletions crates/sparreal-kernel/src/time/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use core::{cell::UnsafeCell, time::Duration};
use core::time::Duration;

use crate::{
driver_manager::{self, device::Device, manager},
driver_manager::{
self,
device::{BorrowGuard, Device},
manager,
},
globals::{cpu_global, cpu_global_mut, global_val},
platform_if::*,
};
use driver_interface::timer::*;
use log::{debug, error};

#[derive(Default)]
pub(crate) struct Timer {
Expand Down Expand Up @@ -36,6 +39,10 @@ pub(crate) fn init_current_cpu() {
unsafe { cpu_global_mut().timer.timer = timer };
}

fn timer_write() -> Option<BorrowGuard<PerCPU>> {
Some(cpu_global().timer.timer.as_ref()?.spin_use("Kernel"))
}

pub fn enable() {
PlatformImpl::timer_set_interval(10000);
PlatformImpl::timer_set_irq(true);
Expand Down

0 comments on commit 6daf756

Please sign in to comment.