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.)
68 ifdef PS2_MOUSE_ENABLE
73 # C source file search path
74 VPATH = $(TARGET_DIR):$(COMMON_DIR)
77 # Output format. (can be srec, ihex, binary)
81 # Object files directory
82 # To put object files in current directory, use a dot (.), do NOT make
83 # this an empty or blank macro!
87 # List C++ source files here. (C dependencies are automatically generated.)
91 # List Assembler source files here.
92 # Make them always end in a capital .S. Files ending in a lowercase .s
93 # will not be considered source files but generated files (assembler
94 # output from the compiler), and will be deleted upon "make clean"!
95 # Even though the DOS/Win* filesystem matches both .s and .S the same,
96 # it will preserve the spelling of the filenames, and gcc itself does
97 # care about how the name is spelled on its command-line.
101 # Optimization level, can be [0, 1, 2, 3, s].
102 # 0 = turn off optimization. s = optimize for size.
103 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
108 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
109 # AVR Studio 4.10 requires dwarf-2.
110 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
114 # List any extra directories to look for include files here.
115 # Each directory must be seperated by a space.
116 # Use forward slashes for directory separators.
117 # For a directory that has spaces, enclose it in quotes.
118 EXTRAINCDIRS = $(TARGET_DIR) $(COMMON_DIR)
121 # Compiler flag to set the C Standard level.
123 # gnu89 = c89 plus GCC extensions
124 # c99 = ISO C99 standard (not yet fully implemented)
125 # gnu99 = c99 plus GCC extensions
126 CSTANDARD = -std=gnu99
130 ifdef USB_NKRO_ENABLE
131 OPT_DEFS += -DUSB_NKRO_ENABLE
133 ifdef MOUSEKEY_ENABLE
134 OPT_DEFS += -DMOUSEKEY_ENABLE
136 ifdef PS2_MOUSE_ENABLE
137 OPT_DEFS += -DPS2_MOUSE_ENABLE
140 # Place -D or -U options here for C sources
141 CDEFS = -DF_CPU=$(F_CPU)UL
145 # Place -D or -U options here for ASM sources
146 ADEFS = -DF_CPU=$(F_CPU)
150 # Place -D or -U options here for C++ sources
151 CPPDEFS = -DF_CPU=$(F_CPU)UL
152 #CPPDEFS += -D__STDC_LIMIT_MACROS
153 #CPPDEFS += -D__STDC_CONSTANT_MACROS
154 CPPDEFS += $(OPT_DEFS)
158 #---------------- Compiler Options C ----------------
159 # -g*: generate debugging information
160 # -O*: optimization level
161 # -f...: tuning, see GCC manual and avr-libc documentation
162 # -Wall...: warning level
163 # -Wa,...: tell GCC to pass this to the assembler.
164 # -adhlns...: create assembler listing
168 CFLAGS += -funsigned-char
169 CFLAGS += -funsigned-bitfields
170 CFLAGS += -ffunction-sections
171 CFLAGS += -fpack-struct
172 CFLAGS += -fshort-enums
174 CFLAGS += -Wstrict-prototypes
175 #CFLAGS += -mshort-calls
176 #CFLAGS += -fno-unit-at-a-time
178 #CFLAGS += -Wunreachable-code
179 #CFLAGS += -Wsign-compare
180 CFLAGS += -Wa,-adhlns=$(@:%.o=$(OBJDIR)/%.lst)
181 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
182 CFLAGS += $(CSTANDARD)
183 CFLAGS += -include config.h
186 #---------------- Compiler Options C++ ----------------
187 # -g*: generate debugging information
188 # -O*: optimization level
189 # -f...: tuning, see GCC manual and avr-libc documentation
190 # -Wall...: warning level
191 # -Wa,...: tell GCC to pass this to the assembler.
192 # -adhlns...: create assembler listing
193 CPPFLAGS = -g$(DEBUG)
194 CPPFLAGS += $(CPPDEFS)
196 CPPFLAGS += -funsigned-char
197 CPPFLAGS += -funsigned-bitfields
198 CPPFLAGS += -fpack-struct
199 CPPFLAGS += -fshort-enums
200 CPPFLAGS += -fno-exceptions
203 #CPPFLAGS += -mshort-calls
204 #CPPFLAGS += -fno-unit-at-a-time
205 #CPPFLAGS += -Wstrict-prototypes
206 #CPPFLAGS += -Wunreachable-code
207 #CPPFLAGS += -Wsign-compare
208 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
209 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
210 #CPPFLAGS += $(CSTANDARD)
211 CPPFLAGS += -include config.h
214 #---------------- Assembler Options ----------------
215 # -Wa,...: tell GCC to pass this to the assembler.
216 # -adhlns: create listing
217 # -gstabs: have the assembler create line number information; note that
218 # for use in COFF files, additional information about filenames
219 # and function names needs to be present in the assembler source
220 # files -- see avr-libc docs [FIXME: not yet described there]
221 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
222 # dump that will be displayed for a given single line of source input.
223 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
224 ASFLAGS += -include config.h
227 #---------------- Library Options ----------------
228 # Minimalistic printf version
229 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
231 # Floating point printf version (requires MATH_LIB = -lm below)
232 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
234 # If this is left blank, then it will use the Standard printf version.
236 #PRINTF_LIB = $(PRINTF_LIB_MIN)
237 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
240 # Minimalistic scanf version
241 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
243 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
244 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
246 # If this is left blank, then it will use the Standard scanf version.
248 #SCANF_LIB = $(SCANF_LIB_MIN)
249 #SCANF_LIB = $(SCANF_LIB_FLOAT)
255 # List any extra directories to look for libraries here.
256 # Each directory must be seperated by a space.
257 # Use forward slashes for directory separators.
258 # For a directory that has spaces, enclose it in quotes.
263 #---------------- External Memory Options ----------------
265 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
266 # used for variables (.data/.bss) and heap (malloc()).
267 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
269 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
270 # only used for heap (malloc()).
271 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
277 #---------------- Linker Options ----------------
278 # -Wl,...: tell GCC to pass this to linker.
279 # -Map: create map file
280 # --cref: add cross reference to map file
281 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
282 LDFLAGS += -Wl,--relax
283 LDFLAGS += -Wl,--gc-sections
284 LDFLAGS += $(EXTMEMOPTS)
285 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
286 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
287 #LDFLAGS += -T linker_script.x
291 #---------------- Programming Options (avrdude) ----------------
293 # Programming hardware
295 # to get a full listing.
297 AVRDUDE_PROGRAMMER = stk500v2
299 # com1 = serial port. Use lpt1 to connect to parallel port.
300 AVRDUDE_PORT = com1 # programmer connected to serial device
302 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
303 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
306 # Uncomment the following if you want avrdude's erase cycle counter.
307 # Note that this counter needs to be initialized first using -Yn,
308 # see avrdude manual.
309 #AVRDUDE_ERASE_COUNTER = -y
311 # Uncomment the following if you do /not/ wish a verification to be
312 # performed after programming the device.
313 #AVRDUDE_NO_VERIFY = -V
315 # Increase verbosity level. Please use this when submitting bug
316 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
317 # to submit bug reports.
318 #AVRDUDE_VERBOSE = -v -v
320 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
321 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
322 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
323 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
327 #---------------- Debugging Options ----------------
329 # For simulavr only - target MCU frequency.
330 DEBUG_MFREQ = $(F_CPU)
332 # Set the DEBUG_UI to either gdb or insight.
336 # Set the debugging back-end to either avarice, simulavr.
337 DEBUG_BACKEND = avarice
338 #DEBUG_BACKEND = simulavr
341 GDBINIT_FILE = __avr_gdbinit
343 # When using avarice settings for the JTAG
346 # Debugging port used to communicate between GDB / avarice / simulavr.
349 # Debugging host used to communicate between GDB / avarice / simulavr, normally
350 # just set to localhost unless doing some sort of crazy debugging when
351 # avarice is running on a different computer.
352 DEBUG_HOST = localhost
356 #============================================================================
359 # Define programs and commands.
362 OBJCOPY = avr-objcopy
363 OBJDUMP = avr-objdump
376 MSG_ERRORS_NONE = Errors: none
377 MSG_BEGIN = -------- begin --------
378 MSG_END = -------- end --------
379 MSG_SIZE_BEFORE = Size before:
380 MSG_SIZE_AFTER = Size after:
381 MSG_COFF = Converting to AVR COFF:
382 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
383 MSG_FLASH = Creating load file for Flash:
384 MSG_EEPROM = Creating load file for EEPROM:
385 MSG_EXTENDED_LISTING = Creating Extended Listing:
386 MSG_SYMBOL_TABLE = Creating Symbol Table:
387 MSG_LINKING = Linking:
388 MSG_COMPILING = Compiling C:
389 MSG_COMPILING_CPP = Compiling C++:
390 MSG_ASSEMBLING = Assembling:
391 MSG_CLEANING = Cleaning project:
392 MSG_CREATING_LIBRARY = Creating library:
397 # Define all object files.
398 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
400 # Define all listing files.
401 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
404 # Compiler flags to generate dependency files.
405 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
408 # Combine all necessary flags and optional flags.
409 # Add target processor to flags.
410 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
411 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
412 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
419 all: begin gccversion sizebefore build sizeafter end
424 # $(CC) -E $(ALL_CFLAGS) $<
426 # Change the build target to build a HEX file or a library.
427 build: elf hex eep lss sym
436 LIBNAME=lib$(TARGET).a
442 # AVR Studio 3.x does not check make's exit code but relies on
443 # the following magic strings to be generated by the compile job.
453 # Display size of file.
454 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
455 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
456 ELFSIZE = $(SIZE) $(TARGET).elf
459 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
460 2>/dev/null; echo; fi
463 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
464 2>/dev/null; echo; fi
468 # Display compiler version information.
474 # Program the device.
475 program: $(TARGET).hex $(TARGET).eep
476 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
479 # Generate avr-gdb config/init file which does the following:
480 # define the reset signal, load the target file, connect to target, and set
481 # a breakpoint at main().
483 @$(REMOVE) $(GDBINIT_FILE)
484 @echo define reset >> $(GDBINIT_FILE)
485 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
486 @echo end >> $(GDBINIT_FILE)
487 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
488 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
489 ifeq ($(DEBUG_BACKEND),simulavr)
490 @echo load >> $(GDBINIT_FILE)
492 @echo break main >> $(GDBINIT_FILE)
494 debug: gdb-config $(TARGET).elf
495 ifeq ($(DEBUG_BACKEND), avarice)
496 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
497 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
498 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
499 @$(WINSHELL) /c pause
502 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
503 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
505 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
510 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
511 COFFCONVERT = $(OBJCOPY) --debugging
512 COFFCONVERT += --change-section-address .data-0x800000
513 COFFCONVERT += --change-section-address .bss-0x800000
514 COFFCONVERT += --change-section-address .noinit-0x800000
515 COFFCONVERT += --change-section-address .eeprom-0x810000
521 @echo $(MSG_COFF) $(TARGET).cof
522 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
525 extcoff: $(TARGET).elf
527 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
528 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
532 # Create final output files (.hex, .eep) from ELF output file.
535 @echo $(MSG_FLASH) $@
536 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
540 @echo $(MSG_EEPROM) $@
541 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
542 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
544 # Create extended listing file from ELF output file.
547 @echo $(MSG_EXTENDED_LISTING) $@
548 $(OBJDUMP) -h -S -z $< > $@
550 # Create a symbol table from ELF output file.
553 @echo $(MSG_SYMBOL_TABLE) $@
558 # Create library from object files.
559 .SECONDARY : $(TARGET).a
563 @echo $(MSG_CREATING_LIBRARY) $@
567 # Link: create ELF output file from object files.
568 .SECONDARY : $(TARGET).elf
572 @echo $(MSG_LINKING) $@
573 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
576 # Compile: create object files from C source files.
579 @echo $(MSG_COMPILING) $<
580 $(CC) -c $(ALL_CFLAGS) $< -o $@
583 # Compile: create object files from C++ source files.
584 $(OBJDIR)/%.o : %.cpp
586 @echo $(MSG_COMPILING_CPP) $<
587 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
590 # Compile: create assembler files from C source files.
592 $(CC) -S $(ALL_CFLAGS) $< -o $@
595 # Compile: create assembler files from C++ source files.
597 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
600 # Assemble: create object files from assembler source files.
603 @echo $(MSG_ASSEMBLING) $<
604 $(CC) -c $(ALL_ASFLAGS) $< -o $@
607 # Create preprocessed source for use in sending a bug report.
609 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
612 # Target: clean project.
613 clean: begin clean_list end
617 @echo $(MSG_CLEANING)
618 $(REMOVE) $(TARGET).hex
619 $(REMOVE) $(TARGET).eep
620 $(REMOVE) $(TARGET).cof
621 $(REMOVE) $(TARGET).elf
622 $(REMOVE) $(TARGET).map
623 $(REMOVE) $(TARGET).sym
624 $(REMOVE) $(TARGET).lss
625 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
626 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
627 $(REMOVE) $(SRC:.c=.s)
628 $(REMOVE) $(SRC:.c=.d)
629 $(REMOVE) $(SRC:.c=.i)
633 # Create object files directory
634 $(shell mkdir $(OBJDIR) 2>/dev/null)
637 # Include the dependency files.
638 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
641 # Listing of phony targets.
642 .PHONY : all begin finish end sizebefore sizeafter gccversion \
643 build elf hex eep lss sym coff extcoff \
644 clean clean_list program debug gdb-config