1 include(stm32-bootloader)
6 include(CMakeParseArguments)
8 option(DEBUG_HARDFAULTS "Enable debugging of hard faults via custom handler")
9 option(SEMIHOSTING "Enable semihosting")
11 message("-- DEBUG_HARDFAULTS: ${DEBUG_HARDFAULTS}, SEMIHOSTING: ${SEMIHOSTING}")
13 set(CMSIS_DIR "${MAIN_LIB_DIR}/main/CMSIS")
14 set(CMSIS_INCLUDE_DIR "${CMSIS_DIR}/Core/Include")
15 set(CMSIS_DSP_DIR "${MAIN_LIB_DIR}/main/CMSIS/DSP")
16 set(CMSIS_DSP_INCLUDE_DIR "${CMSIS_DSP_DIR}/Include")
19 BasicMathFunctions/arm_scale_f32.c
20 BasicMathFunctions/arm_sub_f32.c
21 BasicMathFunctions/arm_mult_f32.c
22 TransformFunctions/arm_rfft_fast_f32.c
23 TransformFunctions/arm_cfft_f32.c
24 TransformFunctions/arm_rfft_fast_init_f32.c
25 TransformFunctions/arm_cfft_radix8_f32.c
26 TransformFunctions/arm_bitreversal2.S
27 CommonTables/arm_common_tables.c
28 ComplexMathFunctions/arm_cmplx_mag_f32.c
29 StatisticsFunctions/arm_max_f32.c
31 list(TRANSFORM CMSIS_DSP_SRC PREPEND "${CMSIS_DSP_DIR}/Source/")
33 set(STM32_STARTUP_DIR "${MAIN_SRC_DIR}/startup")
35 main_sources(STM32_VCP_SRC
36 drivers/serial_usb_vcp.c
40 main_sources(STM32_SDCARD_SRC
41 drivers/sdcard/sdcard.c
42 drivers/sdcard/sdcard_spi.c
43 drivers/sdcard/sdcard_sdio.c
44 drivers/sdcard/sdcard_standard.c
47 # XXX: This code is not STM32 specific
48 main_sources(STM32_ASYNCFATFS_SRC
49 io/asyncfatfs/asyncfatfs.c
50 io/asyncfatfs/fat_standard.c
53 main_sources(STM32_MSC_SRC
57 main_sources(STM32_MSC_FLASH_SRC
58 msc/usbd_storage_emfat.c
63 main_sources(STM32_MSC_SDCARD_SRC
64 msc/usbd_storage_sd_spi.c
67 set(STM32_INCLUDE_DIRS
68 "${CMSIS_INCLUDE_DIR}"
69 "${CMSIS_DSP_INCLUDE_DIR}"
70 "${MAIN_SRC_DIR}/target"
75 set(STM32_DEFAULT_HSE_MHZ 8)
76 set(STM32_LINKER_DIR "${MAIN_SRC_DIR}/target/link")
77 set(STM32_COMPILE_OPTIONS
83 set(STM32_LINK_LIBRARIES
89 list(APPEND STM32_LINK_LIBRARIES --specs=rdimon.specs -lrdimon)
90 list(APPEND STM32_DEFINITIONS SEMIHOSTING)
92 list(APPEND STM32_LINK_LIBRARIES -lnosys)
95 set(STM32_LINK_OPTIONS
100 -Wl,-L${STM32_LINKER_DIR}
102 -Wl,--no-wchar-size-warning
103 -Wl,--print-memory-usage
106 macro(get_stm32_target_features output_var dir target_name)
107 execute_process(COMMAND "${CMAKE_C_COMPILER}" -E -dD -D${ARGV2} "${ARGV1}/target.h"
108 ERROR_VARIABLE _errors
109 RESULT_VARIABLE _result
110 OUTPUT_STRIP_TRAILING_WHITESPACE
111 OUTPUT_VARIABLE _contents)
113 if(NOT _result EQUAL 0)
114 message(FATAL_ERROR "error extracting features for stm32 target ${ARGV2}: ${_errors}")
117 string(REGEX MATCH "#define[\t ]+USE_VCP" HAS_VCP ${_contents})
119 list(APPEND ${ARGV0} VCP)
121 string(REGEX MATCH "define[\t ]+USE_FLASHFS" HAS_FLASHFS ${_contents})
123 list(APPEND ${ARGV0} FLASHFS)
125 string(REGEX MATCH "define[\t ]+USE_SDCARD" HAS_SDCARD ${_contents})
127 list(APPEND ${ARGV0} SDCARD)
128 string(REGEX MATCH "define[\t ]+USE_SDCARD_SDIO" HAS_SDIO ${_contents})
130 list(APPEND ${ARGV0} SDIO)
133 if(HAS_FLASHFS OR HAS_SDCARD)
134 list(APPEND ${ARGV0} MSC)
138 function(get_stm32_flash_size out size)
139 # 4: 16, 6: 32, 8: 64, B: 128, C: 256, D: 384, E: 512, F: 768, G: 1024, H: 1536, I: 2048 KiB
140 string(TOUPPER ${size} s)
141 if(${s} STREQUAL "4")
142 set(${out} 16 PARENT_SCOPE)
145 if(${s} STREQUAL "6")
146 set(${out} 32 PARENT_SCOPE)
149 if(${s} STREQUAL "8")
150 set(${out} 64 PARENT_SCOPE)
153 if(${s} STREQUAL "8")
154 set(${out} 64 PARENT_SCOPE)
157 if(${s} STREQUAL "B")
158 set(${out} 128 PARENT_SCOPE)
161 if(${s} STREQUAL "C")
162 set(${out} 256 PARENT_SCOPE)
165 if(${s} STREQUAL "D")
166 set(${out} 384 PARENT_SCOPE)
169 if(${s} STREQUAL "E")
170 set(${out} 512 PARENT_SCOPE)
173 if(${s} STREQUAL "F")
174 set(${out} 768 PARENT_SCOPE)
177 if(${s} STREQUAL "G")
178 set(${out} 1024 PARENT_SCOPE)
181 if(${s} STREQUAL "H")
182 set(${out} 1536 PARENT_SCOPE)
185 if(${s} STREQUAL "I")
186 set(${out} 2048 PARENT_SCOPE)
191 function(add_hex_target name exe hex)
192 add_custom_target(${name} ALL
193 cmake -E env PATH="$ENV{PATH}"
194 # TODO: Overriding the start address with --set-start 0x08000000
195 # seems to be required due to some incorrect assumptions about .hex
196 # files in the configurator. Verify wether that's the case and fix
197 # the bug in configurator or delete this comment.
198 ${CMAKE_OBJCOPY} -Oihex --set-start 0x08000000 $<TARGET_FILE:${exe}> ${hex}
203 function(add_bin_target name exe bin)
204 add_custom_target(${name}
205 cmake -E env PATH="$ENV{PATH}"
206 ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${exe}> ${bin}
211 function(generate_map_file target)
212 if(CMAKE_VERSION VERSION_LESS 3.15)
213 set(map "$<TARGET_FILE:${target}>.map")
215 set(map "$<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.map")
217 target_link_options(${target} PRIVATE "-Wl,-Map,${map}")
220 function(set_linker_script target script)
221 set(script_path ${STM32_LINKER_DIR}/${args_LINKER_SCRIPT}.ld)
222 if(NOT EXISTS ${script_path})
223 message(FATAL_ERROR "linker script ${script_path} doesn't exist")
225 set_target_properties(${target} PROPERTIES LINK_DEPENDS ${script_path})
226 target_link_options(${elf_target} PRIVATE -T${script_path})
229 function(add_stm32_executable)
230 cmake_parse_arguments(
234 # Single value arguments
235 "FILENAME;NAME;OPTIMIZATION;OUTPUT_BIN_FILENAME;OUTPUT_HEX_FILENAME;OUTPUT_TARGET_NAME"
236 # Multi-value arguments
237 "COMPILE_DEFINITIONS;COMPILE_OPTIONS;INCLUDE_DIRECTORIES;LINK_OPTIONS;LINKER_SCRIPT;SOURCES"
238 # Start parsing after the known arguments
241 set(elf_target ${args_NAME}.elf)
242 add_executable(${elf_target})
243 target_sources(${elf_target} PRIVATE ${args_SOURCES})
244 target_include_directories(${elf_target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${args_INCLUDE_DIRECTORIES} ${STM32_INCLUDE_DIRS})
245 target_compile_definitions(${elf_target} PRIVATE ${args_COMPILE_DEFINITIONS})
246 target_compile_options(${elf_target} PRIVATE ${STM32_COMPILE_OPTIONS} ${args_COMPILE_OPTIONS})
247 if(WARNINGS_AS_ERRORS)
248 target_compile_options(${elf_target} PRIVATE -Werror)
250 if (IS_RELEASE_BUILD)
251 target_compile_options(${elf_target} PRIVATE ${args_OPTIMIZATION})
252 target_link_options(${elf_target} PRIVATE ${args_OPTIMIZATION})
254 target_link_libraries(${elf_target} PRIVATE ${STM32_LINK_LIBRARIES})
255 target_link_options(${elf_target} PRIVATE ${STM32_LINK_OPTIONS} ${args_LINK_OPTIONS})
256 generate_map_file(${elf_target})
257 set_linker_script(${elf_target} ${args_LINKER_SCRIPT})
259 set(basename ${CMAKE_BINARY_DIR}/${args_FILENAME})
260 set(hex_filename ${basename}.hex)
261 add_hex_target(${args_NAME} ${elf_target} ${hex_filename})
262 set(bin_filename ${basename}.bin)
263 add_bin_target(${args_NAME}.bin ${elf_target} ${bin_filename})
265 if(args_OUTPUT_BIN_FILENAME)
266 set(${args_OUTPUT_BIN_FILENAME} ${bin_filename} PARENT_SCOPE)
268 if(args_OUTPUT_TARGET_NAME)
269 set(${args_OUTPUT_TARGET_NAME} ${elf_target} PARENT_SCOPE)
271 if(args_OUTPUT_HEX_FILENAME)
272 set(${args_OUTPUT_HEX_FILENAME} ${hex_filename} PARENT_SCOPE)
276 function(target_stm32)
277 if(NOT arm-none-eabi STREQUAL TOOLCHAIN)
280 # Parse keyword arguments
281 cmake_parse_arguments(
284 "DISABLE_MSC;BOOTLOADER"
285 # Single value arguments
286 "HSE_MHZ;LINKER_SCRIPT;NAME;OPENOCD_TARGET;OPTIMIZATION;STARTUP;SVD"
287 # Multi-value arguments
288 "COMPILE_DEFINITIONS;COMPILE_OPTIONS;INCLUDE_DIRECTORIES;LINK_OPTIONS;SOURCES;MSC_SOURCES;MSC_INCLUDE_DIRECTORIES;VCP_SOURCES;VCP_INCLUDE_DIRECTORIES"
289 # Start parsing after the known arguments
292 set(name ${args_NAME})
295 set(hse_mhz ${args_HSE_MHZ})
297 set(hse_mhz ${STM32_DEFAULT_HSE_MHZ})
300 set(target_sources ${STM32_STARTUP_DIR}/${args_STARTUP})
301 list(APPEND target_sources ${args_SOURCES})
302 file(GLOB target_c_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
303 file(GLOB target_h_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
304 list(APPEND target_sources ${target_c_sources} ${target_h_sources})
306 set(target_include_directories ${args_INCLUDE_DIRECTORIES})
308 set(target_definitions ${STM32_DEFINITIONS} ${COMMON_COMPILE_DEFINITIONS})
310 get_stm32_target_features(features "${CMAKE_CURRENT_SOURCE_DIR}" ${name})
311 set_property(TARGET ${elf_target} PROPERTY FEATURES ${features})
313 if(VCP IN_LIST features)
314 list(APPEND target_sources ${STM32_VCP_SRC} ${args_VCP_SOURCES})
315 list(APPEND target_include_directories ${args_VCP_INCLUDE_DIRECTORIES})
317 if(SDCARD IN_LIST features)
318 list(APPEND target_sources ${STM32_SDCARD_SRC} ${STM32_ASYNCFATFS_SRC})
322 if(NOT args_DISABLE_MSC AND MSC IN_LIST features)
323 list(APPEND target_include_directories ${args_MSC_INCLUDE_DIRECTORIES})
324 list(APPEND msc_sources ${STM32_MSC_SRC} ${args_MSC_SOURCES})
325 list(APPEND target_definitions USE_USB_MSC)
326 if(FLASHFS IN_LIST features)
327 list(APPEND msc_sources ${STM32_MSC_FLASH_SRC})
329 if (SDCARD IN_LIST features)
330 list(APPEND msc_sources ${STM32_MSC_SDCARD_SRC})
334 math(EXPR hse_value "${hse_mhz} * 1000000")
335 list(APPEND target_definitions "HSE_VALUE=${hse_value}")
336 if(args_COMPILE_DEFINITIONS)
337 list(APPEND target_definitions ${args_COMPILE_DEFINITIONS})
340 list(APPEND target_definitions DEBUG_HARDFAULTS)
343 string(TOLOWER ${PROJECT_NAME} lowercase_project_name)
344 set(binary_name ${lowercase_project_name}_${FIRMWARE_VERSION}_${name})
345 if(DEFINED BUILD_SUFFIX AND NOT "" STREQUAL "${BUILD_SUFFIX}")
346 set(binary_name "${binary_name}_${BUILD_SUFFIX}")
350 add_stm32_executable(
352 FILENAME ${binary_name}
353 SOURCES ${target_sources} ${msc_sources} ${CMSIS_DSP_SRC} ${COMMON_SRC}
354 COMPILE_DEFINITIONS ${target_definitions}
355 COMPILE_OPTIONS ${args_COMPILE_OPTIONS}
356 INCLUDE_DIRECTORIES ${target_include_directories}
357 LINK_OPTIONS ${args_LINK_OPTIONS}
358 LINKER_SCRIPT ${args_LINKER_SCRIPT}
359 OPTIMIZATION ${args_OPTIMIZATION}
361 OUTPUT_HEX_FILENAME main_hex_filename
362 OUTPUT_TARGET_NAME main_target_name
365 set_property(TARGET ${main_target_name} PROPERTY OPENOCD_TARGET ${args_OPENOCD_TARGET})
366 set_property(TARGET ${main_target_name} PROPERTY OPENOCD_DEFAULT_INTERFACE stlink)
367 set_property(TARGET ${main_target_name} PROPERTY SVD ${args_SVD})
369 setup_firmware_target(${main_target_name} ${name} ${ARGN})
372 # Bootloader for the target
374 add_stm32_executable(
375 NAME ${name}${bl_suffix}
376 FILENAME ${binary_name}${bl_suffix}
377 SOURCES ${target_sources} ${BOOTLOADER_SOURCES}
378 COMPILE_DEFINITIONS ${target_definitions} BOOTLOADER MSP_FIRMWARE_UPDATE
379 COMPILE_OPTIONS ${args_COMPILE_OPTIONS}
380 INCLUDE_DIRECTORIES ${target_include_directories}
381 LINK_OPTIONS ${args_LINK_OPTIONS}
382 LINKER_SCRIPT ${args_LINKER_SCRIPT}${bl_suffix}
383 OPTIMIZATION ${args_OPTIMIZATION}
385 OUTPUT_BIN_FILENAME bl_bin_filename
386 OUTPUT_HEX_FILENAME bl_hex_filename
387 OUTPUT_TARGET_NAME bl_target_name
389 setup_executable(${bl_target_name} ${name})
391 # Main firmware, but for running with the bootloader
392 set(for_bl_suffix _for_bl)
393 add_stm32_executable(
394 NAME ${name}${for_bl_suffix}
395 FILENAME ${binary_name}${for_bl_suffix}
396 SOURCES ${target_sources} ${msc_sources} ${CMSIS_DSP_SRC} ${COMMON_SRC}
397 COMPILE_DEFINITIONS ${target_definitions} MSP_FIRMWARE_UPDATE
398 COMPILE_OPTIONS ${args_COMPILE_OPTIONS}
399 INCLUDE_DIRECTORIES ${target_include_directories}
400 LINK_OPTIONS ${args_LINK_OPTIONS}
401 LINKER_SCRIPT ${args_LINKER_SCRIPT}${for_bl_suffix}
402 OPTIMIZATION ${args_OPTIMIZATION}
404 OUTPUT_BIN_FILENAME for_bl_bin_filename
405 OUTPUT_HEX_FILENAME for_bl_hex_filename
406 OUTPUT_TARGET_NAME for_bl_target_name
408 setup_executable(${for_bl_target_name} ${name})
410 # Combined with bootloader and main firmware
411 set(with_bl_suffix _with_bl)
412 set(combined_hex ${CMAKE_BINARY_DIR}/${binary_name}${with_bl_suffix}.hex)
413 set(with_bl_target ${name}${with_bl_suffix})
414 add_custom_target(${with_bl_target}
415 ${CMAKE_SOURCE_DIR}/src/utils/combine_tool ${bl_bin_filename} ${for_bl_bin_filename} ${combined_hex}
416 BYPRODUCTS ${combined_hex}
418 add_dependencies(${with_bl_target} ${bl_target_name} ${for_bl_target_name})
422 set(generator_cmd "")
423 if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
424 set(generator_cmd "make")
425 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
426 set(generator_cmd "ninja")
428 if (NOT generator_cmd STREQUAL "")
429 set(clean_target "clean_${name}")
430 add_custom_target(${clean_target}
431 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
432 COMMAND ${generator_cmd} clean
433 COMMENT "Removing intermediate files for ${name}")
434 set_property(TARGET ${clean_target} PROPERTY
436 EXCLUDE_FROM_DEFAULT_BUILD 1)