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