mbed-os Upstream Contribution

STM32G4 clock sources · merged upstream

Overview

This one's small: a merged contribution to mbed-os, the open-source RTOS, adding support for more high-speed external (HSE) oscillator frequencies on the STM32G431xB. It's the only project on this site where someone else, in this case maintainers at Arm and ST, reviewed my code against their standards and hit merge: PR #15385, merged into mbed-os master in March 2023.

The problem

mbed-os's clock configuration for the STM32G431xB assumed you had the 24 MHz HSE crystal found on ST's own Nucleo dev boards, and rejected anything else. Sunswift's custom boards used the same microcontroller with a different crystal, which upstream mbed-os simply refused to configure. So the choice was: carry a local patch forever and hope future team members remember why it exists, or fix it in the RTOS itself.

The change

The patch extends the clock initialization to accept HSE values of 4, 8 and 16 MHz alongside the original 24 MHz, adjusting the PLL multiplier and divider values (PLLM/PLLN) for each input so that every supported crystal still lands on the 160 MHz system clock the rest of the configuration expects.

The review process

My first version wasn't quite right. An ST maintainer pointed out that the error handling was misleading. I was rejecting valid crystals outright when what was actually needed was a PLL configuration to match them. So I reworked the patch into proper HSE range validation covering the full set of frequencies instead of a blunt error. From opened to merged took under two weeks, with the full CI suite passing along the way.

The full back-and-forth is right there on the PR.

View the merged PR ← Back to all projects