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 #----------------------------------------------------------------------------
63 MCU = ### INSERT NAME OF MICROCONTROLLER MODEL HERE ###
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.
73 BOARD = ### INSERT NAME OF BOARD HERE, OR NONE IF NO BOARD DRIVERS USED ###
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 will be an integer division of F_CLOCK below, as it is sourced by
83 # F_CLOCK after it has run through any CPU prescalers. Note that this value
84 # does not *change* the processor frequency - it should merely be updated to
85 # reflect the processor speed set externally so that the code can use accurate
87 F_CPU = ### INSERT PRESCALED SYSTEM CLOCK SPEED HERE, IN HZ ###
90 # Input clock frequency.
91 # This will define a symbol, F_USB, in all source code files equal to the
92 # input clock frequency (before any prescaling is performed) in Hz. This value may
93 # differ from F_CPU if prescaling is used on the latter, and is required as the
94 # raw input clock is fed directly to the PLL sections of the AVR for high speed
95 # clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
96 # at the end, this will be done automatically to create a 32-bit value in your
99 # If no clock division is performed on the input clock inside the AVR (via the
100 # CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
101 F_USB = ### INSERT CLOCK TO USB MODULE HERE, IN HZ ###
104 # Output format. (can be srec, ihex, binary)
108 # Target file name (without extension).
109 TARGET = ### INSERT NAME OF MAIN FILENAME HERE, WITHOUT EXTENSION ###
112 # Object files directory
113 # To put object files in current directory, use a dot (.), do NOT make
114 # this an empty or blank macro!
118 # Path to the LUFA library
119 LUFA_PATH = ### INSERT PATH TO LUFA LIBRARY RELATIVE TO PROJECT DIRECTORY HERE ###
122 # LUFA library compile-time options and predefined tokens (add '-D' before each token)
123 LUFA_OPTS = ### INSERT LUFA COMPILE TIME TOKES HERE ###
126 # Create the LUFA source path variables by including the LUFA root makefile
127 include $(LUFA_PATH)/LUFA/makefile
130 # List C source files here. (C dependencies are automatically generated.)
134 ### INSERT ADDITIONAL PROJECT SOURCE FILENAMES OR LUFA MODULE NAMES HERE ###
136 # List C++ source files here. (C dependencies are automatically generated.)
140 # List Assembler source files here.
141 # Make them always end in a capital .S. Files ending in a lowercase .s
142 # will not be considered source files but generated files (assembler
143 # output from the compiler), and will be deleted upon "make clean"!
144 # Even though the DOS/Win* filesystem matches both .s and .S the same,
145 # it will preserve the spelling of the filenames, and gcc itself does
146 # care about how the name is spelled on its command-line.
150 # Optimization level, can be [0, 1, 2, 3, s].
151 # 0 = turn off optimization. s = optimize for size.
152 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
157 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
158 # AVR Studio 4.10 requires dwarf-2.
159 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
163 # List any extra directories to look for include files here.
164 # Each directory must be seperated by a space.
165 # Use forward slashes for directory separators.
166 # For a directory that has spaces, enclose it in quotes.
167 EXTRAINCDIRS = $(LUFA_PATH)/
170 # Compiler flag to set the C Standard level.
172 # gnu89 = c89 plus GCC extensions
173 # c99 = ISO C99 standard (not yet fully implemented)
174 # gnu99 = c99 plus GCC extensions
178 # Place -D or -U options here for C sources
179 CDEFS = -DF_CPU=$(F_CPU)UL
180 CDEFS += -DF_USB=$(F_USB)UL
181 CDEFS += -DBOARD=BOARD_$(BOARD)
182 CDEFS += -DARCH=ARCH_$(ARCH)
183 CDEFS += $(LUFA_OPTS)
186 # Place -D or -U options here for ASM sources
187 ADEFS = -DF_CPU=$(F_CPU)
188 ADEFS += -DF_USB=$(F_USB)UL
189 ADEFS += -DBOARD=BOARD_$(BOARD)
190 ADEFS += -DARCH=ARCH_$(ARCH)
191 ADEFS += $(LUFA_OPTS)
193 # Place -D or -U options here for C++ sources
194 CPPDEFS = -DF_CPU=$(F_CPU)UL
195 CPPDEFS += -DF_USB=$(F_USB)UL
196 CPPDEFS += -DBOARD=BOARD_$(BOARD)
197 CPPDEFS += -DARCH=ARCH_$(ARCH)
198 CPPDEFS += $(LUFA_OPTS)
199 #CPPDEFS += -D__STDC_LIMIT_MACROS
200 #CPPDEFS += -D__STDC_CONSTANT_MACROS
204 #---------------- Compiler Options C ----------------
205 # -g*: generate debugging information
206 # -O*: optimization level
207 # -f...: tuning, see GCC manual and avr-libc documentation
208 # -Wall...: warning level
209 # -Wa,...: tell GCC to pass this to the assembler.
210 # -adhlns...: create assembler listing
214 CFLAGS += -funsigned-char
215 CFLAGS += -funsigned-bitfields
216 CFLAGS += -ffunction-sections
217 CFLAGS += -fno-inline-small-functions
218 CFLAGS += -fpack-struct
219 CFLAGS += -fshort-enums
220 CFLAGS += -fno-strict-aliasing
222 CFLAGS += -Wstrict-prototypes
223 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
224 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
225 CFLAGS += $(CSTANDARD)
228 #---------------- Compiler Options C++ ----------------
229 # -g*: generate debugging information
230 # -O*: optimization level
231 # -f...: tuning, see GCC manual and avr-libc documentation
232 # -Wall...: warning level
233 # -Wa,...: tell GCC to pass this to the assembler.
234 # -adhlns...: create assembler listing
235 CPPFLAGS = -g$(DEBUG)
236 CPPFLAGS += $(CPPDEFS)
238 CPPFLAGS += -funsigned-char
239 CPPFLAGS += -funsigned-bitfields
240 CPPFLAGS += -fpack-struct
241 CPPFLAGS += -fshort-enums
242 CPPFLAGS += -ffunction-sections
243 CPPFLAGS += -fno-strict-aliasing
244 CPPFLAGS += -fno-exceptions
247 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
248 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
249 #CPPFLAGS += $(CSTANDARD)
252 #---------------- Assembler Options ----------------
253 # -Wa,...: tell GCC to pass this to the assembler.
254 # -adhlns: create listing
255 # -gstabs: have the assembler create line number information; note that
256 # for use in COFF files, additional information about filenames
257 # and function names needs to be present in the assembler source
258 # files -- see avr-libc docs [FIXME: not yet described there]
259 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
260 # dump that will be displayed for a given single line of source input.
261 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
264 #---------------- Library Options ----------------
265 # Minimalistic printf version
266 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
268 # Floating point printf version (requires MATH_LIB = -lm below)
269 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
271 # If this is left blank, then it will use the Standard printf version.
273 #PRINTF_LIB = $(PRINTF_LIB_MIN)
274 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
277 # Minimalistic scanf version
278 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
280 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
281 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
283 # If this is left blank, then it will use the Standard scanf version.
285 #SCANF_LIB = $(SCANF_LIB_MIN)
286 #SCANF_LIB = $(SCANF_LIB_FLOAT)
292 # List any extra directories to look for libraries here.
293 # Each directory must be seperated by a space.
294 # Use forward slashes for directory separators.
295 # For a directory that has spaces, enclose it in quotes.
300 #---------------- External Memory Options ----------------
302 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
303 # used for variables (.data/.bss) and heap (malloc()).
304 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
306 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
307 # only used for heap (malloc()).
308 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
314 #---------------- Linker Options ----------------
315 # -Wl,...: tell GCC to pass this to linker.
316 # -Map: create map file
317 # --cref: add cross reference to map file
318 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
319 LDFLAGS += -Wl,--relax
320 LDFLAGS += -Wl,--gc-sections
321 LDFLAGS += $(EXTMEMOPTS)
322 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
323 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
324 #LDFLAGS += -T linker_script.x
328 #---------------- Programming Options (avrdude) ----------------
330 # Programming hardware
332 # to get a full listing.
334 AVRDUDE_PROGRAMMER = jtagmkII
336 # com1 = serial port. Use lpt1 to connect to parallel port.
339 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
340 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
343 # Uncomment the following if you want avrdude's erase cycle counter.
344 # Note that this counter needs to be initialized first using -Yn,
345 # see avrdude manual.
346 #AVRDUDE_ERASE_COUNTER = -y
348 # Uncomment the following if you do /not/ wish a verification to be
349 # performed after programming the device.
350 #AVRDUDE_NO_VERIFY = -V
352 # Increase verbosity level. Please use this when submitting bug
353 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
354 # to submit bug reports.
355 #AVRDUDE_VERBOSE = -v -v
357 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
358 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
359 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
360 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
364 #---------------- Debugging Options ----------------
366 # For simulavr only - target MCU frequency.
367 DEBUG_MFREQ = $(F_CPU)
369 # Set the DEBUG_UI to either gdb or insight.
373 # Set the debugging back-end to either avarice, simulavr.
374 DEBUG_BACKEND = avarice
375 #DEBUG_BACKEND = simulavr
378 GDBINIT_FILE = __avr_gdbinit
380 # When using avarice settings for the JTAG
383 # Debugging port used to communicate between GDB / avarice / simulavr.
386 # Debugging host used to communicate between GDB / avarice / simulavr, normally
387 # just set to localhost unless doing some sort of crazy debugging when
388 # avarice is running on a different computer.
389 DEBUG_HOST = localhost
393 #============================================================================
396 # Define programs and commands.
399 OBJCOPY = avr-objcopy
400 OBJDUMP = avr-objdump
413 MSG_ERRORS_NONE = Errors: none
414 MSG_BEGIN = -------- begin --------
415 MSG_END = -------- end --------
416 MSG_SIZE_BEFORE = Size before:
417 MSG_SIZE_AFTER = Size after:
418 MSG_COFF = Converting to AVR COFF:
419 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
420 MSG_FLASH = Creating load file for Flash:
421 MSG_EEPROM = Creating load file for EEPROM:
422 MSG_EXTENDED_LISTING = Creating Extended Listing:
423 MSG_SYMBOL_TABLE = Creating Symbol Table:
424 MSG_LINKING = Linking:
425 MSG_COMPILING = Compiling C:
426 MSG_COMPILING_CPP = Compiling C++:
427 MSG_ASSEMBLING = Assembling:
428 MSG_CLEANING = Cleaning project:
429 MSG_CREATING_LIBRARY = Creating library:
434 # Define all object files.
435 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
437 # Define all listing files.
438 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
441 # Compiler flags to generate dependency files.
442 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
445 # Combine all necessary flags and optional flags.
446 # Add target processor to flags.
447 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
448 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
449 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
456 all: begin gccversion sizebefore build sizeafter end
458 # Change the build target to build a HEX file or a library.
459 build: elf hex eep lss sym
468 LIBNAME=lib$(TARGET).a
474 # AVR Studio 3.x does not check make's exit code but relies on
475 # the following magic strings to be generated by the compile job.
485 # Display size of file.
486 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
487 ELFSIZE = $(SIZE) $(MCU_FLAG) $(FORMAT_FLAG) $(TARGET).elf
488 MCU_FLAG = $(shell $(SIZE) --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
489 FORMAT_FLAG = $(shell $(SIZE) --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
493 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
494 2>/dev/null; echo; fi
497 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
498 2>/dev/null; echo; fi
502 # Display compiler version information.
507 # Program the device.
508 program: $(TARGET).hex $(TARGET).eep
509 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
512 batchisp -hardware usb -device $(MCU) -operation erase f
513 batchisp -hardware usb -device $(MCU) -operation loadbuffer $(TARGET).hex program
514 batchisp -hardware usb -device $(MCU) -operation start reset 0
517 dfu-programmer $(MCU) erase
518 dfu-programmer $(MCU) flash $(TARGET).hex
519 dfu-programmer $(MCU) reset
521 flip-ee: $(TARGET).hex $(TARGET).eep
522 $(COPY) $(TARGET).eep $(TARGET)eep.hex
523 batchisp -hardware usb -device $(MCU) -operation memory EEPROM erase
524 batchisp -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(TARGET)eep.hex program
525 batchisp -hardware usb -device $(MCU) -operation start reset 0
526 $(REMOVE) $(TARGET)eep.hex
528 dfu-ee: $(TARGET).hex $(TARGET).eep
529 dfu-programmer $(MCU) eeprom-flash $(TARGET).eep
530 dfu-programmer $(MCU) reset
533 # Generate avr-gdb config/init file which does the following:
534 # define the reset signal, load the target file, connect to target, and set
535 # a breakpoint at main().
537 @$(REMOVE) $(GDBINIT_FILE)
538 @echo define reset >> $(GDBINIT_FILE)
539 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
540 @echo end >> $(GDBINIT_FILE)
541 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
542 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
543 ifeq ($(DEBUG_BACKEND),simulavr)
544 @echo load >> $(GDBINIT_FILE)
546 @echo break main >> $(GDBINIT_FILE)
548 debug: gdb-config $(TARGET).elf
549 ifeq ($(DEBUG_BACKEND), avarice)
550 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
551 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
552 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
553 @$(WINSHELL) /c pause
556 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
557 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
559 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
564 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
565 COFFCONVERT = $(OBJCOPY) --debugging
566 COFFCONVERT += --change-section-address .data-0x800000
567 COFFCONVERT += --change-section-address .bss-0x800000
568 COFFCONVERT += --change-section-address .noinit-0x800000
569 COFFCONVERT += --change-section-address .eeprom-0x810000
575 @echo $(MSG_COFF) $(TARGET).cof
576 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
579 extcoff: $(TARGET).elf
581 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
582 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
586 # Create final output files (.hex, .eep) from ELF output file.
589 @echo $(MSG_FLASH) $@
590 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
594 @echo $(MSG_EEPROM) $@
595 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
596 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
598 # Create extended listing file from ELF output file.
601 @echo $(MSG_EXTENDED_LISTING) $@
602 $(OBJDUMP) -h -S -z $< > $@
604 # Create a symbol table from ELF output file.
607 @echo $(MSG_SYMBOL_TABLE) $@
612 # Create library from object files.
613 .SECONDARY : $(TARGET).a
617 @echo $(MSG_CREATING_LIBRARY) $@
621 # Link: create ELF output file from object files.
622 .SECONDARY : $(TARGET).elf
626 @echo $(MSG_LINKING) $@
627 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
630 # Compile: create object files from C source files.
633 @echo $(MSG_COMPILING) $<
634 $(CC) -c $(ALL_CFLAGS) $< -o $@
637 # Compile: create object files from C++ source files.
638 $(OBJDIR)/%.o : %.cpp
640 @echo $(MSG_COMPILING_CPP) $<
641 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
644 # Compile: create assembler files from C source files.
646 $(CC) -S $(ALL_CFLAGS) $< -o $@
649 # Compile: create assembler files from C++ source files.
651 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
654 # Assemble: create object files from assembler source files.
657 @echo $(MSG_ASSEMBLING) $<
658 $(CC) -c $(ALL_ASFLAGS) $< -o $@
661 # Create preprocessed source for use in sending a bug report.
663 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
666 # Target: clean project.
667 clean: begin clean_list end
671 @echo $(MSG_CLEANING)
672 $(REMOVE) $(TARGET).hex
673 $(REMOVE) $(TARGET).eep
674 $(REMOVE) $(TARGET).cof
675 $(REMOVE) $(TARGET).elf
676 $(REMOVE) $(TARGET).map
677 $(REMOVE) $(TARGET).sym
678 $(REMOVE) $(TARGET).lss
679 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
680 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
681 $(REMOVE) $(SRC:.c=.s)
682 $(REMOVE) $(SRC:.c=.d)
683 $(REMOVE) $(SRC:.c=.i)
687 @echo Generating Project Documentation...
688 @doxygen Doxygen.conf
689 @echo Documentation Generation Complete.
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 \