core: Add matrix_clear() and default impl.
[tmk_keyboard.git] / tmk_core / rules.mk
CommitLineData
a074364c 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.
29# Please customize your programmer settings(PROGRAM_CMD)
30#
31# make teensy = Download the hex file to the device, using teensy_loader_cli.
32# (must have teensy_loader_cli installed).
33#
34# make dfu = Download the hex file to the device, using dfu-programmer (must
35# have dfu-programmer installed).
36#
37# make flip = Download the hex file to the device, using Atmel FLIP (must
38# have Atmel FLIP installed).
39#
40# make dfu-ee = Download the eeprom file to the device, using dfu-programmer
41# (must have dfu-programmer installed).
42#
43# make flip-ee = Download the eeprom file to the device, using Atmel FLIP
44# (must have Atmel FLIP installed).
45#
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.
48#
49# make filename.s = Just compile filename.c into the assembler code only.
50#
51# make filename.i = Create a preprocessed source file for use in submitting
52# bug reports to the GCC project.
53#
54# To rebuild project do "make clean" then "make all".
55#----------------------------------------------------------------------------
56
57
58# Output format. (can be srec, ihex, binary)
59FORMAT = ihex
60
61
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!
65OBJDIR = obj_$(TARGET)
66
67
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.)
71OPT = s
72
73
74# Debugging format.
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.
78DEBUG = dwarf-2
79
80
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.
85EXTRAINCDIRS = $(subst :, ,$(VPATH))
86
87
88# Compiler flag to set the C Standard level.
89# c89 = "ANSI" C
90# gnu89 = c89 plus GCC extensions
91# c99 = ISO C99 standard (not yet fully implemented)
92# gnu99 = c99 plus GCC extensions
93CSTANDARD = -std=gnu99
94
95
96# Place -D or -U options here for C sources
97CDEFS = -DF_CPU=$(F_CPU)UL
98CDEFS += $(OPT_DEFS)
99
100
101# Place -D or -U options here for ASM sources
102ADEFS = -DF_CPU=$(F_CPU)
103ADEFS += $(OPT_DEFS)
104
105
106# Place -D or -U options here for C++ sources
107CPPDEFS = -DF_CPU=$(F_CPU)UL
108#CPPDEFS += -D__STDC_LIMIT_MACROS
109#CPPDEFS += -D__STDC_CONSTANT_MACROS
110CPPDEFS += $(OPT_DEFS)
111
112
113
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
121CFLAGS = -g$(DEBUG)
122CFLAGS += $(CDEFS)
123CFLAGS += -O$(OPT)
124CFLAGS += -funsigned-char
125CFLAGS += -funsigned-bitfields
126CFLAGS += -ffunction-sections
1c73e574 127CFLAGS += -fdata-sections
a074364c 128CFLAGS += -fno-inline-small-functions
129CFLAGS += -fpack-struct
130CFLAGS += -fshort-enums
131CFLAGS += -fno-strict-aliasing
132CFLAGS += -Wall
133CFLAGS += -Wstrict-prototypes
134#CFLAGS += -mshort-calls
135#CFLAGS += -fno-unit-at-a-time
136#CFLAGS += -Wundef
137#CFLAGS += -Wunreachable-code
138#CFLAGS += -Wsign-compare
139CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
140CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
141CFLAGS += $(CSTANDARD)
142ifdef CONFIG_H
143 CFLAGS += -include $(CONFIG_H)
144endif
145
146
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
154CPPFLAGS = -g$(DEBUG)
155CPPFLAGS += $(CPPDEFS)
156CPPFLAGS += -O$(OPT)
157CPPFLAGS += -funsigned-char
158CPPFLAGS += -funsigned-bitfields
159CPPFLAGS += -fpack-struct
160CPPFLAGS += -fshort-enums
161CPPFLAGS += -fno-exceptions
162CPPFLAGS += -ffunction-sections
163CPPFLAGS += -fdata-sections
164# to supress "warning: only initialized variables can be placed into program memory area"
165CPPFLAGS += -w
166CPPFLAGS += -Wall
167CPPFLAGS += -Wundef
168#CPPFLAGS += -mshort-calls
169#CPPFLAGS += -fno-unit-at-a-time
170#CPPFLAGS += -Wstrict-prototypes
171#CPPFLAGS += -Wunreachable-code
172#CPPFLAGS += -Wsign-compare
173CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
174CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
175#CPPFLAGS += $(CSTANDARD)
176ifdef CONFIG_H
177 CPPFLAGS += -include $(CONFIG_H)
178endif
179
180
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.
190ASFLAGS = $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
191ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
192ifdef CONFIG_H
193 ASFLAGS += -include $(CONFIG_H)
194endif
195
196
197#---------------- Library Options ----------------
198# Minimalistic printf version
199PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
200
201# Floating point printf version (requires MATH_LIB = -lm below)
202PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
203
204# If this is left blank, then it will use the Standard printf version.
205PRINTF_LIB =
206#PRINTF_LIB = $(PRINTF_LIB_MIN)
207#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
208
209
210# Minimalistic scanf version
211SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
212
213# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
214SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
215
216# If this is left blank, then it will use the Standard scanf version.
217SCANF_LIB =
218#SCANF_LIB = $(SCANF_LIB_MIN)
219#SCANF_LIB = $(SCANF_LIB_FLOAT)
220
221
222MATH_LIB = -lm
223
224
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.
229EXTRALIBDIRS =
230
231
232
233#---------------- External Memory Options ----------------
234
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
238
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
242
243EXTMEMOPTS =
244
245
246
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
251#
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'
254#
255LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
256#LDFLAGS += -Wl,--relax
257LDFLAGS += -Wl,--gc-sections
258LDFLAGS += $(EXTMEMOPTS)
259LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
260LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
261#LDFLAGS += -T linker_script.x
262# You can give EXTRALDFLAGS at 'make' command line.
263LDFLAGS += $(EXTRALDFLAGS)
264
265
266
267#---------------- Debugging Options ----------------
268
269# For simulavr only - target MCU frequency.
270DEBUG_MFREQ = $(F_CPU)
271
272# Set the DEBUG_UI to either gdb or insight.
273# DEBUG_UI = gdb
274DEBUG_UI = insight
275
276# Set the debugging back-end to either avarice, simulavr.
277DEBUG_BACKEND = avarice
278#DEBUG_BACKEND = simulavr
279
280# GDB Init Filename.
281GDBINIT_FILE = __avr_gdbinit
282
283# When using avarice settings for the JTAG
284JTAG_DEV = /dev/com1
285
286# Debugging port used to communicate between GDB / avarice / simulavr.
287DEBUG_PORT = 4242
288
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.
292DEBUG_HOST = localhost
293
294
295
296#============================================================================
297
298
299# Define programs and commands.
300SHELL = sh
301CC = avr-gcc
302OBJCOPY = avr-objcopy
303OBJDUMP = avr-objdump
304SIZE = avr-size
305AR = avr-ar rcs
306NM = avr-nm
307REMOVE = rm -f
308REMOVEDIR = rmdir
309COPY = cp
310WINSHELL = cmd
311
312
313# Define Messages
314# English
315MSG_ERRORS_NONE = Errors: none
316MSG_BEGIN = -------- begin --------
317MSG_END = -------- end --------
318MSG_SIZE_BEFORE = Size before:
319MSG_SIZE_AFTER = Size after:
320MSG_COFF = Converting to AVR COFF:
321MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
322MSG_FLASH = Creating load file for Flash:
323MSG_EEPROM = Creating load file for EEPROM:
324MSG_EXTENDED_LISTING = Creating Extended Listing:
325MSG_SYMBOL_TABLE = Creating Symbol Table:
326MSG_LINKING = Linking:
327MSG_COMPILING = Compiling C:
328MSG_COMPILING_CPP = Compiling C++:
329MSG_ASSEMBLING = Assembling:
330MSG_CLEANING = Cleaning project:
331MSG_CREATING_LIBRARY = Creating library:
332
333
334
335
336# Define all object files.
337OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
338
339# Define all listing files.
340LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
341
342
343# Compiler flags to generate dependency files.
344#GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
345GENDEPFLAGS = -MMD -MP -MF .dep/$(subst /,_,$@).d
346
347
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
351ALL_CFLAGS = -mmcu=$(MCU) $(CFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
352ALL_CPPFLAGS = -mmcu=$(MCU) -x c++ $(CPPFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
353ALL_ASFLAGS = -mmcu=$(MCU) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
354
355
356
357
358
359# Default target.
360all: begin gccversion sizebefore build sizeafter end
361
362# Change the build target to build a HEX file or a library.
363build: elf hex eep lss sym
364#build: lib
365
366
367elf: $(TARGET).elf
368hex: $(TARGET).hex
369eep: $(TARGET).eep
370lss: $(TARGET).lss
371sym: $(TARGET).sym
372LIBNAME=lib$(TARGET).a
373lib: $(LIBNAME)
374
375
376
377# Eye candy.
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.
380begin:
381 @echo
382 @echo $(MSG_BEGIN)
383
384end:
385 @echo $(MSG_END)
386 @echo
387
388
389# Display size of file.
390HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
391#ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
392ELFSIZE = $(SIZE) $(TARGET).elf
393
394sizebefore:
395 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
396 2>/dev/null; echo; fi
397
398sizeafter:
399 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
400 2>/dev/null; echo; fi
401
402
403
404# Display compiler version information.
405gccversion :
406 @$(CC) --version
407
408
409
410# Program the device.
411program: $(TARGET).hex $(TARGET).eep
412 $(PROGRAM_CMD)
413
414teensy: $(TARGET).hex
415 teensy_loader_cli -mmcu=$(MCU) -w -v $(TARGET).hex
416
417flip: $(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
421
422dfu: $(TARGET).hex
4703afac 423 @echo -n dfu-programmer: waiting
bd68c71e 424 @until dfu-programmer $(MCU) get bootloader-version > /dev/null 2>&1; do \
e5acff03 425 echo -n "."; \
426 sleep 1; \
427 done
4703afac 428 @echo
429
a074364c 430ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
431 dfu-programmer $(MCU) erase --force
432else
433 dfu-programmer $(MCU) erase
434endif
a074364c 435 dfu-programmer $(MCU) flash $(TARGET).hex
436 dfu-programmer $(MCU) reset
437
438dfu-start:
439 dfu-programmer $(MCU) reset
440 dfu-programmer $(MCU) start
441
442flip-ee: $(TARGET).hex $(TARGET).eep
443 $(COPY) $(TARGET).eep $(TARGET)eep.hex
444 batchisp -hardware usb -device $(MCU) -operation memory EEPROM erase
445 batchisp -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(TARGET)eep.hex program
446 batchisp -hardware usb -device $(MCU) -operation start reset 0
447 $(REMOVE) $(TARGET)eep.hex
448
449dfu-ee: $(TARGET).hex $(TARGET).eep
bd24de44
JW
450ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
451 dfu-programmer $(MCU) flash --eeprom $(TARGET).eep
452else
453 dfu-programmer $(MCU) flash-eeprom $(TARGET).eep
454endif
a074364c 455 dfu-programmer $(MCU) reset
456
457
458# Generate avr-gdb config/init file which does the following:
459# define the reset signal, load the target file, connect to target, and set
460# a breakpoint at main().
461gdb-config:
462 @$(REMOVE) $(GDBINIT_FILE)
463 @echo define reset >> $(GDBINIT_FILE)
464 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
465 @echo end >> $(GDBINIT_FILE)
466 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
467 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
468ifeq ($(DEBUG_BACKEND),simulavr)
469 @echo load >> $(GDBINIT_FILE)
470endif
471 @echo break main >> $(GDBINIT_FILE)
472
473debug: gdb-config $(TARGET).elf
474ifeq ($(DEBUG_BACKEND), avarice)
475 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
476 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
477 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
478 @$(WINSHELL) /c pause
479
480else
481 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
482 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
483endif
484 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
485
486
487
488
489# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
490COFFCONVERT = $(OBJCOPY) --debugging
491COFFCONVERT += --change-section-address .data-0x800000
492COFFCONVERT += --change-section-address .bss-0x800000
493COFFCONVERT += --change-section-address .noinit-0x800000
494COFFCONVERT += --change-section-address .eeprom-0x810000
495
496
497
498coff: $(TARGET).elf
499 @echo
500 @echo $(MSG_COFF) $(TARGET).cof
501 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
502
503
504extcoff: $(TARGET).elf
505 @echo
506 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
507 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
508
509
510
511# Create final output files (.hex, .eep) from ELF output file.
512%.hex: %.elf
513 @echo
514 @echo $(MSG_FLASH) $@
515 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
516
517%.eep: %.elf
518 @echo
519 @echo $(MSG_EEPROM) $@
520 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
521 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
522
523# Create extended listing file from ELF output file.
524%.lss: %.elf
525 @echo
526 @echo $(MSG_EXTENDED_LISTING) $@
527 $(OBJDUMP) -h -S -z $< > $@
528
529# Create a symbol table from ELF output file.
530%.sym: %.elf
531 @echo
532 @echo $(MSG_SYMBOL_TABLE) $@
533 $(NM) -n $< > $@
534
535
536
537# Create library from object files.
538.SECONDARY : $(TARGET).a
539.PRECIOUS : $(OBJ)
540%.a: $(OBJ)
541 @echo
542 @echo $(MSG_CREATING_LIBRARY) $@
543 $(AR) $@ $(OBJ)
544
545
546# Link: create ELF output file from object files.
547.SECONDARY : $(TARGET).elf
548.PRECIOUS : $(OBJ)
549%.elf: $(OBJ)
550 @echo
551 @echo $(MSG_LINKING) $@
552 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
553
554
555# Compile: create object files from C source files.
556$(OBJDIR)/%.o : %.c
557 @echo
558 mkdir -p $(@D)
559 @echo $(MSG_COMPILING) $<
560 $(CC) -c $(ALL_CFLAGS) $< -o $@
561
562
563# Compile: create object files from C++ source files.
564$(OBJDIR)/%.o : %.cpp
565 @echo
566 mkdir -p $(@D)
567 @echo $(MSG_COMPILING_CPP) $<
568 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
569
570
571# Compile: create assembler files from C source files.
572%.s : %.c
573 $(CC) -S $(ALL_CFLAGS) $< -o $@
574
575
576# Compile: create assembler files from C++ source files.
577%.s : %.cpp
578 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
579
580
581# Assemble: create object files from assembler source files.
582$(OBJDIR)/%.o : %.S
583 @echo
584 mkdir -p $(@D)
585 @echo $(MSG_ASSEMBLING) $<
586 $(CC) -c $(ALL_ASFLAGS) $< -o $@
587
588
589# Create preprocessed source for use in sending a bug report.
590%.i : %.c
591 $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
592
593
594# Target: clean project.
595clean: begin clean_list end
596
597clean_list :
598 @echo
599 $(REMOVE) $(TARGET).hex
600 $(REMOVE) $(TARGET).eep
601 $(REMOVE) $(TARGET).cof
602 $(REMOVE) $(TARGET).elf
603 $(REMOVE) $(TARGET).map
604 $(REMOVE) $(TARGET).sym
605 $(REMOVE) $(TARGET).lss
606 $(REMOVE) $(OBJ)
607 $(REMOVE) $(LST)
608 $(REMOVE) $(OBJ:.o=.s)
609 $(REMOVE) $(OBJ:.o=.i)
610 $(REMOVE) -r .dep
611 $(REMOVE) -r $(OBJDIR)
612
613show_path:
614 @echo VPATH=$(VPATH)
615 @echo SRC=$(SRC)
616
617
618# Create object files directory
619$(shell mkdir $(OBJDIR) 2>/dev/null)
620
621
622# Include the dependency files.
623-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
624
625
626# Listing of phony targets.
627.PHONY : all begin finish end sizebefore sizeafter gccversion \
628build elf hex eep lss sym coff extcoff \
629clean clean_list debug gdb-config show_path \
630program teensy dfu flip dfu-ee flip-ee dfu-start
Imprint / Impressum