aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/timer/one_pulse.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-stm32/src/timer/one_pulse.rs')
-rw-r--r--embassy-stm32/src/timer/one_pulse.rs70
1 files changed, 44 insertions, 26 deletions
diff --git a/embassy-stm32/src/timer/one_pulse.rs b/embassy-stm32/src/timer/one_pulse.rs
index 933165ef9..a75b41bd7 100644
--- a/embassy-stm32/src/timer/one_pulse.rs
+++ b/embassy-stm32/src/timer/one_pulse.rs
@@ -7,16 +7,15 @@ use core::pin::Pin;
7use core::task::{Context, Poll}; 7use core::task::{Context, Poll};
8 8
9use super::low_level::{ 9use super::low_level::{
10 CountingMode, FilterValue, InputCaptureMode, InputTISelection, SlaveMode, Timer, TriggerSource, 10 CountingMode, FilterValue, InputCaptureMode, InputTISelection, SlaveMode, Timer, TriggerSource as Ts,
11};
12use super::{
13 CaptureCompareInterruptHandler, Channel, Channel1Pin, Channel2Pin, ExternalTriggerPin, GeneralInstance4Channel,
14}; 11};
12use super::{CaptureCompareInterruptHandler, Channel, ExternalTriggerPin, GeneralInstance4Channel, TimerPin};
15pub use super::{Ch1, Ch2}; 13pub use super::{Ch1, Ch2};
16use crate::gpio::{AfType, AnyPin, Pull}; 14use crate::gpio::{AfType, AnyPin, Pull};
17use crate::interrupt::typelevel::{Binding, Interrupt}; 15use crate::interrupt::typelevel::{Binding, Interrupt};
18use crate::pac::timer::vals::Etp; 16use crate::pac::timer::vals::Etp;
19use crate::time::Hertz; 17use crate::time::Hertz;
18use crate::timer::TimerChannel;
20use crate::Peri; 19use crate::Peri;
21 20
22/// External input marker type. 21/// External input marker type.
@@ -44,28 +43,46 @@ impl From<ExternalTriggerPolarity> for Etp {
44/// 43///
45/// This wraps a pin to make it usable as a timer trigger. 44/// This wraps a pin to make it usable as a timer trigger.
46pub struct TriggerPin<'d, T, C> { 45pub struct TriggerPin<'d, T, C> {
47 _pin: Peri<'d, AnyPin>, 46 #[allow(unused)]
47 pin: Peri<'d, AnyPin>,
48 phantom: PhantomData<(T, C)>, 48 phantom: PhantomData<(T, C)>,
49} 49}
50 50
51macro_rules! channel_impl { 51trait SealedTriggerSource {}
52 ($new_chx:ident, $channel:ident, $pin_trait:ident) => { 52
53 impl<'d, T: GeneralInstance4Channel> TriggerPin<'d, T, $channel> { 53/// Marker trait for a trigger source.
54 #[doc = concat!("Create a new ", stringify!($channel), " trigger pin instance.")] 54#[expect(private_bounds)]
55 pub fn $new_chx(pin: Peri<'d, impl $pin_trait<T>>, pull: Pull) -> Self { 55pub trait TriggerSource: SealedTriggerSource {}
56 pin.set_as_af(pin.af_num(), AfType::input(pull)); 56
57 TriggerPin { 57impl TriggerSource for Ch1 {}
58 _pin: pin.into(), 58impl TriggerSource for Ch2 {}
59 phantom: PhantomData, 59impl TriggerSource for Ext {}
60 } 60
61 } 61impl SealedTriggerSource for Ch1 {}
62impl SealedTriggerSource for Ch2 {}
63impl SealedTriggerSource for Ext {}
64
65impl<'d, T: GeneralInstance4Channel, C: TriggerSource + TimerChannel> TriggerPin<'d, T, C> {
66 /// Create a new Channel trigger pin instance.
67 pub fn new<#[cfg(afio)] A>(pin: Peri<'d, if_afio!(impl TimerPin<T, C, A>)>, pull: Pull) -> Self {
68 set_as_af!(pin, AfType::input(pull));
69 TriggerPin {
70 pin: pin.into(),
71 phantom: PhantomData,
62 } 72 }
63 }; 73 }
64} 74}
65 75
66channel_impl!(new_ch1, Ch1, Channel1Pin); 76impl<'d, T: GeneralInstance4Channel> TriggerPin<'d, T, Ext> {
67channel_impl!(new_ch2, Ch2, Channel2Pin); 77 /// Create a new external trigger pin instance.
68channel_impl!(new_ext, Ext, ExternalTriggerPin); 78 pub fn new_external<#[cfg(afio)] A>(pin: Peri<'d, if_afio!(impl ExternalTriggerPin<T, A>)>, pull: Pull) -> Self {
79 set_as_af!(pin, AfType::input(pull));
80 TriggerPin {
81 pin: pin.into(),
82 phantom: PhantomData,
83 }
84 }
85}
69 86
70/// One pulse driver. 87/// One pulse driver.
71/// 88///
@@ -79,9 +96,10 @@ impl<'d, T: GeneralInstance4Channel> OnePulse<'d, T> {
79 /// 96 ///
80 /// The pulse is triggered by a channel 1 input pin on both rising and 97 /// The pulse is triggered by a channel 1 input pin on both rising and
81 /// falling edges. Channel 1 will unusable as an output. 98 /// falling edges. Channel 1 will unusable as an output.
99 #[allow(unused)]
82 pub fn new_ch1_edge_detect( 100 pub fn new_ch1_edge_detect(
83 tim: Peri<'d, T>, 101 tim: Peri<'d, T>,
84 _pin: TriggerPin<'d, T, Ch1>, 102 pin: TriggerPin<'d, T, Ch1>,
85 _irq: impl Binding<T::CaptureCompareInterrupt, CaptureCompareInterruptHandler<T>> + 'd, 103 _irq: impl Binding<T::CaptureCompareInterrupt, CaptureCompareInterruptHandler<T>> + 'd,
86 freq: Hertz, 104 freq: Hertz,
87 pulse_end: u32, 105 pulse_end: u32,
@@ -89,7 +107,7 @@ impl<'d, T: GeneralInstance4Channel> OnePulse<'d, T> {
89 ) -> Self { 107 ) -> Self {
90 let mut this = Self { inner: Timer::new(tim) }; 108 let mut this = Self { inner: Timer::new(tim) };
91 109
92 this.inner.set_trigger_source(TriggerSource::TI1F_ED); 110 this.inner.set_trigger_source(Ts::TI1F_ED);
93 this.inner 111 this.inner
94 .set_input_ti_selection(Channel::Ch1, InputTISelection::Normal); 112 .set_input_ti_selection(Channel::Ch1, InputTISelection::Normal);
95 this.inner 113 this.inner
@@ -114,7 +132,7 @@ impl<'d, T: GeneralInstance4Channel> OnePulse<'d, T> {
114 ) -> Self { 132 ) -> Self {
115 let mut this = Self { inner: Timer::new(tim) }; 133 let mut this = Self { inner: Timer::new(tim) };
116 134
117 this.inner.set_trigger_source(TriggerSource::TI1FP1); 135 this.inner.set_trigger_source(Ts::TI1FP1);
118 this.inner 136 this.inner
119 .set_input_ti_selection(Channel::Ch1, InputTISelection::Normal); 137 .set_input_ti_selection(Channel::Ch1, InputTISelection::Normal);
120 this.inner 138 this.inner
@@ -131,7 +149,7 @@ impl<'d, T: GeneralInstance4Channel> OnePulse<'d, T> {
131 /// as an output. 149 /// as an output.
132 pub fn new_ch2( 150 pub fn new_ch2(
133 tim: Peri<'d, T>, 151 tim: Peri<'d, T>,
134 _pin: TriggerPin<'d, T, Ch1>, 152 _pin: TriggerPin<'d, T, Ch2>,
135 _irq: impl Binding<T::CaptureCompareInterrupt, CaptureCompareInterruptHandler<T>> + 'd, 153 _irq: impl Binding<T::CaptureCompareInterrupt, CaptureCompareInterruptHandler<T>> + 'd,
136 freq: Hertz, 154 freq: Hertz,
137 pulse_end: u32, 155 pulse_end: u32,
@@ -140,7 +158,7 @@ impl<'d, T: GeneralInstance4Channel> OnePulse<'d, T> {
140 ) -> Self { 158 ) -> Self {
141 let mut this = Self { inner: Timer::new(tim) }; 159 let mut this = Self { inner: Timer::new(tim) };
142 160
143 this.inner.set_trigger_source(TriggerSource::TI2FP2); 161 this.inner.set_trigger_source(Ts::TI2FP2);
144 this.inner 162 this.inner
145 .set_input_ti_selection(Channel::Ch2, InputTISelection::Normal); 163 .set_input_ti_selection(Channel::Ch2, InputTISelection::Normal);
146 this.inner 164 this.inner
@@ -172,7 +190,7 @@ impl<'d, T: GeneralInstance4Channel> OnePulse<'d, T> {
172 // No filtering 190 // No filtering
173 r.set_etf(FilterValue::NO_FILTER); 191 r.set_etf(FilterValue::NO_FILTER);
174 }); 192 });
175 this.inner.set_trigger_source(TriggerSource::ETRF); 193 this.inner.set_trigger_source(Ts::ETRF);
176 this.new_inner(freq, pulse_end, counting_mode); 194 this.new_inner(freq, pulse_end, counting_mode);
177 195
178 this 196 this