Subversion Repositories HomeAutomation

Rev

Rev 810 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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