# *
# * Copyright (c) 2008 Department of Information Engineering, University of Padova, Italy
# * Contributors: Giovanni Zanca, Nicola Bui, Riccardo Crepaldi and Michele Rossi
# * All rights reserved
# *
# * Redistribution and use in source and binary forms, with or without
# * modification, are permitted provided that the following conditions are
# * met:
# *
# *     * Redistributions of source code must retain the above copyright
# *       notice, this list of conditions and the following disclaimer.
# *     * Redistributions in binary form must reproduce the above
# *       copyright notice, this list of conditions and the following
# *       disclaimer in the documentation and/or other materials provided
# *       with the distribution.
# *     * The name of the author may not be used to endorse or promote
# *       products derived from this software without specific prior
# *       written permission.
# *
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# *
 
PRG			= bootloader
OBJ			= bootloader.o
OPTIMIZE	= -Os
IFLAGS		= -Wall -W -pipe -DBOOTLOADER 
IFLAGS		+= -I$(TOSROOT)/tos/chips/stm25p/ -I$(TOSROOT)/tos/types/

ifeq ($(PLATFORMS)_x, _x)
	PLATFORMS = telosb
endif

define USAGE
Usage:\tmake <platform> \n \
\tmake all \n \
\tmake help \n \
\tmake clean \n \
\tmake cleanall \n \
 \n \
Valid platforms are: $(PLATFORMS)
endef

DEBUG := $(filter debug, $(MAKECMDGOALS))
MAKECMDGOALS := $(filter-out $(DEBUG), $(MAKECMDGOALS))

PLATAUX=$(PLATFORMS) all clean cleanall
PLATFORM := $(filter $(PLATAUX), $(MAKECMDGOALS))

ifeq ($(PLATFORM)_x,_x)
PLATFORM := telosb
endif

ifeq ($(PLATFORM),telosb)
  ifeq ($(DEBUG), debug)
    OPTIMIZE = -O0 -g
  endif
	INCDIR				= -I./include/msp -I./include/telosb -I$(TOSDIR)/platform/msp430 -I.
	CC					= msp430-gcc
	OBJ					+= asm_utils.o leds.o internal_flash.o external_flash.o
	MCU_TARGET			= msp430x1611
	BOOTLOADER_START	= 0x4000
	BOOTLOADER_END		= 0x4fff
	PROG_START			= 0x5000
	# PROG_START MUST be the same as in 
	# TOSDIR/support/make/telosb.target
	OBJCOPY				= msp430-objcopy
	OBJDUMP				= msp430-objdump
endif


DEFS	+= -DPROG_START=$(PROG_START) -UDEBUG_SYNAPSE_BOOTLOADER
CFLAGS   = $(OPTIMIZE) $(IFLAGS) -mmcu=$(MCU_TARGET) $(DEFS) $(INCDIR)
LDFLAGS  = -Wl,-Map,$(PRG).map,--section-start=.text=$(BOOTLOADER_START)

  
telosb: clean $(PRG).elf lst ihex only_$(PRG).ihex
	cp bootloader.ihex binaries/bl_telosb.ihex
	cp only_$(PRG).ihex binaries/only_bl_telosb.ihex
	@$(OBJDUMP) -h $(PRG).elf | perl -ne '$$b{$$1}=hex $$2 if /^\s*\d+\s*\.(text|data|bss)\s+(\S+)/; END { printf("%16d bytes in ROM\n%16d bytes in RAM\n",$$b{text}+$$b{data},$$b{data}+$$b{bss}); }'
	$(MAKE) cleanall
all:
	for platform in $(PLATFORMS); do \
		$(MAKE) $$platform || exit 1; \
	done

clean:
	rm -rf $(PRG).elf $(PRG).srec $(PRG).ihex only_$(PRG).ihex *.o *.lst *.map *~
	tos-storage-stm25p < volumes-stm25p.xml > volumes-stm25p.h

cleanall:
	rm -rf $(PRG).elf *.srec *.ihex *.o *.lst *.map *~

help:
	@echo -e " $(USAGE) "
	@true

$(PRG).elf: $(OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

lst: $(PRG).lst

%.lst: %.elf
	$(OBJDUMP) -h -S $< > $@

ihex: $(PRG).ihex
srec: $(PRG).srec

only_$(PRG).ihex: $(PRG).elf
	$(OBJCOPY) -O ihex $< $@

%.ihex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

%.srec: %.elf
	$(OBJCOPY) -j .text -j .data -O srec $< $@

