LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / flight / make / firmware-defs.mk
blob48f65bb3cad07a2f8bc7bc28cfe152d38edd2432
2 # Copyright (C) 2015, The LibrePilot Project, http://www.librepilot.org
3 # Copyright (c) 2010-2013, The OpenPilot Team, http://www.openpilot.org
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ifndef FLIGHT_MAKEFILE
21 $(error Top level Makefile must be used to build this target)
22 endif
24 # Define toolchain component names.
25 CC = $(CCACHE) $(ARM_SDK_PREFIX)gcc
26 CXX = $(CCACHE) $(ARM_SDK_PREFIX)g++
27 AR = $(ARM_SDK_PREFIX)ar
28 OBJCOPY = $(ARM_SDK_PREFIX)objcopy
29 OBJDUMP = $(ARM_SDK_PREFIX)objdump
30 SIZE = $(ARM_SDK_PREFIX)size
31 NM = $(ARM_SDK_PREFIX)nm
32 STRIP = $(ARM_SDK_PREFIX)strip
34 THUMB = -mthumb
36 # Add a board designator to the terse message text
37 ifeq ($(ENABLE_MSG_EXTRA),yes)
38 MSG_EXTRA := [$(BUILD_TYPE)|$(BOARD_SHORT_NAME)]
39 else
40 MSG_EXTRA :=
41 endif
43 # Define Messages
44 MSG_FORMATERROR = $(QUOTE) Can not handle output-format$(QUOTE)
45 MSG_MODINIT = $(QUOTE) MODINIT $(MSG_EXTRA) $(QUOTE)
46 MSG_SIZE = $(QUOTE) SIZE $(MSG_EXTRA) $(QUOTE)
47 MSG_LOAD_FILE = $(QUOTE) BIN/HEX $(MSG_EXTRA) $(QUOTE)
48 MSG_BIN_OBJ = $(QUOTE) BINO $(MSG_EXTRA) $(QUOTE)
49 MSG_STRIP_FILE = $(QUOTE) STRIP $(MSG_EXTRA) $(QUOTE)
50 MSG_EXTENDED_LISTING = $(QUOTE) LIS $(MSG_EXTRA) $(QUOTE)
51 MSG_SYMBOL_TABLE = $(QUOTE) NM $(MSG_EXTRA) $(QUOTE)
52 MSG_ARCHIVING = $(QUOTE) AR $(MSG_EXTRA) $(QUOTE)
53 MSG_LINKING = $(QUOTE) LD $(MSG_EXTRA) $(QUOTE)
54 MSG_COMPILING = $(QUOTE) CC $(MSG_EXTRA) $(QUOTE)
55 MSG_COMPILING_ARM = $(QUOTE) CC-ARM $(MSG_EXTRA) $(QUOTE)
56 MSG_COMPILINGCXX = $(QUOTE) CXX $(MSG_EXTRA) $(QUOTE)
57 MSG_COMPILINGCXX_ARM = $(QUOTE) CXX-ARM $(MSG_EXTRA) $(QUOTE)
58 MSG_ASSEMBLING = $(QUOTE) AS $(MSG_EXTRA) $(QUOTE)
59 MSG_ASSEMBLING_ARM = $(QUOTE) AS-ARM $(MSG_EXTRA) $(QUOTE)
60 MSG_CLEANING = $(QUOTE) CLEAN $(MSG_EXTRA) $(QUOTE)
61 MSG_ASMFROMC = $(QUOTE) AS(C) $(MSG_EXTRA) $(QUOTE)
62 MSG_ASMFROMC_ARM = $(QUOTE) AS(C)-ARM $(MSG_EXTRA) $(QUOTE)
63 MSG_PYMITEINIT = $(QUOTE) PY $(MSG_EXTRA) $(QUOTE)
64 MSG_OPFIRMWARE = $(QUOTE) OPFW $(MSG_EXTRA) $(QUOTE)
65 MSG_FWINFO = $(QUOTE) FWINFO $(MSG_EXTRA) $(QUOTE)
66 MSG_JTAG_PROGRAM = $(QUOTE) JTAG-PGM $(MSG_EXTRA) $(QUOTE)
67 MSG_JTAG_WIPE = $(QUOTE) JTAG-WIPE $(MSG_EXTRA) $(QUOTE)
68 MSG_PADDING = $(QUOTE) PADDING $(MSG_EXTRA) $(QUOTE)
69 MSG_FLASH_IMG = $(QUOTE) FLASH_IMG $(MSG_EXTRA) $(QUOTE)
71 # Function for converting an absolute path to one relative
72 # to the top of the source tree.
73 toprel = $(subst $(realpath $(FLIGHT_ROOT_DIR))/,,$(abspath $(1)))
75 # Function to replace special characters like is done for the symbols.
76 replace_special_chars = $(subst +,_,$(subst ~,_,$(subst @,_,$(subst :,_,$(subst -,_,$(subst .,_,$(subst /,_,$1)))))))
78 # Display compiler version information.
79 .PHONY: gccversion
80 gccversion:
81 @$(CC) --version
83 # Create final output file (.hex) from ELF output file.
84 %.hex: %.elf
85 @echo $(MSG_LOAD_FILE) $(call toprel, $@)
86 $(V1) $(OBJCOPY) -O ihex $< $@
88 # Create stripped output file (.elf.stripped) from ELF output file.
89 %.elf.stripped: %.elf
90 @echo $(MSG_STRIP_FILE) $(call toprel, $@)
91 $(V1) $(STRIP) --strip-unneeded $< -o $@
93 # Create final output file (.bin) from ELF output file.
94 %.bin: %.elf
95 @echo $(MSG_LOAD_FILE) $(call toprel, $@)
96 $(V1) $(OBJCOPY) -O binary $< $@
98 %.bin: %.o
99 @echo $(MSG_LOAD_FILE) $(call toprel, $@)
100 $(V1) $(OBJCOPY) -O binary $< $@
102 %.bin.o: %.bin
103 @echo $(MSG_BIN_OBJ) $(call toprel, $@)
104 $(V1) $(OBJCOPY) -I binary -O elf32-littlearm --binary-architecture arm \
105 --rename-section .data=.rodata,alloc,load,readonly,data,contents \
106 --wildcard \
107 --redefine-sym _binary_$(call replace_special_chars,$<)_start=_binary_start \
108 --redefine-sym _binary_$(call replace_special_chars,$<)_end=_binary_end \
109 --redefine-sym _binary_$(call replace_special_chars,$<)_size=_binary_size \
110 $< $@
112 # Create extended listing file/disassambly from ELF output file.
113 # using objdump testing: option -C
114 %.lss: %.elf
115 @echo $(MSG_EXTENDED_LISTING) $(call toprel, $@)
116 $(V1) $(OBJDUMP) -h -S -C -r $< > $@
118 # Create a symbol table from ELF output file.
119 %.sym: %.elf
120 @echo $(MSG_SYMBOL_TABLE) $(call toprel, $@)
121 $(V1) $(NM) -n $< > $@
123 define SIZE_TEMPLATE
124 .PHONY: size
125 size: $(1)_size
127 .PHONY: $(1)_size
128 $(1)_size: $(1)
129 @echo $(MSG_SIZE) $$(call toprel, $$<)
130 $(V1) $(SIZE) -A $$<
131 endef
133 # OpenPilot firmware image template
134 # $(1) = path to bin file
135 # $(2) = boardtype in hex
136 # $(3) = board revision in hex
137 define OPFW_TEMPLATE
138 FORCE:
140 $(1).firmware_info.c: $(1) $(FLIGHT_ROOT_DIR)/templates/firmware_info.c.template FORCE
141 @echo $(MSG_FWINFO) $$(call toprel, $$@)
142 $(V1) $(VERSION_INFO) \
143 --template=$(FLIGHT_ROOT_DIR)/templates/firmware_info.c.template \
144 --outfile=$$@ \
145 --image=$(1) \
146 --type=$(2) \
147 --revision=$(3) \
148 --uavodir=$(FLIGHT_ROOT_DIR)/../shared/uavobjectdefinition
150 $(eval $(call COMPILE_C_TEMPLATE, $(1).firmware_info.c))
152 $(OUTDIR)/$(notdir $(basename $(1))).opfw : $(1) $(1).firmware_info.bin
153 @echo $(MSG_OPFIRMWARE) $$(call toprel, $$@)
154 $(V1) cat $(1) $(1).firmware_info.bin > $$@
155 endef
157 # Assemble: create object files from assembler source files.
158 define ASSEMBLE_TEMPLATE
159 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
160 @echo $(MSG_ASSEMBLING) $$(call toprel, $$<)
161 $(V1) $(CC) -c $(THUMB) $$(ASFLAGS) $$< -o $$@
162 endef
164 # Assemble: create object files from assembler source files. ARM-only
165 define ASSEMBLE_ARM_TEMPLATE
166 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
167 @echo $(MSG_ASSEMBLING_ARM) $$(call toprel, $$<)
168 $(V1) $(CC) -c $$(ASFLAGS) $$< -o $$@
169 endef
171 # Compile: create object files from C source files.
172 define COMPILE_C_TEMPLATE
173 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
174 @echo $(MSG_COMPILING) $$(call toprel, $$<)
175 $(V1) $(CC) -c $(THUMB) $$(CFLAGS) $$(CONLYFLAGS) $$(CPPFLAGS) $$< -o $$@
176 endef
178 # Compile: create object files from C source files. ARM-only
179 define COMPILE_C_ARM_TEMPLATE
180 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
181 @echo $(MSG_COMPILING_ARM) $$(call toprel, $$<)
182 $(V1) $(CC) -c $$(CFLAGS) $$(CONLYFLAGS) $$(CPPFLAGS) $$< -o $$@
183 endef
185 # Compile: create object files from C++ source files.
186 define COMPILE_CXX_TEMPLATE
187 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
188 @echo $(MSG_COMPILINGCXX) $$(call toprel, $$<)
189 $(V1) $(CXX) -c $(THUMB) $$(CFLAGS) $$(CPPFLAGS) $$(CXXFLAGS) $$< -o $$@
190 endef
192 # Compile: create object files from C++ source files. ARM-only
193 define COMPILE_CXX_ARM_TEMPLATE
194 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
195 @echo $(MSG_COMPILINGCXX_ARM) $$(call toprel, $$<)
196 $(V1) $(CPP) -c $$(CFLAGS) $$(CPPFLAGS) $$(CXXFLAGS) $$< -o $$@
197 endef
199 # Archive: create ar library file from object files.
200 # $1 = library file to produce
201 # $2 = list of object files that make up the library file
202 # $3 = optional object files directory
203 define ARCHIVE_TEMPLATE
204 .SECONDARY : $(1)
205 .PRECIOUS : $(2)
206 $(1): $(2)
207 @echo $(MSG_ARCHIVING) $$(call toprel, $$@)
208 ifeq ($(3),)
209 $(V1) $(AR) rcs $$@ $(2)
210 else
211 # This is a workaround for Windows CreateProcess() line length
212 # limitation. It is assumed that if the object files directory
213 # is given, all object files are in that directory.
214 $(V1) ( \
215 pwd=`pwd` && \
216 cd $(3) && \
217 $(AR) rcs $$@ $(notdir $(2)) && \
218 cd $$$${pwd} >/dev/null \
220 endif
221 endef
223 # Link: create ELF output file from object files.
224 # $1 = elf file to produce
225 # $2 = list of object files that make up the elf file
226 # $3 = optional list of libraries to build and link
227 define LINK_TEMPLATE
228 .SECONDARY : $(1)
229 .PRECIOUS : $(2) $(3)
230 $(1): $(2) $(3)
231 @echo $(MSG_LINKING) $$(call toprel, $$@)
232 $(V1) $(CC) $(THUMB) $$(CFLAGS) $$(CPPFLAGS) $(2) $(3) --output $$@ $$(LDFLAGS)
233 endef
235 # Link: create ELF output file from object files.
236 # $1 = elf file to produce
237 # $2 = list of object files that make up the elf file
238 define LINK_CXX_TEMPLATE
239 .SECONDARY : $(1)
240 .PRECIOUS : $(2) $(3)
241 $(1): $(2) $(3)
242 @echo $(MSG_LINKING) $$(call toprel, $$@)
243 $(V1) $(CXX) $(THUMB) $$(CFLAGS) $$(CPPFLAGS) $$(CXXFLAGS) $(2) $(3) --output $$@ $$(LDFLAGS)
244 endef
246 # Compile: create assembler files from C source files. ARM/Thumb
247 define PARTIAL_COMPILE_TEMPLATE
248 $($(1):.c=.s) : %.s : %.c
249 @echo $(MSG_ASMFROMC) $$(call toprel, $$<)
250 $(V1) $(CC) $(THUMB) -S $$(CFLAGS) $$(CONLYFLAGS) $$< -o $$@
251 endef
253 # Compile: create assembler files from C source files. ARM only
254 define PARTIAL_COMPILE_ARM_TEMPLATE
255 $($(1):.c=.s) : %.s : %.c
256 @echo $(MSG_ASMFROMC_ARM) $$(call toprel, $$<)
257 $(V1) $(CC) -S $$(CFLAGS) $$(CONLYFLAGS) $$< -o $$@
258 endef
260 # $(1) = Name of binary image to write
261 # $(2) = Base of flash region to write/wipe
262 # $(3) = Size of flash region to write/wipe
263 # $(4) = OpenOCD JTAG interface configuration file to use
264 # $(5) = OpenOCD configuration file to use
265 define JTAG_TEMPLATE
266 # ---------------------------------------------------------------------------
267 # Options for OpenOCD flash-programming
268 # see openocd.pdf/openocd.texi for further information
270 # debug level
271 OOCD_JTAG_SETUP = -d0
272 # interface and board/target settings (using the OOCD target-library here)
273 OOCD_JTAG_SETUP += -s $(FLIGHT_ROOT_DIR)/Project/OpenOCD
274 OOCD_JTAG_SETUP += -f $(4) -f $(5)
276 # initialize
277 OOCD_BOARD_RESET = -c init
278 # show the targets
279 #OOCD_BOARD_RESET += -c targets
280 # commands to prepare flash-write
281 OOCD_BOARD_RESET += -c "reset halt"
283 .PHONY: program
284 program: $(1)
285 @echo $(MSG_JTAG_PROGRAM) $$(call toprel, $$<)
286 $(V1) $(OPENOCD) \
287 $$(OOCD_JTAG_SETUP) \
288 $$(OOCD_BOARD_RESET) \
289 -c "flash write_image erase $$< $(2) bin" \
290 -c "verify_image $$< $(2) bin" \
291 -c "reset run" \
292 -c "shutdown"
294 .PHONY: wipe
295 wipe:
296 @echo $(MSG_JTAG_WIPE) wiping $(3) bytes starting from $(2)
297 $(V1) $(OPENOCD) \
298 $$(OOCD_JTAG_SETUP) \
299 $$(OOCD_BOARD_RESET) \
300 -c "flash erase_address pad $(2) $(3)" \
301 -c "reset run" \
302 -c "shutdown"
303 endef