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
+= -fno-inline-small-functions
113 CFLAGS
+= -fpack-struct
114 CFLAGS
+= -fshort-enums
115 CFLAGS
+= -fno-strict-aliasing
117 CFLAGS
+= -Wstrict-prototypes
118 #CFLAGS += -mshort-calls
119 #CFLAGS += -fno-unit-at-a-time
121 #CFLAGS += -Wunreachable-code
122 #CFLAGS += -Wsign-compare
123 CFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
)
124 CFLAGS
+= $(patsubst %,-I
%,$(EXTRAINCDIRS
))
125 CFLAGS
+= $(CSTANDARD
)
127 CFLAGS
+= -include $(CONFIG_H
)
131 #---------------- Compiler Options C++ ----------------
132 # -g*: generate debugging information
133 # -O*: optimization level
134 # -f...: tuning, see GCC manual and avr-libc documentation
135 # -Wall...: warning level
136 # -Wa,...: tell GCC to pass this to the assembler.
137 # -adhlns...: create assembler listing
138 CPPFLAGS
= -g
$(DEBUG
)
139 CPPFLAGS
+= $(CPPDEFS
)
141 CPPFLAGS
+= -funsigned-char
142 CPPFLAGS
+= -funsigned-bitfields
143 CPPFLAGS
+= -fpack-struct
144 CPPFLAGS
+= -fshort-enums
145 CPPFLAGS
+= -fno-exceptions
148 #CPPFLAGS += -mshort-calls
149 #CPPFLAGS += -fno-unit-at-a-time
150 #CPPFLAGS += -Wstrict-prototypes
151 #CPPFLAGS += -Wunreachable-code
152 #CPPFLAGS += -Wsign-compare
153 CPPFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
)
154 CPPFLAGS
+= $(patsubst %,-I
%,$(EXTRAINCDIRS
))
155 #CPPFLAGS += $(CSTANDARD)
157 CPPFLAGS
+= -include $(CONFIG_H
)
161 #---------------- Assembler Options ----------------
162 # -Wa,...: tell GCC to pass this to the assembler.
163 # -adhlns: create listing
164 # -gstabs: have the assembler create line number information; note that
165 # for use in COFF files, additional information about filenames
166 # and function names needs to be present in the assembler source
167 # files -- see avr-libc docs [FIXME: not yet described there]
168 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
169 # dump that will be displayed for a given single line of source input.
170 ASFLAGS
= $(ADEFS
) -Wa
,-adhlns
=$(@
:%.o
=%.lst
),-gstabs
,--listing-cont-lines
=100
172 ASFLAGS
+= -include $(CONFIG_H
)
176 #---------------- Library Options ----------------
177 # Minimalistic printf version
178 PRINTF_LIB_MIN
= -Wl
,-u
,vfprintf
-lprintf_min
180 # Floating point printf version (requires MATH_LIB = -lm below)
181 PRINTF_LIB_FLOAT
= -Wl
,-u
,vfprintf
-lprintf_flt
183 # If this is left blank, then it will use the Standard printf version.
185 #PRINTF_LIB = $(PRINTF_LIB_MIN)
186 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
189 # Minimalistic scanf version
190 SCANF_LIB_MIN
= -Wl
,-u
,vfscanf
-lscanf_min
192 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
193 SCANF_LIB_FLOAT
= -Wl
,-u
,vfscanf
-lscanf_flt
195 # If this is left blank, then it will use the Standard scanf version.
197 #SCANF_LIB = $(SCANF_LIB_MIN)
198 #SCANF_LIB = $(SCANF_LIB_FLOAT)
204 # List any extra directories to look for libraries here.
205 # Each directory must be seperated by a space.
206 # Use forward slashes for directory separators.
207 # For a directory that has spaces, enclose it in quotes.
212 #---------------- External Memory Options ----------------
214 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
215 # used for variables (.data/.bss) and heap (malloc()).
216 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
218 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
219 # only used for heap (malloc()).
220 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
226 #---------------- Linker Options ----------------
227 # -Wl,...: tell GCC to pass this to linker.
228 # -Map: create map file
229 # --cref: add cross reference to map file
231 # Comennt out "--relax" option to avoid a error such:
232 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
234 LDFLAGS
= -Wl
,-Map
=$(TARGET
).map
,--cref
235 LDFLAGS
+= -Wl
,--relax
236 LDFLAGS
+= -Wl
,--gc-sections
237 LDFLAGS
+= $(EXTMEMOPTS
)
238 LDFLAGS
+= $(patsubst %,-L
%,$(EXTRALIBDIRS
))
239 LDFLAGS
+= $(PRINTF_LIB
) $(SCANF_LIB
) $(MATH_LIB
)
240 #LDFLAGS += -T linker_script.x
241 # You can give EXTRALDFLAGS at 'make' command line.
242 LDFLAGS
+= $(EXTRALDFLAGS
)
246 #---------------- Debugging Options ----------------
248 # For simulavr only - target MCU frequency.
249 DEBUG_MFREQ
= $(F_CPU
)
251 # Set the DEBUG_UI to either gdb or insight.
255 # Set the debugging back-end to either avarice, simulavr.
256 DEBUG_BACKEND
= avarice
257 #DEBUG_BACKEND = simulavr
260 GDBINIT_FILE
= __avr_gdbinit
262 # When using avarice settings for the JTAG
265 # Debugging port used to communicate between GDB / avarice / simulavr.
268 # Debugging host used to communicate between GDB / avarice / simulavr, normally
269 # just set to localhost unless doing some sort of crazy debugging when
270 # avarice is running on a different computer.
271 DEBUG_HOST
= localhost
275 #============================================================================
278 # Define programs and commands.
281 OBJCOPY
= avr-objcopy
282 OBJDUMP
= avr-objdump
294 MSG_ERRORS_NONE
= Errors
: none
295 MSG_BEGIN
= -------- begin
--------
296 MSG_END
= -------- end
--------
297 MSG_SIZE_BEFORE
= Size before
:
298 MSG_SIZE_AFTER
= Size after
:
299 MSG_COFF
= Converting to AVR COFF
:
300 MSG_EXTENDED_COFF
= Converting to AVR Extended COFF
:
301 MSG_FLASH
= Creating load file for Flash
:
302 MSG_EEPROM
= Creating load file for EEPROM
:
303 MSG_EXTENDED_LISTING
= Creating Extended Listing
:
304 MSG_SYMBOL_TABLE
= Creating Symbol Table
:
305 MSG_LINKING
= Linking
:
306 MSG_COMPILING
= Compiling C
:
307 MSG_COMPILING_CPP
= Compiling C
++:
308 MSG_ASSEMBLING
= Assembling
:
309 MSG_CLEANING
= Cleaning project
:
310 MSG_CREATING_LIBRARY
= Creating library
:
315 # Define all object files.
316 OBJ
= $(patsubst %.c
,$(OBJDIR
)/%.o
,$(patsubst %.
cpp,$(OBJDIR
)/%.o
,$(patsubst %.S
,$(OBJDIR
)/%.o
,$(SRC
))))
318 # Define all listing files.
319 LST
= $(patsubst %.c
,$(OBJDIR
)/%.lst
,$(patsubst %.
cpp,$(OBJDIR
)/%.lst
,$(patsubst %.S
,$(OBJDIR
)/%.lst
,$(SRC
))))
322 # Compiler flags to generate dependency files.
323 GENDEPFLAGS
= -MMD
-MP
-MF .dep
/$(@F
).d
326 # Combine all necessary flags and optional flags.
327 # Add target processor to flags.
328 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
329 ALL_CFLAGS
= -mmcu
=$(MCU
) -I.
$(CFLAGS
) $(GENDEPFLAGS
) $(EXTRAFLAGS
)
330 ALL_CPPFLAGS
= -mmcu
=$(MCU
) -I.
-x c
++ $(CPPFLAGS
) $(GENDEPFLAGS
) $(EXTRAFLAGS
)
331 ALL_ASFLAGS
= -mmcu
=$(MCU
) -I.
-x assembler-with-cpp
$(ASFLAGS
) $(EXTRAFLAGS
)
338 all: begin gccversion sizebefore build sizeafter end
340 # Change the build target to build a HEX file or a library.
341 build
: elf hex eep lss sym
350 LIBNAME
=lib
$(TARGET
).a
356 # AVR Studio 3.x does not check make's exit code but relies on
357 # the following magic strings to be generated by the compile job.
367 # Display size of file.
368 HEXSIZE
= $(SIZE
) --target
=$(FORMAT
) $(TARGET
).hex
369 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
370 ELFSIZE
= $(SIZE
) $(TARGET
).elf
373 @if
test -f
$(TARGET
).elf
; then echo
; echo
$(MSG_SIZE_BEFORE
); $(ELFSIZE
); \
374 2>/dev
/null
; echo
; fi
377 @if
test -f
$(TARGET
).elf
; then echo
; echo
$(MSG_SIZE_AFTER
); $(ELFSIZE
); \
378 2>/dev
/null
; echo
; fi
382 # Display compiler version information.
388 # Program the device.
389 program
: $(TARGET
).hex
$(TARGET
).eep
392 teensy
: $(TARGET
).hex
393 teensy_loader_cli
-mmcu
=$(MCU
) -w
-v
$(TARGET
).hex
396 batchisp
-hardware usb
-device
$(MCU
) -operation erase f
397 batchisp
-hardware usb
-device
$(MCU
) -operation loadbuffer
$(TARGET
).hex program
398 batchisp
-hardware usb
-device
$(MCU
) -operation start reset
0
401 dfu-programmer
$(MCU
) erase
402 dfu-programmer
$(MCU
) flash
$(TARGET
).hex
403 dfu-programmer
$(MCU
) reset
405 flip-ee
: $(TARGET
).hex
$(TARGET
).eep
406 $(COPY
) $(TARGET
).eep
$(TARGET
)eep.hex
407 batchisp
-hardware usb
-device
$(MCU
) -operation memory EEPROM erase
408 batchisp
-hardware usb
-device
$(MCU
) -operation memory EEPROM loadbuffer
$(TARGET
)eep.hex program
409 batchisp
-hardware usb
-device
$(MCU
) -operation start reset
0
410 $(REMOVE
) $(TARGET
)eep.hex
412 dfu-ee
: $(TARGET
).hex
$(TARGET
).eep
413 dfu-programmer
$(MCU
) eeprom-flash
$(TARGET
).eep
414 dfu-programmer
$(MCU
) reset
417 # Generate avr-gdb config/init file which does the following:
418 # define the reset signal, load the target file, connect to target, and set
419 # a breakpoint at main().
421 @
$(REMOVE
) $(GDBINIT_FILE
)
422 @echo
define reset
>> $(GDBINIT_FILE
)
423 @echo SIGNAL SIGHUP
>> $(GDBINIT_FILE
)
424 @echo end
>> $(GDBINIT_FILE
)
425 @echo file
$(TARGET
).elf
>> $(GDBINIT_FILE
)
426 @echo target remote
$(DEBUG_HOST
):$(DEBUG_PORT
) >> $(GDBINIT_FILE
)
427 ifeq ($(DEBUG_BACKEND
),simulavr
)
428 @echo load
>> $(GDBINIT_FILE
)
430 @echo break main
>> $(GDBINIT_FILE
)
432 debug
: gdb-config
$(TARGET
).elf
433 ifeq ($(DEBUG_BACKEND
), avarice
)
434 @echo Starting AVaRICE
- Press enter when
"waiting to connect" message displays.
435 @
$(WINSHELL
) /c start avarice
--jtag
$(JTAG_DEV
) --erase
--program
--file \
436 $(TARGET
).elf
$(DEBUG_HOST
):$(DEBUG_PORT
)
437 @
$(WINSHELL
) /c pause
440 @
$(WINSHELL
) /c start simulavr
--gdbserver
--device
$(MCU
) --clock-freq \
441 $(DEBUG_MFREQ
) --port
$(DEBUG_PORT
)
443 @
$(WINSHELL
) /c start avr-
$(DEBUG_UI
) --command
=$(GDBINIT_FILE
)
448 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
449 COFFCONVERT
= $(OBJCOPY
) --debugging
450 COFFCONVERT
+= --change-section-address .data-0x800000
451 COFFCONVERT
+= --change-section-address .bss-0x800000
452 COFFCONVERT
+= --change-section-address .noinit-0x800000
453 COFFCONVERT
+= --change-section-address .eeprom-0x810000
459 @echo
$(MSG_COFF
) $(TARGET
).cof
460 $(COFFCONVERT
) -O coff-avr
$< $(TARGET
).cof
463 extcoff
: $(TARGET
).elf
465 @echo
$(MSG_EXTENDED_COFF
) $(TARGET
).cof
466 $(COFFCONVERT
) -O coff-ext-avr
$< $(TARGET
).cof
470 # Create final output files (.hex, .eep) from ELF output file.
473 @echo
$(MSG_FLASH
) $@
474 $(OBJCOPY
) -O
$(FORMAT
) -R .eeprom
-R .fuse
-R .lock
-R .signature
$< $@
478 @echo
$(MSG_EEPROM
) $@
479 -$(OBJCOPY
) -j .eeprom
--set-section-flags
=.eeprom
="alloc,load" \
480 --change-section-lma .eeprom
=0 --no-change-warnings
-O
$(FORMAT
) $< $@ || exit
0
482 # Create extended listing file from ELF output file.
485 @echo
$(MSG_EXTENDED_LISTING
) $@
486 $(OBJDUMP
) -h
-S
-z
$< > $@
488 # Create a symbol table from ELF output file.
491 @echo
$(MSG_SYMBOL_TABLE
) $@
496 # Create library from object files.
497 .SECONDARY
: $(TARGET
).a
501 @echo
$(MSG_CREATING_LIBRARY
) $@
505 # Link: create ELF output file from object files.
506 .SECONDARY
: $(TARGET
).elf
510 @echo
$(MSG_LINKING
) $@
511 $(CC
) $(ALL_CFLAGS
) $^
--output
$@
$(LDFLAGS
)
514 # Compile: create object files from C source files.
518 @echo
$(MSG_COMPILING
) $<
519 $(CC
) -c
$(ALL_CFLAGS
) $< -o
$@
522 # Compile: create object files from C++ source files.
523 $(OBJDIR
)/%.o
: %.
cpp
525 @echo
$(MSG_COMPILING_CPP
) $<
526 $(CC
) -c
$(ALL_CPPFLAGS
) $< -o
$@
529 # Compile: create assembler files from C source files.
531 $(CC
) -S
$(ALL_CFLAGS
) $< -o
$@
534 # Compile: create assembler files from C++ source files.
536 $(CC
) -S
$(ALL_CPPFLAGS
) $< -o
$@
539 # Assemble: create object files from assembler source files.
542 @echo
$(MSG_ASSEMBLING
) $<
543 $(CC
) -c
$(ALL_ASFLAGS
) $< -o
$@
546 # Create preprocessed source for use in sending a bug report.
548 $(CC
) -E
-mmcu
=$(MCU
) -I.
$(CFLAGS
) $< -o
$@
551 # Target: clean project.
552 clean: begin clean_list end
556 $(REMOVE
) $(TARGET
).hex
557 $(REMOVE
) $(TARGET
).eep
558 $(REMOVE
) $(TARGET
).cof
559 $(REMOVE
) $(TARGET
).elf
560 $(REMOVE
) $(TARGET
).map
561 $(REMOVE
) $(TARGET
).sym
562 $(REMOVE
) $(TARGET
).lss
565 $(REMOVE
) $(OBJ
:.o
=.s
)
566 $(REMOVE
) $(OBJ
:.o
=.i
)
568 $(REMOVE
) -r
$(OBJDIR
)
571 # Create object files directory
572 $(shell mkdir
$(OBJDIR
) 2>/dev
/null
)
575 # Include the dependency files.
576 -include $(shell mkdir .dep
2>/dev
/null
) $(wildcard .dep
/*)
579 # Listing of phony targets.
580 .PHONY
: all begin finish end sizebefore sizeafter gccversion \
581 build elf hex eep lss sym coff extcoff \
582 clean clean_list program debug gdb-config