Skip to main content
Associate
Nov 2, 2026 11:08 AM UTC
Question

Feature Request – Compile-Time Register-Level Code Generation for STM32CubeMX / CubeMX2

Dear STMicroelectronics Development Team,

I would like to suggest a feature that could significantly improve STM32CubeMX and STM32CubeMX2, particularly for embedded projects with limited Flash memory and strict performance requirements.

Current Situation

When configuring peripherals such as GPIO, timers, ADC, UART, SPI, etc., CubeMX typically generates initialization code using HAL or LL functions.

Although these libraries provide a convenient and structured programming interface, the generated initialization code can introduce considerable overhead.

In many cases, the hardware configuration is completely static and known at compile time. However, the generated code may still involve multiple function calls, intermediate structures, parameter assignments, and runtime operations just to write a few fixed values into hardware registers.

For example, configuring a GPIO port might ultimately require only a few register writes with constant values. Yet the generated initialization code can be considerably larger than the actual hardware configuration requires.

This becomes particularly problematic in memory-constrained applications, where even initialization code contributes to Flash consumption.

Proposed Feature: Direct Register-Level Code Generation

I suggest adding an optional code-generation backend that converts the selected CubeMX configuration directly into optimized register-level initialization code.

Instead of generating HAL/LL initialization functions, CubeMX could calculate all required register values during code generation and produce a minimal C implementation using constant values.

For example, instead of generating multiple HAL calls and configuration structures, the output could be equivalent to:

void GPIO_Init(void)
{
RCC->AHB2ENR |= 0x00000001UL;

GPIOA->MODER = 0x........UL;
GPIOA->OTYPER = 0x........UL;
GPIOA->OSPEEDR = 0x........UL;
GPIOA->PUPDR = 0x........UL;
GPIOA->AFR[0] = 0x........UL;
}

The actual values would be calculated by CubeMX based on the user's configuration.

With constant values and suitable compiler optimization, the resulting assembly could be reduced to a very small number of direct register-write instructions, without unnecessary runtime calculations or function calls.

Suggested Implementation

Rather than replacing HAL or LL, this could be offered as an additional code-generation option:

  1. HAL-based generation (existing functionality).

  2. LL-based generation (existing functionality).

  3. Direct Register-Level generation (new option).

The new option should:

  • Calculate register values during code generation, not at runtime.

  • Generate direct register writes using compile-time constants.

  • Avoid unnecessary initialization structures and function calls.

  • Generate only the registers and operations required by the selected configuration.

  • Respect register-specific behavior, including reserved bits, write-one-to-clear flags, and required initialization sequences.

  • Support all applicable STM32 families, not just newly introduced families.

Ideally, users should be able to select this generation method independently for each peripheral.

Expected Benefits

  • Significantly reduced initialization code size.

  • Lower Flash consumption.

  • Faster system startup.

  • Reduced runtime overhead.

  • No dependency on HAL or LL for the generated initialization code.

  • Better suitability for resource-constrained embedded applications.

  • Greater control over the final machine code.

An Additional Benefit

CubeMX already has all the necessary information about pin assignments, clock configuration, peripheral modes, and register settings.

Therefore, it should be possible to calculate the final register values internally without requiring the user to manually inspect generated code, debug initialization functions, or extract register values.

This would also reduce human errors associated with manually translating HAL configurations into register-level code.

I believe this feature would be valuable not only for advanced embedded developers but also for applications where code size, startup time, and execution efficiency are critical.

Thank you for considering this suggestion.

Best regards

10 replies

Ghofrane GSOURI
ST Technical Moderator
Nov 2, 2026 1:32 PM UTC

Hello ​@pf_55 

Thank you for your detailed suggestion and for highlighting the needs of memory-constrained applications.

STMicroelectronics has invested significantly in HAL and LL to provide supported, maintainable peripheral drivers across the STM32 portfolio. HAL prioritizes portability and ease of integration, while LL provides interfaces closer to the hardware for applications requiring finer control and lower overhead.

