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
)
147 #---------------- Compiler Options C++ ----------------
148 # -g*: generate debugging information
149 # -O*: optimization level
150 # -f...: tuning, see GCC manual and avr-libc documentation
151 # -Wall...: warning level
152 # -Wa,...: tell GCC to pass this to the assembler.
153 # -adhlns...: create assembler listing
154 CPPFLAGS
= -g
$(DEBUG
)
155 CPPFLAGS
+= $(CPPDEFS
)
157 CPPFLAGS
+= -funsigned-char
158 CPPFLAGS
+= -funsigned-bitfields
159 CPPFLAGS
+= -fpack-struct
160 CPPFLAGS
+= -fshort-enums
161 CPPFLAGS
+= -fno-exceptions
162 CPPFLAGS
+= -ffunction-sections
163 CPPFLAGS
+= -fdata-sections
164 # to supress "warning: only initialized variables can be placed into program memory area"
168 #CPPFLAGS += -mshort-calls
169 #CPPFLAGS += -fno-unit-at-a-time
170 #CPPFLAGS += -Wstrict-prototypes
171 #CPPFLAGS += -Wunreachable-code
172 #CPPFLAGS += -Wsign-compare
173 CPPFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
)
174 CPPFLAGS
+= $(patsubst %,-I
%,$(EXTRAINCDIRS
))
175 #CPPFLAGS += $(CSTANDARD)
177 CPPFLAGS
+= -include $(CONFIG_H
)
181 #---------------- Assembler Options ----------------
182 # -Wa,...: tell GCC to pass this to the assembler.
183 # -adhlns: create listing
184 # -gstabs: have the assembler create line number information; note that
185 # for use in COFF files, additional information about filenames
186 # and function names needs to be present in the assembler source
187 # files -- see avr-libc docs [FIXME: not yet described there]
188 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
189 # dump that will be displayed for a given single line of source input.
190 ASFLAGS
= $(ADEFS
) -Wa
,-adhlns
=$(@
:%.o
=%.lst
),-gstabs
,--listing-cont-lines
=100
191 ASFLAGS
+= $(patsubst %,-I
%,$(EXTRAINCDIRS
))
193 ASFLAGS
+= -include $(CONFIG_H
)
197 #---------------- Library Options ----------------
198 # Minimalistic printf version
199 PRINTF_LIB_MIN
= -Wl
,-u
,vfprintf
-lprintf_min
201 # Floating point printf version (requires MATH_LIB = -lm below)
202 PRINTF_LIB_FLOAT
= -Wl
,-u
,vfprintf
-lprintf_flt
204 # If this is left blank, then it will use the Standard printf version.
206 #PRINTF_LIB = $(PRINTF_LIB_MIN)
207 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
210 # Minimalistic scanf version
211 SCANF_LIB_MIN
= -Wl
,-u
,vfscanf
-lscanf_min
213 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
214 SCANF_LIB_FLOAT
= -Wl
,-u
,vfscanf
-lscanf_flt
216 # If this is left blank, then it will use the Standard scanf version.
218 #SCANF_LIB = $(SCANF_LIB_MIN)
219 #SCANF_LIB = $(SCANF_LIB_FLOAT)
225 # List any extra directories to look for libraries here.
226 # Each directory must be seperated by a space.
227 # Use forward slashes for directory separators.
228 # For a directory that has spaces, enclose it in quotes.
233 #---------------- External Memory Options ----------------
235 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
236 # used for variables (.data/.bss) and heap (malloc()).
237 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
239 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
240 # only used for heap (malloc()).
241 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
247 #---------------- Linker Options ----------------
248 # -Wl,...: tell GCC to pass this to linker.
249 # -Map: create map file
250 # --cref: add cross reference to map file
252 # Comennt out "--relax" option to avoid a error such:
253 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
255 LDFLAGS
= -Wl
,-Map
=$(TARGET
).map
,--cref
256 #LDFLAGS += -Wl,--relax
257 LDFLAGS
+= -Wl
,--gc-sections
258 LDFLAGS
+= $(EXTMEMOPTS
)
259 LDFLAGS
+= $(patsubst %,-L
%,$(EXTRALIBDIRS
))
260 LDFLAGS
+= $(PRINTF_LIB
) $(SCANF_LIB
) $(MATH_LIB
)
261 #LDFLAGS += -T linker_script.x
262 # You can give EXTRALDFLAGS at 'make' command line.
263 LDFLAGS
+= $(EXTRALDFLAGS
)
267 #---------------- Debugging Options ----------------
269 # For simulavr only - target MCU frequency.
270 DEBUG_MFREQ
= $(F_CPU
)
272 # Set the DEBUG_UI to either gdb or insight.
276 # Set the debugging back-end to either avarice, simulavr.
277 DEBUG_BACKEND
= avarice
278 #DEBUG_BACKEND = simulavr
281 GDBINIT_FILE
= __avr_gdbinit
283 # When using avarice settings for the JTAG
286 # Debugging port used to communicate between GDB / avarice / simulavr.
289 # Debugging host used to communicate between GDB / avarice / simulavr, normally
290 # just set to localhost unless doing some sort of crazy debugging when
291 # avarice is running on a different computer.
292 DEBUG_HOST
= localhost
296 #============================================================================
299 # Define programs and commands.
302 OBJCOPY
= avr-objcopy
303 OBJDUMP
= avr-objdump
315 MSG_ERRORS_NONE
= Errors
: none
316 MSG_BEGIN
= -------- begin
--------
317 MSG_END
= -------- end
--------
318 MSG_SIZE_BEFORE
= Size before
:
319 MSG_SIZE_AFTER
= Size after
:
320 MSG_COFF
= Converting to AVR COFF
:
321 MSG_EXTENDED_COFF
= Converting to AVR Extended COFF
:
322 MSG_FLASH
= Creating load file for Flash
:
323 MSG_EEPROM
= Creating load file for EEPROM
:
324 MSG_EXTENDED_LISTING
= Creating Extended Listing
:
325 MSG_SYMBOL_TABLE
= Creating Symbol Table
:
326 MSG_LINKING
= Linking
:
327 MSG_COMPILING
= Compiling C
:
328 MSG_COMPILING_CPP
= Compiling C
++:
329 MSG_ASSEMBLING
= Assembling
:
330 MSG_CLEANING
= Cleaning project
:
331 MSG_CREATING_LIBRARY
= Creating library
:
336 # Define all object files.
337 OBJ
= $(patsubst %.c
,$(OBJDIR
)/%.o
,$(patsubst %.
cpp,$(OBJDIR
)/%.o
,$(patsubst %.S
,$(OBJDIR
)/%.o
,$(SRC
))))
339 # Define all listing files.
340 LST
= $(patsubst %.c
,$(OBJDIR
)/%.lst
,$(patsubst %.
cpp,$(OBJDIR
)/%.lst
,$(patsubst %.S
,$(OBJDIR
)/%.lst
,$(SRC
))))
343 # Compiler flags to generate dependency files.
344 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
345 GENDEPFLAGS
= -MMD
-MP
-MF .dep
/$(subst /,_
,$@
).d
348 # Combine all necessary flags and optional flags.
349 # Add target processor to flags.
350 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
351 ALL_CFLAGS
= -mmcu
=$(MCU
) $(CFLAGS
) $(GENDEPFLAGS
) $(EXTRAFLAGS
)
352 ALL_CPPFLAGS
= -mmcu
=$(MCU
) -x c
++ $(CPPFLAGS
) $(GENDEPFLAGS
) $(EXTRAFLAGS
)
353 ALL_ASFLAGS
= -mmcu
=$(MCU
) -x assembler-with-cpp
$(ASFLAGS
) $(EXTRAFLAGS
)
360 all: begin gccversion sizebefore build sizeafter end
362 # Change the build target to build a HEX file or a library.
363 build
: elf hex eep lss sym
372 LIBNAME
=lib
$(TARGET
).a
378 # AVR Studio 3.x does not check make's exit code but relies on
379 # the following magic strings to be generated by the compile job.
389 # Display size of file.
390 HEXSIZE
= $(SIZE
) --target
=$(FORMAT
) $(TARGET
).hex
391 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
392 ELFSIZE
= $(SIZE
) $(TARGET
).elf
395 @if
test -f
$(TARGET
).elf
; then echo
; echo
$(MSG_SIZE_BEFORE
); $(ELFSIZE
); \
396 2>/dev
/null
; echo
; fi
399 @if
test -f
$(TARGET
).elf
; then echo
; echo
$(MSG_SIZE_AFTER
); $(ELFSIZE
); \
400 2>/dev
/null
; echo
; fi
404 # Display compiler version information.
410 # Program the device.
411 program
: $(TARGET
).hex
$(TARGET
).eep
414 teensy
: $(TARGET
).hex
415 teensy_loader_cli
-mmcu
=$(MCU
) -w
-v
$(TARGET
).hex
418 batchisp
-hardware usb
-device
$(MCU
) -operation erase f
419 batchisp
-hardware usb
-device
$(MCU
) -operation loadbuffer
$(TARGET
).hex program
420 batchisp
-hardware usb
-device
$(MCU
) -operation start reset
0
423 ifneq (, $(findstring 0.7, $(shell dfu-programmer
--version
2>&1)))
424 dfu-programmer
$(MCU
) erase
--force
426 dfu-programmer
$(MCU
) erase
428 dfu-programmer
$(MCU
) erase
429 dfu-programmer
$(MCU
) flash
$(TARGET
).hex
430 dfu-programmer
$(MCU
) reset
433 dfu-programmer
$(MCU
) reset
434 dfu-programmer
$(MCU
) start
436 flip-ee
: $(TARGET
).hex
$(TARGET
).eep
437 $(COPY
) $(TARGET
).eep
$(TARGET
)eep.hex
438 batchisp
-hardware usb
-device
$(MCU
) -operation memory EEPROM erase
439 batchisp
-hardware usb
-device
$(MCU
) -operation memory EEPROM loadbuffer
$(TARGET
)eep.hex program
440 batchisp
-hardware usb
-device
$(MCU
) -operation start reset
0
441 $(REMOVE
) $(TARGET
)eep.hex
443 dfu-ee
: $(TARGET
).hex
$(TARGET
).eep
444 ifneq (, $(findstring 0.7, $(shell dfu-programmer
--version
2>&1)))
445 dfu-programmer
$(MCU
) flash
--eeprom
$(TARGET
).eep
447 dfu-programmer
$(MCU
) flash-eeprom
$(TARGET
).eep
449 dfu-programmer
$(MCU
) reset
452 # Generate avr-gdb config/init file which does the following:
453 # define the reset signal, load the target file, connect to target, and set
454 # a breakpoint at main().
456 @
$(REMOVE
) $(GDBINIT_FILE
)
457 @echo
define reset
>> $(GDBINIT_FILE
)
458 @echo SIGNAL SIGHUP
>> $(GDBINIT_FILE
)
459 @echo end
>> $(GDBINIT_FILE
)
460 @echo file
$(TARGET
).elf
>> $(GDBINIT_FILE
)
461 @echo target remote
$(DEBUG_HOST
):$(DEBUG_PORT
) >> $(GDBINIT_FILE
)
462 ifeq ($(DEBUG_BACKEND
),simulavr
)
463 @echo load
>> $(GDBINIT_FILE
)
465 @echo break main
>> $(GDBINIT_FILE
)
467 debug
: gdb-config
$(TARGET
).elf
468 ifeq ($(DEBUG_BACKEND
), avarice
)
469 @echo Starting AVaRICE
- Press enter when
"waiting to connect" message displays.
470 @
$(WINSHELL
) /c start avarice
--jtag
$(JTAG_DEV
) --erase
--program
--file \
471 $(TARGET
).elf
$(DEBUG_HOST
):$(DEBUG_PORT
)
472 @
$(WINSHELL
) /c pause
475 @
$(WINSHELL
) /c start simulavr
--gdbserver
--device
$(MCU
) --clock-freq \
476 $(DEBUG_MFREQ
) --port
$(DEBUG_PORT
)
478 @
$(WINSHELL
) /c start avr-
$(DEBUG_UI
) --command
=$(GDBINIT_FILE
)
483 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
484 COFFCONVERT
= $(OBJCOPY
) --debugging
485 COFFCONVERT
+= --change-section-address .data-0x800000
486 COFFCONVERT
+= --change-section-address .bss-0x800000
487 COFFCONVERT
+= --change-section-address .noinit-0x800000
488 COFFCONVERT
+= --change-section-address .eeprom-0x810000
494 @echo
$(MSG_COFF
) $(TARGET
).cof
495 $(COFFCONVERT
) -O coff-avr
$< $(TARGET
).cof
498 extcoff
: $(TARGET
).elf
500 @echo
$(MSG_EXTENDED_COFF
) $(TARGET
).cof
501 $(COFFCONVERT
) -O coff-ext-avr
$< $(TARGET
).cof
505 # Create final output files (.hex, .eep) from ELF output file.
508 @echo
$(MSG_FLASH
) $@
509 $(OBJCOPY
) -O
$(FORMAT
) -R .eeprom
-R .fuse
-R .lock
-R .signature
$< $@
513 @echo
$(MSG_EEPROM
) $@
514 -$(OBJCOPY
) -j .eeprom
--set-section-flags
=.eeprom
="alloc,load" \
515 --change-section-lma .eeprom
=0 --no-change-warnings
-O
$(FORMAT
) $< $@ || exit
0
517 # Create extended listing file from ELF output file.
520 @echo
$(MSG_EXTENDED_LISTING
) $@
521 $(OBJDUMP
) -h
-S
-z
$< > $@
523 # Create a symbol table from ELF output file.
526 @echo
$(MSG_SYMBOL_TABLE
) $@
531 # Create library from object files.
532 .SECONDARY
: $(TARGET
).a
536 @echo
$(MSG_CREATING_LIBRARY
) $@
540 # Link: create ELF output file from object files.
541 .SECONDARY
: $(TARGET
).elf
545 @echo
$(MSG_LINKING
) $@
546 $(CC
) $(ALL_CFLAGS
) $^
--output
$@
$(LDFLAGS
)
549 # Compile: create object files from C source files.
553 @echo
$(MSG_COMPILING
) $<
554 $(CC
) -c
$(ALL_CFLAGS
) $< -o
$@
557 # Compile: create object files from C++ source files.
558 $(OBJDIR
)/%.o
: %.
cpp
561 @echo
$(MSG_COMPILING_CPP
) $<
562 $(CC
) -c
$(ALL_CPPFLAGS
) $< -o
$@
565 # Compile: create assembler files from C source files.
567 $(CC
) -S
$(ALL_CFLAGS
) $< -o
$@
570 # Compile: create assembler files from C++ source files.
572 $(CC
) -S
$(ALL_CPPFLAGS
) $< -o
$@
575 # Assemble: create object files from assembler source files.
579 @echo
$(MSG_ASSEMBLING
) $<
580 $(CC
) -c
$(ALL_ASFLAGS
) $< -o
$@
583 # Create preprocessed source for use in sending a bug report.
585 $(CC
) -E
-mmcu
=$(MCU
) $(CFLAGS
) $< -o
$@
588 # Target: clean project.
589 clean: begin clean_list end
593 $(REMOVE
) $(TARGET
).hex
594 $(REMOVE
) $(TARGET
).eep
595 $(REMOVE
) $(TARGET
).cof
596 $(REMOVE
) $(TARGET
).elf
597 $(REMOVE
) $(TARGET
).map
598 $(REMOVE
) $(TARGET
).sym
599 $(REMOVE
) $(TARGET
).lss
602 $(REMOVE
) $(OBJ
:.o
=.s
)
603 $(REMOVE
) $(OBJ
:.o
=.i
)
605 $(REMOVE
) -r
$(OBJDIR
)
612 # Create object files directory
613 $(shell mkdir
$(OBJDIR
) 2>/dev
/null
)
616 # Include the dependency files.
617 -include $(shell mkdir .dep
2>/dev
/null
) $(wildcard .dep
/*)
620 # Listing of phony targets.
621 .PHONY
: all begin finish end sizebefore sizeafter gccversion \
622 build elf hex eep lss sym coff extcoff \
623 clean clean_list debug gdb-config show_path \
624 program teensy dfu flip dfu-ee flip-ee dfu-start