CROSS = arm-linux-gnueabi-

CC = gcc
CFLAGS = -Wall -Dat91sam7l128 -mcpu=arm7tdmi -marm

LDFLAGS = -nostdlib -Wl,--gc-sections -static
# -Wl,--print-map
#-Wl,--verbose 

LDSCRIPT = flash.lds

TARGET = lab3

SRC = main.c lcd.c keyboard.c

OBJS =	crt0.o $(SRC:.c=.o)

LIBS = -lgcc

HEADERS = AT91SAM7L128.h lcd.h keyboard.h openocd.cfg \
	board/hp-20b-calculator.cfg target/at91sam7l128.cfg 

TARFILES = Makefile $(LDSCRIPT) $(SRC) $(HEADERS) crt0.S

$(TARGET).hex :

.PHONY : flash
flash : $(TARGET).hex
	openocd -f openocd.cfg \
	-c "init" \
	-c "write_flash $(TARGET).hex" \
	-c "reset" \
	-c "resume" \
	-c "shutdown"

$(TARGET).elf : $(OBJS) $(LDSCRIPT)
	$(CROSS)$(CC) $(LDFLAGS) -T$(LDSCRIPT) -o $(TARGET).elf $(OBJS) $(LIBS)

%.hex : %.elf
	$(CROSS)objcopy -O ihex $< $@

%.o : %.c
	$(CROSS)$(CC) $(CFLAGS) -c $<

%.o : %.S
	$(CROSS)$(CC) $(CFLAGS) -c $<

$(TARGET).tar.gz : $(TARFILES)
	cd .. && tar zcf $(TARGET)/$@ $(TARFILES:%=$(TARGET)/%)

.PHONY : clean
clean :
	rm -rf *.o *.elf *.hex