For the use case you describe, LL is the existing option most closely aligned with your objectives. Depending on the peripheral and device, LL offers inline register-access functions that, with constant arguments and compiler optimization, can produce compact code. 

Introducing a separate direct-register backend would require an additional code-generation path to be developed, validated, and maintained across supported devices. It would still need to respect device-specific register behavior and initialization sequences, while ensuring compatibility with the application’s drivers and middleware.

Given the investment in HAL and LL, improving the efficiency of existing LL-based generation would be a more practical direction to evaluate than introducing a third backend with its own ongoing validation and maintenance requirements.

THX

Ghofrane

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
TDK
Nov 2, 2026 2:41 PM UTC

ST has consistently denied this request. Back when we could vote on suggestions, this was the #1 highest voted suggestion.

The answer to “library with less overhead” has been LL and HAL2.

"If you feel a post has answered your question, please click ""Accept as Solution""."
waclawek.jan
Super User
Nov 2, 2026 3:51 PM UTC

In other words, if you want register access programming, simply avoid using Cube/CubeMX. There’s no support from ST for that, though, and they are stubborn in this for years.

May be fine as a graphical tool to assign pins, though (that was the original aim of the MicroXplorer, from which CubeMX evolved).

JW

gbm
Super User
Nov 3, 2026 7:34 AM UTC

Well, I did that few years ago - defined some useful data structures and macros for use in all my STM32 projects. The next step should be the pull-down list-driven generation of code using (maybe) a spreadsheet. The data structures I use may be found here:

https://github.com/gbm-ii/STM32_Inc/blob/main/bf_reg.h

And here is a sample of code using them:

// the whole port init
GPIOB->AFR[0] = (union bf4_) {
.p0 = 2, // TIM3CH3 INJ2
.p4 = 8, // I2C2 SDA
.p5 = 8, // I2C2 SCL
}.w;
GPIOB->AFR[1] = (union bf4_) {
.p12 = 5, // SPI2 NSS2A - PT2
.p13 = 5, // SPI2 SCK
.p14 = 5, // SPI2 MISO
.p15 = 5, // SPI2 MOSI
}.w;
GPIOB->OSPEEDR = (union bf2_) {
.p4 = GPIO_OSPEEDR_MED, // I2C2_SDA
.p5 = GPIO_OSPEEDR_MED, // I2C2_SCL
.p12 = GPIO_OSPEEDR_HI,
.p13 = GPIO_OSPEEDR_HI,
.p14 = GPIO_OSPEEDR_HI,
.p15 = GPIO_OSPEEDR_HI,
}.w;

GPIOB->MODER = (union bf2_) {
.p0 = GPIO_MODER_AF, // INJ2
.p1 = GPIO_MODER_AN, // nc
.p2 = GPIO_MODER_AN, // nc
.p3 = GPIO_MODER_AN, // nc
.p4 = GPIO_MODER_AF, // SDA
.p5 = GPIO_MODER_AF, // SCL
.p6 = GPIO_MODER_AN, // nc
.p7 = GPIO_MODER_AN, // nc
.p8 = GPIO_MODER_AN, // button
.p9 = GPIO_MODER_AN, // no pin
.p10 = GPIO_MODER_AN, // nc
.p11 = GPIO_MODER_AN, // no pin
.p12 = GPIO_MODER_OUT , // NSS2A
.p13 = GPIO_MODER_AF, // SCK2
.p14 = GPIO_MODER_AF, // MISO
.p15 = GPIO_MODER_AF, // MOSI
}.w;

