RELEASE=R9V728
SRC=main.c adc.c button.c config.c eeprom.c gpio.c iwdg.c regul.c pireg.c pwm.c timer1.c timer2.c timer4.c uart.c
NAME=R9V715a
DEFINES= STM8S103 -D TEST
#// eeprom verze
#SRC=main-eeprom.c config.c eeprom.c uart.c
#NAME=R9V711eeprom
#DEFINES= STM8S103 -D PROGEEPROM
#//
PACKAGE= stm8s103f3
OUT=build
BINF=bin
#PACKAGE= stm8s105k4
CFLAGS= -lstm8 -mstm8 --opt-code-size --std-sdcc99 --max-allocs-per-node 10000
OBJ=$(SRC:.c=.rel)
OBJ=$(SRC:%.c=$(OUT)/%.rel)
DEP=$(SRC:%.c=$(OUT)/.%.c.d)
SDCC=sdcc $(CFLAGS)

#NEJM=$$(grep '^RELEASE' version.c | head -n1 | sed -e 's/^.*=\([0-9a-zA-Z_\-]\+\).*$$/\1/');
#NEJM=$(grep '^RELEASE' version.c | head -n1 | sed -e 's/^.*=\([0-9a-zA-Z_\-]\+\).*/\1/')
#NEJM=$(shell grep '^RELEASE' version.c | sed -e 's/^.*=\([0-9a-zA-Z_\-]\+\).*$$/\1/')
NEJM=$(shell git status| head -n1 | cut -d" " -f3)

FLASH=8192
typ=
NAME=$(NEJM)

all: printSDCC $(NAME).ihx size

-include $(DEP)

size: $(NAME).ihx
	@CODESIZE=$$(($$(grep '^HOME' $(OUT)/$(NAME).map |head -n1 | sed -e 's/^.*=\s*\([0-9]\+\).*$$/\1/') + \
	              $$(grep '^GSINIT' $(OUT)/$(NAME).map |head -n1 | sed -e 's/^.*=\s*\([0-9]\+\).*$$/\1/') + \
	              $$(grep '^GSFINAL' $(OUT)/$(NAME).map |head -n1 | sed -e 's/^.*=\s*\([0-9]\+\).*$$/\1/') + \
	              $$(grep '^CODE' $(OUT)/$(NAME).map |head -n1 | sed -e 's/^.*=\s*\([0-9]\+\).*$$/\1/') + \
	              $$(grep '^CONST' $(OUT)/$(NAME).map |head -n1 | sed -e 's/^.*=\s*\([0-9]\+\).*$$/\1/') + \
	              $$(grep '^INITIALIZER' $(OUT)/$(NAME).map |head -n1 | sed -e 's/^.*=\s*\([0-9]\+\).*$$/\1/'))); \
	if [ "$$CODESIZE" -gt $(FLASH) ]; then echo "Code is $$CODESIZE bytes, up $$(($$CODESIZE - $(FLASH))) bytes, max is $(FLASH) bytes"; exit 1; \
	else echo "Code fits the flash, it is $$CODESIZE from $(FLASH), $$(($(FLASH) - $$CODESIZE)) is free"; \
	fi

printSDCC:
	@echo "SDCC \c"
	@echo $(NAME)

$(NAME).ihx: $(OBJ)
	@echo "\nLINK $(NAME).ihx"
	@$(SDCC) --out-fmt-ihx --code-size 8192 -o $(OUT)/$(NAME).ihx $^
	@mv $(OUT)/$(NAME).ihx $(BINF)/
	

$(OUT)/%.rel: %.c
	@echo "$< \c"
	@$(SDCC) -D $(DEFINES) -M $< > $(OUT)/.$<.tmp.d
	@mv $(OUT)/.$<.tmp.d $(OUT)/.$<.d
	@$(SDCC) -c -D $(DEFINES) -o $@ $<

