From 3dfa983559f0b31ee1e8e2418142b8f5b2274937 Mon Sep 17 00:00:00 2001 From: Jonathan 'theJPster' Pallant Date: Sun, 1 Sep 2024 17:26:29 +0100 Subject: [PATCH] Add a MachineTimer driver. --- rp235x-hal/src/sio.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/rp235x-hal/src/sio.rs b/rp235x-hal/src/sio.rs index 825612c03..e967c4a1d 100644 --- a/rp235x-hal/src/sio.rs +++ b/rp235x-hal/src/sio.rs @@ -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, @@ -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 { @@ -85,6 +92,7 @@ impl Sio { lane0: Interp1Lane0 { _private: () }, lane1: Interp1Lane1 { _private: () }, }, + machine_timer: MachineTimer { _private: () }, } } @@ -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 {}