From 76c082ca2a2c560e660daca0a8294b73881423d8 Mon Sep 17 00:00:00 2001 From: Leo C Date: Thu, 30 May 2019 20:32:16 +0200 Subject: [PATCH] Refactor makefiles --- avr/Makefile | 112 ++++++++++++++++++++++++----------------------- avr/main.c | 2 +- include/common.h | 2 + mk/avr.rules.mk | 97 ++++++++++++++++++++++++---------------- 4 files changed, 120 insertions(+), 93 deletions(-) diff --git a/avr/Makefile b/avr/Makefile index c392e43..89d13a0 100644 --- a/avr/Makefile +++ b/avr/Makefile @@ -6,10 +6,7 @@ DEBUG := 1 TARGET = stamp-monitor -FATFSDIR := ../fatfs/source -FATFS := $(FATFSDIR)/ff.c $(FATFSDIR)/ffunicode.c - -SRC_Z := ../z180/hdrom.c ../z180/cfboot.c ../z180/cpuinfo.c +############################################################################### SRC := main.c SRC += cli.c cli_readline.c command.c command_tbl.c @@ -19,6 +16,15 @@ SRC += cmd_loadihex.c cmd_loadcpm3.c cmd_sd.c cmd_fat.c SRC += env.c con-utils.c print-utils.c getopt-min.c eval_arg.c SRC += timer.c serial.c i2c.c bcd.c pcf8583.c mmc.c strerror.c SRC += background.c z180-serv.c z80-if.c gpio.c + +FATFSDIR := ../fatfs/source +FATFS := $(FATFSDIR)/ff.c $(FATFSDIR)/ffunicode.c +ZDIR := ../z180 +SRC_Z := $(ZDIR)/hdrom.c $(ZDIR)/cfboot.c $(ZDIR)/cpuinfo.c + +# Sources that may depend on Z180 code: +Z_DEP := $(shell grep -sl 'include[[:blank:]]\+"$(ZDIR)' $(SRC)) + SRC += $(FATFS) SRC += $(SRC_Z) @@ -27,13 +33,7 @@ $(SRC_Z:%.c=%.o): CFLAGS += -D'const=const __flash' # Place -D or -U options here -CDEFS := -DF_CPU=$(F_CPU)UL - -ifeq ($(MCU),atmega1281) - CDEFS += -DMCU_STRING=\"ATmega1281\" -else - CDEFS += -DMCU_STRING=\"ATmega2561\" -endif +#CDEFS += ifdef DEBUG SRC += debug.c @@ -43,31 +43,45 @@ endif # Place -I options here CINCS := -I../include -OPT = s -CSTANDARD = -std=gnu11 -CDEBUG = -g -CWARN = -Wall -Wextra -Wstrict-prototypes -Wredundant-decls -CTUNING = -fshort-enums -funsigned-bitfields -fpack-struct -#CTUNING += -funsigned-char -CTUNING += -mrelax -fno-common -CTUNING += -fno-move-loop-invariants -CTUNING += -fno-tree-loop-optimize -#CTUNING += -fno-split-wide-types -CEXTRA = -ffunction-sections -fdata-sections -#CEXTRA += -flto -CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) $(CTUNING) +# Extra C flags +#TGT_CFLAGS += -funsigned-char +TGT_CFLAGS += -fno-move-loop-invariants +TGT_CFLAGS += -fno-tree-loop-optimize +#TGT_CFLAGS += -fno-split-wide-types +#TGT_CFLAGS += -flto - -# Linker Options - Create mapfile with cross reference -LDFLAGS := -Wl,-Map=$(TARGET).map,--cref -LDFLAGS += -Wl,--gc-sections - +############################################################################### # Programming support using avrdude. AVRDUDE_PROGRAMMER := dragon_isp -AVRDUDE_PORT := usb +# Uncomment, if the programmer is /not/ on USB. +#AVRDUDE_PORT := +LFUSE = 0x97 +HFUSE = 0xD6 +EFUSE = 0xF5 + +# Fuse low byte: +# 0x97 = 1 0 0 1 0 1 1 1 +# ^ ^ \ / \--+--/ +# | | | +------- CKSEL 3..0 (Low Power Crystal) +# | | +--------------- SUT 1..0 (Crystal Osc, slowly rising power) +# | +------------------ CKOUT (Clock output enabled) +# +-------------------- CKDIV8 (don't divide clock by 8) +# Fuse high byte: +# 0xd6 = 1 1 0 1 0 1 1 0 +# ^ ^ ^ ^ ^ \+/ ^---- BOOTRST (Boot Reset vector Enabled 0x3c00) +# | | | | | +------- BOOTSZ 1..0 (Boot Flash section size 512 words) +# | | | | + --------- EESAVE (Preserve EEPROM over chip erase) +# | | | +-------------- WDTON (WDT not always on) +# | | +---------------- SPIEN (allow serial programming) +# | +------------------ JTAGEN (JTAG disabled) +# +-------------------- OCDEN (OCD disabled) +# Fuse Extended byte: +# 0xf5 = 1 1 1 1 0 1 0 1 +# \-+-/ +# +------ Bodlevel (VCC = 2.7V) # Uncomment the following if you do /not/ wish a verification to be # performed after programming the device. @@ -78,35 +92,23 @@ AVRDUDE_PORT := usb # to submit bug reports. #AVRDUDE_VERBOSE = -v -v -#HFUSE = 0xDE -#LFUSE = 0xFF -#EFUSE = 0xFD - -# Fuse high byte: -# 0xda = 1 1 0 1 1 1 1 0 <-- BOOTRST (Boot Reset vector Enabled 0x3c00) -# ^ ^ ^ ^ ^ ^ ^------ BOOTSZ0 -# | | | | | +-------- BOOTSZ1 (Boot Flash section size 256 words) -# | | | | + --------- EESAVE (don't preserve EEPROM over chip erase) -# | | | +-------------- WDTON (WDT not always on) -# | | +---------------- SPIEN (allow serial programming) -# | +------------------ DWEN (debugWIRE is not enabled) -# +-------------------- RSTDISBL (reset pin is enabled) -# Fuse low byte: -# 0x9f = 1 1 1 1 1 1 1 1 -# ^ ^ \ / \--+--/ -# | | | +------- CKSEL 3..0 (Low Power Crystal) -# | | +--------------- SUT 1..0 (Crystal Osc, slowly rising power) -# | +------------------ CKOUT (Clock output disabled) -# +-------------------- CKDIV8 (don't divide clock by 8) -# Fuse Extended byte: -# 0x9f = 1 1 1 1 1 1 0 1 -# \-+-/ -# +------ Bodlevel (VCC = 2.7V) +############################################################################### +#.SECONDEXPANSION: .PHONY: all build # Default target. all: build -build: elf hex lss +build: hex lss + +clean: clean_z +$(Z_DEP:%.c=%.o): $(SRC_Z) + +$(SRC_Z): + $(MAKE) -C $(ZDIR) $(@F) + +.PHONY: clean_z +clean_z: + $(MAKE) -C $(ZDIR) clean include ../mk/avr.rules.mk diff --git a/avr/main.c b/avr/main.c index 486d992..6fd29a8 100644 --- a/avr/main.c +++ b/avr/main.c @@ -269,7 +269,7 @@ int main(void) setup_system_time(); setup_fatfs(); - printf_P(PSTR("\n" MCU_STRING "+Z8S180 Stamp Monitor - Version: " VERSION " \n\n")); + printf_P(PSTR("\n" stringify(DEVICE_NAME) "+Z8S180 Stamp Monitor - Version: " VERSION " \n\n")); setup_z180_serv(); diff --git a/include/common.h b/include/common.h index 53e3276..4037ac8 100644 --- a/include/common.h +++ b/include/common.h @@ -47,6 +47,8 @@ struct bits { #define GCC_BUG_61443 1 #endif /* PR61443 */ +#define DEVICE_NAME __AVR_DEVICE_NAME__ + #else // TODO: stm32 #endif /* __AVR__ */ diff --git a/mk/avr.rules.mk b/mk/avr.rules.mk index 155397c..66fab58 100644 --- a/mk/avr.rules.mk +++ b/mk/avr.rules.mk @@ -1,37 +1,70 @@ +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +QP := \# +endif -CC = avr-gcc -OBJCOPY = avr-objcopy -OBJDUMP = avr-objdump -SIZE = avr-size -NM = avr-nm -AVRDUDE = avrdude -RM = rm -f -COPY = cp +############################################################################### +# Executables +CC = avr-gcc +LD = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size +NM = avr-nm +AVRDUDE = avrdude +RM = rm -f +COPY = cp + +OPT := -Os +CSTD ?= -std=gnu11 + +############################################################################### # Define all object files. + OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) +############################################################################### +# C flags + +TGT_CFLAGS += -mmcu=$(MCU) $(OPT) $(CSTD) -g +TGT_CFLAGS += $(ARCH_FLAGS) +TGT_CFLAGS += -Wextra -Wshadow -Wimplicit-function-declaration +TGT_CFLAGS += -Wredundant-decls -Wstrict-prototypes +TGT_CFLAGS += -fshort-enums -funsigned-bitfields +TGT_CFLAGS += -fno-common -ffunction-sections -fdata-sections +TGT_CFLAGS += -mrelax -# Compiler flags to generate dependency files. -GENDEPFLAGS = -MMD -MP -MF .dep/$(@F:%.o=%.d) +############################################################################### +# C & C++ preprocessor common flags +TGT_CPPFLAGS += -MMD -MP -MF .dep/$(@F:%.o=%.d) +TGT_CPPFLAGS += -Wall -Wundef +TGT_CPPFLAGS += -DF_CPU=$(F_CPU)UL $(CDEFS) $(CINCS) -# Combine all necessary flags and optional flags. -# Add target processor to flags. -ALL_CFLAGS = -mmcu=$(MCU) $(CFLAGS) $(GENDEPFLAGS) +############################################################################### +# Linker flags +TGT_LDFLAGS += $(ARCH_FLAGS) +TGT_LDFLAGS += -Wl,-Map=$(TARGET).map,--cref +TGT_LDFLAGS += -Wl,--gc-sections + +############################################################################### +# Avrdude + +AVRDUDE_PORT ?= usb AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -NULL := 2>/dev/null -else -QP := \# -endif +############################################################################### + +.SUFFIXES: .elf .hex .lss .map .sym +.SECONDARY: .PHONY: elf hex lss sym size elf: $(TARGET).elf @@ -40,7 +73,7 @@ lss: $(TARGET).lss sym: $(TARGET).sym size: $(TARGET).elf - @$(QP)printf " SIZE $(TARGET).elf\n" + @#$(QP)printf " SIZE $(TARGET).elf\n" $(Q)$(SIZE) $(TARGET).elf # Program the device. @@ -51,14 +84,10 @@ flash: $(TARGET).hex fuses: $(AVRDUDE) $(AVRDUDE_FLAGS) -u -U hfuse:w:$(HFUSE):m -U lfuse:w:$(LFUSE):m - -.SUFFIXES: .elf .hex .eep .lss .sym - %.hex: %.elf @$(QP)printf " OBJCOPY $(*).hex\n" $(Q)$(OBJCOPY) -Oihex -R .eeprom $< $@ - # Create extended listing file from ELF output file. %.lss: %.elf @$(QP)printf " OBJDUMP $@\n" @@ -69,34 +98,28 @@ fuses: @$(QP)printf " NM $@\n" $(Q)$(NM) -n $< > $@ - - # Link: create ELF output file from object files. -.SECONDARY : $(TARGET).elf -.PRECIOUS : $(OBJ) %.elf %.map: $(OBJ) @$(QP)printf " LD $@\n" - $(Q)$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS) + $(Q)$(LD) $(TGT_CFLAGS) $(CFLAGS) $(TGT_LDFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ # Compile: create object files from C source files. %.o: %.c @$(QP)printf " CC $<\n" - $(Q)$(CC) -c $(ALL_CFLAGS) $< -o $@ - + $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $< # Target: clean project. -.PHONY: clean distclean +.PHONY: clean distclean dist clean: @$(QP)printf " CLEAN\n" - $(Q)$(RM) $(TARGET).elf $(TARGET).lss $(TARGET).map $(TARGET).sym \ - $(OBJ) $(SRC:.c=.s) .dep/* + $(Q)$(RM) $(OBJ) $(SRC:.c=.s) .dep/* $(TARGET).elf \ + $(TARGET).hex $(TARGET).lss $(TARGET).map $(TARGET).sym distclean: clean @$(QP)printf " DISTCLEAN\n" - $(Q)$(RM) $(TARGET).hex $(TARGET).tar.gz + $(Q)$(RM) $(TARGET).tar.gz -.PHONY: dist dist: build tar cvfz $(TARGET).tar.gz \ $(SRC) \ -- 2.39.2