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

sys/ztimer: make use of periph_timer_query_freqs #20582

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

maribu
Copy link
Member

@maribu maribu commented Apr 15, 2024

Contribution description

This makes use of the periph_timer_query_freqs feature:

  1. It does choose the closest frequency supported before calling timer_init() in the ztimer_periph_timer backend.
  2. It does make use of the actually chosen frequency when using ztimer_convert_frac.
  3. It does assert() the frequency is within 5% of the specified when no frequency conversion is performed or ztimer_convert_shift_up is used.

Motivation

We have a number of MCUs that can run from internal oscillators (without using external crystal for accuracy):

  • ATmega MCUs
  • STM32 MCUs
  • MSP430 MCUs
  • RP2040
  • FE310 (but it doesn't have a proper periph_timer, so not relevant here)
  • ...

If those MCUs are used without external crystal, the actual CPU frequency may be off from what the the board config aims for significantly due to large production variance and temperature and supply voltage dependence. As a result, all periph_timer instances driven by the same clock will also be off significantly, which can hinder accuracy in time keeping significantly.

This adds the support to ztimer to react on the actual frequency information provided by periph_timer_query_freqs and adjust the time keeping accordingly.

Testing procedure

As of now, only the MSP430 (and the FE310) will measure the actual CPU frequency at runtime, and only the MSP430 timer will compensate frequencies exposed via timer_query_freqs(). So only on MSP430 this can really be tested now, e.g. with tests/sys/ztimer_msg.

In master this test sadly fails because of timer_set() not yet working correctly on small timeouts (and waiting one period longer than needed).

With this PR the test passes and the messages at second 30 is indeed about thirty seconds after the start message received. (It would be off by 40 % without compensation.)

Issues/PRs references

Fixes #8251

Add an API to search for the frequency supported by a timer that
is closest to the given target frequency.

This is in fact non-trivial to get right, as pre-scaler registers can
be 16 bit or even 32 bit in size, making a naive loop over all possible
pre-scalers too expensive (computationally).
@maribu maribu added Type: new feature The issue requests / The PR implemements a new feature for RIOT CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Apr 15, 2024
@github-actions github-actions bot added Area: tests Area: tests and testing framework Area: drivers Area: Device drivers Area: timers Area: timer subsystems Area: boards Area: Board ports Area: sys Area: System labels Apr 15, 2024
@riot-ci
Copy link

riot-ci commented Apr 15, 2024

Murdock results

✔️ PASSED

4933b9b boards/olimex-msp430-h1611: fix ztimer config

Success Failures Total Runtime
10045 0 10045 14m:01s

Artifacts

@maribu maribu force-pushed the ztimer_periph_timer_query_freqs branch from e1ba4e2 to a1a90c7 Compare April 16, 2024 16:22
This makes use of the `periph_timer_query_freqs` feature:

1. It does choose the closest frequency supported before calling
   timer_init() in the ztimer_periph_timer backend.
2. It does make use of the actually chosen frequency when using
   `ztimer_convert_frac`.
3. It does `assert()` the frequency is within 5% of the specified when
   no frequency conversion is performed or `ztimer_convert_shift_up`
   is used.
@maribu maribu force-pushed the ztimer_periph_timer_query_freqs branch from a1a90c7 to 6d3abf6 Compare April 16, 2024 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: boards Area: Board ports Area: drivers Area: Device drivers Area: sys Area: System Area: tests Area: tests and testing framework Area: timers Area: timer subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MSP430: periph_timer clock config wrong
2 participants