BIN     = /opt/redhat/m32c/bin
CC      = ${BIN}/m32c-elf-gcc
LD      = ${CC} -nostartfiles
OC	= ${BIN}/m32c-elf-objcopy
AR	= ${BIN}/m32c-elf-ar

CFLAGS = -g -Os
PCFLAGS = ${CFLAGS} -MMD -I. -Iuip -Iuip/pm

.PRECIOUS : %.o

BSP_FILES = \
	cprintf.o \
	reverse.o \
	timer_a.o \
	spi.o \
	tty.o \
	ivects.o


all : r8c27.h laminator.elf

%.mot : %.elf
	$(OC) -O srec $*.elf $*.mot

elf=laminator
flash : $(elf).elf
	sudo ~/m32c/uflash/uflash -b 57600 -v -c $(elf).elf
console :
	sudo ~/m32c/uflash/uflash -b 57600 -v -c
watch:
	sudo ~/m32c/uflash/uflash -b 57600 -v -c | ./watcher


%.elf : init.o %.o bsp.a
	$(LD) -o $@ init.o $*.o '-Wl,--start-group' bsp.a '-Wl,-end-group' -Tr8c27.ld -Wl,-Map,$*.map

%.o : %.c
	${CC} ${PCFLAGS} -c $< -o $@

%.o : %.S
	${CC} ${PCFLAGS} -c $< -o $@

r8c27.h r8c27.inc : r8c27.io mkports
	./mkports r8c27.io r8c27.h r8c27.inc

bsp.a : $(BSP_FILES)
	$(AR) rvs bsp.a $(BSP_FILES)

zip :
	zip -9 laminator-sw.zip Makefile init.S r8c27.* laminator.c cprintf.c reverse.c timer_a.c spi.c tty.c ivects.S bsp.h spi.h tty.h pmclock.h mkports

DEPS=$(wildcard *.d uip/*/*.d uip/*/*/*.d)
ifneq ($(DEPS),)
include $(DEPS)
endif
