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 #----------------------------------------------------------------------------
42 # Following variables need to be set in <target>/Makefile:
51 # List C source files here. (C dependencies are automatically generated.)
65 ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE)
71 ifdef PS2_MOUSE_ENABLE
75 ifdef USB_EXTRA_ENABLE
80 ALL_SRC += usb_mouse.c \
87 # C source file search path
88 VPATH = $(TARGET_DIR):$(COMMON_DIR)
91 # Output format. (can be srec, ihex, binary)
95 # Object files directory
96 # To put object files in current directory, use a dot (.), do NOT make
97 # this an empty or blank macro!
101 # List C++ source files here. (C dependencies are automatically generated.)
105 # List Assembler source files here.
106 # Make them always end in a capital .S. Files ending in a lowercase .s
107 # will not be considered source files but generated files (assembler
108 # output from the compiler), and will be deleted upon "make clean"!
109 # Even though the DOS/Win* filesystem matches both .s and .S the same,
110 # it will preserve the spelling of the filenames, and gcc itself does
111 # care about how the name is spelled on its command-line.
115 # Optimization level, can be [0, 1, 2, 3, s].
116 # 0 = turn off optimization. s = optimize for size.
117 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
122 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
123 # AVR Studio 4.10 requires dwarf-2.
124 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
128 # List any extra directories to look for include files here.
129 # Each directory must be seperated by a space.
130 # Use forward slashes for directory separators.
131 # For a directory that has spaces, enclose it in quotes.
132 EXTRAINCDIRS = $(TARGET_DIR) $(COMMON_DIR)
135 # Compiler flag to set the C Standard level.
137 # gnu89 = c89 plus GCC extensions
138 # c99 = ISO C99 standard (not yet fully implemented)
139 # gnu99 = c99 plus GCC extensions
140 CSTANDARD = -std=gnu99
144 ifdef MOUSEKEY_ENABLE
145 OPT_DEFS += -DMOUSEKEY_ENABLE
147 ifdef PS2_MOUSE_ENABLE
148 OPT_DEFS += -DPS2_MOUSE_ENABLE
150 ifdef USB_EXTRA_ENABLE
151 OPT_DEFS += -DUSB_EXTRA_ENABLE
153 ifdef USB_NKRO_ENABLE
154 OPT_DEFS += -DUSB_NKRO_ENABLE
156 ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE)
157 OPT_DEFS += -DUSB_MOUSE_ENABLE
160 # Place -D or -U options here for C sources
161 CDEFS = -DF_CPU=$(F_CPU)UL
165 # Place -D or -U options here for ASM sources
166 ADEFS = -DF_CPU=$(F_CPU)
170 # Place -D or -U options here for C++ sources
171 CPPDEFS = -DF_CPU=$(F_CPU)UL
172 #CPPDEFS += -D__STDC_LIMIT_MACROS
173 #CPPDEFS += -D__STDC_CONSTANT_MACROS
174 CPPDEFS += $(OPT_DEFS)
178 #---------------- Compiler Options C ----------------
179 # -g*: generate debugging information
180 # -O*: optimization level
181 # -f...: tuning, see GCC manual and avr-libc documentation
182 # -Wall...: warning level
183 # -Wa,...: tell GCC to pass this to the assembler.
184 # -adhlns...: create assembler listing
188 CFLAGS += -funsigned-char
189 CFLAGS += -funsigned-bitfields
190 CFLAGS += -ffunction-sections
191 CFLAGS += -fpack-struct
192 CFLAGS += -fshort-enums
194 CFLAGS += -Wstrict-prototypes
195 #CFLAGS += -mshort-calls
196 #CFLAGS += -fno-unit-at-a-time
198 #CFLAGS += -Wunreachable-code
199 #CFLAGS += -Wsign-compare
200 CFLAGS += -Wa,-adhlns=$(@:%.o=$(OBJDIR)/%.lst)
201 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
202 CFLAGS += $(CSTANDARD)
203 CFLAGS += -include config.h
206 #---------------- Compiler Options C++ ----------------
207 # -g*: generate debugging information
208 # -O*: optimization level
209 # -f...: tuning, see GCC manual and avr-libc documentation
210 # -Wall...: warning level
211 # -Wa,...: tell GCC to pass this to the assembler.
212 # -adhlns...: create assembler listing
213 CPPFLAGS = -g$(DEBUG)
214 CPPFLAGS += $(CPPDEFS)
216 CPPFLAGS += -funsigned-char
217 CPPFLAGS += -funsigned-bitfields
218 CPPFLAGS += -fpack-struct
219 CPPFLAGS += -fshort-enums
220 CPPFLAGS += -fno-exceptions
223 #CPPFLAGS += -mshort-calls
224 #CPPFLAGS += -fno-unit-at-a-time
225 #CPPFLAGS += -Wstrict-prototypes
226 #CPPFLAGS += -Wunreachable-code
227 #CPPFLAGS += -Wsign-compare
228 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
229 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
230 #CPPFLAGS += $(CSTANDARD)
231 CPPFLAGS += -include config.h
234 #---------------- Assembler Options ----------------
235 # -Wa,...: tell GCC to pass this to the assembler.
236 # -adhlns: create listing
237 # -gstabs: have the assembler create line number information; note that
238 # for use in COFF files, additional information about filenames
239 # and function names needs to be present in the assembler source
240 # files -- see avr-libc docs [FIXME: not yet described there]
241 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
242 # dump that will be displayed for a given single line of source input.
243 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
244 ASFLAGS += -include config.h
247 #---------------- Library Options ----------------
248 # Minimalistic printf version
249 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
251 # Floating point printf version (requires MATH_LIB = -lm below)
252 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
254 # If this is left blank, then it will use the Standard printf version.
256 #PRINTF_LIB = $(PRINTF_LIB_MIN)
257 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
260 # Minimalistic scanf version
261 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
263 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
264 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
266 # If this is left blank, then it will use the Standard scanf version.
268 #SCANF_LIB = $(SCANF_LIB_MIN)
269 #SCANF_LIB = $(SCANF_LIB_FLOAT)
275 # List any extra directories to look for libraries here.
276 # Each directory must be seperated by a space.
277 # Use forward slashes for directory separators.
278 # For a directory that has spaces, enclose it in quotes.
283 #---------------- External Memory Options ----------------
285 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
286 # used for variables (.data/.bss) and heap (malloc()).
287 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
289 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
290 # only used for heap (malloc()).
291 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
297 #---------------- Linker Options ----------------
298 # -Wl,...: tell GCC to pass this to linker.
299 # -Map: create map file
300 # --cref: add cross reference to map file
301 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
302 LDFLAGS += -Wl,--relax
303 LDFLAGS += -Wl,--gc-sections
304 LDFLAGS += $(EXTMEMOPTS)
305 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
306 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
307 #LDFLAGS += -T linker_script.x
311 #---------------- Programming Options (avrdude) ----------------
313 # Programming hardware
315 # to get a full listing.
317 AVRDUDE_PROGRAMMER = stk500v2
319 # com1 = serial port. Use lpt1 to connect to parallel port.
320 AVRDUDE_PORT = com1 # programmer connected to serial device
322 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
323 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
326 # Uncomment the following if you want avrdude's erase cycle counter.
327 # Note that this counter needs to be initialized first using -Yn,
328 # see avrdude manual.
329 #AVRDUDE_ERASE_COUNTER = -y
331 # Uncomment the following if you do /not/ wish a verification to be
332 # performed after programming the device.
333 #AVRDUDE_NO_VERIFY = -V
335 # Increase verbosity level. Please use this when submitting bug
336 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
337 # to submit bug reports.
338 #AVRDUDE_VERBOSE = -v -v
340 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
341 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
342 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
343 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
347 #---------------- Debugging Options ----------------
349 # For simulavr only - target MCU frequency.
350 DEBUG_MFREQ = $(F_CPU)
352 # Set the DEBUG_UI to either gdb or insight.
356 # Set the debugging back-end to either avarice, simulavr.
357 DEBUG_BACKEND = avarice
358 #DEBUG_BACKEND = simulavr
361 GDBINIT_FILE = __avr_gdbinit
363 # When using avarice settings for the JTAG
366 # Debugging port used to communicate between GDB / avarice / simulavr.
369 # Debugging host used to communicate between GDB / avarice / simulavr, normally
370 # just set to localhost unless doing some sort of crazy debugging when
371 # avarice is running on a different computer.
372 DEBUG_HOST = localhost
376 #============================================================================
379 # Define programs and commands.
382 OBJCOPY = avr-objcopy
383 OBJDUMP = avr-objdump
396 MSG_ERRORS_NONE = Errors: none
397 MSG_BEGIN = -------- begin --------
398 MSG_END = -------- end --------
399 MSG_SIZE_BEFORE = Size before:
400 MSG_SIZE_AFTER = Size after:
401 MSG_COFF = Converting to AVR COFF:
402 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
403 MSG_FLASH = Creating load file for Flash:
404 MSG_EEPROM = Creating load file for EEPROM:
405 MSG_EXTENDED_LISTING = Creating Extended Listing:
406 MSG_SYMBOL_TABLE = Creating Symbol Table:
407 MSG_LINKING = Linking:
408 MSG_COMPILING = Compiling C:
409 MSG_COMPILING_CPP = Compiling C++:
410 MSG_ASSEMBLING = Assembling:
411 MSG_CLEANING = Cleaning project:
412 MSG_CREATING_LIBRARY = Creating library:
417 # Define all object files.
418 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
420 # Define all listing files.
421 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
424 # Compiler flags to generate dependency files.
425 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
428 # Combine all necessary flags and optional flags.
429 # Add target processor to flags.
430 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
431 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
432 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
439 all: begin gccversion sizebefore build sizeafter end
444 # $(CC) -E $(ALL_CFLAGS) $<
446 # Change the build target to build a HEX file or a library.
447 build: elf hex eep lss sym
456 LIBNAME=lib$(TARGET).a
462 # AVR Studio 3.x does not check make's exit code but relies on
463 # the following magic strings to be generated by the compile job.
473 # Display size of file.
474 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
475 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
476 ELFSIZE = $(SIZE) $(TARGET).elf
479 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
480 2>/dev/null; echo; fi
483 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
484 2>/dev/null; echo; fi
488 # Display compiler version information.
494 # Program the device.
495 program: $(TARGET).hex $(TARGET).eep
496 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
499 # Generate avr-gdb config/init file which does the following:
500 # define the reset signal, load the target file, connect to target, and set
501 # a breakpoint at main().
503 @$(REMOVE) $(GDBINIT_FILE)
504 @echo define reset >> $(GDBINIT_FILE)
505 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
506 @echo end >> $(GDBINIT_FILE)
507 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
508 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
509 ifeq ($(DEBUG_BACKEND),simulavr)
510 @echo load >> $(GDBINIT_FILE)
512 @echo break main >> $(GDBINIT_FILE)
514 debug: gdb-config $(TARGET).elf
515 ifeq ($(DEBUG_BACKEND), avarice)
516 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
517 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
518 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
519 @$(WINSHELL) /c pause
522 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
523 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
525 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
530 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
531 COFFCONVERT = $(OBJCOPY) --debugging
532 COFFCONVERT += --change-section-address .data-0x800000
533 COFFCONVERT += --change-section-address .bss-0x800000
534 COFFCONVERT += --change-section-address .noinit-0x800000
535 COFFCONVERT += --change-section-address .eeprom-0x810000
541 @echo $(MSG_COFF) $(TARGET).cof
542 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
545 extcoff: $(TARGET).elf
547 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
548 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
552 # Create final output files (.hex, .eep) from ELF output file.
555 @echo $(MSG_FLASH) $@
556 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
560 @echo $(MSG_EEPROM) $@
561 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
562 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
564 # Create extended listing file from ELF output file.
567 @echo $(MSG_EXTENDED_LISTING) $@
568 $(OBJDUMP) -h -S -z $< > $@
570 # Create a symbol table from ELF output file.
573 @echo $(MSG_SYMBOL_TABLE) $@
578 # Create library from object files.
579 .SECONDARY : $(TARGET).a
583 @echo $(MSG_CREATING_LIBRARY) $@
587 # Link: create ELF output file from object files.
588 .SECONDARY : $(TARGET).elf
592 @echo $(MSG_LINKING) $@
593 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
596 # Compile: create object files from C source files.
599 @echo $(MSG_COMPILING) $<
600 $(CC) -c $(ALL_CFLAGS) $< -o $@
603 # Compile: create object files from C++ source files.
604 $(OBJDIR)/%.o : %.cpp
606 @echo $(MSG_COMPILING_CPP) $<
607 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
610 # Compile: create assembler files from C source files.
612 $(CC) -S $(ALL_CFLAGS) $< -o $@
615 # Compile: create assembler files from C++ source files.
617 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
620 # Assemble: create object files from assembler source files.
623 @echo $(MSG_ASSEMBLING) $<
624 $(CC) -c $(ALL_ASFLAGS) $< -o $@
627 # Create preprocessed source for use in sending a bug report.
629 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
632 # Target: clean project.
633 clean: begin clean_list end
637 @echo $(MSG_CLEANING)
638 $(REMOVE) $(TARGET).hex
639 $(REMOVE) $(TARGET).eep
640 $(REMOVE) $(TARGET).cof
641 $(REMOVE) $(TARGET).elf
642 $(REMOVE) $(TARGET).map
643 $(REMOVE) $(TARGET).sym
644 $(REMOVE) $(TARGET).lss
645 $(REMOVE) $(ALL_SRC:%.c=$(OBJDIR)/%.o)
646 $(REMOVE) $(ALL_SRC:%.c=$(OBJDIR)/%.lst)
647 $(REMOVE) $(ALL_SRC:.c=.s)
648 $(REMOVE) $(ALL_SRC:.c=.d)
649 $(REMOVE) $(ALL_SRC:.c=.i)
653 # Create object files directory
654 $(shell mkdir $(OBJDIR) 2>/dev/null)
657 # Include the dependency files.
658 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
661 # Listing of phony targets.
662 .PHONY : all begin finish end sizebefore sizeafter gccversion \
663 build elf hex eep lss sym coff extcoff \
664 clean clean_list program debug gdb-config