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!
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)
124 CFLAGS += -include config.h
127 #---------------- Compiler Options C++ ----------------
128 # -g*: generate debugging information
129 # -O*: optimization level
130 # -f...: tuning, see GCC manual and avr-libc documentation
131 # -Wall...: warning level
132 # -Wa,...: tell GCC to pass this to the assembler.
133 # -adhlns...: create assembler listing
134 CPPFLAGS = -g$(DEBUG)
135 CPPFLAGS += $(CPPDEFS)
137 CPPFLAGS += -funsigned-char
138 CPPFLAGS += -funsigned-bitfields
139 CPPFLAGS += -fpack-struct
140 CPPFLAGS += -fshort-enums
141 CPPFLAGS += -fno-exceptions
144 #CPPFLAGS += -mshort-calls
145 #CPPFLAGS += -fno-unit-at-a-time
146 #CPPFLAGS += -Wstrict-prototypes
147 #CPPFLAGS += -Wunreachable-code
148 #CPPFLAGS += -Wsign-compare
149 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
150 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
151 #CPPFLAGS += $(CSTANDARD)
152 CPPFLAGS += -include config.h
155 #---------------- Assembler Options ----------------
156 # -Wa,...: tell GCC to pass this to the assembler.
157 # -adhlns: create listing
158 # -gstabs: have the assembler create line number information; note that
159 # for use in COFF files, additional information about filenames
160 # and function names needs to be present in the assembler source
161 # files -- see avr-libc docs [FIXME: not yet described there]
162 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
163 # dump that will be displayed for a given single line of source input.
164 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
165 ASFLAGS += -include config.h
168 #---------------- Library Options ----------------
169 # Minimalistic printf version
170 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
172 # Floating point printf version (requires MATH_LIB = -lm below)
173 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
175 # If this is left blank, then it will use the Standard printf version.
177 #PRINTF_LIB = $(PRINTF_LIB_MIN)
178 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
181 # Minimalistic scanf version
182 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
184 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
185 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
187 # If this is left blank, then it will use the Standard scanf version.
189 #SCANF_LIB = $(SCANF_LIB_MIN)
190 #SCANF_LIB = $(SCANF_LIB_FLOAT)
196 # List any extra directories to look for libraries here.
197 # Each directory must be seperated by a space.
198 # Use forward slashes for directory separators.
199 # For a directory that has spaces, enclose it in quotes.
204 #---------------- External Memory Options ----------------
206 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
207 # used for variables (.data/.bss) and heap (malloc()).
208 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
210 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
211 # only used for heap (malloc()).
212 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
218 #---------------- Linker Options ----------------
219 # -Wl,...: tell GCC to pass this to linker.
220 # -Map: create map file
221 # --cref: add cross reference to map file
223 # Comennt out "--relax" option to avoid a error such:
224 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
226 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
227 LDFLAGS += -Wl,--relax
228 LDFLAGS += -Wl,--gc-sections
229 LDFLAGS += $(EXTMEMOPTS)
230 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
231 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
232 #LDFLAGS += -T linker_script.x
236 #---------------- Programming Options (avrdude) ----------------
238 # Programming hardware
240 # to get a full listing.
242 AVRDUDE_PROGRAMMER = stk500v2
244 # com1 = serial port. Use lpt1 to connect to parallel port.
245 AVRDUDE_PORT = com1 # programmer connected to serial device
247 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
248 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
251 # Uncomment the following if you want avrdude's erase cycle counter.
252 # Note that this counter needs to be initialized first using -Yn,
253 # see avrdude manual.
254 #AVRDUDE_ERASE_COUNTER = -y
256 # Uncomment the following if you do /not/ wish a verification to be
257 # performed after programming the device.
258 #AVRDUDE_NO_VERIFY = -V
260 # Increase verbosity level. Please use this when submitting bug
261 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
262 # to submit bug reports.
263 #AVRDUDE_VERBOSE = -v -v
265 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
266 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
267 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
268 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
272 #---------------- Debugging Options ----------------
274 # For simulavr only - target MCU frequency.
275 DEBUG_MFREQ = $(F_CPU)
277 # Set the DEBUG_UI to either gdb or insight.
281 # Set the debugging back-end to either avarice, simulavr.
282 DEBUG_BACKEND = avarice
283 #DEBUG_BACKEND = simulavr
286 GDBINIT_FILE = __avr_gdbinit
288 # When using avarice settings for the JTAG
291 # Debugging port used to communicate between GDB / avarice / simulavr.
294 # Debugging host used to communicate between GDB / avarice / simulavr, normally
295 # just set to localhost unless doing some sort of crazy debugging when
296 # avarice is running on a different computer.
297 DEBUG_HOST = localhost
301 #============================================================================
304 # Define programs and commands.
307 OBJCOPY = avr-objcopy
308 OBJDUMP = avr-objdump
321 MSG_ERRORS_NONE = Errors: none
322 MSG_BEGIN = -------- begin --------
323 MSG_END = -------- end --------
324 MSG_SIZE_BEFORE = Size before:
325 MSG_SIZE_AFTER = Size after:
326 MSG_COFF = Converting to AVR COFF:
327 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
328 MSG_FLASH = Creating load file for Flash:
329 MSG_EEPROM = Creating load file for EEPROM:
330 MSG_EXTENDED_LISTING = Creating Extended Listing:
331 MSG_SYMBOL_TABLE = Creating Symbol Table:
332 MSG_LINKING = Linking:
333 MSG_COMPILING = Compiling C:
334 MSG_COMPILING_CPP = Compiling C++:
335 MSG_ASSEMBLING = Assembling:
336 MSG_CLEANING = Cleaning project:
337 MSG_CREATING_LIBRARY = Creating library:
342 # Define all object files.
343 OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
345 # Define all listing files.
346 LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
349 # Compiler flags to generate dependency files.
350 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
353 # Combine all necessary flags and optional flags.
354 # Add target processor to flags.
355 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
356 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
357 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
364 all: begin gccversion sizebefore build sizeafter end
366 # Change the build target to build a HEX file or a library.
367 build: elf hex eep lss sym
376 LIBNAME=lib$(TARGET).a
382 # AVR Studio 3.x does not check make's exit code but relies on
383 # the following magic strings to be generated by the compile job.
393 # Display size of file.
394 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
395 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
396 ELFSIZE = $(SIZE) $(TARGET).elf
399 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
400 2>/dev/null; echo; fi
403 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
404 2>/dev/null; echo; fi
408 # Display compiler version information.
414 # Program the device.
415 program: $(TARGET).hex $(TARGET).eep
416 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
419 # Generate avr-gdb config/init file which does the following:
420 # define the reset signal, load the target file, connect to target, and set
421 # a breakpoint at main().
423 @$(REMOVE) $(GDBINIT_FILE)
424 @echo define reset >> $(GDBINIT_FILE)
425 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
426 @echo end >> $(GDBINIT_FILE)
427 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
428 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
429 ifeq ($(DEBUG_BACKEND),simulavr)
430 @echo load >> $(GDBINIT_FILE)
432 @echo break main >> $(GDBINIT_FILE)
434 debug: gdb-config $(TARGET).elf
435 ifeq ($(DEBUG_BACKEND), avarice)
436 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
437 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
438 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
439 @$(WINSHELL) /c pause
442 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
443 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
445 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
450 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
451 COFFCONVERT = $(OBJCOPY) --debugging
452 COFFCONVERT += --change-section-address .data-0x800000
453 COFFCONVERT += --change-section-address .bss-0x800000
454 COFFCONVERT += --change-section-address .noinit-0x800000
455 COFFCONVERT += --change-section-address .eeprom-0x810000
461 @echo $(MSG_COFF) $(TARGET).cof
462 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
465 extcoff: $(TARGET).elf
467 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
468 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
472 # Create final output files (.hex, .eep) from ELF output file.
475 @echo $(MSG_FLASH) $@
476 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
480 @echo $(MSG_EEPROM) $@
481 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
482 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
484 # Create extended listing file from ELF output file.
487 @echo $(MSG_EXTENDED_LISTING) $@
488 $(OBJDUMP) -h -S -z $< > $@
490 # Create a symbol table from ELF output file.
493 @echo $(MSG_SYMBOL_TABLE) $@
498 # Create library from object files.
499 .SECONDARY : $(TARGET).a
503 @echo $(MSG_CREATING_LIBRARY) $@
507 # Link: create ELF output file from object files.
508 .SECONDARY : $(TARGET).elf
512 @echo $(MSG_LINKING) $@
513 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
516 # Compile: create object files from C source files.
519 @echo $(MSG_COMPILING) $<
520 $(CC) -c $(ALL_CFLAGS) $< -o $@
523 # Compile: create object files from C++ source files.
524 $(OBJDIR)/%.o : %.cpp
526 @echo $(MSG_COMPILING_CPP) $<
527 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
530 # Compile: create assembler files from C source files.
532 $(CC) -S $(ALL_CFLAGS) $< -o $@
535 # Compile: create assembler files from C++ source files.
537 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
540 # Assemble: create object files from assembler source files.
543 @echo $(MSG_ASSEMBLING) $<
544 $(CC) -c $(ALL_ASFLAGS) $< -o $@
547 # Create preprocessed source for use in sending a bug report.
549 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
552 # Target: clean project.
553 clean: begin clean_list end
557 $(REMOVE) $(TARGET).hex
558 $(REMOVE) $(TARGET).eep
559 $(REMOVE) $(TARGET).cof
560 $(REMOVE) $(TARGET).elf
561 $(REMOVE) $(TARGET).map
562 $(REMOVE) $(TARGET).sym
563 $(REMOVE) $(TARGET).lss
566 $(REMOVE) $(OBJ:.o=.s)
567 $(REMOVE) $(OBJ:.o=.i)
569 $(REMOVEDIR) $(OBJDIR)
572 # Create object files directory
573 $(shell mkdir $(OBJDIR) 2>/dev/null)
576 # Include the dependency files.
577 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
580 # Listing of phony targets.
581 .PHONY : all begin finish end sizebefore sizeafter gccversion \
582 build elf hex eep lss sym coff extcoff \
583 clean clean_list program debug gdb-config