Skip to content

Instantly share code, notes, and snippets.

@eugene-babichenko
Last active July 1, 2023 18:01
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save eugene-babichenko/3118042a23d2082bfba624f3f2039843 to your computer and use it in GitHub Desktop.
Save eugene-babichenko/3118042a23d2082bfba624f3f2039843 to your computer and use it in GitHub Desktop.
CMakeLists.txt and toolchain file for building STM32 HAL-based projects generated with STM32CubeMX with arm-none-eabi-gcc. STM32CubeMX settings. Toolchain: SW4STM32. ☑ Generate under root.
set(PRJ_NAME CLion_STM_LED)
set(MCU_FAMILY STM32F1xx)
set(MCU_LINE STM32F103xB)
set(MCU_LINKER_SCRIPT STM32F103RBTx_FLASH.ld)
cmake_minimum_required(VERSION 3.6)
project(${PRJ_NAME} C ASM)
add_definitions(-D${MCU_LINE})
add_definitions(-DUSE_HAL_LIBRARY)
file(GLOB_RECURSE USER_SOURCES Src/*.c)
file(GLOB_RECURSE HAL_SOURCES Drivers/${MCU_FAMILY}_HAL_DRIVER/Src/*.c)
file(GLOB_RECURSE CMSIS_SYSTEM Drivers/CMSIS/Device/ST/${MCU_FAMILY}/Source/Templates/system_${MCU_FAMILY}.c)
file(GLOB_RECURSE CMSIS_STARTUP Drivers/CMSIS/Device/ST/${MCU_FAMILY}/Source/Templates/gcc/startup_${MCU_LINE}.s)
set(SOURCE_FILES ${USER_SOURCES} ${HAL_SOURCES} ${CMSIS_SYSTEM} ${CMSIS_STARTUP} ${MCU_LINKER_SCRIPT})
include_directories(Drivers/CMSIS/Device/ST/STM32F1xx/Include)
include_directories(Drivers/CMSIS/Include)
include_directories(Drivers/STM32F1xx_HAL_Driver/Inc)
include_directories(Inc)
add_executable(${PROJECT_NAME}.elf ${SOURCE_FILES})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.map")
set(HEX_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.bin)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMENT "Building ${HEX_FILE} \nBuilding ${BIN_FILE}")
include(CMakeForceCompiler)
set(MCU_LINKER_SCRIPT STM32F103RBTx_FLASH.ld)
set(MCU_ARCH cortex-m3)
set(MCU_FLOAT_ABI soft)
set(MCU_FPU fpv4-sp-d16)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
CMAKE_FORCE_C_COMPILER(arm-none-eabi-gcc GCC)
CMAKE_FORCE_CXX_COMPILER(arm-none-eabi-g++ GCC)
set(COMMON_FLAGS "-mcpu=${MCU_ARCH} -mthumb -mthumb-interwork -mfloat-abi=${MCU_FLOAT_ABI} -ffunction-sections -fdata-sections -g -fno-common -fmessage-length=0")
if (MCU_FLOAT_ABI STREQUAL hard)
set(COMMON_FLAGS "${COMMON_FLAGS} -mfpu=${MCU_FPU}")
endif ()
set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} -std=c++11")
set(CMAKE_C_FLAGS "${COMMON_FLAGS} -std=gnu99")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-gc-sections -T ${CMAKE_SOURCE_DIR}/${MCU_LINKER_SCRIPT}")
@akshay2251
Copy link

Is this , cmake & toolchain feasible for Nucleo-STM32F103RB .
if yes -> it gave
CMakeFiles/C_STM_LED.elf.dir/Src/main.c.obj: In function main': main.c:(.text+0x5): undefined reference to HAL_Init'
main.c:(.text+0x1e): undefined reference to HAL_GPIO_TogglePin' main.c:(.text+0x28): undefined reference to HAL_Delay'
CMakeFiles/C_STM_LED.elf.dir/Src/main.c.obj: In function SystemClock_Config': main.c:(.text+0xb6): undefined reference to HAL_RCC_OscConfig'
main.c:(.text+0xf3): undefined reference to HAL_RCC_ClockConfig' CMakeFiles/C_STM_LED.elf.dir/Src/main.c.obj: In function MX_GPIO_Init':
main.c:(.text+0x172): undefined reference to HAL_GPIO_WritePin' main.c:(.text+0x19f): undefined reference to HAL_GPIO_Init'
CMakeFiles/C_STM_LED.elf.dir/Src/stm32f1xx_it.c.obj: In function SysTick_Handler': stm32f1xx_it.c:(.text+0x39): undefined reference to HAL_IncTick'
collect2: error: ld returned 1 exit status
CMakeFiles/C_STM_LED.elf.dir/build.make:198: recipe for target 'C_STM_LED.elf' failed
make[2]: *** [C_STM_LED.elf] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/C_STM_LED.elf.dir/all' failed
make[1]: *** [CMakeFiles/C_STM_LED.elf.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

i'm new to this cmake & toolchain. plz help me regarding this point

@akshay2251
Copy link

There is no folder like gcc/.... in Templates & not present system_STM32F1XX.c file means in -> Templates/system_${MCU_FAMILY}.c)

@eugene-babichenko
Copy link
Author

eugene-babichenko commented Jun 11, 2019

Well, I created this gist 3 years ago and many things may have changed since that time. Haven't used it in a while. I suggest that you may be using an incorrect MCU_FAMILY value (are you really running STM32F1XX?) or STMicroelectronics have changed the structure of their generated projects and replaced Templates/system_${MCU_FAMILY}.c with something else.

@karginbilgehan
Copy link

Hi,
Which file path should we put these two files (CMakeList.txt and STM32Toolchain.cmake) in the file structure of the project?
Secondly,
For cmake, What parameters should we run in terminal?

@eugene-babichenko
Copy link
Author

eugene-babichenko commented Sep 29, 2019

@karginbilgehan
Copy link

karginbilgehan commented Sep 29, 2019 via email

@karginbilgehan
Copy link

karginbilgehan commented Sep 30, 2019 via email

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