1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jg 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 #----------------------------------------------------------------------------
43 # Output format. (can be srec, ihex, binary)
47 # Object files directory
48 # To put object files in current directory, use a dot (.), do NOT make
49 # this an empty or blank macro!
50 OBJDIR = obj_$(TARGET)
53 # Optimization level, can be [0, 1, 2, 3, s].
54 # 0 = turn off optimization. s = optimize for size.
55 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
60 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
61 # AVR Studio 4.10 requires dwarf-2.
62 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
66 # List any extra directories to look for include files here.
67 # Each directory must be seperated by a space.
68 # Use forward slashes for directory separators.
69 # For a directory that has spaces, enclose it in quotes.
70 EXTRAINCDIRS = $(subst :, ,$(VPATH))
73 # Compiler flag to set the C Standard level.
75 # gnu89 = c89 plus GCC extensions
76 # c99 = ISO C99 standard (not yet fully implemented)
77 # gnu99 = c99 plus GCC extensions
78 CSTANDARD = -std=gnu99
81 # Place -D or -U options here for C sources
82 CDEFS = -DF_CPU=$(F_CPU)UL
86 # Place -D or -U options here for ASM sources
87 ADEFS = -DF_CPU=$(F_CPU)
91 # Place -D or -U options here for C++ sources
92 CPPDEFS = -DF_CPU=$(F_CPU)UL
93 #CPPDEFS += -D__STDC_LIMIT_MACROS
94 #CPPDEFS += -D__STDC_CONSTANT_MACROS
95 CPPDEFS += $(OPT_DEFS)
99 #---------------- Compiler Options C ----------------
100 # -g*: generate debugging information
101 # -O*: optimization level
102 # -f...: tuning, see GCC manual and avr-libc documentation
103 # -Wall...: warning level
104 # -Wa,...: tell GCC to pass this to the assembler.
105 # -adhlns...: create assembler listing
109 CFLAGS += -funsigned-char
110 CFLAGS += -funsigned-bitfields
111 CFLAGS += -ffunction-sections
112 CFLAGS += -fpack-struct
113 CFLAGS += -fshort-enums
115 CFLAGS += -Wstrict-prototypes
116 #CFLAGS += -mshort-calls
117 #CFLAGS += -fno-unit-at-a-time
119 #CFLAGS += -Wunreachable-code
120 #CFLAGS += -Wsign-compare
121 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
122 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
123 CFLAGS += $(CSTANDARD)
125 CFLAGS += -include $(CONFIG_H)
129 #---------------- Compiler Options C++ ----------------
130 # -g*: generate debugging information
131 # -O*: optimization level
132 # -f...: tuning, see GCC manual and avr-libc documentation
133 # -Wall...: warning level
134 # -Wa,...: tell GCC to pass this to the assembler.
135 # -adhlns...: create assembler listing
136 CPPFLAGS = -g$(DEBUG)
137 CPPFLAGS += $(CPPDEFS)
139 CPPFLAGS += -funsigned-char
140 CPPFLAGS += -funsigned-bitfields
141 CPPFLAGS += -fpack-struct
142 CPPFLAGS += -fshort-enums
143 CPPFLAGS += -fno-exceptions
146 #CPPFLAGS += -mshort-calls
147 #CPPFLAGS += -fno-unit-at-a-time
148 #CPPFLAGS += -Wstrict-prototypes
149 #CPPFLAGS += -Wunreachable-code
150 #CPPFLAGS += -Wsign-compare
151 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
152 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
153 #CPPFLAGS += $(CSTANDARD)
155 CPPFLAGS += -include $(CONFIG_H)
159 #---------------- Assembler Options ----------------
160 # -Wa,...: tell GCC to pass this to the assembler.
161 # -adhlns: create listing
162 # -gstabs: have the assembler create line number information; note that
163 # for use in COFF files, additional information about filenames
164 # and function names needs to be present in the assembler source
165 # files -- see avr-libc docs [FIXME: not yet described there]
166 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
167 # dump that will be displayed for a given single line of source input.
168 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
170 ASFLAGS += -include $(CONFIG_H)
174 #---------------- Library Options ----------------
175 # Minimalistic printf version
176 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
178 # Floating point printf version (requires MATH_LIB = -lm below)
179 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
181 # If this is left blank, then it will use the Standard printf version.
183 #PRINTF_LIB = $(PRINTF_LIB_MIN)
184 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
187 # Minimalistic scanf version
188 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
190 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
191 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
193 # If this is left blank, then it will use the Standard scanf version.
195 #SCANF_LIB = $(SCANF_LIB_MIN)
196 #SCANF_LIB = $(SCANF_LIB_FLOAT)
202 # List any extra directories to look for libraries here.
203 # Each directory must be seperated by a space.
204 # Use forward slashes for directory separators.
205 # For a directory that has spaces, enclose it in quotes.
210 #---------------- External Memory Options ----------------
212 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
213 # used for variables (.data/.bss) and heap (malloc()).
214 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
216 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
217 # only used for heap (malloc()).
218 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
224 #---------------- Linker Options ----------------
225 # -Wl,...: tell GCC to pass this to linker.
226 # -Map: create map file
227 # --cref: add cross reference to map file
229 # Comennt out "--relax" option to avoid a error such:
230 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
232 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
233 #LDFLAGS += -Wl,--relax
234 LDFLAGS += -Wl,--gc-sections
235 LDFLAGS += $(EXTMEMOPTS)
236 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
237 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
238 #LDFLAGS += -T linker_script.x
239 # You can give EXTRALDFLAGS at 'make' command line.
240 LDFLAGS += $(EXTRALDFLAGS)
244 #---------------- Debugging Options ----------------
246 # For simulavr only - target MCU frequency.
247 DEBUG_MFREQ = $(F_CPU)
249 # Set the DEBUG_UI to either gdb or insight.
253 # Set the debugging back-end to either avarice, simulavr.
254 DEBUG_BACKEND = avarice
255 #DEBUG_BACKEND = simulavr
258 GDBINIT_FILE = __avr_gdbinit
260 # When using avarice settings for the JTAG
263 # Debugging port used to communicate between GDB / avarice / simulavr.
266 # Debugging host used to communicate between GDB / avarice / simulavr, normally
267 # just set to localhost unless doing some sort of crazy debugging when
268 # avarice is running on a different computer.
269 DEBUG_HOST = localhost
273 #============================================================================
276 # Define programs and commands.
279 OBJCOPY = avr-objcopy
280 OBJDUMP = avr-objdump
292 MSG_ERRORS_NONE = Errors: none
293 MSG_BEGIN = -------- begin --------
294 MSG_END = -------- end --------
295 MSG_SIZE_BEFORE = Size before:
296 MSG_SIZE_AFTER = Size after:
297 MSG_COFF = Converting to AVR COFF:
298 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
299 MSG_FLASH = Creating load file for Flash:
300 MSG_EEPROM = Creating load file for EEPROM:
301 MSG_EXTENDED_LISTING = Creating Extended Listing:
302 MSG_SYMBOL_TABLE = Creating Symbol Table:
303 MSG_LINKING = Linking:
304 MSG_COMPILING = Compiling C:
305 MSG_COMPILING_CPP = Compiling C++:
306 MSG_ASSEMBLING = Assembling:
307 MSG_CLEANING = Cleaning project:
308 MSG_CREATING_LIBRARY = Creating library:
313 # Define all object files.
314 OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
316 # Define all listing files.
317 LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
320 # Compiler flags to generate dependency files.
321 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
324 # Combine all necessary flags and optional flags.
325 # Add target processor to flags.
326 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
327 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
328 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
329 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
336 all: begin gccversion sizebefore build sizeafter end
338 # Change the build target to build a HEX file or a library.
339 build: elf hex eep lss sym
348 LIBNAME=lib$(TARGET).a
354 # AVR Studio 3.x does not check make's exit code but relies on
355 # the following magic strings to be generated by the compile job.
365 # Display size of file.
366 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
367 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
368 ELFSIZE = $(SIZE) $(TARGET).elf
371 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
372 2>/dev/null; echo; fi
375 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
376 2>/dev/null; echo; fi
380 # Display compiler version information.
386 # Program the device.
387 program: $(TARGET).hex $(TARGET).eep
391 # Generate avr-gdb config/init file which does the following:
392 # define the reset signal, load the target file, connect to target, and set
393 # a breakpoint at main().
395 @$(REMOVE) $(GDBINIT_FILE)
396 @echo define reset >> $(GDBINIT_FILE)
397 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
398 @echo end >> $(GDBINIT_FILE)
399 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
400 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
401 ifeq ($(DEBUG_BACKEND),simulavr)
402 @echo load >> $(GDBINIT_FILE)
404 @echo break main >> $(GDBINIT_FILE)
406 debug: gdb-config $(TARGET).elf
407 ifeq ($(DEBUG_BACKEND), avarice)
408 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
409 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
410 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
411 @$(WINSHELL) /c pause
414 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
415 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
417 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
422 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
423 COFFCONVERT = $(OBJCOPY) --debugging
424 COFFCONVERT += --change-section-address .data-0x800000
425 COFFCONVERT += --change-section-address .bss-0x800000
426 COFFCONVERT += --change-section-address .noinit-0x800000
427 COFFCONVERT += --change-section-address .eeprom-0x810000
433 @echo $(MSG_COFF) $(TARGET).cof
434 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
437 extcoff: $(TARGET).elf
439 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
440 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
444 # Create final output files (.hex, .eep) from ELF output file.
447 @echo $(MSG_FLASH) $@
448 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
452 @echo $(MSG_EEPROM) $@
453 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
454 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
456 # Create extended listing file from ELF output file.
459 @echo $(MSG_EXTENDED_LISTING) $@
460 $(OBJDUMP) -h -S -z $< > $@
462 # Create a symbol table from ELF output file.
465 @echo $(MSG_SYMBOL_TABLE) $@
470 # Create library from object files.
471 .SECONDARY : $(TARGET).a
475 @echo $(MSG_CREATING_LIBRARY) $@
479 # Link: create ELF output file from object files.
480 .SECONDARY : $(TARGET).elf
484 @echo $(MSG_LINKING) $@
485 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
488 # Compile: create object files from C source files.
491 @echo $(MSG_COMPILING) $<
492 $(CC) -c $(ALL_CFLAGS) $< -o $@
495 # Compile: create object files from C++ source files.
496 $(OBJDIR)/%.o : %.cpp
498 @echo $(MSG_COMPILING_CPP) $<
499 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
502 # Compile: create assembler files from C source files.
504 $(CC) -S $(ALL_CFLAGS) $< -o $@
507 # Compile: create assembler files from C++ source files.
509 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
512 # Assemble: create object files from assembler source files.
515 @echo $(MSG_ASSEMBLING) $<
516 $(CC) -c $(ALL_ASFLAGS) $< -o $@
519 # Create preprocessed source for use in sending a bug report.
521 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
524 # Target: clean project.
525 clean: begin clean_list end
529 $(REMOVE) $(TARGET).hex
530 $(REMOVE) $(TARGET).eep
531 $(REMOVE) $(TARGET).cof
532 $(REMOVE) $(TARGET).elf
533 $(REMOVE) $(TARGET).map
534 $(REMOVE) $(TARGET).sym
535 $(REMOVE) $(TARGET).lss
538 $(REMOVE) $(OBJ:.o=.s)
539 $(REMOVE) $(OBJ:.o=.i)
541 $(REMOVEDIR) $(OBJDIR)
544 # Create object files directory
545 $(shell mkdir $(OBJDIR) 2>/dev/null)
548 # Include the dependency files.
549 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
552 # Listing of phony targets.
553 .PHONY : all begin finish end sizebefore sizeafter gccversion \
554 build elf hex eep lss sym coff extcoff \
555 clean clean_list program debug gdb-config