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 mono timer #254

Merged
merged 2 commits into from
Aug 12, 2020
Merged

Fix mono timer #254

merged 2 commits into from
Aug 12, 2020

Conversation

TheZoq2
Copy link
Member

@TheZoq2 TheZoq2 commented Aug 12, 2020

Closes #252

Unfortunately this a breaking change as we now need to take the DCP peripheral when starting the mono timer.

I also added a note from one of the linked issues about mono_timer being stopped when hprintln is running.

For reference, here is a code sample that does not work currently, but does with this fix

#![deny(unsafe_code)]
#![no_main]
#![no_std]

use panic_halt as _;

use cortex_m_semihosting::hprintln;
use cortex_m_rt::entry;
use stm32f1xx_hal::{
    pac,
    prelude::*,
    serial::{Config, Serial},
};
use embedded_hal::digital::v2::OutputPin;

use core::fmt::Write;

#[entry]
fn main() -> ! {
    let mut cp = cortex_m::peripheral::Peripherals::take().unwrap();
    let p = pac::Peripherals::take().unwrap();

    let mut flash = p.FLASH.constrain();
    let mut rcc = p.RCC.constrain();
    let clocks = rcc.cfgr.freeze(&mut flash.acr);

    let mut afio = p.AFIO.constrain(&mut rcc.apb2);
    let mut gpioc = p.GPIOC.split(&mut rcc.apb2);

    let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);


    // Needed in order for MonoTimer to work properly
    // cp.DCB.enable_trace();

    let mut delay = stm32f1xx_hal::delay::Delay::new(cp.SYST, clocks);
    let mono_timer = stm32f1xx_hal::time::MonoTimer::new(cp.DWT, cp.DCB, clocks);

    let start = mono_timer.now();

    let mut led_state = false;
    let mut prev = start.elapsed();
    loop {
        // hprintln!("{}", new);
        let new = start.elapsed();
        if prev + mono_timer.frequency().0 < new {
            if led_state {
                led.set_high();
            }
            else {
                led.set_low();
            }
            led_state = !led_state;
            prev = new;
        }
    }
}

@TheZoq2 TheZoq2 added bug Something isn't working Breaking change labels Aug 12, 2020
@TheZoq2 TheZoq2 requested a review from therealprof August 12, 2020 18:52
@TheZoq2
Copy link
Member Author

TheZoq2 commented Aug 12, 2020

I should make cargo fmt run automatically before I commit to this repo 😅

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 8816963 into stm32-rs:master Aug 12, 2020
@therealprof
Copy link
Member

Oh, but since it's a breaking change it should be mentioned in the CHANGELOG. DOH!

@TheZoq2
Copy link
Member Author

TheZoq2 commented Aug 12, 2020

Oops, I'll just do that manually with another commit :)

@TheZoq2
Copy link
Member Author

TheZoq2 commented Aug 12, 2020

Maybe we should add a CONTRIBUTING.md that just says "Oh, and don't forget to modify the changelog"

TheZoq2 added a commit to TheZoq2/stm32f1xx-hal that referenced this pull request Aug 12, 2020
@therealprof
Copy link
Member

Yeah.

TheZoq2 added a commit that referenced this pull request Aug 12, 2020
@TheZoq2 TheZoq2 mentioned this pull request Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking change bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DWT clock cycle count stuck unless trace is enabled
2 participants