-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstm32-toolchain.cmake
155 lines (135 loc) · 7.22 KB
/
stm32-toolchain.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# @author Alexander Vassilev
# @copyright BSD License
# CMake toolchain for arm-none-eabi-gcc and STM32
cmake_minimum_required(VERSION 3.0)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(ENV_SCRIPTS_DIR "${CMAKE_CURRENT_LIST_DIR}")
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-as)
set(CMAKE_LINKER arm-none-eabi-ld)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-common -mcpu=cortex-m3 -mthumb -nostartfiles -Wall" CACHE STRING "")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS} -std=c++14 -fno-exceptions -fno-rtti -fno-use-cxa-atexit -fno-threadsafe-statics" CACHE STRING "")
set(CMAKE_C_FLAGS_DEBUG "-g -O0" CACHE STRING "")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0" CACHE STRING "")
#defaults to 1 but can be overridden by commandline, in which case
#this statement has no effect
set(optUseOpencm3 1 CACHE BOOL "Use the libopencm3 platform")
if (optUseOpencm3)
message(STATUS "Using OpenCM3 framework")
set(CMAKE_SYSROOT "${CMAKE_CURRENT_LIST_DIR}/sysroot")
set(optUseOpencm3 1 CACHE BOOL "Use the libopencm3 platform" FORCE)
set(optChipFamily STM32F1 CACHE STRING "Chip family for opencm3")
set_property(CACHE optChipFamily PROPERTY STRINGS STM32F0 STM32F1 STM32F2
STM32F3 STM32F4 STM32F7 STM32L0 STM32L1 STM32L4)
string(SUBSTRING "${optChipFamily}" 5 2 stm32model_uc)
string(TOLOWER "${stm32model_uc}" stm32model)
set(ldscriptBaseDir "${CMAKE_CURRENT_LIST_DIR}/libopencm3/lib/stm32")
set(modeldir "${ldscriptBaseDir}/${stm32model}")
file(GLOB ldscripts "${modeldir}/stm32*.ld")
set(optLinkScript "${ldscriptBaseDir}/f1/stm32f103xb.ld" CACHE STRING "Linker script")
set_property(CACHE optLinkScript PROPERTY STRINGS ${ldscripts} "${CMAKE_CURRENT_SOURCE_DIR}/stm32.ld")
set(linkDirs "-L${CMAKE_CURRENT_LIST_DIR}/libopencm3/lib")
link_libraries("opencm3_stm32${stm32model}")
# We cannot use link_directories because of a CMake quirk - it does not
# get applied in most cases (but it does in some, probably if it is
# executed during compiler instrospection CMake decides that the path is
# built into the compiler driver and does not apply it).
# see https://public.kitware.com/Bug/view.php?id=16074
include_directories("${CMAKE_CURRENT_LIST_DIR}/libopencm3/include")
else()
set(optUseOpencm3 0 CACHE BOOL "Use the libopencm3 platform" FORCE)
set(CMAKE_SYSROOT "${CMAKE_CURRENT_LIST_DIR}/sysroot-baremetal")
set(defaultLinkScript "${CMAKE_CURRENT_LIST_DIR}/sysroot/stm32.ld")
set(optChipFamily STM32F10X_MD CACHE STRING "Chip family for platform headers")
set_property(CACHE optChipFamily PROPERTY STRINGS STM32F10X_LD STM32F10X_LD_VL
STM32F10X_MD STM32F10X_MD_VL STM32F10X_HD STM32F10X_HD_VL STM32F10X_XL STM32F10X_CL)
set(optLinkScript "${defaultLinkScript}" CACHE PATH "Linker script" FORCE)
endif()
set(optStdioLibcInDebug 0 CACHE BOOL
"In DEBUG mode, link to a version of the standard C library that has stdio and printf(), and supports semihosting.\n\
This has a somewhat large footprint - if you just need printing via semihosting,\n\
you can just use tprint()")
set(optStdioLibcInRelease 0 CACHE BOOL
"In RELEASE mode, use the stdio-enabled standard C library")
set(optStdioLibcInMinSizeRel 0 CACHE BOOL
"In MinSizeRelease mode, use the stdio-enabled standard C library")
set(optCustomOffset 0 CACHE STRING "Custom offset of the .text section")
add_definitions(-DCHIP_TYPE=STM32 -D${optChipFamily})
include_directories("${CMAKE_CURRENT_LIST_DIR}/stm32++/include")
if (optCustomOffset)
set(linkerOffsetArg "-Ttext=${optCustomOffset}")
endif()
set(exeLinkerFlags "-nostartfiles -T${optLinkScript} ${linkerOffsetArg} ${linkDirs}")
if (NOT "${exeLinkerFlags}" STREQUAL "${CMAKE_EXE_LINKER_FLAGS}")
message(STATUS "EXE linker flags changed")
UNSET(CMAKE_EXE_LINKER_FLAGS CACHE)
set(CMAKE_EXE_LINKER_FLAGS "${exeLinkerFlags}" CACHE STRING "")
endif()
if (optStdioLibcInDebug)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "--specs=rdimon.specs -lc" CACHE STRING "" FORCE)
else()
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "--specs=nosys.specs" CACHE STRING "" FORCE)
endif()
if (optStdioLibcInRelease)
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "--specs=rdimon.specs -lc" CACHE STRING "" FORCE)
else()
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "--specs=nosys.specs" CACHE STRING "" FORCE)
endif()
if (optStdioLibcInMinSizeRel)
set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "--specs=rdimon.specs -lc" CACHE STRING "" FORCE)
else()
set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "--specs=nosys.specs" CACHE STRING "" FORCE)
endif()
set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# Default to debug build
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type")
set_Property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release MinSizeRel RelWithDebInfo)
# Utilities to facilitate user CMakeLists
if (optUseOpencm3)
set(STM32PP_SRCPATH "${ENV_SCRIPTS_DIR}/stm32++/src")
set(STM32PP_SRCS
"${STM32PP_SRCPATH}/semihosting.cpp"
"${STM32PP_SRCPATH}/printSink.cpp"
"${STM32PP_SRCPATH}/tsnprintf.cpp"
)
endif()
function(stm32_create_utility_targets imgname)
set_target_properties(${imgname} PROPERTIES LINK_DEPENDS "${optLinkScript}")
string(REGEX REPLACE "\\.[^.]*$" "" imgnameNoExt "${imgname}")
if (optCustomOffset)
set(binName "${imgnameNoExt}_${optCustomOffset}.bin")
else()
set(binName "${imgnameNoExt}.bin")
endif()
add_custom_target(bin bash -c "arm-none-eabi-objcopy -O binary ./${imgname} ./${binName}" DEPENDS "${imgname}")
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "./${binName}")
if (optCustomOffset)
add_custom_target(flash bash -c "${ENV_SCRIPTS_DIR}/flash.sh -o ${optCustomOffset} ./${binName}" DEPENDS bin)
else()
add_custom_target(flash bash -c "${ENV_SCRIPTS_DIR}/flash.sh ./${imgname}" DEPENDS "${imgname}")
endif()
# remove all derived firmware files before rebuilding (or before linking if PRE_BUILD is not supported)
add_custom_command(TARGET ${imgname} PRE_BUILD COMMAND rm -vf "./${imgnameNoExt}.bin" "./${imgnameNoExt}_0x????????.bin")
# debugger support
add_custom_target(gdb
arm-none-eabi-gdb -ex 'file ${CMAKE_CURRENT_BINARY_DIR}/${imgname}'
-ex 'directory ${CMAKE_CURRENT_SOURCE_DIR}'
-ex 'target remote localhost:3333'
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS "${imgname}")
add_custom_target(wflash
COMMAND echo "Flashing over network (verify OFF), please wait..."
COMMAND curl -i "http://stm32.local:80/fileflash?file=${imgnameNoExt}.bin&verify=0" -H "Content-Type: application/octet-stream" -H "Expect:" --data-binary @./${imgnameNoExt}.bin --silent --output /dev/null --show-error --fail
VERBATIM
DEPENDS bin)
add_custom_target(wflashv
COMMAND echo "Flashing over network (verify ON), please wait..."
COMMAND curl -i "http://stm32.local:80/fileflash?file=${imgnameNoExt}.bin&verify=1" -H "Content-Type: application/octet-stream" -H "Expect:" --data-binary @./${imgnameNoExt}.bin --silent --output /dev/null --show-error --fail
VERBATIM
DEPENDS bin)
endfunction()