-
Notifications
You must be signed in to change notification settings - Fork 991
Closed
Labels
Description
Problem
Currently, the pattern for STM32 MCUs is to do the oscillator and PLL configuration in the runtime package. This is causing a few issues:
- Declaration of magic values separate from the code that initializes the system frequency, e.g.: https://github.com/tinygo-org/tinygo/blob/dev/src/machine/machine_stm32l0.go#L13
- Lack of access to tick counter in machine package, leading to
- Arbitrary loop counts used for delays, e.g.: https://github.com/tinygo-org/tinygo/blob/dev/src/machine/machine_stm32_i2c_reva.go#L53
- Linker hacks to access tick counter anyway, e.g.: https://github.com/tinygo-org/tinygo/blob/dev/src/machine/machine_stm32_i2c_revb.go#L10
Proposed Changes
- Move the
initCLK(and where presentinitOSC) functions to machine package - Move
runtime_stm32_timers.goto machine package - Make
initCLKinitialize: Oscillators, PLL, Timers
The runtime package would be responsible for calling initCLK (to preserve initialization order) as well as UART initialization / putchar
Bigger Picture
This would be the first of multiple steps to improve the clock frequency code:
- Move OSC, PLL code into machine package
- Specify external oscillator frequencies in board-specific files (HSE/LSE)
- Remove magic values, use calculated clock frequencies based on actual clock speed
- Remove arbitrary retry/tick counts in i2c/other driver logic, use calculated delays (more robust across chips with different clock frequencies)
- Harmonize the PLL & OSC initialization code (there's a few basic patterns)