CROSS_COMPILE ?= avr- TARGET_FILE ?= avr.elf MCU = attiny85 C_FLAGS += -Wall -Werror C_FLAGS += -Os C_FLAGS += -mmcu=$(MCU) C_FLAGS += -ffunction-sections C_FLAGS += -fdata-sections C_FLAGS += -std=c11 C_FLAGS += -pedantic C_FLAGS += -pedantic-errors C_FLAGS += -Iinclude LD_FLAGS += -mmcu=$(MCU) DUDE_FLAGS = -p $(MCU) DUDE_FLAGS += -P /dev/spidev0.0 DUDE_FLAGS += -c linuxspi DUDE_FLAGS += -b 10000 CC = $(CROSS_COMPILE)gcc AS = $(CROSS_COMPILE)as LD = $(CROSS_COMPILE)ld OBJCOPY = $(CROSS_COMPILE)objcopy SIZE = $(CROSS_COMPILE)size AVRDUDE = avrdude OBJ_DIR := objs BIN_DIR := bin SRC_DIR := src C_SRCS = $(wildcard $(SRC_DIR)/*.c) C_OBJS = $(patsubst %.c,$(OBJ_DIR)/%.o,$(notdir $(C_SRCS))) OBJS = $(C_OBJS) TARGET = $(BIN_DIR)/$(TARGET_FILE) .PHONY: all all: $(TARGET) $(TARGET): $(OBJS) Makefile @mkdir -p $(BIN_DIR) $(CC) $(LD_FLAGS) $(LINKER_SCRIPT) $(OBJS) $(LD_LIBS) -o $@ -Wl,-Map=$(BIN_DIR)/$(notdir $(basename $@)).map $(OBJCOPY) -O ihex $(TARGET) $(subst .elf,.hex,$(TARGET)) $(SIZE) $@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(dir $@) @$(call makedep,$<,$@,$(patsubst %.c,%.d,$(OBJ_DIR)/$(notdir $<)),$(C_FLAGS)) $(CC) -c $(C_FLAGS) -o $@ $< .PHONY: clean clean: rm -rf \ $(OBJS) \ $(patsubst %.o,%.d,$(OBJS)) \ $(TARGET) \ $(subst .elf,.map,$(TARGET)) \ $(subst .elf,.hex,$(TARGET)) .PHONY: flash flash: $(TARGET) gpio 22 off $(AVRDUDE) $(DUDE_FLAGS) -U flash:w:$(subst .elf,.hex,$(TARGET)) gpio 22 on define makedep $(CC) -MM -MF $3 -MP -MT $2 $4 $1 endef ifneq ($(MAKECMDGOALS),clean) -include $($(subst .o,.d,$(OBJS)) endif