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 # Display compiler version information.
79 # Create final output file (.hex) from ELF output file.
81 @
$(ECHO
) $(MSG_LOAD_FILE
) $(call toprel
, $@
)
82 $(V1
) $(OBJCOPY
) -O ihex
$< $@
84 # Create stripped output file (.elf.stripped) from ELF output file.
86 @
$(ECHO
) $(MSG_STRIP_FILE
) $(call toprel
, $@
)
87 $(V1
) $(STRIP
) --strip-unneeded
$< -o
$@
89 # Create final output file (.bin) from ELF output file.
91 @
$(ECHO
) $(MSG_LOAD_FILE
) $(call toprel
, $@
)
92 $(V1
) $(OBJCOPY
) -O binary
$< $@
95 @
$(ECHO
) $(MSG_LOAD_FILE
) $(call toprel
, $@
)
96 $(V1
) $(OBJCOPY
) -O binary
$< $@
99 @
$(ECHO
) $(MSG_BIN_OBJ
) $(call toprel
, $@
)
100 $(V1
) $(OBJCOPY
) -I binary
-O elf32-littlearm
--binary-architecture arm \
101 --rename-section .data
=.rodata
,alloc
,load
,readonly
,data
,contents \
103 --redefine-sym _binary_
$(subst :,_
,$(subst -,_
,$(subst .
,_
,$(subst /,_
,$<))))_start
=_binary_start \
104 --redefine-sym _binary_
$(subst :,_
,$(subst -,_
,$(subst .
,_
,$(subst /,_
,$<))))_end
=_binary_end \
105 --redefine-sym _binary_
$(subst :,_
,$(subst -,_
,$(subst .
,_
,$(subst /,_
,$<))))_size
=_binary_size \
108 # Create extended listing file/disassambly from ELF output file.
109 # using objdump testing: option -C
111 @
$(ECHO
) $(MSG_EXTENDED_LISTING
) $(call toprel
, $@
)
112 $(V1
) $(OBJDUMP
) -h
-S
-C
-r
$< > $@
114 # Create a symbol table from ELF output file.
116 @
$(ECHO
) $(MSG_SYMBOL_TABLE
) $(call toprel
, $@
)
117 $(V1
) $(NM
) -n
$< > $@
125 @
$(ECHO
) $(MSG_SIZE
) $$(call toprel
, $$<)
129 # OpenPilot firmware image template
130 # $(1) = path to bin file
131 # $(2) = boardtype in hex
132 # $(3) = board revision in hex
136 $(1).firmware_info.c
: $(1) $(ROOT_DIR
)/flight
/templates
/firmware_info.c.template FORCE
137 @
$(ECHO
) $(MSG_FWINFO
) $$(call toprel
, $$@
)
138 $(V1
) $(VERSION_INFO
) \
139 --template
=$(ROOT_DIR
)/flight
/templates
/firmware_info.c.template \
144 --uavodir
=$(ROOT_DIR
)/shared
/uavobjectdefinition
146 $(eval
$(call COMPILE_C_TEMPLATE
, $(1).firmware_info.c
))
148 $(OUTDIR
)/$(notdir $(basename $(1))).opfw
: $(1) $(1).firmware_info.bin
149 @
$(ECHO
) $(MSG_OPFIRMWARE
) $$(call toprel
, $$@
)
150 $(V1
) $(CAT
) $(1) $(1).firmware_info.bin
> $$@
153 # Assemble: create object files from assembler source files.
154 define ASSEMBLE_TEMPLATE
155 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
156 @
$(ECHO
) $(MSG_ASSEMBLING
) $$(call toprel
, $$<)
157 $(V1
) $(CC
) -c
$(THUMB
) $$(ASFLAGS
) $$< -o
$$@
160 # Assemble: create object files from assembler source files. ARM-only
161 define ASSEMBLE_ARM_TEMPLATE
162 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
163 @
$(ECHO
) $(MSG_ASSEMBLING_ARM
) $$(call toprel
, $$<)
164 $(V1
) $(CC
) -c
$$(ASFLAGS
) $$< -o
$$@
167 # Compile: create object files from C source files.
168 define COMPILE_C_TEMPLATE
169 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
170 @
$(ECHO
) $(MSG_COMPILING
) $$(call toprel
, $$<)
171 $(V1
) $(CC
) -c
$(THUMB
) $$(CFLAGS
) $$(CONLYFLAGS
) $$< -o
$$@
174 # Compile: create object files from C source files. ARM-only
175 define COMPILE_C_ARM_TEMPLATE
176 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
177 @
$(ECHO
) $(MSG_COMPILING_ARM
) $$(call toprel
, $$<)
178 $(V1
) $(CC
) -c
$$(CFLAGS
) $$(CONLYFLAGS
) $$< -o
$$@
181 # Compile: create object files from C++ source files.
182 define COMPILE_CXX_TEMPLATE
183 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
184 @
$(ECHO
) $(MSG_COMPILINGCXX
) $$(call toprel
, $$<)
185 $(V1
) $(CXX
) -c
$(THUMB
) $$(CFLAGS
) $$(CPPFLAGS
) $$(CXXFLAGS
) $$< -o
$$@
188 # Compile: create object files from C++ source files. ARM-only
189 define COMPILE_CXX_ARM_TEMPLATE
190 $(OUTDIR
)/$(notdir $(basename $(1))).o
: $(1)
191 @
$(ECHO
) $(MSG_COMPILINGCXX_ARM
) $$(call toprel
, $$<)
192 $(V1
) $(CPP
) -c
$$(CFLAGS
) $$(CPPFLAGS
) $$(CXXFLAGS
) $$< -o
$$@
195 # Archive: create ar library file from object files.
196 # $1 = library file to produce
197 # $2 = list of object files that make up the library file
198 # $3 = optional object files directory
199 define ARCHIVE_TEMPLATE
203 @
$(ECHO
) $(MSG_ARCHIVING
) $$(call toprel
, $$@
)
205 $(V1
) $(AR
) rcs
$$@
$(2)
207 # This is a workaround for Windows CreateProcess() line length
208 # limitation. It is assumed that if the object files directory
209 # is given, all object files are in that directory.
213 $(AR
) rcs
$$@
$(notdir $(2)) && \
214 cd
$$$${pwd} >/dev
/null \
219 # Link: create ELF output file from object files.
220 # $1 = elf file to produce
221 # $2 = list of object files that make up the elf file
222 # $3 = optional list of libraries to build and link
225 .PRECIOUS
: $(2) $(3)
227 @
$(ECHO
) $(MSG_LINKING
) $$(call toprel
, $$@
)
228 $(V1
) $(CC
) $(THUMB
) $$(CFLAGS
) $(2) $(3) --output
$$@
$$(LDFLAGS
)
231 # Link: create ELF output file from object files.
232 # $1 = elf file to produce
233 # $2 = list of object files that make up the elf file
234 define LINK_CXX_TEMPLATE
238 @
$(ECHO
) $(MSG_LINKING
) $$(call toprel
, $$@
)
239 $(V1
) $(CXX
) $(THUMB
) $$(CFLAGS
) $(2) --output
$$@
$$(LDFLAGS
)
242 # Compile: create assembler files from C source files. ARM/Thumb
243 define PARTIAL_COMPILE_TEMPLATE
244 $($(1):.c
=.s
) : %.s
: %.c
245 @
$(ECHO
) $(MSG_ASMFROMC
) $$(call toprel
, $$<)
246 $(V1
) $(CC
) $(THUMB
) -S
$$(CFLAGS
) $$(CONLYFLAGS
) $$< -o
$$@
249 # Compile: create assembler files from C source files. ARM only
250 define PARTIAL_COMPILE_ARM_TEMPLATE
251 $($(1):.c
=.s
) : %.s
: %.c
252 @
$(ECHO
) $(MSG_ASMFROMC_ARM
) $$(call toprel
, $$<)
253 $(V1
) $(CC
) -S
$$(CFLAGS
) $$(CONLYFLAGS
) $$< -o
$$@
256 # $(1) = Name of binary image to write
257 # $(2) = Base of flash region to write/wipe
258 # $(3) = Size of flash region to write/wipe
259 # $(4) = OpenOCD JTAG interface configuration file to use
260 # $(5) = OpenOCD configuration file to use
262 # ---------------------------------------------------------------------------
263 # Options for OpenOCD flash-programming
264 # see openocd.pdf/openocd.texi for further information
267 OOCD_JTAG_SETUP
= -d0
268 # interface and board/target settings (using the OOCD target-library here)
269 OOCD_JTAG_SETUP
+= -s
$(ROOT_DIR
)/flight
/Project
/OpenOCD
270 OOCD_JTAG_SETUP
+= -f
$(4) -f
$(5)
273 OOCD_BOARD_RESET
= -c init
275 #OOCD_BOARD_RESET += -c targets
276 # commands to prepare flash-write
277 OOCD_BOARD_RESET
+= -c
"reset halt"
281 @
$(ECHO
) $(MSG_JTAG_PROGRAM
) $$(call toprel
, $$<)
283 $$(OOCD_JTAG_SETUP
) \
284 $$(OOCD_BOARD_RESET
) \
285 -c
"flash write_image erase $$< $(2) bin" \
286 -c
"verify_image $$< $(2) bin" \
292 @
$(ECHO
) $(MSG_JTAG_WIPE
) wiping
$(3) bytes starting from
$(2)
294 $$(OOCD_JTAG_SETUP
) \
295 $$(OOCD_BOARD_RESET
) \
296 -c
"flash erase_address pad $(2) $(3)" \