Skip to content

Commit

Permalink
Add a MachineTimer driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
thejpster committed Sep 1, 2024
1 parent 3241169 commit 3dfa983
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions rp235x-hal/src/sio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub struct SioGpioQspi {
_private: (),
}

/// Marker struct for ownership of SIO Machine Timer
pub struct MachineTimer {
_private: (),
}

/// Struct containing ownership markers for managing ownership of the SIO registers.
pub struct Sio {
_sio: pac::SIO,
Expand All @@ -67,6 +72,8 @@ pub struct Sio {
pub interp0: Interp0,
/// Interpolator 1
pub interp1: Interp1,
/// RISC-V Machine Timer
pub machine_timer: MachineTimer,
}

impl Sio {
Expand All @@ -85,6 +92,7 @@ impl Sio {
lane0: Interp1Lane0 { _private: () },
lane1: Interp1Lane1 { _private: () },
},
machine_timer: MachineTimer { _private: () },
}
}

Expand Down Expand Up @@ -191,6 +199,33 @@ impl SioFifo {
}
}

impl MachineTimer {
/// Read the SIO's Machine Timer
pub fn read(&self) -> u64 {
let sio = unsafe { &(*pac::SIO::ptr()) };
loop {
let mtimeh = sio.mtimeh().read().mtimeh().bits();
let mtime = sio.mtime().read().mtime().bits();
let mtimeh2 = sio.mtimeh().read().mtimeh().bits();
if mtimeh == mtimeh2 {
return u64::from(mtimeh) << 32 | u64::from(mtime);
}
}
}

/// Set whether the timer is enabled
pub fn set_enabled(&mut self, enabled: bool) {
let sio = unsafe { &(*pac::SIO::ptr()) };
sio.mtime_ctrl().write(|w| w.en().variant(enabled));
}

/// Set the speed the clock runs at
pub fn set_fullspeed(&mut self, fullspeed: bool) {
let sio = unsafe { &(*pac::SIO::ptr()) };
sio.mtime_ctrl().write(|w| w.fullspeed().variant(fullspeed));
}
}

/// This type is just used to limit us to Spinlocks `0..=31`
pub trait SpinlockValid: Sealed {}

Expand Down

0 comments on commit 3dfa983

Please sign in to comment.