# Hey Emacs, this is a -*- makefile -*-
#
# Copyright (C) 2012 The Paparazzi Team
#
# 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.
#


# Verify that libopencm3 is compiled with chibios compatible float abi
# correct abi is -mfloat-abi=softfp
# on non chibios branches, libopencm3 is compiled with more optimised -mfloat-abi=hard
TAG_ABI_VFP_COUNT= \
	$(shell (readelf -A  $(PAPARAZZI_SRC)/sw/ext/libopencm3/lib/libopencm3_stm32f4.a | \
	grep -c Tag_ABI_VFP_args))

ifneq ($(TAG_ABI_VFP_COUNT),0)
  $(error libopencm3_stm32f4.a is generated with chibios incompatible -mfloat-abi=hard, \
	please recompile with -mfloat-abi=softfp))
endif


#
# This is the common Makefile for target using chibios

CHIBIOS_BOARD_DIR = $(PAPARAZZI_SRC)/sw/airborne/boards/$(BOARD)/chibios-libopencm3
CHIBIOS_LIB_DIR = $(PAPARAZZI_SRC)/sw/airborne/subsystems/chibios-libopencm3
CHIBIOS_EXT =  $(PAPARAZZI_SRC)/sw/ext/chibios
OPENCM3_EXT =  $(PAPARAZZI_SRC)/sw/ext/libopencm3
PPRZ_GENERATED = $(AIRCRAFT_BUILD_DIR)/$(AIRCRAFT)/$(TARGET)/generated
PPRZ_CHIBIOS_OCM3 = $(SRC_FIRMWARE)/chibios-libopencm3

# Launch with "make Q=''" to get full command display
Q=@

#
# General rules
#

$(TARGET).srcsnd = $(notdir $($(TARGET).srcs))
$(TARGET).objso	= $($(TARGET).srcs:%.c=$(OBJDIR)/%.o)
$(TARGET).objs	= $($(TARGET).objso:%.S=$(OBJDIR)/%.o)



##############################################################################
# Build global options
# NOTE: Can be overridden externally.
#

# Paparazzi options here.
ifeq ($(PPRZ_DEFINITION),)
  PPRZ_DEFINITION = -DRTOS_IS_CHIBIOS -DUSE_ADC_WATCHDOG -DSYS_TIME_FREQUENCY=1000
endif

# Compiler options here.
ifeq ($(USE_OPT),)
  # ChibiOS already define OPT
  CH_OPT ?= 0 -g -ggdb3 -fno-inline

  USE_OPT = -std=gnu11 -O$(CH_OPT) \
	-falign-functions=16  -fno-omit-frame-pointer\
	-W -Wall -Werror -Wno-error=unused-variable -Wno-error=format \
	-Wno-error=unused-function -Wno-error=unused-parameter \
	$(PPRZ_DEFINITION)
endif


# C specific options here (added to USE_OPT).
ifeq ($(USE_COPT),)
  USE_COPT =
endif

# C++ specific options here (added to USE_OPT).
ifeq ($(USE_CPPOPT),)
  USE_CPPOPT = -fno-rtti
endif

# Enable this if you want the linker to remove unused code and data
ifeq ($(USE_LINK_GC),)
  USE_LINK_GC = yes
endif

# If enabled, this option allows to compile the application in THUMB mode.
ifeq ($(USE_THUMB),)
  USE_THUMB = yes
endif

# Enable this if you want to see the full log while compiling.
ifeq ($(USE_VERBOSE_COMPILE),)
  ifeq ($(Q),@)
    USE_VERBOSE_COMPILE = no
  else
    USE_VERBOSE_COMPILE = yes
  endif
endif

#
# Build global options
##############################################################################

##############################################################################
# Architecture or project specific options
#

# Enables the use of FPU on Cortex-M4.
ifeq ($(USE_FPU),)
  USE_FPU = yes
endif


#
# Architecture or project specific options
##############################################################################

##############################################################################
# Project, sources and paths
#

# Define project name here
PROJECT = ap

# Imported source files and paths

CHIBIOS = $(CHIBIOS_EXT)
OPENCM3 = $(OPENCM3_EXT)
OPENCM3_INC = $(OPENCM3)/include
OPENCM3_LIB =  $(OPENCM3)/lib/

include $(CHIBIOS_BOARD_DIR)/board.mk
include $(CHIBIOS)/os/hal/platforms/STM32F4xx/platform.mk
include $(CHIBIOS)/os/hal/hal.mk
include $(CHIBIOS)/os/ports/GCC/ARMCMx/STM32F4xx/port.mk
#include $(CHIBIOS)/os/various/fatfs_bindings/fatfs.mk
include $(PAPARAZZI_HOME)/conf/chibios/fatfs.mk
include $(PAPARAZZI_HOME)/conf/chibios/chibi_lib_for_pprz.mk

