Skip to content

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

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

loss-and-quick wants to merge 5 commits into
Arksine: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

Depends on #189 — 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, matching the identical fix proposed for Klipper.

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 Katapult 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.

@loss-and-quick

Copy link
Copy Markdown
Author

Taking this out of draft now that I have an n32g455 board to test it on, and now that the Klipper side exists.

The two have to land together: this changes the uuid the bootloader reports, and the application derives its own from the same bytes, so a mismatched pair would report two different uuids for one board. The Klipper side is Klipper3d/klipper#7363.

Rebased on the current #189 branch, which now also enables ex mode.

@loss-and-quick

Copy link
Copy Markdown
Author

A small status note, since this one now gates a change on the Klipper side.

The matching Klipper PR is Klipper3d/klipper#7363, and Kevin's position there is that he does not have a strong preference either way and will follow whatever Katapult decides:

For what it is worth, I do not have a strong preference one way or another on this change. So, if Katapult wishes to make the change then I'll follow suit here.

So the decision on whether to read the n32g45x unique id from its documented address rests here. Both sides are written and tested on an n32g455 board, and they have to land together — the bootloader and the application derive their uuid from the same bytes, so a mismatched pair would report two different uuids for one board.

Happy to adjust the change if you'd prefer it in a different form, or to drop it if you'd rather keep the current offset for compatibility with already-flashed boards.

The N32G45x flash controller differs from the stm32f103: pages are
always 2KiB, and programming only works at 32-bit width (not
half-word). Add the corresponding page size and programming
implementations; page erase is unchanged.

Signed-off-by: Lev Voronov <minicx@disroot.org>
The N32G45x reuses the stm32f1 RCC layout but its clock tree differs:
a 5-bit PLL multiplier, separate PCLK1/PCLK2 limits, flash wait states
that scale in 32MHz steps, no ADC prescaler bits, and a 2-bit USB
prescaler with non-integer divisors.  It also keeps part of its
peripheral set - the adc, sdio, qspi, opamp, comp and can2 - behind an
"ex mode" bit in PWR_CTRL3.  While that bit is clear those peripherals
stay unclocked and their RCC enable bits read back as zero, so the
vendor SystemInit() sets it before configuring any clock.

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.  Enable ex mode the way the vendor
code does, then 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.

Signed-off-by: Lev Voronov <minicx@disroot.org>
Add build support for the Nations N32G452 and N32G455, which are pin
and register compatible with the stm32f103 but built around a
Cortex-M4 core with 128KiB flash / 64KiB ram. They select
MACH_STM32F1 and reuse the stm32f103xe headers, matching Klipper.

Default system clock is 128MHz (96MHz with USB, since the prescaler
can't produce 48MHz from 128MHz). The internal oscillator default of
64MHz matches the value Klipper has used for this chip since it was
introduced. An additional 64KiB app-start offset is provided to match
Klipper's bootloader offsets for this chip.

Signed-off-by: Lev Voronov <minicx@disroot.org>
The n32g45x defaulted to 128Mhz, which is not reachable from a 12Mhz or
24Mhz crystal: the PLL takes the crystal or half of it, so only exact
multiples of crystal / 2 can be generated, and a 12Mhz crystal silently
ran the chip at 126Mhz instead.

Run it at 144Mhz instead, the chip's rated maximum, which is an exact
multiple of half of each of the 8, 12, 16, 24 and 32Mhz crystals.  That
includes usb builds: at 144Mhz the usb prescaler divides by 3 to give
the 48Mhz the peripheral needs, so the separate 96Mhz usb rate is no
longer required.  Add a build time check so that a crystal which cannot
produce the rate, such as 20Mhz or 25Mhz, fails to build instead of
running at the wrong speed.  This matches the clock setup now in
Klipper.

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.  Klipper 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