cur_dir := $(dir $(lastword $(MAKEFILE_LIST)))

obj-y := $(patsubst $(cur_dir)%,%,$(wildcard $(cur_dir)*.c $(cur_dir)*.S))
obj-y := $(obj-y:.c=.o)
obj-y := $(obj-y:.S=.o)

obj-y += $(CHIP)/

subdir-ccflags-y += -Iplatform/drivers/ana

# PMU settings
export PMU_CFG_FLAGS :=

# ANALOG settings
export ANA_CFG_FLAGS :=

# USBPHY settings
export USBPHY_CFG_FLAGS :=

export PMU_DCDC_CALIB ?= 1
ifeq ($(PMU_DCDC_CALIB),1)
PMU_CFG_FLAGS += -DPMU_DCDC_CALIB
endif

ifeq ($(PMU_FULL_INIT),1)
PMU_CFG_FLAGS += -DPMU_FULL_INIT
PMU_INIT := 1
endif
ifeq ($(PMU_INIT),1)
PMU_CFG_FLAGS += -DPMU_INIT
endif

ifeq ($(NO_SLEEP),1)
PMU_CFG_FLAGS += -DNO_SLEEP
endif

ifeq ($(MCU_HIGH_PERFORMANCE_MODE),1)
PMU_CFG_FLAGS += -DMCU_HIGH_PERFORMANCE_MODE
endif

ifneq ($(CHIP),best1000)
ifneq ($(PROGRAMMER),1)
VCRYSTAL_OFF ?= 1
endif
endif
ifeq ($(VCRYSTAL_OFF),1)
PMU_CFG_FLAGS += -DVCRYSTAL_OFF
endif

ifeq ($(VMEM_ON),1)
PMU_CFG_FLAGS += -DVMEM_ON
endif

ifeq ($(VUSB_ON),1)
PMU_CFG_FLAGS += -DVUSB_ON
endif

ifeq ($(ULTRA_LOW_POWER),1)
PMU_CFG_FLAGS += -DULTRA_LOW_POWER
endif

ifeq ($(DIGMIC_HIGH_VOLT),1)
PMU_CFG_FLAGS += -DDIGMIC_HIGH_VOLT
endif

ifeq ($(ANA_26M_X4_ENABLE),1)
PMU_CFG_FLAGS += -DANA_26M_X4_ENABLE
endif
ifeq ($(OSC_26M_X4_AUD2BB),1)
PMU_CFG_FLAGS += -DOSC_26M_X4_AUD2BB
endif

ifeq ($(DIG_OSC_X4_ENABLE),1)
PMU_CFG_FLAGS += -DDIG_OSC_X4_ENABLE
endif

ifeq ($(RTC_ENABLE),1)
PMU_CFG_FLAGS += -DRTC_ENABLE
endif

ifeq ($(CLK_32K_FROM_OSC),1)
PMU_CFG_FLAGS += -DCLK_32K_FROM_OSC
endif

ifeq ($(CHIP_HAS_DCO),1)
PMU_CFG_FLAGS += -DCHIP_HAS_DCO
ANA_CFG_FLAGS += -DCHIP_HAS_DCO
endif

ifeq ($(USB_HIGH_SPEED),1)
PMU_CFG_FLAGS += -DUSB_HIGH_SPEED
USBPHY_CFG_FLAGS += -DUSB_HIGH_SPEED
endif

ifeq ($(CHIP_HAS_USBPHY),1)
USBPHY_CFG_FLAGS += -DCHIP_HAS_USBPHY
ifeq ($(CHIP_HAS_SPIPHY),1)
USBPHY_CFG_FLAGS += -DCHIP_HAS_SPIPHY
PMU_CFG_FLAGS += -DCHIP_HAS_SPIPHY
endif
endif

ifeq ($(USB_HS_LOOPBACK_TEST),1)
USBPHY_CFG_FLAGS += -DUSB_HS_LOOPBACK_TEST
endif

export PLL_CFG_FLAGS :=
ifeq ($(CHIP_HAS_SPIPHY),1)
PLL_CFG_FLAGS += -DCHIP_HAS_SPIPHY
endif

