################################ ATTiny2313 #################################
# ATTiny2313 FUSE_L (Fuse low byte):
# 0xef = 1 1 1 0   1 1 1 1
#        ^ ^ \+/   \--+--/
#        | |  |       +------- CKSEL 3..0 (clock selection -> crystal @ 12 MHz)
#        | |  +--------------- SUT 1..0 (BOD enabled, fast rising power)
#        | +------------------ CKOUT (Output Clock on CKOUT pin -> disabled)
#        +-------------------- CKDIV8 (divide clock by 8 -> don't divide)
# ATTiny2313 FUSE_H (Fuse high byte):
# 0xdb = 1 1 0 1   1 0 1 1
#        ^ ^ ^ ^   \-+-/ ^
#        | | | |     |   +---- RSTDISBL (disable external reset -> enabled)
#        | | | |     +-------- BODLEVEL 2..0 (brownout trigger level -> 2.7V)
#        | | | +-------------- WDTON (watchdog timer always on -> disable)
#        | | +---------------- SPIEN (enable serial programming -> enabled)
#        | +------------------ EESAVE (preserve EEPROM on Chip Erase -> not preserved)
#        +-------------------- DWEN (debug wire enable)
#
################################ ATMEGA8 ####################################
# ATMEGA8 FUSE_L (Fuse low byte):
# 0xef = 1 1 1 0   1 1 1 1
#        ^ ^ \+/   \--+--/
#        | |  |       +------- 0001  CKSEL0123 Select Clock source  
#        | |  +---------------   10  SUT01     Select start-up time 
#        | +------------------    1  BODEN     Brown out detector enable 
#        +--------------------    1  BODLEVEL  Brown out detector trigger level
# ATMEGA8 FUSE_H (Fuse high byte):
# 0xc9 = 1 1 0 0   1 0 0 1
#        ^ ^ ^ ^   ^ ^ ^ ^
#        | | | |   | \+/ +----    1  BOOTRST   Select Reset Vector 
#        | | | |   |  +-------   00  BOOTSZ01  Select Boot Size
#        | | | |   +----------    1  EESAVE    EEPROM memory is preserved through the Chip Erase 
#        | | | +--------------    1  CKOPT     Oscillator options 
#        | | +----------------    0  SPIEN     Enable Serial Program and Data Downloading 
#        | +------------------    1  WDTON     WDT always on
#        +--------------------    1  RSTDISBL  Select if PC6 is I/O pin or RESET pin
################################ ATMEGA8 ####################################
# ATMEGA16 FUSE_L (Fuse low byte):
# 0xef = 1 1 1 0   1 1 1 1
#        ^ ^ \+/   \--+--/
#        | |  |       +------- 0001  CKSEL0123 Select Clock source  
#        | |  +---------------   10  SUT01     Select start-up time 
#        | +------------------    1  BODEN     Brown out detector enable 
#        +--------------------    1  BODLEVEL  Brown out detector trigger level
# ATMEGA16 FUSE_H (Fuse high byte):
# 0x89 = 1 0 0 0   1 0 0 1
#        ^ ^ ^ ^   ^ ^ ^ ^
#        | | | |   | \+/ +----    1  BOOTRST   Select Reset Vector 
#        | | | |   |  +-------   00  BOOTSZ01  Select Boot Size
#        | | | |   +----------    1  EESAVE    EEPROM memory is preserved through the Chip Erase 
#        | | | +--------------    1  CKOPT     Oscillator options 
#        | | +----------------    0  SPIEN     Enable Serial Program and Data Downloading 
#        | +------------------    0  JTAGEN    Enable JTAG
#        +--------------------    1  OCDEN     Enable OCD

ARDUINO_FORMAT=ihex
ARDUINO_RATE=19200
ARDUINO_PORT=/dev/ttyUSB0
ARDUINO_PROGRAMMER=arduino


AVRDUDE = sudo avrdude -c usbasp -p $(DEVICE) 
AVRDUDE_ARDUINO = sudo avrdude -F -p $(DEVICE) -P $(ARDUINO_PORT) -c $(ARDUINO_PROGRAMMER) -b $(ARDUINO_RATE)
COMPILE = avr-g++ -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)


# symbolic targets:
help:
	@echo "This Makefile has no default rule. Use one of the following:"
	@echo "make hex ............. to build $(MAIN).hex"
	@echo "make program ......... to flash fuses and firmware"
	@echo "make fuse ............ to flash the fuses"
	@echo "make flash ........... to flash the firmware"
	@echo "make upload2arduino .. to upload the firmware to an Arduino"
	@echo "make clean ........... to delete objects and hex file"
	@echo "make readFuse ........ read the current programmed fuses"
	@echo "make showSize ........ display the current used memory"

hex: $(MAIN).hex

readFuse:
#ifeq ($(DEVICE), atmega8)
	$(AVRDUDE) -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h -U lock:r:-:h
#endif

showSize:
	avr-nm --size-sort --print-size $(MAIN).elf 
	avr-size --mcu=$(DEVICE) -C $(MAIN).elf

program: fuse flash

# rule for programming fuse bits:
fuse:
	@[ "$(FUSE_H)" != "" -a "$(FUSE_L)" != "" ] || \
		{ echo "*** Edit Makefile and choose values for FUSE_L and FUSE_H!"; exit 1; }
	$(AVRDUDE) -U hfuse:w:$(FUSE_H):m -U lfuse:w:$(FUSE_L):m

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

upload2arduino: $(MAIN).hex
	$(AVRDUDE_ARDUINO) -U flash:w:$(MAIN).hex

# rule for deleting dependent files (those which can be built by Make):
clean:
	#rm -f $(MAIN).hex $(MAIN).lst $(MAIN).obj $(MAIN).cof $(MAIN).list $(MAIN).map $(MAIN).eep.hex $(MAIN).elf *.o $(MAIN).s
	trash-put -f $(MAIN).hex $(MAIN).lst $(MAIN).obj $(MAIN).cof $(MAIN).list $(MAIN).map $(MAIN).eep.hex $(MAIN).elf *.o $(MAIN).s

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

# Generic rule for compiling C++ files:
.cpp.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.

# Generic rule for compiling C to assembler, used for debugging only.
.c.s:
	$(COMPILE) -S $< -o $@

# file targets:

$(MAIN).elf: $(OBJECTS)
	$(COMPILE) -o $(MAIN).elf $(OBJECTS)

$(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 $(MAIN).hex

# debugging targets:

disasm:	$(MAIN).elf
	avr-objdump -d $(MAIN).elf

#cpp:
#	$(COMPILE) -E $(MAIN).c
