#
#       !!!! Do NOT edit this makefile with an editor which replace tabs by spaces !!!!   
#
##############################################################################################
#
# On command line:
#
# make all = Create project
#
# make clean = Clean project files.
#
# To rebuild project do "make clean" and "make all".
#
# Included originally in the yagarto projects. Original Author : Michael Fischer
# Modified to suit our purposes by Hussam Al-Hertani
# Use at your own risk!!!!!
##############################################################################################
# Start of default section
#
CCPREFIX = arm-none-eabi-
CC   = $(CCPREFIX)gcc
CP   = $(CCPREFIX)objcopy
OD   = $(CCPREFIX)objdump --disassemble-all --source
#--disassemble -D -S -x
AS   = $(CCPREFIX)gcc -x assembler-with-cpp
GDBTUI = $(CCPREFIX)gdbtui
HEX  = $(CP) -O ihex
BIN  = $(CP) -O binary -S
MCU  = cortex-m0
 
.SUFFIXES:

.SUFFIXES: .c .o. elf. hex .bin .dump


# List all C defines here
DDEFS = 
#
# Define project name and Ram/Flash mode here
PROJECT        = lDec32
 
# List C source files here

SRC  = ./main.c
#SRC += ./debug.c
#SRC += ./awaDrehEncoder/awa_DrehEncoder.c
#SRC += ./cmsis_boot/system_stm32f0xx_temp.c
SRC += ./cmsis_boot/system_stm32f0xx.c
#SRC += ./dogmLCD/awa_lcd_spi.c
#LNetC
#SRC += ./stdio/printf.c
#SRC += ./stm32_lib/src/stm32f0xx_adc.c
#SRC += ./stm32_lib/src/stm32f0xx_crc.c
#SRC += ./stm32_lib/src/stm32f0xx_dma.c
#SRC += ./stm32_lib/src/stm32f0xx_exti.c
#SRC += ./stm32_lib/src/stm32f0xx_flash.c
SRC += ./stm32_lib/src/stm32f0xx_gpio.c
#SRC += ./stm32_lib/src/stm32f0xx_i2c.c
#SRC += ./stm32_lib/src/stm32f0xx_iwdg.c
#SRC += ./stm32_lib/src/stm32f0xx_misc.c
#SRC += ./stm32_lib/src/stm32f0xx_pwr.c
SRC += ./stm32_lib/src/stm32f0xx_rcc.c
#SRC += ./stm32_lib/src/stm32f0xx_rtc.c
#SRC += ./stm32_lib/src/stm32f0xx_spi.c
#SRC += ./stm32_lib/src/stm32f0xx_syscfg.c
#SRC += ./stm32_lib/src/stm32f0xx_tim.c
#SRC += ./stm32_lib/src/stm32f0xx_usart.c
#SRC += ./stm32_lib/src/stm32f0xx_wwdg.c

#SRC += ./sysTick.c


# List assembly startup source file here
STARTUP = ./cmsis_boot/startup/startup_stm32f0xx.s
 
# List all include directories here
INCDIRS = ./awaDrehEncoder ./cmsis_boot ./cmsis_core ./dogmLCD ./stm32_lib/inc

# List the user directory to look for the libraries here
LIBDIRS += 
 
# List all user libraries here
LIBS =
 
# Define optimisation level here
OPT = -O0
 

# Define linker script file here
#LINKER_SCRIPT = ./linker/stm32f0_linker.ld
LINKER_SCRIPT = ./linker/arm-gcc-link.ld

 
INCDIR  = $(patsubst %,-I%, $(INCDIRS))
LIBDIR  = $(patsubst %,-L%, $(LIBDIRS))
LIB     = $(patsubst %,-l%, $(LIBS))
##reference only flags for run from ram...not used here
##DEFS    = $(DDEFS) $(UDEFS) -DRUN_FROM_FLASH=0 -DVECT_TAB_SRAM

