|
7 | 7 | //! [Write]: embedded_hal::timer::Periodic
|
8 | 8 |
|
9 | 9 | use core::convert::{From, TryFrom};
|
| 10 | +use core::marker::PhantomData; |
| 11 | +use core::ops::Deref; |
10 | 12 |
|
11 | 13 | use crate::pac::{DCB, DWT};
|
12 | 14 | #[cfg(feature = "enumset")]
|
@@ -563,3 +565,45 @@ cfg_if::cfg_if! {
|
563 | 565 | fn test(tim: pac::TIM16) {
|
564 | 566 | let tim6: *const pac::tim6::RegisterBlock = unsafe {core::mem::transmute(pac::TIM16::ptr())};
|
565 | 567 | }
|
| 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