1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.
4 # >> Modified for use with the LUFA project. <<
6 # Released to the Public Domain
8 # Additional material for this makefile was written by:
21 #----------------------------------------------------------------------------
24 # make all = Make software.
26 # make clean = Clean out built project files.
28 # make coff = Convert ELF to AVR COFF.
30 # make extcoff = Convert ELF to AVR Extended COFF.
32 # make program = Download the hex file to the device, using avrdude.
33 # Please customize the avrdude settings below first!
35 # make dfu = Download the hex file to the device, using dfu-programmer (must
36 # have dfu-programmer installed).
38 # make flip = Download the hex file to the device, using Atmel FLIP (must
39 # have Atmel FLIP installed).
41 # make dfu-ee = Download the eeprom file to the device, using dfu-programmer
42 # (must have dfu-programmer installed).
44 # make flip-ee = Download the eeprom file to the device, using Atmel FLIP
45 # (must have Atmel FLIP installed).
47 # make doxygen = Generate DoxyGen documentation for the project (must have
50 # make debug = Start either simulavr or avarice as specified for debugging,
51 # with avr-gdb or avr-insight as the front end for debugging.
53 # make filename.s = Just compile filename.c into the assembler code only.
55 # make filename.i = Create a preprocessed source file for use in submitting
56 # bug reports to the GCC project.
58 # To rebuild project do "make clean" then "make all".
59 #----------------------------------------------------------------------------
66 # Targeted chip architecture (see library "Architectures" documentation)
70 # Target board (see library "Board Types" documentation, NONE for projects not requiring
71 # LUFA board drivers). If USER is selected, put custom board drivers in a directory called
72 # "Board" inside the application directory.
76 # Processor frequency.
77 # This will define a symbol, F_CPU, in all source code files equal to the
78 # processor frequency in Hz. You can then use this symbol in your source code to
79 # calculate timings. Do NOT tack on a 'UL' at the end, this will be done
80 # automatically to create a 32-bit value in your source code.
82 # This should be the frequency the system core runs at, after the system clock
83 # has been set up correctly and started.
87 # USB controller master clock frequency.
88 # This will define a symbol, F_USB, in all source code files equal to the
89 # input clock frequency of the USB controller's clock generator in Hz.
91 # For the XMEGA chips, this should be equal to a multiple of 6MHz for Low
92 # Speed USB mode, or a multiple of 48MHz for Full Speed USB mode.
96 # Output format. (can be srec, ihex, binary)
100 # Target file name (without extension).
104 # Object files directory
105 # To put object files in current directory, use a dot (.), do NOT make
106 # this an empty or blank macro!
110 # Path to the LUFA library
114 # LUFA library compile-time options and predefined tokens
118 # Create the LUFA source path variables by including the LUFA root makefile
119 include $(LUFA_PATH)/LUFA/makefile
122 # List C source files here. (C dependencies are automatically generated.)
125 $(LUFA_SRC_USBCLASS) \
127 $(LUFA_SRC_SCHEDULER)
130 # List C++ source files here. (C dependencies are automatically generated.)
131 CPPSRC = Test_CPP.cpp
134 # List Assembler source files here.
135 # Make them always end in a capital .S. Files ending in a lowercase .s
136 # will not be considered source files but generated files (assembler
137 # output from the compiler), and will be deleted upon "make clean"!
138 # Even though the DOS/Win* filesystem matches both .s and .S the same,
139 # it will preserve the spelling of the filenames, and gcc itself does
140 # care about how the name is spelled on its command-line.
144 # Optimization level, can be [0, 1, 2, 3, s].
145 # 0 = turn off optimization. s = optimize for size.
146 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
150 # List any extra directories to look for include files here.
151 # Each directory must be seperated by a space.
152 # Use forward slashes for directory separators.
153 # For a directory that has spaces, enclose it in quotes.
154 EXTRAINCDIRS = $(LUFA_PATH)/
157 # Compiler flag to set the C Standard level.
159 # gnu89 = c89 plus GCC extensions
160 # c99 = ISO C99 standard (not yet fully implemented)
161 # gnu99 = c99 plus GCC extensions
162 CSTANDARD = -std=gnu99
165 # Place -D or -U options here for C sources
166 CDEFS = -DF_CPU=$(F_CPU)UL
167 CDEFS += -DF_USB=$(F_USB)UL
168 CDEFS += -DBOARD=BOARD_$(BOARD)
169 CDEFS += -DARCH=ARCH_$(ARCH)
170 CDEFS += $(LUFA_OPTS)
173 # Place -D or -U options here for ASM sources
174 ADEFS = -DF_CPU=$(F_CPU)
175 ADEFS += -DF_USB=$(F_USB)UL
176 ADEFS += -DBOARD=BOARD_$(BOARD)
177 ADEFS += -DARCH=ARCH_$(ARCH)
178 ADEFS += $(LUFA_OPTS)
180 # Place -D or -U options here for C++ sources
181 CPPDEFS = -DF_CPU=$(F_CPU)UL
182 CPPDEFS += -DF_USB=$(F_USB)UL
183 CPPDEFS += -DBOARD=BOARD_$(BOARD)
184 CPPDEFS += -DARCH=ARCH_$(ARCH)
185 CPPDEFS += $(LUFA_OPTS)
189 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
190 # AVR Studio 4.10 requires dwarf-2.
191 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
195 #---------------- Compiler Options C ----------------
196 # -g*: generate debugging information
197 # -O*: optimization level
198 # -f...: tuning, see GCC manual and avr-libc documentation
199 # -Wall...: warning level
200 # -Wa,...: tell GCC to pass this to the assembler.
201 # -adhlns...: create assembler listing
205 CFLAGS += -funsigned-char
206 CFLAGS += -funsigned-bitfields
207 CFLAGS += -ffunction-sections
208 CFLAGS += -fno-inline-small-functions
209 CFLAGS += -fpack-struct
210 CFLAGS += -fshort-enums
211 CFLAGS += -fno-strict-aliasing
213 CFLAGS += -Wstrict-prototypes
214 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
215 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
216 CFLAGS += $(CSTANDARD)
219 #---------------- Compiler Options C++ ----------------
220 # -g*: generate debugging information
221 # -O*: optimization level
222 # -f...: tuning, see GCC manual and avr-libc documentation
223 # -Wall...: warning level
224 # -Wa,...: tell GCC to pass this to the assembler.
225 # -adhlns...: create assembler listing
226 CPPFLAGS = -g$(DEBUG)
227 CPPFLAGS += $(CPPDEFS)
229 CPPFLAGS += -funsigned-char
230 CPPFLAGS += -funsigned-bitfields
231 CPPFLAGS += -fpack-struct
232 CPPFLAGS += -fshort-enums
233 CPPFLAGS += -ffunction-sections
234 CPPFLAGS += -fno-strict-aliasing
235 CPPFLAGS += -fno-exceptions
238 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
239 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
240 #CPPFLAGS += $(CSTANDARD)
243 #---------------- Assembler Options ----------------
244 # -Wa,...: tell GCC to pass this to the assembler.
245 # -adhlns: create listing
246 # -gstabs: have the assembler create line number information; note that
247 # for use in COFF files, additional information about filenames
248 # and function names needs to be present in the assembler source
249 # files -- see avr-libc docs [FIXME: not yet described there]
250 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
251 # dump that will be displayed for a given single line of source input.
252 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
255 #---------------- Library Options ----------------
256 # Minimalistic printf version
257 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
259 # Floating point printf version (requires MATH_LIB = -lm below)
260 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
262 # If this is left blank, then it will use the Standard printf version.
264 #PRINTF_LIB = $(PRINTF_LIB_MIN)
265 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
268 # Minimalistic scanf version
269 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
271 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
272 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
274 # If this is left blank, then it will use the Standard scanf version.
276 #SCANF_LIB = $(SCANF_LIB_MIN)
277 #SCANF_LIB = $(SCANF_LIB_FLOAT)
283 # List any extra directories to look for libraries here.
284 # Each directory must be seperated by a space.
285 # Use forward slashes for directory separators.
286 # For a directory that has spaces, enclose it in quotes.
291 #---------------- External Memory Options ----------------
293 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
294 # used for variables (.data/.bss) and heap (malloc()).
295 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
297 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
298 # only used for heap (malloc()).
299 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
305 #---------------- Linker Options ----------------
306 # -Wl,...: tell GCC to pass this to linker.
307 # -Map: create map file
308 # --cref: add cross reference to map file
309 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
310 LDFLAGS += -Wl,--relax
311 LDFLAGS += -Wl,--gc-sections
312 LDFLAGS += $(EXTMEMOPTS)
313 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
314 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
315 #LDFLAGS += -T linker_script.x
319 #---------------- Programming Options (avrdude) ----------------
321 # Programming hardware
323 # to get a full listing.
325 AVRDUDE_PROGRAMMER = jtagmkII
327 # com1 = serial port. Use lpt1 to connect to parallel port.
330 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
331 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
334 # Uncomment the following if you want avrdude's erase cycle counter.
335 # Note that this counter needs to be initialized first using -Yn,
336 # see avrdude manual.
337 #AVRDUDE_ERASE_COUNTER = -y
339 # Uncomment the following if you do /not/ wish a verification to be
340 # performed after programming the device.
341 #AVRDUDE_NO_VERIFY = -V
343 # Increase verbosity level. Please use this when submitting bug
344 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
345 # to submit bug reports.
346 #AVRDUDE_VERBOSE = -v -v
348 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
349 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
350 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
351 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
355 #---------------- Debugging Options ----------------
357 # For simulavr only - target MCU frequency.
358 DEBUG_MFREQ = $(F_CPU)
360 # Set the DEBUG_UI to either gdb or insight.
364 # Set the debugging back-end to either avarice, simulavr.
365 DEBUG_BACKEND = avarice
366 #DEBUG_BACKEND = simulavr
369 GDBINIT_FILE = __avr_gdbinit
371 # When using avarice settings for the JTAG
374 # Debugging port used to communicate between GDB / avarice / simulavr.
377 # Debugging host used to communicate between GDB / avarice / simulavr, normally
378 # just set to localhost unless doing some sort of crazy debugging when
379 # avarice is running on a different computer.
380 DEBUG_HOST = localhost
384 #============================================================================
387 # Define programs and commands.
390 OBJCOPY = avr-objcopy
391 OBJDUMP = avr-objdump
404 MSG_ERRORS_NONE = Errors: none
405 MSG_BEGIN = -------- begin --------
406 MSG_END = -------- end --------
407 MSG_SIZE_BEFORE = Size before:
408 MSG_SIZE_AFTER = Size after:
409 MSG_COFF = Converting to AVR COFF:
410 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
411 MSG_FLASH = Creating load file for Flash:
412 MSG_EEPROM = Creating load file for EEPROM:
413 MSG_EXTENDED_LISTING = Creating Extended Listing:
414 MSG_SYMBOL_TABLE = Creating Symbol Table:
415 MSG_LINKING = Linking:
416 MSG_COMPILING = Compiling C:
417 MSG_COMPILING_CPP = Compiling C++:
418 MSG_ASSEMBLING = Assembling:
419 MSG_CLEANING = Cleaning project:
420 MSG_CREATING_LIBRARY = Creating library:
425 # Define all object files.
426 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
428 # Define all listing files.
429 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
432 # Compiler flags to generate dependency files.
433 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
436 # Combine all necessary flags and optional flags.
437 # Add target processor to flags.
438 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
439 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
440 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
447 all: begin gccversion sizebefore build sizeafter end
449 # Change the build target to build a HEX file or a library.
450 build: elf hex eep lss sym
459 LIBNAME=lib$(TARGET).a
465 # AVR Studio 3.x does not check make's exit code but relies on
466 # the following magic strings to be generated by the compile job.
476 # Display size of file.
477 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
478 ELFSIZE = $(SIZE) $(MCU_FLAG) $(FORMAT_FLAG) $(TARGET).elf
479 MCU_FLAG = $(shell $(SIZE) --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
480 FORMAT_FLAG = $(shell $(SIZE) --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
484 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
485 2>/dev/null; echo; fi
488 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
489 2>/dev/null; echo; fi
493 # Display compiler version information.
498 # Program the device.
499 program: $(TARGET).hex $(TARGET).eep
500 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
503 batchisp -hardware usb -device $(MCU) -operation erase f
504 batchisp -hardware usb -device $(MCU) -operation loadbuffer $(TARGET).hex program
505 batchisp -hardware usb -device $(MCU) -operation start reset 0
508 dfu-programmer $(MCU) erase
509 dfu-programmer $(MCU) flash $(TARGET).hex
510 dfu-programmer $(MCU) reset
512 flip-ee: $(TARGET).hex $(TARGET).eep
513 $(COPY) $(TARGET).eep $(TARGET)eep.hex
514 batchisp -hardware usb -device $(MCU) -operation memory EEPROM erase
515 batchisp -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(TARGET)eep.hex program
516 batchisp -hardware usb -device $(MCU) -operation start reset 0
517 $(REMOVE) $(TARGET)eep.hex
519 dfu-ee: $(TARGET).hex $(TARGET).eep
520 dfu-programmer $(MCU) eeprom-flash $(TARGET).eep
521 dfu-programmer $(MCU) reset
524 # Generate avr-gdb config/init file which does the following:
525 # define the reset signal, load the target file, connect to target, and set
526 # a breakpoint at main().
528 @$(REMOVE) $(GDBINIT_FILE)
529 @echo define reset >> $(GDBINIT_FILE)
530 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
531 @echo end >> $(GDBINIT_FILE)
532 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
533 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
534 ifeq ($(DEBUG_BACKEND),simulavr)
535 @echo load >> $(GDBINIT_FILE)
537 @echo break main >> $(GDBINIT_FILE)
539 debug: gdb-config $(TARGET).elf
540 ifeq ($(DEBUG_BACKEND), avarice)
541 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
542 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
543 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
544 @$(WINSHELL) /c pause
547 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
548 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
550 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
555 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
556 COFFCONVERT = $(OBJCOPY) --debugging
557 COFFCONVERT += --change-section-address .data-0x800000
558 COFFCONVERT += --change-section-address .bss-0x800000
559 COFFCONVERT += --change-section-address .noinit-0x800000
560 COFFCONVERT += --change-section-address .eeprom-0x810000
566 @echo $(MSG_COFF) $(TARGET).cof
567 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
570 extcoff: $(TARGET).elf
572 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
573 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
577 # Create final output files (.hex, .eep) from ELF output file.
580 @echo $(MSG_FLASH) $@
581 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
585 @echo $(MSG_EEPROM) $@
586 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
587 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
589 # Create extended listing file from ELF output file.
592 @echo $(MSG_EXTENDED_LISTING) $@
593 $(OBJDUMP) -h -S -z $< > $@
595 # Create a symbol table from ELF output file.
598 @echo $(MSG_SYMBOL_TABLE) $@
603 # Create library from object files.
604 .SECONDARY : $(TARGET).a
608 @echo $(MSG_CREATING_LIBRARY) $@
612 # Link: create ELF output file from object files.
613 .SECONDARY : $(TARGET).elf
617 @echo $(MSG_LINKING) $@
618 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
621 # Compile: create object files from C source files.
624 @echo $(MSG_COMPILING) $<
625 $(CC) -c $(ALL_CFLAGS) $< -o $@
628 # Compile: create object files from C++ source files.
629 $(OBJDIR)/%.o : %.cpp
631 @echo $(MSG_COMPILING_CPP) $<
632 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
635 # Compile: create assembler files from C source files.
637 $(CC) -S $(ALL_CFLAGS) $< -o $@
640 # Compile: create assembler files from C++ source files.
642 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
645 # Assemble: create object files from assembler source files.
648 @echo $(MSG_ASSEMBLING) $<
649 $(CC) -c $(ALL_ASFLAGS) $< -o $@
652 # Create preprocessed source for use in sending a bug report.
654 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
657 # Target: clean project.
658 clean: begin clean_list end
662 @echo $(MSG_CLEANING)
663 $(REMOVE) $(TARGET).hex
664 $(REMOVE) $(TARGET).eep
665 $(REMOVE) $(TARGET).cof
666 $(REMOVE) $(TARGET).elf
667 $(REMOVE) $(TARGET).map
668 $(REMOVE) $(TARGET).sym
669 $(REMOVE) $(TARGET).lss
670 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
671 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
672 $(REMOVE) $(SRC:.c=.s)
673 $(REMOVE) $(SRC:.c=.d)
674 $(REMOVE) $(SRC:.c=.i)
678 @echo Generating Project Documentation \($(TARGET)\)...
679 @doxygen Doxygen.conf
680 @echo Documentation Generation Complete.
686 @for f in $(SRC) $(CPPSRC) $(ASRC); do \
687 if [ -f $$f ]; then \
688 echo "Found Source File: $$f" ; \
690 echo "Source File Not Found: $$f" ; \
694 # Create object files directory
695 $(shell mkdir $(OBJDIR) 2>/dev/null)
698 # Include the dependency files.
699 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
702 # Listing of phony targets.
703 .PHONY : all begin finish end sizebefore sizeafter gccversion \
704 build elf hex eep lss sym coff extcoff doxygen clean \
705 clean_list clean_doxygen program dfu flip flip-ee dfu-ee \
706 debug gdb-config checksource