all : burn program.lst

GCCPATH:=/usr/local/lpcxpresso_4.0.6_131/lpcxpresso/tools/bin
TOOLSPATH:=/usr/local/lpcxpresso_4.0.6_131/lpcxpresso/bin
INCPATH:=-Icommon/inc
CC:=$(GCCPATH)/arm-none-eabi-gcc
GDB:=$(GCCPATH)/arm-none-eabi-gdb
PROGTOOL:=$(TOOLSPATH)/crt_emu_lpc11_13_nxp


#debug
#CFLAGS:=-O0 -g3 -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m3 -mthumb -DDEBUG -D__USE_CMSIS=CMSISv1p30_LPC13xx -D__CODE_RED -D__REDLIB__
CFLAGS:=-Os -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m3 -mthumb -DDEBUG -D__USE_CMSIS=CMSISv1p30_LPC13xx -D__CODE_RED -D__REDLIB__


OBJS:=blinky_main.o clkconfig.o gpio.o cr_startup_lpc13.o timer32.o common/src/core_cm3.o  common/src/system_LPC13xx.o
SRCS:=blinky_main.c clkconfig.c gpio.c cr_startup_lpc13.c timer32.c common/src/core_cm3.c  common/src/system_LPC13xx.c

OUTPUT:=blinky.axf

%.o : %.c
	$(CC) $(CFLAGS) $(INCPATH) -c  -o $@ $^

%.axf : $(OBJS)
	$(CC) -nostdlib -Xlinker --gc-sections -Xlinker -Map=blinky.map -mcpu=cortex-m3 -mthumb -T "loaderscript_debug.ld" -o $@ $^  

program.lst : $(SRCS)
	$(CC) $(INCPATH) $(CFLAGS) -c -g -Wa,-a,-ad $(CFLAGS) $^ > $@

setup : 
	dfu-util -d 0x471:0xdf55 -c 0 -t 2048 -R -D /usr/local/lpcxpresso_4.0.6_131/lpcxpresso/bin/LPCXpressoWIN.enc

burn : $(OUTPUT)
	$(PROGTOOL)  -pLPC1343 -wire=winusb -flash-load-exec=blinky.axf
	echo finish burn

#note: this is busted.
gdbrun : burn
	$(GDB) $(OUTPUT) target extended-remote | $(PROGTOOL) -2 -pLPC1343 -wire=winusb

clean :
	rm -rf *.o *~ *.map *.axf common/src/*.o program.lst