1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jg Wunsch, et al.
5 # Released to the Public Domain
7 # Additional material for this makefile was written by:
17 #----------------------------------------------------------------------------
20 # make all = Make software.
22 # make clean = Clean out built project files.
24 # make coff = Convert ELF to AVR COFF.
26 # make extcoff = Convert ELF to AVR Extended COFF.
28 # make program = Download the hex file to the device.
29 # Please customize your programmer settings(PROGRAM_CMD)
31 # make teensy = Download the hex file to the device, using teensy_loader_cli.
32 # (must have teensy_loader_cli installed).
34 # make dfu = Download the hex file to the device, using dfu-programmer (must
35 # have dfu-programmer installed).
37 # make flip = Download the hex file to the device, using Atmel FLIP (must
38 # have Atmel FLIP installed).
40 # make dfu-ee = Download the eeprom file to the device, using dfu-programmer
41 # (must have dfu-programmer installed).
43 # make flip-ee = Download the eeprom file to the device, using Atmel FLIP
44 # (must have Atmel FLIP installed).
46 # make debug = Start either simulavr or avarice as specified for debugging,
47 # with avr-gdb or avr-insight as the front end for debugging.
49 # make filename.s = Just compile filename.c into the assembler code only.
51 # make filename.i = Create a preprocessed source file for use in submitting
52 # bug reports to the GCC project.
54 # To rebuild project do "make clean" then "make all".
55 #----------------------------------------------------------------------------
58 # Output format. (can be srec, ihex, binary)
62 # Object files directory
63 # To put object files in current directory, use a dot (.), do NOT make
64 # this an empty or blank macro!
65 OBJDIR
= obj_
$(TARGET
)
68 # Optimization level, can be [0, 1, 2, 3, s].
69 # 0 = turn off optimization. s = optimize for size.
70 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
75 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
76 # AVR Studio 4.10 requires dwarf-2.
77 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
81 # List any extra directories to look for include files here.
82 # Each directory must be seperated by a space.
83 # Use forward slashes for directory separators.
84 # For a directory that has spaces, enclose it in quotes.
85 EXTRAINCDIRS
= $(subst :, ,$(VPATH
))
88 # Compiler flag to set the C Standard level.
90 # gnu89 = c89 plus GCC extensions
91 # c99 = ISO C99 standard (not yet fully implemented)
92 # gnu99 = c99 plus GCC extensions
93 CSTANDARD
= -std
=gnu99
96 # Place -D or -U options here for C sources
97 CDEFS
= -DF_CPU
=$(F_CPU
)UL
101 # Place -D or -U options here for ASM sources
102 ADEFS
= -DF_CPU
=$(F_CPU
)
106 # Place -D or -U options here for C++ sources
107 CPPDEFS
= -DF_CPU
=$(F_CPU
)UL
108 #CPPDEFS += -D__STDC_LIMIT_MACROS
109 #CPPDEFS += -D__STDC_CONSTANT_MACROS
110 CPPDEFS
+= $(OPT_DEFS
)
114 #---------------- Compiler Options C ----------------
115 # -g*: generate debugging information
116 # -O*: optimization level
117 # -f...: tuning, see GCC manual and avr-libc documentation
118 # -Wall...: warning level
119 # -Wa,...: tell GCC to pass this to the assembler.
120 # -adhlns...: create assembler listing
124 CFLAGS
+= -funsigned-char
125 CFLAGS
+= -funsigned-bitfields
126 CFLAGS
+= -ffunction-sections
127 CFLAGS
+= -fno-inline-small-functions
128 CFLAGS
+= -fpack-struct
129 CFLAGS
+= -fshort-enums
130 CFLAGS
+= -fno-strict-aliasing
132 CFLAGS
+= -Wstrict-prototypes
133 #CFLAGS += -mshort-calls
134 #CFLAGS += -fno-unit-at-a-time
136 #CFLAGS += -Wunreachable-code
137 #CFLAGS += -Wsign-compare
138 CFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
)
139 CFLAGS
+= $(patsubst %,-I
%,$(EXTRAINCDIRS
))
140 CFLAGS
+= $(CSTANDARD
)
142 CFLAGS
+= -include $(CONFIG_H
)
146 #---------------- Compiler Options C++ ----------------
147 # -g*: generate debugging information
148 # -O*: optimization level
149 # -f...: tuning, see GCC manual and avr-libc documentation
150 # -Wall...: warning level
151 # -Wa,...: tell GCC to pass this to the assembler.
152 # -adhlns...: create assembler listing
153 CPPFLAGS
= -g
$(DEBUG
)
154 CPPFLAGS
+= $(CPPDEFS
)
156 CPPFLAGS
+= -funsigned-char
157 CPPFLAGS
+= -funsigned-bitfields
158 CPPFLAGS
+= -fpack-struct
159 CPPFLAGS
+= -fshort-enums
160 CPPFLAGS
+= -fno-exceptions
163 #CPPFLAGS += -mshort-calls
164 #CPPFLAGS += -fno-unit-at-a-time
165 #CPPFLAGS += -Wstrict-prototypes
166 #CPPFLAGS += -Wunreachable-code
167 #CPPFLAGS += -Wsign-compare
168 CPPFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
)
169 CPPFLAGS
+= $(patsubst %,-I
%,$(EXTRAINCDIRS
))
170 #CPPFLAGS += $(CSTANDARD)
172 CPPFLAGS
+= -include $(CONFIG_H
)
176 #---------------- Assembler Options ----------------
177 # -Wa,...: tell GCC to pass this to the assembler.
178 # -adhlns: create listing
179 # -gstabs: have the assembler create line number information; note that
180 # for use in COFF files, additional information about filenames
181 # and function names needs to be present in the assembler source
182 # files -- see avr-libc docs [FIXME: not yet described there]
183 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
184 # dump that will be displayed for a given single line of source input.
185 ASFLAGS
= $(ADEFS
) -Wa
,-adhlns
=$(@
:%.o
=%.lst
),-gstabs
,--listing-cont-lines
=100
187 ASFLAGS
+= -include $(CONFIG_H
)
191 #---------------- Library Options ----------------
192 # Minimalistic printf version
193 PRINTF_LIB_MIN
= -Wl
,-u
,vfprintf
-lprintf_min
195 # Floating point printf version (requires MATH_LIB = -lm below)
196 PRINTF_LIB_FLOAT
= -Wl
,-u
,vfprintf
-lprintf_flt
198 # If this is left blank, then it will use the Standard printf version.
200 #PRINTF_LIB = $(PRINTF_LIB_MIN)
201 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
204 # Minimalistic scanf version
205 SCANF_LIB_MIN
= -Wl
,-u
,vfscanf
-lscanf_min
207 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
208 SCANF_LIB_FLOAT
= -Wl
,-u
,vfscanf
-lscanf_flt
210 # If this is left blank, then it will use the Standard scanf version.
212 #SCANF_LIB = $(SCANF_LIB_MIN)
213 #SCANF_LIB = $(SCANF_LIB_FLOAT)
219 # List any extra directories to look for libraries here.
220 # Each directory must be seperated by a space.
221 # Use forward slashes for directory separators.
222 # For a directory that has spaces, enclose it in quotes.
227 #---------------- External Memory Options ----------------
229 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
230 # used for variables (.data/.bss) and heap (malloc()).
231 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
233 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
234 # only used for heap (malloc()).
235 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
241 #---------------- Linker Options ----------------
242 # -Wl,...: tell GCC to pass this to linker.
243 # -Map: create map file
244 # --cref: add cross reference to map file
246 # Comennt out "--relax" option to avoid a error such:
247 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
249 LDFLAGS
= -Wl
,-Map
=$(TARGET
).map
,--cref
250 LDFLAGS
+= -Wl
,--relax
251 LDFLAGS
+= -Wl
,--gc-sections
252 LDFLAGS
+= $(EXTMEMOPTS
)
253 LDFLAGS
+= $(patsubst %,-L
%,$(EXTRALIBDIRS
))
254 LDFLAGS
+= $(PRINTF_LIB
) $(SCANF_LIB
) $(MATH_LIB
)
255 #LDFLAGS += -T linker_script.x
256 # You can give EXTRALDFLAGS at 'make' command line.
257 LDFLAGS
+= $(EXTRALDFLAGS
)
261 #---------------- Debugging Options ----------------
263 # For simulavr only - target MCU frequency.
264 DEBUG_MFREQ
= $(F_CPU
)
266 # Set the DEBUG_UI to either gdb or insight.
270 # Set the debugging back-end to either avarice, simulavr.
271 DEBUG_BACKEND
= avarice
272 #DEBUG_BACKEND = simulavr
275 GDBINIT_FILE
= __avr_gdbinit
277 # When using avarice settings for the JTAG
280 # Debugging port used to communicate between GDB / avarice / simulavr.
283 # Debugging host used to communicate between GDB / avarice / simulavr, normally
284 # just set to localhost unless doing some sort of crazy debugging when
285 # avarice is running on a different computer.
286 DEBUG_HOST
= localhost
290 #============================================================================
293 # Define programs and commands.
296 OBJCOPY
= avr-objcopy
297 OBJDUMP
= avr-objdump
309 MSG_ERRORS_NONE
= Errors
: none
310 MSG_BEGIN
= -------- begin
--------
311 MSG_END
= -------- end
--------
312 MSG_SIZE_BEFORE
= Size before
:
313 MSG_SIZE_AFTER
= Size after
:
314 MSG_COFF
= Converting to AVR COFF
:
315 MSG_EXTENDED_COFF
= Converting to AVR Extended COFF
:
316 MSG_FLASH
= Creating load file for Flash
:
317 MSG_EEPROM
= Creating load file for EEPROM
:
318 MSG_EXTENDED_LISTING
= Creating Extended Listing
:
319 MSG_SYMBOL_TABLE
= Creating Symbol Table
:
320 MSG_LINKING
= Linking
:
321 MSG_COMPILING
= Compiling C
:
322 MSG_COMPILING_CPP
= Compiling C
++:
323 MSG_ASSEMBLING
= Assembling
:
324 MSG_CLEANING
= Cleaning project
:
325 MSG_CREATING_LIBRARY
= Creating library
:
330 # Define all object files.
331 OBJ
= $(patsubst %.c
,$(OBJDIR
)/%.o
,$(patsubst %.
cpp,$(OBJDIR
)/%.o
,$(patsubst %.S
,$(OBJDIR
)/%.o
,$(SRC
))))
333 # Define all listing files.
334 LST
= $(patsubst %.c
,$(OBJDIR
)/%.lst
,$(patsubst %.
cpp,$(OBJDIR
)/%.lst
,$(patsubst %.S
,$(OBJDIR
)/%.lst
,$(SRC
))))
337 # Compiler flags to generate dependency files.
338 GENDEPFLAGS
= -MMD
-MP
-MF .dep
/$(@F
).d
341 # Combine all necessary flags and optional flags.
342 # Add target processor to flags.
343 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
344 ALL_CFLAGS
= -mmcu
=$(MCU
) -I.
$(CFLAGS
) $(GENDEPFLAGS
) $(EXTRAFLAGS
)
345 ALL_CPPFLAGS
= -mmcu
=$(MCU
) -I.
-x c
++ $(CPPFLAGS
) $(GENDEPFLAGS
) $(EXTRAFLAGS
)
346 ALL_ASFLAGS
= -mmcu
=$(MCU
) -I.
-x assembler-with-cpp
$(ASFLAGS
) $(EXTRAFLAGS
)
353 all: begin gccversion sizebefore build sizeafter end
355 # Change the build target to build a HEX file or a library.
356 build
: elf hex eep lss sym
365 LIBNAME
=lib
$(TARGET
).a
371 # AVR Studio 3.x does not check make's exit code but relies on
372 # the following magic strings to be generated by the compile job.
382 # Display size of file.
383 HEXSIZE
= $(SIZE
) --target
=$(FORMAT
) $(TARGET
).hex
384 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
385 ELFSIZE
= $(SIZE
) $(TARGET
).elf
388 @if
test -f
$(TARGET
).elf
; then echo
; echo
$(MSG_SIZE_BEFORE
); $(ELFSIZE
); \
389 2>/dev
/null
; echo
; fi
392 @if
test -f
$(TARGET
).elf
; then echo
; echo
$(MSG_SIZE_AFTER
); $(ELFSIZE
); \
393 2>/dev
/null
; echo
; fi
397 # Display compiler version information.
403 # Program the device.
404 program
: $(TARGET
).hex
$(TARGET
).eep
407 teensy
: $(TARGET
).hex
408 teensy_loader_cli
-mmcu
=$(MCU
) -w
-v
$(TARGET
).hex
411 batchisp
-hardware usb
-device
$(MCU
) -operation erase f
412 batchisp
-hardware usb
-device
$(MCU
) -operation loadbuffer
$(TARGET
).hex program
413 batchisp
-hardware usb
-device
$(MCU
) -operation start reset
0
416 dfu-programmer
$(MCU
) erase
417 dfu-programmer
$(MCU
) flash
$(TARGET
).hex
418 dfu-programmer
$(MCU
) reset
420 flip-ee
: $(TARGET
).hex
$(TARGET
).eep
421 $(COPY
) $(TARGET
).eep
$(TARGET
)eep.hex
422 batchisp
-hardware usb
-device
$(MCU
) -operation memory EEPROM erase
423 batchisp
-hardware usb
-device
$(MCU
) -operation memory EEPROM loadbuffer
$(TARGET
)eep.hex program
424 batchisp
-hardware usb
-device
$(MCU
) -operation start reset
0
425 $(REMOVE
) $(TARGET
)eep.hex
427 dfu-ee
: $(TARGET
).hex
$(TARGET
).eep
428 dfu-programmer
$(MCU
) eeprom-flash
$(TARGET
).eep
429 dfu-programmer
$(MCU
) reset
432 # Generate avr-gdb config/init file which does the following:
433 # define the reset signal, load the target file, connect to target, and set
434 # a breakpoint at main().
436 @
$(REMOVE
) $(GDBINIT_FILE
)
437 @echo
define reset
>> $(GDBINIT_FILE
)
438 @echo SIGNAL SIGHUP
>> $(GDBINIT_FILE
)
439 @echo end
>> $(GDBINIT_FILE
)
440 @echo file
$(TARGET
).elf
>> $(GDBINIT_FILE
)
441 @echo target remote
$(DEBUG_HOST
):$(DEBUG_PORT
) >> $(GDBINIT_FILE
)
442 ifeq ($(DEBUG_BACKEND
),simulavr
)
443 @echo load
>> $(GDBINIT_FILE
)
445 @echo break main
>> $(GDBINIT_FILE
)
447 debug
: gdb-config
$(TARGET
).elf
448 ifeq ($(DEBUG_BACKEND
), avarice
)
449 @echo Starting AVaRICE
- Press enter when
"waiting to connect" message displays.
450 @
$(WINSHELL
) /c start avarice
--jtag
$(JTAG_DEV
) --erase
--program
--file \
451 $(TARGET
).elf
$(DEBUG_HOST
):$(DEBUG_PORT
)
452 @
$(WINSHELL
) /c pause
455 @
$(WINSHELL
) /c start simulavr
--gdbserver
--device
$(MCU
) --clock-freq \
456 $(DEBUG_MFREQ
) --port
$(DEBUG_PORT
)
458 @
$(WINSHELL
) /c start avr-
$(DEBUG_UI
) --command
=$(GDBINIT_FILE
)
463 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
464 COFFCONVERT
= $(OBJCOPY
) --debugging
465 COFFCONVERT
+= --change-section-address .data-0x800000
466 COFFCONVERT
+= --change-section-address .bss-0x800000
467 COFFCONVERT
+= --change-section-address .noinit-0x800000
468 COFFCONVERT
+= --change-section-address .eeprom-0x810000
474 @echo
$(MSG_COFF
) $(TARGET
).cof
475 $(COFFCONVERT
) -O coff-avr
$< $(TARGET
).cof
478 extcoff
: $(TARGET
).elf
480 @echo
$(MSG_EXTENDED_COFF
) $(TARGET
).cof
481 $(COFFCONVERT
) -O coff-ext-avr
$< $(TARGET
).cof
485 # Create final output files (.hex, .eep) from ELF output file.
488 @echo
$(MSG_FLASH
) $@
489 $(OBJCOPY
) -O
$(FORMAT
) -R .eeprom
-R .fuse
-R .lock
-R .signature
$< $@
493 @echo
$(MSG_EEPROM
) $@
494 -$(OBJCOPY
) -j .eeprom
--set-section-flags
=.eeprom
="alloc,load" \
495 --change-section-lma .eeprom
=0 --no-change-warnings
-O
$(FORMAT
) $< $@ || exit
0
497 # Create extended listing file from ELF output file.
500 @echo
$(MSG_EXTENDED_LISTING
) $@
501 $(OBJDUMP
) -h
-S
-z
$< > $@
503 # Create a symbol table from ELF output file.
506 @echo
$(MSG_SYMBOL_TABLE
) $@
511 # Create library from object files.
512 .SECONDARY
: $(TARGET
).a
516 @echo
$(MSG_CREATING_LIBRARY
) $@
520 # Link: create ELF output file from object files.
521 .SECONDARY
: $(TARGET
).elf
525 @echo
$(MSG_LINKING
) $@
526 $(CC
) $(ALL_CFLAGS
) $^
--output
$@
$(LDFLAGS
)
529 # Compile: create object files from C source files.
533 @echo
$(MSG_COMPILING
) $<
534 $(CC
) -c
$(ALL_CFLAGS
) $< -o
$@
537 # Compile: create object files from C++ source files.
538 $(OBJDIR
)/%.o
: %.
cpp
540 @echo
$(MSG_COMPILING_CPP
) $<
541 $(CC
) -c
$(ALL_CPPFLAGS
) $< -o
$@
544 # Compile: create assembler files from C source files.
546 $(CC
) -S
$(ALL_CFLAGS
) $< -o
$@
549 # Compile: create assembler files from C++ source files.
551 $(CC
) -S
$(ALL_CPPFLAGS
) $< -o
$@
554 # Assemble: create object files from assembler source files.
557 @echo
$(MSG_ASSEMBLING
) $<
558 $(CC
) -c
$(ALL_ASFLAGS
) $< -o
$@
561 # Create preprocessed source for use in sending a bug report.
563 $(CC
) -E
-mmcu
=$(MCU
) -I.
$(CFLAGS
) $< -o
$@
566 # Target: clean project.
567 clean: begin clean_list end
571 $(REMOVE
) $(TARGET
).hex
572 $(REMOVE
) $(TARGET
).eep
573 $(REMOVE
) $(TARGET
).cof
574 $(REMOVE
) $(TARGET
).elf
575 $(REMOVE
) $(TARGET
).map
576 $(REMOVE
) $(TARGET
).sym
577 $(REMOVE
) $(TARGET
).lss
580 $(REMOVE
) $(OBJ
:.o
=.s
)
581 $(REMOVE
) $(OBJ
:.o
=.i
)
583 $(REMOVE
) -r
$(OBJDIR
)
590 # Create object files directory
591 $(shell mkdir
$(OBJDIR
) 2>/dev
/null
)
594 # Include the dependency files.
595 -include $(shell mkdir .dep
2>/dev
/null
) $(wildcard .dep
/*)
598 # Listing of phony targets.
599 .PHONY
: all begin finish end sizebefore sizeafter gccversion \
600 build elf hex eep lss sym coff extcoff \
601 clean clean_list debug gdb-config show_path \
602 program teensy dfu flip dfu-ee flip-ee