trennfix/sw: Added smokefix, further modularized the mm decoder
[eisenbahn.git] / trennfix / sw / Makefile
CommitLineData
54de37bf 1
f12652df 2#######################################
54de37bf 3
f12652df
PH
4HW?=trennfix_0.4
5PROG?=smokefix
54de37bf 6
f12652df 7#######################################
54de37bf 8
54de37bf 9
f12652df
PH
10#######################################
11# VERBOSITY CONTROL
12ifeq ($(V),)
13Q=@
14else
15Q=
16endif
54de37bf 17
f12652df
PH
18include mk/hw/${HW}.mk
19include mk/prog/*.mk
54de37bf 20
f12652df
PH
21INCLUDES+=-I mm/include -I include -I.
22CFLAGS+=-D__HW_CONF_HEADER__="<config/${CONFIG}.h>"
54de37bf 23
f12652df
PH
24CFLAGS += -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL
25CFLAGS += -std=gnu99
54de37bf
PH
26CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
27CFLAGS += -Wall -Wstrict-prototypes
f12652df 28CFLAGS += -Os -gdwarf-2
54de37bf 29CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
f12652df 30CFLAGS += -MD $(@:.c:.d)
93cb14d4 31CFLAGS += $(INCLUDES)
54de37bf 32
f12652df
PH
33ASFLAGS += -Wa,-adhlns=$(<:.S=.lst),-gstabs -mmcu=$(MCU)
34ASFLAGS += -x assembler-with-cpp
54de37bf
PH
35
36#---------------- Library Options ----------------
37# Minimalistic printf version
38PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
39
40# Floating point printf version (requires MATH_LIB = -lm below)
41PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
42
43# If this is left blank, then it will use the Standard printf version.
44PRINTF_LIB =
45#PRINTF_LIB = $(PRINTF_LIB_MIN)
46#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
47
54de37bf
PH
48# Minimalistic scanf version
49SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
50
51# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
52SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
53
54# If this is left blank, then it will use the Standard scanf version.
55SCANF_LIB =
56#SCANF_LIB = $(SCANF_LIB_MIN)
57#SCANF_LIB = $(SCANF_LIB_FLOAT)
58
f12652df
PH
59LDFLAGS = -Wl,-Map=$(@:.elf=.map),--cref
60LIBS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
54de37bf
PH
61
62#---------------- Programming Options (avrdude) ----------------
63
64# Programming hardware: alf avr910 avrisp bascom bsd
65# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
66#
67# Type: avrdude -c ?
68# to get a full listing.
69#
54de37bf
PH
70AVRDUDE_PROGRAMMER ?= avrisp2
71
54de37bf
PH
72# com1 = serial port. Use lpt1 to connect to parallel port.
73AVRDUDE_PORT ?= usb
74
54de37bf
PH
75AVRDUDE_WRITE_FUSES = -U lfuse:w:${LFUSE}:m -U hfuse:w:${HFUSE}:m \
76 -U efuse:w:${EFUSE}:m
77
78# Uncomment the following if you want avrdude's erase cycle counter.
79# Note that this counter needs to be initialized first using -Yn,
80# see avrdude manual.
81#AVRDUDE_ERASE_COUNTER = -y
82
83# Uncomment the following if you do /not/ wish a verification to be
84# performed after programming the device.
85# AVRDUDE_NO_VERIFY = -V
86
87# Increase verbosity level. Please use this when submitting bug
88# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
89# to submit bug reports.
90#AVRDUDE_VERBOSE = -v -v
91
92AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
93AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
94AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
95AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
f12652df 96AVRDUDE_FLAGS += -B 5
54de37bf
PH
97
98# Define programs and commands.
99SHELL = sh
100CC = avr-gcc
101OBJCOPY = avr-objcopy
102OBJDUMP = avr-objdump
103SIZE = avr-size
104NM = avr-nm
105AVRDUDE = avrdude
f12652df 106RM = rm -rf
54de37bf 107
f12652df
PH
108define SELECT_DEFAULT_template =
109elf: $(1).elf
110hex: $(1).hex
111eep: $(1).eep
112lss: $(1).lss
113sym: $(1).sym
114clean: $(1)_clean
115build: $(1)_build
116program: $(1)_program
117fuses: $(1)_fuses
118endef
54de37bf 119
f12652df 120all: build
54de37bf 121
f12652df
PH
122cleanall: $(foreach prog, $(PROGRAMS), $(prog)_clean)
123buildall: $(foreach prog, $(PROGRAMS), $(prog)_build)
54de37bf 124
f12652df 125.PHONY: clean build program cleanall buildall all
54de37bf 126
f12652df 127$(eval $(call SELECT_DEFAULT_template, $(PROG)))
54de37bf
PH
128
129# Create final output files (.hex, .eep) from ELF output file.
130%.hex: %.elf
f12652df
PH
131 @echo HEX $@
132 $(Q)$(OBJCOPY) -O ihex -R .fuse -R .eeprom $< $@
54de37bf
PH
133
134%.eep: %.elf
f12652df
PH
135 @echo EEP $@
136 $(Q)$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
137 --change-section-lma .eeprom=0 -O ihex $< $@
54de37bf
PH
138
139# Create extended listing file from ELF output file.
140%.lss: %.elf
f12652df
PH
141 @echo LSS $@
142 $(Q)$(OBJDUMP) -h -S $< > $@
54de37bf
PH
143
144# Create a symbol table from ELF output file.
145%.sym: %.elf
f12652df
PH
146 @echo SYM $@
147 $(Q)$(NM) -n $< > $@
54de37bf
PH
148
149# Link: create ELF output file from object files.
54de37bf 150%.elf: $(OBJ)
f12652df
PH
151 @echo LD $@
152 $(Q)$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
54de37bf
PH
153
154# Compile: create object files from C source files.
155%.o : %.c Makefile
f12652df
PH
156 @echo CC $@
157 $(Q)$(CC) -c $(CFLAGS) $< -o $@
54de37bf
PH
158
159# Compile: create assembler files from C source files.
160%.s : %.c Makefile
f12652df
PH
161 @echo CC-ASM $<
162 $(Q)$(CC) -S $(CFLAGS) $< -o $@
54de37bf
PH
163
164# Assemble: create object files from assembler source files.
165%.o : %.S
f12652df
PH
166 @echo ASM $@
167 $(Q)$(CC) -c $(ASFLAGS) $< -o $@
54de37bf 168
f12652df
PH
169########################### The big template for each program ##################
170define PROGRAM_template=
54de37bf 171
f12652df
PH
172# Determine program's object files
173$(1)_OBJS=$$($(1)_SRC:.c=.o)
174$(1)_OBJS+=$$($(1)_ASRC:.S=.o)
54de37bf 175
f12652df
PH
176# Collect all listing files - for cleanup
177$(1)_LST += $$($(1)_SRC:.c=.lst) $$($(1)_ASRC:.S=.lst)
54de37bf 178
f12652df
PH
179$(1): $(1).elf
180$(1).elf: $$($(1)_OBJS)
54de37bf 181
f12652df
PH
182$(1)_program : $(1).hex $(1).eep
183 @echo PROG $(1)
184 $(Q)$(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(1).hex -U eeprom:w:$(1).eep
54de37bf 185
f12652df
PH
186$(1)_unfug:
187 @echo UNFUG $$@
54de37bf 188
f12652df
PH
189$(1)_fuses.bin: $(1).elf
190 @echo OBJCOPY $$@
191 $(Q)$(OBJCOPY) -j .fuse $(1).elf -O binary $$@
192 $(Q)chmod -x $$@
54de37bf 193
f12652df
PH
194$(1)_fuses: $(1)_fuses.bin
195 @echo FUSES $$@
196 $(Q)$(AVRDUDE) $(AVRDUDE_FLAGS)\
197 -U lfuse:w:0x$$$$(hd $(1)_fuses.bin| awk '{print $$$$2}'):m \
198 -U hfuse:w:0x$$$$(hd $(1)_fuses.bin| awk '{print $$$$3}'):m \
199 -U efuse:w:0x$$$$(hd $(1)_fuses.bin| head -n1 |awk '{print $$$$4}'):m
54de37bf 200
f12652df
PH
201$(1)_size: $(1).elf
202 @echo; echo
203 $(Q) $(SIZE) -A $(1).elf
54de37bf 204
f12652df
PH
205# Target: clean project.
206$(1)_clean:
207 @echo CLEAN $(1)
208 $(Q)$(RM) $(1).hex
209 $(Q)$(RM) $(1).eep
210 $(Q)$(RM) $(1).cof
211 $(Q)$(RM) $(1).elf
212 $(Q)$(RM) $(1).map
213 $(Q)$(RM) $(1).sym
214 $(Q)$(RM) $(1).lss
215 $(Q)$(RM) $$($(1)_OBJS)
216 $(Q)$(RM) $$($(1)_LST)
217 $(Q)$(RM) $$($(1)_SRC:.c=.s)
218 $(Q)$(RM) $$($(1)_SRC:.c=.d)
219 @$(Q)$(RM) $(1)_fuses.bin
220
221$(1)_build: $(1).hex $(1)_size
222
223-include $$($(1)_OBJS:.o=.d)
224
225.PRECIOUS : $$($(1)_OBJS)
226.PHONY: $(1) $(1)_clean $(1)_size $(1)_build
227
228#############
229endef
230################################################################################
231
232$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))