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.
29 # Please customize your programmer settings(PROGRAM_CMD)
31 # make teensy = Download the hex file to the device, using teensy_loader_cli.
32 # (must have teensy_loader_cli installed).
34 # make dfu = Download the hex file to the device, using dfu-programmer (must
35 # have dfu-programmer installed).
37 # make flip = Download the hex file to the device, using Atmel FLIP (must
38 # have Atmel FLIP installed).
40 # make dfu-ee = Download the eeprom file to the device, using dfu-programmer
41 # (must have dfu-programmer installed).
43 # make flip-ee = Download the eeprom file to the device, using Atmel FLIP
44 # (must have Atmel FLIP installed).
46 # make debug = Start either simulavr or avarice as specified for debugging,
47 # with avr-gdb or avr-insight as the front end for debugging.
49 # make filename.s = Just compile filename.c into the assembler code only.
51 # make filename.i = Create a preprocessed source file for use in submitting
52 # bug reports to the GCC project.
54 # To rebuild project do "make clean" then "make all".
55 #----------------------------------------------------------------------------
58 # Output format. (can be srec, ihex, binary)
62 # Object files directory
63 # To put object files in current directory, use a dot (.), do NOT make
64 # this an empty or blank macro!
65 OBJDIR = obj_$(TARGET)
68 # Optimization level, can be [0, 1, 2, 3, s].
69 # 0 = turn off optimization. s = optimize for size.
70 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
75 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
76 # AVR Studio 4.10 requires dwarf-2.
77 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
81 # List any extra directories to look for include files here.
82 # Each directory must be seperated by a space.
83 # Use forward slashes for directory separators.
84 # For a directory that has spaces, enclose it in quotes.
85 EXTRAINCDIRS = $(subst :, ,$(VPATH))
88 # Compiler flag to set the C Standard level.
90 # gnu89 = c89 plus GCC extensions
91 # c99 = ISO C99 standard (not yet fully implemented)
92 # gnu99 = c99 plus GCC extensions
93 CSTANDARD = -std=gnu99
96 # Place -D or -U options here for C sources
97 CDEFS = -DF_CPU=$(F_CPU)UL
101 # Place -D or -U options here for ASM sources
102 ADEFS = -DF_CPU=$(F_CPU)
106 # Place -D or -U options here for C++ sources
107 CPPDEFS = -DF_CPU=$(F_CPU)UL
108 #CPPDEFS += -D__STDC_LIMIT_MACROS
109 #CPPDEFS += -D__STDC_CONSTANT_MACROS
110 CPPDEFS += $(OPT_DEFS)
114 #---------------- Compiler Options C ----------------
115 # -g*: generate debugging information
116 # -O*: optimization level
117 # -f...: tuning, see GCC manual and avr-libc documentation
118 # -Wall...: warning level
119 # -Wa,...: tell GCC to pass this to the assembler.
120 # -adhlns...: create assembler listing
124 CFLAGS += -funsigned-char
125 CFLAGS += -funsigned-bitfields
126 CFLAGS += -ffunction-sections
127 CFLAGS += -fdata-sections
128 CFLAGS += -fno-inline-small-functions
129 CFLAGS += -fpack-struct
130 CFLAGS += -fshort-enums
131 CFLAGS += -fno-strict-aliasing
133 CFLAGS += -Wstrict-prototypes
134 #CFLAGS += -mshort-calls
135 #CFLAGS += -fno-unit-at-a-time
137 #CFLAGS += -Wunreachable-code
138 #CFLAGS += -Wsign-compare
139 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
140 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
141 CFLAGS += $(CSTANDARD)
143 CFLAGS += -include $(CONFIG_H)
146 CFLAGS += -include $(CONFIG_USER_H)
150 #---------------- Compiler Options C++ ----------------
151 # -g*: generate debugging information
152 # -O*: optimization level
153 # -f...: tuning, see GCC manual and avr-libc documentation
154 # -Wall...: warning level
155 # -Wa,...: tell GCC to pass this to the assembler.
156 # -adhlns...: create assembler listing
157 CPPFLAGS = -g$(DEBUG)
158 CPPFLAGS += $(CPPDEFS)
160 CPPFLAGS += -funsigned-char
161 CPPFLAGS += -funsigned-bitfields
162 CPPFLAGS += -fpack-struct
163 CPPFLAGS += -fshort-enums
164 CPPFLAGS += -fno-exceptions
165 CPPFLAGS += -ffunction-sections
166 CPPFLAGS += -fdata-sections
167 # to supress "warning: only initialized variables can be placed into program memory area"
171 #CPPFLAGS += -mshort-calls
172 #CPPFLAGS += -fno-unit-at-a-time
173 #CPPFLAGS += -Wstrict-prototypes
174 #CPPFLAGS += -Wunreachable-code
175 #CPPFLAGS += -Wsign-compare
176 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
177 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
178 #CPPFLAGS += $(CSTANDARD)
180 CPPFLAGS += -include $(CONFIG_H)
183 CPPFLAGS += -include $(CONFIG_USER_H)
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=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
197 ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
199 ASFLAGS += -include $(CONFIG_H)
202 ASFLAGS += -include $(CONFIG_USER_H)
206 #---------------- Library Options ----------------
207 # Minimalistic printf version
208 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
210 # Floating point printf version (requires MATH_LIB = -lm below)
211 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
213 # If this is left blank, then it will use the Standard printf version.
215 #PRINTF_LIB = $(PRINTF_LIB_MIN)
216 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
219 # Minimalistic scanf version
220 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
222 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
223 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
225 # If this is left blank, then it will use the Standard scanf version.
227 #SCANF_LIB = $(SCANF_LIB_MIN)
228 #SCANF_LIB = $(SCANF_LIB_FLOAT)
234 # List any extra directories to look for libraries here.
235 # Each directory must be seperated by a space.
236 # Use forward slashes for directory separators.
237 # For a directory that has spaces, enclose it in quotes.
242 #---------------- External Memory Options ----------------
244 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
245 # used for variables (.data/.bss) and heap (malloc()).
246 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
248 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
249 # only used for heap (malloc()).
250 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
256 #---------------- Linker Options ----------------
257 # -Wl,...: tell GCC to pass this to linker.
258 # -Map: create map file
259 # --cref: add cross reference to map file
261 # Comennt out "--relax" option to avoid a error such:
262 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
264 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
265 #LDFLAGS += -Wl,--relax
266 LDFLAGS += -Wl,--gc-sections
267 LDFLAGS += $(EXTMEMOPTS)
268 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
269 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
270 #LDFLAGS += -T linker_script.x
271 # You can give EXTRALDFLAGS at 'make' command line.
272 LDFLAGS += $(EXTRALDFLAGS)
276 #---------------- Debugging Options ----------------
278 # For simulavr only - target MCU frequency.
279 DEBUG_MFREQ = $(F_CPU)
281 # Set the DEBUG_UI to either gdb or insight.
285 # Set the debugging back-end to either avarice, simulavr.
286 DEBUG_BACKEND = avarice
287 #DEBUG_BACKEND = simulavr
290 GDBINIT_FILE = __avr_gdbinit
292 # When using avarice settings for the JTAG
295 # Debugging port used to communicate between GDB / avarice / simulavr.
298 # Debugging host used to communicate between GDB / avarice / simulavr, normally
299 # just set to localhost unless doing some sort of crazy debugging when
300 # avarice is running on a different computer.
301 DEBUG_HOST = localhost
305 #============================================================================
308 # Define programs and commands.
311 OBJCOPY = avr-objcopy
312 OBJDUMP = avr-objdump
324 MSG_ERRORS_NONE = Errors: none
325 MSG_BEGIN = -------- begin --------
326 MSG_END = -------- end --------
327 MSG_SIZE_BEFORE = Size before:
328 MSG_SIZE_AFTER = Size after:
329 MSG_COFF = Converting to AVR COFF:
330 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
331 MSG_FLASH = Creating load file for Flash:
332 MSG_EEPROM = Creating load file for EEPROM:
333 MSG_EXTENDED_LISTING = Creating Extended Listing:
334 MSG_SYMBOL_TABLE = Creating Symbol Table:
335 MSG_LINKING = Linking:
336 MSG_COMPILING = Compiling C:
337 MSG_COMPILING_CPP = Compiling C++:
338 MSG_ASSEMBLING = Assembling:
339 MSG_CLEANING = Cleaning project:
340 MSG_CREATING_LIBRARY = Creating library:
345 # Define all object files.
346 OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
348 # Define all listing files.
349 LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
352 # Compiler flags to generate dependency files.
353 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
354 GENDEPFLAGS = -MMD -MP -MF .dep/$(subst /,_,$@).d
357 # Combine all necessary flags and optional flags.
358 # Add target processor to flags.
359 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
360 ALL_CFLAGS = -mmcu=$(MCU) $(CFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
361 ALL_CPPFLAGS = -mmcu=$(MCU) -x c++ $(CPPFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
362 ALL_ASFLAGS = -mmcu=$(MCU) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
377 # Change the build target to build a HEX file or a library.
378 build: elf hex eep lss sym
387 LIBNAME=lib$(TARGET).a
393 # AVR Studio 3.x does not check make's exit code but relies on
394 # the following magic strings to be generated by the compile job.
402 # Display size of file.
403 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
404 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
405 ELFSIZE = $(SIZE) $(TARGET).elf
408 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
409 2>/dev/null; echo; fi
412 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
413 2>/dev/null; echo; fi
417 # Display compiler version information.
423 # Program the device.
424 program: $(TARGET).hex $(TARGET).eep
427 teensy: $(TARGET).hex
428 teensy_loader_cli -mmcu=$(MCU) -w -v $(TARGET).hex
431 batchisp -hardware usb -device $(MCU) -operation erase f
432 batchisp -hardware usb -device $(MCU) -operation loadbuffer $(TARGET).hex program
433 batchisp -hardware usb -device $(MCU) -operation start reset 0
435 dfu: $(TARGET).hex sizeafter
436 ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
437 dfu-programmer $(MCU) erase --force
439 dfu-programmer $(MCU) erase
441 dfu-programmer $(MCU) erase
442 dfu-programmer $(MCU) flash $(TARGET).hex
443 dfu-programmer $(MCU) reset
446 dfu-programmer $(MCU) reset
447 dfu-programmer $(MCU) start
449 flip-ee: $(TARGET).hex $(TARGET).eep
450 $(COPY) $(TARGET).eep $(TARGET)eep.hex
451 batchisp -hardware usb -device $(MCU) -operation memory EEPROM erase
452 batchisp -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(TARGET)eep.hex program
453 batchisp -hardware usb -device $(MCU) -operation start reset 0
454 $(REMOVE) $(TARGET)eep.hex
456 dfu-ee: $(TARGET).hex $(TARGET).eep
457 ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
458 dfu-programmer $(MCU) flash --eeprom $(TARGET).eep
460 dfu-programmer $(MCU) flash-eeprom $(TARGET).eep
462 dfu-programmer $(MCU) reset
465 # Generate avr-gdb config/init file which does the following:
466 # define the reset signal, load the target file, connect to target, and set
467 # a breakpoint at main().
469 @$(REMOVE) $(GDBINIT_FILE)
470 @echo define reset >> $(GDBINIT_FILE)
471 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
472 @echo end >> $(GDBINIT_FILE)
473 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
474 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
475 ifeq ($(DEBUG_BACKEND),simulavr)
476 @echo load >> $(GDBINIT_FILE)
478 @echo break main >> $(GDBINIT_FILE)
480 debug: gdb-config $(TARGET).elf
481 ifeq ($(DEBUG_BACKEND), avarice)
482 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
483 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
484 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
485 @$(WINSHELL) /c pause
488 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
489 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
491 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
496 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
497 COFFCONVERT = $(OBJCOPY) --debugging
498 COFFCONVERT += --change-section-address .data-0x800000
499 COFFCONVERT += --change-section-address .bss-0x800000
500 COFFCONVERT += --change-section-address .noinit-0x800000
501 COFFCONVERT += --change-section-address .eeprom-0x810000
506 @echo $(MSG_COFF) $(TARGET).cof
507 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
510 extcoff: $(TARGET).elf
511 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
512 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
516 # Create final output files (.hex, .eep) from ELF output file.
518 @echo $(MSG_FLASH) $@
519 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
522 @echo $(MSG_EEPROM) $@
523 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
524 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
526 # Create extended listing file from ELF output file.
528 @echo $(MSG_EXTENDED_LISTING) $@
529 $(OBJDUMP) -h -S -z $< > $@
531 # Create a symbol table from ELF output file.
533 @echo $(MSG_SYMBOL_TABLE) $@
538 # Create library from object files.
539 .SECONDARY : $(TARGET).a
542 @echo $(MSG_CREATING_LIBRARY) $@
546 # Link: create ELF output file from object files.
547 .SECONDARY : $(TARGET).elf
550 @echo $(MSG_LINKING) $@
551 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
554 # Compile: create object files from C source files.
557 @echo $(MSG_COMPILING) $<
558 $(CC) -c $(ALL_CFLAGS) $< -o $@
561 # Compile: create object files from C++ source files.
562 $(OBJDIR)/%.o : %.cpp
564 @echo $(MSG_COMPILING_CPP) $<
565 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
568 # Compile: create assembler files from C source files.
570 $(CC) -S $(ALL_CFLAGS) $< -o $@
573 # Compile: create assembler files from C++ source files.
575 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
578 # Assemble: create object files from assembler source files.
581 @echo $(MSG_ASSEMBLING) $<
582 $(CC) -c $(ALL_ASFLAGS) $< -o $@
585 # Create preprocessed source for use in sending a bug report.
587 $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
590 # Target: clean project.
591 clean: begin clean_list end
594 $(REMOVE) $(TARGET).hex
595 $(REMOVE) $(TARGET).eep
596 $(REMOVE) $(TARGET).cof
597 $(REMOVE) $(TARGET).elf
598 $(REMOVE) $(TARGET).map
599 $(REMOVE) $(TARGET).sym
600 $(REMOVE) $(TARGET).lss
603 $(REMOVE) $(OBJ:.o=.s)
604 $(REMOVE) $(OBJ:.o=.i)
606 $(REMOVE) -r $(OBJDIR)
613 # Create object files directory
614 $(shell mkdir $(OBJDIR) 2>/dev/null)
617 # Include the dependency files.
618 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
621 # Listing of phony targets.
622 .PHONY : all begin finish end sizebefore sizeafter gccversion \
623 build elf hex eep lss sym coff extcoff \
624 clean clean_list debug gdb-config show_path \
625 program teensy dfu flip dfu-ee flip-ee dfu-start