1 include(at32-bootloader)
4 include(CMakeParseArguments)
6 option(DEBUG_HARDFAULTS "Enable debugging of hard faults via custom handler")
7 option(SEMIHOSTING "Enable semihosting")
9 message("-- DEBUG_HARDFAULTS: ${DEBUG_HARDFAULTS}, SEMIHOSTING: ${SEMIHOSTING}")
11 set(CMSIS_DIR "${MAIN_LIB_DIR}/lib/main/AT32F43x/Drivers/CMSIS")
12 set(CMSIS_INCLUDE_DIR "${CMSIS_DIR}/cm4/core_support")
14 set(CMSIS_DSP_DIR "${MAIN_LIB_DIR}/main/CMSIS/DSP")
15 set(CMSIS_DSP_INCLUDE_DIR "${CMSIS_DSP_DIR}/Include")
18 BasicMathFunctions/arm_scale_f32.c
19 BasicMathFunctions/arm_sub_f32.c
20 BasicMathFunctions/arm_mult_f32.c
21 BasicMathFunctions/arm_offset_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
30 StatisticsFunctions/arm_rms_f32.c
31 StatisticsFunctions/arm_std_f32.c
32 StatisticsFunctions/arm_mean_f32.c
34 list(TRANSFORM CMSIS_DSP_SRC PREPEND "${CMSIS_DSP_DIR}/Source/")
36 set(AT32_STARTUP_DIR "${MAIN_SRC_DIR}/startup")
38 main_sources(AT32_VCP_SRC
39 drivers/serial_usb_vcp_at32f43x.c
42 # SDCARD not supported yet
43 main_sources(AT32_SDCARD_SRC
44 drivers/sdcard/sdcard.c
45 drivers/sdcard/sdcard_spi.c
46 drivers/sdcard/sdcard_sdio.c
47 drivers/sdcard/sdcard_standard.c
50 # XXX: This code is not STM32 specific
51 main_sources(AT32_ASYNCFATFS_SRC
52 io/asyncfatfs/asyncfatfs.c
53 io/asyncfatfs/fat_standard.c
56 main_sources(AT32_MSC_SRC
63 "${CMSIS_INCLUDE_DIR}"
64 "${CMSIS_DSP_INCLUDE_DIR}"
65 "${MAIN_SRC_DIR}/target"
70 set(AT32_DEFAULT_HSE_MHZ 8)
71 set(AT32_LINKER_DIR "${MAIN_SRC_DIR}/target/link")
72 set(AT32_COMPILE_OPTIONS
78 set(AT32_LINK_LIBRARIES
84 list(APPEND AT32_LINK_LIBRARIES --specs=rdimon.specs -lrdimon)
85 list(APPEND AT32_DEFINITIONS SEMIHOSTING)
87 list(APPEND AT32_LINK_LIBRARIES -lnosys)
95 -Wl,-L${AT32_LINKER_DIR}
97 -Wl,--no-wchar-size-warning
98 -Wl,--print-memory-usage
99 -Wl,--no-warn-rwx-segments
101 # Get target features
102 macro(get_at32_target_features output_var dir target_name)
103 execute_process(COMMAND "${CMAKE_C_COMPILER}" -E -dD -D${ARGV2} "${ARGV1}/target.h"
104 ERROR_VARIABLE _errors
105 RESULT_VARIABLE _result
106 OUTPUT_STRIP_TRAILING_WHITESPACE
107 OUTPUT_VARIABLE _contents)
109 if(NOT _result EQUAL 0)
110 message(FATAL_ERROR "error extracting features for AT32 target ${ARGV2}: ${_errors}")
113 string(REGEX MATCH "#define[\t ]+USE_VCP" HAS_VCP ${_contents})
115 list(APPEND ${ARGV0} VCP)
117 string(REGEX MATCH "define[\t ]+USE_FLASHFS" HAS_FLASHFS ${_contents})
119 list(APPEND ${ARGV0} FLASHFS)
121 string(REGEX MATCH "define[\t ]+USE_SDCARD" HAS_SDCARD ${_contents})
123 list(APPEND ${ARGV0} SDCARD)
124 string(REGEX MATCH "define[\t ]+USE_SDCARD_SDIO" HAS_SDIO ${_contents})
126 list(APPEND ${ARGV0} SDIO)
129 if(HAS_FLASHFS OR HAS_SDCARD)
130 list(APPEND ${ARGV0} MSC)
134 function(get_at32_flash_size out size)
135 # 4: 16, 6: 32, 8: 64, B: 128, C: 256, D: 384, E: 512, F: 768, G: 1024, H: 1536, I: 2048 KiB
136 string(TOUPPER ${size} s)
137 if(${s} STREQUAL "4")
138 set(${out} 16 PARENT_SCOPE)
141 if(${s} STREQUAL "6")
142 set(${out} 32 PARENT_SCOPE)
145 if(${s} STREQUAL "8")
146 set(${out} 64 PARENT_SCOPE)
149 if(${s} STREQUAL "8")
150 set(${out} 64 PARENT_SCOPE)
153 if(${s} STREQUAL "B")
154 set(${out} 128 PARENT_SCOPE)
157 if(${s} STREQUAL "C")
158 set(${out} 256 PARENT_SCOPE)
161 if(${s} STREQUAL "D")
162 set(${out} 384 PARENT_SCOPE)
165 if(${s} STREQUAL "E")
166 set(${out} 512 PARENT_SCOPE)
169 if(${s} STREQUAL "F")
170 set(${out} 768 PARENT_SCOPE)
173 if(${s} STREQUAL "G")
174 set(${out} 1024 PARENT_SCOPE)
177 if(${s} STREQUAL "H")
178 set(${out} 1536 PARENT_SCOPE)
181 if(${s} STREQUAL "I")
182 set(${out} 2048 PARENT_SCOPE)
187 function(add_hex_target name exe hex)
188 add_custom_target(${name} ALL
189 cmake -E env PATH="$ENV{PATH}"
190 # TODO: Overriding the start address with --set-start 0x08000000
191 # seems to be required due to some incorrect assumptions about .hex
192 # files in the configurator. Verify wether that's the case and fix
193 # the bug in configurator or delete this comment.
194 ${CMAKE_OBJCOPY} -Oihex --set-start 0x08000000 $<TARGET_FILE:${exe}> ${hex}
199 function(add_bin_target name exe bin)
200 add_custom_target(${name}
201 cmake -E env PATH="$ENV{PATH}"
202 ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${exe}> ${bin}
207 function(generate_map_file target)
208 if(CMAKE_VERSION VERSION_LESS 3.15)
209 set(map "$<TARGET_FILE:${target}>.map")
211 set(map "$<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.map")
213 target_link_options(${target} PRIVATE "-Wl,-Map,${map}")
216 function(set_linker_script target script)
217 set(script_path ${AT32_LINKER_DIR}/${args_LINKER_SCRIPT}.ld)
218 if(NOT EXISTS ${script_path})
219 message(FATAL_ERROR "linker script ${script_path} doesn't exist")
221 set_target_properties(${target} PROPERTIES LINK_DEPENDS ${script_path})
222 target_link_options(${elf_target} PRIVATE -T${script_path})
225 function(add_at32_executable)
226 cmake_parse_arguments(
230 # Single value arguments
231 "FILENAME;NAME;OPTIMIZATION;OUTPUT_BIN_FILENAME;OUTPUT_HEX_FILENAME;OUTPUT_TARGET_NAME"
232 # Multi-value arguments
233 "COMPILE_DEFINITIONS;COMPILE_OPTIONS;INCLUDE_DIRECTORIES;LINK_OPTIONS;LINKER_SCRIPT;SOURCES"
234 # Start parsing after the known arguments
237 set(elf_target ${args_NAME}.elf)
238 add_executable(${elf_target})
239 target_sources(${elf_target} PRIVATE ${args_SOURCES})
240 target_include_directories(${elf_target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${args_INCLUDE_DIRECTORIES} ${AT32_INCLUDE_DIRS})
241 target_compile_definitions(${elf_target} PRIVATE ${args_COMPILE_DEFINITIONS})
242 target_compile_options(${elf_target} PRIVATE ${AT32_COMPILE_OPTIONS} ${args_COMPILE_OPTIONS})
243 if(WARNINGS_AS_ERRORS)
244 target_compile_options(${elf_target} PRIVATE -Werror)
246 if (IS_RELEASE_BUILD)
247 target_compile_options(${elf_target} PRIVATE ${args_OPTIMIZATION})
248 target_link_options(${elf_target} PRIVATE ${args_OPTIMIZATION})
250 target_link_libraries(${elf_target} PRIVATE ${AT32_LINK_LIBRARIES})
251 target_link_options(${elf_target} PRIVATE ${AT32_LINK_OPTIONS} ${args_LINK_OPTIONS})
252 generate_map_file(${elf_target})
253 set_linker_script(${elf_target} ${args_LINKER_SCRIPT})
255 set(basename ${CMAKE_BINARY_DIR}/${args_FILENAME})
256 set(hex_filename ${basename}.hex)
257 add_hex_target(${args_NAME} ${elf_target} ${hex_filename})
258 set(bin_filename ${basename}.bin)
259 add_bin_target(${args_NAME}.bin ${elf_target} ${bin_filename})
261 if(args_OUTPUT_BIN_FILENAME)
262 set(${args_OUTPUT_BIN_FILENAME} ${bin_filename} PARENT_SCOPE)
264 if(args_OUTPUT_TARGET_NAME)
265 set(${args_OUTPUT_TARGET_NAME} ${elf_target} PARENT_SCOPE)
267 if(args_OUTPUT_HEX_FILENAME)
268 set(${args_OUTPUT_HEX_FILENAME} ${hex_filename} PARENT_SCOPE)
272 # Main function of AT32
273 function(target_at32)
274 if(NOT arm-none-eabi STREQUAL TOOLCHAIN)
277 # Parse keyword arguments
278 cmake_parse_arguments(
281 "DISABLE_MSC;BOOTLOADER"
282 # Single value arguments
283 "HSE_MHZ;LINKER_SCRIPT;NAME;OPENOCD_TARGET;OPTIMIZATION;STARTUP;SVD"
284 # Multi-value arguments
285 "COMPILE_DEFINITIONS;COMPILE_OPTIONS;INCLUDE_DIRECTORIES;LINK_OPTIONS;SOURCES;MSC_SOURCES;MSC_INCLUDE_DIRECTORIES;VCP_SOURCES;VCP_INCLUDE_DIRECTORIES"
286 # Start parsing after the known arguments
289 set(name ${args_NAME})
292 set(hse_mhz ${args_HSE_MHZ})
294 set(hse_mhz ${AT32_DEFAULT_HSE_MHZ})
297 set(target_sources ${AT32_STARTUP_DIR}/${args_STARTUP})
298 list(APPEND target_sources ${args_SOURCES})
299 file(GLOB target_c_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
300 file(GLOB target_h_sources "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
301 list(APPEND target_sources ${target_c_sources} ${target_h_sources})
303 set(target_include_directories ${args_INCLUDE_DIRECTORIES})
305 set(target_definitions ${AT32_DEFINITIONS} ${COMMON_COMPILE_DEFINITIONS})
307 get_at32_target_features(features "${CMAKE_CURRENT_SOURCE_DIR}" ${name})
308 set_property(TARGET ${elf_target} PROPERTY FEATURES ${features})
310 if(VCP IN_LIST features)
311 list(APPEND target_sources ${AT32_VCP_SRC} ${args_VCP_SOURCES})
312 list(APPEND target_include_directories ${args_VCP_INCLUDE_DIRECTORIES})
314 if(SDCARD IN_LIST features)
315 list(APPEND target_sources ${AT32_SDCARD_SRC} ${AT32_ASYNCFATFS_SRC})
319 if(NOT args_DISABLE_MSC AND MSC IN_LIST features)
320 list(APPEND target_include_directories ${args_MSC_INCLUDE_DIRECTORIES})
321 list(APPEND msc_sources ${AT32_MSC_SRC} ${args_MSC_SOURCES})
322 list(APPEND target_definitions USE_USB_MSC)
323 if(FLASHFS IN_LIST features)
324 list(APPEND msc_sources ${AT32_MSC_FLASH_SRC})
326 if (SDCARD IN_LIST features)
327 list(APPEND msc_sources ${AT32_MSC_SDCARD_SRC})
331 math(EXPR hse_value "${hse_mhz} * 1000000")
332 list(APPEND target_definitions "HSE_VALUE=${hse_value}")
335 list(APPEND target_definitions "MSP_UART=${MSP_UART}")
338 if(args_COMPILE_DEFINITIONS)
339 list(APPEND target_definitions ${args_COMPILE_DEFINITIONS})
342 list(APPEND target_definitions DEBUG_HARDFAULTS)
345 string(TOLOWER ${PROJECT_NAME} lowercase_project_name)
346 set(binary_name ${lowercase_project_name}_${FIRMWARE_VERSION}_${name})
347 if(DEFINED BUILD_SUFFIX AND NOT "" STREQUAL "${BUILD_SUFFIX}")
348 set(binary_name "${binary_name}_${BUILD_SUFFIX}")
354 FILENAME ${binary_name}
355 SOURCES ${target_sources} ${msc_sources} ${CMSIS_DSP_SRC} ${COMMON_SRC}
356 COMPILE_DEFINITIONS ${target_definitions}
357 COMPILE_OPTIONS ${args_COMPILE_OPTIONS}
358 INCLUDE_DIRECTORIES ${target_include_directories}
359 LINK_OPTIONS ${args_LINK_OPTIONS}
360 LINKER_SCRIPT ${args_LINKER_SCRIPT}
361 OPTIMIZATION ${args_OPTIMIZATION}
363 OUTPUT_BIN_FILENAME main_bin_filename
364 OUTPUT_HEX_FILENAME main_hex_filename
365 OUTPUT_TARGET_NAME main_target_name
369 set_property(TARGET ${main_target_name} PROPERTY OPENOCD_TARGET ${args_OPENOCD_TARGET})
370 set_property(TARGET ${main_target_name} PROPERTY OPENOCD_DEFAULT_INTERFACE atlink)
371 set_property(TARGET ${main_target_name} PROPERTY SVD ${args_SVD})
373 setup_firmware_target(${main_target_name} ${name} ${ARGN})
376 # Bootloader for the target
379 NAME ${name}${bl_suffix}
380 FILENAME ${binary_name}${bl_suffix}
381 SOURCES ${target_sources} ${BOOTLOADER_SOURCES}
382 COMPILE_DEFINITIONS ${target_definitions} BOOTLOADER MSP_FIRMWARE_UPDATE
383 COMPILE_OPTIONS ${args_COMPILE_OPTIONS}
384 INCLUDE_DIRECTORIES ${target_include_directories}
385 LINK_OPTIONS ${args_LINK_OPTIONS}
386 LINKER_SCRIPT ${args_LINKER_SCRIPT}${bl_suffix}
387 OPTIMIZATION ${args_OPTIMIZATION}
389 OUTPUT_BIN_FILENAME bl_bin_filename
390 OUTPUT_HEX_FILENAME bl_hex_filename
391 OUTPUT_TARGET_NAME bl_target_name
393 setup_executable(${bl_target_name} ${name})
395 # Main firmware, but for running with the bootloader
396 set(for_bl_suffix _for_bl)
398 NAME ${name}${for_bl_suffix}
399 FILENAME ${binary_name}${for_bl_suffix}
400 SOURCES ${target_sources} ${msc_sources} ${CMSIS_DSP_SRC} ${COMMON_SRC}
401 COMPILE_DEFINITIONS ${target_definitions} MSP_FIRMWARE_UPDATE
402 COMPILE_OPTIONS ${args_COMPILE_OPTIONS}
403 INCLUDE_DIRECTORIES ${target_include_directories}
404 LINK_OPTIONS ${args_LINK_OPTIONS}
405 LINKER_SCRIPT ${args_LINKER_SCRIPT}${for_bl_suffix}
406 OPTIMIZATION ${args_OPTIMIZATION}
408 OUTPUT_BIN_FILENAME for_bl_bin_filename
409 OUTPUT_HEX_FILENAME for_bl_hex_filename
410 OUTPUT_TARGET_NAME for_bl_target_name
412 setup_executable(${for_bl_target_name} ${name})
414 # Combined with bootloader and main firmware
415 set(with_bl_suffix _with_bl)
416 set(combined_hex ${CMAKE_BINARY_DIR}/${binary_name}${with_bl_suffix}.hex)
417 set(with_bl_target ${name}${with_bl_suffix})
418 add_custom_target(${with_bl_target}
419 ${CMAKE_SOURCE_DIR}/src/utils/combine_tool ${bl_bin_filename} ${for_bl_bin_filename} ${combined_hex}
420 BYPRODUCTS ${combined_hex}
422 add_dependencies(${with_bl_target} ${bl_target_name} ${for_bl_target_name})
426 set(generator_cmd "")
427 if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
428 set(generator_cmd "make")
429 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
430 set(generator_cmd "ninja")
432 if (NOT generator_cmd STREQUAL "")
433 set(clean_target "clean_${name}")
434 add_custom_target(${clean_target}
435 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
436 COMMAND ${generator_cmd} clean
437 COMMENT "Removing intermediate files for ${name}")
438 set_property(TARGET ${clean_target} PROPERTY
440 EXCLUDE_FROM_DEFAULT_BUILD 1)