-
Notifications
You must be signed in to change notification settings - Fork 2k
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: introduce sysclk function to retrieve core clock frequency #17342
Conversation
Some static checks nitpicks, lets see if murdock spots some important issues. |
sys/include/clk.h
Outdated
return CLOCK_CORECLOCK; | ||
} | ||
#else | ||
uint32_t sysclk(void); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we require this to be a static inline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On fe310, this function is not static inline. it could be but that would need an indirection to another function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But that wouldn't be an issue, would it? It would be IMO easier to read to have
extern uint32_t sysclk_implementation(void);
static uint32_t sysclk(void)
{
return sysclk_implementation ();
}
well encapsulated in the implementation rather than preprocessor conditionals in tge public header. But I guess it is a matter of personal taste :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
encapsulated in the implementation rather than preprocessor conditionals in tge public header
That would move the preprocessor conditionals in all cpu implementations. But I agree that when CLOCK_CORECLOCK
is not defined, we can use an extern
to a concrete cpu level implementation.
Maybe the name sysclk is not right. I agree that it has a different meaning on stm32. Maybe |
these options both seem better to me they also are more similar to the marco they replace (I got a slight pref for the shorter one (there isn't much readability gain in the snakey _clock)) cpu_freq() seems also reasonable |
cpu/fe310/clock.c
Outdated
cpu_coreclk = PRCI_measure_mcycle_freq(3000, RTC_FREQ); | ||
cpu_coreclk = PRCI_measure_mcycle_freq(3000, RTC_FREQ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cpu_coreclk = PRCI_measure_mcycle_freq(3000, RTC_FREQ); | |
cpu_coreclk = PRCI_measure_mcycle_freq(3000, RTC_FREQ); | |
cpu_coreclk = PRCI_measure_mcycle_freq(3000, RTC_FREQ); | |
cpu_coreclk = PRCI_measure_mcycle_freq(3000, RTC_FREQ); |
cpu/fe310/clock.c
Outdated
/* Clock frequency with HFROSC cannot be determined precisely from | ||
settings */ | ||
/* If not done already, estimate the CPU frequency */ | ||
if (_cpu_frequency == 0) { | ||
/* Ignore the first run (for icache reasons) */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/* Ignore the first run (for icache reasons) */ | |
/* Ignore the first run (for icache reasons) */ |
The binary diff changes for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also ran the changed tests on arduino-uno arduino-zero atmega256rfr2-xpro b-l072z-lrwan1 frdm-k64f frdm-kw41z hifive1b i-nucleo-lrwan1 nrf52dk nucleo-f030r8 nucleo-f091rc nucleo-f103rb nucleo-f207zg nucleo-f303k8 nucleo-f303re nucleo-f334r8 nucleo-f767zi nucleo-g071rb nucleo-g474re nucleo-l073rz nucleo-l152re nucleo-l433rc nucleo-l452re nucleo-l496zg nucleo-l4r5zi nucleo-l552ze-q nucleo-wl55jc openmote-b p-nucleo-wb55 samr21-xpro slstk3402a z1
All passed excepto some stm32
where there was a race condition in downloading cmsis
and openmote-b
where periph-timer
fails on master as well. See here for details
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please squash the fe310
nitpicks right away, once fe310
test results are provided I'll ACK.
It probably changes because of the removed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume it is guaranteed that coreclk() always is either a read of a non-volatile global or a const; but somehow when i read this i assume there might be a function call and a beginner might be inspired by this.
Hifive1b output with
(And the LED is blinking) |
@aabadie: did you disable the use of ztimer in blinkys-main?
|
This allows access to CLOCK_CORECLOCK from periph_conf.h which is used by clk.h
Not in the first place but I just did and got the same result. |
All last comments are addressed and directly squashed. |
For ESPs there no code changes, only two additional includes that are correct. |
lets ping @fjmolinas for the ack |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK from my side, I tested as much as I could.
Contribution description
This PR introduces the
sysclk()
to get the value of the system core clock frequency. In most situation, this frequency corresponds to theCLOCK_CORECLOCK
constant. But in some cases, this constant is wrong or not defined:As an example, doing the following doesn't build on hifive1b:
So the idea of this PR is to wrap
CLOCK_CORECLOCK
in a static inline function calledsysclk
(suggestions for a better name welcome), inspired by what was done with fe310 and let the cpu implementation define its own version when it cannot be determined at compile time, but only a runtime.With compiler optimization, using this function has no effect compared to directly using the constant.
All tests, examples, drivers and pkg that are using
CLOCK_CORECLOCK
are adapted to use this function. We could also imagine to use it in the CPU implementations directly (like the fe310).Last, but not least, I wasn't sure what's the best location for the header
clk.h
. For the moment, it's insys/
, before I put it incore/
but maybe it would be preferable to put it incpu/
and in this case, where ? Suggestion welcome.Testing procedure
tests/driver_ws281x
Issues/PRs references