2 # Copyright (c) 2010-2013, The OpenPilot Team, http://www.openpilot.org
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ifndef OPENPILOT_IS_COOL
20 $(error Top level Makefile must be used to build this target
)
23 # Define toolchain component names.
24 CC
= $(ARM_SDK_PREFIX
)gcc
25 CXX
= $(ARM_SDK_PREFIX
)g
++
26 AR
= $(ARM_SDK_PREFIX
)ar
27 OBJCOPY
= $(ARM_SDK_PREFIX
)objcopy
28 OBJDUMP
= $(ARM_SDK_PREFIX
)objdump
29 SIZE
= $(ARM_SDK_PREFIX
)size
30 NM
= $(ARM_SDK_PREFIX
)nm
31 STRIP
= $(ARM_SDK_PREFIX
)strip
35 # Add a board designator to the terse message text
36 ifeq ($(ENABLE_MSG_EXTRA
),yes
)
37 MSG_EXTRA
:= [$(BUILD_TYPE
)|
$(BOARD_SHORT_NAME
)]
43 MSG_FORMATERROR
= $(QUOTE
) Can not handle output-format
$(QUOTE
)
44 MSG_MODINIT
= $(QUOTE
) MODINIT
$(MSG_EXTRA
) $(QUOTE
)
45 MSG_SIZE
= $(QUOTE
) SIZE
$(MSG_EXTRA
) $(QUOTE
)
46 MSG_LOAD_FILE
= $(QUOTE
) BIN
/HEX
$(MSG_EXTRA
) $(QUOTE
)
47 MSG_BIN_OBJ
= $(QUOTE
) BINO
$(MSG_EXTRA
) $(QUOTE
)
48 MSG_STRIP_FILE
= $(QUOTE
) STRIP
$(MSG_EXTRA
) $(QUOTE
)
49 MSG_EXTENDED_LISTING
= $(QUOTE
) LIS
$(MSG_EXTRA
) $(QUOTE
)
50 MSG_SYMBOL_TABLE
= $(QUOTE
) NM
$(MSG_EXTRA
) $(QUOTE
)
51 MSG_ARCHIVING
= $(QUOTE
) AR
$(MSG_EXTRA
) $(QUOTE
)
52 MSG_LINKING
= $(QUOTE
) LD
$(MSG_EXTRA
) $(QUOTE
)
53 MSG_COMPILING
= $(QUOTE
) CC
$(MSG_EXTRA
) $(QUOTE
)
54 MSG_COMPILING_ARM
= $(QUOTE
) CC-ARM
$(MSG_EXTRA
) $(QUOTE
)
55 MSG_COMPILINGCXX
= $(QUOTE
) CXX
$(MSG_EXTRA
) $(QUOTE
)
56 MSG_COMPILINGCXX_ARM
= $(QUOTE
) CXX-ARM
$(MSG_EXTRA
) $(QUOTE
)
57 MSG_ASSEMBLING
= $(QUOTE
) AS
$(MSG_EXTRA
) $(QUOTE
)
58 MSG_ASSEMBLING_ARM
= $(QUOTE
) AS-ARM
$(MSG_EXTRA
) $(QUOTE
)
59 MSG_CLEANING
= $(QUOTE
) CLEAN
$(MSG_EXTRA
) $(QUOTE
)
60 MSG_ASMFROMC
= $(QUOTE
) AS
(C
) $(MSG_EXTRA
) $(QUOTE
)
61 MSG_ASMFROMC_ARM
= $(QUOTE
) AS
(C
)-ARM
$(MSG_EXTRA
) $(QUOTE
)
62 MSG_PYMITEINIT
= $(QUOTE
) PY
$(MSG_EXTRA
) $(QUOTE
)
63 MSG_OPFIRMWARE
= $(QUOTE
) OPFW
$(MSG_EXTRA
) $(QUOTE
)
64 MSG_FWINFO
= $(QUOTE
) FWINFO
$(MSG_EXTRA
) $(QUOTE
)
65 MSG_JTAG_PROGRAM
= $(QUOTE
) JTAG-PGM
$(MSG_EXTRA
) $(QUOTE
)
66 MSG_JTAG_WIPE
= $(QUOTE
) JTAG-WIPE
$(MSG_EXTRA
) $(QUOTE
)
67 MSG_PADDING
= $(QUOTE
) PADDING
$(MSG_EXTRA
) $(QUOTE
)
68 MSG_FLASH_IMG
= $(QUOTE
) FLASH_IMG
$(MSG_EXTRA
) $(QUOTE
)
70 # Function for converting an absolute path to one relative
71 # to the top of the source tree.
72 toprel
= $(subst $(realpath
$(ROOT_DIR
))/,,$(abspath
$(1)))
74 # Function to replace special characters like is done for the symbols.
75 replace_special_chars
= $(subst +,_
,$(subst ~
,_
,$(subst @
,_
,$(subst :,_
,$(subst -,_
,$(subst .
,_
,$(subst /,_
,$1)))))))
77 # Display compiler version information.
82 # Create final output file (.hex) from ELF output file.
84 @
$(ECHO
) $(MSG_LOAD_FILE
) $(call toprel
, $@
)
85 $(V1
) $(OBJCOPY
) -O ihex
$< $@
87 # Create stripped output file (.elf.stripped) from ELF output file.
89 @
$(ECHO
) $(MSG_STRIP_FILE
) $(call toprel
, $@
)
90 $(V1
) $(STRIP
) --strip-unneeded
$< -o
$@
92 # Create final output file (.bin) from ELF output file.
94 @
$(ECHO
) $(MSG_LOAD_FILE
) $(call toprel
, $@
)
95 $(V1
) $(OBJCOPY
) -O binary
$< $@
98 @
$(ECHO
) $(MSG_LOAD_FILE
) $(call toprel
, $@
)
99 $(V1
) $(OBJCOPY
) -O binary
$< $@
102 @
$(ECHO
) $(MSG_BIN_OBJ
) $(call toprel
, $@
)
103 $(V1
) $(OBJCOPY
) -I binary
-O elf32-littlearm
--binary-architecture arm \
104 --rename-section .data
=.rodata
,alloc
,load
,readonly
,data
,contents \
106 --redefine-sym _binary_
$(call replace_special_chars
,$<)_start
=_binary_start \
107 --redefine-sym _binary_
$(call replace_special_chars
,$<)_end
=_binary_end \
108 --redefine-sym _binary_
$(call replace_special_chars
,$<)_size
=_binary_size \
111 # Create extended listing file/disassambly from ELF output file.
112 # using objdump testing: option -C
114 @
$(ECHO
) $(MSG_EXTENDED_LISTING
) $(call toprel
, $@
)
115 $(V1
) $(OBJDUMP
) -h
-S
-C
-r
$< > $@
117 # Create a symbol table from ELF output file.
119 @
$(ECHO
) $(MSG_SYMBOL_TABLE
) $(call toprel
, $@
)
120 $(V1
) $(NM
) -n
$< > $@
128 @
$(ECHO
) $(MSG_SIZE
) $$(call toprel
, $$<)
132 # OpenPilot firmware image template
133 # $(1) = path to bin file
134 # $(2) = boardtype in hex
135 # $(3) = board revision in hex
139 $(1).firmware_info.c
: $(1) $(ROOT_DIR
)/flight
/templates
/firmware_info.c.template FORCE
140 @
$(ECHO
) $(MSG_FWINFO
) $$(call toprel
, $$@
)
141 $(V1
) $(VERSION_INFO
) \
142 --template
=$(ROOT_DIR
)/flight
/templates
/firmware_info.c.template \
147 --uavodir
=$(ROOT_DIR
)/shared
/uavobjectdefinition
149 $(eval
$(call COMPILE_C_TEMPLATE
, $(1).firmware_info.c
))
151 $(OUTDIR
)/$(notdir $(basename $(1))).opfw
: $(1) $(1).firmware_info.bin
152 @
$(ECHO
) $(MSG_OPFIRMWARE
) $$(call toprel
, $$@
)
153 $(V1
) $(CAT
) $(1) $(1).firmware_info.bin
> $$@
156 # Assemble: create object files from assembler source files.
157 define ASSEMBLE_TEMPLATE
158 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
159 @
$(ECHO
) $(MSG_ASSEMBLING
) $$(call toprel
, $$<)
160 $(V1
) $(CC
) -c
$(THUMB
) $$(ASFLAGS
) $$< -o
$$@
163 # Assemble: create object files from assembler source files. ARM-only
164 define ASSEMBLE_ARM_TEMPLATE
165 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
166 @
$(ECHO
) $(MSG_ASSEMBLING_ARM
) $$(call toprel
, $$<)
167 $(V1
) $(CC
) -c
$$(ASFLAGS
) $$< -o
$$@
170 # Compile: create object files from C source files.
171 define COMPILE_C_TEMPLATE
172 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
173 @
$(ECHO
) $(MSG_COMPILING
) $$(call toprel
, $$<)
174 $(V1
) $(CC
) -c
$(THUMB
) $$(CFLAGS
) $$(CONLYFLAGS
) $$(CPPFLAGS
) $$< -o
$$@
177 # Compile: create object files from C source files. ARM-only
178 define COMPILE_C_ARM_TEMPLATE
179 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
180 @
$(ECHO
) $(MSG_COMPILING_ARM
) $$(call toprel
, $$<)
181 $(V1
) $(CC
) -c
$$(CFLAGS
) $$(CONLYFLAGS
) $$(CPPFLAGS
) $$< -o
$$@
184 # Compile: create object files from C++ source files.
185 define COMPILE_CXX_TEMPLATE
186 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
187 @
$(ECHO
) $(MSG_COMPILINGCXX
) $$(call toprel
, $$<)
188 $(V1
) $(CXX
) -c
$(THUMB
) $$(CFLAGS
) $$(CPPFLAGS
) $$(CXXFLAGS
) $$< -o
$$@
191 # Compile: create object files from C++ source files. ARM-only
192 define COMPILE_CXX_ARM_TEMPLATE
193 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
194 @
$(ECHO
) $(MSG_COMPILINGCXX_ARM
) $$(call toprel
, $$<)
195 $(V1
) $(CPP
) -c
$$(CFLAGS
) $$(CPPFLAGS
) $$(CXXFLAGS
) $$< -o
$$@
198 # Archive: create ar library file from object files.
199 # $1 = library file to produce
200 # $2 = list of object files that make up the library file
201 # $3 = optional object files directory
202 define ARCHIVE_TEMPLATE
206 @
$(ECHO
) $(MSG_ARCHIVING
) $$(call toprel
, $$@
)
208 $(V1
) $(AR
) rcs
$$@
$(2)
210 # This is a workaround for Windows CreateProcess() line length
211 # limitation. It is assumed that if the object files directory
212 # is given, all object files are in that directory.
216 $(AR
) rcs
$$@
$(notdir $(2)) && \
217 cd
$$$${pwd} >/dev
/null \
222 # Link: create ELF output file from object files.
223 # $1 = elf file to produce
224 # $2 = list of object files that make up the elf file
225 # $3 = optional list of libraries to build and link
228 .PRECIOUS
: $(2) $(3)
230 @
$(ECHO
) $(MSG_LINKING
) $$(call toprel
, $$@
)
231 $(V1
) $(CC
) $(THUMB
) $$(CFLAGS
) $$(CPPFLAGS
) $(2) $(3) --output
$$@
$$(LDFLAGS
)
234 # Link: create ELF output file from object files.
235 # $1 = elf file to produce
236 # $2 = list of object files that make up the elf file
237 define LINK_CXX_TEMPLATE
239 .PRECIOUS
: $(2) $(3)
241 @
$(ECHO
) $(MSG_LINKING
) $$(call toprel
, $$@
)
242 $(V1
) $(CXX
) $(THUMB
) $$(CFLAGS
) $$(CPPFLAGS
) $$(CXXFLAGS
) $(2) $(3) --output
$$@
$$(LDFLAGS
)
245 # Compile: create assembler files from C source files. ARM/Thumb
246 define PARTIAL_COMPILE_TEMPLATE
247 $($(1):.c
=.s
) : %.s
: %.c
248 @
$(ECHO
) $(MSG_ASMFROMC
) $$(call toprel
, $$<)
249 $(V1
) $(CC
) $(THUMB
) -S
$$(CFLAGS
) $$(CONLYFLAGS
) $$< -o
$$@
252 # Compile: create assembler files from C source files. ARM only
253 define PARTIAL_COMPILE_ARM_TEMPLATE
254 $($(1):.c
=.s
) : %.s
: %.c
255 @
$(ECHO
) $(MSG_ASMFROMC_ARM
) $$(call toprel
, $$<)
256 $(V1
) $(CC
) -S
$$(CFLAGS
) $$(CONLYFLAGS
) $$< -o
$$@
259 # $(1) = Name of binary image to write
260 # $(2) = Base of flash region to write/wipe
261 # $(3) = Size of flash region to write/wipe
262 # $(4) = OpenOCD JTAG interface configuration file to use
263 # $(5) = OpenOCD configuration file to use
265 # ---------------------------------------------------------------------------
266 # Options for OpenOCD flash-programming
267 # see openocd.pdf/openocd.texi for further information
270 OOCD_JTAG_SETUP
= -d0
271 # interface and board/target settings (using the OOCD target-library here)
272 OOCD_JTAG_SETUP
+= -s
$(ROOT_DIR
)/flight
/Project
/OpenOCD
273 OOCD_JTAG_SETUP
+= -f
$(4) -f
$(5)
276 OOCD_BOARD_RESET
= -c init
278 #OOCD_BOARD_RESET += -c targets
279 # commands to prepare flash-write
280 OOCD_BOARD_RESET
+= -c
"reset halt"
284 @
$(ECHO
) $(MSG_JTAG_PROGRAM
) $$(call toprel
, $$<)
286 $$(OOCD_JTAG_SETUP
) \
287 $$(OOCD_BOARD_RESET
) \
288 -c
"flash write_image erase $$< $(2) bin" \
289 -c
"verify_image $$< $(2) bin" \
295 @
$(ECHO
) $(MSG_JTAG_WIPE
) wiping
$(3) bytes starting from
$(2)
297 $$(OOCD_JTAG_SETUP
) \
298 $$(OOCD_BOARD_RESET
) \
299 -c
"flash erase_address pad $(2) $(3)" \