ifeq ($(POWER_MODE),LDO)
PMU_CFG_FLAGS += -DLDO_MODE
else
ifeq ($(POWER_MODE),DIG_DCDC)
PMU_CFG_FLAGS += -DDIG_DCDC_MODE
else
ifeq ($(POWER_MODE),ANA_DCDC)
PMU_CFG_FLAGS += -DANA_DCDC_MODE
else
ifneq ($(POWER_MODE),)
$(error Invalid POWER_MODE: $(POWER_MODE))
endif
endif
endif
endif

ifeq ($(MTEST_ENABLED),1)
PMU_CFG_FLAGS +=-DMTEST_ENABLED
ifneq ($(MTEST_CLK_MHZ),)
PMU_CFG_FLAGS +=-DMTEST_CLK_MHZ=$(MTEST_CLK_MHZ)
endif # MTEST_CLK_MHZ
ifneq ($(MTEST_VOLT),)
VCORE_STEM :=$(subst .,_,$(MTEST_VOLT))
ifeq ($(POWER_MODE),LDO)
PMU_CFG_FLAGS += -DMTEST_VOLT=PMU_VDIG_$(VCORE_STEM)
else
PMU_CFG_FLAGS += -DMTEST_VOLT=PMU_DCDC_DIG_$(VCORE_STEM)
endif
endif # MTEST_VOLT
endif # MTEST_ENABLED

ANA_CFG_FLAGS += $(MAX_DAC_OUTPUT_FLAGS)

ifeq ($(AUDIO_INPUT_CAPLESSMODE),1)
ANA_CFG_FLAGS += -D_MIC_CAPLESSMODE_
endif

ifeq ($(AUDIO_INPUT_LARGEGAIN),1)
ANA_CFG_FLAGS += -D_MIC_LARGEGAIN_
endif

ifeq ($(AUDIO_OUTPUT_DIFF),1)
ANA_CFG_FLAGS += -DAUDIO_OUTPUT_DIFF
PMU_CFG_FLAGS += -DAUDIO_OUTPUT_DIFF
endif

ifeq ($(AUDIO_OUTPUT_DC_CALIB),1)
ANA_CFG_FLAGS += -DAUDIO_OUTPUT_DC_CALIB
endif
ifeq ($(AUDIO_OUTPUT_DC_CALIB_ANA),1)
ANA_CFG_FLAGS += -DAUDIO_OUTPUT_DC_CALIB_ANA
endif

ifeq ($(CODEC_HIGH_QUALITY),1)
ANA_CFG_FLAGS += -DCODEC_HIGH_QUALITY
PMU_CFG_FLAGS += -DCODEC_HIGH_QUALITY
endif

ifeq ($(AUDIO_RESAMPLE),1)
ANA_CFG_FLAGS += -D__AUDIO_RESAMPLE__
PMU_CFG_FLAGS += -D__AUDIO_RESAMPLE__
endif

ifeq ($(ANC_APP),1)
ANA_CFG_FLAGS += -DANC_APP
PMU_CFG_FLAGS += -DANC_APP
endif

ifeq ($(ANC_INIT_SPEEDUP),1)
ANA_CFG_FLAGS += -DANC_INIT_SPEEDUP
endif
ifeq ($(ANC_FF_ENABLED),1)
ANA_CFG_FLAGS += -DANC_FF_ENABLED
endif
ifeq ($(ANC_FB_ENABLED),1)
ANA_CFG_FLAGS += -DANC_FB_ENABLED
endif

ifneq ($(ANA_DC_CALIB_L),)
ANA_CFG_FLAGS += -DANA_DC_CALIB_L=$(ANA_DC_CALIB_L)
endif
ifneq ($(ANA_DC_CALIB_R),)
ANA_CFG_FLAGS += -DANA_DC_CALIB_R=$(ANA_DC_CALIB_R)
endif

ifeq ($(DYN_ADC_GAIN),1)
ANA_CFG_FLAGS += -DDYN_ADC_GAIN
endif

