Skip to main content
AAnth.1
Senior
April 23, 2020
Question

STM32 minimalistic project template WITHOUT STM32CubeIDE C Code generation. How to include ST HAL and/or LL drivers into an empty project?

  • April 23, 2020
  • 13 replies
  • 13481 views

Hi there

I moved from TI DSPs using Code Composer Studio to a STM32F4 MCU using STM32CubeIDE and I already struggle with the first steps. While I fancy the automatic Code generation that the STM32CubeIDE configurator offers, I would like to start my first projects without it in order to get a better understanding of the MCU and the CubeIDE.

I am coming straight to the point:

  1. How do I best create an empty project such that I afterwards include the pre-defined functions (peripherals such as ADC, Timers etc.)?
    1. My current approach has been: File->New->STM32 Project->Target Project Type: Empty (shown in figure CubeIDE)
    2. This seems to do the job (shown in CubeIDE1). But that brings me directly to question nr 2:
  2. Once I have this empty project, I would like to include the necessary pre-defined functions, header files etc. Where and how in the CubeIDE (project settings) do I do the proper linking and including (see CubeIDE2)?
  3. I have downloaded the STM32CubeF4 driver package and read the getting started manual. So now the question is how to include the necessary driver files into my project via STMCubeIDE.
  4. Is there any document that describes this procedure and the required header files/driver files etc?

I hope I am not asking for too much help here.

Thank you,

This topic has been closed for replies.

13 replies

Nikita91
Lead II
April 23, 2020

For the LL/HAL files its easy:

in the STM32CubeF4 driver package locate the folder "\STM32Cube_FW_F4_V1.25.0\Drivers" an copy it in your project folder: il will appear in the IDE (or copy only inc and src folders).

Remenber to set the path to this inc folder in your project properties.

You will also need the CMSIS file for your STM32 device, in : STM32Cube_FW_F4_V1.25.0\Drivers\CMSIS\Device\ST\STM32F4xx\Include

and also : \STM32Cube_FW_F4_V1.25.0\Drivers\CMSIS\Include

Etc. Search into this Drivers folder, you will find everything you need.

KnarfB
Super User
April 23, 2020

I would not recommend starting from an empty project if you intend using HAL and/or LL drivers and STM32CubeIDE anyway. There are many tool settings like include paths, prepocessor defines etc.. which you have to copy. And, there is the linker description (.ld) file and probably the startup file that you want to reuse.

A minimalistic generated project does not contain much more than that plus some init functions in main().

You can delete everything you dont want and never re-generate the code again. Add missing HAL/CMSIS drivers as described by @Nikita91​ above.

Some people don't like HAL very much. You can make up your mind by learning from small examples. You can even use register level programming in the generated project or mix that with HAL. All CMSIS register defines are readily available for you.

I have compiled some "blinky" examples using different approaches here https://gitlab.com/stm32mcu/wiki/-/wikis/home#blinky-the-embedded-hello-world

AAnth.1
AAnth.1Author
Senior
April 23, 2020

Hi KnarfB

you're probably right. But I still don't like the approach for me to use the graphic interface that afterwards generates the necessary code for me. I would like to use the functions and header files from HAL (or LL) setup the MCU myself.

Then let me ask you:

Is there any approach in between starting an empty project from scratch (my initial approach) together with using the STM32CubeIDE configuration + automatic code generation?

Thank you,

Nikita91
Lead II
April 24, 2020

Hello,

CubeMX and CubeIDE are high level code generator. Don't be surprised that they generate code!

In CubeMX you have configured peripherals, it is normal for the generated code to initialize these peripherals (did you selected ADC ?). It is also the most educational part of the tool. For me the biggest utility of CubeMx is the generation of the SystemClock_Config () function which I copy as it is in my projects (In some MCU there is 3 PLL and many many clocks…)

If you want to stay close to the hardware and write the code yourself it is better to configure CubeMX to generate code using only LL. This applies to all simple devices (UART, timer, ADC, DAC ...). For complex devices (USB, Ethernet, file system ...) there is only the HAL.

You can also ask to CubeMX to generate each device initialisation in separate .c and .h files. It's clearer.

The link provided by @KnarfB (Community Member) is very interesting, particularly: https://gitlab.com/stm32mcu/blinky_register-level.

A very small project with everything you need. Replace the files related to the MCU F0 with the files of your F4 MCU and you have what you are looking for (Tedious to do but very informative)!

RMcCa
Senior II
April 24, 2020

What i do is use the cube only once for the initial generation and importing into the eclipse workspace and then delete all the generated code and import and cut and paste my base code from a template project. You still have to edit the project properties abit, but i find cube helpful for creating the initial project structure​. I suppose you could use cube just once and then replicate the file structure yourself, but that seems like a waste of time to me.

AAnth.1
AAnth.1Author
Senior
April 24, 2020

Hi all,

short update. I believe I finally managed to find my way through the IDE and the project options to include all necessary header files, setup the defines etc. It was a good exercise for me in order to get a better understanding as of which files are actually necessary and from where these header files are used/called.

I actually got my point proven to go through this (painful) exercise of creating the project manually step-by-step because using the code generation from CubeIDE, the highly critical header files stm32f4xx.h and stm32f4xx_hal.h were called within a header file usb_host.h which is a header file which I do not manage to find in the stm32cubef4 driver package (please correct me when I am wrong). I would have blindly believed the IDE that usb_host.h is crucial for my project and I would have copied this mistake from one project to another.

I now seem to have a proper minimalistic base project where only the absolute necessary header files are present and NO source files (neither HAL or LL). Next step is to include the necessary source files that are used to initialize the MCU clock, and I will then take it from there to probably make my first LED blink.

It was a tough run, and I would have not managed to get this far without your help, which I highly appreciate.

Thank you,

RKS
Associate
July 10, 2020

Hi, I'm working on setting up a similar minimal project for a STM32H755 (dual core). Would it be possible to get a copy of your base project to use as a reference? It would be a huge help. Thanks

Piranha
Principal III
April 25, 2020

Also add the useful CMSIS core files from the latest release there:

https://github.com/ARM-software/CMSIS_5/releases

NBlag.1
Visitor II
March 8, 2021

My approach to the problem is to immediately open a new file, named my_main.c, and create a while (1) {} function in it. Then I have a completely blank screen and a "main loop". In the original main.c the file only has a call to my_main (). This may not be the smartest solution, but it helps me to not to look at the original main.c, which doesn't actually do anything, and is overloaded with various /*...*/,//! I don't know if anyone, whether a beginner or a pro, got a little scared when they saw the automatically generated main.c from CubeMX. That file looks like an ad that says, "Hey look what we did all for you."

Thanks,