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.)
49 usb_keyboard_debug.c \
53 # MCU name, you MUST set this to match the board you are using
54 # type "make clean" after changing this, so all files will be rebuilt
55 #MCU = at90usb162 # Teensy 1.0
56 MCU = atmega32u4 # Teensy 2.0
57 #MCU = at90usb646 # Teensy++ 1.0
58 #MCU = at90usb1286 # Teensy++ 2.0
61 # Processor frequency.
62 # Normally the first thing your program should do is set the clock prescaler,
63 # so your program will run at the correct speed. You should also set this
64 # variable to same clock speed. The _delay_ms() macro uses this, and many
65 # examples use this variable to calculate timings. Do not add a "UL" here.
69 # Output format. (can be srec, ihex, binary)
73 # Object files directory
74 # To put object files in current directory, use a dot (.), do NOT make
75 # this an empty or blank macro!
79 # List C++ source files here. (C dependencies are automatically generated.)
83 # List Assembler source files here.
84 # Make them always end in a capital .S. Files ending in a lowercase .s
85 # will not be considered source files but generated files (assembler
86 # output from the compiler), and will be deleted upon "make clean"!
87 # Even though the DOS/Win* filesystem matches both .s and .S the same,
88 # it will preserve the spelling of the filenames, and gcc itself does
89 # care about how the name is spelled on its command-line.
93 # Optimization level, can be [0, 1, 2, 3, s].
94 # 0 = turn off optimization. s = optimize for size.
95 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
100 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
101 # AVR Studio 4.10 requires dwarf-2.
102 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
106 # List any extra directories to look for include files here.
107 # Each directory must be seperated by a space.
108 # Use forward slashes for directory separators.
109 # For a directory that has spaces, enclose it in quotes.
113 # Compiler flag to set the C Standard level.
115 # gnu89 = c89 plus GCC extensions
116 # c99 = ISO C99 standard (not yet fully implemented)
117 # gnu99 = c99 plus GCC extensions
118 CSTANDARD = -std=gnu99
121 # Place -D or -U options here for C sources
122 CDEFS = -DF_CPU=$(F_CPU)UL
125 # Place -D or -U options here for ASM sources
126 ADEFS = -DF_CPU=$(F_CPU)
129 # Place -D or -U options here for C++ sources
130 CPPDEFS = -DF_CPU=$(F_CPU)UL
131 #CPPDEFS += -D__STDC_LIMIT_MACROS
132 #CPPDEFS += -D__STDC_CONSTANT_MACROS
136 #---------------- Compiler Options C ----------------
137 # -g*: generate debugging information
138 # -O*: optimization level
139 # -f...: tuning, see GCC manual and avr-libc documentation
140 # -Wall...: warning level
141 # -Wa,...: tell GCC to pass this to the assembler.
142 # -adhlns...: create assembler listing
146 CFLAGS += -funsigned-char
147 CFLAGS += -funsigned-bitfields
148 CFLAGS += -ffunction-sections
149 CFLAGS += -fpack-struct
150 CFLAGS += -fshort-enums
152 CFLAGS += -Wstrict-prototypes
153 #CFLAGS += -mshort-calls
154 #CFLAGS += -fno-unit-at-a-time
156 #CFLAGS += -Wunreachable-code
157 #CFLAGS += -Wsign-compare
158 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
159 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
160 CFLAGS += $(CSTANDARD)
163 #---------------- Compiler Options C++ ----------------
164 # -g*: generate debugging information
165 # -O*: optimization level
166 # -f...: tuning, see GCC manual and avr-libc documentation
167 # -Wall...: warning level
168 # -Wa,...: tell GCC to pass this to the assembler.
169 # -adhlns...: create assembler listing
170 CPPFLAGS = -g$(DEBUG)
171 CPPFLAGS += $(CPPDEFS)
173 CPPFLAGS += -funsigned-char
174 CPPFLAGS += -funsigned-bitfields
175 CPPFLAGS += -fpack-struct
176 CPPFLAGS += -fshort-enums
177 CPPFLAGS += -fno-exceptions
180 #CPPFLAGS += -mshort-calls
181 #CPPFLAGS += -fno-unit-at-a-time
182 #CPPFLAGS += -Wstrict-prototypes
183 #CPPFLAGS += -Wunreachable-code
184 #CPPFLAGS += -Wsign-compare
185 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
186 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
187 #CPPFLAGS += $(CSTANDARD)
190 #---------------- Assembler Options ----------------
191 # -Wa,...: tell GCC to pass this to the assembler.
192 # -adhlns: create listing
193 # -gstabs: have the assembler create line number information; note that
194 # for use in COFF files, additional information about filenames
195 # and function names needs to be present in the assembler source
196 # files -- see avr-libc docs [FIXME: not yet described there]
197 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
198 # dump that will be displayed for a given single line of source input.
199 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
202 #---------------- Library Options ----------------
203 # Minimalistic printf version
204 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
206 # Floating point printf version (requires MATH_LIB = -lm below)
207 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
209 # If this is left blank, then it will use the Standard printf version.
211 #PRINTF_LIB = $(PRINTF_LIB_MIN)
212 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
215 # Minimalistic scanf version
216 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
218 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
219 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
221 # If this is left blank, then it will use the Standard scanf version.
223 #SCANF_LIB = $(SCANF_LIB_MIN)
224 #SCANF_LIB = $(SCANF_LIB_FLOAT)
230 # List any extra directories to look for libraries here.
231 # Each directory must be seperated by a space.
232 # Use forward slashes for directory separators.
233 # For a directory that has spaces, enclose it in quotes.
238 #---------------- External Memory Options ----------------
240 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
241 # used for variables (.data/.bss) and heap (malloc()).
242 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
244 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
245 # only used for heap (malloc()).
246 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
252 #---------------- Linker Options ----------------
253 # -Wl,...: tell GCC to pass this to linker.
254 # -Map: create map file
255 # --cref: add cross reference to map file
256 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
257 LDFLAGS += -Wl,--relax
258 LDFLAGS += -Wl,--gc-sections
259 LDFLAGS += $(EXTMEMOPTS)
260 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
261 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
262 #LDFLAGS += -T linker_script.x
266 #---------------- Programming Options (avrdude) ----------------
268 # Programming hardware
270 # to get a full listing.
272 AVRDUDE_PROGRAMMER = stk500v2
274 # com1 = serial port. Use lpt1 to connect to parallel port.
275 AVRDUDE_PORT = com1 # programmer connected to serial device
277 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
278 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
281 # Uncomment the following if you want avrdude's erase cycle counter.
282 # Note that this counter needs to be initialized first using -Yn,
283 # see avrdude manual.
284 #AVRDUDE_ERASE_COUNTER = -y
286 # Uncomment the following if you do /not/ wish a verification to be
287 # performed after programming the device.
288 #AVRDUDE_NO_VERIFY = -V
290 # Increase verbosity level. Please use this when submitting bug
291 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
292 # to submit bug reports.
293 #AVRDUDE_VERBOSE = -v -v
295 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
296 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
297 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
298 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
302 #---------------- Debugging Options ----------------
304 # For simulavr only - target MCU frequency.
305 DEBUG_MFREQ = $(F_CPU)
307 # Set the DEBUG_UI to either gdb or insight.
311 # Set the debugging back-end to either avarice, simulavr.
312 DEBUG_BACKEND = avarice
313 #DEBUG_BACKEND = simulavr
316 GDBINIT_FILE = __avr_gdbinit
318 # When using avarice settings for the JTAG
321 # Debugging port used to communicate between GDB / avarice / simulavr.
324 # Debugging host used to communicate between GDB / avarice / simulavr, normally
325 # just set to localhost unless doing some sort of crazy debugging when
326 # avarice is running on a different computer.
327 DEBUG_HOST = localhost
331 #============================================================================
334 # Define programs and commands.
337 OBJCOPY = avr-objcopy
338 OBJDUMP = avr-objdump
351 MSG_ERRORS_NONE = Errors: none
352 MSG_BEGIN = -------- begin --------
353 MSG_END = -------- end --------
354 MSG_SIZE_BEFORE = Size before:
355 MSG_SIZE_AFTER = Size after:
356 MSG_COFF = Converting to AVR COFF:
357 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
358 MSG_FLASH = Creating load file for Flash:
359 MSG_EEPROM = Creating load file for EEPROM:
360 MSG_EXTENDED_LISTING = Creating Extended Listing:
361 MSG_SYMBOL_TABLE = Creating Symbol Table:
362 MSG_LINKING = Linking:
363 MSG_COMPILING = Compiling C:
364 MSG_COMPILING_CPP = Compiling C++:
365 MSG_ASSEMBLING = Assembling:
366 MSG_CLEANING = Cleaning project:
367 MSG_CREATING_LIBRARY = Creating library:
372 # Define all object files.
373 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
375 # Define all listing files.
376 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
379 # Compiler flags to generate dependency files.
380 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
383 # Combine all necessary flags and optional flags.
384 # Add target processor to flags.
385 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
386 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
387 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
394 all: begin gccversion sizebefore build sizeafter end
396 # Change the build target to build a HEX file or a library.
397 build: elf hex eep lss sym
406 LIBNAME=lib$(TARGET).a
412 # AVR Studio 3.x does not check make's exit code but relies on
413 # the following magic strings to be generated by the compile job.
423 # Display size of file.
424 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
425 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
426 ELFSIZE = $(SIZE) $(TARGET).elf
429 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
430 2>/dev/null; echo; fi
433 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
434 2>/dev/null; echo; fi
438 # Display compiler version information.
444 # Program the device.
445 program: $(TARGET).hex $(TARGET).eep
446 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
449 # Generate avr-gdb config/init file which does the following:
450 # define the reset signal, load the target file, connect to target, and set
451 # a breakpoint at main().
453 @$(REMOVE) $(GDBINIT_FILE)
454 @echo define reset >> $(GDBINIT_FILE)
455 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
456 @echo end >> $(GDBINIT_FILE)
457 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
458 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
459 ifeq ($(DEBUG_BACKEND),simulavr)
460 @echo load >> $(GDBINIT_FILE)
462 @echo break main >> $(GDBINIT_FILE)
464 debug: gdb-config $(TARGET).elf
465 ifeq ($(DEBUG_BACKEND), avarice)
466 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
467 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
468 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
469 @$(WINSHELL) /c pause
472 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
473 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
475 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
480 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
481 COFFCONVERT = $(OBJCOPY) --debugging
482 COFFCONVERT += --change-section-address .data-0x800000
483 COFFCONVERT += --change-section-address .bss-0x800000
484 COFFCONVERT += --change-section-address .noinit-0x800000
485 COFFCONVERT += --change-section-address .eeprom-0x810000
491 @echo $(MSG_COFF) $(TARGET).cof
492 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
495 extcoff: $(TARGET).elf
497 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
498 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
502 # Create final output files (.hex, .eep) from ELF output file.
505 @echo $(MSG_FLASH) $@
506 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
510 @echo $(MSG_EEPROM) $@
511 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
512 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
514 # Create extended listing file from ELF output file.
517 @echo $(MSG_EXTENDED_LISTING) $@
518 $(OBJDUMP) -h -S -z $< > $@
520 # Create a symbol table from ELF output file.
523 @echo $(MSG_SYMBOL_TABLE) $@
528 # Create library from object files.
529 .SECONDARY : $(TARGET).a
533 @echo $(MSG_CREATING_LIBRARY) $@
537 # Link: create ELF output file from object files.
538 .SECONDARY : $(TARGET).elf
542 @echo $(MSG_LINKING) $@
543 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
546 # Compile: create object files from C source files.
549 @echo $(MSG_COMPILING) $<
550 $(CC) -c $(ALL_CFLAGS) $< -o $@
553 # Compile: create object files from C++ source files.
554 $(OBJDIR)/%.o : %.cpp
556 @echo $(MSG_COMPILING_CPP) $<
557 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
560 # Compile: create assembler files from C source files.
562 $(CC) -S $(ALL_CFLAGS) $< -o $@
565 # Compile: create assembler files from C++ source files.
567 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
570 # Assemble: create object files from assembler source files.
573 @echo $(MSG_ASSEMBLING) $<
574 $(CC) -c $(ALL_ASFLAGS) $< -o $@
577 # Create preprocessed source for use in sending a bug report.
579 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
582 # Target: clean project.
583 clean: begin clean_list end
587 @echo $(MSG_CLEANING)
588 $(REMOVE) $(TARGET).hex
589 $(REMOVE) $(TARGET).eep
590 $(REMOVE) $(TARGET).cof
591 $(REMOVE) $(TARGET).elf
592 $(REMOVE) $(TARGET).map
593 $(REMOVE) $(TARGET).sym
594 $(REMOVE) $(TARGET).lss
595 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
596 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
597 $(REMOVE) $(SRC:.c=.s)
598 $(REMOVE) $(SRC:.c=.d)
599 $(REMOVE) $(SRC:.c=.i)
603 # Create object files directory
604 $(shell mkdir $(OBJDIR) 2>/dev/null)
607 # Include the dependency files.
608 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
611 # Listing of phony targets.
612 .PHONY : all begin finish end sizebefore sizeafter gccversion \
613 build elf hex eep lss sym coff extcoff \
614 clean clean_list program debug gdb-config