From b57b66647399545475576e7d2a7cff97b115771c Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Wed, 3 Feb 2021 13:03:17 +0100 Subject: [PATCH] Updating input capture for timers --- src/bin/lockin-external.rs | 3 +++ src/hardware/digital_input_stamper.rs | 2 +- src/hardware/timers.rs | 29 +++++++++++++++++---------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/bin/lockin-external.rs b/src/bin/lockin-external.rs index 11d4958..8dc9907 100644 --- a/src/bin/lockin-external.rs +++ b/src/bin/lockin-external.rs @@ -71,6 +71,9 @@ const APP: () = { // Start sampling ADCs. stabilizer.adc_dac_timer.start(); + // Enable the timestamper. + stabilizer.timestamper.start(); + init::LateResources { afes: stabilizer.afes, adcs: stabilizer.adcs, diff --git a/src/hardware/digital_input_stamper.rs b/src/hardware/digital_input_stamper.rs index 6bd8629..1aa0bb1 100644 --- a/src/hardware/digital_input_stamper.rs +++ b/src/hardware/digital_input_stamper.rs @@ -45,7 +45,7 @@ impl InputStamper { // Utilize the TIM5 CH4 as an input capture channel - use TI4 (the DI0 input trigger) as the // capture source. let input_capture = - timer_channel.into_input_capture(timers::CaptureTrigger::Input24); + timer_channel.into_input_capture(timers::tim5::CaptureSource4::TI4); Self { capture_channel: input_capture, diff --git a/src/hardware/timers.rs b/src/hardware/timers.rs index b686220..da11cf3 100644 --- a/src/hardware/timers.rs +++ b/src/hardware/timers.rs @@ -1,13 +1,14 @@ ///! The sampling timer is used for managing ADC sampling and external reference timestamping. use super::hal; -/// The source of an input capture trigger. -#[allow(dead_code)] -pub enum CaptureTrigger { - Input13 = 0b01, - Input24 = 0b10, - TriggerInput = 0b11, -} +use hal::stm32::{ + // TIM1 and TIM8 have identical registers. + tim1 as __tim8, + tim2 as __tim2, + // TIM2 and TIM5 have identical registers. + tim2 as __tim5, + tim3 as __tim3, +}; /// The event that should generate an external trigger from the peripheral. #[allow(dead_code)] @@ -225,6 +226,8 @@ macro_rules! timer_channels { ($index:expr, $TY:ty, $ccmrx:expr, $size:ty) => { paste::paste! { + pub use super::[< __ $TY:lower >]::[< $ccmrx _input >]::[< CC $index S_A>] as [< CaptureSource $index >]; + /// A capture/compare channel of the timer. pub struct [< Channel $index >] {} @@ -232,6 +235,8 @@ macro_rules! timer_channels { pub struct [< Channel $index InputCapture>] {} impl [< Channel $index >] { + + /// Construct a new timer channel. /// /// Note(unsafe): This function must only be called once. Once constructed, the @@ -267,12 +272,11 @@ macro_rules! timer_channels { /// # Args /// * `input` - The input source for the input capture event. #[allow(dead_code)] - pub fn into_input_capture(self, input: super::CaptureTrigger) -> [< Channel $index InputCapture >]{ + pub fn into_input_capture(self, input: [< CaptureSource $index >]) -> [< Channel $index InputCapture >]{ let regs = unsafe { &*<$TY>::ptr() }; - // Note(unsafe): The bit configuration is guaranteed to be valid by the - // CaptureTrigger enum definition. - regs.[< $ccmrx _input >]().modify(|_, w| unsafe { w.[< cc $index s>]().bits(input as u8) }); + regs.[< $ccmrx _input >]().modify(|_, w| w.[< cc $index s>]().variant(input)); + [< Channel $index InputCapture >] {} } @@ -316,6 +320,9 @@ macro_rules! timer_channels { /// Enable the input capture to begin capturing timer values. #[allow(dead_code)] pub fn enable(&mut self) { + // Read the latest input capture to clear any pending data in the register. + let _ = self.latest_capture(); + // Note(unsafe): This channel owns all access to the specific timer channel. // Only atomic operations on completed on the timer registers. let regs = unsafe { &*<$TY>::ptr() };