-
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
cpu/sam0: provide interface to query GCLK frequency #12969
Conversation
Let me mention it once more. There are now a lot of places where the code has In the mean time a define was added in
into
|
Alright, that makes sense! |
89fcbb8
to
e48dde5
Compare
I ran |
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.
It looks good to me. |
Nice ! @benpicco Please squash ! |
To simplify board definitions and for unification between samd2x and newer models, don't use the GCLK bitmask in board definitions. Instead use the GCLK index and generate the bitmask when needed.
Instead of hard-coding the peripheral clocks to CLOCK_CORECLOCK introduce helper functions to return the frequency of the individual GCLKs and use those for baud-rate calculations. This requires the GCLK to be part of the peripheral's config struct. While this is already the case for most peripherals, this also adds it for those where it wasn't used before. As it defaults to 0 (CLOCK_CORECLOCK) no change is to be expected.
We can't run I2C off the 120 MHz main clock as the availiable dividers are too small. Use the 48 MHz GCLK 6 instead which offers an appropriate frequency. fixes RIOT-OS#12037
Use the same 32 kHz GCLK to feed the PLL and the RTT, etc.
Give the clocks explicit names to better identify their meaning.
Squashed & rebased to the master. |
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.
Thanks for this PR @benpicco !
Here we go. |
Thank you all for reviewing & testing this! |
@@ -72,19 +94,35 @@ static void clk_init(void) | |||
while (!(SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_OSC8MRDY)) {} | |||
#endif | |||
|
|||
/* Setup GCLK2 with divider 1 (32.768kHz) */ |
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.
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 see you use it for the dfll, I'll move GCLK->CTRL.reg = GCLK_CTRL_SWRS
up.
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.
Hm previously this was in the #if CLOCK_USE_PLL
block, but a 32kHz clock is always needed so I moved it here.
I must have missed the GCLK_CTRL_SWRS
bit.
Contribution description
The sam0 family offers multiple GCLKs that can be sourced from different timers.
However, all peripherals were effectively always clocked by the hard-coded
CLOCK_CORECLOCK
.This causes several issues:
This introduces a
sam0_gclk_freq()
function to query the frequency of the individual GCLKs, so other GCLKs than 0 can be used for peripherals.This would also allow to change the CPU frequency at run-time without affecting peripherals, unless they use GCLKs sourced from GCLK0.
The first commit in this series also changes the
.gclk_src
config struct from a bit mask to the GCLK ID.While not strictly necessary, it cleans up the board configurations and removes the need to differentiate between samd2x and later MCUs.
Testing procedure
In only tested the changes on
samr21-xpro
andsame54-xpro
On
same54-xpro
you will find thati2c_scan
(and I2C in general) now works (USEMODULE += i2c_scan
), before it would get stuck due to a wrong clock frequency.Issues/PRs references
fixes #12037