summaryrefslogtreecommitdiffstats
path: root/firmware/Makefile
blob: 8b31dff917772d0768497e733b5f24f52602a2ba (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Project name
NAME			:= moistcontrol

# Project source files
SRCS			:= comm.c \
			   controller.c \
			   datetime.c \
			   log.c \
			   main.c \
			   ioext.c \
			   pcf8574.c \
			   rv3029.c \
			   sensor.c \
			   twi_master.c \
			   twi_master_sync.c \
			   util.c

# Project fuse bits
LFUSE			:= 0xA0
HFUSE			:= 0xD9

# CPU speed, in Hz
F_CPU			:= 16000000ul

# Architecture configuration
GCC_ARCH		:= atmega8
AVRDUDE_ARCH		:= m8
AVRDUDE_SPEED		:= 1
AVRDUDE_SLOW_SPEED	:= 100

# Programmer selection.
# Values can be:  avrisp2, mysmartusb
PROGRAMMER		:= avrisp2

# Additional compiler flags
CFLAGS			:= -DTWI_SCL_HZ=100000ul \
			   -DCOMM_BAUDRATE=19200ul \
			   -DCOMM_PAYLOAD_LEN=12
LDFLAGS			:=

# Additional "clean" and "distclean" target files
CLEAN_FILES		:=
DISTCLEAN_FILES		:=


# =============================================================================
# =============================================================================
# =============================================================================


BINEXT			:=
NODEPS			:=

# The toolchain definitions
CC			= avr-gcc$(BINEXT)
OBJCOPY			= avr-objcopy$(BINEXT)
OBJDUMP			= avr-objdump$(BINEXT)
SIZE			= avr-size$(BINEXT)
MKDIR			= mkdir$(BINEXT)
MV			= mv$(BINEXT)
RM			= rm$(BINEXT)
CP			= cp$(BINEXT)
ECHO			= echo$(BINEXT)
GREP			= grep$(BINEXT)
TRUE			= true$(BINEXT)
TEST			= test$(BINEXT)
AVRDUDE			= avrdude$(BINEXT)
MYSMARTUSB		= mysmartusb.py
DOXYGEN			= doxygen$(BINEXT)

V			:= @		# Verbose build:  make V=1
O			:= s		# Optimize flag
Q			:= $(V:1=)
QUIET_CC		= $(Q:@=@$(ECHO) '     CC       '$@;)$(CC)
QUIET_DEPEND		= $(Q:@=@$(ECHO) '     DEPEND   '$@;)$(CC)
QUIET_OBJCOPY		= $(Q:@=@$(ECHO) '     OBJCOPY  '$@;)$(OBJCOPY)
QUIET_SIZE		= $(Q:@=@$(ECHO) '     SIZE     '$@;)$(SIZE)

WARN_CFLAGS		= -Wall -Wextra -Wno-unused-parameter -Wswitch-enum \
			  -Wsuggest-attribute=noreturn \
			  -Wundef -Wpointer-arith -Winline \
			  -Wstack-usage=192 \
			  -Wcast-qual -Wlogical-op

CFLAGS			+= -mmcu=$(GCC_ARCH) -std=c99 -g -O$(O) $(WARN_CFLAGS) \
			  "-Dinline=inline __attribute__((__always_inline__))" \
			  -fshort-enums -DF_CPU=$(F_CPU) \
			  -flto

BIN			:= $(NAME).bin
HEX			:= $(NAME).hex
EEP			:= $(NAME).eep.hex

.SUFFIXES:
.DEFAULT_GOAL := all

# Programmer parameters
ifeq ($(PROGRAMMER),mysmartusb)
AVRDUDE_PROGRAMMER	:= avr910
PROGPORT		:= /dev/ttyUSB0
endif
ifeq ($(PROGRAMMER),avrisp2)
AVRDUDE_PROGRAMMER	:= avrisp2
PROGPORT		:= usb
endif

ifeq ($(AVRDUDE_PROGRAMMER),)
$(error Invalid PROGRAMMER specified)
endif

PROGRAMMER_CMD_PWRCYCLE := \
	$(if $(filter mysmartusb,$(PROGRAMMER)), \
		$(MYSMARTUSB) -p0 $(PROGPORT) && \
		sleep 1 && \
		$(MYSMARTUSB) -p1 $(PROGPORT) \
	)

PROGRAMMER_CMD_PROG_ENTER := \
	$(if $(filter mysmartusb,$(PROGRAMMER)), \
		$(MYSMARTUSB) -mp $(PROGPORT) \
	)

PROGRAMMER_CMD_PROG_LEAVE := \
	$(if $(filter mysmartusb,$(PROGRAMMER)), \
		$(MYSMARTUSB) -md $(PROGPORT) \
	)

DEPS = $(sort $(patsubst %.c,dep/%.d,$(1)))
OBJS = $(sort $(patsubst %.c,obj/%.o,$(1)))

# Generate dependencies
$(call DEPS,$(SRCS)): dep/%.d: %.c 
	@$(MKDIR) -p $(dir $@)
	@$(MKDIR) -p obj
	$(QUIET_DEPEND) -o $@.tmp -MM -MT "$@ $(patsubst dep/%.d,obj/%.o,$@)" $(CFLAGS) $<
	@$(MV) -f $@.tmp $@

ifeq ($(NODEPS),)
-include $(call DEPS,$(SRCS))
endif

# Generate object files
$(call OBJS,$(SRCS)): obj/%.o: %.c
	@$(MKDIR) -p $(dir $@)
	$(QUIET_CC) -o $@ -c $(CFLAGS) $<

all: $(HEX)

%.s: %.c
	$(QUIET_CC) $(CFLAGS) -S $*.c

$(BIN): $(call OBJS,$(SRCS))
	$(QUIET_CC) $(CFLAGS) -o $(BIN) -fwhole-program $(call OBJS,$(SRCS)) $(LDFLAGS)

$(HEX): $(BIN)
	$(QUIET_OBJCOPY) -R.eeprom -O ihex $(BIN) $(HEX)
	@$(if $(filter .exe,$(BINEXT)),$(TRUE), \
	$(OBJDUMP) -h $(BIN) | $(GREP) -qe .eeprom && \
	 $(OBJCOPY) -j.eeprom --set-section-flags=.eeprom="alloc,load" \
	 --change-section-lma .eeprom=0 -O ihex $(BIN) $(EEP) \
	 || $(TRUE))
	$(QUIET_SIZE) $(BIN)

avrdude:
	$(call PROGRAMMER_CMD_PROG_ENTER)
	$(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
	 -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) -t
	$(call PROGRAMMER_CMD_PWRCYCLE)
	$(call PROGRAMMER_CMD_PROG_LEAVE)

install: all
	$(call PROGRAMMER_CMD_PROG_ENTER)
	$(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
	 -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
	 -U flash:w:$(HEX)
	$(TEST) -r $(EEP) && ( \
	 $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
	  -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
	  -U eeprom:w:$(EEP) \
	) || $(TRUE)
	$(call PROGRAMMER_CMD_PWRCYCLE)
	$(call PROGRAMMER_CMD_PROG_LEAVE)

reset:
	$(call PROGRAMMER_CMD_PROG_ENTER)
	$(AVRDUDE) -B $(AVRDUDE_SLOW_SPEED) -p $(AVRDUDE_ARCH) \
	 -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
	 -U signature:r:/dev/null:i -q -q
	$(call PROGRAMMER_CMD_PWRCYCLE)

writefuse:
	$(call PROGRAMMER_CMD_PROG_ENTER)
	$(AVRDUDE) -B $(AVRDUDE_SLOW_SPEED) -p $(AVRDUDE_ARCH) \
	 -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) -q -q \
	 -U lfuse:w:$(LFUSE):m \
	 -U hfuse:w:$(HFUSE):m
	$(call PROGRAMMER_CMD_PWRCYCLE)
	$(call PROGRAMMER_CMD_PROG_LEAVE)

doxygen:
	$(DOXYGEN) Doxyfile

clean:
	-$(RM) -rf obj dep $(BIN) $(CLEAN_FILES)

distclean: clean
	-$(RM) -f $(HEX) $(EEP) $(DISTCLEAN_FILES)
	-$(RM) -f $(if $(filter .exe,$(BINEXT)),$(patsubst %.c,%.s,$(SRCS)),*.s)
bues.ch cgit interface