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 BasicMathFunctions/arm_offset_f32.c
23 TransformFunctions/arm_rfft_fast_f32.c
24 TransformFunctions/arm_cfft_f32.c
25 TransformFunctions/arm_rfft_fast_init_f32.c
26 TransformFunctions/arm_cfft_radix8_f32.c
27 TransformFunctions/arm_bitreversal2.S
28 CommonTables/arm_common_tables.c
29 ComplexMathFunctions/arm_cmplx_mag_f32.c
30 StatisticsFunctions/arm_max_f32.c
31 StatisticsFunctions/arm_rms_f32.c
32 StatisticsFunctions/arm_std_f32.c
33 StatisticsFunctions/arm_mean_f32.c
35 list(TRANSFORM CMSIS_DSP_SRC PREPEND "${CMSIS_DSP_DIR}/Source/")
37 set(STM32_STARTUP_DIR "${MAIN_SRC_DIR}/startup")
39 main_sources(STM32_VCP_SRC
40 drivers/serial_usb_vcp.c
44 main_sources(STM32_SDCARD_SRC
45 drivers/sdcard/sdcard.c
46 drivers/sdcard/sdcard_spi.c
47 drivers/sdcard/sdcard_sdio.c
48 drivers/sdcard/sdcard_standard.c
51 # XXX: This code is not STM32 specific
52 main_sources(STM32_ASYNCFATFS_SRC
53 io/asyncfatfs/asyncfatfs.c
54 io/asyncfatfs/fat_standard.c
57 main_sources(STM32_MSC_SRC
61 main_sources(STM32_MSC_FLASH_SRC
62 msc/usbd_storage_emfat.c
67 main_sources(STM32_MSC_SDCARD_SRC
68 msc/usbd_storage_sd_spi.c
71 set(STM32_INCLUDE_DIRS
72 "${CMSIS_INCLUDE_DIR}"
73 "${CMSIS_DSP_INCLUDE_DIR}"
74 "${MAIN_SRC_DIR}/target"
79 set(STM32_DEFAULT_HSE_MHZ 8)
80 set(STM32_LINKER_DIR "${MAIN_SRC_DIR}/target/link")
81 set(STM32_COMPILE_OPTIONS
87 set(STM32_LINK_LIBRARIES
93 list(APPEND STM32_LINK_LIBRARIES --specs=rdimon.specs -lrdimon)
94 list(APPEND STM32_DEFINITIONS SEMIHOSTING)
96 list(APPEND STM32_LINK_LIBRARIES -lnosys)
99 set(STM32_LINK_OPTIONS
104 -Wl,-L${STM32_LINKER_DIR}
106 -Wl,--no-wchar-size-warning
107 -Wl,--print-memory-usage
110 macro(get_stm32_target_features output_var dir target_name)
111 execute_process(COMMAND "${CMAKE_C_COMPILER}" -E -dD -D${ARGV2} "${ARGV1}/target.h"
112 ERROR_VARIABLE _errors
113 RESULT_VARIABLE _result
114 OUTPUT_STRIP_TRAILING_WHITESPACE
115 OUTPUT_VARIABLE _contents)
117 if(NOT _result EQUAL 0)
118 message(FATAL_ERROR "error extracting features for stm32 target ${ARGV2}: ${_errors}")
121 string(REGEX MATCH "#define[\t ]+USE_VCP" HAS_VCP ${_contents})
123 list(APPEND ${ARGV0} VCP)
125 string(REGEX MATCH "define[\t ]+USE_FLASHFS" HAS_FLASHFS ${_contents})
127 list(APPEND ${ARGV0} FLASHFS)
129 string(REGEX MATCH "define[\t ]+USE_SDCARD" HAS_SDCARD ${_contents})
131 list(APPEND ${ARGV0} SDCARD)
132 string(REGEX MATCH "define[\t ]+USE_SDCARD_SDIO" HAS_SDIO ${_contents})
134 list(APPEND ${ARGV0} SDIO)
137 if(HAS_FLASHFS OR HAS_SDCARD)
138 list(APPEND ${ARGV0} MSC)
142 function(get_stm32_flash_size out size)
143 # 4: 16, 6: 32, 8: 64, B: 128, C: 256, D: 384, E: 512, F: 768, G: 1024, H: 1536, I: 2048 KiB
144 string(TOUPPER ${size} s)
145 if(${s} STREQUAL "4")
146 set(${out} 16 PARENT_SCOPE)
149 if(${s} STREQUAL "6")
150 set(${out} 32 PARENT_SCOPE)
153 if(${s} STREQUAL "8")
154 set(${out} 64 PARENT_SCOPE)
157 if(${s} STREQUAL "8")
158 set(${out} 64 PARENT_SCOPE)
161 if(${s} STREQUAL "B")
162 set(${out} 128 PARENT_SCOPE)
165 if(${s} STREQUAL "C")
166 set(${out} 256 PARENT_SCOPE)
169 if(${s} STREQUAL "D")
170 set(${out} 384 PARENT_SCOPE)
173 if(${s} STREQUAL "E")
174 set(${out} 512 PARENT_SCOPE)
177 if(${s} STREQUAL "F")
178 set(${out} 768 PARENT_SCOPE)
181 if(${s} STREQUAL "G")
182 set(${out} 1024 PARENT_SCOPE)
185 if(${s} STREQUAL "H")
186 set(${out} 1536 PARENT_SCOPE)
189 if(${s} STREQUAL "I")
190 set(${out} 2048 PARENT_SCOPE)
195 function(add_hex_target name exe hex)
196 add_custom_target(${name} ALL
197 cmake -E env PATH="$ENV{PATH}"
198 # TODO: Overriding the start address with --set-start 0x08000000
199 # seems to be required due to some incorrect assumptions about .hex
200 # files in the configurator. Verify wether that's the case and fix
201 # the bug in configurator or delete this comment.
202 ${CMAKE_OBJCOPY} -Oihex --set-start 0x08000000 $<TARGET_FILE:${exe}> ${hex}
207 function(add_bin_target name exe bin)
208 add_custom_target(${name}
209 cmake -E env PATH="$ENV{PATH}"
210 ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${exe}> ${bin}
215 function(generate_map_file target)
216 if(CMAKE_VERSION VERSION_LESS 3.15)
217 set(map "$<TARGET_FILE:${target}>.map")
219 set(map "$<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.map")
221 target_link_options(${target} PRIVATE "-Wl,-Map,${map}")
224 function(set_linker_script target script)
225 set(script_path ${STM32_LINKER_DIR}/${args_LINKER_SCRIPT}.ld)
226 if(NOT EXISTS ${script_path})
227 message(FATAL_ERROR "linker script ${script_path} doesn't exist")
229 set_target_properties(${target} PROPERTIES LINK_DEPENDS ${script_path})
230 target_link_options(${elf_target} PRIVATE -T${script_path})
233 function(add_stm32_executable)
234 cmake_parse_arguments(
238 # Single value arguments
239 "FILENAME;NAME;OPTIMIZATION;OUTPUT_BIN_FILENAME;OUTPUT_HEX_FILENAME;OUTPUT_TARGET_NAME"
240 # Multi-value arguments
241 "COMPILE_DEFINITIONS;COMPILE_OPTIONS;INCLUDE_DIRECTORIES;LINK_OPTIONS;LINKER_SCRIPT;SOURCES"
242 # Start parsing after the known arguments
245 set(elf_target ${args_NAME}.elf)
246 add_executable(${elf_target})
247 target_sources(${elf_target} PRIVATE ${args_SOURCES})
248 target_include_directories(${elf_target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${args_INCLUDE_DIRECTORIES} ${STM32_INCLUDE_DIRS})
249 target_compile_definitions(${elf_target} PRIVATE ${args_COMPILE_DEFINITIONS})
250 target_compile_options(${elf_target} PRIVATE ${STM32_COMPILE_OPTIONS} ${args_COMPILE_OPTIONS})
251 if(WARNINGS_AS_ERRORS)
252 target_compile_options(${elf_target} PRIVATE -Werror)
254 if (IS_RELEASE_BUILD)
255 target_compile_options(${elf_target} PRIVATE ${args_OPTIMIZATION})
256 target_link_options(${elf_target} PRIVATE ${args_OPTIMIZATION})
258 target_link_libraries(${elf_target} PRIVATE ${STM32_LINK_LIBRARIES})
259 target_link_options(${elf_target} PRIVATE ${STM32_LINK_OPTIONS} ${args_LINK_OPTIONS})
260 generate_map_file(${elf_target})
261 set_linker_script(${elf_target} ${args_LINKER_SCRIPT})
263 set(basename ${CMAKE_BINARY_DIR}/${args_FILENAME})
264 set(hex_filename ${basename}.hex)
265 add_hex_target(${args_NAME} ${elf_target} ${hex_filename})
266 set(bin_filename ${basename}.bin)
267 add_bin_target(${args_NAME}.bin ${elf_target} ${bin_filename})
269 if(args_OUTPUT_BIN_FILENAME)
270 set(${args_OUTPUT_BIN_FILENAME} ${bin_filename} PARENT_SCOPE)
272 if(args_OUTPUT_TARGET_NAME)
273 set(${args_OUTPUT_TARGET_NAME} ${elf_target} PARENT_SCOPE)
275 if(args_OUTPUT_HEX_FILENAME)
276 set(${args_OUTPUT_HEX_FILENAME} ${hex_filename} PARENT_SCOPE)
280 function(target_stm32)
281 if(NOT arm-none-eabi STREQUAL TOOLCHAIN)
284 # Parse keyword arguments
285 cmake_parse_arguments(
288 "DISABLE_MSC;BOOTLOADER"
289 # Single value arguments
290 "HSE_MHZ;LINKER_SCRIPT;NAME;OPENOCD_TARGET;OPTIMIZATION;STARTUP;SVD"
291 # Multi-value arguments
292 "COMPILE_DEFINITIONS;COMPILE_OPTIONS;INCLUDE_DIRECTORIES;LINK_OPTIONS;SOURCES;MSC_SOURCES;MSC_INCLUDE_DIRECTORIES;VCP_SOURCES;VCP_INCLUDE_DIRECTORIES"
293 # Start parsing after the known arguments
296 set(name ${args_NAME})
299 set(hse_mhz ${args_HSE_MHZ})
301 set(hse_mhz ${STM32_DEFAULT_HSE_MHZ})
304 set(target_sources ${STM32_STARTUP_DIR}/${args_STARTUP})
305 list(APPEND target_sources ${args_SOURCES})
306 file(GLOB target_c_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
307 file(GLOB target_h_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
308 list(APPEND target_sources ${target_c_sources} ${target_h_sources})
310 set(target_include_directories ${args_INCLUDE_DIRECTORIES})
312 set(target_definitions ${STM32_DEFINITIONS} ${COMMON_COMPILE_DEFINITIONS})
314 get_stm32_target_features(features "${CMAKE_CURRENT_SOURCE_DIR}" ${name})
315 set_property(TARGET ${elf_target} PROPERTY FEATURES ${features})
317 if(VCP IN_LIST features)
318 list(APPEND target_sources ${STM32_VCP_SRC} ${args_VCP_SOURCES})
319 list(APPEND target_include_directories ${args_VCP_INCLUDE_DIRECTORIES})
321 if(SDCARD IN_LIST features)
322 list(APPEND target_sources ${STM32_SDCARD_SRC} ${STM32_ASYNCFATFS_SRC})
326 if(NOT args_DISABLE_MSC AND MSC IN_LIST features)
327 list(APPEND target_include_directories ${args_MSC_INCLUDE_DIRECTORIES})
328 list(APPEND msc_sources ${STM32_MSC_SRC} ${args_MSC_SOURCES})
329 list(APPEND target_definitions USE_USB_MSC)
330 if(FLASHFS IN_LIST features)
331 list(APPEND msc_sources ${STM32_MSC_FLASH_SRC})
333 if (SDCARD IN_LIST features)
334 list(APPEND msc_sources ${STM32_MSC_SDCARD_SRC})
338 math(EXPR hse_value "${hse_mhz} * 1000000")
339 list(APPEND target_definitions "HSE_VALUE=${hse_value}")
342 list(APPEND target_definitions "MSP_UART=${MSP_UART}")
345 if(args_COMPILE_DEFINITIONS)
346 list(APPEND target_definitions ${args_COMPILE_DEFINITIONS})
349 list(APPEND target_definitions DEBUG_HARDFAULTS)
352 string(TOLOWER ${PROJECT_NAME} lowercase_project_name)
353 set(binary_name ${lowercase_project_name}_${FIRMWARE_VERSION}_${name})
354 if(DEFINED BUILD_SUFFIX AND NOT "" STREQUAL "${BUILD_SUFFIX}")
355 set(binary_name "${binary_name}_${BUILD_SUFFIX}")
359 add_stm32_executable(
361 FILENAME ${binary_name}
362 SOURCES ${target_sources} ${msc_sources} ${CMSIS_DSP_SRC} ${COMMON_SRC}
363 COMPILE_DEFINITIONS ${target_definitions}
364 COMPILE_OPTIONS ${args_COMPILE_OPTIONS}
365 INCLUDE_DIRECTORIES ${target_include_directories}
366 LINK_OPTIONS ${args_LINK_OPTIONS}
367 LINKER_SCRIPT ${args_LINKER_SCRIPT}
368 OPTIMIZATION ${args_OPTIMIZATION}
370 OUTPUT_HEX_FILENAME main_hex_filename
371 OUTPUT_TARGET_NAME main_target_name
374 set_property(TARGET ${main_target_name} PROPERTY OPENOCD_TARGET ${args_OPENOCD_TARGET})
375 set_property(TARGET ${main_target_name} PROPERTY OPENOCD_DEFAULT_INTERFACE stlink)
376 set_property(TARGET ${main_target_name} PROPERTY SVD ${args_SVD})
378 setup_firmware_target(${main_target_name} ${name} ${ARGN})
381 # Bootloader for the target
383 add_stm32_executable(
384 NAME ${name}${bl_suffix}
385 FILENAME ${binary_name}${bl_suffix}
386 SOURCES ${target_sources} ${BOOTLOADER_SOURCES}
387 COMPILE_DEFINITIONS ${target_definitions} BOOTLOADER MSP_FIRMWARE_UPDATE
388 COMPILE_OPTIONS ${args_COMPILE_OPTIONS}
389 INCLUDE_DIRECTORIES ${target_include_directories}
390 LINK_OPTIONS ${args_LINK_OPTIONS}
391 LINKER_SCRIPT ${args_LINKER_SCRIPT}${bl_suffix}
392 OPTIMIZATION ${args_OPTIMIZATION}
394 OUTPUT_BIN_FILENAME bl_bin_filename
395 OUTPUT_HEX_FILENAME bl_hex_filename
396 OUTPUT_TARGET_NAME bl_target_name
398 setup_executable(${bl_target_name} ${name})
400 # Main firmware, but for running with the bootloader
401 set(for_bl_suffix _for_bl)
402 add_stm32_executable(
403 NAME ${name}${for_bl_suffix}
404 FILENAME ${binary_name}${for_bl_suffix}
405 SOURCES ${target_sources} ${msc_sources} ${CMSIS_DSP_SRC} ${COMMON_SRC}
406 COMPILE_DEFINITIONS ${target_definitions} MSP_FIRMWARE_UPDATE
407 COMPILE_OPTIONS ${args_COMPILE_OPTIONS}
408 INCLUDE_DIRECTORIES ${target_include_directories}
409 LINK_OPTIONS ${args_LINK_OPTIONS}
410 LINKER_SCRIPT ${args_LINKER_SCRIPT}${for_bl_suffix}
411 OPTIMIZATION ${args_OPTIMIZATION}
413 OUTPUT_BIN_FILENAME for_bl_bin_filename
414 OUTPUT_HEX_FILENAME for_bl_hex_filename
415 OUTPUT_TARGET_NAME for_bl_target_name
417 setup_executable(${for_bl_target_name} ${name})
419 # Combined with bootloader and main firmware
420 set(with_bl_suffix _with_bl)
421 set(combined_hex ${CMAKE_BINARY_DIR}/${binary_name}${with_bl_suffix}.hex)
422 set(with_bl_target ${name}${with_bl_suffix})
423 add_custom_target(${with_bl_target}
424 ${CMAKE_SOURCE_DIR}/src/utils/combine_tool ${bl_bin_filename} ${for_bl_bin_filename} ${combined_hex}
425 BYPRODUCTS ${combined_hex}
427 add_dependencies(${with_bl_target} ${bl_target_name} ${for_bl_target_name})
431 set(generator_cmd "")
432 if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
433 set(generator_cmd "make")
434 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
435 set(generator_cmd "ninja")
437 if (NOT generator_cmd STREQUAL "")
438 set(clean_target "clean_${name}")
439 add_custom_target(${clean_target}
440 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
441 COMMAND ${generator_cmd} clean
442 COMMENT "Removing intermediate files for ${name}")
443 set_property(TARGET ${clean_target} PROPERTY
445 EXCLUDE_FROM_DEFAULT_BUILD 1)