stm32f4xx-hal icon indicating copy to clipboard operation
stm32f4xx-hal copied to clipboard

GetTick and DelayUntilMs/DelayUntilUs utilities for Delay?

Open romancardenas opened this issue 2 years ago • 1 comments

Hi! I think it would be nice to add a DelayUntil utility to delays. In this way, we can have actual periodic tasks, regardless of the time consumed by a task when executing. To do so, we would need to add the following methods to delays:

  • getTick(&self) → X (I'm not sure which the return value should be): it returns the current internal clock tick value.
  • delayUntilMs(&mut self, baseline_tick: X, ms: u16): it waits the given amount of milliseconds FROM THE BASELINE.
  • delayUntilUs(&mut self, baseline_tick: X, us: u32): it waits the given amount of microseconds FROM THE BASELINE.

With that, we could do something like this:

let tick = timer.getTick();  // tick now represents the time
do_stuff(); // this takes time...
timer.delayUntilMs(tick, 1_000); // we wait one second starting from before we triggered do_stuff

Thus, we would not delay for one second, but 1 second SINCE WE CAPTURED THE BASELINE.

What do you think? Is there anything I'm missing that makes this a bad idea?

romancardenas avatar Aug 10 '22 13:08 romancardenas

Maybe it is better to implement this feature in the cortex-m crate, so more HALs would benefit from it. I opened this issue in the cortex_m GitHub repo.

romancardenas avatar Aug 10 '22 14:08 romancardenas