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

Make SYST::get_current take &self? #460

Open
nmattia opened this issue Jan 14, 2023 · 1 comment
Open

Make SYST::get_current take &self? #460

nmattia opened this issue Jan 14, 2023 · 1 comment

Comments

@nmattia
Copy link

nmattia commented Jan 14, 2023

Hi,

I'm just getting started with embedded rust, so apologies if this is a silly question!

While trying to get a tick counter, I realized SYST::get_current() would (almost) always return 0:

    /// Gets current value
    #[inline]
    pub fn get_current() -> u32 {
        // NOTE(unsafe) atomic read with no side effects
        unsafe { (*Self::PTR).cvr.read() }
    }

I hadn't read the docs properly and was actually using the systick somewhere else:

    let core = pac::CorePeripherals::take().unwrap();
    let mut syst = core.SYST;
    syst.set_reload(0); // very small value as an example
    syst.clear_current();
    syst.enable_counter();

Without reading the cortex docs and the cortex-m crate sources, I would never have figured the reason why get_current was almost always zero (reason as far as I understand: it will increment until the value passed to set_reload() and then wrap).

I was wondering, wouldn't it be safer to also have get_current() take a &SYST? Then it would also be clearer that get_current() and other functions (set_reload, enable_counter, etc) are related.

Again, apologies if this is silly! I'm just getting started with rust embedded.

@chemicstry
Copy link

At least in my experience systick is mostly used as a debug tool to measure code execution performance and clock ticks. Hence, it is often used in random parts of the code and carrying &SYST everywhere would be very inconvenient.

I think best option would be to improve get_current() docs and mention how to properly setup and enable systick.

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

No branches or pull requests

2 participants