Skip to content

Limitation in From<MicroSecond> for Hertz: Cannot configure timers for periods > 1s #232

@Patricio-Andre

Description

@Patricio-Andre

I've encountered a runtime panic when trying to initialize a countdown timer with a duration longer than 1 second. The issue stems from the integer arithmetic used when converting MicroSecond durations into Hertz.

Because the HAL attempts to convert the duration into a frequency, any duration longer than 1 second results in a frequency lower than 1 Hz. Since Hertz wraps an integer, this truncates to 0, triggering the assertion.

// This is the absolute maximum allowed
let mut count_down_timer = timer.start_count_down(1000.ms());

However, increasing the duration by just 1ms causes a panic.
Root Cause In the implementation of From for Hertz:

impl From<MicroSecond> for Hertz {
    fn from(period: MicroSecond) -> Hertz {
        assert!(period.0 > 0 && period.0 <= 1_000_000);
        Hertz(1_000_000 / period.0)
    }
}

The STM32 hardware timers are capable of counting periods much longer than 1 second (using appropriate prescalers and ARR values). The HAL API should support passing Duration directly to start_count_down without forcing a lossy conversion to Hertz, or at least support a milliHertz type to allow lower frequencies.

Alternatively, if Hertz is strictly required, we might need a workaround for frequencies <1 Hz.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions