# Name: Makefile
# Project: TempGuy
# Author: Daniel Hoepfl
# Creation Date: 2008-09-24
# Tabsize: 4
# Copyright: (c) 2008 by Daniel Hoepfl
# License: GNU GPL v2

DEVICE = attiny25
F_CPU = 16500000 # 16.5 MHz

AVRDUDE = avrdude -c usbtiny -P usb -p $(DEVICE)
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) -Iusbdrv -I. -mmcu=$(DEVICE) -finline-limit=0 -DDEBUG_LEVEL=0

CFLAGS  = -ffunction-sections
LDFLAGS = -Wl,-gc-sections 

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

# symbolic targets:
all:	main.hex

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

.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.

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

flash:	all
	$(AVRDUDE) -U flash:w:main.hex:i

# ATTiny25/45/85
# Fuse low byte:
# 0xe1 = 1 1 1 0   0 0 0 1
#        ^ ^ \+/   \--+--/
#        | |  |       +------- CKSEL 3..0 (clock selection -> internal @ 16 MHz)
#        | |  +--------------- SUT 1..0 (BOD enabled, fast rising power)
#        | +------------------ CKOUT (clock output on CKOUT pin -> disabled)
#        +-------------------- CKDIV8 (divide clock by 8 -> don't divide)
#
# Fuse high byte:
# 0xdd = 1 1 0 1   1 1 0 1
#        ^ ^ ^ ^   ^ \-+-/
#        | | | |   |   +------ BODLEVEL 2..0 (brownout trigger level -> 2.7V)
#        | | | |   +---------- EESAVE (preserve EEPROM on Chip Erase -> not preserved)
#        | | | +-------------- WDTON (watchdog timer always on -> disable)
#        | | +---------------- SPIEN (enable serial programming -> enabled)
#        | +------------------ DWEN (debug wire enable -> enabled)
#        +-------------------- RSTDISBL (disable external reset -> enabled)
#
# Fuse extra byte:
# 0xff = 1 1 1 1   1 1 1 1
#        \-------+-----/ ^
#                |       +---- SELFPRGEN (Self-programming enabled -> enabled)
#        |       +------------ reserved

# only needed for attiny25/45/85
fuse_tinyx5:
	$(AVRDUDE) -U hfuse:w:0xdd:m -U lfuse:w:0xe1:m -U efuse:w:0xff:m

clean:
	rm -f main.hex main.lst main.obj main.cof main.list main.map main.eep.hex main.bin *.o usbdrv/*.o main.s usbdrv/oddebug.s usbdrv/usbdrv.s

# file targets:
main.bin: $(OBJECTS)
	$(COMPILE) $(LDFLAGS) -o main.bin $(OBJECTS)

main.hex: main.bin
	rm -f main.hex main.eep.hex
	avr-objcopy -j .text -j .data -O ihex main.bin main.hex
	./checksize main.bin $(DEVICE)

disasm:	main.bin
	avr-objdump -d main.bin

