AVR Makefile

BIN = project
OBJ = main.o one.o two.o

MCU ?= atmega328
LFUSE ?= 0xFF
HFUSE ?= 0xDE
EFUSE ?= 0x05

DIRSOURCE ?= src
DIRBUILD ?= build
DIRBIN ?= bin

PROG ?= atmelice_isp
PORT ?= usb
BAUD ?= 

PREFIX = avr-
CC = $(PREFIX)gcc
OBJCOPY = $(PREFIX)objcopy
OBJDUMP = $(PREFIX)objdump
SIZE = $(PREFIX)size

ELF = $(DIRBUILD)/$(BIN).elf
DIS = $(DIRBUILD)/$(BIN).dis
HEX = $(DIRBIN)/$(BIN).hex

OBJ = $(addprefix $(DIRBUILD)/,$(OBJ))

$(HEX): $(ELF) | $(DIRBIN)
	$(OBJCOPY) -O ihex $ $(DIS)

$(OBJ): | $(DIRBUILD)
	$(CC) -MCU=$(MCU) 
	-Wall -Wextra -ffreestanding -Og -g -c 
	-o $@ $(DIRSOURCE)/$($(notdir $@):.o=.c)

fuses:
	avrdude -c $(PROG) -P $(PORT) -p $(MCU) -qq 
	$(if BAUD, -b $(BAUD)) 
	-U lfuse:w:$(LFUSE):m 
	-U hfuse:w:$(HFUSE):m 
	-U efuse:w:$(EFUSE):m

install: $(HEX) fuses
	avrdude -c $(PROG) -P $(PORT) -p $(MCU) -qq 
	$(if BAUD, -b $(BAUD)) 
	-U flash:w:$(HEX):i

$(DIRBUILD):
	mkdir build

$(DIRBIND):
	mkdir bin

clean:
	rm -Rf $(DIRBUILD)
	rm -Rf $(DIRBIN)

Leave a Reply

Your email address will not be published. Required fields are marked *