1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jörg 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, using avrdude.
29 # Please customize the avrdude settings below first!
31 # make debug = Start either simulavr or avarice as specified for debugging,
32 # with avr-gdb or avr-insight as the front end for debugging.
34 # make filename.s = Just compile filename.c into the assembler code only.
36 # make filename.i = Create a preprocessed source file for use in submitting
37 # bug reports to the GCC project.
39 # To rebuild project do "make clean" then "make all".
40 #----------------------------------------------------------------------------
43 # Target file name (without extension).
47 # List C source files here. (C dependencies are automatically generated.)
59 # MCU name, you MUST set this to match the board you are using
60 # type "make clean" after changing this, so all files will be rebuilt
61 #MCU = at90usb162 # Teensy 1.0
62 #MCU = atmega32u4 # Teensy 2.0
63 #MCU = at90usb646 # Teensy++ 1.0
64 MCU = at90usb1286 # Teensy++ 2.0
67 # Processor frequency.
68 # Normally the first thing your program should do is set the clock prescaler,
69 # so your program will run at the correct speed. You should also set this
70 # variable to same clock speed. The _delay_ms() macro uses this, and many
71 # examples use this variable to calculate timings. Do not add a "UL" here.
75 # Output format. (can be srec, ihex, binary)
79 # Object files directory
80 # To put object files in current directory, use a dot (.), do NOT make
81 # this an empty or blank macro!
85 # List C++ source files here. (C dependencies are automatically generated.)
89 # List Assembler source files here.
90 # Make them always end in a capital .S. Files ending in a lowercase .s
91 # will not be considered source files but generated files (assembler
92 # output from the compiler), and will be deleted upon "make clean"!
93 # Even though the DOS/Win* filesystem matches both .s and .S the same,
94 # it will preserve the spelling of the filenames, and gcc itself does
95 # care about how the name is spelled on its command-line.
99 # Optimization level, can be [0, 1, 2, 3, s].
100 # 0 = turn off optimization. s = optimize for size.
101 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
106 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
107 # AVR Studio 4.10 requires dwarf-2.
108 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
112 # List any extra directories to look for include files here.
113 # Each directory must be seperated by a space.
114 # Use forward slashes for directory separators.
115 # For a directory that has spaces, enclose it in quotes.
119 # Compiler flag to set the C Standard level.
121 # gnu89 = c89 plus GCC extensions
122 # c99 = ISO C99 standard (not yet fully implemented)
123 # gnu99 = c99 plus GCC extensions
124 CSTANDARD = -std=gnu99
127 # Place -D or -U options here for C sources
128 CDEFS = -DF_CPU=$(F_CPU)UL
131 # Place -D or -U options here for ASM sources
132 ADEFS = -DF_CPU=$(F_CPU)
135 # Place -D or -U options here for C++ sources
136 CPPDEFS = -DF_CPU=$(F_CPU)UL
137 #CPPDEFS += -D__STDC_LIMIT_MACROS
138 #CPPDEFS += -D__STDC_CONSTANT_MACROS
142 #---------------- Compiler Options C ----------------
143 # -g*: generate debugging information
144 # -O*: optimization level
145 # -f...: tuning, see GCC manual and avr-libc documentation
146 # -Wall...: warning level
147 # -Wa,...: tell GCC to pass this to the assembler.
148 # -adhlns...: create assembler listing
152 CFLAGS += -funsigned-char
153 CFLAGS += -funsigned-bitfields
154 CFLAGS += -ffunction-sections
155 CFLAGS += -fpack-struct
156 CFLAGS += -fshort-enums
158 CFLAGS += -Wstrict-prototypes
159 #CFLAGS += -mshort-calls
160 #CFLAGS += -fno-unit-at-a-time
162 #CFLAGS += -Wunreachable-code
163 #CFLAGS += -Wsign-compare
164 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
165 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
166 CFLAGS += $(CSTANDARD)
169 #---------------- Compiler Options C++ ----------------
170 # -g*: generate debugging information
171 # -O*: optimization level
172 # -f...: tuning, see GCC manual and avr-libc documentation
173 # -Wall...: warning level
174 # -Wa,...: tell GCC to pass this to the assembler.
175 # -adhlns...: create assembler listing
176 CPPFLAGS = -g$(DEBUG)
177 CPPFLAGS += $(CPPDEFS)
179 CPPFLAGS += -funsigned-char
180 CPPFLAGS += -funsigned-bitfields
181 CPPFLAGS += -fpack-struct
182 CPPFLAGS += -fshort-enums
183 CPPFLAGS += -fno-exceptions
186 #CPPFLAGS += -mshort-calls
187 #CPPFLAGS += -fno-unit-at-a-time
188 #CPPFLAGS += -Wstrict-prototypes
189 #CPPFLAGS += -Wunreachable-code
190 #CPPFLAGS += -Wsign-compare
191 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
192 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
193 #CPPFLAGS += $(CSTANDARD)
196 #---------------- Assembler Options ----------------
197 # -Wa,...: tell GCC to pass this to the assembler.
198 # -adhlns: create listing
199 # -gstabs: have the assembler create line number information; note that
200 # for use in COFF files, additional information about filenames
201 # and function names needs to be present in the assembler source
202 # files -- see avr-libc docs [FIXME: not yet described there]
203 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
204 # dump that will be displayed for a given single line of source input.
205 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
208 #---------------- Library Options ----------------
209 # Minimalistic printf version
210 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
212 # Floating point printf version (requires MATH_LIB = -lm below)
213 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
215 # If this is left blank, then it will use the Standard printf version.
217 #PRINTF_LIB = $(PRINTF_LIB_MIN)
218 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
221 # Minimalistic scanf version
222 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
224 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
225 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
227 # If this is left blank, then it will use the Standard scanf version.
229 #SCANF_LIB = $(SCANF_LIB_MIN)
230 #SCANF_LIB = $(SCANF_LIB_FLOAT)
236 # List any extra directories to look for libraries here.
237 # Each directory must be seperated by a space.
238 # Use forward slashes for directory separators.
239 # For a directory that has spaces, enclose it in quotes.
244 #---------------- External Memory Options ----------------
246 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
247 # used for variables (.data/.bss) and heap (malloc()).
248 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
250 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
251 # only used for heap (malloc()).
252 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
258 #---------------- Linker Options ----------------
259 # -Wl,...: tell GCC to pass this to linker.
260 # -Map: create map file
261 # --cref: add cross reference to map file
262 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
263 LDFLAGS += -Wl,--relax
264 LDFLAGS += -Wl,--gc-sections
265 LDFLAGS += $(EXTMEMOPTS)
266 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
267 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
268 #LDFLAGS += -T linker_script.x
272 #---------------- Programming Options (avrdude) ----------------
274 # Programming hardware
276 # to get a full listing.
278 AVRDUDE_PROGRAMMER = stk500v2
280 # com1 = serial port. Use lpt1 to connect to parallel port.
281 AVRDUDE_PORT = com1 # programmer connected to serial device
283 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
284 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
287 # Uncomment the following if you want avrdude's erase cycle counter.
288 # Note that this counter needs to be initialized first using -Yn,
289 # see avrdude manual.
290 #AVRDUDE_ERASE_COUNTER = -y
292 # Uncomment the following if you do /not/ wish a verification to be
293 # performed after programming the device.
294 #AVRDUDE_NO_VERIFY = -V
296 # Increase verbosity level. Please use this when submitting bug
297 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
298 # to submit bug reports.
299 #AVRDUDE_VERBOSE = -v -v
301 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
302 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
303 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
304 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
308 #---------------- Debugging Options ----------------
310 # For simulavr only - target MCU frequency.
311 DEBUG_MFREQ = $(F_CPU)
313 # Set the DEBUG_UI to either gdb or insight.
317 # Set the debugging back-end to either avarice, simulavr.
318 DEBUG_BACKEND = avarice
319 #DEBUG_BACKEND = simulavr
322 GDBINIT_FILE = __avr_gdbinit
324 # When using avarice settings for the JTAG
327 # Debugging port used to communicate between GDB / avarice / simulavr.
330 # Debugging host used to communicate between GDB / avarice / simulavr, normally
331 # just set to localhost unless doing some sort of crazy debugging when
332 # avarice is running on a different computer.
333 DEBUG_HOST = localhost
337 #============================================================================
340 # Define programs and commands.
343 OBJCOPY = avr-objcopy
344 OBJDUMP = avr-objdump
357 MSG_ERRORS_NONE = Errors: none
358 MSG_BEGIN = -------- begin --------
359 MSG_END = -------- end --------
360 MSG_SIZE_BEFORE = Size before:
361 MSG_SIZE_AFTER = Size after:
362 MSG_COFF = Converting to AVR COFF:
363 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
364 MSG_FLASH = Creating load file for Flash:
365 MSG_EEPROM = Creating load file for EEPROM:
366 MSG_EXTENDED_LISTING = Creating Extended Listing:
367 MSG_SYMBOL_TABLE = Creating Symbol Table:
368 MSG_LINKING = Linking:
369 MSG_COMPILING = Compiling C:
370 MSG_COMPILING_CPP = Compiling C++:
371 MSG_ASSEMBLING = Assembling:
372 MSG_CLEANING = Cleaning project:
373 MSG_CREATING_LIBRARY = Creating library:
378 # Define all object files.
379 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
381 # Define all listing files.
382 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
385 # Compiler flags to generate dependency files.
386 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
389 # Combine all necessary flags and optional flags.
390 # Add target processor to flags.
391 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
392 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
393 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
400 all: begin gccversion sizebefore build sizeafter end
402 # Change the build target to build a HEX file or a library.
403 build: elf hex eep lss sym
412 LIBNAME=lib$(TARGET).a
418 # AVR Studio 3.x does not check make's exit code but relies on
419 # the following magic strings to be generated by the compile job.
429 # Display size of file.
430 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
431 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
432 ELFSIZE = $(SIZE) $(TARGET).elf
435 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
436 2>/dev/null; echo; fi
439 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
440 2>/dev/null; echo; fi
444 # Display compiler version information.
450 # Program the device.
451 program: $(TARGET).hex $(TARGET).eep
452 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
455 # Generate avr-gdb config/init file which does the following:
456 # define the reset signal, load the target file, connect to target, and set
457 # a breakpoint at main().
459 @$(REMOVE) $(GDBINIT_FILE)
460 @echo define reset >> $(GDBINIT_FILE)
461 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
462 @echo end >> $(GDBINIT_FILE)
463 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
464 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
465 ifeq ($(DEBUG_BACKEND),simulavr)
466 @echo load >> $(GDBINIT_FILE)
468 @echo break main >> $(GDBINIT_FILE)
470 debug: gdb-config $(TARGET).elf
471 ifeq ($(DEBUG_BACKEND), avarice)
472 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
473 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
474 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
475 @$(WINSHELL) /c pause
478 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
479 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
481 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
486 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
487 COFFCONVERT = $(OBJCOPY) --debugging
488 COFFCONVERT += --change-section-address .data-0x800000
489 COFFCONVERT += --change-section-address .bss-0x800000
490 COFFCONVERT += --change-section-address .noinit-0x800000
491 COFFCONVERT += --change-section-address .eeprom-0x810000
497 @echo $(MSG_COFF) $(TARGET).cof
498 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
501 extcoff: $(TARGET).elf
503 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
504 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
508 # Create final output files (.hex, .eep) from ELF output file.
511 @echo $(MSG_FLASH) $@
512 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
516 @echo $(MSG_EEPROM) $@
517 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
518 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
520 # Create extended listing file from ELF output file.
523 @echo $(MSG_EXTENDED_LISTING) $@
524 $(OBJDUMP) -h -S -z $< > $@
526 # Create a symbol table from ELF output file.
529 @echo $(MSG_SYMBOL_TABLE) $@
534 # Create library from object files.
535 .SECONDARY : $(TARGET).a
539 @echo $(MSG_CREATING_LIBRARY) $@
543 # Link: create ELF output file from object files.
544 .SECONDARY : $(TARGET).elf
548 @echo $(MSG_LINKING) $@
549 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
552 # Compile: create object files from C source files.
555 @echo $(MSG_COMPILING) $<
556 $(CC) -c $(ALL_CFLAGS) $< -o $@
559 # Compile: create object files from C++ source files.
560 $(OBJDIR)/%.o : %.cpp
562 @echo $(MSG_COMPILING_CPP) $<
563 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
566 # Compile: create assembler files from C source files.
568 $(CC) -S $(ALL_CFLAGS) $< -o $@
571 # Compile: create assembler files from C++ source files.
573 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
576 # Assemble: create object files from assembler source files.
579 @echo $(MSG_ASSEMBLING) $<
580 $(CC) -c $(ALL_ASFLAGS) $< -o $@
583 # Create preprocessed source for use in sending a bug report.
585 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
588 # Target: clean project.
589 clean: begin clean_list end
593 @echo $(MSG_CLEANING)
594 $(REMOVE) $(TARGET).hex
595 $(REMOVE) $(TARGET).eep
596 $(REMOVE) $(TARGET).cof
597 $(REMOVE) $(TARGET).elf
598 $(REMOVE) $(TARGET).map
599 $(REMOVE) $(TARGET).sym
600 $(REMOVE) $(TARGET).lss
601 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
602 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
603 $(REMOVE) $(SRC:.c=.s)
604 $(REMOVE) $(SRC:.c=.d)
605 $(REMOVE) $(SRC:.c=.i)
609 # Create object files directory
610 $(shell mkdir $(OBJDIR) 2>/dev/null)
613 # Include the dependency files.
614 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
617 # Listing of phony targets.
618 .PHONY : all begin finish end sizebefore sizeafter gccversion \
619 build elf hex eep lss sym coff extcoff \
620 clean clean_list program debug gdb-config