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 dfu = Download the hex file to the device, using dfu-programmer (must
29 # have dfu-programmer installed).
31 # make flip = Download the hex file to the device, using Atmel FLIP (must
32 # have Atmel FLIP installed).
34 # make doxygen = Generate DoxyGen documentation for the project (must have
37 # make filename.s = Just compile filename.c into the assembler code only.
39 # make filename.i = Create a preprocessed source file for use in submitting
40 # bug reports to the GCC project.
42 # To rebuild project do "make clean" then "make all".
43 #----------------------------------------------------------------------------
47 MCU = ### INSERT NAME OF MICROCONTROLLER MODEL HERE ###
50 # Targeted chip architecture (see library "Architectures" documentation)
54 # Target board (see library "Board Types" documentation, NONE for projects not requiring
55 # LUFA board drivers). If USER is selected, put custom board drivers in a directory called
56 # "Board" inside the application directory.
57 BOARD = ### INSERT NAME OF BOARD HERE, OR NONE IF NO BOARD DRIVERS USED ###
60 # Processor frequency.
61 # This will define a symbol, F_CPU, in all source code files equal to the
62 # processor frequency in Hz. You can then use this symbol in your source code to
63 # calculate timings. Do NOT tack on a 'UL' at the end, this will be done
64 # automatically to create a 32-bit value in your source code.
66 # This should be the frequency the system core runs at, after the system clock
67 # has been set up correctly and started.
68 F_CPU = ### INSERT PRESCALED SYSTEM CLOCK SPEED HERE, IN HZ ###
71 # Input clock frequency.
72 # This will define a symbol, F_USB, in all source code files equal to the
73 # input clock frequency (before any prescaling is performed) in Hz. This value may
74 # differ from F_CPU, as the USB clock is often sourced from the same oscillator as
75 # the CPU clock, but run through multipliers and dividers to achieve the desired
78 # For the UC3 chips, this should be equal to 48MHz or 96MHz.
79 F_USB = ### INSERT CLOCK TO USB MODULE HERE, IN HZ ###
82 # Output format. (can be srec, ihex, binary)
86 # Target file name (without extension).
87 TARGET = ### INSERT NAME OF MAIN FILENAME HERE, WITHOUT EXTENSION ###
90 # Object files directory
91 # To put object files in current directory, use a dot (.), do NOT make
92 # this an empty or blank macro!
96 # Path to the LUFA library
97 LUFA_PATH = ### INSERT PATH TO LUFA LIBRARY RELATIVE TO PROJECT DIRECTORY HERE ###
100 # LUFA library compile-time options and predefined tokens (add '-D' before each token)
101 LUFA_OPTS = ### INSERT LUFA COMPILE TIME TOKES HERE ###
104 # Create the LUFA source path variables by including the LUFA root makefile
105 include $(LUFA_PATH)/LUFA/makefile
108 # List C source files here. (C dependencies are automatically generated.)
112 ### INSERT ADDITIONAL PROJECT SOURCE FILENAMES OR LUFA MODULE NAMES HERE ###
115 # List C++ source files here. (C dependencies are automatically generated.)
119 # List Assembler source files here.
120 # Make them always end in a capital .S. Files ending in a lowercase .s
121 # will not be considered source files but generated files (assembler
122 # output from the compiler), and will be deleted upon "make clean"!
123 # Even though the DOS/Win* filesystem matches both .s and .S the same,
124 # it will preserve the spelling of the filenames, and gcc itself does
125 # care about how the name is spelled on its command-line.
129 # Optimization level, can be [0, 1, 2, 3, s].
130 # 0 = turn off optimization. s = optimize for size.
131 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
135 # List any extra directories to look for include files here.
136 # Each directory must be seperated by a space.
137 # Use forward slashes for directory separators.
138 # For a directory that has spaces, enclose it in quotes.
139 EXTRAINCDIRS = $(LUFA_PATH)/
142 # Compiler flag to set the C Standard level.
144 # gnu89 = c89 plus GCC extensions
145 # c99 = ISO C99 standard (not yet fully implemented)
146 # gnu99 = c99 plus GCC extensions
147 CSTANDARD = -std=gnu99
150 # Place -D or -U options here for C sources
151 CDEFS = -DF_CPU=$(F_CPU)UL
152 CDEFS += -DF_USB=$(F_USB)UL
153 CDEFS += -DBOARD=BOARD_$(BOARD)
154 CDEFS += -DARCH=ARCH_$(ARCH)
155 CDEFS += $(LUFA_OPTS)
158 # Place -D or -U options here for ASM sources
159 ADEFS = -DF_CPU=$(F_CPU)
160 ADEFS += -DF_USB=$(F_USB)UL
161 ADEFS += -DBOARD=BOARD_$(BOARD)
162 ADEFS += -DARCH=ARCH_$(ARCH)
163 ADEFS += $(LUFA_OPTS)
165 # Place -D or -U options here for C++ sources
166 CPPDEFS = -DF_CPU=$(F_CPU)UL
167 CPPDEFS += -DF_USB=$(F_USB)UL
168 CPPDEFS += -DBOARD=BOARD_$(BOARD)
169 CPPDEFS += -DARCH=ARCH_$(ARCH)
170 CPPDEFS += $(LUFA_OPTS)
177 #---------------- Compiler Options C ----------------
178 # -g*: generate debugging information
179 # -O*: optimization level
180 # -f...: tuning, see GCC manual and avr-libc documentation
181 # -Wall...: warning level
182 # -Wa,...: tell GCC to pass this to the assembler.
183 # -adhlns...: create assembler listing
187 CFLAGS += -funsigned-char
188 CFLAGS += -funsigned-bitfields
189 CFLAGS += -ffunction-sections
190 CFLAGS += -fno-strict-aliasing
192 CFLAGS += -Wstrict-prototypes
193 CFLAGS += -masm-addr-pseudos
194 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
195 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
196 CFLAGS += $(CSTANDARD)
199 #---------------- Compiler Options C++ ----------------
200 # -g*: generate debugging information
201 # -O*: optimization level
202 # -f...: tuning, see GCC manual and avr-libc documentation
203 # -Wall...: warning level
204 # -Wa,...: tell GCC to pass this to the assembler.
205 # -adhlns...: create assembler listing
206 CPPFLAGS = -g$(DEBUG)
207 CPPFLAGS += $(CPPDEFS)
209 CPPFLAGS += -funsigned-char
210 CPPFLAGS += -funsigned-bitfields
211 CPPFLAGS += -ffunction-sections
212 CPPFLAGS += -fno-strict-aliasing
213 CPPFLAGS += -fno-exceptions
214 CPPFLAGS += -masm-addr-pseudos
217 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
218 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
219 #CPPFLAGS += $(CSTANDARD)
222 #---------------- Assembler Options ----------------
223 # -Wa,...: tell GCC to pass this to the assembler.
224 # -adhlns: create listing
225 # -gstabs: have the assembler create line number information; note that
226 # for use in COFF files, additional information about filenames
227 # and function names needs to be present in the assembler source
228 # files -- see avr-libc docs [FIXME: not yet described there]
229 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
230 # dump that will be displayed for a given single line of source input.
231 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
234 #---------------- Linker Options ----------------
235 # -Wl,...: tell GCC to pass this to linker.
236 # -Map: create map file
237 # --cref: add cross reference to map file
238 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
239 LDFLAGS += -Wl,--relax
240 LDFLAGS += -Wl,--gc-sections
241 LDFLAGS += -Wl,--rodata-writable
242 LDFLAGS += -Wl,--direct-data
243 #LDFLAGS += -T linker_script.x
246 #============================================================================
249 # Define programs and commands.
252 OBJCOPY = avr32-objcopy
253 OBJDUMP = avr32-objdump
265 MSG_ERRORS_NONE = Errors: none
266 MSG_BEGIN = -------- begin --------
267 MSG_END = -------- end --------
268 MSG_SIZE_BEFORE = Size before:
269 MSG_SIZE_AFTER = Size after:
270 MSG_COFF = Converting to AVR COFF:
271 MSG_FLASH = Creating load file for Flash:
272 MSG_EEPROM = Creating load file for EEPROM:
273 MSG_EXTENDED_LISTING = Creating Extended Listing:
274 MSG_SYMBOL_TABLE = Creating Symbol Table:
275 MSG_LINKING = Linking:
276 MSG_COMPILING = Compiling C:
277 MSG_COMPILING_CPP = Compiling C++:
278 MSG_ASSEMBLING = Assembling:
279 MSG_CLEANING = Cleaning project:
280 MSG_CREATING_LIBRARY = Creating library:
285 # Define all object files.
286 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
288 # Define all listing files.
289 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
292 # Compiler flags to generate dependency files.
293 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
296 # Combine all necessary flags and optional flags.
297 # Add target processor to flags.
298 ALL_CFLAGS = -mpart=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
299 ALL_CPPFLAGS = -mpart=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
300 ALL_ASFLAGS = -mpart=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
307 all: begin gccversion sizebefore build sizeafter end
309 # Change the build target to build a HEX file or a library.
310 build: elf hex lss sym
318 LIBNAME=lib$(TARGET).a
324 # AVR Studio 3.x does not check make's exit code but relies on
325 # the following magic strings to be generated by the compile job.
335 # Display size of file.
336 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
337 ELFSIZE = $(SIZE) $(MCU_FLAG) $(FORMAT_FLAG) $(TARGET).elf
338 MCU_FLAG = $(shell $(SIZE) --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
339 FORMAT_FLAG = $(shell $(SIZE) --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
343 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
344 2>/dev/null; echo; fi
347 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
348 2>/dev/null; echo; fi
352 # Display compiler version information.
357 # Program the device.
359 batchisp -hardware usb -device $(MCU) -operation erase f
360 batchisp -hardware usb -device $(MCU) -operation loadbuffer $(TARGET).hex program
361 batchisp -hardware usb -device $(MCU) -operation start reset 0
364 dfu-programmer $(MCU) erase
365 dfu-programmer $(MCU) flash $(TARGET).hex
366 dfu-programmer $(MCU) reset
369 # Create final output files (.hex, .eep) from ELF output file.
372 @echo $(MSG_FLASH) $@
373 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
375 # Create extended listing file from ELF output file.
378 @echo $(MSG_EXTENDED_LISTING) $@
379 $(OBJDUMP) -h -S -z $< > $@
381 # Create a symbol table from ELF output file.
384 @echo $(MSG_SYMBOL_TABLE) $@
389 # Create library from object files.
390 .SECONDARY : $(TARGET).a
394 @echo $(MSG_CREATING_LIBRARY) $@
398 # Link: create ELF output file from object files.
399 .SECONDARY : $(TARGET).elf
403 @echo $(MSG_LINKING) $@
404 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
407 # Compile: create object files from C source files.
410 @echo $(MSG_COMPILING) $<
411 $(CC) -c $(ALL_CFLAGS) $< -o $@
414 # Compile: create object files from C++ source files.
415 $(OBJDIR)/%.o : %.cpp
417 @echo $(MSG_COMPILING_CPP) $<
418 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
421 # Compile: create assembler files from C source files.
423 $(CC) -S $(ALL_CFLAGS) $< -o $@
426 # Compile: create assembler files from C++ source files.
428 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
431 # Assemble: create object files from assembler source files.
434 @echo $(MSG_ASSEMBLING) $<
435 $(CC) -c $(ALL_ASFLAGS) $< -o $@
438 # Create preprocessed source for use in sending a bug report.
440 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
443 # Target: clean project.
444 clean: begin clean_list end
448 @echo $(MSG_CLEANING)
449 $(REMOVE) $(TARGET).hex
450 $(REMOVE) $(TARGET).cof
451 $(REMOVE) $(TARGET).elf
452 $(REMOVE) $(TARGET).map
453 $(REMOVE) $(TARGET).sym
454 $(REMOVE) $(TARGET).lss
455 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
456 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
457 $(REMOVE) $(SRC:.c=.s)
458 $(REMOVE) $(SRC:.c=.d)
459 $(REMOVE) $(SRC:.c=.i)
463 @echo Generating Project Documentation...
464 @doxygen Doxygen.conf
465 @echo Documentation Generation Complete.
470 # Create object files directory
471 $(shell mkdir $(OBJDIR) 2>/dev/null)
474 # Include the dependency files.
475 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
478 # Listing of phony targets.
479 .PHONY : all begin finish end sizebefore sizeafter gccversion \
480 build elf hex lss sym doxygen clean clean_list clean_doxygen \