1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jg 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 # Output format. (can be srec, ihex, binary)
47 # Object files directory
48 # To put object files in current directory, use a dot (.), do NOT make
49 # this an empty or blank macro!
53 # Optimization level, can be [0, 1, 2, 3, s].
54 # 0 = turn off optimization. s = optimize for size.
55 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
60 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
61 # AVR Studio 4.10 requires dwarf-2.
62 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
66 # List any extra directories to look for include files here.
67 # Each directory must be seperated by a space.
68 # Use forward slashes for directory separators.
69 # For a directory that has spaces, enclose it in quotes.
70 EXTRAINCDIRS = $(subst :, ,$(VPATH))
73 # Compiler flag to set the C Standard level.
75 # gnu89 = c89 plus GCC extensions
76 # c99 = ISO C99 standard (not yet fully implemented)
77 # gnu99 = c99 plus GCC extensions
78 CSTANDARD = -std=gnu99
82 OPT_DEFS += -DMOUSEKEY_ENABLE
84 ifdef PS2_MOUSE_ENABLE
85 OPT_DEFS += -DPS2_MOUSE_ENABLE
87 ifdef USB_EXTRA_ENABLE
88 OPT_DEFS += -DUSB_EXTRA_ENABLE
91 OPT_DEFS += -DUSB_NKRO_ENABLE
93 ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE)
94 OPT_DEFS += -DUSB_MOUSE_ENABLE
97 # Place -D or -U options here for C sources
98 CDEFS = -DF_CPU=$(F_CPU)UL
102 # Place -D or -U options here for ASM sources
103 ADEFS = -DF_CPU=$(F_CPU)
107 # Place -D or -U options here for C++ sources
108 CPPDEFS = -DF_CPU=$(F_CPU)UL
109 #CPPDEFS += -D__STDC_LIMIT_MACROS
110 #CPPDEFS += -D__STDC_CONSTANT_MACROS
111 CPPDEFS += $(OPT_DEFS)
115 #---------------- Compiler Options C ----------------
116 # -g*: generate debugging information
117 # -O*: optimization level
118 # -f...: tuning, see GCC manual and avr-libc documentation
119 # -Wall...: warning level
120 # -Wa,...: tell GCC to pass this to the assembler.
121 # -adhlns...: create assembler listing
125 CFLAGS += -funsigned-char
126 CFLAGS += -funsigned-bitfields
127 CFLAGS += -ffunction-sections
128 CFLAGS += -fpack-struct
129 CFLAGS += -fshort-enums
131 CFLAGS += -Wstrict-prototypes
132 #CFLAGS += -mshort-calls
133 #CFLAGS += -fno-unit-at-a-time
135 #CFLAGS += -Wunreachable-code
136 #CFLAGS += -Wsign-compare
137 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
138 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
139 CFLAGS += $(CSTANDARD)
140 CFLAGS += -include config.h
143 #---------------- Compiler Options C++ ----------------
144 # -g*: generate debugging information
145 # -O*: optimization level
146 # -f...: tuning, see GCC manual and avr-libc documentation
147 # -Wall...: warning level
148 # -Wa,...: tell GCC to pass this to the assembler.
149 # -adhlns...: create assembler listing
150 CPPFLAGS = -g$(DEBUG)
151 CPPFLAGS += $(CPPDEFS)
153 CPPFLAGS += -funsigned-char
154 CPPFLAGS += -funsigned-bitfields
155 CPPFLAGS += -fpack-struct
156 CPPFLAGS += -fshort-enums
157 CPPFLAGS += -fno-exceptions
160 #CPPFLAGS += -mshort-calls
161 #CPPFLAGS += -fno-unit-at-a-time
162 #CPPFLAGS += -Wstrict-prototypes
163 #CPPFLAGS += -Wunreachable-code
164 #CPPFLAGS += -Wsign-compare
165 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
166 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
167 #CPPFLAGS += $(CSTANDARD)
168 CPPFLAGS += -include config.h
171 #---------------- Assembler Options ----------------
172 # -Wa,...: tell GCC to pass this to the assembler.
173 # -adhlns: create listing
174 # -gstabs: have the assembler create line number information; note that
175 # for use in COFF files, additional information about filenames
176 # and function names needs to be present in the assembler source
177 # files -- see avr-libc docs [FIXME: not yet described there]
178 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
179 # dump that will be displayed for a given single line of source input.
180 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
181 ASFLAGS += -include config.h
184 #---------------- Library Options ----------------
185 # Minimalistic printf version
186 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
188 # Floating point printf version (requires MATH_LIB = -lm below)
189 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
191 # If this is left blank, then it will use the Standard printf version.
193 #PRINTF_LIB = $(PRINTF_LIB_MIN)
194 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
197 # Minimalistic scanf version
198 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
200 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
201 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
203 # If this is left blank, then it will use the Standard scanf version.
205 #SCANF_LIB = $(SCANF_LIB_MIN)
206 #SCANF_LIB = $(SCANF_LIB_FLOAT)
212 # List any extra directories to look for libraries here.
213 # Each directory must be seperated by a space.
214 # Use forward slashes for directory separators.
215 # For a directory that has spaces, enclose it in quotes.
220 #---------------- External Memory Options ----------------
222 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
223 # used for variables (.data/.bss) and heap (malloc()).
224 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
226 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
227 # only used for heap (malloc()).
228 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
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
239 # Comennt out "--relax" option to avoid a error such:
240 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
242 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
243 LDFLAGS += -Wl,--relax
244 LDFLAGS += -Wl,--gc-sections
245 LDFLAGS += $(EXTMEMOPTS)
246 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
247 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
248 #LDFLAGS += -T linker_script.x
252 #---------------- Programming Options (avrdude) ----------------
254 # Programming hardware
256 # to get a full listing.
258 AVRDUDE_PROGRAMMER = stk500v2
260 # com1 = serial port. Use lpt1 to connect to parallel port.
261 AVRDUDE_PORT = com1 # programmer connected to serial device
263 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
264 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
267 # Uncomment the following if you want avrdude's erase cycle counter.
268 # Note that this counter needs to be initialized first using -Yn,
269 # see avrdude manual.
270 #AVRDUDE_ERASE_COUNTER = -y
272 # Uncomment the following if you do /not/ wish a verification to be
273 # performed after programming the device.
274 #AVRDUDE_NO_VERIFY = -V
276 # Increase verbosity level. Please use this when submitting bug
277 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
278 # to submit bug reports.
279 #AVRDUDE_VERBOSE = -v -v
281 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
282 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
283 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
284 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
288 #---------------- Debugging Options ----------------
290 # For simulavr only - target MCU frequency.
291 DEBUG_MFREQ = $(F_CPU)
293 # Set the DEBUG_UI to either gdb or insight.
297 # Set the debugging back-end to either avarice, simulavr.
298 DEBUG_BACKEND = avarice
299 #DEBUG_BACKEND = simulavr
302 GDBINIT_FILE = __avr_gdbinit
304 # When using avarice settings for the JTAG
307 # Debugging port used to communicate between GDB / avarice / simulavr.
310 # Debugging host used to communicate between GDB / avarice / simulavr, normally
311 # just set to localhost unless doing some sort of crazy debugging when
312 # avarice is running on a different computer.
313 DEBUG_HOST = localhost
317 #============================================================================
320 # Define programs and commands.
323 OBJCOPY = avr-objcopy
324 OBJDUMP = avr-objdump
337 MSG_ERRORS_NONE = Errors: none
338 MSG_BEGIN = -------- begin --------
339 MSG_END = -------- end --------
340 MSG_SIZE_BEFORE = Size before:
341 MSG_SIZE_AFTER = Size after:
342 MSG_COFF = Converting to AVR COFF:
343 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
344 MSG_FLASH = Creating load file for Flash:
345 MSG_EEPROM = Creating load file for EEPROM:
346 MSG_EXTENDED_LISTING = Creating Extended Listing:
347 MSG_SYMBOL_TABLE = Creating Symbol Table:
348 MSG_LINKING = Linking:
349 MSG_COMPILING = Compiling C:
350 MSG_COMPILING_CPP = Compiling C++:
351 MSG_ASSEMBLING = Assembling:
352 MSG_CLEANING = Cleaning project:
353 MSG_CREATING_LIBRARY = Creating library:
358 # Define all object files.
359 OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
361 # Define all listing files.
362 LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
365 # Compiler flags to generate dependency files.
366 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
369 # Combine all necessary flags and optional flags.
370 # Add target processor to flags.
371 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
372 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
373 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
380 all: begin gccversion sizebefore build sizeafter end
382 # Change the build target to build a HEX file or a library.
383 build: elf hex eep lss sym
392 LIBNAME=lib$(TARGET).a
398 # AVR Studio 3.x does not check make's exit code but relies on
399 # the following magic strings to be generated by the compile job.
409 # Display size of file.
410 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
411 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
412 ELFSIZE = $(SIZE) $(TARGET).elf
415 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
416 2>/dev/null; echo; fi
419 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
420 2>/dev/null; echo; fi
424 # Display compiler version information.
430 # Program the device.
431 program: $(TARGET).hex $(TARGET).eep
432 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
435 # Generate avr-gdb config/init file which does the following:
436 # define the reset signal, load the target file, connect to target, and set
437 # a breakpoint at main().
439 @$(REMOVE) $(GDBINIT_FILE)
440 @echo define reset >> $(GDBINIT_FILE)
441 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
442 @echo end >> $(GDBINIT_FILE)
443 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
444 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
445 ifeq ($(DEBUG_BACKEND),simulavr)
446 @echo load >> $(GDBINIT_FILE)
448 @echo break main >> $(GDBINIT_FILE)
450 debug: gdb-config $(TARGET).elf
451 ifeq ($(DEBUG_BACKEND), avarice)
452 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
453 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
454 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
455 @$(WINSHELL) /c pause
458 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
459 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
461 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
466 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
467 COFFCONVERT = $(OBJCOPY) --debugging
468 COFFCONVERT += --change-section-address .data-0x800000
469 COFFCONVERT += --change-section-address .bss-0x800000
470 COFFCONVERT += --change-section-address .noinit-0x800000
471 COFFCONVERT += --change-section-address .eeprom-0x810000
477 @echo $(MSG_COFF) $(TARGET).cof
478 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
481 extcoff: $(TARGET).elf
483 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
484 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
488 # Create final output files (.hex, .eep) from ELF output file.
491 @echo $(MSG_FLASH) $@
492 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
496 @echo $(MSG_EEPROM) $@
497 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
498 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
500 # Create extended listing file from ELF output file.
503 @echo $(MSG_EXTENDED_LISTING) $@
504 $(OBJDUMP) -h -S -z $< > $@
506 # Create a symbol table from ELF output file.
509 @echo $(MSG_SYMBOL_TABLE) $@
514 # Create library from object files.
515 .SECONDARY : $(TARGET).a
519 @echo $(MSG_CREATING_LIBRARY) $@
523 # Link: create ELF output file from object files.
524 .SECONDARY : $(TARGET).elf
528 @echo $(MSG_LINKING) $@
529 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
532 # Compile: create object files from C source files.
535 @echo $(MSG_COMPILING) $<
536 $(CC) -c $(ALL_CFLAGS) $< -o $@
539 # Compile: create object files from C++ source files.
540 $(OBJDIR)/%.o : %.cpp
542 @echo $(MSG_COMPILING_CPP) $<
543 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
546 # Compile: create assembler files from C source files.
548 $(CC) -S $(ALL_CFLAGS) $< -o $@
551 # Compile: create assembler files from C++ source files.
553 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
556 # Assemble: create object files from assembler source files.
559 @echo $(MSG_ASSEMBLING) $<
560 $(CC) -c $(ALL_ASFLAGS) $< -o $@
563 # Create preprocessed source for use in sending a bug report.
565 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
568 # Target: clean project.
569 clean: begin clean_list end
573 $(REMOVE) $(TARGET).hex
574 $(REMOVE) $(TARGET).eep
575 $(REMOVE) $(TARGET).cof
576 $(REMOVE) $(TARGET).elf
577 $(REMOVE) $(TARGET).map
578 $(REMOVE) $(TARGET).sym
579 $(REMOVE) $(TARGET).lss
582 $(REMOVE) $(OBJ:.o=.s)
583 $(REMOVE) $(OBJ:.o=.i)
585 $(REMOVEDIR) $(OBJDIR)
588 # Create object files directory
589 $(shell mkdir $(OBJDIR) 2>/dev/null)
592 # Include the dependency files.
593 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
596 # Listing of phony targets.
597 .PHONY : all begin finish end sizebefore sizeafter gccversion \
598 build elf hex eep lss sym coff extcoff \
599 clean clean_list program debug gdb-config