cpu/sam0: provide interface to query GCLK frequency#12969
cpu/sam0: provide interface to query GCLK frequency#12969dylad merged 6 commits intoRIOT-OS:masterfrom
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 |
|
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. |
|
Here we go. |
|
Thank you all for reviewing & testing this! |
| while (!(SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_OSC8MRDY)) {} | ||
| #endif | ||
|
|
||
| /* Setup GCLK2 with divider 1 (32.768kHz) */ |
There was a problem hiding this comment.
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.
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_srcconfig 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-xproandsame54-xproOn
same54-xproyou 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