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 #----------------------------------------------------------------------------
48 # Processor frequency.
49 # This will define a symbol, F_CPU, in all source code files equal to the
50 # processor frequency. You can then use this symbol in your source code to
51 # calculate timings. Do NOT tack on a 'UL' at the end, this will be done
52 # automatically to create a 32-bit value in your source code.
69 # Output format. (can be srec, ihex, binary)
73 # Target file name (without extension).
77 # Object files directory
81 # List C source files here. (C dependencies are automatically generated.)
85 # List C++ source files here. (C dependencies are automatically generated.)
89 # List Assembler source files here.
90 # Make them always end in a capital .S. Files ending in a lowercase .s
91 # will not be considered source files but generated files (assembler
92 # output from the compiler), and will be deleted upon "make clean"!
93 # Even though the DOS/Win* filesystem matches both .s and .S the same,
94 # it will preserve the spelling of the filenames, and gcc itself does
95 # care about how the name is spelled on its command-line.
99 # Optimization level, can be [0, 1, 2, 3, s].
100 # 0 = turn off optimization. s = optimize for size.
101 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
106 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
107 # AVR Studio 4.10 requires dwarf-2.
108 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
112 # List any extra directories to look for include files here.
113 # Each directory must be seperated by a space.
114 # Use forward slashes for directory separators.
115 # For a directory that has spaces, enclose it in quotes.
119 # Compiler flag to set the C Standard level.
121 # gnu89 = c89 plus GCC extensions
122 # c99 = ISO C99 standard (not yet fully implemented)
123 # gnu99 = c99 plus GCC extensions
124 CSTANDARD = -std=gnu99
127 # Place -D or -U options here for C sources
128 CDEFS = -DF_CPU=$(F_CPU)UL
131 # Place -D or -U options here for C++ sources
132 CPPDEFS = -DF_CPU=$(F_CPU)UL
133 #CPPDEFS += -D__STDC_LIMIT_MACROS
134 #CPPDEFS += -D__STDC_CONSTANT_MACROS
138 #---------------- Compiler Options C ----------------
139 # -g*: generate debugging information
140 # -O*: optimization level
141 # -f...: tuning, see GCC manual and avr-libc documentation
142 # -Wall...: warning level
143 # -Wa,...: tell GCC to pass this to the assembler.
144 # -adhlns...: create assembler listing
149 #CFLAGS += -mshort-calls
150 CFLAGS += -funsigned-char
151 CFLAGS += -funsigned-bitfields
152 CFLAGS += -fpack-struct
153 CFLAGS += -fshort-enums
154 #CFLAGS += -fno-unit-at-a-time
156 CFLAGS += -Wstrict-prototypes
158 #CFLAGS += -Wunreachable-code
159 #CFLAGS += -Wsign-compare
160 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
161 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
162 CFLAGS += $(CSTANDARD)
165 #---------------- Compiler Options C++ ----------------
166 # -g*: generate debugging information
167 # -O*: optimization level
168 # -f...: tuning, see GCC manual and avr-libc documentation
169 # -Wall...: warning level
170 # -Wa,...: tell GCC to pass this to the assembler.
171 # -adhlns...: create assembler listing
172 CPPFLAGS = -g$(DEBUG)
173 CPPFLAGS += $(CPPDEFS)
176 #CPPFLAGS += -mshort-calls
177 CPPFLAGS += -funsigned-char
178 CPPFLAGS += -funsigned-bitfields
179 CPPFLAGS += -fpack-struct
180 CPPFLAGS += -fshort-enums
181 CPPFLAGS += -fno-exceptions
182 #CPPFLAGS += -fno-unit-at-a-time
184 #CPPFLAGS += -Wstrict-prototypes
186 #CPPFLAGS += -Wunreachable-code
187 #CPPFLAGS += -Wsign-compare
188 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
189 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
190 #CPPFLAGS += $(CSTANDARD)
193 #---------------- Assembler Options ----------------
194 # -Wa,...: tell GCC to pass this to the assembler.
195 # -ahlms: create listing
196 # -gstabs: have the assembler create line number information; note that
197 # for use in COFF files, additional information about filenames
198 # and function names needs to be present in the assembler source
199 # files -- see avr-libc docs [FIXME: not yet described there]
200 ASFLAGS = -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs
203 #---------------- Library Options ----------------
204 # Minimalistic printf version
205 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
207 # Floating point printf version (requires MATH_LIB = -lm below)
208 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
210 # If this is left blank, then it will use the Standard printf version.
212 #PRINTF_LIB = $(PRINTF_LIB_MIN)
213 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
216 # Minimalistic scanf version
217 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
219 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
220 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
222 # If this is left blank, then it will use the Standard scanf version.
224 #SCANF_LIB = $(SCANF_LIB_MIN)
225 #SCANF_LIB = $(SCANF_LIB_FLOAT)
232 #---------------- External Memory Options ----------------
234 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
235 # used for variables (.data/.bss) and heap (malloc()).
236 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
238 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
239 # only used for heap (malloc()).
240 #EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
246 #---------------- Linker Options ----------------
247 # -Wl,...: tell GCC to pass this to linker.
248 # -Map: create map file
249 # --cref: add cross reference to map file
250 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
251 LDFLAGS += $(EXTMEMOPTS)
252 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
253 #LDFLAGS += -T linker_script.x
257 #---------------- Programming Options (avrdude) ----------------
259 # Programming hardware: alf avr910 avrisp bascom bsd
260 # dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
263 # to get a full listing.
265 AVRDUDE_PROGRAMMER = pony-stk200
267 # com1 = serial port. Use lpt1 to connect to parallel port.
270 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
271 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
274 # Uncomment the following if you want avrdude's erase cycle counter.
275 # Note that this counter needs to be initialized first using -Yn,
276 # see avrdude manual.
277 #AVRDUDE_ERASE_COUNTER = -y
279 # Uncomment the following if you do /not/ wish a verification to be
280 # performed after programming the device.
281 #AVRDUDE_NO_VERIFY = -V
283 # Increase verbosity level. Please use this when submitting bug
284 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
285 # to submit bug reports.
286 #AVRDUDE_VERBOSE = -v -v
288 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
289 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
290 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
291 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
295 #---------------- Debugging Options ----------------
297 # For simulavr only - target MCU frequency.
298 DEBUG_MFREQ = $(F_CPU)
300 # Set the DEBUG_UI to either gdb or insight.
304 # Set the debugging back-end to either avarice, simulavr.
305 #DEBUG_BACKEND = avarice
306 DEBUG_BACKEND = simulavr
309 GDBINIT_FILE = __avr_gdbinit
311 # When using avarice settings for the JTAG
314 # Debugging port used to communicate between GDB / avarice / simulavr.
317 # Debugging host used to communicate between GDB / avarice / simulavr, normally
318 # just set to localhost unless doing some sort of crazy debugging when
319 # avarice is running on a different computer.
320 DEBUG_HOST = localhost
324 #============================================================================
327 # Define programs and commands.
330 OBJCOPY = avr-objcopy
331 OBJDUMP = avr-objdump
343 MSG_ERRORS_NONE = Errors: none
344 MSG_BEGIN = -------- begin --------
345 MSG_END = -------- end --------
346 MSG_SIZE_BEFORE = Size before:
347 MSG_SIZE_AFTER = Size after:
348 MSG_COFF = Converting to AVR COFF:
349 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
350 MSG_FLASH = Creating load file for Flash:
351 MSG_EEPROM = Creating load file for EEPROM:
352 MSG_EXTENDED_LISTING = Creating Extended Listing:
353 MSG_SYMBOL_TABLE = Creating Symbol Table:
354 MSG_LINKING = Linking:
355 MSG_COMPILING = Compiling C:
356 MSG_COMPILING_CPP = Compiling C++:
357 MSG_ASSEMBLING = Assembling:
358 MSG_CLEANING = Cleaning project:
359 MSG_CREATING_LIBRARY = Creating library:
364 # Define all object files.
365 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
367 # Define all listing files.
368 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
371 # Compiler flags to generate dependency files.
372 GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
375 # Combine all necessary flags and optional flags.
376 # Add target processor to flags.
377 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
378 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
379 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
386 all: begin gccversion sizebefore build sizeafter end
388 # Change the build target to build a HEX file or a library.
389 build: elf hex eep lss sym
398 LIBNAME=lib$(TARGET).a
404 # AVR Studio 3.x does not check make's exit code but relies on
405 # the following magic strings to be generated by the compile job.
416 # Display size of file.
417 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
418 ELFSIZE = $(SIZE) -A $(TARGET).elf
419 AVRMEM = avr-mem.sh $(TARGET).elf $(MCU)
422 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
423 $(AVRMEM) 2>/dev/null; echo; fi
426 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
427 $(AVRMEM) 2>/dev/null; echo; fi
431 # Display compiler version information.
437 # Program the device.
438 program: $(TARGET).hex $(TARGET).eep
439 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
440 $(AVRDUDE_WRITE_EEPROM)
443 # Generate avr-gdb config/init file which does the following:
444 # define the reset signal, load the target file, connect to target, and set
445 # a breakpoint at main().
447 @$(REMOVE) $(GDBINIT_FILE)
448 @echo define reset >> $(GDBINIT_FILE)
449 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
450 @echo end >> $(GDBINIT_FILE)
451 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
452 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
453 ifeq ($(DEBUG_BACKEND),simulavr)
454 @echo load >> $(GDBINIT_FILE)
456 @echo break main >> $(GDBINIT_FILE)
458 debug: gdb-config $(TARGET).elf
459 ifeq ($(DEBUG_BACKEND), avarice)
460 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
461 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
462 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
463 @$(WINSHELL) /c pause
466 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
467 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
469 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
474 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
475 COFFCONVERT = $(OBJCOPY) --debugging
476 COFFCONVERT += --change-section-address .data-0x800000
477 COFFCONVERT += --change-section-address .bss-0x800000
478 COFFCONVERT += --change-section-address .noinit-0x800000
479 COFFCONVERT += --change-section-address .eeprom-0x810000
485 @echo $(MSG_COFF) $(TARGET).cof
486 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
489 extcoff: $(TARGET).elf
491 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
492 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
496 # Create final output files (.hex, .eep) from ELF output file.
499 @echo $(MSG_FLASH) $@
500 $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
504 @echo $(MSG_EEPROM) $@
505 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
506 --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
508 # Create extended listing file from ELF output file.
511 @echo $(MSG_EXTENDED_LISTING) $@
512 $(OBJDUMP) -h -S $< > $@
514 # Create a symbol table from ELF output file.
517 @echo $(MSG_SYMBOL_TABLE) $@
522 # Create library from object files.
523 .SECONDARY : $(TARGET).a
527 @echo $(MSG_CREATING_LIBRARY) $@
531 # Link: create ELF output file from object files.
532 .SECONDARY : $(TARGET).elf
536 @echo $(MSG_LINKING) $@
537 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
540 # Compile: create object files from C source files.
543 @echo $(MSG_COMPILING) $<
544 $(CC) -c $(ALL_CFLAGS) $< -o $@
547 # Compile: create object files from C++ source files.
548 $(OBJDIR)/%.o : %.cpp
550 @echo $(MSG_COMPILING_CPP) $<
551 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
554 # Compile: create assembler files from C source files.
556 $(CC) -S $(ALL_CFLAGS) $< -o $@
559 # Compile: create assembler files from C++ source files.
561 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
564 # Assemble: create object files from assembler source files.
567 @echo $(MSG_ASSEMBLING) $<
568 $(CC) -c $(ALL_ASFLAGS) $< -o $@
571 # Create preprocessed source for use in sending a bug report.
573 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
576 # Target: clean project.
577 clean: begin clean_list end
581 @echo $(MSG_CLEANING)
582 $(REMOVE) $(TARGET).hex
583 $(REMOVE) $(TARGET).eep
584 $(REMOVE) $(TARGET).cof
585 $(REMOVE) $(TARGET).elf
586 $(REMOVE) $(TARGET).map
587 $(REMOVE) $(TARGET).sym
588 $(REMOVE) $(TARGET).lss
589 $(REMOVEDIR) $(OBJDIR)
590 $(REMOVE) $(SRC:.c=.s)
591 $(REMOVE) $(SRC:.c=.d)
595 # Create object files directory
596 $(shell mkdir $(OBJDIR) 2>/dev/null)
599 # Include the dependency files.
600 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
603 # Listing of phony targets.
604 .PHONY : all begin finish end sizebefore sizeafter gccversion \
605 build elf hex eep lss sym coff extcoff \
606 clean clean_list program debug gdb-config