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.)
66 # C source file search path
67 VPATH = $(TARGET_DIR):$(COMMON_DIR)
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.
111 EXTRAINCDIRS = $(TARGET_DIR) $(COMMON_DIR)
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
124 CDEFS += -DDESCRIPTION=$(DESCRIPTION)
125 CDEFS += -DVENDOR_ID=$(VENDOR_ID)
126 CDEFS += -DPRODUCT_ID=$(PRODUCT_ID)
127 CDEFS += -DMANUFACTURER=$(MANUFACTURER)
128 CDEFS += -DPRODUCT=$(PRODUCT)
129 ifdef MOUSE_DELAY_TIME
130 CDEFS += -DMOUSE_DELAY_TIME=$(MOUSE_DELAY_TIME)
133 CDEFS += -DNKRO_ENABLE
137 # Place -D or -U options here for ASM sources
138 ADEFS = -DF_CPU=$(F_CPU)
139 ADEFS += -DDESCRIPTION=$(DESCRIPTION)
140 ADEFS += -DVENDOR_ID=$(VENDOR_ID)
141 ADEFS += -DPRODUCT_ID=$(PRODUCT_ID)
142 ADEFS += -DMANUFACTURER=$(MANUFACTURER)
143 ADEFS += -DPRODUCT=$(PRODUCT)
144 ifdef MOUSE_DELAY_TIME
145 ADEFS += -DMOUSE_DELAY_TIME=$(MOUSE_DELAY_TIME)
149 # Place -D or -U options here for C++ sources
150 CPPDEFS = -DF_CPU=$(F_CPU)UL -DDESCRIPTION=$(DESCRIPTION) -DVENDOR_ID=$(VENDOR_ID) -DPRODUCT_ID=$(PRODUCT_ID)
151 #CPPDEFS += -D__STDC_LIMIT_MACROS
152 #CPPDEFS += -D__STDC_CONSTANT_MACROS
153 CPPDEFS += -DDESCRIPTION=$(DESCRIPTION)
154 CPPDEFS += -DVENDOR_ID=$(VENDOR_ID)
155 CPPDEFS += -DPRODUCT_ID=$(PRODUCT_ID)
156 CPPDEFS += -DMANUFACTURER=$(MANUFACTURER)
157 CPPDEFS += -DPRODUCT=$(PRODUCT)
158 ifdef MOUSE_DELAY_TIME
159 CPPDEFS += -DMOUSE_DELAY_TIME=$(MOUSE_DELAY_TIME)
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
174 CFLAGS += -funsigned-char
175 CFLAGS += -funsigned-bitfields
176 CFLAGS += -ffunction-sections
177 CFLAGS += -fpack-struct
178 CFLAGS += -fshort-enums
180 CFLAGS += -Wstrict-prototypes
181 #CFLAGS += -mshort-calls
182 #CFLAGS += -fno-unit-at-a-time
184 #CFLAGS += -Wunreachable-code
185 #CFLAGS += -Wsign-compare
186 CFLAGS += -Wa,-adhlns=$(@:%.o=$(OBJDIR)/%.lst)
187 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
188 CFLAGS += $(CSTANDARD)
191 #---------------- Compiler Options C++ ----------------
192 # -g*: generate debugging information
193 # -O*: optimization level
194 # -f...: tuning, see GCC manual and avr-libc documentation
195 # -Wall...: warning level
196 # -Wa,...: tell GCC to pass this to the assembler.
197 # -adhlns...: create assembler listing
198 CPPFLAGS = -g$(DEBUG)
199 CPPFLAGS += $(CPPDEFS)
201 CPPFLAGS += -funsigned-char
202 CPPFLAGS += -funsigned-bitfields
203 CPPFLAGS += -fpack-struct
204 CPPFLAGS += -fshort-enums
205 CPPFLAGS += -fno-exceptions
208 #CPPFLAGS += -mshort-calls
209 #CPPFLAGS += -fno-unit-at-a-time
210 #CPPFLAGS += -Wstrict-prototypes
211 #CPPFLAGS += -Wunreachable-code
212 #CPPFLAGS += -Wsign-compare
213 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
214 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
215 #CPPFLAGS += $(CSTANDARD)
218 #---------------- Assembler Options ----------------
219 # -Wa,...: tell GCC to pass this to the assembler.
220 # -adhlns: create listing
221 # -gstabs: have the assembler create line number information; note that
222 # for use in COFF files, additional information about filenames
223 # and function names needs to be present in the assembler source
224 # files -- see avr-libc docs [FIXME: not yet described there]
225 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
226 # dump that will be displayed for a given single line of source input.
227 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
230 #---------------- Library Options ----------------
231 # Minimalistic printf version
232 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
234 # Floating point printf version (requires MATH_LIB = -lm below)
235 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
237 # If this is left blank, then it will use the Standard printf version.
239 #PRINTF_LIB = $(PRINTF_LIB_MIN)
240 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
243 # Minimalistic scanf version
244 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
246 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
247 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
249 # If this is left blank, then it will use the Standard scanf version.
251 #SCANF_LIB = $(SCANF_LIB_MIN)
252 #SCANF_LIB = $(SCANF_LIB_FLOAT)
258 # List any extra directories to look for libraries here.
259 # Each directory must be seperated by a space.
260 # Use forward slashes for directory separators.
261 # For a directory that has spaces, enclose it in quotes.
266 #---------------- External Memory Options ----------------
268 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
269 # used for variables (.data/.bss) and heap (malloc()).
270 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
272 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
273 # only used for heap (malloc()).
274 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
280 #---------------- Linker Options ----------------
281 # -Wl,...: tell GCC to pass this to linker.
282 # -Map: create map file
283 # --cref: add cross reference to map file
284 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
285 LDFLAGS += -Wl,--relax
286 LDFLAGS += -Wl,--gc-sections
287 LDFLAGS += $(EXTMEMOPTS)
288 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
289 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
290 #LDFLAGS += -T linker_script.x
294 #---------------- Programming Options (avrdude) ----------------
296 # Programming hardware
298 # to get a full listing.
300 AVRDUDE_PROGRAMMER = stk500v2
302 # com1 = serial port. Use lpt1 to connect to parallel port.
303 AVRDUDE_PORT = com1 # programmer connected to serial device
305 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
306 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
309 # Uncomment the following if you want avrdude's erase cycle counter.
310 # Note that this counter needs to be initialized first using -Yn,
311 # see avrdude manual.
312 #AVRDUDE_ERASE_COUNTER = -y
314 # Uncomment the following if you do /not/ wish a verification to be
315 # performed after programming the device.
316 #AVRDUDE_NO_VERIFY = -V
318 # Increase verbosity level. Please use this when submitting bug
319 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
320 # to submit bug reports.
321 #AVRDUDE_VERBOSE = -v -v
323 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
324 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
325 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
326 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
330 #---------------- Debugging Options ----------------
332 # For simulavr only - target MCU frequency.
333 DEBUG_MFREQ = $(F_CPU)
335 # Set the DEBUG_UI to either gdb or insight.
339 # Set the debugging back-end to either avarice, simulavr.
340 DEBUG_BACKEND = avarice
341 #DEBUG_BACKEND = simulavr
344 GDBINIT_FILE = __avr_gdbinit
346 # When using avarice settings for the JTAG
349 # Debugging port used to communicate between GDB / avarice / simulavr.
352 # Debugging host used to communicate between GDB / avarice / simulavr, normally
353 # just set to localhost unless doing some sort of crazy debugging when
354 # avarice is running on a different computer.
355 DEBUG_HOST = localhost
359 #============================================================================
362 # Define programs and commands.
365 OBJCOPY = avr-objcopy
366 OBJDUMP = avr-objdump
379 MSG_ERRORS_NONE = Errors: none
380 MSG_BEGIN = -------- begin --------
381 MSG_END = -------- end --------
382 MSG_SIZE_BEFORE = Size before:
383 MSG_SIZE_AFTER = Size after:
384 MSG_COFF = Converting to AVR COFF:
385 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
386 MSG_FLASH = Creating load file for Flash:
387 MSG_EEPROM = Creating load file for EEPROM:
388 MSG_EXTENDED_LISTING = Creating Extended Listing:
389 MSG_SYMBOL_TABLE = Creating Symbol Table:
390 MSG_LINKING = Linking:
391 MSG_COMPILING = Compiling C:
392 MSG_COMPILING_CPP = Compiling C++:
393 MSG_ASSEMBLING = Assembling:
394 MSG_CLEANING = Cleaning project:
395 MSG_CREATING_LIBRARY = Creating library:
400 # Define all object files.
401 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
403 # Define all listing files.
404 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
407 # Compiler flags to generate dependency files.
408 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
411 # Combine all necessary flags and optional flags.
412 # Add target processor to flags.
413 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
414 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
415 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
422 all: begin gccversion sizebefore build sizeafter end
424 # Change the build target to build a HEX file or a library.
425 build: elf hex eep lss sym
434 LIBNAME=lib$(TARGET).a
440 # AVR Studio 3.x does not check make's exit code but relies on
441 # the following magic strings to be generated by the compile job.
451 # Display size of file.
452 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
453 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
454 ELFSIZE = $(SIZE) $(TARGET).elf
457 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
458 2>/dev/null; echo; fi
461 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
462 2>/dev/null; echo; fi
466 # Display compiler version information.
472 # Program the device.
473 program: $(TARGET).hex $(TARGET).eep
474 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
477 # Generate avr-gdb config/init file which does the following:
478 # define the reset signal, load the target file, connect to target, and set
479 # a breakpoint at main().
481 @$(REMOVE) $(GDBINIT_FILE)
482 @echo define reset >> $(GDBINIT_FILE)
483 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
484 @echo end >> $(GDBINIT_FILE)
485 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
486 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
487 ifeq ($(DEBUG_BACKEND),simulavr)
488 @echo load >> $(GDBINIT_FILE)
490 @echo break main >> $(GDBINIT_FILE)
492 debug: gdb-config $(TARGET).elf
493 ifeq ($(DEBUG_BACKEND), avarice)
494 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
495 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
496 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
497 @$(WINSHELL) /c pause
500 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
501 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
503 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
508 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
509 COFFCONVERT = $(OBJCOPY) --debugging
510 COFFCONVERT += --change-section-address .data-0x800000
511 COFFCONVERT += --change-section-address .bss-0x800000
512 COFFCONVERT += --change-section-address .noinit-0x800000
513 COFFCONVERT += --change-section-address .eeprom-0x810000
519 @echo $(MSG_COFF) $(TARGET).cof
520 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
523 extcoff: $(TARGET).elf
525 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
526 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
530 # Create final output files (.hex, .eep) from ELF output file.
533 @echo $(MSG_FLASH) $@
534 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
538 @echo $(MSG_EEPROM) $@
539 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
540 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
542 # Create extended listing file from ELF output file.
545 @echo $(MSG_EXTENDED_LISTING) $@
546 $(OBJDUMP) -h -S -z $< > $@
548 # Create a symbol table from ELF output file.
551 @echo $(MSG_SYMBOL_TABLE) $@
556 # Create library from object files.
557 .SECONDARY : $(TARGET).a
561 @echo $(MSG_CREATING_LIBRARY) $@
565 # Link: create ELF output file from object files.
566 .SECONDARY : $(TARGET).elf
570 @echo $(MSG_LINKING) $@
571 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
574 # Compile: create object files from C source files.
577 @echo $(MSG_COMPILING) $<
578 $(CC) -c $(ALL_CFLAGS) $< -o $@
581 # Compile: create object files from C++ source files.
582 $(OBJDIR)/%.o : %.cpp
584 @echo $(MSG_COMPILING_CPP) $<
585 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
588 # Compile: create assembler files from C source files.
590 $(CC) -S $(ALL_CFLAGS) $< -o $@
593 # Compile: create assembler files from C++ source files.
595 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
598 # Assemble: create object files from assembler source files.
601 @echo $(MSG_ASSEMBLING) $<
602 $(CC) -c $(ALL_ASFLAGS) $< -o $@
605 # Create preprocessed source for use in sending a bug report.
607 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
610 # Target: clean project.
611 clean: begin clean_list end
615 @echo $(MSG_CLEANING)
616 $(REMOVE) $(TARGET).hex
617 $(REMOVE) $(TARGET).eep
618 $(REMOVE) $(TARGET).cof
619 $(REMOVE) $(TARGET).elf
620 $(REMOVE) $(TARGET).map
621 $(REMOVE) $(TARGET).sym
622 $(REMOVE) $(TARGET).lss
623 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
624 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
625 $(REMOVE) $(SRC:.c=.s)
626 $(REMOVE) $(SRC:.c=.d)
627 $(REMOVE) $(SRC:.c=.i)
631 # Create object files directory
632 $(shell mkdir $(OBJDIR) 2>/dev/null)
635 # Include the dependency files.
636 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
639 # Listing of phony targets.
640 .PHONY : all begin finish end sizebefore sizeafter gccversion \
641 build elf hex eep lss sym coff extcoff \
642 clean clean_list program debug gdb-config