summaryrefslogtreecommitdiff
path: root/mk/avr.rules.mk
blob: 97d9fdf2e313e8c6837ad1e69146c8d20021f337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Be silent per default, but 'make V=1' will show all compiler calls.
ifneq ($(V),1)
Q	:= @
NULL	:= 2>/dev/null
else
QP	:= \#
endif

###############################################################################
# Executables

CC		= avr-gcc
LD		= avr-gcc
OBJCOPY		= avr-objcopy
OBJDUMP		= avr-objdump
SIZE		= avr-size
NM		= avr-nm
AVRDUDE		= avrdude
RM		= rm -f
COPY		= cp

OPT		:= -Os
CSTD		?= -std=gnu11

###############################################################################
# Define all object files.

OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)

###############################################################################
# C flags

TGT_CFLAGS	+= -mmcu=$(MCU) $(OPT) $(CSTD) -g
TGT_CFLAGS	+= $(ARCH_FLAGS)
TGT_CFLAGS	+= -Wextra -Wshadow -Wimplicit-function-declaration -Wimplicit-fallthrough=1
TGT_CFLAGS	+= -Wredundant-decls -Wstrict-prototypes
TGT_CFLAGS	+= -fshort-enums -funsigned-bitfields
TGT_CFLAGS	+= -fno-common -ffunction-sections -fdata-sections
TGT_CFLAGS	+= -mrelax

###############################################################################
# C & C++ preprocessor common flags

TGT_CPPFLAGS	+= -MMD -MP -MF .dep/$(@F:%.o=%.d)
TGT_CPPFLAGS	+= -Wall -Wundef
TGT_CPPFLAGS	+= -DF_CPU=$(F_CPU)UL $(CDEFS) $(CINCS)

###############################################################################
# Linker flags

TGT_LDFLAGS		+= $(ARCH_FLAGS)
TGT_LDFLAGS		+= -Wl,-Map=$(TARGET).map,--cref
TGT_LDFLAGS		+= -Wl,--gc-sections

###############################################################################
# Avrdude

AVRDUDE_PORT ?= usb
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE)

###############################################################################

.SUFFIXES: .elf .hex .lss .map .sym
.SECONDARY:

.PHONY: elf hex lss sym size
elf: $(TARGET).elf
hex: $(TARGET).hex
lss: $(TARGET).lss
sym: $(TARGET).sym

size: $(TARGET).elf
	@#$(QP)printf "  SIZE $(TARGET).elf\n"
	$(Q)$(SIZE) $(TARGET).elf

# Program the device.
.PHONY: flash fuses
flash: $(TARGET).hex
	$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)

fuses:
	$(AVRDUDE) $(AVRDUDE_FLAGS) -u -U hfuse:w:$(HFUSE):m -U lfuse:w:$(LFUSE):m

%.hex: %.elf
	@$(QP)printf "  OBJCOPY $(*).hex\n"
	$(Q)$(OBJCOPY) -Oihex -R .eeprom $< $@

# Create extended listing file from ELF output file.
%.lss: %.elf
	@$(QP)printf "  OBJDUMP $@\n"
	$(Q)$(OBJDUMP) -h -S $< > $@

# Create a symbol table from ELF output file.
%.sym: %.elf
	@$(QP)printf "  NM $@\n"
	$(Q)$(NM) -n $< > $@

# Link: create ELF output file from object files.
%.elf %.map: $(OBJ)
	@$(QP)printf "  LD      $@\n"
	$(Q)$(LD) $(TGT_CFLAGS) $(CFLAGS) $(TGT_LDFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@


# Compile: create object files from C source files.
%.o: %.c
	@$(QP)printf "  CC      $<\n"
	$(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $<

# Target: clean project.
.PHONY:	clean distclean dist
clean:
	@$(QP)printf "  CLEAN\n"
	$(Q)$(RM) $(OBJ) $(SRC:.c=.s) .dep/* $(TARGET).elf \
	$(TARGET).hex $(TARGET).lss $(TARGET).map $(TARGET).sym

distclean: clean
	@$(QP)printf "  DISTCLEAN\n"
	$(Q)$(RM) $(TARGET).tar.gz

dist: build
	tar cvfz $(TARGET).tar.gz \
	$(SRC) \
	$(TARGET).hex $(TARGET).eep

# Include the dependency files.
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)