## run from Flash
DEFS    = $(DDEFS) -DRUN_FROM_FLASH=1 -DSTM32F050F6 -DSTM32F0XX_LD -DUSE_STD_PERIPH_DRIVER -D__ASSEMBLY__

OBJS  = $(STARTUP:.s=.o) $(SRC:.c=.o)
MCFLAGS = -mcpu=$(MCU)
 
ASFLAGS = $(MCFLAGS) -g -gdwarf-2 -mthumb  -Wa,-amhls=$(<:.s=.lst) 
CPFLAGS = $(MCFLAGS) $(OPT) -g -gdwarf-2 -mthumb   -fomit-frame-pointer -Wall -Wstrict-prototypes -fverbose-asm -ffunction-sections -Wa,-ahlms=$(<:.c=.lst) $(DEFS)
LDFLAGS = $(MCFLAGS) -g -gdwarf-2 -mthumb -nostartfiles -T$(LINKER_SCRIPT) -Wl,-Map=$(PROJECT).map,--gc-sections $(LIBDIR) $(LIB)
#,--no-warn-mismatch,--cref

#
# makefile rules
#
 
all: $(OBJS) $(PROJECT).elf  $(PROJECT).hex $(PROJECT).bin $(PROJECT).dump 
	$(CCPREFIX)size $(PROJECT).elf
#$(TRGT)size $(PROJECT).elf
 
%.o: %.c
	$(CC) -c $(CPFLAGS) -I . $(INCDIR) $< -o $@

%.o: %.s
	$(AS) -c $(ASFLAGS) $< -o $@

%.elf: $(OBJS)
	$(CC) $(OBJS) $(LDFLAGS) $(LIBS) -o $@

%.dump:	%.elf
	$(OD) $< > $@

%.hex:	%.elf
	$(HEX) $< $@

%.bin:	%.elf
	$(BIN)  $< $@

flash_openocd: $(PROJECT).bin
	openocd -s ~/EmbeddedArm/openocd-bin/share/openocd/scripts/ -f interface/stlink-v2.cfg -f target/stm32f0x_stlink.cfg -c "init" -c "reset halt" -c "sleep 100" -c "wait_halt 2" -c "flash write_image erase $(PROJECT).bin 0x08000000" -c "sleep 100" -c "verify_image $(PROJECT).bin 0x08000000" -c "sleep 100" -c "reset run" -c shutdown

flash_stlink:	#$(PROJECT).bin
	sync
	st-flash write $(PROJECT).bin 0x8000000

erase_openocd:
	openocd -s ~/EmbeddedArm/openocd-bin/share/openocd/scripts/ -f interface/stlink-v2.cfg -f target/stm32f0x_stlink.cfg -c "init" -c "reset halt" -c "sleep 100" -c "stm32f1x mass_erase 0" -c "sleep 100" -c shutdown 

erase_stlink:
	st-flash erase

debug_openocd: $(PROJECT).elf flash_openocd
	xterm -e openocd -s ~/EmbeddedArm/openocd-bin/share/openocd/scripts/ -f interface/stlink-v2.cfg -f target/stm32f0x_stlink.cfg -c "init" -c "halt" -c "reset halt" &
	$(GDBTUI) --eval-command="target remote localhost:3333" $(PROJECT).elf 

debug_stlink: $(PROJECT).elf
	xterm -e st-util &
	$(GDBTUI) --eval-command="target remote localhost:4242"  $(PROJECT).elf -ex 'load'

clean:
	-rm -rf $(OBJS)
	-rm -rf $(PROJECT).elf
	-rm -rf $(PROJECT).map
	-rm -rf $(PROJECT).hex
	-rm -rf $(PROJECT).bin
	-rm -rf $(PROJECT).dump
	-rm -rf $(SRC:.c=.lst)
	-rm -rf $(ASRC:.s=.lst)
# *** EOF ***
