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.)
57 # MCU name, you MUST set this to match the board you are using
58 # type "make clean" after changing this, so all files will be rebuilt
59 #MCU = at90usb162 # Teensy 1.0
60 MCU = atmega32u4 # Teensy 2.0
61 #MCU = at90usb646 # Teensy++ 1.0
62 #MCU = at90usb1286 # Teensy++ 2.0
65 # Processor frequency.
66 # Normally the first thing your program should do is set the clock prescaler,
67 # so your program will run at the correct speed. You should also set this
68 # variable to same clock speed. The _delay_ms() macro uses this, and many
69 # examples use this variable to calculate timings. Do not add a "UL" here.
73 # Output format. (can be srec, ihex, binary)
77 # Object files directory
78 # To put object files in current directory, use a dot (.), do NOT make
79 # this an empty or blank macro!
83 # List C++ source files here. (C dependencies are automatically generated.)
87 # List Assembler source files here.
88 # Make them always end in a capital .S. Files ending in a lowercase .s
89 # will not be considered source files but generated files (assembler
90 # output from the compiler), and will be deleted upon "make clean"!
91 # Even though the DOS/Win* filesystem matches both .s and .S the same,
92 # it will preserve the spelling of the filenames, and gcc itself does
93 # care about how the name is spelled on its command-line.
97 # Optimization level, can be [0, 1, 2, 3, s].
98 # 0 = turn off optimization. s = optimize for size.
99 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
104 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
105 # AVR Studio 4.10 requires dwarf-2.
106 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
110 # List any extra directories to look for include files here.
111 # Each directory must be seperated by a space.
112 # Use forward slashes for directory separators.
113 # For a directory that has spaces, enclose it in quotes.
117 # Compiler flag to set the C Standard level.
119 # gnu89 = c89 plus GCC extensions
120 # c99 = ISO C99 standard (not yet fully implemented)
121 # gnu99 = c99 plus GCC extensions
122 CSTANDARD = -std=gnu99
125 # Place -D or -U options here for C sources
126 CDEFS = -DF_CPU=$(F_CPU)UL
129 # Place -D or -U options here for ASM sources
130 ADEFS = -DF_CPU=$(F_CPU)
133 # Place -D or -U options here for C++ sources
134 CPPDEFS = -DF_CPU=$(F_CPU)UL
135 #CPPDEFS += -D__STDC_LIMIT_MACROS
136 #CPPDEFS += -D__STDC_CONSTANT_MACROS
140 #---------------- Compiler Options C ----------------
141 # -g*: generate debugging information
142 # -O*: optimization level
143 # -f...: tuning, see GCC manual and avr-libc documentation
144 # -Wall...: warning level
145 # -Wa,...: tell GCC to pass this to the assembler.
146 # -adhlns...: create assembler listing
150 CFLAGS += -funsigned-char
151 CFLAGS += -funsigned-bitfields
152 CFLAGS += -ffunction-sections
153 CFLAGS += -fpack-struct
154 CFLAGS += -fshort-enums
156 CFLAGS += -Wstrict-prototypes
157 #CFLAGS += -mshort-calls
158 #CFLAGS += -fno-unit-at-a-time
160 #CFLAGS += -Wunreachable-code
161 #CFLAGS += -Wsign-compare
162 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
163 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
164 CFLAGS += $(CSTANDARD)
167 #---------------- Compiler Options C++ ----------------
168 # -g*: generate debugging information
169 # -O*: optimization level
170 # -f...: tuning, see GCC manual and avr-libc documentation
171 # -Wall...: warning level
172 # -Wa,...: tell GCC to pass this to the assembler.
173 # -adhlns...: create assembler listing
174 CPPFLAGS = -g$(DEBUG)
175 CPPFLAGS += $(CPPDEFS)
177 CPPFLAGS += -funsigned-char
178 CPPFLAGS += -funsigned-bitfields
179 CPPFLAGS += -fpack-struct
180 CPPFLAGS += -fshort-enums
181 CPPFLAGS += -fno-exceptions
184 #CPPFLAGS += -mshort-calls
185 #CPPFLAGS += -fno-unit-at-a-time
186 #CPPFLAGS += -Wstrict-prototypes
187 #CPPFLAGS += -Wunreachable-code
188 #CPPFLAGS += -Wsign-compare
189 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
190 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
191 #CPPFLAGS += $(CSTANDARD)
194 #---------------- Assembler Options ----------------
195 # -Wa,...: tell GCC to pass this to the assembler.
196 # -adhlns: create listing
197 # -gstabs: have the assembler create line number information; note that
198 # for use in COFF files, additional information about filenames
199 # and function names needs to be present in the assembler source
200 # files -- see avr-libc docs [FIXME: not yet described there]
201 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
202 # dump that will be displayed for a given single line of source input.
203 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
206 #---------------- Library Options ----------------
207 # Minimalistic printf version
208 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
210 # Floating point printf version (requires MATH_LIB = -lm below)
211 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
213 # If this is left blank, then it will use the Standard printf version.
215 #PRINTF_LIB = $(PRINTF_LIB_MIN)
216 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
219 # Minimalistic scanf version
220 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
222 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
223 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
225 # If this is left blank, then it will use the Standard scanf version.
227 #SCANF_LIB = $(SCANF_LIB_MIN)
228 #SCANF_LIB = $(SCANF_LIB_FLOAT)
234 # List any extra directories to look for libraries here.
235 # Each directory must be seperated by a space.
236 # Use forward slashes for directory separators.
237 # For a directory that has spaces, enclose it in quotes.
242 #---------------- External Memory Options ----------------
244 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
245 # used for variables (.data/.bss) and heap (malloc()).
246 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
248 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
249 # only used for heap (malloc()).
250 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
256 #---------------- Linker Options ----------------
257 # -Wl,...: tell GCC to pass this to linker.
258 # -Map: create map file
259 # --cref: add cross reference to map file
260 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
261 LDFLAGS += -Wl,--relax
262 LDFLAGS += -Wl,--gc-sections
263 LDFLAGS += $(EXTMEMOPTS)
264 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
265 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
266 #LDFLAGS += -T linker_script.x
270 #---------------- Programming Options (avrdude) ----------------
272 # Programming hardware
274 # to get a full listing.
276 AVRDUDE_PROGRAMMER = stk500v2
278 # com1 = serial port. Use lpt1 to connect to parallel port.
279 AVRDUDE_PORT = com1 # programmer connected to serial device
281 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
282 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
285 # Uncomment the following if you want avrdude's erase cycle counter.
286 # Note that this counter needs to be initialized first using -Yn,
287 # see avrdude manual.
288 #AVRDUDE_ERASE_COUNTER = -y
290 # Uncomment the following if you do /not/ wish a verification to be
291 # performed after programming the device.
292 #AVRDUDE_NO_VERIFY = -V
294 # Increase verbosity level. Please use this when submitting bug
295 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
296 # to submit bug reports.
297 #AVRDUDE_VERBOSE = -v -v
299 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
300 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
301 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
302 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
306 #---------------- Debugging Options ----------------
308 # For simulavr only - target MCU frequency.
309 DEBUG_MFREQ = $(F_CPU)
311 # Set the DEBUG_UI to either gdb or insight.
315 # Set the debugging back-end to either avarice, simulavr.
316 DEBUG_BACKEND = avarice
317 #DEBUG_BACKEND = simulavr
320 GDBINIT_FILE = __avr_gdbinit
322 # When using avarice settings for the JTAG
325 # Debugging port used to communicate between GDB / avarice / simulavr.
328 # Debugging host used to communicate between GDB / avarice / simulavr, normally
329 # just set to localhost unless doing some sort of crazy debugging when
330 # avarice is running on a different computer.
331 DEBUG_HOST = localhost
335 #============================================================================
338 # Define programs and commands.
341 OBJCOPY = avr-objcopy
342 OBJDUMP = avr-objdump
355 MSG_ERRORS_NONE = Errors: none
356 MSG_BEGIN = -------- begin --------
357 MSG_END = -------- end --------
358 MSG_SIZE_BEFORE = Size before:
359 MSG_SIZE_AFTER = Size after:
360 MSG_COFF = Converting to AVR COFF:
361 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
362 MSG_FLASH = Creating load file for Flash:
363 MSG_EEPROM = Creating load file for EEPROM:
364 MSG_EXTENDED_LISTING = Creating Extended Listing:
365 MSG_SYMBOL_TABLE = Creating Symbol Table:
366 MSG_LINKING = Linking:
367 MSG_COMPILING = Compiling C:
368 MSG_COMPILING_CPP = Compiling C++:
369 MSG_ASSEMBLING = Assembling:
370 MSG_CLEANING = Cleaning project:
371 MSG_CREATING_LIBRARY = Creating library:
376 # Define all object files.
377 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
379 # Define all listing files.
380 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
383 # Compiler flags to generate dependency files.
384 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
387 # Combine all necessary flags and optional flags.
388 # Add target processor to flags.
389 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
390 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
391 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
398 all: begin gccversion sizebefore build sizeafter end
400 # Change the build target to build a HEX file or a library.
401 build: elf hex eep lss sym
410 LIBNAME=lib$(TARGET).a
416 # AVR Studio 3.x does not check make's exit code but relies on
417 # the following magic strings to be generated by the compile job.
427 # Display size of file.
428 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
429 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
430 ELFSIZE = $(SIZE) $(TARGET).elf
433 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
434 2>/dev/null; echo; fi
437 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
438 2>/dev/null; echo; fi
442 # Display compiler version information.
448 # Program the device.
449 program: $(TARGET).hex $(TARGET).eep
450 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
453 # Generate avr-gdb config/init file which does the following:
454 # define the reset signal, load the target file, connect to target, and set
455 # a breakpoint at main().
457 @$(REMOVE) $(GDBINIT_FILE)
458 @echo define reset >> $(GDBINIT_FILE)
459 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
460 @echo end >> $(GDBINIT_FILE)
461 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
462 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
463 ifeq ($(DEBUG_BACKEND),simulavr)
464 @echo load >> $(GDBINIT_FILE)
466 @echo break main >> $(GDBINIT_FILE)
468 debug: gdb-config $(TARGET).elf
469 ifeq ($(DEBUG_BACKEND), avarice)
470 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
471 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
472 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
473 @$(WINSHELL) /c pause
476 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
477 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
479 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
484 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
485 COFFCONVERT = $(OBJCOPY) --debugging
486 COFFCONVERT += --change-section-address .data-0x800000
487 COFFCONVERT += --change-section-address .bss-0x800000
488 COFFCONVERT += --change-section-address .noinit-0x800000
489 COFFCONVERT += --change-section-address .eeprom-0x810000
495 @echo $(MSG_COFF) $(TARGET).cof
496 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
499 extcoff: $(TARGET).elf
501 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
502 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
506 # Create final output files (.hex, .eep) from ELF output file.
509 @echo $(MSG_FLASH) $@
510 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
514 @echo $(MSG_EEPROM) $@
515 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
516 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
518 # Create extended listing file from ELF output file.
521 @echo $(MSG_EXTENDED_LISTING) $@
522 $(OBJDUMP) -h -S -z $< > $@
524 # Create a symbol table from ELF output file.
527 @echo $(MSG_SYMBOL_TABLE) $@
532 # Create library from object files.
533 .SECONDARY : $(TARGET).a
537 @echo $(MSG_CREATING_LIBRARY) $@
541 # Link: create ELF output file from object files.
542 .SECONDARY : $(TARGET).elf
546 @echo $(MSG_LINKING) $@
547 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
550 # Compile: create object files from C source files.
553 @echo $(MSG_COMPILING) $<
554 $(CC) -c $(ALL_CFLAGS) $< -o $@
557 # Compile: create object files from C++ source files.
558 $(OBJDIR)/%.o : %.cpp
560 @echo $(MSG_COMPILING_CPP) $<
561 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
564 # Compile: create assembler files from C source files.
566 $(CC) -S $(ALL_CFLAGS) $< -o $@
569 # Compile: create assembler files from C++ source files.
571 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
574 # Assemble: create object files from assembler source files.
577 @echo $(MSG_ASSEMBLING) $<
578 $(CC) -c $(ALL_ASFLAGS) $< -o $@
581 # Create preprocessed source for use in sending a bug report.
583 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
586 # Target: clean project.
587 clean: begin clean_list end
591 @echo $(MSG_CLEANING)
592 $(REMOVE) $(TARGET).hex
593 $(REMOVE) $(TARGET).eep
594 $(REMOVE) $(TARGET).cof
595 $(REMOVE) $(TARGET).elf
596 $(REMOVE) $(TARGET).map
597 $(REMOVE) $(TARGET).sym
598 $(REMOVE) $(TARGET).lss
599 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
600 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
601 $(REMOVE) $(SRC:.c=.s)
602 $(REMOVE) $(SRC:.c=.d)
603 $(REMOVE) $(SRC:.c=.i)
607 # Create object files directory
608 $(shell mkdir $(OBJDIR) 2>/dev/null)
611 # Include the dependency files.
612 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
615 # Listing of phony targets.
616 .PHONY : all begin finish end sizebefore sizeafter gccversion \
617 build elf hex eep lss sym coff extcoff \
618 clean clean_list program debug gdb-config