-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(machine/stm32): make HSE crystal frequency selectable. #5517
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
deadprogram
merged 1 commit into
tinygo-org:dev
from
helvionics:de_stm32_selectable_xtal
Jul 19, 2026
+193
−79
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ import ( | |
| "runtime/interrupt" | ||
| ) | ||
|
|
||
| const xtalHz = 8_000_000 | ||
|
|
||
| const ( | ||
| LED = LED_BUILTIN | ||
| LED1 = LED_GREEN | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ import ( | |
| "runtime/interrupt" | ||
| ) | ||
|
|
||
| const xtalHz = 8_000_000 | ||
|
|
||
| const ( | ||
| LED1 = LED_GREEN | ||
| LED2 = LED_ORANGE | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| //go:build stm32 | ||
|
|
||
| package machine | ||
|
|
||
| // PLLParams holds the HSE main-PLL dividers/multipliers (RCC_PLLCFGR M/N/P/Q/R | ||
| // fields) needed to reach a chip's target VCO/SYSCLK frequency from a given | ||
| // crystal frequency. R is left zero on chips without a PLLR output. | ||
| type PLLParams struct { | ||
| M, N, P, Q, R uint32 | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| //go:build stm32 && stm32f103 | ||
|
|
||
| package machine | ||
|
|
||
| import "device/stm32" | ||
|
|
||
| // F103PLLParams holds the HSE prescaler (PLLXTPRE) and PLL multiplier | ||
| // (PLLMUL) needed to reach 72MHz SYSCLK from a given crystal frequency. The | ||
| // F1 PLL has no dedicated input divider, only an optional /2 HSE prescaler. | ||
| type F103PLLParams struct { | ||
| Prediv uint32 | ||
| Mul uint32 | ||
| } | ||
|
|
||
| func PLLParams72MHz() F103PLLParams { | ||
| switch xtalHz { | ||
| case 8_000_000: | ||
| return F103PLLParams{Prediv: stm32.RCC_CFGR_PLLXTPRE_Div1, Mul: stm32.RCC_CFGR_PLLMUL_Mul9} | ||
| case 12_000_000: | ||
| return F103PLLParams{Prediv: stm32.RCC_CFGR_PLLXTPRE_Div1, Mul: stm32.RCC_CFGR_PLLMUL_Mul6} | ||
| case 16_000_000: | ||
| // 16MHz / 2 (PLLXTPRE) x9 = 72MHz. | ||
| return F103PLLParams{Prediv: stm32.RCC_CFGR_PLLXTPRE_Div2, Mul: stm32.RCC_CFGR_PLLMUL_Mul9} | ||
| default: | ||
| panic("unsupported xtal frequency") | ||
| } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| //go:build stm32f4 && (stm32f405 || stm32f407) | ||
|
|
||
| package machine | ||
|
|
||
| // PLLParams168MHz returns the HSE PLL dividers needed to reach a 336MHz VCO | ||
| // (168MHz SYSCLK, P=2, Q=7) for the configured crystal frequency. M is chosen | ||
| // to bring the PLL input (HSE/M) to 2MHz, the value RM0090 pg. 95 recommends | ||
| // to minimize jitter; the previous hardcoded F407 table used 1MHz. | ||
| func PLLParams168MHz() PLLParams { | ||
| switch xtalHz { | ||
| case 8_000_000: | ||
| return PLLParams{M: 4, N: 168, P: 2, Q: 7} | ||
| case 12_000_000: | ||
| return PLLParams{M: 6, N: 168, P: 2, Q: 7} | ||
| case 16_000_000: | ||
| return PLLParams{M: 8, N: 168, P: 2, Q: 7} | ||
| default: | ||
| panic("unsupported xtal frequency") | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //go:build stm32f4 && stm32f469 | ||
|
|
||
| package machine | ||
|
|
||
| // PLLParams180MHz returns the HSE PLL dividers needed to reach a 360MHz VCO | ||
| // (180MHz SYSCLK, P=2, Q=7, R=6) for the configured crystal frequency. | ||
| func PLLParams180MHz() PLLParams { | ||
| switch xtalHz { | ||
| case 8_000_000: | ||
| return PLLParams{M: 4, N: 180, P: 2, Q: 7, R: 6} | ||
| case 12_000_000: | ||
| return PLLParams{M: 6, N: 180, P: 2, Q: 7, R: 6} | ||
| case 16_000_000: | ||
| return PLLParams{M: 8, N: 180, P: 2, Q: 7, R: 6} | ||
| default: | ||
| panic("unsupported xtal frequency") | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //go:build stm32 && stm32f7x2 | ||
|
|
||
| package machine | ||
|
|
||
| // PLLParams216MHz returns the HSE PLL dividers needed to reach a 432MHz VCO | ||
| // (216MHz SYSCLK, P=2, Q=9) for the configured crystal frequency. | ||
| func PLLParams216MHz() PLLParams { | ||
| switch xtalHz { | ||
| case 8_000_000: | ||
| return PLLParams{M: 4, N: 216, P: 2, Q: 9} | ||
| case 12_000_000: | ||
| return PLLParams{M: 6, N: 216, P: 2, Q: 9} | ||
| case 16_000_000: | ||
| return PLLParams{M: 8, N: 216, P: 2, Q: 9} | ||
| default: | ||
| panic("unsupported xtal frequency") | ||
| } | ||
| } |
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Is this intended to be referenced from outside the package? It seems to be used only internally for now, so I think unexported (pllParams) would be fine.
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.
The
machine.PLLParamsXXXMHz()functions are called from theruntime(e.g. from the src/runtime/runtime_stm32f405.go). So unfortunately this has to be exported unless we want to do massive refactoring and consolidate all clock initialization in eithermachineorruntimepackage.Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for the explanation, that makes sense.