cancel
Showing results for 
Search instead for 
Did you mean: 

How to organize the folder structure in CubeMX-generated code

Vergnani_ElEn
Associate II

I created a new project using CubeMX, and after that I plan to implement a framework for my firmware. I would like to better understand the best way to organize a clean and maintainable folder structure.

A colleague told me that all folders should be created inside the Core directory. I would like to understand whether using Core to contain my framework is the best approach, or if it is acceptable to create additional source folders outside of it.

Additionally, where can I find examples of STM32 firmware projects generated using CubeMX?

2 REPLIES 2
Ghofrane GSOURI
ST Employee

Hello @Vergnani_ElEn 

A typical STM32CubeMX project looks  like:

Project/
├─ Core/
│  ├─ Inc/
│  ├─ Src/
│  ├─ Startup/
│  └─ ...
├─ Drivers/
│  ├─ CMSIS/
│  └─ STM32xxxx_HAL_Driver/
├─ Middlewares/   (if you enable USB, FATFS, FreeRTOS, etc.)
├─ .project / .cproject / .mxproject / Makefile / etc.
└─ ...

CubeMX guarantees that it will regenerate or update files under:
Core/Inc,Src
Drivers/STM32xxxx_HAL_Driver
Middlewares/...

You don’t need to keep all your code under Core; you can structure it like this:

Project/
├─ Core/                 # CubeMX-owned
│  ├─ Inc/
│  └─ Src/
├─ Drivers/              # ST/CMSIS
├─ Middlewares/          # Optional (FreeRTOS, USB, etc.)
├─ App/                  # Your application logic
│  ├─ Inc/
│  └─ Src/
└─ Framework/            # Your reusable firmware framework
   ├─ Inc/
   └─ Src/

 

Regarding the example : Each MCU family has a Cube package where you can find STM32CubeMX  examples  .

To install any firmware package In STM32CubeMX :Go to:
Help → Manage embedded software packages
Install the package for your MCU family (e.g. STM32F4, STM32H7)

After installation, the packages are typically stored in a path similar to:

C:\Users\<user>\STM32Cube\Repository\STM32Cube_FW_F4_Vx.x.x\

Inside this folder you will find example projects under Projects, for example:

Projects/STM32F4-Discovery/Examples/

Projects/NUCLEO-F401RE/Applications/

These contain ready-to-build projects that follow the same structure generated by STM32CubeMX.

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.


@Ghofrane GSOURI wrote:

You don’t need to keep all your code under Core.


Indeed.

@Vergnani_ElEn I would suggest that you keep all of your own code out of any folders maintained by CubeMX.

That way you can just leave CubeMX to do its own thing entirely in its own folder structure - so no risks of it affecting any of your own stuff, and no headaches if it decides to change its structure.

 

This also makes it easier to share code between projects.

 

See also: Project organisation in CubeIDE - how to go about libraries.

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.