Skip to content

Make SYST::get_current take &self? #460

@nmattia

Description

@nmattia

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions