Updating timestamp timer to be more precise

This commit is contained in:
Ryan Summers 2020-12-07 18:11:46 +01:00
parent ec046bc42d
commit b191a3f01d
2 changed files with 43 additions and 8 deletions

View File

@ -289,14 +289,21 @@ const APP: () = {
let sampling_timer_channels = sampling_timer.channels();
let mut timestamp_timer = {
// TODO: This needs to be precisely controlled via the prescaler of the timer.
let timer5 = dp.TIM5.timer(
(SAMPLE_FREQUENCY_KHZ / SAMPLE_BUFFER_SIZE as u32).khz(),
ccdr.peripheral.TIM5,
&ccdr.clocks,
// The timer frequency is manually adjusted below, so the 1KHz setting here is a
// dont-care.
let timer5 =
dp.TIM5.timer(1.khz(), ccdr.peripheral.TIM5, &ccdr.clocks);
// The time stamp timer must run at exactly a multiple of the sample timer based on the
// batch size. To accomodate this, we manually set the period identical to the sample
// timer, but use a prescaler that is `BATCH_SIZE` longer.
let mut timer = timers::TimestampTimer::new(timer5);
timer.set_period(sampling_timer.get_period());
timer.set_prescaler(
sampling_timer.get_prescaler() * SAMPLE_BUFFER_SIZE as u16,
);
timers::TimestampTimer::new(timer5)
timer
};
let timestamp_timer_channels = timestamp_timer.channels();

View File

@ -32,8 +32,36 @@ macro_rules! timer_channels {
self.channels.take().unwrap()
}
/// Start the sampling timer.
pub fn start(&mut self) {
#[allow(dead_code)]
pub fn get_prescaler(&self) -> u16 {
let regs = unsafe { &*hal::stm32::$TY::ptr() };
regs.psc.read().psc().bits() + 1
}
#[allow(dead_code)]
pub fn set_prescaler(&mut self, prescaler: u16) {
let regs = unsafe { &*hal::stm32::$TY::ptr() };
assert!(prescaler >= 1);
regs.psc.write(|w| w.psc().bits(prescaler - 1));
}
#[allow(dead_code)]
pub fn get_period(&self) -> u32 {
let regs = unsafe { &*hal::stm32::$TY::ptr() };
regs.arr.read().arr().bits()
}
#[allow(dead_code)]
pub fn set_period(&mut self, period: u32) {
let regs = unsafe { &*hal::stm32::$TY::ptr() };
regs.arr.write(|w| w.arr().bits(period));
}
/// Start the timer.
pub fn start(mut self) {
// Force a refresh of the frequency settings.
self.timer.apply_freq();
self.timer.reset_counter();
self.timer.resume();
}