all:	rom_c.hex rom_asm.hex

CC=avr-gcc
OBJCOPY=avr-objcopy
PROGRAMMER=dapa_i2c


CFLAGS= -Wall -mmcu=attiny25 -Os


dis_c:	ot2smbus_c.out
	avr-objdump -C -D ot2smbus_c.out

dis_asm:	ot2smbus_asm.out
	avr-objdump -C -D ot2smbus_asm.out

rom_asm.hex:	ot2smbus_asm.out
	$(OBJCOPY) -O ihex ot2smbus_asm.out rom_asm.hex

rom_c.hex:	ot2smbus_c.out
	$(OBJCOPY) -O ihex ot2smbus_c.out rom_c.hex

ot2smbus_c.out:	ot2smbus.o data_layer.o i2c.o
	$(CC) $(CFLAGS) i2c.o ot2smbus.o data_layer.o -o ot2smbus_c.out  -Wl,--cref -Wl,--Map=ot2smbus_c.map

ot2smbus_asm.out:	ot2smbus.o data_layer.o SMBus_USI.o local_vector.o
	$(CC) $(CFLAGS) -DUSI_LINKER_SCRIPT SMBus_USI.o local_vector.o ot2smbus.o data_layer.o -o ot2smbus_asm.out  -Wl,--cref -Wl,--Map=ot2smbus_asm.map -T attiny25_i2c.x -Wl,--gc-sections
        
local_vector.o:	local_vector.S
	$(CC) $(CFLAGS) -DUSI_LINKER_SCRIPT local_vector.S -c -o local_vector.o 

ot2smbus.o:	ot2smbus.c ot2smbus.h i2c.h data_layer.h
	$(CC) $(CFLAGS) ot2smbus.c -c -o ot2smbus.o

data_layer.o:	data_layer.c data_layer.h ot2smbus.h
	$(CC) $(CFLAGS) data_layer.c -c -o data_layer.o

SMBus_USI.o:	SMBus_USI.S
	$(CC) $(CFLAGS) -DUSI_LINKER_SCRIPT SMBus_USI.S -c -o SMBus_USI.o

i2c.o:	i2c.c i2c.h ot2smbus.h
	$(CC) $(CFLAGS) i2c.c -c -o i2c.o

programRead:
	bash -c "if [ $(PROGRAMMER) == \"dapa_i2c\" ]; then bin/parport_program.sh; fi"
	avrdude -p attiny25 -c $(PROGRAMMER) -v -U flash:r:rom_asm_verify.hex:i
	bash -c "if [ $(PROGRAMMER) == \"dapa_i2c\" ]; then bin/parport_i2c.sh; fi "

fuseREAD:
	bash -c "if [ $(PROGRAMMER) == \"dapa_i2c\" ]; then bin/parport_program.sh; fi"
	avrdude -p attiny25 -c $(PROGRAMMER) -v
	bash -c "if [ $(PROGRAMMER) == \"dapa_i2c\" ]; then bin/parport_i2c.sh; fi "

program_asm:	rom_asm.hex 
	bash -c "if [ $(PROGRAMMER) == \"dapa_i2c\" ]; then bin/parport_program.sh; fi"
	avrdude -p attiny25 -c $(PROGRAMMER) -v -U flash:w:rom_asm.hex
	bash -c "if [ $(PROGRAMMER) == \"dapa_i2c\" ]; then bin/parport_i2c.sh; fi"

program_c:	rom_c.hex 
	bash -c "if [ $(PROGRAMMER) == \"dapa_i2c\" ]; then bin/parport_program.sh; fi"
	avrdude -p attiny25 -c $(PROGRAMMER) -v -U flash:w:rom_c.hex
	bash -c "if [ $(PROGRAMMER) == \"dapa_i2c\" ]; then bin/parport_i2c.sh; fi"
	

clean:
	rm -f *~
	rm -f *.o
	rm -f *.hex *.map *.out *.bak

