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.)
50 usb_keyboard_debug.c \
54 # MCU name, you MUST set this to match the board you are using
55 # type "make clean" after changing this, so all files will be rebuilt
56 #MCU = at90usb162 # Teensy 1.0
57 MCU = atmega32u4 # Teensy 2.0
58 #MCU = at90usb646 # Teensy++ 1.0
59 #MCU = at90usb1286 # Teensy++ 2.0
62 # Processor frequency.
63 # Normally the first thing your program should do is set the clock prescaler,
64 # so your program will run at the correct speed. You should also set this
65 # variable to same clock speed. The _delay_ms() macro uses this, and many
66 # examples use this variable to calculate timings. Do not add a "UL" here.
70 # Output format. (can be srec, ihex, binary)
74 # Object files directory
75 # To put object files in current directory, use a dot (.), do NOT make
76 # this an empty or blank macro!
80 # List C++ source files here. (C dependencies are automatically generated.)
84 # List Assembler source files here.
85 # Make them always end in a capital .S. Files ending in a lowercase .s
86 # will not be considered source files but generated files (assembler
87 # output from the compiler), and will be deleted upon "make clean"!
88 # Even though the DOS/Win* filesystem matches both .s and .S the same,
89 # it will preserve the spelling of the filenames, and gcc itself does
90 # care about how the name is spelled on its command-line.
94 # Optimization level, can be [0, 1, 2, 3, s].
95 # 0 = turn off optimization. s = optimize for size.
96 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
101 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
102 # AVR Studio 4.10 requires dwarf-2.
103 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
107 # List any extra directories to look for include files here.
108 # Each directory must be seperated by a space.
109 # Use forward slashes for directory separators.
110 # For a directory that has spaces, enclose it in quotes.
114 # Compiler flag to set the C Standard level.
116 # gnu89 = c89 plus GCC extensions
117 # c99 = ISO C99 standard (not yet fully implemented)
118 # gnu99 = c99 plus GCC extensions
119 CSTANDARD = -std=gnu99
122 # Place -D or -U options here for C sources
123 CDEFS = -DF_CPU=$(F_CPU)UL
126 # Place -D or -U options here for ASM sources
127 ADEFS = -DF_CPU=$(F_CPU)
130 # Place -D or -U options here for C++ sources
131 CPPDEFS = -DF_CPU=$(F_CPU)UL
132 #CPPDEFS += -D__STDC_LIMIT_MACROS
133 #CPPDEFS += -D__STDC_CONSTANT_MACROS
137 #---------------- Compiler Options C ----------------
138 # -g*: generate debugging information
139 # -O*: optimization level
140 # -f...: tuning, see GCC manual and avr-libc documentation
141 # -Wall...: warning level
142 # -Wa,...: tell GCC to pass this to the assembler.
143 # -adhlns...: create assembler listing
147 CFLAGS += -funsigned-char
148 CFLAGS += -funsigned-bitfields
149 CFLAGS += -ffunction-sections
150 CFLAGS += -fpack-struct
151 CFLAGS += -fshort-enums
153 CFLAGS += -Wstrict-prototypes
154 #CFLAGS += -mshort-calls
155 #CFLAGS += -fno-unit-at-a-time
157 #CFLAGS += -Wunreachable-code
158 #CFLAGS += -Wsign-compare
159 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
160 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
161 CFLAGS += $(CSTANDARD)
164 #---------------- Compiler Options C++ ----------------
165 # -g*: generate debugging information
166 # -O*: optimization level
167 # -f...: tuning, see GCC manual and avr-libc documentation
168 # -Wall...: warning level
169 # -Wa,...: tell GCC to pass this to the assembler.
170 # -adhlns...: create assembler listing
171 CPPFLAGS = -g$(DEBUG)
172 CPPFLAGS += $(CPPDEFS)
174 CPPFLAGS += -funsigned-char
175 CPPFLAGS += -funsigned-bitfields
176 CPPFLAGS += -fpack-struct
177 CPPFLAGS += -fshort-enums
178 CPPFLAGS += -fno-exceptions
181 #CPPFLAGS += -mshort-calls
182 #CPPFLAGS += -fno-unit-at-a-time
183 #CPPFLAGS += -Wstrict-prototypes
184 #CPPFLAGS += -Wunreachable-code
185 #CPPFLAGS += -Wsign-compare
186 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
187 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
188 #CPPFLAGS += $(CSTANDARD)
191 #---------------- Assembler Options ----------------
192 # -Wa,...: tell GCC to pass this to the assembler.
193 # -adhlns: create listing
194 # -gstabs: have the assembler create line number information; note that
195 # for use in COFF files, additional information about filenames
196 # and function names needs to be present in the assembler source
197 # files -- see avr-libc docs [FIXME: not yet described there]
198 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
199 # dump that will be displayed for a given single line of source input.
200 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
203 #---------------- Library Options ----------------
204 # Minimalistic printf version
205 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
207 # Floating point printf version (requires MATH_LIB = -lm below)
208 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
210 # If this is left blank, then it will use the Standard printf version.
212 #PRINTF_LIB = $(PRINTF_LIB_MIN)
213 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
216 # Minimalistic scanf version
217 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
219 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
220 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
222 # If this is left blank, then it will use the Standard scanf version.
224 #SCANF_LIB = $(SCANF_LIB_MIN)
225 #SCANF_LIB = $(SCANF_LIB_FLOAT)
231 # List any extra directories to look for libraries here.
232 # Each directory must be seperated by a space.
233 # Use forward slashes for directory separators.
234 # For a directory that has spaces, enclose it in quotes.
239 #---------------- External Memory Options ----------------
241 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
242 # used for variables (.data/.bss) and heap (malloc()).
243 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
245 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
246 # only used for heap (malloc()).
247 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
253 #---------------- Linker Options ----------------
254 # -Wl,...: tell GCC to pass this to linker.
255 # -Map: create map file
256 # --cref: add cross reference to map file
257 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
258 LDFLAGS += -Wl,--relax
259 LDFLAGS += -Wl,--gc-sections
260 LDFLAGS += $(EXTMEMOPTS)
261 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
262 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
263 #LDFLAGS += -T linker_script.x
267 #---------------- Programming Options (avrdude) ----------------
269 # Programming hardware
271 # to get a full listing.
273 AVRDUDE_PROGRAMMER = stk500v2
275 # com1 = serial port. Use lpt1 to connect to parallel port.
276 AVRDUDE_PORT = com1 # programmer connected to serial device
278 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
279 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
282 # Uncomment the following if you want avrdude's erase cycle counter.
283 # Note that this counter needs to be initialized first using -Yn,
284 # see avrdude manual.
285 #AVRDUDE_ERASE_COUNTER = -y
287 # Uncomment the following if you do /not/ wish a verification to be
288 # performed after programming the device.
289 #AVRDUDE_NO_VERIFY = -V
291 # Increase verbosity level. Please use this when submitting bug
292 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
293 # to submit bug reports.
294 #AVRDUDE_VERBOSE = -v -v
296 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
297 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
298 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
299 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
303 #---------------- Debugging Options ----------------
305 # For simulavr only - target MCU frequency.
306 DEBUG_MFREQ = $(F_CPU)
308 # Set the DEBUG_UI to either gdb or insight.
312 # Set the debugging back-end to either avarice, simulavr.
313 DEBUG_BACKEND = avarice
314 #DEBUG_BACKEND = simulavr
317 GDBINIT_FILE = __avr_gdbinit
319 # When using avarice settings for the JTAG
322 # Debugging port used to communicate between GDB / avarice / simulavr.
325 # Debugging host used to communicate between GDB / avarice / simulavr, normally
326 # just set to localhost unless doing some sort of crazy debugging when
327 # avarice is running on a different computer.
328 DEBUG_HOST = localhost
332 #============================================================================
335 # Define programs and commands.
338 OBJCOPY = avr-objcopy
339 OBJDUMP = avr-objdump
352 MSG_ERRORS_NONE = Errors: none
353 MSG_BEGIN = -------- begin --------
354 MSG_END = -------- end --------
355 MSG_SIZE_BEFORE = Size before:
356 MSG_SIZE_AFTER = Size after:
357 MSG_COFF = Converting to AVR COFF:
358 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
359 MSG_FLASH = Creating load file for Flash:
360 MSG_EEPROM = Creating load file for EEPROM:
361 MSG_EXTENDED_LISTING = Creating Extended Listing:
362 MSG_SYMBOL_TABLE = Creating Symbol Table:
363 MSG_LINKING = Linking:
364 MSG_COMPILING = Compiling C:
365 MSG_COMPILING_CPP = Compiling C++:
366 MSG_ASSEMBLING = Assembling:
367 MSG_CLEANING = Cleaning project:
368 MSG_CREATING_LIBRARY = Creating library:
373 # Define all object files.
374 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
376 # Define all listing files.
377 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
380 # Compiler flags to generate dependency files.
381 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
384 # Combine all necessary flags and optional flags.
385 # Add target processor to flags.
386 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
387 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
388 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
395 all: begin gccversion sizebefore build sizeafter end
397 # Change the build target to build a HEX file or a library.
398 build: elf hex eep lss sym
407 LIBNAME=lib$(TARGET).a
413 # AVR Studio 3.x does not check make's exit code but relies on
414 # the following magic strings to be generated by the compile job.
424 # Display size of file.
425 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
426 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
427 ELFSIZE = $(SIZE) $(TARGET).elf
430 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
431 2>/dev/null; echo; fi
434 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
435 2>/dev/null; echo; fi
439 # Display compiler version information.
445 # Program the device.
446 program: $(TARGET).hex $(TARGET).eep
447 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
450 # Generate avr-gdb config/init file which does the following:
451 # define the reset signal, load the target file, connect to target, and set
452 # a breakpoint at main().
454 @$(REMOVE) $(GDBINIT_FILE)
455 @echo define reset >> $(GDBINIT_FILE)
456 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
457 @echo end >> $(GDBINIT_FILE)
458 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
459 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
460 ifeq ($(DEBUG_BACKEND),simulavr)
461 @echo load >> $(GDBINIT_FILE)
463 @echo break main >> $(GDBINIT_FILE)
465 debug: gdb-config $(TARGET).elf
466 ifeq ($(DEBUG_BACKEND), avarice)
467 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
468 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
469 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
470 @$(WINSHELL) /c pause
473 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
474 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
476 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
481 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
482 COFFCONVERT = $(OBJCOPY) --debugging
483 COFFCONVERT += --change-section-address .data-0x800000
484 COFFCONVERT += --change-section-address .bss-0x800000
485 COFFCONVERT += --change-section-address .noinit-0x800000
486 COFFCONVERT += --change-section-address .eeprom-0x810000
492 @echo $(MSG_COFF) $(TARGET).cof
493 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
496 extcoff: $(TARGET).elf
498 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
499 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
503 # Create final output files (.hex, .eep) from ELF output file.
506 @echo $(MSG_FLASH) $@
507 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
511 @echo $(MSG_EEPROM) $@
512 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
513 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
515 # Create extended listing file from ELF output file.
518 @echo $(MSG_EXTENDED_LISTING) $@
519 $(OBJDUMP) -h -S -z $< > $@
521 # Create a symbol table from ELF output file.
524 @echo $(MSG_SYMBOL_TABLE) $@
529 # Create library from object files.
530 .SECONDARY : $(TARGET).a
534 @echo $(MSG_CREATING_LIBRARY) $@
538 # Link: create ELF output file from object files.
539 .SECONDARY : $(TARGET).elf
543 @echo $(MSG_LINKING) $@
544 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
547 # Compile: create object files from C source files.
550 @echo $(MSG_COMPILING) $<
551 $(CC) -c $(ALL_CFLAGS) $< -o $@
554 # Compile: create object files from C++ source files.
555 $(OBJDIR)/%.o : %.cpp
557 @echo $(MSG_COMPILING_CPP) $<
558 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
561 # Compile: create assembler files from C source files.
563 $(CC) -S $(ALL_CFLAGS) $< -o $@
566 # Compile: create assembler files from C++ source files.
568 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
571 # Assemble: create object files from assembler source files.
574 @echo $(MSG_ASSEMBLING) $<
575 $(CC) -c $(ALL_ASFLAGS) $< -o $@
578 # Create preprocessed source for use in sending a bug report.
580 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
583 # Target: clean project.
584 clean: begin clean_list end
588 @echo $(MSG_CLEANING)
589 $(REMOVE) $(TARGET).hex
590 $(REMOVE) $(TARGET).eep
591 $(REMOVE) $(TARGET).cof
592 $(REMOVE) $(TARGET).elf
593 $(REMOVE) $(TARGET).map
594 $(REMOVE) $(TARGET).sym
595 $(REMOVE) $(TARGET).lss
596 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
597 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
598 $(REMOVE) $(SRC:.c=.s)
599 $(REMOVE) $(SRC:.c=.d)
600 $(REMOVE) $(SRC:.c=.i)
604 # Create object files directory
605 $(shell mkdir $(OBJDIR) 2>/dev/null)
608 # Include the dependency files.
609 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
612 # Listing of phony targets.
613 .PHONY : all begin finish end sizebefore sizeafter gccversion \
614 build elf hex eep lss sym coff extcoff \
615 clean clean_list program debug gdb-config