From 04424fb7d410c51a2fe74c15af7383c6fad5be48 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Sat, 14 Aug 2021 17:10:39 -0500 Subject: [PATCH] implement timer counter --- rp2040-hal/src/timer.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/rp2040-hal/src/timer.rs b/rp2040-hal/src/timer.rs index 5ac28cb..6d60991 100644 --- a/rp2040-hal/src/timer.rs +++ b/rp2040-hal/src/timer.rs @@ -1,3 +1,24 @@ -//! Timers +//! Timer Peripheral // See [Chapter 4 Section 6](https://datasheets.raspberrypi.org/rp2040/rp2040_datasheet.pdf) for more details -// TODO + +use crate::pac::TIMER; + +/// Timer peripheral +pub struct Timer { + timer: TIMER, +} + +impl Timer { + /// Create a new [`Timer`] + pub fn new(timer: TIMER) -> Self { + Self { timer } + } + + /// Get the current counter value. + pub fn get_counter(&self) -> u64 { + // latched read, low before high + let lo = self.timer.timelr.read().bits(); + let hi = self.timer.timehr.read().bits(); + (hi as u64) << 32 | lo as u64 + } +}