#MCU := attiny861
#PART := t861

#MCU := atmega88
#PART := m88

MCU := atmega168
PART := m168

#CFLAGS := -O2 -Wall
CFLAGS := -mmcu=$(MCU) -Wall -W -g -Os -mcall-prologues -std=c99
#ASFLAGS = -a
CFLAGS  += -I../lib_avr/
LDFLAGS += -L../lib_avr/

SRC += $(filter-out Tes%.c,$(wildcard *.c))
SRC := $(sort $(SRC))		# remove possible duplicates

OBJ := $(SRC:.c=.o)

all:	sender.hex receiver.hex

%.hex:	%.out
	avr-size $<
	avr-objcopy -R .eeprom -O ihex $< $@

%.out:	%.o ../lib_avr/$(MCU).a
	@echo LD $<; avr-gcc -mmcu=$(MCU) -o $@ -Wl,-Map,$*.map -lm $^

%.o:	%.c
	@echo CC $<; avr-gcc -mmcu=$(MCU) $(CFLAGS) -Wa,-a=$*.list -c $<

%.load:	%.hex
	avrdude -p $(PART) -c bsd -q -e -E noreset -U flash:w:$< # -i 100
	touch $@

# low fuse default value: 0x62
rstfuse:
	avrdude -p $(PART) -c bsd -q -E noreset -U lfuse:w:0x62:m -i 100

# low power is the goal, use 128kHz int.osc.
sendfuse:
	avrdude -p $(PART) -c bsd -q -E noreset -U lfuse:w:0x63:m -i 100

# 20MHz is the goal
recvfuse:
	avrdude -p $(PART) -c bsd -q -E noreset -U lfuse:w:0x77:m

getfuse:
	avrdude -p $(PART) -c bsd -q -E noreset -U lfuse:r:-:h 2>/dev/null | grep ^0x

dep:
	avr-gcc $(CFLAGS) -MM *.c > depend.mk

clean:
	-rm -f *.o *~ *.hex *.out *.list *.map clean *.bak* *.ps *.cmd *.new.pcb *.net *- > /dev/null

-include depend.mk
