Skip to content

NUMICRO NUC126: fixes for the SysTick priority, PLL setup and PAL modes - #460

Open
mdmower wants to merge 3 commits into
ChibiOS:chibios-21.11.xfrom
mdmower:nuc126-platform-fixes
Open

mdmower wants to merge 3 commits into
ChibiOS:chibios-21.11.xfrom
mdmower:nuc126-platform-fixes

Conversation

@mdmower

@mdmower mdmower commented Oct 24, 2026

Copy link
Copy Markdown

A follow-on to @doombadroid's #454: three fixes to the NUC126 platform code, found while reviewing the port that runs QMK on a Ducky One 3 RGB keyboard (NUC1261SG4AE; qmk/qmk_firmware#26499). References: NUC126 TRM Rev 1.04 and Nuvoton's NUC126 BSP.

One commit per fix:

  1. Valid default SysTick priority and IRQ priority checks. The default NUC126_ST_IRQ_PRIORITY of 8 is out of range on the Cortex-M0 (0–3) and becomes priority 0. The default is now 2, and an out-of-range ST or USB priority stops the build.
  2. Fix the PLL setup in CLK_SetCoreClock. CLK_PLLCTL got its arguments in the wrong order, which set PD and OE, so HCLK stayed on HIRC. Divider 1 would then give 144 MHz, and CLK_PLLCTL_72MHz_HIRC selected HXT. This is the HAL's default configuration.
    • Basis: TRM 6.3 (PLLCTL); BSP clk.c, clk.h.
    • Test (PLL from HIRC): HCLK measured 22.21 MHz before, 72.10 MHz after. HXT untested.
  3. Fix the PAL mode mapping and the HXT clock frequency.
    • Unsupported modes became quasi mode: INPUT_PULLDOWN got a pull-up. Each mode is now mapped; pull-down and unknown modes are ignored, as the PAL contract says.
    • DOUT is set before quasi mode, so the pin isn't driven low (TRM 6.9.5.4).
    • PAL_MODE_NUC126_ALTERNATE_* removed: unused, and equal to PAL_MODE_RESET…PAL_MODE_INPUT_PULLUP.
    • PAL_WHOLE_PORT was 0x11 (^ is XOR); now 0xFFFF.
    • SystemCoreClockUpdate uses NUC126_HSECLK, not the fixed 12 MHz __HXT.
    • Test: typing with several keys held (QMK switches matrix rows between output and pull-up). HXT change only built.

Both NUC126 testhal projects build without warnings. NUC123 (TIMv1, GPIOv1) has the same code; I have no NUC123 hardware, so it's unchanged.

I made the review and these patches with Claude Code, an AI coding agent. The tests ran on my keyboard.

NUC126_ST_IRQ_PRIORITY defaulted to 8, out of range on the two priority
bits of the Cortex-M0, and nothing checked it. Default it to 2 and stop
the build when the ST or USB IRQ priority is out of range.
CLK_EnablePLL passed NO, NR and NF to CLK_PLLCTL in the wrong order, so
PLLCTL got PD and OE set, the PLL never became stable and HCLK stayed on
HIRC. CLK_SetCoreClock also switched HCLK to the PLL with divider 1, which
with a running PLL gives twice the requested frequency, and
CLK_PLLCTL_72MHz_HIRC selected HXT. Pass the arguments in the macro's
order, use divider 2 as the BSP does, and select HIRC.
_pal_lld_setgroupmode set every mode other than input, push-pull and
open drain to quasi-bidirectional, so PAL_MODE_INPUT_PULLDOWN and
PAL_MODE_INPUT_ANALOG got a pull-up, and it wrote the mode before DOUT,
so the pin was driven low while DOUT was 0. Map each mode explicitly:
analog input uses input mode, DOUT is set before quasi mode, and
pull-down, which the chip lacks, is ignored. Remove the
PAL_MODE_NUC126_ALTERNATE_* values, which equal PAL_MODE_RESET to
PAL_MODE_INPUT_PULLUP, and make PAL_WHOLE_PORT 0xFFFF instead of
(2^16)-1. SystemCoreClockUpdate now takes the HXT frequency from
NUC126_HSECLK when HSE is enabled instead of the fixed 12 MHz __HXT.
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