# Folgende Schalter sind beim Uebersetzen mgl.:

MCU     = msp430x449

ROOTDIR = c:\msp430

MSP      = $(ROOTDIR)

CPFLAGS  = -mmcu=$(MCU) -g -Os -Wall -Wcast-align -Wcast-qual -Wimplicit \
	-Wmissing-declarations -Wmissing-prototypes -Wnested-externs \
	-Wpointer-arith -Wredundant-decls -Wreturn-type -Wshadow \
        -Wstrict-prototypes -Wswitch -Wunused 
       
                      
ASFLAGS  = -mmcu=$(MCU) -c -x assembler-with-cpp -D_GNU_ASSEMBLER_ -Wa,--gstabs 

LDFLAGS  = -mmcu=$(MCU) -Wl,-Map=$*.map,--cref


SUBDIRS = latex



DATUM := $(shell date +%D | sed "/[0-9]/ s/\([0-9]*\)\/\([0-9]*\)\//\2\1/g")

NAME = MSP-RTOS


########################################################################################
#SHELL	 = C:\cygwin\bin\bash.exe
SHELL	 = /bin/bash

CC       = msp430-gcc
LD       = msp430-ld
AR       = msp430-ar
AS       = msp430-gcc
#AS       = msp430-as
GASP     = msp430-gasp
NM       = msp430-nm
OBJCOPY  = msp430-objcopy
OBJDUMP  = msp430-objdump
RANLIB   = msp430-ranlib
STRIP    = msp430-strip
SIZE     = msp430-size
READELF  = msp430-readelf
#CP       = copy /Y
CP       = cp
RM       = rm 
ARCHIVE  = /cygdrive/c/windows/command/zip
MV       = ren
SED	 = ssed

########################################################################################

#ASRCS = I2C_EEmem.s43
ASRCS =

CSRCS = main.c \
	portheap.c \
	port.c \
	list.c \
	queue.c \
	tasks.c 


OBJS =  $(CSRCS:.c=.o) $(ASRCS:.s43=.o)


########################################################################################

all:  $(NAME).hex
	

$(NAME).elf: $(OBJS)
	@$(CC) $(OBJS) $(LDFLAGS) -o $@				
	@$(STRIP) --discard-locals -o $(NAME).es $(NAME).elf
	@$(NM) $(NAME).es >$(NAME).syA
	@$(NM) -n $(NAME).es >$(NAME).sy1
	@$(RM) $(NAME).es
	@$(SIZE) -Ad $(NAME).elf
	@$(OBJDUMP) -S $(NAME).elf > $(NAME).odp


depend: clean
	@$(CC) -M -mmcu=$(MCU) $(CSRCS) >depend.mak

scratch::
	-@$(RM) *.hex     >NUL 2>&1

scratch:: clean all

	


# "-" vor einem Kommando laesst make Fehler ignorieren
# "@" vor dem Kommando verhindert echo
clean:
	-@$(RM) $(OBJS)       	
	-@$(RM) *.elf      	
	-@$(RM) *.cof      
	-@$(RM) *.map      
	-@$(RM) *.bak      
	-@$(RM) *.syA      
	-@$(RM) *.sy1      
	-@$(RM) *.odp 	   
	-@$(RM) *.readelf  
	-@$(RM) *.asm      
	-@$(RM) *.bin     
zip:
	@$(ARCHIVE) RTOS_ZIP_$(DATUM) $(ASRCS) $(CSRCS) *.h Makefile \
		*.map *.sy? *.hex doxygen.cfg
	@$(CP) RTOS_ZIP_$(DATUM).zip zips
	@$(RM) RTOS_ZIP_$(DATUM).zip


replace: $(SEDSRCS) $(CSRCS) $(HEADERS)
	 $(SED) $(SEDSRCS) $@
	 @$(RM) $(CSRCS)
	 @$(MV) *.cs *.c
	 @$(RM) $(HEADERS)
	 @$(MV) *.hs *.h
	 

doc:
	@doxygen doxygen.cfg	
	$(MAKE) -C latex -i pdf
	
#	for dir in $(SUBDIRS); do \
#		$(MAKE) -i -C $$dir pdf; \
#	done


#	cd latex && $(MAKE) -i pdf
	
	 
########################################################################################



.SUFFIXES :
.SUFFIXES : .o .c .s43 .elf .bin .hex .asm .

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

%.s : %.c
	@$(CC) $(CPFLAGS) $< -S -o $@

%.asm : %.c
	@$(CC) $(CPFLAGS) $< -S -o $@

%.o : %.s43
	@$(AS) $(ASFLAGS) $< -o $@
	
%.o : %.S43
	@$(AS) $(ASFLAGS) $< -o $@

%.bin : %.elf
	@$(OBJCOPY) $*.elf -O binary $*.bin

%.s19 : %.elf
	@$(OBJCOPY) $*.elf -O srec $*.s19

%.hex : %.elf
	@$(OBJCOPY) $*.elf -O ihex $*.hex
	
%.cs : %.c
	ssed $(SEDCMD) $< >$@
	
%.hs : %.h
	ssed $(SEDCMD) $< >$@
		
#
