Skip to content

stm32: add support for the Nations N32G45x - #189

Open
loss-and-quick wants to merge 4 commits into
Arksine:masterfrom
loss-and-quick:n32g45x
Open

loss-and-quick wants to merge 4 commits into
Arksine:masterfrom
loss-and-quick:n32g45x

Conversation

@loss-and-quick

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

Copy link
Copy Markdown

Adds support for the Nations N32G45x (N32G452 / N32G455), a Cortex-M4 part that is pin- and register-compatible with the stm32f103 but differs in its flash controller and clock tree. The clock setup is the same as what is now merged in Klipper (Klipper3d/klipper#7347, Klipper3d/klipper#7362).

  • Flash controller: pages are always 2KiB (not size-dependent like the stm32f103), and programming is only supported at 32-bit width, not half-word.
  • Clock configuration: a separate clock_setup_n32g45x(), dispatched alongside the untouched clock_setup(). 5-bit PLL multiplier, flash wait states that scale in 32MHz steps, both APB busses divided by the same amount so get_pclock_frequency() stays uniform, and the "ex mode" bit set so the adc and can2 blocks get a clock.
  • 144MHz on every standard crystal: the PLL only gets the crystal or half of it, so only exact multiples of crystal/2 are reachable. 144MHz, the chip's rated maximum, works from 8/12/16/24/32MHz crystals, and the usb prescaler divides it by 3 for 48MHz, so usb builds run at 144MHz too. 20MHz and 25MHz crystals fail at build time instead of running at the wrong speed. The internal oscillator stays at 64MHz.
  • Build support: MACH_STM32F1 variant reusing the stm32f103xe headers, matching Klipper, plus a 64KiB app-start offset matching Klipper's bootloader offsets for this chip.

The stm32f103 path is untouched; everything n32g45x-specific sits behind CONFIG_MACH_N32G45x.

Testing

  • Running on an n32g455 board with a 12MHz crystal at 144MHz.
  • Register values at 144MHz: 8MHz crystal CFGR = 0x08052D00 (0x08C52D00 with usb), 12MHz 0x00292D00, flash ACR = 0x14 (4 wait states).
  • test/configs/n32g45x.config (usb) and n32g45x-canbus.config cover both build paths.

@loss-and-quick

Copy link
Copy Markdown
Author

I now have an n32g455 board running, so this is tested on hardware instead of just built.

Bringing it up turned up one thing missing from the clock setup. The chip keeps its adc, sdio, qspi, opamp, comp and can2 behind an "ex mode" bit in PWR_CTRL3. Until that bit is set those peripherals have no clock at all and their RCC enable bits do not even latch - writing all ones to RCC_AHBPCLKEN reads back 0x00000057 here, and 0x0002fa57 once ex mode is on. The vendor SystemInit() in system_n32g45x.c sets it before configuring any clock, so I have folded that into the clock configuration commit rather than adding a separate one.

Katapult itself does not use those peripherals, but the bootloader should leave the chip in the same state the vendor code does, and can2 is behind the same bit.

The matching Klipper side is in Klipper3d/klipper#7362.

@loss-and-quick

Copy link
Copy Markdown
Author

Status update: this is tested on an n32g455 board now, not just built.

The Klipper side of the same work has been reviewed and largely merged upstream (Klipper3d/klipper#7347), so the clock setup here matches what is now in Klipper: a separate clock_setup_n32g45x() leaving the stm32f103 path untouched, both APB busses divided equally so get_pclock_frequency() stays uniform, flash wait states scaled with HCLK, and the usb prescaler programmed for a 48Mhz clock.

Let me know if you'd like anything reshaped before this is considered for merging.

@Arksine

Arksine commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

Thanks. Am I correct that all pending work has been merged in Klipper? If so, I'll merge this and its companion PR.

@Arksine

Arksine commented Oct 1, 2026

Copy link
Copy Markdown
Owner

FYI, I merged another pending PR that created a merge conflict in the README. You can resolve it if you prefer, or I can resolve it before merging.

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>
@loss-and-quick

Copy link
Copy Markdown
Author

Yes, the Klipper side is done - the clock setup (Klipper3d/klipper#7347) and the adc fixes (Klipper3d/klipper#7362) are both merged. During review Kevin simplified the clock defaults to running the n32g45x at 144Mhz on every standard crystal, with usb at 144Mhz/3, so I've updated this PR to match - the n32g45x clock code here is now the same as in Klipper. I've also rebased onto master and resolved the README conflict, and rebased #190 on top of it.

On #190: its Klipper counterpart, Klipper3d/klipper#7363, is still open because Kevin said he'd follow whatever Katapult decides. So if you merge #190, the Klipper one should follow on the Klipper side. The two need to be flashed together, since the bootloader and the application derive the uuid from the same bytes.

Comment thread src/stm32/Kconfig Resolved
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>

@akarkou1 akarkou1 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

looks good, confirmed to be functional on my end. well done

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.

3 participants