From 2e358dea263b82c2a5d4f9b7d1236190666c1f37 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 9 Feb 2021 14:36:50 +0100 Subject: [PATCH] Adding support for input capture prefilter configuration --- src/hardware/digital_input_stamper.rs | 8 ++++---- src/hardware/timers.rs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/hardware/digital_input_stamper.rs b/src/hardware/digital_input_stamper.rs index cc764f5..baddeba 100644 --- a/src/hardware/digital_input_stamper.rs +++ b/src/hardware/digital_input_stamper.rs @@ -44,12 +44,12 @@ impl InputStamper { ) -> Self { // Utilize the TIM5 CH4 as an input capture channel - use TI4 (the DI0 input trigger) as the // capture source. - let input_capture = + let mut input_capture = timer_channel.into_input_capture(timers::tim5::CaptureSource4::TI4); - // FIXME: hack in de-glitching filter - let regs = unsafe { &*hal::stm32::TIM5::ptr() }; - regs.ccmr2_input().modify(|_, w| w.ic4f().bits(0b0011)); + // Do not prescale the input capture signal - require 8 consecutive samples to record an + // incoming event - this prevents spurious glitches from triggering captures. + input_capture.configure_filter(timers::InputFilter::Div1N8); Self { capture_channel: input_capture, diff --git a/src/hardware/timers.rs b/src/hardware/timers.rs index e2cbbc8..fe90677 100644 --- a/src/hardware/timers.rs +++ b/src/hardware/timers.rs @@ -48,6 +48,13 @@ pub enum SlaveMode { Trigger = 0b0110, } +/// Optional input capture preconditioning filter configurations. +#[allow(dead_code)] +pub enum InputFilter { + Div1N1 = 0b0000, + Div1N8 = 0b0011, +} + macro_rules! timer_channels { ($name:ident, $TY:ident, $size:ty) => { paste::paste! { @@ -334,6 +341,16 @@ macro_rules! timer_channels { let regs = unsafe { &*<$TY>::ptr() }; regs.sr.read().[< cc $index of >]().bit_is_set() } + + /// Configure the input capture input pre-filter. + /// + /// # Args + /// * `filter` - The desired input filter stage configuration. Defaults to disabled. + #[allow(dead_code)] + pub fn configure_filter(&mut self, filter: super::InputFilter) { + let regs = unsafe { &*<$TY>::ptr() }; + regs.[< $ccmrx _input >]().modify(|_, w| w.[< ic $index f >]().bits(filter as u8)); + } } // Note(unsafe): This manually implements DMA support for input-capture channels. This