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.)
63 # C source file search path
64 VPATH = $(TARGET_DIR):$(COMMON_DIR)
67 # Output format. (can be srec, ihex, binary)
71 # Object files directory
72 # To put object files in current directory, use a dot (.), do NOT make
73 # this an empty or blank macro!
77 # List C++ source files here. (C dependencies are automatically generated.)
81 # List Assembler source files here.
82 # Make them always end in a capital .S. Files ending in a lowercase .s
83 # will not be considered source files but generated files (assembler
84 # output from the compiler), and will be deleted upon "make clean"!
85 # Even though the DOS/Win* filesystem matches both .s and .S the same,
86 # it will preserve the spelling of the filenames, and gcc itself does
87 # care about how the name is spelled on its command-line.
91 # Optimization level, can be [0, 1, 2, 3, s].
92 # 0 = turn off optimization. s = optimize for size.
93 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
98 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
99 # AVR Studio 4.10 requires dwarf-2.
100 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
104 # List any extra directories to look for include files here.
105 # Each directory must be seperated by a space.
106 # Use forward slashes for directory separators.
107 # For a directory that has spaces, enclose it in quotes.
108 EXTRAINCDIRS = $(TARGET_DIR) $(COMMON_DIR)
111 # Compiler flag to set the C Standard level.
113 # gnu89 = c89 plus GCC extensions
114 # c99 = ISO C99 standard (not yet fully implemented)
115 # gnu99 = c99 plus GCC extensions
116 CSTANDARD = -std=gnu99
119 # Place -D or -U options here for C sources
120 CDEFS = -DF_CPU=$(F_CPU)UL
121 CDEFS += -DDESCRIPTION=$(DESCRIPTION)
122 CDEFS += -DVENDOR_ID=$(VENDOR_ID)
123 CDEFS += -DPRODUCT_ID=$(PRODUCT_ID)
124 CDEFS += -DMANUFACTURER=$(MANUFACTURER)
125 CDEFS += -DPRODUCT=$(PRODUCT)
126 ifdef MOUSE_DELAY_TIME
127 CDEFS += -DMOUSE_DELAY_TIME=$(MOUSE_DELAY_TIME)
131 # Place -D or -U options here for ASM sources
132 ADEFS = -DF_CPU=$(F_CPU)
133 ADEFS += -DDESCRIPTION=$(DESCRIPTION)
134 ADEFS += -DVENDOR_ID=$(VENDOR_ID)
135 ADEFS += -DPRODUCT_ID=$(PRODUCT_ID)
136 ADEFS += -DMANUFACTURER=$(MANUFACTURER)
137 ADEFS += -DPRODUCT=$(PRODUCT)
138 ifdef MOUSE_DELAY_TIME
139 ADEFS += -DMOUSE_DELAY_TIME=$(MOUSE_DELAY_TIME)
143 # Place -D or -U options here for C++ sources
144 CPPDEFS = -DF_CPU=$(F_CPU)UL -DDESCRIPTION=$(DESCRIPTION) -DVENDOR_ID=$(VENDOR_ID) -DPRODUCT_ID=$(PRODUCT_ID)
145 #CPPDEFS += -D__STDC_LIMIT_MACROS
146 #CPPDEFS += -D__STDC_CONSTANT_MACROS
147 CPPDEFS += -DDESCRIPTION=$(DESCRIPTION)
148 CPPDEFS += -DVENDOR_ID=$(VENDOR_ID)
149 CPPDEFS += -DPRODUCT_ID=$(PRODUCT_ID)
150 CPPDEFS += -DMANUFACTURER=$(MANUFACTURER)
151 CPPDEFS += -DPRODUCT=$(PRODUCT)
152 ifdef MOUSE_DELAY_TIME
153 CPPDEFS += -DMOUSE_DELAY_TIME=$(MOUSE_DELAY_TIME)
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)
185 #---------------- Compiler Options C++ ----------------
186 # -g*: generate debugging information
187 # -O*: optimization level
188 # -f...: tuning, see GCC manual and avr-libc documentation
189 # -Wall...: warning level
190 # -Wa,...: tell GCC to pass this to the assembler.
191 # -adhlns...: create assembler listing
192 CPPFLAGS = -g$(DEBUG)
193 CPPFLAGS += $(CPPDEFS)
195 CPPFLAGS += -funsigned-char
196 CPPFLAGS += -funsigned-bitfields
197 CPPFLAGS += -fpack-struct
198 CPPFLAGS += -fshort-enums
199 CPPFLAGS += -fno-exceptions
202 #CPPFLAGS += -mshort-calls
203 #CPPFLAGS += -fno-unit-at-a-time
204 #CPPFLAGS += -Wstrict-prototypes
205 #CPPFLAGS += -Wunreachable-code
206 #CPPFLAGS += -Wsign-compare
207 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
208 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
209 #CPPFLAGS += $(CSTANDARD)
212 #---------------- Assembler Options ----------------
213 # -Wa,...: tell GCC to pass this to the assembler.
214 # -adhlns: create listing
215 # -gstabs: have the assembler create line number information; note that
216 # for use in COFF files, additional information about filenames
217 # and function names needs to be present in the assembler source
218 # files -- see avr-libc docs [FIXME: not yet described there]
219 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
220 # dump that will be displayed for a given single line of source input.
221 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
224 #---------------- Library Options ----------------
225 # Minimalistic printf version
226 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
228 # Floating point printf version (requires MATH_LIB = -lm below)
229 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
231 # If this is left blank, then it will use the Standard printf version.
233 #PRINTF_LIB = $(PRINTF_LIB_MIN)
234 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
237 # Minimalistic scanf version
238 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
240 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
241 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
243 # If this is left blank, then it will use the Standard scanf version.
245 #SCANF_LIB = $(SCANF_LIB_MIN)
246 #SCANF_LIB = $(SCANF_LIB_FLOAT)
252 # List any extra directories to look for libraries here.
253 # Each directory must be seperated by a space.
254 # Use forward slashes for directory separators.
255 # For a directory that has spaces, enclose it in quotes.
260 #---------------- External Memory Options ----------------
262 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
263 # used for variables (.data/.bss) and heap (malloc()).
264 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
266 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
267 # only used for heap (malloc()).
268 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
274 #---------------- Linker Options ----------------
275 # -Wl,...: tell GCC to pass this to linker.
276 # -Map: create map file
277 # --cref: add cross reference to map file
278 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
279 LDFLAGS += -Wl,--relax
280 LDFLAGS += -Wl,--gc-sections
281 LDFLAGS += $(EXTMEMOPTS)
282 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
283 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
284 #LDFLAGS += -T linker_script.x
288 #---------------- Programming Options (avrdude) ----------------
290 # Programming hardware
292 # to get a full listing.
294 AVRDUDE_PROGRAMMER = stk500v2
296 # com1 = serial port. Use lpt1 to connect to parallel port.
297 AVRDUDE_PORT = com1 # programmer connected to serial device
299 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
300 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
303 # Uncomment the following if you want avrdude's erase cycle counter.
304 # Note that this counter needs to be initialized first using -Yn,
305 # see avrdude manual.
306 #AVRDUDE_ERASE_COUNTER = -y
308 # Uncomment the following if you do /not/ wish a verification to be
309 # performed after programming the device.
310 #AVRDUDE_NO_VERIFY = -V
312 # Increase verbosity level. Please use this when submitting bug
313 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
314 # to submit bug reports.
315 #AVRDUDE_VERBOSE = -v -v
317 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
318 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
319 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
320 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
324 #---------------- Debugging Options ----------------
326 # For simulavr only - target MCU frequency.
327 DEBUG_MFREQ = $(F_CPU)
329 # Set the DEBUG_UI to either gdb or insight.
333 # Set the debugging back-end to either avarice, simulavr.
334 DEBUG_BACKEND = avarice
335 #DEBUG_BACKEND = simulavr
338 GDBINIT_FILE = __avr_gdbinit
340 # When using avarice settings for the JTAG
343 # Debugging port used to communicate between GDB / avarice / simulavr.
346 # Debugging host used to communicate between GDB / avarice / simulavr, normally
347 # just set to localhost unless doing some sort of crazy debugging when
348 # avarice is running on a different computer.
349 DEBUG_HOST = localhost
353 #============================================================================
356 # Define programs and commands.
359 OBJCOPY = avr-objcopy
360 OBJDUMP = avr-objdump
373 MSG_ERRORS_NONE = Errors: none
374 MSG_BEGIN = -------- begin --------
375 MSG_END = -------- end --------
376 MSG_SIZE_BEFORE = Size before:
377 MSG_SIZE_AFTER = Size after:
378 MSG_COFF = Converting to AVR COFF:
379 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
380 MSG_FLASH = Creating load file for Flash:
381 MSG_EEPROM = Creating load file for EEPROM:
382 MSG_EXTENDED_LISTING = Creating Extended Listing:
383 MSG_SYMBOL_TABLE = Creating Symbol Table:
384 MSG_LINKING = Linking:
385 MSG_COMPILING = Compiling C:
386 MSG_COMPILING_CPP = Compiling C++:
387 MSG_ASSEMBLING = Assembling:
388 MSG_CLEANING = Cleaning project:
389 MSG_CREATING_LIBRARY = Creating library:
394 # Define all object files.
395 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
397 # Define all listing files.
398 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
401 # Compiler flags to generate dependency files.
402 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
405 # Combine all necessary flags and optional flags.
406 # Add target processor to flags.
407 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
408 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
409 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
416 all: begin gccversion sizebefore build sizeafter end
418 # Change the build target to build a HEX file or a library.
419 build: elf hex eep lss sym
428 LIBNAME=lib$(TARGET).a
434 # AVR Studio 3.x does not check make's exit code but relies on
435 # the following magic strings to be generated by the compile job.
445 # Display size of file.
446 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
447 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
448 ELFSIZE = $(SIZE) $(TARGET).elf
451 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
452 2>/dev/null; echo; fi
455 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
456 2>/dev/null; echo; fi
460 # Display compiler version information.
466 # Program the device.
467 program: $(TARGET).hex $(TARGET).eep
468 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
471 # Generate avr-gdb config/init file which does the following:
472 # define the reset signal, load the target file, connect to target, and set
473 # a breakpoint at main().
475 @$(REMOVE) $(GDBINIT_FILE)
476 @echo define reset >> $(GDBINIT_FILE)
477 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
478 @echo end >> $(GDBINIT_FILE)
479 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
480 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
481 ifeq ($(DEBUG_BACKEND),simulavr)
482 @echo load >> $(GDBINIT_FILE)
484 @echo break main >> $(GDBINIT_FILE)
486 debug: gdb-config $(TARGET).elf
487 ifeq ($(DEBUG_BACKEND), avarice)
488 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
489 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
490 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
491 @$(WINSHELL) /c pause
494 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
495 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
497 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
502 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
503 COFFCONVERT = $(OBJCOPY) --debugging
504 COFFCONVERT += --change-section-address .data-0x800000
505 COFFCONVERT += --change-section-address .bss-0x800000
506 COFFCONVERT += --change-section-address .noinit-0x800000
507 COFFCONVERT += --change-section-address .eeprom-0x810000
513 @echo $(MSG_COFF) $(TARGET).cof
514 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
517 extcoff: $(TARGET).elf
519 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
520 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
524 # Create final output files (.hex, .eep) from ELF output file.
527 @echo $(MSG_FLASH) $@
528 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
532 @echo $(MSG_EEPROM) $@
533 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
534 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
536 # Create extended listing file from ELF output file.
539 @echo $(MSG_EXTENDED_LISTING) $@
540 $(OBJDUMP) -h -S -z $< > $@
542 # Create a symbol table from ELF output file.
545 @echo $(MSG_SYMBOL_TABLE) $@
550 # Create library from object files.
551 .SECONDARY : $(TARGET).a
555 @echo $(MSG_CREATING_LIBRARY) $@
559 # Link: create ELF output file from object files.
560 .SECONDARY : $(TARGET).elf
564 @echo $(MSG_LINKING) $@
565 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
568 # Compile: create object files from C source files.
571 @echo $(MSG_COMPILING) $<
572 $(CC) -c $(ALL_CFLAGS) $< -o $@
575 # Compile: create object files from C++ source files.
576 $(OBJDIR)/%.o : %.cpp
578 @echo $(MSG_COMPILING_CPP) $<
579 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
582 # Compile: create assembler files from C source files.
584 $(CC) -S $(ALL_CFLAGS) $< -o $@
587 # Compile: create assembler files from C++ source files.
589 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
592 # Assemble: create object files from assembler source files.
595 @echo $(MSG_ASSEMBLING) $<
596 $(CC) -c $(ALL_ASFLAGS) $< -o $@
599 # Create preprocessed source for use in sending a bug report.
601 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
604 # Target: clean project.
605 clean: begin clean_list end
609 @echo $(MSG_CLEANING)
610 $(REMOVE) $(TARGET).hex
611 $(REMOVE) $(TARGET).eep
612 $(REMOVE) $(TARGET).cof
613 $(REMOVE) $(TARGET).elf
614 $(REMOVE) $(TARGET).map
615 $(REMOVE) $(TARGET).sym
616 $(REMOVE) $(TARGET).lss
617 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
618 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
619 $(REMOVE) $(SRC:.c=.s)
620 $(REMOVE) $(SRC:.c=.d)
621 $(REMOVE) $(SRC:.c=.i)
625 # Create object files directory
626 $(shell mkdir $(OBJDIR) 2>/dev/null)
629 # Include the dependency files.
630 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
633 # Listing of phony targets.
634 .PHONY : all begin finish end sizebefore sizeafter gccversion \
635 build elf hex eep lss sym coff extcoff \
636 clean clean_list program debug gdb-config