add ch2 to PwmInput to simplify code downstream

This commit is contained in:
Bruno Bousquet 2024-06-02 16:59:24 -04:00
parent 87c42de805
commit 66a1d101c3

View File

@ -12,7 +12,8 @@ use crate::Peripheral;
/// PWM Input driver.
pub struct PwmInput<'d, T: GeneralInstance4Channel> {
channel: Channel,
ch1: Channel,
ch2: Channel,
inner: Timer<'d, T>,
}
@ -75,7 +76,7 @@ impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
T::CaptureCompareInterrupt::unpend();
unsafe { T::CaptureCompareInterrupt::enable() };
Self { channel: ch1, inner }
Self { ch1, ch2, inner }
}
/// Enable the given channel.
@ -97,16 +98,12 @@ impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
/// Get the period tick count
pub fn get_period_ticks(&self) -> u32 {
self.inner.get_capture_value(self.channel)
self.inner.get_capture_value(self.ch1)
}
/// Get the pulse width tick count
pub fn get_width_ticks(&self) -> u32 {
self.inner.get_capture_value(match self.channel {
Channel::Ch1 => Channel::Ch2,
Channel::Ch2 => Channel::Ch1,
_ => panic!("Invalid channel for PWM input"),
})
self.inner.get_capture_value(self.ch2)
}
/// Get the duty cycle in 100%
@ -120,27 +117,21 @@ impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
/// Asynchronously wait until the pin sees a rising edge (period measurement).
pub async fn wait_for_rising_edge(&self) -> u32 {
self.inner.clear_input_interrupt(self.channel);
self.inner.enable_input_interrupt(self.channel, true);
self.inner.clear_input_interrupt(self.ch1);
self.inner.enable_input_interrupt(self.ch1, true);
// Rising edge is always on the main channel
let future: TimerEventFuture<T> = TimerEventFuture::new(self.channel.into());
let future: TimerEventFuture<T> = TimerEventFuture::new(self.ch1.into());
future.await
}
/// Asynchronously wait until the pin sees a falling edge (width measurement).
pub async fn wait_for_falling_edge(&self) -> u32 {
// Falling edge is always on the alternate channel
let ch = match self.channel {
Channel::Ch1 => Channel::Ch2,
Channel::Ch2 => Channel::Ch1,
_ => panic!("Invalid channel for PWM input"),
};
self.inner.clear_input_interrupt(self.ch2);
self.inner.enable_input_interrupt(self.ch2, true);
self.inner.clear_input_interrupt(ch);
self.inner.enable_input_interrupt(ch, true);
let future: TimerEventFuture<T> = TimerEventFuture::new(ch.into());
let future: TimerEventFuture<T> = TimerEventFuture::new(self.ch2.into());
future.await
}
}