Subversion Repositories HomeAutomation

Rev

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