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)
134 # Place -D or -U options here for ASM sources
135 ADEFS = -DF_CPU=$(F_CPU)
136 ADEFS += -DDESCRIPTION=$(DESCRIPTION)
137 ADEFS += -DVENDOR_ID=$(VENDOR_ID)
138 ADEFS += -DPRODUCT_ID=$(PRODUCT_ID)
139 ADEFS += -DMANUFACTURER=$(MANUFACTURER)
140 ADEFS += -DPRODUCT=$(PRODUCT)
141 ifdef MOUSE_DELAY_TIME
142 ADEFS += -DMOUSE_DELAY_TIME=$(MOUSE_DELAY_TIME)
146 # Place -D or -U options here for C++ sources
147 CPPDEFS = -DF_CPU=$(F_CPU)UL -DDESCRIPTION=$(DESCRIPTION) -DVENDOR_ID=$(VENDOR_ID) -DPRODUCT_ID=$(PRODUCT_ID)
148 #CPPDEFS += -D__STDC_LIMIT_MACROS
149 #CPPDEFS += -D__STDC_CONSTANT_MACROS
150 CPPDEFS += -DDESCRIPTION=$(DESCRIPTION)
151 CPPDEFS += -DVENDOR_ID=$(VENDOR_ID)
152 CPPDEFS += -DPRODUCT_ID=$(PRODUCT_ID)
153 CPPDEFS += -DMANUFACTURER=$(MANUFACTURER)
154 CPPDEFS += -DPRODUCT=$(PRODUCT)
155 ifdef MOUSE_DELAY_TIME
156 CPPDEFS += -DMOUSE_DELAY_TIME=$(MOUSE_DELAY_TIME)
161 #---------------- Compiler Options C ----------------
162 # -g*: generate debugging information
163 # -O*: optimization level
164 # -f...: tuning, see GCC manual and avr-libc documentation
165 # -Wall...: warning level
166 # -Wa,...: tell GCC to pass this to the assembler.
167 # -adhlns...: create assembler listing
171 CFLAGS += -funsigned-char
172 CFLAGS += -funsigned-bitfields
173 CFLAGS += -ffunction-sections
174 CFLAGS += -fpack-struct
175 CFLAGS += -fshort-enums
177 CFLAGS += -Wstrict-prototypes
178 #CFLAGS += -mshort-calls
179 #CFLAGS += -fno-unit-at-a-time
181 #CFLAGS += -Wunreachable-code
182 #CFLAGS += -Wsign-compare
183 CFLAGS += -Wa,-adhlns=$(@:%.o=$(OBJDIR)/%.lst)
184 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
185 CFLAGS += $(CSTANDARD)
188 #---------------- Compiler Options C++ ----------------
189 # -g*: generate debugging information
190 # -O*: optimization level
191 # -f...: tuning, see GCC manual and avr-libc documentation
192 # -Wall...: warning level
193 # -Wa,...: tell GCC to pass this to the assembler.
194 # -adhlns...: create assembler listing
195 CPPFLAGS = -g$(DEBUG)
196 CPPFLAGS += $(CPPDEFS)
198 CPPFLAGS += -funsigned-char
199 CPPFLAGS += -funsigned-bitfields
200 CPPFLAGS += -fpack-struct
201 CPPFLAGS += -fshort-enums
202 CPPFLAGS += -fno-exceptions
205 #CPPFLAGS += -mshort-calls
206 #CPPFLAGS += -fno-unit-at-a-time
207 #CPPFLAGS += -Wstrict-prototypes
208 #CPPFLAGS += -Wunreachable-code
209 #CPPFLAGS += -Wsign-compare
210 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
211 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
212 #CPPFLAGS += $(CSTANDARD)
215 #---------------- Assembler Options ----------------
216 # -Wa,...: tell GCC to pass this to the assembler.
217 # -adhlns: create listing
218 # -gstabs: have the assembler create line number information; note that
219 # for use in COFF files, additional information about filenames
220 # and function names needs to be present in the assembler source
221 # files -- see avr-libc docs [FIXME: not yet described there]
222 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
223 # dump that will be displayed for a given single line of source input.
224 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
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
421 # Change the build target to build a HEX file or a library.
422 build: elf hex eep lss sym
431 LIBNAME=lib$(TARGET).a
437 # AVR Studio 3.x does not check make's exit code but relies on
438 # the following magic strings to be generated by the compile job.
448 # Display size of file.
449 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
450 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
451 ELFSIZE = $(SIZE) $(TARGET).elf
454 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
455 2>/dev/null; echo; fi
458 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
459 2>/dev/null; echo; fi
463 # Display compiler version information.
469 # Program the device.
470 program: $(TARGET).hex $(TARGET).eep
471 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
474 # Generate avr-gdb config/init file which does the following:
475 # define the reset signal, load the target file, connect to target, and set
476 # a breakpoint at main().
478 @$(REMOVE) $(GDBINIT_FILE)
479 @echo define reset >> $(GDBINIT_FILE)
480 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
481 @echo end >> $(GDBINIT_FILE)
482 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
483 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
484 ifeq ($(DEBUG_BACKEND),simulavr)
485 @echo load >> $(GDBINIT_FILE)
487 @echo break main >> $(GDBINIT_FILE)
489 debug: gdb-config $(TARGET).elf
490 ifeq ($(DEBUG_BACKEND), avarice)
491 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
492 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
493 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
494 @$(WINSHELL) /c pause
497 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
498 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
500 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
505 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
506 COFFCONVERT = $(OBJCOPY) --debugging
507 COFFCONVERT += --change-section-address .data-0x800000
508 COFFCONVERT += --change-section-address .bss-0x800000
509 COFFCONVERT += --change-section-address .noinit-0x800000
510 COFFCONVERT += --change-section-address .eeprom-0x810000
516 @echo $(MSG_COFF) $(TARGET).cof
517 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
520 extcoff: $(TARGET).elf
522 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
523 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
527 # Create final output files (.hex, .eep) from ELF output file.
530 @echo $(MSG_FLASH) $@
531 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
535 @echo $(MSG_EEPROM) $@
536 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
537 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
539 # Create extended listing file from ELF output file.
542 @echo $(MSG_EXTENDED_LISTING) $@
543 $(OBJDUMP) -h -S -z $< > $@
545 # Create a symbol table from ELF output file.
548 @echo $(MSG_SYMBOL_TABLE) $@
553 # Create library from object files.
554 .SECONDARY : $(TARGET).a
558 @echo $(MSG_CREATING_LIBRARY) $@
562 # Link: create ELF output file from object files.
563 .SECONDARY : $(TARGET).elf
567 @echo $(MSG_LINKING) $@
568 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
571 # Compile: create object files from C source files.
574 @echo $(MSG_COMPILING) $<
575 $(CC) -c $(ALL_CFLAGS) $< -o $@
578 # Compile: create object files from C++ source files.
579 $(OBJDIR)/%.o : %.cpp
581 @echo $(MSG_COMPILING_CPP) $<
582 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
585 # Compile: create assembler files from C source files.
587 $(CC) -S $(ALL_CFLAGS) $< -o $@
590 # Compile: create assembler files from C++ source files.
592 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
595 # Assemble: create object files from assembler source files.
598 @echo $(MSG_ASSEMBLING) $<
599 $(CC) -c $(ALL_ASFLAGS) $< -o $@
602 # Create preprocessed source for use in sending a bug report.
604 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
607 # Target: clean project.
608 clean: begin clean_list end
612 @echo $(MSG_CLEANING)
613 $(REMOVE) $(TARGET).hex
614 $(REMOVE) $(TARGET).eep
615 $(REMOVE) $(TARGET).cof
616 $(REMOVE) $(TARGET).elf
617 $(REMOVE) $(TARGET).map
618 $(REMOVE) $(TARGET).sym
619 $(REMOVE) $(TARGET).lss
620 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
621 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
622 $(REMOVE) $(SRC:.c=.s)
623 $(REMOVE) $(SRC:.c=.d)
624 $(REMOVE) $(SRC:.c=.i)
628 # Create object files directory
629 $(shell mkdir $(OBJDIR) 2>/dev/null)
632 # Include the dependency files.
633 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
636 # Listing of phony targets.
637 .PHONY : all begin finish end sizebefore sizeafter gccversion \
638 build elf hex eep lss sym coff extcoff \
639 clean clean_list program debug gdb-config