Skip to content

feat(machine/stm32): make HSE crystal frequency selectable.#5517

Merged
deadprogram merged 1 commit into
tinygo-org:devfrom
helvionics:de_stm32_selectable_xtal
Jul 19, 2026
Merged

feat(machine/stm32): make HSE crystal frequency selectable.#5517
deadprogram merged 1 commit into
tinygo-org:devfrom
helvionics:de_stm32_selectable_xtal

Conversation

@digitalentity

@digitalentity digitalentity commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Define the board's external crystal (HSE) frequency xtalHz in the respective board_*.go files. Per-topology PLL tables (F1, F4, F7) will compute the register dividers from it.

Moving this parameter to the board definition level cleanly isolates board hardware characteristics from general MCU chip configurations, eliminating the need for custom target build tags. Targets using HSE-clocked STM32 chips must define xtalHz or fail to build.

Fixes #5516

Comment thread src/runtime/runtime_stm32f103.go Outdated
@deadprogram

Copy link
Copy Markdown
Member

Thank you for working on this @digitalentity

A few comments:

F407 PLL parameters we actually changed. They are probably more correct now, but might want to document this.

The STM32WLx boards (nucleowl55jc, lorae5, gnse) also use HSE (HSE32/32 MHz TCXO) but are not covered by this PR. It that intentional?

The PR changes CPUFrequency() from a constant return to a computed value. There are no tests verifying this, might be good to add some.

@digitalentity

digitalentity commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

F407 PLL parameters we actually changed. They are probably more correct now, but might want to document this.

True. I added code comments to explain this.

Re/ The WLx boards - the HSE32 there is a fixed on-package 32MHz TCXO (per RM0461), not a discrete external crystal
choice like F1/F4/F7 boards. No xtalHz-style selection is possible, hence not touching thouse targets.

The PR changes CPUFrequency() from a constant return to a computed value. There are no tests verifying this, might be good to add some.

I'm not sure that's feasible. We don't have tests for any machine-specific code and CPUFrequency() is not generic enough. Being able to test machine-specific code is a separate problem we should tackle...

Define the board's external crystal (HSE) frequency `xtalHz` in the
respective `board_*.go` files. Per-topology PLL tables (F1, F4, F7)
will compute the register dividers from it.

Moving this parameter to the board definition level cleanly isolates
board hardware characteristics from general MCU chip configurations,
eliminating the need for custom target build tags. Targets using
HSE-clocked STM32 chips must define `xtalHz` or fail to build.
@digitalentity
digitalentity force-pushed the de_stm32_selectable_xtal branch from b47d595 to 410b640 Compare July 17, 2026 09:56
// 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 {

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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 the runtime (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 either machine or runtime package.

@rdon-key rdon-key Jul 17, 2026

Copy link
Copy Markdown
Contributor

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.

@deadprogram deadprogram left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this @digitalentity now merging.

@deadprogram
deadprogram merged commit 5f02d6b into tinygo-org:dev Jul 19, 2026
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

STM32: Support board-specific clock configuration

3 participants