TARGET=main
MCU=atmega2560
SOURCES=main.c

PORT=/dev/ttyACM0
BAUD=115200

OBJECTS=$(SOURCES:.c=.o)
CFLAGS=-c -Os
LDFLAGS=

LIB=-L/usr/avr/lib
INC=-I/usr/avr/include

yeet: hex eeprom program clean

hex: $(TARGET).hex

eeprom: $(TARGET)_eeprom.hex

$(TARGET).hex: $(TARGET).elf
	avr-objcopy -O ihex -j .data -j .text $(TARGET).elf $(TARGET).hex

$(TARGET)_eeprom.hex: $(TARGET).elf
	avr-objcopy -O ihex -j .eeprom --change-section-lma .eeprom=1 $(TARGET).elf $(TARGET)_eeprom.hex

$(TARGET).elf: $(OBJECTS)
	avr-gcc $(LDFLAGS) $(INC) $(LIB) -mmcu=$(MCU) $(OBJECTS) -o $(TARGET).elf

.c.o:
	avr-gcc $(CFLAGS) $(INC) $(LIB) -mmcu=$(MCU) $< -o $@

size:
	avr-size --mcu=$(MCU) -C $(TARGET).elf

program:
	avrdude -p$(MCU) -P$(PORT) -b$(BAUD) -v -V -cwiring -D "-Uflash:w:$(TARGET).hex:i"

clean_tmp:
	rm -rf *.o
	rm -rf *.elf

clean:
	rm -rf *.o
	rm -rf *.elf
	rm -rf *.hex
