aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 881932124c7094cc0687b4171b389190a431bf99 (plain) (blame)
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
##
## Makefile to create firmware image based on mux
##
##

# CPU model
#
MCU=attiny45


# CPU Frequency
#
F_CPU=10000000L


# Serial port used for uploading
#
SERIAL=/dev/ttyACM1
BAUD=115200


# Name of image to produce (can be arbitrary)
#
TARGET=firmware


# Toolchain and flags
#
CC=avr-gcc
CFLAGS= -g -std=gnu99 -Os -Wall -finline-functions -ffunction-sections -fdata-sections -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -mmcu=$(MCU) -DF_CPU=$(F_CPU)

LD=avr-gcc
LDFLAGS= -Wl,--gc-sections,--relax -mmcu=$(MCU)
AR=avr-ar
AS=avr-as
ASFLAGS=-mmcu=$(MCU)
OC=avr-objcopy
OCFLAGS=
OD=avr-objdump
AVRDUDE=avrdude
AVRDUDEFLAGS= -p $(MCU) -P S(SERIAL) -b 19200 -c avrisp
AVRSIZE=avr-size

# Derived variables
SOURCEDIRS=. sys

INCLUDES=. sys

SOURCES=\
	$(foreach dir,$(SOURCEDIRS),$(wildcard $(dir)/*.c)) \
	$(foreach dir,$(SOURCEDIRS),$(wildcard $(dir)/*.S))

OBJECTS=$(addsuffix .o, $(basename $(SOURCES)))

# Rules
all: $(TARGET).hex

$(TARGET).hex: $(TARGET).elf
	$(OC) $(OCFLAGS) -O ihex -R .eeprom $< $@

$(TARGET).elf: $(OBJECTS)
	$(LD) $(LDFLAGS) -o $@ $^

# Sources
%.o: %.S
	$(CC) $(CFLAGS) -I$(dir $<) $(addprefix -I, $(INCLUDES)) -o $@ -c $<

%.o: %.c
	$(CC) $(CFLAGS) -I$(dir $<) $(addprefix -I, $(INCLUDES)) -o $@ -c $<

# Utility rules
size: $(TARGET).hex
	$(AVRSIZE) --format=avr --mcu=$(MCU) $(TARGET).elf

upload: $(TARGET).hex
	$(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i

clean:
	@rm -f *.o
	@for dir in $(SOURCEDIRS); do \
		rm -f $$dir/*.o; \
		rm -f $$dir/*.s-gen; \
	done
	@rm -f $(TARGET).hex
	@rm -f $(TARGET).elf

.PHONY: clean