]> git.donarmstrong.com Git - tmk_firmware.git/blob - rules.mk
initial attempt for LUFA.
[tmk_firmware.git] / rules.mk
1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jg Wunsch, et al.
4 #
5 # Released to the Public Domain
6 #
7 # Additional material for this makefile was written by:
8 # Peter Fleury
9 # Tim Henigan
10 # Colin O'Flynn
11 # Reiner Patommel
12 # Markus Pfaff
13 # Sander Pool
14 # Frederik Rouleau
15 # Carlos Lamas
16 #
17 #----------------------------------------------------------------------------
18 # On command line:
19 #
20 # make all = Make software.
21 #
22 # make clean = Clean out built project files.
23 #
24 # make coff = Convert ELF to AVR COFF.
25 #
26 # make extcoff = Convert ELF to AVR Extended COFF.
27 #
28 # make program = Download the hex file to the device, using avrdude.
29 #                Please customize the avrdude settings below first!
30 #
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.
33 #
34 # make filename.s = Just compile filename.c into the assembler code only.
35 #
36 # make filename.i = Create a preprocessed source file for use in submitting
37 #                   bug reports to the GCC project.
38 #
39 # To rebuild project do "make clean" then "make all".
40 #----------------------------------------------------------------------------
41
42
43 # Output format. (can be srec, ihex, binary)
44 FORMAT = ihex
45
46
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)
51
52
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.)
56 OPT = s
57
58
59 # Debugging format.
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.
63 DEBUG = dwarf-2
64
65
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))
71
72
73 # Compiler flag to set the C Standard level.
74 #     c89   = "ANSI" C
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
79
80
81 # Place -D or -U options here for C sources
82 CDEFS = -DF_CPU=$(F_CPU)UL
83 CDEFS += $(OPT_DEFS)
84
85
86 # Place -D or -U options here for ASM sources
87 ADEFS = -DF_CPU=$(F_CPU)
88 ADEFS += $(OPT_DEFS)
89
90
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)
96
97
98
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
106 CFLAGS = -g$(DEBUG)
107 CFLAGS += $(CDEFS)
108 CFLAGS += -O$(OPT)
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
116 CFLAGS += -Wall
117 CFLAGS += -Wstrict-prototypes
118 #CFLAGS += -mshort-calls
119 #CFLAGS += -fno-unit-at-a-time
120 #CFLAGS += -Wundef
121 #CFLAGS += -Wunreachable-code
122 #CFLAGS += -Wsign-compare
123 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
124 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
125 CFLAGS += $(CSTANDARD)
126 ifdef CONFIG_H
127     CFLAGS += -include $(CONFIG_H)
128 endif
129
130
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)
140 CPPFLAGS += -O$(OPT)
141 CPPFLAGS += -funsigned-char
142 CPPFLAGS += -funsigned-bitfields
143 CPPFLAGS += -fpack-struct
144 CPPFLAGS += -fshort-enums
145 CPPFLAGS += -fno-exceptions
146 CPPFLAGS += -Wall
147 CPPFLAGS += -Wundef
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)
156 ifdef CONFIG_H
157     CPPFLAGS += -include $(CONFIG_H)
158 endif
159
160
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
171 ifdef CONFIG_H
172     ASFLAGS += -include $(CONFIG_H)
173 endif
174
175
176 #---------------- Library Options ----------------
177 # Minimalistic printf version
178 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
179
180 # Floating point printf version (requires MATH_LIB = -lm below)
181 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
182
183 # If this is left blank, then it will use the Standard printf version.
184 PRINTF_LIB = 
185 #PRINTF_LIB = $(PRINTF_LIB_MIN)
186 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
187
188
189 # Minimalistic scanf version
190 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
191
192 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
193 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
194
195 # If this is left blank, then it will use the Standard scanf version.
196 SCANF_LIB = 
197 #SCANF_LIB = $(SCANF_LIB_MIN)
198 #SCANF_LIB = $(SCANF_LIB_FLOAT)
199
200
201 MATH_LIB = -lm
202
203
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.
208 EXTRALIBDIRS = 
209
210
211
212 #---------------- External Memory Options ----------------
213
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
217
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
221
222 EXTMEMOPTS =
223
224
225
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
230 #
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'
233 #
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)
243
244
245
246 #---------------- Debugging Options ----------------
247
248 # For simulavr only - target MCU frequency.
249 DEBUG_MFREQ = $(F_CPU)
250
251 # Set the DEBUG_UI to either gdb or insight.
252 # DEBUG_UI = gdb
253 DEBUG_UI = insight
254
255 # Set the debugging back-end to either avarice, simulavr.
256 DEBUG_BACKEND = avarice
257 #DEBUG_BACKEND = simulavr
258
259 # GDB Init Filename.
260 GDBINIT_FILE = __avr_gdbinit
261
262 # When using avarice settings for the JTAG
263 JTAG_DEV = /dev/com1
264
265 # Debugging port used to communicate between GDB / avarice / simulavr.
266 DEBUG_PORT = 4242
267
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
272
273
274
275 #============================================================================
276
277
278 # Define programs and commands.
279 SHELL = sh
280 CC = avr-gcc
281 OBJCOPY = avr-objcopy
282 OBJDUMP = avr-objdump
283 SIZE = avr-size
284 AR = avr-ar rcs
285 NM = avr-nm
286 REMOVE = rm -f
287 REMOVEDIR = rmdir
288 COPY = cp
289 WINSHELL = cmd
290
291
292 # Define Messages
293 # English
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:
311
312
313
314
315 # Define all object files.
316 OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
317
318 # Define all listing files.
319 LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
320
321
322 # Compiler flags to generate dependency files.
323 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
324
325
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)
332
333
334
335
336
337 # Default target.
338 all: begin gccversion sizebefore build sizeafter end
339
340 # Change the build target to build a HEX file or a library.
341 build: elf hex eep lss sym
342 #build: lib
343
344
345 elf: $(TARGET).elf
346 hex: $(TARGET).hex
347 eep: $(TARGET).eep
348 lss: $(TARGET).lss
349 sym: $(TARGET).sym
350 LIBNAME=lib$(TARGET).a
351 lib: $(LIBNAME)
352
353
354
355 # Eye candy.
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.
358 begin:
359         @echo
360         @echo $(MSG_BEGIN)
361
362 end:
363         @echo $(MSG_END)
364         @echo
365
366
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
371
372 sizebefore:
373         @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
374         2>/dev/null; echo; fi
375
376 sizeafter:
377         @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
378         2>/dev/null; echo; fi
379
380
381
382 # Display compiler version information.
383 gccversion : 
384         @$(CC) --version
385
386
387
388 # Program the device.  
389 program: $(TARGET).hex $(TARGET).eep
390         $(PROGRAM_CMD)
391
392 teensy: $(TARGET).hex
393         teensy_loader_cli -mmcu=$(MCU) -w -v $(TARGET).hex
394
395 flip: $(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
399
400 dfu: $(TARGET).hex
401         dfu-programmer $(MCU) erase
402         dfu-programmer $(MCU) flash $(TARGET).hex
403         dfu-programmer $(MCU) reset
404
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
411
412 dfu-ee: $(TARGET).hex $(TARGET).eep
413         dfu-programmer $(MCU) eeprom-flash $(TARGET).eep
414         dfu-programmer $(MCU) reset
415
416
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().
420 gdb-config: 
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)
429 endif
430         @echo break main >> $(GDBINIT_FILE)
431
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
438
439 else
440         @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
441         $(DEBUG_MFREQ) --port $(DEBUG_PORT)
442 endif
443         @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
444
445
446
447
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
454
455
456
457 coff: $(TARGET).elf
458         @echo
459         @echo $(MSG_COFF) $(TARGET).cof
460         $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
461
462
463 extcoff: $(TARGET).elf
464         @echo
465         @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
466         $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
467
468
469
470 # Create final output files (.hex, .eep) from ELF output file.
471 %.hex: %.elf
472         @echo
473         @echo $(MSG_FLASH) $@
474         $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
475
476 %.eep: %.elf
477         @echo
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
481
482 # Create extended listing file from ELF output file.
483 %.lss: %.elf
484         @echo
485         @echo $(MSG_EXTENDED_LISTING) $@
486         $(OBJDUMP) -h -S -z $< > $@
487
488 # Create a symbol table from ELF output file.
489 %.sym: %.elf
490         @echo
491         @echo $(MSG_SYMBOL_TABLE) $@
492         $(NM) -n $< > $@
493
494
495
496 # Create library from object files.
497 .SECONDARY : $(TARGET).a
498 .PRECIOUS : $(OBJ)
499 %.a: $(OBJ)
500         @echo
501         @echo $(MSG_CREATING_LIBRARY) $@
502         $(AR) $@ $(OBJ)
503
504
505 # Link: create ELF output file from object files.
506 .SECONDARY : $(TARGET).elf
507 .PRECIOUS : $(OBJ)
508 %.elf: $(OBJ)
509         @echo
510         @echo $(MSG_LINKING) $@
511         $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
512
513
514 # Compile: create object files from C source files.
515 $(OBJDIR)/%.o : %.c
516         @echo
517         mkdir -p $(@D)
518         @echo $(MSG_COMPILING) $<
519         $(CC) -c $(ALL_CFLAGS) $< -o $@ 
520
521
522 # Compile: create object files from C++ source files.
523 $(OBJDIR)/%.o : %.cpp
524         @echo
525         @echo $(MSG_COMPILING_CPP) $<
526         $(CC) -c $(ALL_CPPFLAGS) $< -o $@ 
527
528
529 # Compile: create assembler files from C source files.
530 %.s : %.c
531         $(CC) -S $(ALL_CFLAGS) $< -o $@
532
533
534 # Compile: create assembler files from C++ source files.
535 %.s : %.cpp
536         $(CC) -S $(ALL_CPPFLAGS) $< -o $@
537
538
539 # Assemble: create object files from assembler source files.
540 $(OBJDIR)/%.o : %.S
541         @echo
542         @echo $(MSG_ASSEMBLING) $<
543         $(CC) -c $(ALL_ASFLAGS) $< -o $@
544
545
546 # Create preprocessed source for use in sending a bug report.
547 %.i : %.c
548         $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ 
549
550
551 # Target: clean project.
552 clean: begin clean_list end
553
554 clean_list :
555         @echo
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
563         $(REMOVE) $(OBJ)
564         $(REMOVE) $(LST)
565         $(REMOVE) $(OBJ:.o=.s)
566         $(REMOVE) $(OBJ:.o=.i)
567         $(REMOVE) -r .dep
568         $(REMOVE) -r $(OBJDIR)
569
570
571 # Create object files directory
572 $(shell mkdir $(OBJDIR) 2>/dev/null)
573
574
575 # Include the dependency files.
576 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
577
578
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
583