# Hey Emacs, this is a -*- makefile -*-
#
#   Copyright (C) 2009 Antoine Drouin
#
# This file is part of paparazzi.
#
# paparazzi is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# paparazzi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with paparazzi; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

#
# This is the common Makefile for the stm32-target.
#

SRC_ARCH = arch/stm32

# Pretty Printer
# Call with "make Q=''" to get full command display
Q=@


#
# find compiler toolchain
#
include $(PAPARAZZI_SRC)/conf/Makefile.arm-common


MCU   = cortex-m3
#DEBUG = dwarf-2
OPT   = s
#OPT   = 2
#OPT   = 0


PYTHON ?= $(shell which python)

ifneq ($(BOARD_SERIAL),)
OOCD_OPTIONS = -c "ft2232_serial $(BOARD_SERIAL)"
endif

LOADER ?= $(PAPARAZZI_SRC)/sw/tools/stm32loader/stm32loader.py

ifndef $(TARGET).OOCD_INTERFACE
OOCD_INTERFACE = lisa-l
else
OOCD_INTERFACE =  $($(TARGET).OOCD_INTERFACE)
endif

ifndef $(TARGET).OOCD_BOARD
OOCD_BOARD = lisa-l
else
OOCD_BOARD =  $($(TARGET).OOCD_BOARD)
endif

# default: assume the luftboot bootloader is used
# if luftboot is not used define NO_LUFTBOOT to a value != 0
OOCD_START_SECTOR = 4
ASSUMING_LUFTBOOT = "yes"
ifdef NO_LUFTBOOT
$(shell echo luftboot: $(NO_LUFTBOOT))
ifneq ($(NO_LUFTBOOT),0)
OOCD_START_SECTOR = 0
ASSUMING_LUFTBOOT = "no"
endif
endif

# input files
SRCS = $($(TARGET).srcs)
#ASRC =

# object files
COBJ    = $(SRCS:%.c=$(OBJDIR)/%.o)
AOBJ    = $(ASRC:%.S=$(OBJDIR)/%.o)

# linker script :
#   if LDSCRIPT is defined in the airframe use that independantly of TARGET
#   if not, and a TARGET.LDSCRIPT is defined, use that
#   if not, use the default STM32f103re_flash.ld
ifndef LDSCRIPT
ifndef $(TARGET).LDSCRIPT
$(warning Linker script for target "$(TARGET)" on board "$(BOARD)" not defined. Using stm32default.ld.)
LDSCRIPT = $(SRC_ARCH)/stm32default.ld
else
LDSCRIPT = $($(TARGET).LDSCRIPT)
$(info Using "$($(TARGET).LDSCRIPT)" as ldscript for target "$(TARGET)".)
endif
endif

CFLAGS = -I. -I./$(ARCH) -I../ext/libopencm3/include $(INCLUDES) -D__thumb2__ -Wall -msoft-float -O$(OPT)
CFLAGS += -Wl,--gc-sections
CFLAGS += -mfix-cortex-m3-ldrd
CFLAGS += -mcpu=$(MCU) -mthumb -ansi
CFLAGS += -std=gnu99
#CFLAGS += -malignment-traps
CFLAGS += -fno-common
CFLAGS += -g$(DEBUG)
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -Wimplicit
CFLAGS += -Wcast-align
CFLAGS += -Wpointer-arith -Wswitch
CFLAGS += -Wredundant-decls -Wreturn-type -Wshadow -Wunused
CFLAGS += -Wa,-adhlns=$(OBJDIR)/$(notdir $(subst $(suffix $<),.lst,$<))
CFLAGS += -Wnested-externs
CFLAGS += -Wmissing-prototypes
CFLAGS += -Wstrict-prototypes
CFLAGS += -Wmissing-declarations
CFLAGS += -Wswitch-default
CFLAGS += -DSTM32F1

CFLAGS += $($(TARGET).CFLAGS)

AFLAGS  = -ahls
AFLAGS += -mcpu=$(MCU) -mthumb
AFLAGS += -x assembler-with-cpp -Wa,-adhlns=$(OBJDIR)/$(<:.S=.lst),--g$(DEBUG)

LDFLAGS = -L../ext/libopencm3/lib
LDFLAGS += -T$(LDSCRIPT) -nostartfiles -O$(OPT)
LDFLAGS += -mthumb -mcpu=$(MCU) -msoft-float -mfix-cortex-m3-ldrd
LDFLAGS += -Wl,-Map=$(OBJDIR)/$(TARGET).map,--cref,--gc-sections
LDLIBS  += -lc -lm -lgcc -lopencm3_stm32f1

CPFLAGS = -j .isr_vector -j .text -j .data
CPFLAGS_BIN = -Obinary
CPFLAGS_HEX = -Oihex

ODFLAGS = -S



# Default target.
all: printcommands sizebefore build sizeafter

build: elf bin hex
# lss sym

elf: $(OBJDIR)/$(TARGET).elf
bin: $(OBJDIR)/$(TARGET).bin
hex: $(OBJDIR)/$(TARGET).hex
lss: $(OBJDIR)/$(TARGET).lss
sym: $(OBJDIR)/$(TARGET).sym

