
# project specific details

PROJECT = terminal-keyboard
TARGET = atmega48
AVRDUDE_PART = m48
SYS_CLOCK = 12000000
#TARGET = attiny2313
#AVRDUDE_PART = t2313
#SYS_CLOCK = 12000000

DEVICE = $(TARGET)

# details common across projects

CC = avr-gcc
OBJCOPY = avr-objcopy

DEFINES = -DF_CPU=$(SYS_CLOCK)
CFLAGS = -mmcu=$(TARGET) -Os $(DEFINES) -Wall $(DEBUG)
INCLUDES = -I. -Iusbdrv
SRC = $(PROJECT).c
OBJ = $(SRC:.c=.o) usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o
AVRDUDE_OPTS = -c usbasp -p $(AVRDUDE_PART) -q
COMPILE=$(CC) $(CFLAGS) $(INCLUDES)

# don't echo commands issued by make
.SILENT:

# Generic rule for compiling C files:
.c.o:
	$(COMPILE) -c $< -o $@

# Generic rule for assembling Assembler source files:
.S.o:
	$(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.


# Targets...

all: $(OBJ)

$(PROJECT).elf: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROJECT).elf -Wl,-Map,$(PROJECT).map $(OBJ)
	/usr/bin/size $(PROJECT).elf | tail -1 | awk '{print "flash: "$$1+$$2" bytes"; print "RAM:   "$$2+$$3" bytes"}'

$(PROJECT).hex: $(PROJECT).elf
	$(OBJCOPY) -O ihex $(PROJECT).elf $(PROJECT).hex

$(PROJECT).bin: $(PROJECT).elf
	$(OBJCOPY) -O binary $(PROJECT).elf $(PROJECT).bin

simulate: $(PROJECT).bin
	simulavr -d $(TARGET) -P simulavr-disp $(PROJECT).bin

upload: $(PROJECT).hex
	avrdude $(AVRDUDE_OPTS) -U flash:w:$(PROJECT).hex

verify: $(PROJECT).hex
	avrdude $(AVRDUDE_OPTS) -U flash:v:$(PROJECT).hex

clean:
	rm -f *.o *.elf *.map *.hex *.bin $(PROJECT)

