#
# Makefile for msp430
# adapted from the example Makefile from the mspgcc project
#
# 'make' builds everything
# 'make clean' deletes everything except source files and Makefile
# You need to set TARGET, MCU and SOURCES for your project.
# TARGET is the name of the executable file to be produced 
# $(TARGET).elf $(TARGET).hex and $(TARGET).txt nad $(TARGET).map are all generated.
# The TXT file is used for BSL loading, the ELF can be used for JTAG use
# 
TARGET     = oswald_mw
MCU        = msp430f5438a
MEMMODEL   = -mmemory-model=huge -fdata-sections -ffunction-sections # -mdata-region=far
# -gdwarf-2 -fdata-sections -ffunction-sections
#MEMMODEL   = -mmpy=16 -msr20 -md20 -gdwarf-2 -fdata-sections -ffunction-sections
# MEMMODEL   = -mmemory-model=medium -misr20
#MEMMODEL   = -mmemory-model=medium

#APPCONFIG = -DDIGITAL -DMW_DEVBOARD_V2
#APPCONFIG = -DDIGITAL -DMW_DEVBOARD_V2 -DWITH_BTSTACK
#APPCONFIG = -DDIGITAL -DMW_DEVBOARD_V2 -DCC256x_TRANSP
#APPCONFIG = -DDIGITAL -DMW_DIGITAL_V2 -DMW_DEBUG_UART
APPCONFIG = -DDIGITAL -DMW_DIGITAL_V2

BTCC256x_SCRIPT = bluetooth_init_cc2560_2.44.c
#BTCC256x_SCRIPT = bluetooth_init_cc2564_2.8.c

# List all the source files here
# eg if you have a source file foo.c then list it here

OSWALD_SRC = ../ui/oswald_main.c ../ui/oswald_screens.c \
	../ui/oswald_watch_faces.c \
	../ui/oswald_strings.c ../ui/calendar.c \
	../ui/oswald_graphics.c ../ui/oswald_fonts.c \
	../ui/embedvm.c ../ui/dayslib.c ../ui/mymem.c ../ui/strutils.c

SOURCES = mw_main.c mw_uart.c mw_lcd.c mw_adc.c mw_bt.c \
	mw_acc.c $(BTCC256x_SCRIPT) \
	bt_hci.c bt_l2cap.c \
	oswald_hal.c $(OSWALD_SRC) \
	F5xx_F6xx_Core_Lib/HAL_PMM.c \
	F5xx_F6xx_Core_Lib/HAL_UCS.c
#	$(BT_SMALLTOOTH_SRC)
#	$(BT_STACK_SRC) \

#BT_STACK_INC = -Ibtstack/ -Ibtstack/include/
#BT_SMALLTOOTH_INV = -IBluetooth/
# Include are located in the Include directory
#INCLUDES = $(BT_STACK_INC)
#INCLUDES = $(BT_SMALLTOOTH_INV)
INCLUDES = -I../ui/

# BUILDNO = \"\#$(shell cat .buildno)-$(shell date +%y%m%d)\"
BUILDNO = \"$(shell date +%y%m%d)-\#$(shell cat .buildno)\"

# Add or subtract whatever MSPGCC flags you want. There are plenty more
#######################################################################################
CFLAGS   = -mmcu=$(MCU) $(MEMMODEL) -g -Os -Wall -Wunused $(INCLUDES) $(APPCONFIG) -DBUILDNO=$(BUILDNO)
ASFLAGS  = -mmcu=$(MCU) $(MEMMODEL) -x assembler-with-cpp -Wa,-gstabs
LDFLAGS  = -mmcu=$(MCU) $(MEMMODEL) -Wl,-gc-sections -Wl,-Map=$(TARGET).map
########################################################################################
CC       = msp430-gcc
LD       = msp430-ld
AR       = msp430-ar
AS       = msp430-gcc
GASP     = msp430-gasp
NM       = msp430-nm
OBJCOPY  = msp430-objcopy
RANLIB   = msp430-ranlib
STRIP    = msp430-strip
SIZE     = msp430-size
READELF  = msp430-readelf
MAKETXT  = srec_cat
CP       = cp -p
RM       = rm -f
MV       = mv
########################################################################################
# the file which will include dependencies
DEPEND = $(SOURCES:.c=.d)

# all the object files
OBJECTS = $(SOURCES:.c=.o)

all: .buildno $(TARGET).elf $(TARGET).hex $(TARGET).txt

prog: $(TARGET).hex
	mspdebug tilib -d /dev/ttyACM3 -v 2500 "prog $(TARGET).hex"

prog_watch: $(TARGET).hex
	mspdebug rf2500 -v 2500 "prog $(TARGET).hex"

$(TARGET).elf: $(OBJECTS)
	echo "Linking $@"
	$(CC) $(OBJECTS) $(LDFLAGS) $(LIBS) -o $@
	echo
	echo ">>>> Size of Firmware <<<<"
	$(SIZE) $(TARGET).elf
	echo

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

%.txt: %.hex
	$(MAKETXT) -O $@ -TITXT $< -I
	unix2dos $(TARGET).txt
#  The above line is required for the DOS based TI BSL tool to be able to read the txt file generated from linux/unix systems.

%.o: %.c
	echo "Compiling $<"
	$(CC) -c $(CFLAGS) -o $@ $<

# rule for making assembler source listing, to see the code
%.lst: %.c
	$(CC) -c $(ASFLAGS) -Wa,-anlhd $< > $@

# include the dependencies unless we're going to clean, then forget about them.
ifneq ($(MAKECMDGOALS), clean)
-include $(DEPEND)
endif
# dependencies file
# includes also considered, since some of these are our own
# (otherwise use -MM instead of -M)
%.d: %.c
	echo "Generating dependencies $@ from $<"
	$(CC) -M ${CFLAGS} $< >$@

#.SILENT:
.PHONY:	clean
clean:
	-$(RM) $(OBJECTS)
	-$(RM) $(TARGET).*
	-$(RM) $(SOURCES:.c=.lst)
	-$(RM) $(DEPEND)

.buildno: $(OBJECTS)
	@if ! test -f .buildno; then echo 0 > .buildno; fi
	@echo $$(($$(cat .buildno) + 1)) > .buildno