####################################### HW?=trennfix_0.4 # Default program to build PROG?=trennfix BUILD_DIR=build ####################################### # VERBOSITY CONTROL ifeq ($(V),) Q=@ else Q= endif include mk/hw/${HW}.mk include mk/prog/*.mk INCLUDES+=-I mm/include -I include -I. CFLAGS+=-D__HW_CONF_HEADER__="" CFLAGS += -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL CFLAGS += -std=gnu99 CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums CFLAGS += -Wall -Wstrict-prototypes CFLAGS += -Os -gdwarf-2 CFLAGS += -Wa,-adhlns=$(<:.c=.lst) CFLAGS += -MD $(@:.c:.d) CFLAGS += $(INCLUDES) ASFLAGS += -Wa,-adhlns=$(<:.S=.lst),-gstabs -mmcu=$(MCU) ASFLAGS += -x assembler-with-cpp #---------------- Library Options ---------------- # Minimalistic printf version PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min # Floating point printf version (requires MATH_LIB = -lm below) PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt # If this is left blank, then it will use the Standard printf version. PRINTF_LIB = #PRINTF_LIB = $(PRINTF_LIB_MIN) #PRINTF_LIB = $(PRINTF_LIB_FLOAT) # Minimalistic scanf version SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min # Floating point + %[ scanf version (requires MATH_LIB = -lm below) SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt # If this is left blank, then it will use the Standard scanf version. SCANF_LIB = #SCANF_LIB = $(SCANF_LIB_MIN) #SCANF_LIB = $(SCANF_LIB_FLOAT) LDFLAGS = -Wl,-Map=$(@:.elf=.map),--cref LIBS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB) #---------------- Programming Options (avrdude) ---------------- # Programming hardware: alf avr910 avrisp bascom bsd # dt006 pavr picoweb pony-stk200 sp12 stk200 stk500 # # Type: avrdude -c ? # to get a full listing. # AVRDUDE_PROGRAMMER ?= avrisp2 # com1 = serial port. Use lpt1 to connect to parallel port. AVRDUDE_PORT ?= usb AVRDUDE_WRITE_FUSES = -U lfuse:w:${LFUSE}:m -U hfuse:w:${HFUSE}:m \ -U efuse:w:${EFUSE}:m # Uncomment the following if you want avrdude's erase cycle counter. # Note that this counter needs to be initialized first using -Yn, # see avrdude manual. #AVRDUDE_ERASE_COUNTER = -y # Uncomment the following if you do /not/ wish a verification to be # performed after programming the device. # AVRDUDE_NO_VERIFY = -V # Increase verbosity level. Please use this when submitting bug # reports about avrdude. See # to submit bug reports. #AVRDUDE_VERBOSE = -v -v AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY) AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE) AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER) AVRDUDE_FLAGS += -B 5 # Define programs and commands. SHELL = sh CC = avr-gcc OBJCOPY = avr-objcopy OBJDUMP = avr-objdump SIZE = avr-size NM = avr-nm AVRDUDE = avrdude RM = rm -rf define SELECT_DEFAULT_template = elf: $(1).elf hex: $(1).hex eep: $(1).eep lss: $(1).lss sym: $(1).sym clean: $(1)_clean build: $(1)_build program: $(1)_program fuses: $(1)_fuses endef default:build all: buildall cleanall: $(foreach prog, $(PROGRAMS), $(prog)_clean) buildall: $(foreach prog, $(PROGRAMS), $(prog)_build) .PHONY: clean build program cleanall buildall all $(eval $(call SELECT_DEFAULT_template, $(PROG))) # Create final output files (.hex, .eep) from ELF output file. %.hex: %.elf @echo HEX $@ $(Q)$(OBJCOPY) -O ihex -R .fuse -R .eeprom $< $@ %.eep: %.elf @echo EEP $@ $(Q)$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ --change-section-lma .eeprom=0 -O ihex $< $@ # Create extended listing file from ELF output file. %.lss: %.elf @echo LSS $@ $(Q)$(OBJDUMP) -h -S $< > $@ # Create a symbol table from ELF output file. %.sym: %.elf @echo SYM $@ $(Q)$(NM) -n $< > $@ # Link: create ELF output file from object files. %.elf: $(OBJ) @echo LD $@ $(Q)$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) # Compile: create object files from C source files. $(BUILD_DIR)/%.o : %.c Makefile $(wildcard mk/hw/*) @echo CC $@ @mkdir -p $(@D) $(Q)$(CC) -c $(CFLAGS) $< -o $@ # Compile: create assembler files from C source files. %.s : %.c Makefile @echo CC-ASM $< $(Q)$(CC) -S $(CFLAGS) $< -o $@ # Assemble: create object files from assembler source files. %.o : %.S @echo ASM $@ @mkdir -p $(@D) $(Q)$(CC) -c $(ASFLAGS) $< -o $@ ########################### The big template for each program ################## define PROGRAM_template= $(1)_mupf: @echo "$(1) Objects: " $$($(1)_OBJS) # Determine program's object files $(1)_OBJS=$$(foreach obj, $$($(1)_SRC:.c=.o), $(BUILD_DIR)/$$(obj)) $(1)_OBJS+=$$($(1)_ASRC:.S=.o) # Collect all listing files - for cleanup $(1)_LST += $$($(1)_SRC:.c=.lst) $$($(1)_ASRC:.S=.lst) $(1): $(1).elf $(1).elf: $$($(1)_OBJS) $(1)_program : $(1).hex $(1).eep @echo PROG $(1) $(Q)$(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(1).hex -U eeprom:w:$(1).eep $(1)_fuses.bin: $(1).elf @echo OBJCOPY $$@ $(Q)$(OBJCOPY) -j .fuse $(1).elf -O binary $$@ $(Q)chmod -x $$@ $(1)_fuses: $(1)_fuses.bin @echo FUSES $$@ $(Q)$(AVRDUDE) $(AVRDUDE_FLAGS)\ -U lfuse:w:0x$$$$(hd $(1)_fuses.bin| awk '{print $$$$2}'):m \ -U hfuse:w:0x$$$$(hd $(1)_fuses.bin| awk '{print $$$$3}'):m \ -U efuse:w:0x$$$$(hd $(1)_fuses.bin| head -n1 |awk '{print $$$$4}'):m $(1)_size: $(1).elf @echo; echo $(Q) $(SIZE) -A $(1).elf # Target: clean project. $(1)_clean: @echo CLEAN $(1) $(Q)$(RM) $(1).hex $(Q)$(RM) $(1).eep $(Q)$(RM) $(1).cof $(Q)$(RM) $(1).elf $(Q)$(RM) $(1).map $(Q)$(RM) $(1).sym $(Q)$(RM) $(1).lss $(Q)$(RM) $$($(1)_OBJS) $(Q)$(RM) $$($(1)_LST) $(Q)$(RM) $$($(1)_SRC:.c=.s) $(Q)$(RM) $$($(1)_SRC:.c=.d) $(Q)$(RM) $(1)_fuses.bin $(Q) rm -rf $(BUILD_DIR)/ $(1)_build: $(1).hex $(1)_size -include $$($(1)_OBJS:.o=.d) .PRECIOUS : $$($(1)_OBJS) .PHONY: $(1) $(1)_clean $(1)_size $(1)_build ############# endef ################################################################################ $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))