// selected port lines init
RCC->IOENR |= RCC_IOENR_GPIOEN(GPIOA);
AFRF(GPIOA, 11) = AFN_USB;
AFRF(GPIOA, 12) = AFN_USB;
BF2F(GPIOA->OSPEEDR, 11) = GPIO_OSPEEDR_HI;
BF2F(GPIOA->OSPEEDR, 12) = GPIO_OSPEEDR_HI;
BF2F(GPIOA->MODER, 11) = GPIO_MODER_AF;
BF2F(GPIOA->MODER, 12) = GPIO_MODER_AF;

 

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Pavel A.
Nov 3, 2026 11:08 PM UTC

>  The next step should be the pull-down list-driven generation of code using (maybe) a spreadsheet. 

These days: an AI agent with appropriate skill script(s).

 

pf_55Author
Associate
Nov 24, 2026 2:29 PM UTC

Thanks for sharing your approach. Your implementation is quite interesting and demonstrates how practical and efficient register-level programming can be.

Your use of unions, bit-fields, and macros provides a convenient way to generate readable register-level code without relying on HAL function calls.

However, my proposal goes one step further.

Rather than requiring developers to manually define register configurations, structures, macros, or enter configuration data into a separate spreadsheet, I would like CubeMX itself to perform this task automatically.

CubeMX already knows the selected MCU, peripheral configurations, GPIO assignments, clock settings, and all related dependencies. Therefore, it has most of the information needed to calculate the final register values and generate the corresponding direct register assignments.

The important point is that users would continue to use the existing CubeMX graphical wizard exactly as they do today. The only difference would be an additional code generation option.

This could offer a significant advantage:

Developers would benefit from the convenience, speed, and reliability of the CubeMX wizard while obtaining highly optimized, lightweight firmware with minimal runtime overhead and reduced Flash consumption.

I also believe this could be a valuable competitive advantage for ST. Many developers currently choose between the convenience of graphical configuration tools and the efficiency of direct register-level programming. By offering both approaches within CubeMX, ST could effectively eliminate that compromise.

This would make CubeMX attractive not only to developers who prefer HAL and LL, but also to experienced developers working with resource-constrained applications, where code size, execution efficiency, and complete control over hardware are particularly important.

In my opinion, this is not simply a code generation improvement. It is an opportunity for ST to differentiate CubeMX from competing development ecosystems, improve the overall developer experience, and potentially attract more users to STM32 platforms.

Your implementation demonstrates that the register-level approach is practical. What I would like to see is this capability integrated directly into CubeMX, making it automatic, accessible, and consistent across supported STM32 families.

Thanks again for sharing your work.

pf_55Author
Associate
Nov 24, 2026 2:19 PM UTC

Thank you for your response and for considering this feature request.

I would like to clarify the main idea behind this proposal, as I believe there may be a misunderstanding regarding its intended purpose.

This request is not about replacing HAL, LL, or the CubeMX configuration wizard. It is about extending the existing CubeMX code generation capabilities with an additional output option.

The core idea is relatively simple:

During peripheral configuration, CubeMX already has all the necessary information to calculate the final values required for the peripheral registers. Instead of generating a sequence of HAL/LL function calls, structures, macros, and related operations that may need to be processed at runtime, CubeMX could perform these calculations in advance and generate the final register assignments directly.

For example, instead of generating multiple function calls to configure a peripheral, the generated code could simply contain the corresponding 16-bit or 32-bit register assignments with precomputed values.

The user could then choose between two code generation approaches:

  1. Function-Based Generation: The existing HAL/LL approach, preserving the current level of abstraction and flexibility.

  2. Precomputed Register-Based Generation: Direct register assignments using values calculated by CubeMX during code generation, eliminating unnecessary runtime configuration operations.

An important point is that this proposal is intended to complement the CubeMX wizard, not replace it.

In fact, the configuration wizard remains an essential part of this approach. Users would continue to benefit from its graphical interface, automatic peripheral configuration, clock calculations, pin assignments, dependency checking, and validation features. They would simply have an additional choice regarding how the final configuration code is generated.

