-
-
Notifications
You must be signed in to change notification settings - Fork 771
stm32f4xx: clocks rework #4008
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
Merged
Merged
stm32f4xx: clocks rework #4008
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bradjc
requested changes
May 28, 2024
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.
Blocked on removing the as
in the imports.
Introduce a Stm32f4Clocks trait to let peripheral clocks access clocks features without the need of the ChipSpecific bind. Note: this moves the clocks struct initialization into the board crates since it has to be already instantiated when passing its reference to the peripherals. This commit also removes the rcc reference passing to peripherals since it is now accessed from the Stm32f4Clocks trait object.
…odule Peripheral clock logic moved from rcc.rs to a new clocks/periph.rs module. All peripheral modules adapted to access the peripheral clock though the new module path.
…thods The clocks module uses frequencies in MHz base unit. Also, peripherals needs the system clock frequency in Hz base unit. Thus, for clarity, make it explicit when MHz base unit is used and consider Hz as base unit when not specified. This seems to be the general policy around the code base, for instance the kernel::hil::time::Frequency trait.
These getters were added in commit f0d2308 stm32f4xx: rcc: implement get_frequency method for peripheral clocks Renamed just to comply with the names of their respective setters.
Peripheral clocks used to rely on the rcc module to retrieve the HCLK frequency (mistakenly called SYSCLK as SYSCLK should not consider the AHB prescaler). Such implementation had the limitation that the rcc module was not aware of the HSE clock settings, meaning that dynamic frequency operations were not supported when the HSE clock was used as SYSCLK source. See commit f0d2308: stm32f4xx: rcc: implement get_frequency method for peripheral clocks This commit instead exposes the HCLK frequency calculated by the clocks module to peripheral clocks, thus extending the support for the HSE source clock. This commit also moves the methods to retrieve the PLL frequency and SYSCLK frequency (without AHB prescaler division) from the rcc module to the pll and clocks modules respectively. They are not necessary anymore since PLL and SYSCLK frequencies are being retrieved from cached variables, but are kept for future use.
28f3515
to
d32ebff
Compare
@bradjc I applied the changes based on your suggestions. I also rebased on top of the current master and rerun the tests mentioned in the PR description. |
FYI @wjakobczyk and @alexandruradovici |
alevy
approved these changes
May 29, 2024
bradjc
approved these changes
May 30, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
chips/stm32
Change pertains to the stm32 family of MCUSs
WG-Network
In the purview of the Network working group.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Overview
This PR is a continuation of PR #3888. It is mostly a refactoring to allow peripheral clocks to access the clocks module features. The main motivation is to allow peripherals to get their clock frequency also when the HSE clock is used as PLL source or SYSCLK source, that was not possible before this PR because the HSE clock is controlled by the clocks module but peripheral clocks were only able to access the rcc module. Also, this would also help to extend the peripherals implementation in future: other peripherals might need to access some other clock info from the clocks module (e.g. PLLI2S or PLL48CK). To let peripheral access the clocks module, a
ClocksCtrl
trait object is passed to the peripherals. Even if a new trait seems unnecessary here, it avoids the peripherals to become chip specific: they would had required theChipSpecific
trait bound generic instead. At first, I tried to just pass aClocks
struct reference to peripherals but they became far more verbose and most of the implementation became bound to theChipSpecific
trait. There were also lifetime conflicts between theChipSpecific
generic and gpio pins. Here a non working example. Thus I think that using theClocksCtrl
trait is the less intrusive approach for now and might be reconsidered in future.This PR also comes with some other minor refactoring and cleanup for rcc and clocks modules.
Testing Strategy
The tests listed in #3888 have been re-run on a nucleo_f39zi board with the following additional configurations:
Clock Config 5
Clock Config 6
Clock Config 7
I also had to increase the enable wait cycles for PLL and HSE clocks due to #4004 :
With all configurations (1 to 7) I repeated the usart tests (process console at different baud rates 9600, 115200, 921600) and the timer test (simple application toggling a gpio every second).
All tests succeeded without issues except for config 6 with usart at baudrate 921600, but this is likely due to the baud error being too high at that configuration, no issue with baudrate 460800.
TODO or Help Wanted
This PR affects all the stm32f4 boards, but I could only test the nucleo_f439zi board.
Documentation Updated
/docs
, or no updates are required.Formatting
make prepush
.