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.)
62 # C source file search path
63 VPATH = $(TARGET_DIR):$(COMMON_DIR)
66 # Output format. (can be srec, ihex, binary)
70 # Object files directory
71 # To put object files in current directory, use a dot (.), do NOT make
72 # this an empty or blank macro!
76 # List C++ source files here. (C dependencies are automatically generated.)
80 # List Assembler source files here.
81 # Make them always end in a capital .S. Files ending in a lowercase .s
82 # will not be considered source files but generated files (assembler
83 # output from the compiler), and will be deleted upon "make clean"!
84 # Even though the DOS/Win* filesystem matches both .s and .S the same,
85 # it will preserve the spelling of the filenames, and gcc itself does
86 # care about how the name is spelled on its command-line.
90 # Optimization level, can be [0, 1, 2, 3, s].
91 # 0 = turn off optimization. s = optimize for size.
92 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
97 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
98 # AVR Studio 4.10 requires dwarf-2.
99 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
103 # List any extra directories to look for include files here.
104 # Each directory must be seperated by a space.
105 # Use forward slashes for directory separators.
106 # For a directory that has spaces, enclose it in quotes.
107 EXTRAINCDIRS = $(TARGET_DIR) $(COMMON_DIR)
110 # Compiler flag to set the C Standard level.
112 # gnu89 = c89 plus GCC extensions
113 # c99 = ISO C99 standard (not yet fully implemented)
114 # gnu99 = c99 plus GCC extensions
115 CSTANDARD = -std=gnu99
118 # Place -D or -U options here for C sources
119 CDEFS = -DF_CPU=$(F_CPU)UL
122 # Place -D or -U options here for ASM sources
123 ADEFS = -DF_CPU=$(F_CPU)
126 # Place -D or -U options here for C++ sources
127 CPPDEFS = -DF_CPU=$(F_CPU)UL
128 #CPPDEFS += -D__STDC_LIMIT_MACROS
129 #CPPDEFS += -D__STDC_CONSTANT_MACROS
133 #---------------- Compiler Options C ----------------
134 # -g*: generate debugging information
135 # -O*: optimization level
136 # -f...: tuning, see GCC manual and avr-libc documentation
137 # -Wall...: warning level
138 # -Wa,...: tell GCC to pass this to the assembler.
139 # -adhlns...: create assembler listing
143 CFLAGS += -funsigned-char
144 CFLAGS += -funsigned-bitfields
145 CFLAGS += -ffunction-sections
146 CFLAGS += -fpack-struct
147 CFLAGS += -fshort-enums
149 CFLAGS += -Wstrict-prototypes
150 #CFLAGS += -mshort-calls
151 #CFLAGS += -fno-unit-at-a-time
153 #CFLAGS += -Wunreachable-code
154 #CFLAGS += -Wsign-compare
155 CFLAGS += -Wa,-adhlns=$(@:%.o=$(OBJDIR)/%.lst)
156 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
157 CFLAGS += $(CSTANDARD)
160 #---------------- Compiler Options C++ ----------------
161 # -g*: generate debugging information
162 # -O*: optimization level
163 # -f...: tuning, see GCC manual and avr-libc documentation
164 # -Wall...: warning level
165 # -Wa,...: tell GCC to pass this to the assembler.
166 # -adhlns...: create assembler listing
167 CPPFLAGS = -g$(DEBUG)
168 CPPFLAGS += $(CPPDEFS)
170 CPPFLAGS += -funsigned-char
171 CPPFLAGS += -funsigned-bitfields
172 CPPFLAGS += -fpack-struct
173 CPPFLAGS += -fshort-enums
174 CPPFLAGS += -fno-exceptions
177 #CPPFLAGS += -mshort-calls
178 #CPPFLAGS += -fno-unit-at-a-time
179 #CPPFLAGS += -Wstrict-prototypes
180 #CPPFLAGS += -Wunreachable-code
181 #CPPFLAGS += -Wsign-compare
182 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
183 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
184 #CPPFLAGS += $(CSTANDARD)
187 #---------------- Assembler Options ----------------
188 # -Wa,...: tell GCC to pass this to the assembler.
189 # -adhlns: create listing
190 # -gstabs: have the assembler create line number information; note that
191 # for use in COFF files, additional information about filenames
192 # and function names needs to be present in the assembler source
193 # files -- see avr-libc docs [FIXME: not yet described there]
194 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
195 # dump that will be displayed for a given single line of source input.
196 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
199 #---------------- Library Options ----------------
200 # Minimalistic printf version
201 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
203 # Floating point printf version (requires MATH_LIB = -lm below)
204 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
206 # If this is left blank, then it will use the Standard printf version.
208 #PRINTF_LIB = $(PRINTF_LIB_MIN)
209 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
212 # Minimalistic scanf version
213 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
215 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
216 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
218 # If this is left blank, then it will use the Standard scanf version.
220 #SCANF_LIB = $(SCANF_LIB_MIN)
221 #SCANF_LIB = $(SCANF_LIB_FLOAT)
227 # List any extra directories to look for libraries here.
228 # Each directory must be seperated by a space.
229 # Use forward slashes for directory separators.
230 # For a directory that has spaces, enclose it in quotes.
235 #---------------- External Memory Options ----------------
237 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
238 # used for variables (.data/.bss) and heap (malloc()).
239 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
241 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
242 # only used for heap (malloc()).
243 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
249 #---------------- Linker Options ----------------
250 # -Wl,...: tell GCC to pass this to linker.
251 # -Map: create map file
252 # --cref: add cross reference to map file
253 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
254 LDFLAGS += -Wl,--relax
255 LDFLAGS += -Wl,--gc-sections
256 LDFLAGS += $(EXTMEMOPTS)
257 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
258 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
259 #LDFLAGS += -T linker_script.x
263 #---------------- Programming Options (avrdude) ----------------
265 # Programming hardware
267 # to get a full listing.
269 AVRDUDE_PROGRAMMER = stk500v2
271 # com1 = serial port. Use lpt1 to connect to parallel port.
272 AVRDUDE_PORT = com1 # programmer connected to serial device
274 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
275 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
278 # Uncomment the following if you want avrdude's erase cycle counter.
279 # Note that this counter needs to be initialized first using -Yn,
280 # see avrdude manual.
281 #AVRDUDE_ERASE_COUNTER = -y
283 # Uncomment the following if you do /not/ wish a verification to be
284 # performed after programming the device.
285 #AVRDUDE_NO_VERIFY = -V
287 # Increase verbosity level. Please use this when submitting bug
288 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
289 # to submit bug reports.
290 #AVRDUDE_VERBOSE = -v -v
292 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
293 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
294 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
295 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
299 #---------------- Debugging Options ----------------
301 # For simulavr only - target MCU frequency.
302 DEBUG_MFREQ = $(F_CPU)
304 # Set the DEBUG_UI to either gdb or insight.
308 # Set the debugging back-end to either avarice, simulavr.
309 DEBUG_BACKEND = avarice
310 #DEBUG_BACKEND = simulavr
313 GDBINIT_FILE = __avr_gdbinit
315 # When using avarice settings for the JTAG
318 # Debugging port used to communicate between GDB / avarice / simulavr.
321 # Debugging host used to communicate between GDB / avarice / simulavr, normally
322 # just set to localhost unless doing some sort of crazy debugging when
323 # avarice is running on a different computer.
324 DEBUG_HOST = localhost
328 #============================================================================
331 # Define programs and commands.
334 OBJCOPY = avr-objcopy
335 OBJDUMP = avr-objdump
348 MSG_ERRORS_NONE = Errors: none
349 MSG_BEGIN = -------- begin --------
350 MSG_END = -------- end --------
351 MSG_SIZE_BEFORE = Size before:
352 MSG_SIZE_AFTER = Size after:
353 MSG_COFF = Converting to AVR COFF:
354 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
355 MSG_FLASH = Creating load file for Flash:
356 MSG_EEPROM = Creating load file for EEPROM:
357 MSG_EXTENDED_LISTING = Creating Extended Listing:
358 MSG_SYMBOL_TABLE = Creating Symbol Table:
359 MSG_LINKING = Linking:
360 MSG_COMPILING = Compiling C:
361 MSG_COMPILING_CPP = Compiling C++:
362 MSG_ASSEMBLING = Assembling:
363 MSG_CLEANING = Cleaning project:
364 MSG_CREATING_LIBRARY = Creating library:
369 # Define all object files.
370 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
372 # Define all listing files.
373 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
376 # Compiler flags to generate dependency files.
377 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
380 # Combine all necessary flags and optional flags.
381 # Add target processor to flags.
382 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
383 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
384 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
391 all: begin gccversion sizebefore build sizeafter end
393 # Change the build target to build a HEX file or a library.
394 build: elf hex eep lss sym
403 LIBNAME=lib$(TARGET).a
409 # AVR Studio 3.x does not check make's exit code but relies on
410 # the following magic strings to be generated by the compile job.
420 # Display size of file.
421 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
422 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
423 ELFSIZE = $(SIZE) $(TARGET).elf
426 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
427 2>/dev/null; echo; fi
430 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
431 2>/dev/null; echo; fi
435 # Display compiler version information.
441 # Program the device.
442 program: $(TARGET).hex $(TARGET).eep
443 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
446 # Generate avr-gdb config/init file which does the following:
447 # define the reset signal, load the target file, connect to target, and set
448 # a breakpoint at main().
450 @$(REMOVE) $(GDBINIT_FILE)
451 @echo define reset >> $(GDBINIT_FILE)
452 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
453 @echo end >> $(GDBINIT_FILE)
454 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
455 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
456 ifeq ($(DEBUG_BACKEND),simulavr)
457 @echo load >> $(GDBINIT_FILE)
459 @echo break main >> $(GDBINIT_FILE)
461 debug: gdb-config $(TARGET).elf
462 ifeq ($(DEBUG_BACKEND), avarice)
463 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
464 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
465 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
466 @$(WINSHELL) /c pause
469 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
470 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
472 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
477 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
478 COFFCONVERT = $(OBJCOPY) --debugging
479 COFFCONVERT += --change-section-address .data-0x800000
480 COFFCONVERT += --change-section-address .bss-0x800000
481 COFFCONVERT += --change-section-address .noinit-0x800000
482 COFFCONVERT += --change-section-address .eeprom-0x810000
488 @echo $(MSG_COFF) $(TARGET).cof
489 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
492 extcoff: $(TARGET).elf
494 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
495 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
499 # Create final output files (.hex, .eep) from ELF output file.
502 @echo $(MSG_FLASH) $@
503 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
507 @echo $(MSG_EEPROM) $@
508 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
509 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
511 # Create extended listing file from ELF output file.
514 @echo $(MSG_EXTENDED_LISTING) $@
515 $(OBJDUMP) -h -S -z $< > $@
517 # Create a symbol table from ELF output file.
520 @echo $(MSG_SYMBOL_TABLE) $@
525 # Create library from object files.
526 .SECONDARY : $(TARGET).a
530 @echo $(MSG_CREATING_LIBRARY) $@
534 # Link: create ELF output file from object files.
535 .SECONDARY : $(TARGET).elf
539 @echo $(MSG_LINKING) $@
540 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
543 # Compile: create object files from C source files.
546 @echo $(MSG_COMPILING) $<
547 $(CC) -c $(ALL_CFLAGS) $< -o $@
550 # Compile: create object files from C++ source files.
551 $(OBJDIR)/%.o : %.cpp
553 @echo $(MSG_COMPILING_CPP) $<
554 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
557 # Compile: create assembler files from C source files.
559 $(CC) -S $(ALL_CFLAGS) $< -o $@
562 # Compile: create assembler files from C++ source files.
564 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
567 # Assemble: create object files from assembler source files.
570 @echo $(MSG_ASSEMBLING) $<
571 $(CC) -c $(ALL_ASFLAGS) $< -o $@
574 # Create preprocessed source for use in sending a bug report.
576 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
579 # Target: clean project.
580 clean: begin clean_list end
584 @echo $(MSG_CLEANING)
585 $(REMOVE) $(TARGET).hex
586 $(REMOVE) $(TARGET).eep
587 $(REMOVE) $(TARGET).cof
588 $(REMOVE) $(TARGET).elf
589 $(REMOVE) $(TARGET).map
590 $(REMOVE) $(TARGET).sym
591 $(REMOVE) $(TARGET).lss
592 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
593 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
594 $(REMOVE) $(SRC:.c=.s)
595 $(REMOVE) $(SRC:.c=.d)
596 $(REMOVE) $(SRC:.c=.i)
600 # Create object files directory
601 $(shell mkdir $(OBJDIR) 2>/dev/null)
604 # Include the dependency files.
605 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
608 # Listing of phony targets.
609 .PHONY : all begin finish end sizebefore sizeafter gccversion \
610 build elf hex eep lss sym coff extcoff \
611 clean clean_list program debug gdb-config