clean:
	-rm -f *.rel *.lk *.map *.rst *.lst *.asm *.sym .*.d *.cdb #*.ihx
	-rm -f $(OUT)/*.rel $(OUT)/*.lk $(OUT)/*.map $(OUT)/*.rst $(OUT)/*.lst $(OUT)/*.asm $(OUT)/*.sym $(OUT)/.*.d $(OUT)/*.cdb 

uartk: 
	$(eval override SUFFIX=uart)
	$(eval override PACKAGE=stm8s103f3)
	$(eval override DEFINES= STM8S103 -D I9)
	$(eval override FLASH=8192)
	$(eval override NAME=$(NAME)-$(SUFFIX))
	
testk: 
	$(eval override SUFFIX=test)
	$(eval override PACKAGE=stm8s103f3)
	$(eval override DEFINES= STM8S103 -D TEST -D I30)
	$(eval override FLASH=8192)
	$(eval override NAME=$(NAME)-$(SUFFIX))

lightk: 
	$(eval override SUFFIX=light)
	$(eval override PACKAGE=stm8s103f3)
	$(eval override DEFINES= STM8S103 -D LIGHT -D I9)
	$(eval override FLASH=8192)
	$(eval override NAME=$(NAME)-$(SUFFIX))

uart: uartk define all
	@a=5
	
test: testk define all
	@a=5

light: lightk define all
	@a=5

define:
	@python eeprom.py define

eeptxt:
	@python eeprom.py txt 
	
eepbin:
	@python eeprom.py bin $(SUFFIX)
	@python eeprom.py txt $(SUFFIX)

flash:
	stm8flash -c stlinkv2 -p $(PACKAGE) -s eeprom -w $(BINF)/$(NAME).bin
#	stm8flash -c stlinkv2 -p $(PACKAGE) -s eeprom -v $(BINF)/$(NAME).bin
	stm8flash -c stlinkv2 -p $(PACKAGE) -s flash -w $(BINF)/$(NAME).ihx
#	stm8flash -c stlinkv2 -p $(PACKAGE) -s flash -v $(BINF)/$(NAME).ihx

flashtest: testk eepbin flash
	@echo finish flashing test
	
flashuart: uartk eepbin unlock flash setrop103
	@echo finish flashing uart
	
flashlight: lightk eepbin unlock flash setrop103
	@echo finish flashing light


scpdev: devk
	scp $(BINF)/$(NAME).ihx root@192.168.24.150:/root/
	scp $(BINF)/$(NAME).bin root@192.168.24.150:/root/

scpuart: uartk
	scp $(BINF)/$(NAME).ihx root@192.168.24.150:/root/
	scp $(BINF)/$(NAME).bin root@192.168.24.150:/root/

scplight: lightk
	scp $(BINF)/$(NAME).ihx root@192.168.24.150:/root/
	scp $(BINF)/$(NAME).bin root@192.168.24.150:/root/

hex:
#	packihx $(OUT)/$(NAME).ihx > $(NAME)-`date +%Y%m%d-%H%M%S`.hex
	packihx $(NAME).ihx > $(NAME)-`date +%Y%m%d-%H%M%S`.hex

.PHONY: all clean size

eepread:
	stm8flash -c stlinkv2 -p $(PACKAGE) -s eeprom -r eeprom.bin

eepwrite:
	stm8flash -c stlinkv2 -p $(PACKAGE) -s eeprom -w eeprom.bin

eeprom:
	python eeprom.py $(NAME).bin

eep7r:
	stm8flash -c stlinkv2 -p $(PACKAGE) -s eeprom -r $(NAME).bin

eep7w:
	stm8flash -c stlinkv2 -p $(PACKAGE) -s eeprom -w $(NAME).bin

opt103:
	echo "00 00 ff 91 6e 00 ff 00 ff 00 ff" | xxd -r -p > $(OUT)/opt103.bin
	stm8flash -c stlinkv2 -p stm8s103f3 -s opt -w $(OUT)/opt103.bin
#	rm -f $(OUT)/opt.bin

opt105:
	echo "00 00 ff 6a 95 00 ff 00 ff 00 ff" | xxd -r -p > $(OUT)/opt105.bin
	stm8flash -c stlinkv2 -p stm8s105?4 -s opt -w $(OUT)/opt105.bin
#	rm -f $(OUT)/opt.bin

rop103:
	echo "aa 00 ff 91 6e 00 ff 00 ff 00 ff" | xxd -r -p > $(OUT)/rop103.bin
	stm8flash -c stlinkv2 -p stm8s103f3 -s opt -w $(OUT)/rop103.bin
#	rm -f $(OUT)/rop.bin
	
rop105:
	echo "aa 3e c1 6a 95 00 ff 00 ff 00 ff" | xxd -r -p > $(OUT)/rop105.bin
	stm8flash -c stlinkv2 -p stm8s105k4 -s opt -w $(OUT)/rop105.bin
#	rm -f $(OUT)/rop.bin

reset: unlock

unlock:
	stm8flash -c stlinkv2 -p $(PACKAGE) -u