%.bin: %.elf
	@echo OBJCB $@
	$(Q)$(CP) $(CPFLAGS) $(CPFLAGS_BIN) $< $@

%.hex: %.elf
	@echo OBJCH $@
	$(Q)$(CP) $(CPFLAGS) $(CPFLAGS_HEX) $< $@

# Create extended listing file from ELF output file.
# testing: option -C
%.lss: %.elf
	@echo OBJD $@
	$(Q)$(DMP) -h -S -C $< > $@


# Create a symbol table from ELF output file.
%.sym: %.elf
	@echo NM $@
	$(Q)$(NM) -n $< > $@


# Link: create ELF output file from object files.
.SECONDARY : $(OBJDIR)/$(TARGET).elf
.PRECIOUS : $(COBJ) $(AOBJ)
%.elf:  $(COBJ) $(AOBJ)
	@echo LD $@
	$(Q)$(LD) $(LDFLAGS) $($(TARGET).LDFLAGS) -o $@ $(COBJ) $(AOBJ) $(LDLIBS)

# Compile: create object files from C source files.
$(OBJDIR)/%.o : %.c $(OBJDIR)/../Makefile.ac
	@echo CC $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CC) -c $(CFLAGS) $< -o $@

# Assemble: create object files from assembler source files. ARM/Thumb
$(AOBJ) : $(OBJDIR)/%.o : %.S
	@echo AS $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CC) -c $(AFLAGS) $< -o $@

#
# check which flash mode is configured
#
ifeq ($(FLASH_MODE),DFU)
#
# DFU flash mode
upload: $(OBJDIR)/$(TARGET).bin
	@echo "Using stm32 mem dfu loader"
	$(PYTHON) $(PAPARAZZI_SRC)/sw/tools/dfu/stm32_mem.py $^
#
# serial flash mode
else ifeq ($(FLASH_MODE),SERIAL)
upload: $(OBJDIR)/$(TARGET).bin
	$(LOADER) -p /dev/ttyUSB0  -b 115200 -e -w -v $^
#
# JTAG flash mode
else ifeq ($(FLASH_MODE),JTAG)
# either via normal jtag or BlackMagicProbe
ifeq ($(BMP_PORT),)
# normal jtag via OpenOCD
upload: $(OBJDIR)/$(TARGET).hex
	@echo "Assuming luftboot bootloader: $(ASSUMING_LUFTBOOT)"
	@echo "Using OOCD = $(OOCD)"
	@echo "  OOCD\t$<"
	$(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
		-f board/$(OOCD_BOARD).cfg $(OOCD_OPTIONS) \
		-c init \
		-c "reset halt" \
		-c "reset init" \
		-c "flash erase_sector 0 $(OOCD_START_SECTOR) last" \
		-c "flash write_image $<" \
		-c reset \
		-c shutdown
else
# jtag via BMP
BMP_UPLOAD_SCRIPT ?= $(PAPARAZZI_SRC)/sw/tools/flash_scripts/bmp_jtag_flash.scr

upload: $(OBJDIR)/$(TARGET).elf
	@echo "Assuming luftboot bootloader: $(ASSUMING_LUFTBOOT)"
	@echo "Using Black Magic Probe with JTAG on BMP_PORT $(BMP_PORT)"
	@echo "Using GDB = $(GDB)"
	@echo "  BMP\t$<"
	$(Q)$(GDB) --batch \
		   -ex 'target extended-remote $(BMP_PORT)' \
		   -x $(BMP_UPLOAD_SCRIPT) \
		   $<
endif
#
# SWD flash mode
else ifeq ($(FLASH_MODE),SWD)
# only works if BMP_PORT is defined
BMP_PORT ?= /dev/ttyACM0
BMP_UPLOAD_SCRIPT ?= $(PAPARAZZI_SRC)/sw/tools/flash_scripts/bmp_swd_flash.scr
upload: $(OBJDIR)/$(TARGET).elf
	@echo "Assuming luftboot bootloader: $(ASSUMING_LUFTBOOT)"
	@echo "Using Black Magic Probe with SWD on BMP_PORT $(BMP_PORT)"
	@echo "Using GDB = $(GDB)"
	@echo "  BMP\t$<"
	$(Q)$(GDB) --batch \
		   -ex 'target extended-remote $(BMP_PORT)' \
		   -x $(BMP_UPLOAD_SCRIPT) \
		   $<
#
# no known flash mode
else
upload:
	@echo unknown flash_mode $(FLASH_MODE)
endif


# Listing of phony targets.
.PHONY : all build elf bin lss sym upload start_gdb start_telnet

#
# Dependencies
#

$(OBJDIR)/.depend:
	@echo DEPEND $@
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR)
	$(Q)$(CC) -MM -MG $(THUMB) $(CFLAGS) $($(TARGET).srcs) | sed 's|\([^\.]*\.o\)|$(OBJDIR)/\1|' > $@

ifneq ($(MAKECMDGOALS),clean)
-include $(OBJDIR)/.depend
endif
