Skip to content

Commit 3d24df2

Browse files
committed
Extending thoughts
1 parent f2d0f93 commit 3d24df2

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/timer.rs

+44
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//! [Write]: embedded_hal::timer::Periodic
88
99
use core::convert::{From, TryFrom};
10+
use core::marker::PhantomData;
11+
use core::ops::Deref;
1012

1113
use crate::pac::{DCB, DWT};
1214
#[cfg(feature = "enumset")]
@@ -563,3 +565,45 @@ cfg_if::cfg_if! {
563565
fn test(tim: pac::TIM16) {
564566
let tim6: *const pac::tim6::RegisterBlock = unsafe {core::mem::transmute(pac::TIM16::ptr())};
565567
}
568+
569+
struct BasicTimer<T> {
570+
_ptr: usize,
571+
real_timer: PhantomData<T>,
572+
}
573+
574+
impl<T> Deref for BasicTimer<T> {
575+
type Target = pac::tim6::RegisterBlock;
576+
577+
#[inline(always)]
578+
fn deref(&self) -> &Self::Target {
579+
unsafe { &*(self._ptr as *const Self::Target) }
580+
}
581+
}
582+
583+
impl From<pac::TIM6> for BasicTimer<pac::TIM6> {
584+
fn from(tim: pac::TIM6) -> Self {
585+
Self {
586+
_ptr: unsafe { pac::TIM6::ptr() as _ },
587+
real_timer: PhantomData,
588+
}
589+
}
590+
}
591+
592+
impl<T> BasicTimer<T> {
593+
pub fn free(self) -> T {
594+
self.real_timer
595+
}
596+
}
597+
598+
// TODO: Is that trait needed, when we already have Into<BasicTimer>?
599+
pub trait BasicTimerInstance: Deref<Target = pac::tim6::RegisterBlock> {}
600+
impl<T> BasicTimerInstance for BasicTimer<T> {}
601+
602+
fn test2<T>(tim: impl Into<BasicTimer<T>>) {
603+
let tim: BasicTimer<T> = tim.into();
604+
tim.cr1.read();
605+
}
606+
607+
fn test3(tim: pac::TIM6) {
608+
test2(tim);
609+
}

0 commit comments

Comments
 (0)