# HACK : in order to make chibios to work with opencm3, we need a specially crafted
#        vector.c file, with some vectors for chibios, and others for opencm3
#	 shunting should be done in mcuconf.h
PORTSRC := $(subst $(CHIBIOS)/os/ports/GCC/ARMCMx/STM32F4xx/vectors.c,\
	$(PAPARAZZI_SRC)/sw/airborne/arch/stm32/stm32f4_chibios_vectors.c,\
	$(PORTSRC))

include $(CHIBIOS)/os/kernel/kernel.mk

# Define linker script file here
LDSCRIPT= $($(TARGET).LDSCRIPT)


# C sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CSRC = $(PORTSRC) \
       $(KERNSRC) \
       $(TESTSRC) \
       $(HALSRC) \
       $(PLATFORMSRC) \
       $(BOARDSRC) \
       $(FATFSSRC) \
       $(CHIBIOSLIBSRC) \
       $(CHIBIOS)/os/various/chrtclib.c \
       $(CHIBIOS)/os/various/syscalls.c

ECSRC = $($(TARGET).srcs)

# C++ sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CPPSRC =

# C sources to be compiled in ARM mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
ACSRC =

# C++ sources to be compiled in ARM mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
ACPPSRC =

# C sources to be compiled in THUMB mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
TCSRC =

# C sources to be compiled in THUMB mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
TCPPSRC =

# List ASM source files here
ASMSRC = $(PORTASM)

INCDIR = $(PORTINC) $(KERNINC) $(TESTINC) \
         $(HALINC) $(PLATFORMINC) $(BOARDINC) \
         $(CHIBIOS)/os/various $(OPENCM3_INC) \
	 $(CHIBIOS_BOARD_DIR) $(CHIBIOS_LIB_DIR) \
	 $(PPRZ_GENERATED) $(FATFSINC) \
	 $(PPRZ_CHIBIOS_OCM3)

BUILDDIR := $(OBJDIR)
#
# Project, sources and paths
##############################################################################

##############################################################################
# Compiler settings
#

MCU  = cortex-m4

#TRGT = arm-elf-
TRGT = arm-none-eabi-
CC   = $(TRGT)gcc
CPPC = $(TRGT)g++
# Enable loading with g++ only if you need C++ runtime support.
# NOTE: You can use C++ even without C++ support if you are careful. C++
#       runtime support makes code size explode.
LD   = $(TRGT)gcc
#LD   = $(TRGT)g++
CP   = $(TRGT)objcopy
AS   = $(TRGT)gcc -x assembler-with-cpp
OD   = $(TRGT)objdump
HEX  = $(CP) -O ihex
BIN  = $(CP) -O binary

# ARM-specific options here
AOPT =

# THUMB-specific options here
TOPT = -mthumb -DTHUMB

# Define C warning options here
CWARN = -Wall -Wextra -Wstrict-prototypes

# Define C++ warning options here
CPPWARN = -Wall -Wextra

#
# Compiler settings
##############################################################################

##############################################################################
# Start of default section
#

# List all default C defines here, like -D_DEBUG=1
DDEFS =  -DSTM32F4
#DDEFS =

# List all default ASM defines here, like -D_DEBUG=1
DADEFS =

# List all default directories to look for include files here
DINCDIR =

# List the default directory to look for the libraries here
DLIBDIR = $(OPENCM3_LIB)

# List all default libraries here
DLIBS = -lm -lopencm3_stm32f4
#DLIBS = -lm -lc -lnosys -nostartfiles -Wl,--gc-sections

#
# End of default section
##############################################################################

##############################################################################
# Start of user section
#

# List all user C define here, like -D_DEBUG=1
UDEFS = $($(TARGET).CFLAGS) $(USER_CFLAGS) -DUSE_OCM3_SYSTICK_INIT=0

# Define ASM defines here
UADEFS =

# List all user directories here
# remove -I before include dir because ChibiOS is adding them again
UINCDIR = $(patsubst -I%,%,$(INCLUDES))

# List the user directory to look for the libraries here
ULIBDIR =

# List all user libraries here
ULIBS =

#
# End of user defines
##############################################################################

#
# Include upload rules
##############################################################################
include $(PAPARAZZI_HOME)/conf/Makefile.arm-embedded-toolchain
include $(PAPARAZZI_HOME)/conf/Makefile.stm32-upload


ifeq ($(USE_FPU),yes)
  USE_OPT += -mfloat-abi=softfp  -mfpu=fpv4-sp-d16 -fsingle-precision-constant
  DDEFS += -DCORTEX_USE_FPU=TRUE
else
  DDEFS += -DCORTEX_USE_FPU=FALSE
endif

EXTRA_RULES_INCLUDE_PATH = $(PAPARAZZI_HOME)/conf/chibios/chibios_extra_rules.mk
include $(PAPARAZZI_HOME)/conf/chibios/chibios_rules.mk




#include $(CHIBIOS_EXT)/os/ports/GCC/ARMCMx/rules.mk
