Updating input capture for timers

This commit is contained in:
Ryan Summers 2021-02-03 13:03:17 +01:00
parent 14abaad7de
commit b57b666473
3 changed files with 22 additions and 12 deletions

View File

@ -71,6 +71,9 @@ const APP: () = {
// Start sampling ADCs. // Start sampling ADCs.
stabilizer.adc_dac_timer.start(); stabilizer.adc_dac_timer.start();
// Enable the timestamper.
stabilizer.timestamper.start();
init::LateResources { init::LateResources {
afes: stabilizer.afes, afes: stabilizer.afes,
adcs: stabilizer.adcs, adcs: stabilizer.adcs,

View File

@ -45,7 +45,7 @@ impl InputStamper {
// Utilize the TIM5 CH4 as an input capture channel - use TI4 (the DI0 input trigger) as the // Utilize the TIM5 CH4 as an input capture channel - use TI4 (the DI0 input trigger) as the
// capture source. // capture source.
let input_capture = let input_capture =
timer_channel.into_input_capture(timers::CaptureTrigger::Input24); timer_channel.into_input_capture(timers::tim5::CaptureSource4::TI4);
Self { Self {
capture_channel: input_capture, capture_channel: input_capture,

View File

@ -1,13 +1,14 @@
///! The sampling timer is used for managing ADC sampling and external reference timestamping. ///! The sampling timer is used for managing ADC sampling and external reference timestamping.
use super::hal; use super::hal;
/// The source of an input capture trigger. use hal::stm32::{
#[allow(dead_code)] // TIM1 and TIM8 have identical registers.
pub enum CaptureTrigger { tim1 as __tim8,
Input13 = 0b01, tim2 as __tim2,
Input24 = 0b10, // TIM2 and TIM5 have identical registers.
TriggerInput = 0b11, tim2 as __tim5,
} tim3 as __tim3,
};
/// The event that should generate an external trigger from the peripheral. /// The event that should generate an external trigger from the peripheral.
#[allow(dead_code)] #[allow(dead_code)]
@ -225,6 +226,8 @@ macro_rules! timer_channels {
($index:expr, $TY:ty, $ccmrx:expr, $size:ty) => { ($index:expr, $TY:ty, $ccmrx:expr, $size:ty) => {
paste::paste! { paste::paste! {
pub use super::[< __ $TY:lower >]::[< $ccmrx _input >]::[< CC $index S_A>] as [< CaptureSource $index >];
/// A capture/compare channel of the timer. /// A capture/compare channel of the timer.
pub struct [< Channel $index >] {} pub struct [< Channel $index >] {}
@ -232,6 +235,8 @@ macro_rules! timer_channels {
pub struct [< Channel $index InputCapture>] {} pub struct [< Channel $index InputCapture>] {}
impl [< Channel $index >] { impl [< Channel $index >] {
/// Construct a new timer channel. /// Construct a new timer channel.
/// ///
/// Note(unsafe): This function must only be called once. Once constructed, the /// Note(unsafe): This function must only be called once. Once constructed, the
@ -267,12 +272,11 @@ macro_rules! timer_channels {
/// # Args /// # Args
/// * `input` - The input source for the input capture event. /// * `input` - The input source for the input capture event.
#[allow(dead_code)] #[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() }; let regs = unsafe { &*<$TY>::ptr() };
// Note(unsafe): The bit configuration is guaranteed to be valid by the regs.[< $ccmrx _input >]().modify(|_, w| w.[< cc $index s>]().variant(input));
// CaptureTrigger enum definition.
regs.[< $ccmrx _input >]().modify(|_, w| unsafe { w.[< cc $index s>]().bits(input as u8) });
[< Channel $index InputCapture >] {} [< Channel $index InputCapture >] {}
} }
@ -316,6 +320,9 @@ macro_rules! timer_channels {
/// Enable the input capture to begin capturing timer values. /// Enable the input capture to begin capturing timer values.
#[allow(dead_code)] #[allow(dead_code)]
pub fn enable(&mut self) { 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. // Note(unsafe): This channel owns all access to the specific timer channel.
// Only atomic operations on completed on the timer registers. // Only atomic operations on completed on the timer registers.
let regs = unsafe { &*<$TY>::ptr() }; let regs = unsafe { &*<$TY>::ptr() };