STM32Cube Library's

ST distributes a Hal Driver called STM32Cube.
I used this driver also in my projects.
To use it with meson build we just have to make a small change and add some path’s.

inspect Library

first I downloaded the STM32Cube-F3 for my STM32F3Discovery project.

├── Documentation
|   ├──...
├── Drivers                   # Folder of interest
|   ├──BSP                    # Abstraction for example projects
|   ├──CMSIS                  # cmsis and core defintions
|   ├──STM32F3xx_HAL_Driver   # hal driver itself
├── _htmlresc
|   ├──...
├── Middlewares
|   ├──...
├── Projects
|   ├──...
├── Utilities
|   ├──...
├── package.xml
├── Release_Notes.html

We see here the folder structure of the STM32Cube-F3 Library (V1.9.0)
The interesting part is in the Drivers folder. In there we have the CMSIS Folder and the STM32F3xx_HAL_Driver

CMSIS

├── Device
|   ├── ST
|       ├── STM32F3xx
|           ├── Include           # header files of specific devices
|           ├── Source             
|               ├── Templates
|                   ├── gcc       # startup files
|            Release_Notes.html
├── Documentation
├── DSP_Lib
├── Include
├── Lib
├── RTOS

In the CMSIS Folder are files which defines the Cortex-M Core.
In the CMSIS/Device/ST/STM32F3xx/Source/Template/gcc we find the startup files from ST.
All startup files in my repo are adjusted copy’s from here.
We have to include all the include folders and all C files in the Source directory for a minimal compilation.
If you do not use the DSP Library or an RTOS you don’t have to link these folders.

STM32F3xx_HAL_Driver

├── Include      # hal driver header files
|   ├── Legacy   # hal driver header files
├── Source       # hal driver source files

As you might expect. Here is the hal driver.
In the Source and Include folder we have some template files. We don’t have to compile these of course.
But there is one file : Include/stm32f3xx_hal_conf_template.h
This file specifies which hal drivers are getting loaded and configures some compiler flags if they are not defined by the user.

I copied the Include/stm32f3xx_hal_conf_template.h template to the Drivers folder and renamed it to stm32f3xx_hal_conf.h.
With this change other files from the HAL-Driver will find this configuration file.
In its initial state it enables all libraries. Thats totally okay. We use the linker to get rid of unused code later! And because meson build together with ninja is very efficient, it will only compile it once and will never unnecessarily recompile it again.

meson.build

I also placed an meson.build file in the root folder of this library.
Later we can call the library with just one line in the meson.build script and it will load all necessary stuff!

In the STM32Cube-F3-meson/meson.build file I defined two variables

stm32cube_incdirs = []
stm32cube_srcs    = []

we will use this arguments later in the root meson.build file to create our executable with this hal-driver.

In stm32cube_incdirs we save all folders which contain header files
In stm32cube_srcs and here of course all source files.

Because its really annoying to get every C-File path I wrote a python script to search all c file.

At the beginning of the meson.build i also defined two flags which are used by the hal-driver

c_args += '-DUSE_HAL_DRIVER'
c_args += '-DUSE_LEGACY'

thats all it needs.

usage

In the project root meson.build we just have to call the library

# add STM library
subdir('STM32Cube-F3-meson')

and use the path variables in the executable command

main = executable(
            'main.elf',
            [srcs, stm32cube_srcs, 'main.c', startupfile] ,
            c_args              : [c_args ],
            link_args           : [link_args, '-Wl,--gc-sections'],
            dependencies        : link_deps,
            include_directories : [incdirs, stm32cube_incdirs] )
Written on January 9, 2018