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 += -fno-inline-small-functions
128 CFLAGS += -fpack-struct
129 CFLAGS += -fshort-enums
130 CFLAGS += -fno-strict-aliasing
132 CFLAGS += -Wstrict-prototypes
133 #CFLAGS += -mshort-calls
134 #CFLAGS += -fno-unit-at-a-time
136 #CFLAGS += -Wunreachable-code
137 #CFLAGS += -Wsign-compare
138 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
139 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
140 CFLAGS += $(CSTANDARD)
142 CFLAGS += -include $(CONFIG_H)
146 #---------------- Compiler Options C++ ----------------
147 # -g*: generate debugging information
148 # -O*: optimization level
149 # -f...: tuning, see GCC manual and avr-libc documentation
150 # -Wall...: warning level
151 # -Wa,...: tell GCC to pass this to the assembler.
152 # -adhlns...: create assembler listing
153 CPPFLAGS = -g$(DEBUG)
154 CPPFLAGS += $(CPPDEFS)
156 CPPFLAGS += -funsigned-char
157 CPPFLAGS += -funsigned-bitfields
158 CPPFLAGS += -fpack-struct
159 CPPFLAGS += -fshort-enums
160 CPPFLAGS += -fno-exceptions
161 CPPFLAGS += -ffunction-sections
162 CPPFLAGS += -fdata-sections
163 # to supress "warning: only initialized variables can be placed into program memory area"
167 #CPPFLAGS += -mshort-calls
168 #CPPFLAGS += -fno-unit-at-a-time
169 #CPPFLAGS += -Wstrict-prototypes
170 #CPPFLAGS += -Wunreachable-code
171 #CPPFLAGS += -Wsign-compare
172 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
173 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
174 #CPPFLAGS += $(CSTANDARD)
176 CPPFLAGS += -include $(CONFIG_H)
180 #---------------- Assembler Options ----------------
181 # -Wa,...: tell GCC to pass this to the assembler.
182 # -adhlns: create listing
183 # -gstabs: have the assembler create line number information; note that
184 # for use in COFF files, additional information about filenames
185 # and function names needs to be present in the assembler source
186 # files -- see avr-libc docs [FIXME: not yet described there]
187 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
188 # dump that will be displayed for a given single line of source input.
189 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
190 ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
192 ASFLAGS += -include $(CONFIG_H)
196 #---------------- Library Options ----------------
197 # Minimalistic printf version
198 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
200 # Floating point printf version (requires MATH_LIB = -lm below)
201 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
203 # If this is left blank, then it will use the Standard printf version.
205 #PRINTF_LIB = $(PRINTF_LIB_MIN)
206 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
209 # Minimalistic scanf version
210 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
212 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
213 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
215 # If this is left blank, then it will use the Standard scanf version.
217 #SCANF_LIB = $(SCANF_LIB_MIN)
218 #SCANF_LIB = $(SCANF_LIB_FLOAT)
224 # List any extra directories to look for libraries here.
225 # Each directory must be seperated by a space.
226 # Use forward slashes for directory separators.
227 # For a directory that has spaces, enclose it in quotes.
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,--section-start,.data=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
251 # Comennt out "--relax" option to avoid a error such:
252 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
254 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
255 #LDFLAGS += -Wl,--relax
256 LDFLAGS += -Wl,--gc-sections
257 LDFLAGS += $(EXTMEMOPTS)
258 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
259 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
260 #LDFLAGS += -T linker_script.x
261 # You can give EXTRALDFLAGS at 'make' command line.
262 LDFLAGS += $(EXTRALDFLAGS)
266 #---------------- Debugging Options ----------------
268 # For simulavr only - target MCU frequency.
269 DEBUG_MFREQ = $(F_CPU)
271 # Set the DEBUG_UI to either gdb or insight.
275 # Set the debugging back-end to either avarice, simulavr.
276 DEBUG_BACKEND = avarice
277 #DEBUG_BACKEND = simulavr
280 GDBINIT_FILE = __avr_gdbinit
282 # When using avarice settings for the JTAG
285 # Debugging port used to communicate between GDB / avarice / simulavr.
288 # Debugging host used to communicate between GDB / avarice / simulavr, normally
289 # just set to localhost unless doing some sort of crazy debugging when
290 # avarice is running on a different computer.
291 DEBUG_HOST = localhost
295 #============================================================================
298 # Define programs and commands.
301 OBJCOPY = avr-objcopy
302 OBJDUMP = avr-objdump
314 MSG_ERRORS_NONE = Errors: none
315 MSG_BEGIN = -------- begin --------
316 MSG_END = -------- end --------
317 MSG_SIZE_BEFORE = Size before:
318 MSG_SIZE_AFTER = Size after:
319 MSG_COFF = Converting to AVR COFF:
320 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
321 MSG_FLASH = Creating load file for Flash:
322 MSG_EEPROM = Creating load file for EEPROM:
323 MSG_EXTENDED_LISTING = Creating Extended Listing:
324 MSG_SYMBOL_TABLE = Creating Symbol Table:
325 MSG_LINKING = Linking:
326 MSG_COMPILING = Compiling C:
327 MSG_COMPILING_CPP = Compiling C++:
328 MSG_ASSEMBLING = Assembling:
329 MSG_CLEANING = Cleaning project:
330 MSG_CREATING_LIBRARY = Creating library:
335 # Define all object files.
336 OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
338 # Define all listing files.
339 LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
342 # Compiler flags to generate dependency files.
343 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
344 GENDEPFLAGS = -MMD -MP -MF .dep/$(subst /,_,$@).d
347 # Combine all necessary flags and optional flags.
348 # Add target processor to flags.
349 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
350 ALL_CFLAGS = -mmcu=$(MCU) $(CFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
351 ALL_CPPFLAGS = -mmcu=$(MCU) -x c++ $(CPPFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
352 ALL_ASFLAGS = -mmcu=$(MCU) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
359 all: begin gccversion sizebefore build sizeafter end
361 # Change the build target to build a HEX file or a library.
362 build: elf hex eep lss sym
371 LIBNAME=lib$(TARGET).a
377 # AVR Studio 3.x does not check make's exit code but relies on
378 # the following magic strings to be generated by the compile job.
388 # Display size of file.
389 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
390 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
391 ELFSIZE = $(SIZE) $(TARGET).elf
394 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
395 2>/dev/null; echo; fi
398 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
399 2>/dev/null; echo; fi
403 # Display compiler version information.
409 # Program the device.
410 program: $(TARGET).hex $(TARGET).eep
413 teensy: $(TARGET).hex
414 teensy_loader_cli -mmcu=$(MCU) -w -v $(TARGET).hex
417 batchisp -hardware usb -device $(MCU) -operation erase f
418 batchisp -hardware usb -device $(MCU) -operation loadbuffer $(TARGET).hex program
419 batchisp -hardware usb -device $(MCU) -operation start reset 0
422 dfu-programmer $(MCU) erase
423 dfu-programmer $(MCU) flash $(TARGET).hex
424 dfu-programmer $(MCU) reset
427 dfu-programmer $(MCU) reset
428 dfu-programmer $(MCU) start
430 flip-ee: $(TARGET).hex $(TARGET).eep
431 $(COPY) $(TARGET).eep $(TARGET)eep.hex
432 batchisp -hardware usb -device $(MCU) -operation memory EEPROM erase
433 batchisp -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(TARGET)eep.hex program
434 batchisp -hardware usb -device $(MCU) -operation start reset 0
435 $(REMOVE) $(TARGET)eep.hex
437 dfu-ee: $(TARGET).hex $(TARGET).eep
438 dfu-programmer $(MCU) eeprom-flash $(TARGET).eep
439 dfu-programmer $(MCU) reset
442 # Generate avr-gdb config/init file which does the following:
443 # define the reset signal, load the target file, connect to target, and set
444 # a breakpoint at main().
446 @$(REMOVE) $(GDBINIT_FILE)
447 @echo define reset >> $(GDBINIT_FILE)
448 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
449 @echo end >> $(GDBINIT_FILE)
450 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
451 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
452 ifeq ($(DEBUG_BACKEND),simulavr)
453 @echo load >> $(GDBINIT_FILE)
455 @echo break main >> $(GDBINIT_FILE)
457 debug: gdb-config $(TARGET).elf
458 ifeq ($(DEBUG_BACKEND), avarice)
459 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
460 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
461 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
462 @$(WINSHELL) /c pause
465 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
466 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
468 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
473 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
474 COFFCONVERT = $(OBJCOPY) --debugging
475 COFFCONVERT += --change-section-address .data-0x800000
476 COFFCONVERT += --change-section-address .bss-0x800000
477 COFFCONVERT += --change-section-address .noinit-0x800000
478 COFFCONVERT += --change-section-address .eeprom-0x810000
484 @echo $(MSG_COFF) $(TARGET).cof
485 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
488 extcoff: $(TARGET).elf
490 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
491 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
495 # Create final output files (.hex, .eep) from ELF output file.
498 @echo $(MSG_FLASH) $@
499 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
503 @echo $(MSG_EEPROM) $@
504 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
505 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
507 # Create extended listing file from ELF output file.
510 @echo $(MSG_EXTENDED_LISTING) $@
511 $(OBJDUMP) -h -S -z $< > $@
513 # Create a symbol table from ELF output file.
516 @echo $(MSG_SYMBOL_TABLE) $@
521 # Create library from object files.
522 .SECONDARY : $(TARGET).a
526 @echo $(MSG_CREATING_LIBRARY) $@
530 # Link: create ELF output file from object files.
531 .SECONDARY : $(TARGET).elf
535 @echo $(MSG_LINKING) $@
536 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
539 # 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
551 @echo $(MSG_COMPILING_CPP) $<
552 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
555 # Compile: create assembler files from C source files.
557 $(CC) -S $(ALL_CFLAGS) $< -o $@
560 # Compile: create assembler files from C++ source files.
562 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
565 # Assemble: create object files from assembler source files.
569 @echo $(MSG_ASSEMBLING) $<
570 $(CC) -c $(ALL_ASFLAGS) $< -o $@
573 # Create preprocessed source for use in sending a bug report.
575 $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
578 # Target: clean project.
579 clean: begin clean_list end
583 $(REMOVE) $(TARGET).hex
584 $(REMOVE) $(TARGET).eep
585 $(REMOVE) $(TARGET).cof
586 $(REMOVE) $(TARGET).elf
587 $(REMOVE) $(TARGET).map
588 $(REMOVE) $(TARGET).sym
589 $(REMOVE) $(TARGET).lss
592 $(REMOVE) $(OBJ:.o=.s)
593 $(REMOVE) $(OBJ:.o=.i)
595 $(REMOVE) -r $(OBJDIR)
602 # Create object files directory
603 $(shell mkdir $(OBJDIR) 2>/dev/null)
606 # Include the dependency files.
607 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
610 # Listing of phony targets.
611 .PHONY : all begin finish end sizebefore sizeafter gccversion \
612 build elf hex eep lss sym coff extcoff \
613 clean clean_list debug gdb-config show_path \
614 program teensy dfu flip dfu-ee flip-ee dfu-start