2020-12-08 20:53:34 +08:00
|
|
|
///! Digital Input 0 (DI0) reference clock timestamper
|
|
|
|
///!
|
|
|
|
///! This module provides a means of timestamping the rising edges of an external reference clock on
|
|
|
|
///! the DI0 with a timer value from TIM5.
|
|
|
|
///!
|
|
|
|
///! # Design
|
|
|
|
///! An input capture channel is configured on DI0 and fed into TIM5's capture channel 4. TIM5 is
|
2020-12-08 23:14:27 +08:00
|
|
|
///! then run in a free-running mode with a configured tick rate (PSC) and maximum count value
|
|
|
|
///! (ARR). Whenever an edge on DI0 triggers, the current TIM5 counter value is captured and
|
|
|
|
///! recorded as a timestamp. This timestamp can be either directly read from the timer channel or
|
|
|
|
///! can be collected asynchronously via DMA collection.
|
2020-12-08 20:53:34 +08:00
|
|
|
///!
|
2020-12-08 23:14:27 +08:00
|
|
|
///! To prevent silently discarding timestamps, the TIM5 input capture over-capture flag is
|
|
|
|
///! continually checked. Any over-capture event (which indicates an overwritten timestamp) then
|
|
|
|
///! triggers a panic to indicate the dropped timestamp so that design parameters can be adjusted.
|
2020-12-08 20:53:34 +08:00
|
|
|
///!
|
|
|
|
///! # Tradeoffs
|
|
|
|
///! It appears that DMA transfers can take a significant amount of time to disable (400ns) if they
|
|
|
|
///! are being prematurely stopped (such is the case here). As such, for a sample batch size of 1,
|
|
|
|
///! this can take up a significant amount of the total available processing time for the samples.
|
2020-12-15 21:34:14 +08:00
|
|
|
///! This module checks for any captured timestamps from the timer capture channel manually. In
|
|
|
|
///! this mode, the maximum input clock frequency supported is dependant on the sampling rate and
|
|
|
|
///! batch size.
|
2020-12-08 20:53:34 +08:00
|
|
|
///!
|
2020-12-15 21:34:14 +08:00
|
|
|
///! This module only supports DI0 for timestamping due to trigger constraints on the DIx pins. If
|
|
|
|
///! timestamping is desired in DI1, a separate timer + capture channel will be necessary.
|
2021-01-18 23:47:47 +08:00
|
|
|
use super::{hal, timers};
|
2020-11-12 01:28:48 +08:00
|
|
|
|
2020-12-08 20:53:34 +08:00
|
|
|
/// The timestamper for DI0 reference clock inputs.
|
2020-11-12 01:28:48 +08:00
|
|
|
pub struct InputStamper {
|
2020-12-08 00:58:36 +08:00
|
|
|
_di0_trigger: hal::gpio::gpioa::PA3<hal::gpio::Alternate<hal::gpio::AF2>>,
|
2020-12-15 21:34:14 +08:00
|
|
|
capture_channel: timers::tim5::Channel4InputCapture,
|
2020-11-12 01:28:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InputStamper {
|
2020-12-08 20:53:34 +08:00
|
|
|
/// Construct the DI0 input timestamper.
|
|
|
|
///
|
|
|
|
/// # Args
|
|
|
|
/// * `trigger` - The capture trigger input pin.
|
|
|
|
/// * `timer_channel - The timer channel used for capturing timestamps.
|
2020-11-12 01:28:48 +08:00
|
|
|
pub fn new(
|
2020-12-08 00:58:36 +08:00
|
|
|
trigger: hal::gpio::gpioa::PA3<hal::gpio::Alternate<hal::gpio::AF2>>,
|
|
|
|
timer_channel: timers::tim5::Channel4,
|
2020-11-12 01:28:48 +08:00
|
|
|
) -> Self {
|
2020-12-08 00:58:36 +08:00
|
|
|
// Utilize the TIM5 CH4 as an input capture channel - use TI4 (the DI0 input trigger) as the
|
2020-11-12 01:28:48 +08:00
|
|
|
// capture source.
|
2020-12-08 00:58:36 +08:00
|
|
|
let input_capture =
|
2021-02-03 20:03:17 +08:00
|
|
|
timer_channel.into_input_capture(timers::tim5::CaptureSource4::TI4);
|
2020-11-12 01:28:48 +08:00
|
|
|
|
2021-02-04 19:47:35 +08:00
|
|
|
// FIXME: hack in de-glitching filter
|
|
|
|
let regs = unsafe { &*hal::stm32::TIM5::ptr() };
|
|
|
|
regs.ccmr2_input().modify(|_, w| w.ic4f().bits(0b0011));
|
|
|
|
|
2020-11-12 01:28:48 +08:00
|
|
|
Self {
|
2020-12-08 20:53:34 +08:00
|
|
|
capture_channel: input_capture,
|
2020-11-12 01:28:48 +08:00
|
|
|
_di0_trigger: trigger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 21:34:14 +08:00
|
|
|
/// Start to capture timestamps on DI0.
|
2021-01-18 23:47:47 +08:00
|
|
|
#[allow(dead_code)]
|
2020-12-08 23:14:27 +08:00
|
|
|
pub fn start(&mut self) {
|
2020-12-15 21:34:14 +08:00
|
|
|
self.capture_channel.enable();
|
2020-12-08 23:14:27 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 21:34:14 +08:00
|
|
|
/// Get the latest timestamp that has occurred.
|
|
|
|
///
|
|
|
|
/// # Note
|
2021-02-02 19:34:07 +08:00
|
|
|
/// This function must be called at least as often as timestamps arrive.
|
|
|
|
/// If an over-capture event occurs, this function will clear the overflow,
|
|
|
|
/// and return a new timestamp of unknown recency an `Err()`.
|
|
|
|
/// Note that this indicates at least one timestamp was inadvertently dropped.
|
2021-01-18 23:47:47 +08:00
|
|
|
#[allow(dead_code)]
|
2021-02-02 19:34:07 +08:00
|
|
|
pub fn latest_timestamp(&mut self) -> Result<Option<u32>, Option<u32>> {
|
|
|
|
self.capture_channel.latest_capture()
|
2020-11-12 01:28:48 +08:00
|
|
|
}
|
|
|
|
}
|