aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 45757d5ad982e0400a5df9baf4b54d060bdbbd3f (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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
# CPU model
#
MCU=atmega2560


# CPU Frequency
#
F_CPU=16000000L

# App
#
APP?=template
include apps/$(APP)/app.mk


# Hardware modules
#
MODULES?=


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


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


# Toolchain flags
#
CC=avr-gcc
CFLAGS= -std=gnu99 -O2 -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=-O ihex -R .eeprom
AVRDUDE=avrdude
AVRDUDEFLAGS= -DV -p $(MCU) -P $(SERIAL) -c stk500v2 -b 115200
AVRSIZE=avr-size
CPP=avr-g++
CPPFLAGS= -O2 -Wall -fno-exceptions -ffunction-sections -fdata-sections -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -mmcu=$(MCU) -DF_CPU=$(F_CPU)
GDBINITFILE=gdb.conf

# Derived variables
SOURCEDIRS=\
	kernel \
	kernel/io \
	kernel/sched \
	kernel/mcu/$(MCU) \
	$(foreach module,$(MODULES),modules/$(module)) \
	$(foreach module,$(MODULES),modules/$(module)/mcu/$(MCU)) \
	apps/$(APP)

INCLUDES=\
	kernel/include \
	kernel/mcu/$(MCU)/include \
	$(foreach module,$(MODULES),modules/$(module)/include)

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) $< $@

$(TARGET).elf: $(OBJECTS)
	$(LD) $(LDFLAGS) -o $@ $^
	
# Kernel sources
kernel/%.o: kernel/%.S
	$(CC) $(CFLAGS) -o $@ -c $<
	
kernel/%.o: kernel/%.c
	$(CC) $(CFLAGS) -I$(dir $<) $(addprefix -I, $(INCLUDES)) -o $@ -c $<
	
# Local sources
%.o: %.s
	$(CC) $(CFLAGS) -I$(dir $<) $(addprefix -I, $(INCLUDES)) -o $@ -c $<
	
%.o: %.c
	$(CC) $(CFLAGS) -I$(dir $<) $(addprefix -I, $(INCLUDES)) -o $@ -c $<
	
%.o: %.cpp
	$(CPP) $(CPPFLAGS) -I$(dir $<) $(addprefix -I, $(INCLUDES)) -o $@ -c $<

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

upload: target
	$(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i
	
monitor:
	cu -l $(SERIAL) -s $(BAUD) --parity=none -h

cycle:
	@make clean && \
	make > /dev/null && \
	make size && \
	read -p "Press enter to proceed with upload..." nothing && \
	make upload

clean:
	@rm -f *.o
	@for dir in $(SOURCEDIRS); do \
		rm -f $$dir/*.o; \
	done
	@rm -f $(TARGET).hex
	@rm -f $(TARGET).elf
	@rm -f $(GDBINITFILE)
	
# Debugging
ddd: gdbinit
	ddd --debugger "avr-gdb -x $(GDBINITFILE)"

gdbserver: gdbinit
#	run_avr -g -m $(MCU) -f $(F_CPU)
	simulavr -p 1234 -d $(MCU) -F 16000000 -f $(TARGET).elf -g

gdbinit: $(GDBINITFILE)

$(GDBINITFILE): $(TARGET).elf
	@echo "file $(TARGET).elf" > $(GDBINITFILE)
	@echo "target remote localhost:1234" >> $(GDBINITFILE)
	@echo "load" >> $(GDBINITFILE) 
	@echo "break main.c:main" >> $(GDBINITFILE)
	@echo
	@echo "Use 'avr-gdb -x $(GDBINITFILE)'"

	
.PHONY: clean cycle target