all: compile

########
# MCU Type

MCUL 	= atmega3208
MCUS 	= m3208

########
# crystal speed in Hz (default internal 20MHz)

FREQ	= 20000000


########
# Use Atmel Compiler Packs?

# Path to compiler packs
AVRBASE 	= ~/tronic/atmel-compiler-packs/mega

# find MCU description
AVRPREFIX	= $(AVRBASE)/gcc/dev/$(MCUL)

# additional compiler flags
AVRINCLUDE	= -B $(AVRPREFIX) -isystem $(AVRBASE)/include -D__AVR_DEV_LIB_NAME__=$(MCUS)
AVRLINK 	= -B $(AVRPREFIX)

# Use defaults instead
#AVRINCLUDE	=
#AVRLINK 	=

########
#Compiler & Tools
CC	= avr-gcc
CXX	= avr-g++
LD	= $(CXX)

#Flags: Compiler/Linker (usually those do not need chang)
CFLAGS	= -Wall -Os -ffunction-sections -fdata-sections -mmcu=$(MCUL) $(AVRINCLUDE)
CXXFLAGS= $(CFLAGS) -fno-exceptions -std=c++17
LDFLAGS	= -Os -Wl,--gc-sections $(AVRLINK) -mmcu=$(MCUL)

# END OF CONFIGURATION
###############################

.PHONY: compile clean

# main targets AVR
compile: ml.elf

# AVR compile
CXXFILES= $(wildcard *.cpp)
OBJFILES= $(CFILES:.c=.o) $(CXXFILES:.cpp=.o)

ml.elf: $(OBJFILES)
	$(LD) $(LDFLAGS) -o $@ $^

DEFINES = -D F_CPU=$(FREQ) -D MCU_SHORT=$(MCUS)

%.o: %.cpp
	$(CXX) $(CXXFLAGS) $(DEFINES) -c -o $@ $<

#cleanup
clean:
	-rm -f ml.elf *.o
