# Adapted from Makefile.msp430

# Default to STM32F103CB
SUBTARGET = CB


### Code common for all ARM CPUs

CONTIKI_CPU_ARM=$(CONTIKI)/cpu/arm
CONTIKI_CPU_ARM_COMMON=$(CONTIKI_CPU_ARM)/common

### Define the CPU directory
CONTIKI_CPU=$(CONTIKI_CPU_ARM)/stm32f103


### Define the source files we have in the STM32F103 port

CONTIKI_CPU_DIRS = . ../common/dbg-io ../common/usb loader



STM32F103     = clock.c debug-uart.c # interrupt-utils.c newlib-syscalls.c sys-interrupt.c rtimer-arch.c rtimer-arch-interrupt.c uip-log.c

# SYSAPPS = codeprop-otf.c
# APPDIRS += $(CONTIKI)/cpu/at91sam7s/loader

# ELFLOADER = elfloader-otf.c elfloader-arm.c symtab.c cfs-ram.c

include $(CONTIKI_CPU_ARM_COMMON)/usb/Makefile.usb

include $(CONTIKI_CPU_ARM_COMMON)/SD-card/Makefile.sdcard

TARGETLIBS = random.c dbg-printf.c dbg-puts.c dbg-putchar.c dbg-sprintf.c dbg-snprintf.c strformat.c

CONTIKI_TARGET_SOURCEFILES += $(STM32F103) $(SYSAPPS) $(ELFLOADER) \
			$(TARGETLIBS) $(UIPDRIVERS) $(USB)

CONTIKI_SOURCEFILES        += $(CONTIKI_TARGET_SOURCEFILES)


THREADS =

### Compiler definitions
CC       = arm-elf-gcc
LD       = arm-elf-ld
AS       = arm-elf-as
AR       = arm-elf-ar
NM       = arm-elf-nm
OBJCOPY  = arm-elf-objcopy
STRIP    = arm-elf-strip

XSLTPROC=xsltproc

PROJECT_OBJECTFILES += ${addprefix $(OBJECTDIR)/,$(CONTIKI_TARGET_MAIN:.c=.o)}

LINKERSCRIPT = $(CONTIKI_CPU)/STM32F103$(SUBTARGET).ld

STARTUP=${addprefix $(OBJECTDIR)/,startup-STM32F10x.o}

# JTAG program upload
OPENOCD	 = openocd
OPENOCD_DIR = $(CONTIKI_CPU_ARM)/openocd/ 
OPENOCD_CFG = arm7_wig.cfg

# USB program upload
SAMIAM=Sam_I_Am
SAMIAM_TTY=/dev/ttyACM0

# Use OpenOCD by default
PROG=openocd

ARCH_FLAGS= -march=armv7-m -mthumb

CFLAGSNO = -I. -I$(CONTIKI)/core -I$(CONTIKI_CPU) -I$(CONTIKI_CPU)/loader \
	   -I$(CONTIKI_CPU)/dbg-io \
           -I$(CONTIKI)/platform/$(TARGET) \
           ${addprefix -I,$(APPDIRS)} \
           -DWITH_ASCII -DMCK=$(MCK) \
           -Wall $(ARCH_FLAGS) -g -D SUBTARGET=$(SUBTARGET)

CFLAGS  += $(CFLAGSNO) -O -DRUN_AS_SYSTEM -DROM_RUN
LDFLAGS += -L $(CONTIKI_CPU) -T $(LINKERSCRIPT) -nostartfiles

CDEPFLAGS = $(CFLAGS) -D __MAKING_DEPS__




### Setup directory search path for source files

CUSTOM_RULE_C_TO_OBJECTDIR_O=yes
CUSTOM_RULE_C_TO_O=yes

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

$(OBJECTDIR)/%.o: %.c | $(OBJECTDIR)
	$(CC) $(CFLAGS) -c $< -o $@


CUSTOM_RULE_S_TO_OBJECTDIR_O = yes
%.o: %.S
	$(CC) $(CFLAGS) $< -c

$(OBJECTDIR)/%.o: %.S | $(OBJECTDIR)
	$(CC) $(CFLAGS) $< -c  -o $@


CUSTOM_RULE_C_TO_CO=yes

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

CUSTOM_RULE_C_TO_CE=yes

%.ce: %.o
	$(LD) $(LDFLAGS) --relocatable -T $(CONTIKI_CPU)/merge-rodata.ld $< -o $@
	$(STRIP) -K _init -K _fini --strip-unneeded -g -x $@

CUSTOM_RULE_LINK=yes

%-stripped.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@
	$(STRIP) --strip-unneeded -g -x $@

%-stripped.o: %.o
	$(STRIP) --strip-unneeded -g -x -o $@ $<

%.o: ${CONTIKI_TARGET}/loader/%.S
	$(AS) -o $(notdir $(<:.S=.o)) $<

%-nosyms.$(TARGET): %.co $(PROJECT_OBJECTFILES) contiki-$(TARGET).a $(STARTUP) # $(OBJECTDIR)/empty-symbols.o
	$(CC) $(LDFLAGS) $(CFLAGS) -nostartfiles -o $@ $(filter-out %.a,$^) $(filter %.a,$^) -lc $(filter %.a,$^)


%.ihex: %.$(TARGET)
	$(OBJCOPY) $^ -O ihex $@

%.bin: %.$(TARGET)
	$(OBJCOPY) -O binary $< $@
 
.PHONY: symbols.c
ifdef CORE
%.$(TARGET): %.co $(PROJECT_OBJECTFILES) contiki-$(TARGET).a $(STARTUP) $(OBJECTDIR)/symbols.o
	$(CC) $(LDFLAGS) $(CFLAGS) -nostartfiles -o $@ $(filter-out %.a,$^) $(filter %.a,$^) -lc $(filter %.a,$^)

symbols.c: $(CORE)
	$(NM) $< | awk -f $(CONTIKI_CPU)/builtins.awk -f ../../tools/mknmlist > symbols.c

else
%.$(TARGET): %-nosyms.$(TARGET)
	ln -sf $< $@
endif

empty-symbols.c:
	cp ${CONTIKI}/tools/empty-symbols.c symbols.c
	cp ${CONTIKI}/tools/empty-symbols.h symbols.h



ifeq ($(PROG),openocd)
%.u: %.bin
	# Clear lock bits
	$(OPENOCD) -s $(OPENOCD_DIR) -f $(OPENOCD_CFG) -c init -c "reset halt;sleep 250;flash probe 0;stm32x mass_erase 0; sleep 500;flash write_bank 0 $< 0;reset halt;resume;sleep 500;exit"
else ifeq ($(PROG),samiam)
%.u: %.ihex
	# Clear lock bits
	$(SAMIAM) "open $(SAMIAM_TTY) , writew 0xffffff64 5a000004"
	$(SAMIAM) "open $(SAMIAM_TTY) , writew 0xffffff64 5a002004"
	$(SAMIAM) "open $(SAMIAM_TTY) , flash $< , go"

endif

ocd_reset:
	$(OPENOCD) -s $(OPENOCD_DIR) -f $(OPENOCD_CFG) -c init -c "reset halt;resume" -c exit

# Don't use core/loader/elfloader.c, use elfloader-otf.c instead
$(OBJECTDIR)/elfloader.o: | $(OBJECTDIR)
	echo -n >$@

clean: clean_cpu

.PHONY: stm32test_clean

clean_cpu:
	-rm -rf $(BUILTSRCDIR)

.PRECIOUS: %-nosyms.$(TARGET)