|
11 | 11 | //! [examples/adc.rs]: https://github.com/stm32-rs/stm32f3xx-hal/blob/v0.9.0/examples/adc.rs
|
12 | 12 |
|
13 | 13 | use core::convert::{From, TryFrom};
|
| 14 | +use core::marker::PhantomData; |
| 15 | +use core::ops::Deref; |
14 | 16 |
|
15 | 17 | use crate::pac::{DCB, DWT};
|
16 | 18 | #[cfg(feature = "enumset")]
|
@@ -592,3 +594,45 @@ cfg_if::cfg_if! {
|
592 | 594 | fn test(tim: pac::TIM16) {
|
593 | 595 | let tim6: *const pac::tim6::RegisterBlock = unsafe {core::mem::transmute(pac::TIM16::ptr())};
|
594 | 596 | }
|
| 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