#-----------------------------------------
# Set the port and command for avrdude to flash
#-----------------------------------------

UNAME   := $(shell uname)
ifeq ($(UNAME), Darwin)
    PORT := /dev/tty.usbmodem12341
else
    PORT := /dev/ttyACM1
endif
AVRDUDE = avrdude -c avrisp -P $(PORT) -p $(DEVICE)

#-----------------------------------------
# Nothing below here should need changing
#-----------------------------------------

DEVICE  = attiny85
F_CPU   = 16500000      # in Hz
CFLAGS  = -std=gnu99 -Iusbdrv -I.
LDFLAGS = -lm

OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o main.o

COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE) -DDEBUG_LEVEL=0

CLIENT_USBFLAGS = `libusb-config --cflags`
CLIENT_USBLIBS  = `libusb-config --libs` -framework CoreFoundation

all: hex

hex: main.hex

program: flash

# rule for uploading firmware:
flash: main.hex
	$(AVRDUDE) -U flash:w:main.hex:i

# rule for deleting dependent files (those which can be built by Make):
clean:
	rm -f main.hex *.o usbdrv/*.o usbdrv/*.s main.elf

.c.o:
	$(COMPILE) -c $< -o $@

.S.o:
	$(COMPILE) -x assembler-with-cpp -c $< -o $@

.c.s:
	$(COMPILE) -S $< -o $@

main.elf: $(OBJECTS)
	$(COMPILE) -o main.elf $(OBJECTS) $(LDFLAGS)

main.hex: main.elf
	rm -f main.hex main.eep.hex
	avr-objcopy -j .text -j .data -O ihex main.elf main.hex
	avr-size -C --mcu=$(DEVICE) main.elf

# Internal clock at 16Mhz, no clock divice by 8, watchdog enabled, BOD@2.7V
fuse:
	$(AVRDUDE) -U hfuse:w:0xdd:m -U lfuse:w:0xe1:m
