######################################################################
#                         SETUP DEVICE                               #
######################################################################

# Programmer target
PGM_TARGET = t24
#PGM_TARGET = t44

# Target device
MCU_TARGET = attiny24
#MCU_TARGET = attiny44

# Target clock speed
F_CPU = 8000000

######################################################################
#                         SETUP paths and names                                  #
######################################################################

# My source file(s)
SRCS  = main.S

INC_DIRS = 

######################################################################
#                         SETUP TOOLS                                #
######################################################################

# GCC ...
CC = avr-gcc
LD = avr-ld
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump

# Directories to be searched for header files
INCLUDE = $(addprefix -I,$(INC_DIRS))

# Optimization level
OPTIMIZE       = -O2

# Programmer
PGM = stk500v2
#PGM = dragon_isp

# Programmer Port
PGM_PORT = /dev/ttyS0
#PGM_PORT = usb

# Programmer SPEED
#PGM_SPEED = 10
PGM_SPEED = 1

# Compiler options
CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) -DF_CPU=$(F_CPU) 

# Linker options
LFLAGS = -Wl,-Map,$(BUILD_DIR)/$(PROJ_NAME).map

AFLAGS = -Wa,-gstabs $(CFLAGS)
 
######################################################################
#                         SETUP TARGETS                              #
######################################################################

all: clean main.hex flash

main.hex: 
	$(CC) $(INCLUDE) -c $(AFLAGS) -o main.o main.S
	$(LD) -o main.out main.o
	$(OBJDUMP) -S main.out > main.list
	$(OBJCOPY) -O ihex main.out main.hex

clean:
	rm -f main.o main.out main.list main.hex

erase:
	sudo avrdude -v -c $(PGM) -p $(PGM_TARGET) -B $(PGM_SPEED) -P $(PGM_PORT) -e

flash: all
	sudo avrdude -v -c $(PGM) -p $(PGM_TARGET) -B $(PGM_SPEED) -P $(PGM_PORT) -U flash:w:main.hex

# PROGRAM THE FUSES
#sudo avrdude -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m -v -c stk500v2 -p t24 -B 10 -P /dev/ttyS0



