Skip to content

stm32: read the n32g45x unique id from its documented address - #7348

Closed
loss-and-quick wants to merge 5 commits into
Klipper3d:masterfrom
loss-and-quick:n32g45x-uid-fix
Closed

loss-and-quick wants to merge 5 commits into
Klipper3d:masterfrom
loss-and-quick:n32g45x-uid-fix

Conversation

@loss-and-quick

@loss-and-quick loss-and-quick commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #7347 — the diff below includes that PR's commits until it lands;

What this does

The n32g45x reuses the stm32f103xe headers and therefore inherits UID_BASE = 0x1FFFF7E8. Per the N32G45x system-config-block map, that address only covers the last 4 of the 12 bytes chipid.c reads as the "unique ID" — the other 8 bytes come from an undocumented region of the system block, not the documented 96-bit UID (which starts at 0x1FFFF7F0). Override UID_BASE for CONFIG_MACH_N32G45x so the full 96-bit UID is used.

Why this is a draft

  • I can't confirm the 8 undocumented bytes are safe to drop. If they're chip-unique (die coordinates, lot number), the current code is fine as-is and this patch is unnecessary. If they're a fixed constant, CAN UUID uniqueness today rests entirely on the last 4 bytes
  • This is a breaking change for any board already running Klipper on n32g45x. The CAN UUID and USB serial number both change. canbus_uuid in printer.cfg will need updating, and any /dev/serial/by-id/... udev rule keyed on the old serial breaks too.

Testing

Compiled a standalone probe with the real build flags: UID_BASE resolves to 0x1FFFF7F0 under MACH_N32G452, and is unchanged at 0x1FFFF7E8 under MACH_STM32F103. No other code references UID_BASE, so the diff is confined to this one macro.

The n32g45x reused clock_setup(), which is invalid at the 128MHz it
runs at: PPRE1_DIV2 leaves PCLK1 at 64MHz against a 36MHz limit, and
the flash latency of two wait states is one short of what 128MHz
needs.

Give the n32g45x its own clock_setup_n32g45x(), keeping it isolated
from the shared stm32f103 code path, so clock_setup() and the rest of
the core stm32 code are unchanged. Divide both APB busses by the same
amount, chosen from PCLK1's tighter limit, so every peripheral clock
stays identical -- some stm32 code (e.g. hard_pwm.c) assumes
get_pclock_frequency() returns one value for all peripherals. Scale
flash wait states with HCLK, and widen the PLL multiplier encoding to
five bits (PLLMUL[4] at bit 27) so multipliers above 16 can be
reached.

Signed-off-by: Lev Voronov <minicx@disroot.org>
USB was enabled for the n32g45x when it was introduced, but the USB
prescaler bits were never programmed, leaving the reset divisor of
PLLCLK/1.5 -- 85.33MHz from the default 128MHz clock, not the 48MHz
the peripheral needs. Program the prescaler in clock_setup_n32g45x(),
and lower the clock to 96MHz when USB is selected, since 128MHz can't
produce 48MHz through any of this chip's divisors (1.5/1/2/3). CAN
and uart builds keep 128MHz. An unsupported clock still fails the
build with #error instead of producing a device that won't enumerate.

The internal oscillator default is left at 64MHz. USB requires a
crystal, so that path cannot be affected by this change, and raising
it would be a hardware change that is not needed to fix this bug.

Signed-off-by: Lev Voronov <minicx@disroot.org>
@loss-and-quick
loss-and-quick force-pushed the n32g45x-uid-fix branch 4 times, most recently from efd7e67 to 5182969 Compare August 29, 2026 09:14
The n32g45x PLL multiplies the crystal (or its half) by an integer, so
a system clock rate is only reachable when it is an exact multiple of
crystal / 2.  The flat 128Mhz default silently produced 126Mhz from a
12Mhz crystal, 120Mhz from 20Mhz and 24Mhz crystals, and 125Mhz from a
25Mhz crystal - enough of an error to break uart baud rates without any
build time indication.

Pick per crystal defaults that the PLL can generate exactly: 128Mhz
where it was already exact (8Mhz, 16Mhz), otherwise the highest exact
rate the crystal supports (144Mhz for 12Mhz and 24Mhz, 140Mhz for
20Mhz, 125Mhz for 25Mhz).  Fail the build if the requested rate is not
an exact multiple of crystal / 2.  Also stop offering usb on crystals
that cannot produce its 48Mhz clock: no exact usb rate exists from a
20Mhz or 25Mhz crystal.

Signed-off-by: Lev Voronov <minicx@disroot.org>
The n32g45x had no build coverage. Add a USB and a CAN config so CI
compiles both the 96MHz and 128MHz clock paths.

Signed-off-by: Lev Voronov <minicx@disroot.org>
The n32g45x builds against the stm32f103 headers and so inherits
UID_BASE = 0x1FFFF7E8.  On this chip the 96bit unique device id is
documented to start at 0x1FFFF7F0, so only the first four of the twelve
bytes that are read are actually part of the unique id.  The remaining
eight bytes come from an undocumented part of the system configuration
block and may well be identical on every device.

Override UID_BASE for the n32g45x so that the full unique id is used.

Note that this changes the CAN uuid and the usb serial number reported
by existing n32g45x boards.  Katapult needs the matching change, and
both must be flashed together, otherwise the uuid reported by the
bootloader will no longer match the one reported by the application.

Signed-off-by: Lev Voronov <minicx@disroot.org>
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.

1 participant