Skip to content

Commit dd9d667

Browse files
committed
Extending thoughts
1 parent 0460537 commit dd9d667

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/timer.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//! [examples/adc.rs]: https://github.com/stm32-rs/stm32f3xx-hal/blob/v0.9.0/examples/adc.rs
1212
1313
use core::convert::{From, TryFrom};
14+
use core::marker::PhantomData;
15+
use core::ops::Deref;
1416

1517
use crate::pac::{DCB, DWT};
1618
#[cfg(feature = "enumset")]
@@ -592,3 +594,45 @@ cfg_if::cfg_if! {
592594
fn test(tim: pac::TIM16) {
593595
let tim6: *const pac::tim6::RegisterBlock = unsafe {core::mem::transmute(pac::TIM16::ptr())};
594596
}
597+
598+
struct BasicTimer<T> {
599+
_ptr: usize,
600+
real_timer: PhantomData<T>,
601+
}
602+
603+
impl<T> Deref for BasicTimer<T> {
604+
type Target = pac::tim6::RegisterBlock;
605+
606+
#[inline(always)]
607+
fn deref(&self) -> &Self::Target {
608+
unsafe { &*(self._ptr as *const Self::Target) }
609+
}
610+
}
611+
612+
impl From<pac::TIM6> for BasicTimer<pac::TIM6> {
613+
fn from(tim: pac::TIM6) -> Self {
614+
Self {
615+
_ptr: unsafe { pac::TIM6::ptr() as _ },
616+
real_timer: PhantomData,
617+
}
618+
}
619+
}
620+
621+
impl<T> BasicTimer<T> {
622+
pub fn free(self) -> T {
623+
self.real_timer
624+
}
625+
}
626+
627+
// TODO: Is that trait needed, when we already have Into<BasicTimer>?
628+
pub trait BasicTimerInstance: Deref<Target = pac::tim6::RegisterBlock> {}
629+
impl<T> BasicTimerInstance for BasicTimer<T> {}
630+
631+
fn test2<T>(tim: impl Into<BasicTimer<T>>) {
632+
let tim: BasicTimer<T> = tim.into();
633+
tim.cr1.read();
634+
}
635+
636+
fn test3(tim: pac::TIM6) {
637+
test2(tim);
638+
}

0 commit comments

Comments
 (0)