This would provide the best of both worlds:

  • Development efficiency: Users can continue using the CubeMX wizard to configure peripherals quickly and reliably, without manually calculating register values or consulting reference manuals for every configuration.

  • Runtime efficiency and Flash optimization: The generated firmware can avoid unnecessary initialization function calls, configuration structures, and runtime calculations, potentially reducing code size and Flash consumption.

  • User choice: Developers can select the abstraction level that best suits their application, hardware constraints, and optimization requirements.

I also want to emphasize that this does not require introducing and maintaining an entirely new driver library. The existing CubeMX configuration engine already performs much of the necessary analysis and parameter calculation. The proposed feature would primarily involve adding another code generation backend that translates the resulting configuration into direct register-level operations.

Of course, operations that genuinely depend on runtime conditions would remain runtime operations. The proposal specifically targets configuration operations whose final results are already known at design time.

I believe this feature would be particularly valuable for resource-constrained microcontrollers and applications where Flash usage, startup time, and execution efficiency are important considerations.

The goal is to let developers benefit from the convenience and intelligence of CubeMX during development, while retaining the efficiency and control of direct register-level programming in the generated firmware.

Thank you for considering this clarification. I hope this helps communicate the intended scope and practical benefits of the proposal.

Andrew Neil
Super User
Nov 24, 2026 2:34 PM UTC

During peripheral configuration, CubeMX already has all the necessary information to calculate the final values required for the peripheral registers. .

I’m not sure that’s actually true?

I think that CubeMX is targetted at the HAL/LL abstraction - not directly at the raw registers?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
waclawek.jan
Super User
Nov 24, 2026 6:23 PM UTC

> Thank you for your response and for considering this feature request.

Note, that ST does *not* consider your feature request.

> The core idea is relatively simple:

But the implementation is complex and extensive thus expensive.

Don’t hold your breath.

JW

Uwe Bonnes
Chief
Nov 24, 2026 6:50 PM UTC

From the mcu/db or stm32XXxx-dfp files   I generate with python  header  files like  for stm32f031:

#define EVENTOUT_FUNC            0
#define HDMI_CEC_CEC_FUNC        1
#define I2C1_SCL_FUNC            2
#define I2C1_SDA_FUNC            3
#define I2C1_SMBA_FUNC           4
...
# define PINMUX(gpio, func) ((gpio == PIN_NONE) ? AF_PIN_NONE :         \
        ((gpio == PA00) && (func == RTC_TAMP2_FUNC           )) ?  0 : \
        ((gpio == PA00) && (func == SYS_WKUP1_FUNC           )) ?  0 : \
        ((gpio == PA00) && (func == USART1_CTS_FUNC          )) ?  1 : \
        ((gpio == PA00) && (func == USART2_CTS_FUNC          )) ?  1 : \
        ((gpio == PA00) && (func == TIM2_CH1_FUNC            )) ?  2 : \
        ((gpio == PA00) && (func == TIM2_ETR_FUNC            )) ?  2 : \
        ((gpio == PA00) && (func == TSC_G1_IO1_FUNC          )) ?  3 : \
…

        ((gpio == PB15) && (timer_nr == 15) && (channel_nr == -1)) ?  3 : \
        AF_NO_SUCH_PINFUNC)
 

In code I use it like:

#include PINMUXHEADER
#define USB_DM_AF  PINMUX(PA11, USB_DM_FUNC)
#if USB_DM_AF == AF_NO_SUCH_PINFUNC
# define USB_DM_AF  PINMUX(PA11, USB_OTG_FS_DM_FUNC)
# if USB_DM_AF != AF_NO_SUCH_PINFUNC
#  define USB_DP_AF  PINMUX(PA11, USB_OTG_FS_DP_FUNC)
#  define OTGFS_ID_AF  PINMUX(PA10, USB_OTG_FS_ID_FUNC)
# endif
#else
# define USB_DP_AF  PINMUX(PA11, USB_DP_FUNC)
#endif