Skip to content

Automatically enable used HAL driver modules #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
HAL module will search all drivers supported by family and create the following targets:

* `HAL::STM32::<FAMILY>` (e.g. `HAL::STM32::F4`) - common HAL source, depends on `CMSIS::STM32::<FAMILY>`
* `HAL::STM32::<FAMILY>::<DRIVER>` (e.g. `HAL::STM32::F4::GPIO`) - HAL driver <DRIVER>, depends on `HAL::STM32::<FAMILY>`
* `HAL::STM32::<FAMILY>::<DRIVER>_ENABLE` (e.g. `HAL::STM32::F4::GPIO_ENABLE`) - Enables HAL driver <DRIVER>, by setting the `HAL_<DRIVER>_MODULE_ENABLED` macro.
* `HAL::STM32::<FAMILY>::<DRIVER>` (e.g. `HAL::STM32::F4::GPIO`) - HAL driver <DRIVER>, depends on `HAL::STM32::<FAMILY>` and `HAL::STM32::<FAMILY>::<DRIVER>_ENABLE`. Causes the source file for the driver to be built.
* `HAL::STM32::<FAMILY>::<DRIVER>Ex` (e.g. `HAL::STM32::F4::ADCEx`) - HAL Extension driver , depends on `HAL::STM32::<FAMILY>::<DRIVER>`
* `HAL::STM32::<FAMILY>::LL_<DRIVER>` (e.g. `HAL::STM32::F4::LL_ADC`) - HAL LL (Low-Level) driver , depends on `HAL::STM32::<FAMILY>`

