Skip to content

[FEATURE] reorganization of STM32 ports #18894

Closed

Description

@raiden00pl

Is your feature request related to a problem? Please describe.

STM32 ports are a complete mess. Some have their own directory, some don't. Adding new ports means duplicating a whole bunch of code or adding a new family to already existing ports, which is also not elegant.

There is also a problem with naming (kconfig options, port directory, board directory): #16172

Maintaining such code is becoming increasingly difficult and we already have differences in the features supported by STM32 families. We need a better solution that will work better in the long term.

Here is a previous discussion on this topic: https://groups.google.com/g/nuttx/c/G3-EWjjVIT8
Since then, no one has proposed any solution, the number of STM32 maintainers in the project has decreased and the situation with code duplication and chaos in the code has become even worse.

Describe the solution you'd like

I think we should do it like risc-v ports from Espressif. Introduce common directory for STM32, move all duplicated code there and gradually clean up duplicates.

https://github.com/apache/nuttx/tree/master/arch/risc-v/src/common/espressif
https://github.com/apache/nuttx/tree/master/arch/risc-v/src/esp32c3
https://github.com/apache/nuttx/tree/master/arch/risc-v/src/esp32c6
https://github.com/apache/nuttx/tree/master/arch/risc-v/src/esp32h2

This is a really nice solution. Ultimately, the family-specific directory should contain only the family-specific code. No code shared between families in the family-specific directory.

This requires a breaking change on the Kconfig options side. All Kconfig options must be generalized:

CONFIG_STM32L4_ -> CONFIG_STM32_
CONFIG_STM32H7_ -> CONFIG_STM32_
CONFIG_STM32F7_ -> CONFIG_STM32_

and so one. Common Kconfig with common options must be placed in arch/arm/src/common/stm32/Kconfig to avoid Kconfig complaining.

This is the only breaking change that is necessary, and it's the first necessary step to reorganize the STM32. The breaking change is rather easy to fix on the user side, as it requires simply replacing CONFIG_STM32XX_ with CONFIG_STM32_. This change should be implemented in release 13.0 if we want to reorganize STM32 ports as this is significant change. Further work can be done later, gradually, as it's a lot of work.

The longer we wait with this reorganization, the worse it will get. With each new STM32 family, the problem will only get worse.

Work plan:

  1. generalize hardware definitions for STM32 ports. Use the same prefix for all STM32 chips CONFIG_STM32_. Breaking change.
  2. generalize Kconfig options for STM32. Use the same prefix for all STM32 chips CONFIG_STM32_. Breaking change.
  3. New page in Docoumentation with descriptions of all supported and unsupported STM32 families. Listing of all IP cores used by the families. Chibios may be helpful here: https://github.com/ChibiOS/ChibiOS/tree/master/os/hal/ports/STM32/LLD
  4. move common source code to arch/arm/src/common/stm32/. Keep all reusable sources in one place. No code will be removed yet, there are still a lot of duplicates, but reusing files for new architectures will be easier.
  5. split arch/arm/src/common/stm32/ into arch/arm/src/common/stm32f1/, arch/arm/src/common/stm32f2/, arch/arm/src/common/stm32f3/, arch/arm/src/common/stm32f4/, arch/arm/src/common/stm32g4/, arch/arm/src/common/stm32l1/. The same for boards.
  6. split arch/arm/src/common/stm32f0l0g0/ into arch/arm/src/common/stm32f0/, arch/arm/src/common/stm32g0/, arch/arm/src/common/stm32l0/, arch/arm/src/common/stm32c0/. The same for boards.
  7. regenerate CODEOWNERS.
  8. unify common sources, remove duplicates and so one. The longest and most difficult step, but it can be done gradually.

The remaining problem is how to handle drivers that are shared between arm and arm64 cores (as in the case of stm32mp). One approach is to create arch/common/src/stm32 shared by different archs, another is to just duplicate the code in arm64.

Describe alternatives you've considered

No response

Verification

  • I have verified before submitting the report.

Activity

  1. added
    Type: Enhancement
    on May 18, 2026
  2. linguini1 commented on May 25, 2026

    @linguini1
    Contributor

    Great initiative!

  3. raiden00pl commented on May 28, 2026

    @raiden00pl
    MemberAuthor

    I have already completed tasks from 1 to 5, but for now moved to common/stm32 only files from stm32 m3/m4 and stm32 m0.
    It looks pretty good. Unfortunately, the differences between the families are so great that this task is HUGE and DIFFICULT. Impossible to complete without AI and burning a huge amount of tokens.
    The maintainers for years didn't pay much attention to keeping the STM32 ports similar and merged the PRs without much understanding or thought about the future of code maintenance. I'm therefore even more convinced that the proposed change, even if it can piss off many people because it is breaking change, is the only reasonable approach to taming the stm32 sources

  4. raiden00pl commented on May 29, 2026

    @raiden00pl
    MemberAuthor

    here is my work in progress branch raiden00pl#50 The change is huge, but there is no other way to do it.

    Github is poor for viewing such large changes, it's better to download the entire branch locally

  5. acassis commented on Jul 2, 2026

    @acassis
    Contributor

    @raiden00pl I think we can close this Issue, right?

  6. raiden00pl commented on Jul 3, 2026

    @raiden00pl
    MemberAuthor

    @acassis step 8 of the plan is still not finished, but since it's a long term task that require substantial work I'll create a separate issue for it. I'm closing this issue now.

  7. linguini1 commented on Jul 3, 2026

    @linguini1
    Contributor

    Great work @raiden00pl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: Enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions