Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
801 linlun 1
# Paths: default pathnames if they are not
2
# passed on from parent Makefile.
3
ifndef TARGET
4
TARGET = main
5
endif
6
ifndef LIBDIR
7
LIBDIR = ../../../avr-lib
8
endif
817 migan 9
ifndef MODDIR
10
MODDIR = ../modules
11
endif
801 linlun 12
ifndef SRCDIR
13
SRCDIR = ../src
14
endif
15
ifndef BIOSDIR
16
BIOSDIR = ../bios
17
endif
18
ifndef CFGDIR
19
CFGDIR = ..
20
endif
21
 
22
include $(CFGDIR)/bios.inc
23
 
24
# VPATH: setup search path for source files.
25
vpath
817 migan 26
vpath %.c $(LIBDIR):$(SRCDIR):$(MODDIR)
801 linlun 27
 
28
# List source files here. (C dependencies are automatically generated.)
817 migan 29
SOURCES +=	main.c\
30
		drivers/timer/timer.c\
31
		drivers/mcu/mcu.c\
32
		drivers/can/stdcan.c\
801 linlun 33
 
817 migan 34
include $(CFGDIR)/modules.inc
801 linlun 35
 
36
# Optimization level, can be [0, 1, 2, 3, s]. 
37
# 0 = turn off optimization. s = optimize for size.
38
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
39
OPT = s
40
#OPT = 3
41
 
42
# Debugging format.
43
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
44
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
45
DEBUG = stabs
46
#DEBUG = dwarf-2
47
 
48
# List any extra directories to look for include files here.
49
# Each directory must be separated by a space.
50
EXTRAINCDIRS = $(LIBDIR) $(BIOSDIR)
51
 
52
# Compiler flag to set the C Standard level.
53
# c89   - "ANSI" C
54
# gnu89 - c89 plus GCC extensions
55
# c99   - ISO C99 standard (not yet fully implemented)
56
# gnu99 - c99 plus GCC extensions
57
CSTANDARD = -std=gnu99
58
 
59
# Place -D or -U options here
60
CDEFS =
61
 
62
# Place -I options here
63
CINCS =
64
 
65
 
66
# Compiler flags.
67
#  -g*:          generate debugging information
68
#  -O*:          optimization level
69
#  -f...:        tuning, see GCC manual and avr-libc documentation
70
#  -Wall...:     warning level
71
#  -Wa,...:      tell GCC to pass this to the assembler.
72
#    -adhlns...: create assembler listing
73
CFLAGS = -mmcu=$(MCU) -I.
74
CFLAGS += -g$(DEBUG)
75
CFLAGS += $(CDEFS) $(CINCS)
76
CFLAGS += -O$(OPT)
77
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
78
CFLAGS += -Wall -Wstrict-prototypes
79
CFLAGS += -Wa,-adhlns=$(@:.o=.lst)
80
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
81
CFLAGS += $(CSTANDARD)
82
CFLAGS += $(GENDEPFLAGS)
83
 
84
 
85
 
86
# Assembler flags.
87
#  -Wa,...:   tell GCC to pass this to the assembler.
88
#  -ahlms:    create listing
89
#  -gstabs:   have the assembler create line number information; note that
90
#             for use in COFF files, additional information about filenames
91
#             and function names needs to be present in the assembler source
92
#             files -- see avr-libc docs [FIXME: not yet described there]
93
##ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs 
94
ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp
95
ASFLAGS += -Wa,-adhlns=$(@:.o=.lst),-g$(DEBUG)
96
 
97
 
98
#Additional libraries.
99
 
100
# Minimalistic printf version
101
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
102
 
103
# Floating point printf version (requires MATH_LIB = -lm below)
104
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
105
 
106
PRINTF_LIB = 
107
 
108
# Minimalistic scanf version
109
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
110
 
111
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
112
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
113
 
114
SCANF_LIB = 
115
 
116
MATH_LIB = -lm
117
 
118
# External memory options
119
 
120
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
121
# used for variables (.data/.bss) and heap (malloc()).
122
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
123
 
124
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
125
# only used for heap (malloc()).
126
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
127
 
128
EXTMEMOPTS =
129
 
130
BIOSOPTS = -Wl,-L$(BIOSDIR),-Tdata,$(BIOS_RAM_END) -lbios
131
 
132
# Linker flags.
133
#  -Wl,...:     tell GCC to pass this to linker.
134
#    -Map:      create map file
135
#    --cref:    add cross reference to  map file
136
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
137
LDFLAGS += $(EXTMEMOPTS)
138
LDFLAGS += $(BIOSOPTS)
139
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
140
 
141
# ---------------------------------------------------------------------------
142
 
143
# Define programs and commands.
144
SHELL = sh
145
CC = avr-gcc
146
AWK = awk
147
OBJCOPY = avr-objcopy
148
OBJDUMP = avr-objdump
149
SIZE = avr-size
150
NM = avr-nm
151
RM = rm -f
152
CP = cp
153
MKDIR = mkdir
154
 
155
 
156
 
157
# Define Messages
158
# English
159
MSG_ERRORS_NONE = Errors: none
160
MSG_BEGIN = -------- begin --------
161
MSG_END = --------  end  --------
162
MSG_SIZE_BEFORE = Size before: 
163
MSG_SIZE_AFTER = Size after:
164
MSG_COFF = Converting to AVR COFF:
165
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
166
MSG_FLASH = Creating load file for Flash:
167
MSG_EEPROM = Creating load file for EEPROM:
168
MSG_EXTENDED_LISTING = Creating Extended Listing:
169
MSG_SYMBOL_TABLE = Creating Symbol Table:
170
MSG_LINKING = Linking:
171
MSG_COMPILING = Compiling:
172
MSG_ASSEMBLING = Assembling:
173
MSG_DEPEND = Generating dependencies:
174
MSG_CLEANING = Cleaning project:
175
MSG_CREATE = Creating:
176
 
177
# Define all object files.
178
OBJ = $(filter %.o,$(SOURCES:.c=.o) $(SOURCES:.S=.o))
179
 
180
# Define all listing files.
181
LST = $(OBJ:.o=.lst)
182
 
183
# Define all dependency files.
184
DEP = $(OBJ:.o=.d)
185
 
186
BUILDDIRS = $(sort $(filter-out ./,$(dir $(OBJ))))
187
 
188
BIOS_RAM_END := $(shell $(AWK) '/0x[0-9a-f]+.*__bios_ram_end/ { print $$1 }' $(BIOSDIR)/bios.map)
189
 
190
# Compiler flags to generate dependency files.
191
GENDEPFLAGS = -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)"
192
 
193
# Default target.
194
all: sizebefore buildtree config.h build sizeafter
195
 
196
build: elf hex eep lss sym
197
 
198
elf: $(TARGET).elf
199
hex: $(TARGET).hex
200
eep: $(TARGET).eep
201
lss: $(TARGET).lss 
202
sym: $(TARGET).sym
203
 
204
buildtree: $(BUILDDIRS)
205
 
206
# Display size of file.
207
HEXSIZE = $(SIZE) --target=ihex $(TARGET).hex
208
ELFSIZE = $(SIZE) $(TARGET).elf
209
sizebefore:
210
	@if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
211
 
212
sizeafter:
213
	@if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
214
 
215
# Generate config.h from bios.inc and config.inc
216
config.h: $(CFGDIR)/bios.inc $(CFGDIR)/config.inc
217
	@echo Generating $@
218
	@echo "/* This file is automatically generated. Do not edit. */" > $@
219
	@echo "" >> $@
220
	@cat $^ | $(AWK) 'BEGIN { FS="="; print "#ifndef CONFIG_H_\n#define CONFIG_H_\n" } /^[^#].*=.*/ { print "#define "$$1,$$2 } END { print "\n#endif /*CONFIG_H_*/" }' >> $@
221
 
222
$(CFGDIR)/config.inc: $(SRCDIR)/config.inc.template
223
	@if [ ! -f $@ ]; then cp $< $@; fi
224
 
225
# Display compiler version information.
226
gccversion : 
227
	@$(CC) --version
228
 
229
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
230
COFFCONVERT=$(OBJCOPY) --debugging \
231
--change-section-address .data-0x800000 \
232
--change-section-address .bss-0x800000 \
233
--change-section-address .noinit-0x800000 \
234
--change-section-address .eeprom-0x810000 
235
 
236
 
237
coff: $(TARGET).elf
238
	@echo $(MSG_COFF) $(TARGET).cof
239
	@$(COFFCONVERT) -O coff-avr $< $(TARGET).cof
240
 
241
extcoff: $(TARGET).elf
242
	@echo $(MSG_EXTENDED_COFF) $(TARGET).cof
243
	@$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
244
 
245
# Create final output files (.hex, .eep) from ELF output file.
246
%.hex: %.elf
247
	@echo $(MSG_FLASH) $@
248
	@$(OBJCOPY) -j .text -j .data -O ihex $< $@
249
 
250
%.eep: %.elf
251
	@echo $(MSG_EEPROM) $@
252
	-@$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
253
	--change-section-lma .eeprom=0 -O ihex $< $@
254
 
255
# Create extended listing file from ELF output file.
256
%.lss: %.elf
257
	@echo $(MSG_EXTENDED_LISTING) $@
258
	@$(OBJDUMP) -h -S $< > $@
259
 
260
# Create a symbol table from ELF output file.
261
%.sym: %.elf
262
	@echo $(MSG_SYMBOL_TABLE) $@
263
	@$(NM) -n $< > $@
264
 
265
# Link: create ELF output file from object files.
266
.SECONDARY : $(TARGET).elf
267
.PRECIOUS : $(OBJ)
268
ifneq (,$(filter atmega88 atmega168, $(MCU)))
817 migan 269
#main.elf: $(OBJ)
801 linlun 270
%.elf: $(OBJ)
271
	@echo $(MSG_LINKING) $@
272
	@echo GCC bug workaround for ATmega88/ATmega168:
273
	@eval $$($(CC) -v $(CFLAGS) -o $@ $^ $(LDFLAGS) 2>&1 | $(AWK) '{ if (gsub("0x800100","$(BIOS_RAM_END)") == 1) print }')
274
else
275
%.elf: $(OBJ)
276
	@echo $(MSG_LINKING) $@
277
	@$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
278
endif
279
 
280
# Compile: create object files from C source files.
281
%.o : %.c
282
	@echo $(MSG_COMPILING) $<
283
	@$(CC) -c $(CFLAGS) $< -o $@ 
284
 
285
# Compile: create assembler files from C source files.
286
%.s : %.c
287
	@$(CC) -S $(CFLAGS) $< -o $@
288
 
289
# Assemble: create object files from assembler source files.
290
%.o : %.S
291
	@echo $(MSG_ASSEMBLING) $<
292
	$(CC) -c $(ASFLAGS) $< -o $@
293
 
294
# Depends: create dependency files from c source files.
295
#%.d : %.c | $(BUILDDIRS)
296
#	@echo $(MSG_DEPEND) $<
297
#	@$(CC) -c $(CFLAGS) $(GENDEPFLAGS) $<
298
 
299
# Buildtree: create directories for output files.
300
$(BUILDDIRS) :
301
	@echo $(MSG_CREATE) $@
302
	@$(MKDIR) -p $@
303
 
304
# Target: clean project.
305
clean: 
306
	@echo $(MSG_CLEANING)
307
	$(RM) $(TARGET).hex
308
	$(RM) $(TARGET).eep
309
	$(RM) $(TARGET).cof
310
	$(RM) $(TARGET).elf
311
	$(RM) $(TARGET).map
312
	$(RM) $(TARGET).sym
313
	$(RM) $(TARGET).lss
314
	$(RM) $(OBJ)
315
	$(RM) $(DEP)
316
	$(RM) $(LST)
317
	$(RM) config.h
318
 
319
distclean: clean
320
	$(RM) -r $(BUILDDIRS)
321
 
322
# Include the dependency files.
323
ifneq ($(MAKECMDGOALS),clean)
324
ifneq ($(strip $(DEP)),)
325
-include $(DEP)
326
endif
327
endif
328
 
329
# Listing of phony targets.
330
.PHONY : all sizebefore sizeafter build \
331
elf hex eep lss sym coff extcoff clean \
332
distclean gccversion
333
 
817 migan 334
printsources:
335
	@echo $(SOURCES)
336
	@echo $(OBJ)
337
	@echo $(BUILDDIRS)