0ad4b1b1ff8862a37fe0b070f72dc891e9b0293c
[eisenbahn.git] / trennfix / sw / Makefile
1
2 #######################################
3
4 HW?=trennfix_0.4
5 PROG?=smokefix
6
7 #######################################
8
9
10 #######################################
11 # VERBOSITY CONTROL
12 ifeq ($(V),)
13 Q=@
14 else
15 Q=
16 endif
17
18 include mk/hw/${HW}.mk
19 include mk/prog/*.mk
20
21 INCLUDES+=-I mm/include -I include -I.
22 CFLAGS+=-D__HW_CONF_HEADER__="<config/${CONFIG}.h>"
23
24 CFLAGS += -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL
25 CFLAGS += -std=gnu99
26 CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
27 CFLAGS += -Wall -Wstrict-prototypes
28 CFLAGS += -Os -gdwarf-2
29 CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
30 CFLAGS += -MD $(@:.c:.d)
31 CFLAGS += $(INCLUDES)
32
33 ASFLAGS += -Wa,-adhlns=$(<:.S=.lst),-gstabs -mmcu=$(MCU)
34 ASFLAGS += -x assembler-with-cpp
35
36 #---------------- Library Options ----------------
37 # Minimalistic printf version
38 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
39
40 # Floating point printf version (requires MATH_LIB = -lm below)
41 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
42
43 # If this is left blank, then it will use the Standard printf version.
44 PRINTF_LIB =
45 #PRINTF_LIB = $(PRINTF_LIB_MIN)
46 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
47
48 # Minimalistic scanf version
49 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
50
51 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
52 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
53
54 # If this is left blank, then it will use the Standard scanf version.
55 SCANF_LIB =
56 #SCANF_LIB = $(SCANF_LIB_MIN)
57 #SCANF_LIB = $(SCANF_LIB_FLOAT)
58
59 LDFLAGS = -Wl,-Map=$(@:.elf=.map),--cref
60 LIBS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
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 #
70 AVRDUDE_PROGRAMMER ?= avrisp2
71
72 # com1 = serial port. Use lpt1 to connect to parallel port.
73 AVRDUDE_PORT ?= usb
74
75 AVRDUDE_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
92 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
93 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
94 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
95 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
96 AVRDUDE_FLAGS += -B 5
97
98 # Define programs and commands.
99 SHELL = sh
100 CC = avr-gcc
101 OBJCOPY = avr-objcopy
102 OBJDUMP = avr-objdump
103 SIZE = avr-size
104 NM = avr-nm
105 AVRDUDE = avrdude
106 RM = rm -rf
107
108 define SELECT_DEFAULT_template =
109 elf: $(1).elf
110 hex: $(1).hex
111 eep: $(1).eep
112 lss: $(1).lss
113 sym: $(1).sym
114 clean: $(1)_clean
115 build: $(1)_build
116 program: $(1)_program
117 fuses: $(1)_fuses
118 endef
119
120 all: build
121
122 cleanall: $(foreach prog, $(PROGRAMS), $(prog)_clean)
123 buildall: $(foreach prog, $(PROGRAMS), $(prog)_build)
124
125 .PHONY: clean build program cleanall buildall all
126
127 $(eval $(call SELECT_DEFAULT_template, $(PROG)))
128
129 # Create final output files (.hex, .eep) from ELF output file.
130 %.hex: %.elf
131 @echo HEX $@
132 $(Q)$(OBJCOPY) -O ihex -R .fuse -R .eeprom $< $@
133
134 %.eep: %.elf
135 @echo EEP $@
136 $(Q)$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
137 --change-section-lma .eeprom=0 -O ihex $< $@
138
139 # Create extended listing file from ELF output file.
140 %.lss: %.elf
141 @echo LSS $@
142 $(Q)$(OBJDUMP) -h -S $< > $@
143
144 # Create a symbol table from ELF output file.
145 %.sym: %.elf
146 @echo SYM $@
147 $(Q)$(NM) -n $< > $@
148
149 # Link: create ELF output file from object files.
150 %.elf: $(OBJ)
151 @echo LD $@
152 $(Q)$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
153
154 # Compile: create object files from C source files.
155 %.o : %.c Makefile
156 @echo CC $@
157 $(Q)$(CC) -c $(CFLAGS) $< -o $@
158
159 # Compile: create assembler files from C source files.
160 %.s : %.c Makefile
161 @echo CC-ASM $<
162 $(Q)$(CC) -S $(CFLAGS) $< -o $@
163
164 # Assemble: create object files from assembler source files.
165 %.o : %.S
166 @echo ASM $@
167 $(Q)$(CC) -c $(ASFLAGS) $< -o $@
168
169 ########################### The big template for each program ##################
170 define PROGRAM_template=
171
172 # Determine program's object files
173 $(1)_OBJS=$$($(1)_SRC:.c=.o)
174 $(1)_OBJS+=$$($(1)_ASRC:.S=.o)
175
176 # Collect all listing files - for cleanup
177 $(1)_LST += $$($(1)_SRC:.c=.lst) $$($(1)_ASRC:.S=.lst)
178
179 $(1): $(1).elf
180 $(1).elf: $$($(1)_OBJS)
181
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
185
186 $(1)_unfug:
187 @echo UNFUG $$@
188
189 $(1)_fuses.bin: $(1).elf
190 @echo OBJCOPY $$@
191 $(Q)$(OBJCOPY) -j .fuse $(1).elf -O binary $$@
192 $(Q)chmod -x $$@
193
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
200
201 $(1)_size: $(1).elf
202 @echo; echo
203 $(Q) $(SIZE) -A $(1).elf
204
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 #############
229 endef
230 ################################################################################
231
232 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))