Expand All @@ -170,6 +171,7 @@ Here is typical usage:
add_executable(stm32-blinky-f4 blinky.c stm32f4xx_hal_conf.h)
target_link_libraries(stm32-blinky-f4
HAL::STM32::F4::RCC
HAL::STM32::F4::FLASH_ENABLE
HAL::STM32::F4::GPIO
HAL::STM32::F4::CORTEX
CMSIS::STM32::F407VG
Expand Down
55 changes: 50 additions & 5 deletions cmake/FindHAL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,35 @@ function(get_list_hal_drivers out_list_hal_drivers hal_drivers_path hal_driver_t
list(FILTER filtered_files INCLUDE REGEX ${file_pattern})
# From the files names keep only the driver type part using the regex (stm32xx_hal_(rcc).c or stm32xx_ll_(rcc).c => catches rcc)
list(TRANSFORM filtered_files REPLACE ${file_pattern} "\\1")
#Making a return by reference by seting the output variable to PARENT_SCOPE
# Making a return by reference by seting the output variable to PARENT_SCOPE
set(${out_list_hal_drivers} ${filtered_files} PARENT_SCOPE)
endfunction()

# This function removes excluded drivers from the drivers_list
#
# drivers_list name of HAL drivers list
#
function(drop_excluded_hal_drivers drivers_list_name)
if(DEFINED EXCLUDED_${drivers_list_name}_OVERRIDE)
# User has overidden excluded drivers list
set(excluded_drivers_list_name "EXCLUDED_${drivers_list_name}_OVERRIDE")
else()
# User has not overridden excluded drivers list
# Fall back on default excluded drivers list
set(excluded_drivers_list_name "EXCLUDED_${drivers_list_name}")
endif()

set(excluded_drivers_list ${${excluded_drivers_list_name}})
if(excluded_drivers_list)
message(STATUS "Ignoring drivers listed in ${excluded_drivers_list_name}: ${excluded_drivers_list}")
foreach(excluded_driver ${excluded_drivers_list})
string(TOLOWER ${excluded_driver} excluded_driver)
list(REMOVE_ITEM ${drivers_list_name} ${excluded_driver})
endforeach()
# Update the original drivers list in the parent scope
set(${drivers_list_name} ${${drivers_list_name}} PARENT_SCOPE)
endif()
endfunction()
################################################################################
# Checking the parameters provided to the find_package(HAL ...) call
# The expected parameters are families and or drivers in *any orders*
Expand Down Expand Up @@ -75,9 +100,16 @@ foreach(family_comp ${HAL_FIND_COMPONENTS_FAMILIES})
set(HAL_${family_comp}_FOUND TRUE)
endif()
if(CMAKE_MATCH_1) #Matches the family part of the provided STM32<FAMILY>[..] component
# HAL drivers
get_list_hal_drivers(HAL_DRIVERS_${FAMILY} ${HAL_${FAMILY}_PATH} "hal")
drop_excluded_hal_drivers(HAL_DRIVERS_${FAMILY})

get_list_hal_drivers(HAL_EX_DRIVERS_${FAMILY} ${HAL_${FAMILY}_PATH} "ex")
drop_excluded_hal_drivers(HAL_EX_DRIVERS_${FAMILY})

get_list_hal_drivers(HAL_LL_DRIVERS_${FAMILY} ${HAL_${FAMILY}_PATH} "ll")
drop_excluded_hal_drivers(HAL_LL_DRIVERS_${FAMILY})

list(APPEND HAL_DRIVERS ${HAL_DRIVERS_${FAMILY}})
list(APPEND HAL_LL_DRIVERS ${HAL_LL_DRIVERS_${FAMILY}})
else()
Expand Down Expand Up @@ -212,9 +244,11 @@ foreach(COMP ${HAL_FIND_COMPONENTS_FAMILIES})
continue()
endif()

# Add the overall target HAL library
if(NOT (TARGET HAL::STM32::${FAMILY}${CORE_C}))
message(TRACE "FindHAL: creating library HAL::STM32::${FAMILY}${CORE_C}")
add_library(HAL::STM32::${FAMILY}${CORE_C} INTERFACE IMPORTED)
target_compile_definitions(HAL::STM32::${FAMILY}${CORE_C} INTERFACE "USE_HAL_DRIVER")
target_link_libraries(HAL::STM32::${FAMILY}${CORE_C} INTERFACE
STM32::${FAMILY}${CORE_C}
CMSIS::STM32::${FAMILY}${CORE_C})
Expand All @@ -225,7 +259,7 @@ foreach(COMP ${HAL_FIND_COMPONENTS_FAMILIES})
foreach(DRV_COMP ${HAL_FIND_COMPONENTS_DRIVERS})
string(TOLOWER ${DRV_COMP} DRV_L)
string(TOUPPER ${DRV_COMP} DRV)

if(NOT (DRV_L IN_LIST HAL_DRIVERS_${FAMILY}))
continue()
endif()
Expand All @@ -241,15 +275,26 @@ foreach(COMP ${HAL_FIND_COMPONENTS_FAMILIES})
set(HAL_${DRV_COMP}_FOUND FALSE)
continue()
endif()


# Add the individual target HAL libraries
set(HAL_${DRV_COMP}_FOUND TRUE)
if(HAL_${FAMILY}${CORE_U}_${DRV}_SOURCE AND (NOT (TARGET HAL::STM32::${FAMILY}::${DRV})))
message(TRACE "FindHAL: creating library HAL::STM32::${FAMILY}${CORE_C}::${DRV}")

# Target to add a "MODULE_ENABLED" define
add_library(HAL::STM32::${FAMILY}${CORE_C}::${DRV}_ENABLE INTERFACE IMPORTED)
target_compile_definitions(HAL::STM32::${FAMILY}${CORE_C}::${DRV}_ENABLE INTERFACE "HAL_${DRV}_MODULE_ENABLED")

# Target to build the source file (and also enable)
add_library(HAL::STM32::${FAMILY}${CORE_C}::${DRV} INTERFACE IMPORTED)
target_link_libraries(HAL::STM32::${FAMILY}${CORE_C}::${DRV} INTERFACE HAL::STM32::${FAMILY}${CORE_C})
target_link_libraries(HAL::STM32::${FAMILY}${CORE_C}::${DRV}
INTERFACE
HAL::STM32::${FAMILY}${CORE_C}
HAL::STM32::${FAMILY}${CORE_C}::${DRV}_ENABLE)
target_sources(HAL::STM32::${FAMILY}${CORE_C}::${DRV} INTERFACE "${HAL_${FAMILY}${CORE_U}_${DRV}_SOURCE}")
endif()


# Add the individual target HAL Ex libraries
if(HAL_${FAMILY}${CORE_U}_${DRV}_SOURCE AND (${DRV_L} IN_LIST HAL_EX_DRIVERS_${FAMILY}))
find_file(HAL_${FAMILY}${CORE_U}_${DRV}_EX_SOURCE
NAMES stm32${FAMILY_L}xx_hal_${DRV_L}_ex.c
Expand Down
6 changes: 6 additions & 0 deletions cmake/stm32/mp1.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ set(STM32_MP1_CCRAM_SIZES
0K 0K
0K 0K)

# The MDIOS module is broken in STM32CubeMP1 up to and including v. 1.6.0
# Exclude it by default
set(EXCLUDED_HAL_DRIVERS_MP1
MDIOS
)

stm32_util_create_family_targets(MP1 M4)

target_compile_options(STM32::MP1::M4 INTERFACE -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard)
Expand Down
6 changes: 3 additions & 3 deletions cmake/stm32/utilities.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ include(FetchContent)

# A CMSIS or HAL driver can specify 'cube' as version number to indicate that the driver is taken from the Cube repository
set(STM32_FETCH_FAMILIES F0 F1 F2 F3 F4 F7 G0 G4 H7 L0 L1 L4 L5 MP1 U5 WB WL )
set(STM32_FETCH_CUBE_VERSIONS v1.11.2 v1.8.4 v1.9.3 v1.11.2 v1.26.1 v1.16.1 v1.4.1 v1.4.0 v1.9.0 v1.12.0 v1.10.3 v1.17.0 v1.4.0 1.5.0 v1.0.0 v1.12.0 v1.1.0)
set(STM32_FETCH_CMSIS_VERSIONS v2.3.5 v4.3.3 v2.2.5 v2.3.5 v2.6.6 v1.2.6 v1.4.0 v1.2.1 v1.10.0 v1.9.1 v2.3.2 v1.7.1 v1.0.4 cube v1.0.0 v1.9.0 v1.1.0)
set(STM32_FETCH_HAL_VERSIONS v1.7.5 v1.1.8 v1.2.7 v1.5.5 v1.7.12 v1.2.9 v1.4.1 v1.2.1 v1.10.0 v1.10.4 v1.4.4 v1.13.0 v1.0.4 cube v1.0.0 v1.9.0 v1.1.0)
set(STM32_FETCH_CUBE_VERSIONS v1.11.2 v1.8.4 v1.9.3 v1.11.2 v1.26.1 v1.16.1 v1.6.1 v1.4.0 v1.9.0 v1.12.0 v1.10.3 v1.17.0 v1.4.0 1.5.0 v1.0.0 v1.12.0 v1.1.0)
set(STM32_FETCH_CMSIS_VERSIONS v2.3.5 v4.3.3 v2.2.5 v2.3.5 v2.6.6 v1.2.6 v1.4.3 v1.2.1 v1.10.0 v1.9.1 v2.3.2 v1.7.1 v1.0.4 cube v1.0.0 v1.9.0 v1.1.0)
set(STM32_FETCH_HAL_VERSIONS v1.7.5 v1.1.8 v1.2.7 v1.5.5 v1.7.12 v1.2.9 v1.4.5 v1.2.1 v1.10.0 v1.10.4 v1.4.4 v1.13.0 v1.0.4 cube v1.0.0 v1.9.0 v1.1.0)

FetchContent_Declare(
STM32-CMSIS
Expand Down
5 changes: 4 additions & 1 deletion examples/blinky/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ endif()
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)


set(HAL_COMP_LIST RCC GPIO CORTEX)
set(HAL_COMP_LIST RCC GPIO CORTEX FLASH)
set(CMSIS_COMP_LIST "")

if(BLINKY_F4_EXAMPLE)
Expand Down Expand Up @@ -53,6 +53,7 @@ find_package(HAL COMPONENTS "${HAL_COMP_LIST}" REQUIRED)
if(BLINKY_F4_EXAMPLE)
add_executable(stm32-blinky-f4 ${MAIN_SOURCE_FILE} stm32f4xx_hal_conf.h)
target_link_libraries(stm32-blinky-f4
HAL::STM32::F4::FLASH_ENABLE
HAL::STM32::F4::RCC
HAL::STM32::F4::GPIO
HAL::STM32::F4::CORTEX
Expand All @@ -66,6 +67,7 @@ endif()
if(BLINKY_F1_EXAMPLE)
add_executable(stm32-blinky-f1 ${MAIN_SOURCE_FILE} stm32f1xx_hal_conf.h)
target_link_libraries(stm32-blinky-f1
HAL::STM32::F1::FLASH_ENABLE
HAL::STM32::F1::RCC
HAL::STM32::F1::GPIO
HAL::STM32::F1::CORTEX
Expand All @@ -79,6 +81,7 @@ endif()
if(BLINKY_L0_EXAMPLE)
add_executable(stm32-blinky-l0 ${MAIN_SOURCE_FILE} stm32l0xx_hal_conf.h)
target_link_libraries(stm32-blinky-l0
HAL::STM32::L0::FLASH_ENABLE
HAL::STM32::L0::RCC
HAL::STM32::L0::GPIO
HAL::STM32::L0::CORTEX
Expand Down
15 changes: 10 additions & 5 deletions examples/blinky/stm32f1xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,25 @@ extern "C" {
/* ########################## Module Selection ############################## */
/**
* @brief This is the list of modules to be used in the HAL driver
*
* `stm32-cmake` automatically defines the `HAL_*_MODULE_ENABLED` variables
* when a driver is added to a project using `target_link_libraries`, so they
* don't need to be defined here
*
*/
#define HAL_MODULE_ENABLED
// #define HAL_ADC_MODULE_ENABLED
// #define HAL_CAN_MODULE_ENABLED
// #define HAL_CAN_LEGACY_MODULE_ENABLED
// #define HAL_CEC_MODULE_ENABLED
#define HAL_CORTEX_MODULE_ENABLED
// #define HAL_CORTEX_MODULE_ENABLED
// #define HAL_CRC_MODULE_ENABLED
// #define HAL_DAC_MODULE_ENABLED
#define HAL_DMA_MODULE_ENABLED
// #define HAL_DMA_MODULE_ENABLED
// #define HAL_ETH_MODULE_ENABLED
// #define HAL_EXTI_MODULE_ENABLED
#define HAL_FLASH_MODULE_ENABLED
#define HAL_GPIO_MODULE_ENABLED
// #define HAL_FLASH_MODULE_ENABLED
// #define HAL_GPIO_MODULE_ENABLED
// #define HAL_HCD_MODULE_ENABLED
// #define HAL_I2C_MODULE_ENABLED
// #define HAL_I2S_MODULE_ENABLED
Expand All @@ -57,7 +62,7 @@ extern "C" {
// #define HAL_PCCARD_MODULE_ENABLED
// #define HAL_PCD_MODULE_ENABLED
// #define HAL_PWR_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED
// #define HAL_RCC_MODULE_ENABLED
// #define HAL_RTC_MODULE_ENABLED
// #define HAL_SD_MODULE_ENABLED
// #define HAL_SMARTCARD_MODULE_ENABLED
Expand Down
17 changes: 11 additions & 6 deletions examples/blinky/stm32f4xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
/* ########################## Module Selection ############################## */
/**
* @brief This is the list of modules to be used in the HAL driver
*
* `stm32-cmake` automatically defines the `HAL_*_MODULE_ENABLED` variables
* when a driver is added to a project using `target_link_libraries`, so they
* don't need to be defined here
*
*/
#define HAL_MODULE_ENABLED
// #define HAL_ADC_MODULE_ENABLED
Expand All @@ -43,27 +48,27 @@
// #define HAL_CRYP_MODULE_ENABLED
// #define HAL_DAC_MODULE_ENABLED
// #define HAL_DCMI_MODULE_ENABLED
#define HAL_DMA_MODULE_ENABLED
// #define HAL_DMA_MODULE_ENABLED
// #define HAL_DMA2D_MODULE_ENABLED
// #define HAL_ETH_MODULE_ENABLED
#define HAL_FLASH_MODULE_ENABLED
// #define HAL_FLASH_MODULE_ENABLED
// #define HAL_NAND_MODULE_ENABLED
// #define HAL_NOR_MODULE_ENABLED
// #define HAL_PCCARD_MODULE_ENABLED
// #define HAL_SRAM_MODULE_ENABLED
// #define HAL_SDRAM_MODULE_ENABLED
// #define HAL_HASH_MODULE_ENABLED
#define HAL_GPIO_MODULE_ENABLED
// #define HAL_GPIO_MODULE_ENABLED
// #define HAL_EXTI_MODULE_ENABLED
// #define HAL_I2C_MODULE_ENABLED
// #define HAL_SMBUS_MODULE_ENABLED
// #define HAL_I2S_MODULE_ENABLED
// #define HAL_IWDG_MODULE_ENABLED
// #define HAL_LTDC_MODULE_ENABLED
// #define HAL_DSI_MODULE_ENABLED
#define HAL_PWR_MODULE_ENABLED
// #define HAL_PWR_MODULE_ENABLED
// #define HAL_QSPI_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED
// #define HAL_RCC_MODULE_ENABLED
// #define HAL_RNG_MODULE_ENABLED
// #define HAL_RTC_MODULE_ENABLED
// #define HAL_SAI_MODULE_ENABLED
Expand All @@ -75,7 +80,7 @@
// #define HAL_IRDA_MODULE_ENABLED
// #define HAL_SMARTCARD_MODULE_ENABLED
// #define HAL_WWDG_MODULE_ENABLED
#define HAL_CORTEX_MODULE_ENABLED
// #define HAL_CORTEX_MODULE_ENABLED
// #define HAL_PCD_MODULE_ENABLED
// #define HAL_HCD_MODULE_ENABLED
// #define HAL_FMPI2C_MODULE_ENABLED
Expand Down
15 changes: 10 additions & 5 deletions examples/blinky/stm32l0xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,30 @@
/* ########################## Module Selection ############################## */
/**
* @brief This is the list of modules to be used in the HAL driver
*
* `stm32-cmake` automatically defines the `HAL_*_MODULE_ENABLED` variables
* when a driver is added to a project using `target_link_libraries`, so they
* don't need to be defined here
*
*/
#define HAL_MODULE_ENABLED
// #define HAL_ADC_MODULE_ENABLED
// #define HAL_COMP_MODULE_ENABLED
// #define HAL_CRC_MODULE_ENABLED
// #define HAL_CRYP_MODULE_ENABLED
// #define HAL_DAC_MODULE_ENABLED
#define HAL_DMA_MODULE_ENABLED
// #define HAL_DMA_MODULE_ENABLED
// #define HAL_EXTI_MODULE_ENABLED
// #define HAL_FIREWALL_MODULE_ENABLED
#define HAL_FLASH_MODULE_ENABLED
#define HAL_GPIO_MODULE_ENABLED
// #define HAL_FLASH_MODULE_ENABLED
// #define HAL_GPIO_MODULE_ENABLED
// #define HAL_I2C_MODULE_ENABLED
// #define HAL_I2S_MODULE_ENABLED
// #define HAL_IWDG_MODULE_ENABLED
// #define HAL_LCD_MODULE_ENABLED
// #define HAL_LPTIM_MODULE_ENABLED
// #define HAL_PWR_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED
// #define HAL_RCC_MODULE_ENABLED
// #define HAL_RNG_MODULE_ENABLED
// #define HAL_RTC_MODULE_ENABLED
// #define HAL_SPI_MODULE_ENABLED
Expand All @@ -63,7 +68,7 @@
// #define HAL_SMARTCARD_MODULE_ENABLED
// #define HAL_SMBUS_MODULE_ENABLED
// #define HAL_WWDG_MODULE_ENABLED
#define HAL_CORTEX_MODULE_ENABLED
// #define HAL_CORTEX_MODULE_ENABLED
// #define HAL_PCD_MODULE_ENABLED

/* ########################## Oscillator Values adaptation ####################*/
Expand Down