Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MonoTimer and Timer::syst frequency #236

Merged
merged 2 commits into from
Jun 25, 2020

Conversation

TheZoq2
Copy link
Member

@TheZoq2 TheZoq2 commented Jun 24, 2020

Continuation of #191, broken into its own PR because I didn't want to force push to someone elses master branch 😉

After some correct testing, I reproduced the bug. I also noticed the same problem with Timer::syst using the following code:

#![no_main]
#![no_std]

use panic_halt as _;

use stm32f1xx_hal as hal;

use crate::hal::{
    gpio::{gpioc, Output, PushPull},
    pac::{Peripherals, CorePeripherals, TIM2},
    prelude::*,
    timer::{CountDownTimer, Event, Timer},
    time::MonoTimer,
};

use core::cell::RefCell;
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use nb::block;
use cortex_m_semihosting::hprintln;

#[entry]
fn main() -> ! {
    let dp = Peripherals::take().unwrap();
    let cp = CorePeripherals::take().unwrap();

    let mut rcc = dp.RCC.constrain();
    let mut flash = dp.FLASH.constrain();
    let clocks = rcc
        .cfgr
        .sysclk(16.mhz())
        .hclk(4.mhz())
        .freeze(&mut flash.acr);

    hprintln!("sysclk {}", clocks.sysclk().0);
    hprintln!("hclk {}", clocks.hclk().0);

    let mono = MonoTimer::new(cp.DWT, clocks);

    let mut timer = Timer::syst(cp.SYST, &clocks)
        .start_count_down(1.hz());

    let mut start = mono.now();

    loop {
        block!(timer.wait()).unwrap();
        hprintln!("{}", start.elapsed() as f32 / mono.frequency().0 as f32).unwrap();
        timer.start(1.hz());
        start = mono.now();
    }
}

which uses the now functioning MonoTimer to measure the incorrectness of syst, which is now 4 times slower than intended.

cc: @ra-kete

teskje and others added 2 commits June 24, 2020 23:10
This commit sets `MonoTimer`'s frequency to HCLK.

The `MonoTimer` internally counts the cycles of the Cortex core.
The core's clock is HCLK, but SYSCLK was used for the timer's frequency
before this commit.
Copy link
Member

@therealprof therealprof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@therealprof therealprof merged commit b162517 into stm32-rs:master Jun 25, 2020
@TheZoq2 TheZoq2 mentioned this pull request Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants