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 should be the frequency the system core runs at, after the system clock
83 # has been set up correctly and started.
84 F_CPU = ### INSERT PRESCALED SYSTEM CLOCK SPEED HERE, IN HZ ###
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 (before any prescaling is performed) in Hz. This value may
90 # differ from F_CPU, as the USB clock is often sourced from the same oscillator as
91 # the CPU clock, but run through multipliers and dividers to achieve the desired
94 # For the UC3 chips, this should be equal to 48MHz or 96MHz.
95 F_USB = ### INSERT CLOCK TO USB MODULE HERE, IN HZ ###
98 # Output format. (can be srec, ihex, binary)
102 # Target file name (without extension).
103 TARGET = ### INSERT NAME OF MAIN FILENAME HERE, WITHOUT EXTENSION ###
106 # Object files directory
107 # To put object files in current directory, use a dot (.), do NOT make
108 # this an empty or blank macro!
112 # Path to the LUFA library
113 LUFA_PATH = ### INSERT PATH TO LUFA LIBRARY RELATIVE TO PROJECT DIRECTORY HERE ###
116 # LUFA library compile-time options and predefined tokens (add '-D' before each token)
117 LUFA_OPTS = ### INSERT LUFA COMPILE TIME TOKES HERE ###
120 # Create the LUFA source path variables by including the LUFA root makefile
121 include $(LUFA_PATH)/LUFA/makefile
124 # List C source files here. (C dependencies are automatically generated.)
128 ### INSERT ADDITIONAL PROJECT SOURCE FILENAMES OR LUFA MODULE NAMES HERE ###
131 # List C++ source files here. (C dependencies are automatically generated.)
135 # List Assembler source files here.
136 # Make them always end in a capital .S. Files ending in a lowercase .s
137 # will not be considered source files but generated files (assembler
138 # output from the compiler), and will be deleted upon "make clean"!
139 # Even though the DOS/Win* filesystem matches both .s and .S the same,
140 # it will preserve the spelling of the filenames, and gcc itself does
141 # care about how the name is spelled on its command-line.
145 # Optimization level, can be [0, 1, 2, 3, s].
146 # 0 = turn off optimization. s = optimize for size.
147 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
151 # List any extra directories to look for include files here.
152 # Each directory must be seperated by a space.
153 # Use forward slashes for directory separators.
154 # For a directory that has spaces, enclose it in quotes.
155 EXTRAINCDIRS = $(LUFA_PATH)/
158 # Compiler flag to set the C Standard level.
160 # gnu89 = c89 plus GCC extensions
161 # c99 = ISO C99 standard (not yet fully implemented)
162 # gnu99 = c99 plus GCC extensions
163 CSTANDARD = -std=gnu99
166 # Place -D or -U options here for C sources
167 CDEFS = -DF_CPU=$(F_CPU)UL
168 CDEFS += -DF_USB=$(F_USB)UL
169 CDEFS += -DBOARD=BOARD_$(BOARD)
170 CDEFS += -DARCH=ARCH_$(ARCH)
171 CDEFS += $(LUFA_OPTS)
174 # Place -D or -U options here for ASM sources
175 ADEFS = -DF_CPU=$(F_CPU)
176 ADEFS += -DF_USB=$(F_USB)UL
177 ADEFS += -DBOARD=BOARD_$(BOARD)
178 ADEFS += -DARCH=ARCH_$(ARCH)
179 ADEFS += $(LUFA_OPTS)
181 # Place -D or -U options here for C++ sources
182 CPPDEFS = -DF_CPU=$(F_CPU)UL
183 CPPDEFS += -DF_USB=$(F_USB)UL
184 CPPDEFS += -DBOARD=BOARD_$(BOARD)
185 CPPDEFS += -DARCH=ARCH_$(ARCH)
186 CPPDEFS += $(LUFA_OPTS)
190 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
191 # AVR Studio 4.10 requires dwarf-2.
192 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
196 #---------------- Compiler Options C ----------------
197 # -g*: generate debugging information
198 # -O*: optimization level
199 # -f...: tuning, see GCC manual and avr-libc documentation
200 # -Wall...: warning level
201 # -Wa,...: tell GCC to pass this to the assembler.
202 # -adhlns...: create assembler listing
206 CFLAGS += -funsigned-char
207 CFLAGS += -funsigned-bitfields
208 CFLAGS += -ffunction-sections
209 CFLAGS += -fno-inline-small-functions
210 CFLAGS += -fpack-struct
211 CFLAGS += -fshort-enums
212 CFLAGS += -fno-strict-aliasing
214 CFLAGS += -Wstrict-prototypes
215 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
216 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
217 CFLAGS += $(CSTANDARD)
220 #---------------- Compiler Options C++ ----------------
221 # -g*: generate debugging information
222 # -O*: optimization level
223 # -f...: tuning, see GCC manual and avr-libc documentation
224 # -Wall...: warning level
225 # -Wa,...: tell GCC to pass this to the assembler.
226 # -adhlns...: create assembler listing
227 CPPFLAGS = -g$(DEBUG)
228 CPPFLAGS += $(CPPDEFS)
230 CPPFLAGS += -funsigned-char
231 CPPFLAGS += -funsigned-bitfields
232 CPPFLAGS += -fpack-struct
233 CPPFLAGS += -fshort-enums
234 CPPFLAGS += -ffunction-sections
235 CPPFLAGS += -fno-strict-aliasing
236 CPPFLAGS += -fno-exceptions
239 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
240 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
241 #CPPFLAGS += $(CSTANDARD)
244 #---------------- Assembler Options ----------------
245 # -Wa,...: tell GCC to pass this to the assembler.
246 # -adhlns: create listing
247 # -gstabs: have the assembler create line number information; note that
248 # for use in COFF files, additional information about filenames
249 # and function names needs to be present in the assembler source
250 # files -- see avr-libc docs [FIXME: not yet described there]
251 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
252 # dump that will be displayed for a given single line of source input.
253 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
256 #---------------- Library Options ----------------
257 # Minimalistic printf version
258 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
260 # Floating point printf version (requires MATH_LIB = -lm below)
261 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
263 # If this is left blank, then it will use the Standard printf version.
265 #PRINTF_LIB = $(PRINTF_LIB_MIN)
266 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
269 # Minimalistic scanf version
270 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
272 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
273 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
275 # If this is left blank, then it will use the Standard scanf version.
277 #SCANF_LIB = $(SCANF_LIB_MIN)
278 #SCANF_LIB = $(SCANF_LIB_FLOAT)
284 # List any extra directories to look for libraries here.
285 # Each directory must be seperated by a space.
286 # Use forward slashes for directory separators.
287 # For a directory that has spaces, enclose it in quotes.
292 #---------------- External Memory Options ----------------
294 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
295 # used for variables (.data/.bss) and heap (malloc()).
296 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
298 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
299 # only used for heap (malloc()).
300 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
306 #---------------- Linker Options ----------------
307 # -Wl,...: tell GCC to pass this to linker.
308 # -Map: create map file
309 # --cref: add cross reference to map file
310 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
311 LDFLAGS += -Wl,--relax
312 LDFLAGS += -Wl,--gc-sections
313 LDFLAGS += $(EXTMEMOPTS)
314 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
315 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
316 #LDFLAGS += -T linker_script.x
320 #---------------- Programming Options (avrdude) ----------------
322 # Programming hardware
324 # to get a full listing.
326 AVRDUDE_PROGRAMMER = jtagmkII
328 # com1 = serial port. Use lpt1 to connect to parallel port.
331 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
332 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
335 # Uncomment the following if you want avrdude's erase cycle counter.
336 # Note that this counter needs to be initialized first using -Yn,
337 # see avrdude manual.
338 #AVRDUDE_ERASE_COUNTER = -y
340 # Uncomment the following if you do /not/ wish a verification to be
341 # performed after programming the device.
342 #AVRDUDE_NO_VERIFY = -V
344 # Increase verbosity level. Please use this when submitting bug
345 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
346 # to submit bug reports.
347 #AVRDUDE_VERBOSE = -v -v
349 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
350 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
351 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
352 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
356 #---------------- Debugging Options ----------------
358 # For simulavr only - target MCU frequency.
359 DEBUG_MFREQ = $(F_CPU)
361 # Set the DEBUG_UI to either gdb or insight.
365 # Set the debugging back-end to either avarice, simulavr.
366 DEBUG_BACKEND = avarice
367 #DEBUG_BACKEND = simulavr
370 GDBINIT_FILE = __avr_gdbinit
372 # When using avarice settings for the JTAG
375 # Debugging port used to communicate between GDB / avarice / simulavr.
378 # Debugging host used to communicate between GDB / avarice / simulavr, normally
379 # just set to localhost unless doing some sort of crazy debugging when
380 # avarice is running on a different computer.
381 DEBUG_HOST = localhost
385 #============================================================================
388 # Define programs and commands.
391 OBJCOPY = avr-objcopy
392 OBJDUMP = avr-objdump
405 MSG_ERRORS_NONE = Errors: none
406 MSG_BEGIN = -------- begin --------
407 MSG_END = -------- end --------
408 MSG_SIZE_BEFORE = Size before:
409 MSG_SIZE_AFTER = Size after:
410 MSG_COFF = Converting to AVR COFF:
411 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
412 MSG_FLASH = Creating load file for Flash:
413 MSG_EEPROM = Creating load file for EEPROM:
414 MSG_EXTENDED_LISTING = Creating Extended Listing:
415 MSG_SYMBOL_TABLE = Creating Symbol Table:
416 MSG_LINKING = Linking:
417 MSG_COMPILING = Compiling C:
418 MSG_COMPILING_CPP = Compiling C++:
419 MSG_ASSEMBLING = Assembling:
420 MSG_CLEANING = Cleaning project:
421 MSG_CREATING_LIBRARY = Creating library:
426 # Define all object files.
427 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
429 # Define all listing files.
430 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
433 # Compiler flags to generate dependency files.
434 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
437 # Combine all necessary flags and optional flags.
438 # Add target processor to flags.
439 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
440 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
441 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
448 all: begin gccversion sizebefore build sizeafter end
450 # Change the build target to build a HEX file or a library.
451 build: elf hex eep lss sym
460 LIBNAME=lib$(TARGET).a
466 # AVR Studio 3.x does not check make's exit code but relies on
467 # the following magic strings to be generated by the compile job.
477 # Display size of file.
478 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
479 ELFSIZE = $(SIZE) $(MCU_FLAG) $(FORMAT_FLAG) $(TARGET).elf
480 MCU_FLAG = $(shell $(SIZE) --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
481 FORMAT_FLAG = $(shell $(SIZE) --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
485 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
486 2>/dev/null; echo; fi
489 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
490 2>/dev/null; echo; fi
494 # Display compiler version information.
499 # Program the device.
500 program: $(TARGET).hex $(TARGET).eep
501 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
504 batchisp -hardware usb -device $(MCU) -operation erase f
505 batchisp -hardware usb -device $(MCU) -operation loadbuffer $(TARGET).hex program
506 batchisp -hardware usb -device $(MCU) -operation start reset 0
509 dfu-programmer $(MCU) erase
510 dfu-programmer $(MCU) flash $(TARGET).hex
511 dfu-programmer $(MCU) reset
513 flip-ee: $(TARGET).hex $(TARGET).eep
514 $(COPY) $(TARGET).eep $(TARGET)eep.hex
515 batchisp -hardware usb -device $(MCU) -operation memory EEPROM erase
516 batchisp -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(TARGET)eep.hex program
517 batchisp -hardware usb -device $(MCU) -operation start reset 0
518 $(REMOVE) $(TARGET)eep.hex
520 dfu-ee: $(TARGET).hex $(TARGET).eep
521 dfu-programmer $(MCU) eeprom-flash $(TARGET).eep
522 dfu-programmer $(MCU) reset
525 # Generate avr-gdb config/init file which does the following:
526 # define the reset signal, load the target file, connect to target, and set
527 # a breakpoint at main().
529 @$(REMOVE) $(GDBINIT_FILE)
530 @echo define reset >> $(GDBINIT_FILE)
531 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
532 @echo end >> $(GDBINIT_FILE)
533 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
534 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
535 ifeq ($(DEBUG_BACKEND),simulavr)
536 @echo load >> $(GDBINIT_FILE)
538 @echo break main >> $(GDBINIT_FILE)
540 debug: gdb-config $(TARGET).elf
541 ifeq ($(DEBUG_BACKEND), avarice)
542 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
543 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
544 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
545 @$(WINSHELL) /c pause
548 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
549 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
551 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
556 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
557 COFFCONVERT = $(OBJCOPY) --debugging
558 COFFCONVERT += --change-section-address .data-0x800000
559 COFFCONVERT += --change-section-address .bss-0x800000
560 COFFCONVERT += --change-section-address .noinit-0x800000
561 COFFCONVERT += --change-section-address .eeprom-0x810000
567 @echo $(MSG_COFF) $(TARGET).cof
568 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
571 extcoff: $(TARGET).elf
573 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
574 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
578 # Create final output files (.hex, .eep) from ELF output file.
581 @echo $(MSG_FLASH) $@
582 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
586 @echo $(MSG_EEPROM) $@
587 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
588 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
590 # Create extended listing file from ELF output file.
593 @echo $(MSG_EXTENDED_LISTING) $@
594 $(OBJDUMP) -h -S -z $< > $@
596 # Create a symbol table from ELF output file.
599 @echo $(MSG_SYMBOL_TABLE) $@
604 # Create library from object files.
605 .SECONDARY : $(TARGET).a
609 @echo $(MSG_CREATING_LIBRARY) $@
613 # Link: create ELF output file from object files.
614 .SECONDARY : $(TARGET).elf
618 @echo $(MSG_LINKING) $@
619 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
622 # Compile: create object files from C source files.
625 @echo $(MSG_COMPILING) $<
626 $(CC) -c $(ALL_CFLAGS) $< -o $@
629 # Compile: create object files from C++ source files.
630 $(OBJDIR)/%.o : %.cpp
632 @echo $(MSG_COMPILING_CPP) $<
633 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
636 # Compile: create assembler files from C source files.
638 $(CC) -S $(ALL_CFLAGS) $< -o $@
641 # Compile: create assembler files from C++ source files.
643 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
646 # Assemble: create object files from assembler source files.
649 @echo $(MSG_ASSEMBLING) $<
650 $(CC) -c $(ALL_ASFLAGS) $< -o $@
653 # Create preprocessed source for use in sending a bug report.
655 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
658 # Target: clean project.
659 clean: begin clean_list end
663 @echo $(MSG_CLEANING)
664 $(REMOVE) $(TARGET).hex
665 $(REMOVE) $(TARGET).eep
666 $(REMOVE) $(TARGET).cof
667 $(REMOVE) $(TARGET).elf
668 $(REMOVE) $(TARGET).map
669 $(REMOVE) $(TARGET).sym
670 $(REMOVE) $(TARGET).lss
671 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
672 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
673 $(REMOVE) $(SRC:.c=.s)
674 $(REMOVE) $(SRC:.c=.d)
675 $(REMOVE) $(SRC:.c=.i)
679 @echo Generating Project Documentation...
680 @doxygen Doxygen.conf
681 @echo Documentation Generation Complete.
687 @for f in $(SRC) $(CPPSRC) $(ASRC); do \
688 if [ -f $$f ]; then \
689 echo "Found Source File: $$f" ; \
691 echo "Source File Not Found: $$f" ; \
695 # Create object files directory
696 $(shell mkdir $(OBJDIR) 2>/dev/null)
699 # Include the dependency files.
700 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
703 # Listing of phony targets.
704 .PHONY : all begin finish end sizebefore sizeafter gccversion \
705 build elf hex eep lss sym coff extcoff doxygen clean \
706 clean_list clean_doxygen program dfu flip flip-ee dfu-ee \
707 debug gdb-config checksource