#************************************************
# Monitor LPC2478
# Marcos Augusto Stemmer
#*************************************************/

SERIALDEV = /dev/ttyS0
TERMINAL=ltser
CLOCKFREQ=12000
TARGET=lcdirq
#MODULOS = main.o monitor.o iap.o pll.o lpc2478stk_lcd.o crt.o
MODULOS = crt.o
HEADERS = 
BAUDRATE= 19200

CC      = arm-elf-gcc
LD      = arm-elf-gcc
CFLAGS  = -Wall -O2 -mcpu=arm7tdmi-s -D BAUDRATE=$(BAUDRATE)
LFLAGS  = -nostartfiles

all: $(TARGET).hex

%.bin: %.hex
	hex2bin $<

%.hex: %.elf
	arm-elf-objcopy -O ihex $< $@

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

mainr.o: main.c $(HEADERS)
	$(CC) -c $(CFLAGS) -o mainr.o -D RAM_RUN main.c

$(TARGET).elf: $(MODULOS) main.o
	$(LD) -Tlpc2478_flash.ld $(LFLAGS) -o $(TARGET).elf $(MODULOS) main.o

$(TARGET)r.elf: $(MODULOS) mainr.o
	$(LD) $(LFLAGS) -Tlpc2478_ram.ld -o $(TARGET)r.elf $(MODULOS) mainr.o

#Chama o terminal e executa o programa na RAM (necessita do mon23)
tser: $(TARGET)r.hex
	$(TERMINAL) $(SERIALDEV) b=$(BAUDRATE) $(TARGET)r.hex

tseru: $(TARGET)r.hex
	$(TERMINAL) /dev/ttyUSB0 b=$(BAUDRATE) $(TARGET)r.hex

# Use 'make isp' para programar a memoria flash
isp: $(TARGET).hex
	lpc21isp $(TARGET).hex $(SERIALDEV) $(BAUDRATE) $(CLOCKFREQ)

# Use 'make isp' para programar a memoria flash
ispu: $(TARGET).hex
	lpc21isp $(TARGET).hex /dev/ttyUSB0 $(BAUDRATE) $(CLOCKFREQ)
# isp[1..5] usa nomes de portas seriais do windows */ 
isp1: $(TARGET).hex
	lpc21isp $(TARGET).hex com1 $(BAUDRATE) $(CLOCKFREQ)
isp2: $(TARGET).hex
	lpc21isp $(TARGET).hex com2 $(BAUDRATE) $(CLOCKFREQ)
isp3: $(TARGET).hex
	lpc21isp $(TARGET).hex com3 $(BAUDRATE) $(CLOCKFREQ)
isp4: $(TARGET).hex
	lpc21isp $(TARGET).hex com4 $(BAUDRATE) $(CLOCKFREQ)
isp5: $(TARGET).hex
	lpc21isp $(TARGET).hex com5 $(BAUDRATE) $(CLOCKFREQ)

clean:
	-rm -f *.o *.elf *~ *.bin *.map *.hex
