0

I would like to test CLion for my stm32 project since it now support remote debugging! To do so I need to setup Cmake for my project and it is my issue.

I tried using this link which seems to be depreciated so I made some changes. It almost builds but there is a problem with the .elf .bin .hex.

CMakeLists.txt:

project(F466cmake)
cmake_minimum_required(VERSION 3.10)

add_definitions(-DSTM32F446xx)

file(GLOB_RECURSE USER_SOURCES "Src/*.c")
file(GLOB_RECURSE HAL_SOURCES "Drivers/STM32F4xx_HAL_Driver/Src/*.c")

add_library(CMSIS
        Drivers/CMSIS/Device/Src/system_stm32f4xx.c
        Drivers/CMSIS/Device/Src/startup_stm32f446xx.s)

include_directories(Inc)
include_directories(Drivers/STM32F4xx_HAL_Driver/Inc)
include_directories(Drivers/CMSIS/Inc)
include_directories(Drivers/CMSIS/Device/Inc)

set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F446RETx_FLASH.ld)
set(COMMON_FLAGS "-mcpu=cortex-m4 -mthumb -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16 -ffunction-sections -fdata-sections -g -fno-common -fmessage-length=0")
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 ${LINKER_SCRIPT}")

add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${HAL_SOURCES} ${LINKER_SCRIPT})

target_link_libraries(${PROJECT_NAME}.elf CMSIS)

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}")

terminal output:

[ 95%] Building C object CMakeFiles/F466cmake.elf.dir/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c.o
[100%] Linking C executable F466cmake.elf
/home/flo/bin/gcc-arm-none-eabi-7-2018-q2-update/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/bin/ld: warning: cannot find entry symbol Reset_Handler; defaulting to 0000000008000000
Building /home/flo/STM32Cube/Testing/build/F466cmake.hex 
Building /home/flo/STM32Cube/Testing/build/F466cmake.bin
/usr/bin/objcopy: Unable to recognise the format of the input file `/home/flo/STM32Cube/Testing/build/F466cmake.elf'
CMakeFiles/F466cmake.elf.dir/build.make:563: recipe for target 'F466cmake.elf' failed
make[2]: *** [F466cmake.elf] Error 1
make[2]: *** Deleting file 'F466cmake.elf'
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/F466cmake.elf.dir/all' failed
make[1]: *** [CMakeFiles/F466cmake.elf.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Florian K
  • 92
  • 9
  • 2
    You are using the host's objcopy not the cross version. Did the .hex actually get written? You never seem to configure the cross version of objcopy the way you do the other tools. Also note that part of your project is missing as indicated by the warning about the missing reset handler, the guess is not going to work as flash must start with a vector block, not code. – Chris Stratton Aug 04 '19 at 17:41
  • @ChrisStratton Yes the .hex and .map are in my build folder. So I guess I have 2 issues, defining the reset handler address and investigate objectcopy – Florian K Aug 04 '19 at 17:51
  • The reset handler probably is defined, the issue is that the file providing it was not included in your build or at least the corresponding object was not included among those given to the linker. The objcopy you want to use is the one alongside the compiler and linker you are using, you just need to figure out how to specify that to cmake. You could also try running it manually to see if you get a different result. – Chris Stratton Aug 04 '19 at 17:53
  • @ChrisStratton I found /* Entry Point */ ENTRY(Reset_Handler) in my STM32F446RETx_FLASH.ld file. I remember moving a file in the same folder somewhere else. Maybe I should move this file in the same folder. – Florian K Aug 04 '19 at 18:01
  • No, that is a *reference* to it, but it is not a definition of it. You need to make sure the code that actually *is* the reset handler is getting built and linked. – Chris Stratton Aug 04 '19 at 18:02
  • @ChrisStratton The definition is in Drivers/CMSIS/Device/Src/startup_stm32f446xx.s which is given ```add_library(CMSIS Drivers/CMSIS/Device/Src/system_stm32f4xx.c Drivers/CMSIS/Device/Src/startup_stm32f446xx.s) ``` – Florian K Aug 04 '19 at 18:09
  • 1
    `ENTRY()` is effectively ignored in bare metal systems. Its value is present in the `.elf` and `.hex` files, but does not get written into the flash. The effective entry point is at offset 4 of the flash image (on the Cortex-M), in the vector table defined by the startup files. – followed Monica to Codidact Aug 05 '19 at 04:19

1 Answers1

3

Got it to work, thanks to Chris for the guidance. The issue was the ASM was not build and I had to link arm object copy.

CMakeLists.txt:

project(F466cmake C ASM)
cmake_minimum_required(VERSION 3.10)

add_definitions(-DSTM32F446xx)

file(GLOB_RECURSE USER_SOURCES "Src/*.c")
file(GLOB_RECURSE HAL_SOURCES "Drivers/STM32F4xx_HAL_Driver/Src/*.c")

add_library(CMSIS
        Drivers/CMSIS/Device/Src/system_stm32f4xx.c)
add_library(STARTUP
    Drivers/CMSIS/Device/Src/startup_stm32f446xx.s)

set_source_files_properties(Drivers/CMSIS/Device/Src/startup_stm32f446xx.s PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp")
set_property(SOURCE Drivers/CMSIS/Device/Src/startup_stm32f446xx.s PROPERTY LANGUAGE C)

include_directories(Inc)
include_directories(Drivers/STM32F4xx_HAL_Driver/Inc)
include_directories(Drivers/CMSIS/Inc)
include_directories(Drivers/CMSIS/Device/Inc)

set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F446RETx_FLASH.ld)
set(COMMON_FLAGS "-mcpu=cortex-m4 -mthumb -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16 -ffunction-sections -fdata-sections -g -fno-common -fmessage-length=0")
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 ${LINKER_SCRIPT}")

add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${HAL_SOURCES} ${LINKER_SCRIPT})

target_link_libraries(${PROJECT_NAME}.elf CMSIS STARTUP)

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 ${OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMENT "Building ${HEX_FILE} \nBuilding ${BIN_FILE}")
Florian K
  • 92
  • 9