############################################################################
# arch/xtensa/src/esp32s2/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################

include common/Make.defs

STORAGETMP = $(if $(wildcard $(NXTMPDIR)/.*),y,)

# The start-up, "head", file.  May be either a .S or a .c file.

HEAD_CSRC  = esp32s2_start.c esp32s2_wdt.c

# Required ESP32-S2 files (arch/xtensa/src/esp32s2)

CHIP_CSRCS  = esp32s2_allocateheap.c esp32s2_clockconfig.c esp32s2_irq.c
CHIP_CSRCS += esp32s2_gpio.c esp32s2_rtc_gpio.c esp32s2_region.c esp32s2_user.c
CHIP_CSRCS += esp32s2_timerisr.c esp32s2_lowputc.c esp32s2_systemreset.c
CHIP_CSRCS += esp32s2_dma.c esp32s2_libc_stubs.c

# Configuration-dependent ESP32-S2 files

ifneq ($(CONFIG_ARCH_IDLE_CUSTOM),y)
CHIP_CSRCS += esp32s2_idle.c
endif

ifeq ($(CONFIG_ESP32S2_UART),y)
CHIP_CSRCS += esp32s2_serial.c
endif

ifeq ($(CONFIG_ESP32S2_EFUSE),y)
CHIP_CSRCS += esp32s2_efuse.c
CHIP_CSRCS += esp32s2_efuse_table.c
CHIP_CSRCS += esp32s2_efuse_lowerhalf.c
endif

ifeq ($(CONFIG_ESP32S2_RNG),y)
CHIP_CSRCS += esp32s2_rng.c
endif

ifeq ($(CONFIG_ESP32S2_I2C),y)
ifeq ($(CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE),y)
CHIP_CSRCS += esp32s2_i2c.c
endif
endif

ifeq ($(CONFIG_ESP32S2_I2S),y)
CHIP_CSRCS += esp32s2_i2s.c
endif

ifeq ($(CONFIG_ESP32S2_TWAI),y)
CHIP_CSRCS += esp32s2_twai.c
endif

ifeq ($(CONFIG_ESP32S2_LEDC),y)
CHIP_CSRCS += esp32s2_ledc.c
endif

ifeq ($(CONFIG_ESP32S2_SPI),y)
CHIP_CSRCS += esp32s2_spi.c
ifeq ($(CONFIG_SPI_SLAVE),y)
CHIP_CSRCS += esp32s2_spi_slave.c
endif
endif

ifeq ($(CONFIG_ESP32S2_PARTITION_TABLE),y)
CHIP_CSRCS += esp32_partition.c
endif

ifeq ($(CONFIG_ESP32S2_TIMER),y)
CHIP_CSRCS += esp32s2_tim.c
ifeq ($(CONFIG_TIMER),y)
CHIP_CSRCS += esp32s2_tim_lowerhalf.c
endif
endif

ifeq ($(CONFIG_WATCHDOG),y)
CHIP_CSRCS += esp32s2_wdt_lowerhalf.c
endif

ifeq ($(CONFIG_ARCH_HAVE_EXTRA_HEAPS),y)
CHIP_CSRCS += esp32s2_extraheaps.c
endif

ifeq ($(CONFIG_ESP32S2_RTC_HEAP),y)
CHIP_CSRCS += esp32s2_rtcheap.c
endif

ifeq ($(CONFIG_ARCH_USE_TEXT_HEAP),y)
CHIP_CSRCS += esp32s2_textheap.c
endif

ifeq ($(CONFIG_ESP32S2_FREERUN),y)
CHIP_CSRCS += esp32s2_freerun.c
endif

ifeq ($(CONFIG_ESP32S2_RT_TIMER),y)
CHIP_CSRCS += esp32s2_rt_timer.c
endif

ifeq ($(CONFIG_ESP32S2_TOUCH),y)
CHIP_CSRCS += esp32s2_touch.c
endif

ifeq ($(CONFIG_ESP32S2_ONESHOT),y)
CHIP_CSRCS += esp32s2_oneshot.c
ifeq ($(CONFIG_ONESHOT),y)
CHIP_CSRCS += esp32s2_oneshot_lowerhalf.c
endif
endif

ifeq ($(CONFIG_ESP32S2_SPIRAM),y)
CHIP_CSRCS += esp32s2_spiram.c
CHIP_CSRCS += esp32s2_psram.c
endif

CHIP_CSRCS += esp32s2_rtc.c

ifeq ($(CONFIG_RTC_DRIVER),y)
CHIP_CSRCS += esp32s2_rtc_lowerhalf.c
endif

ifeq ($(CONFIG_ESPRESSIF_WIFI),y)
CHIP_CSRCS += esp32s2_wifi_adapter.c
endif

#############################################################################
# Espressif HAL for 3rd Party Platforms
#############################################################################

# Fetch source files and add them to build

ESP_HAL_3RDPARTY_REPO   = esp-hal-3rdparty
ifndef ESP_HAL_3RDPARTY_VERSION
	ESP_HAL_3RDPARTY_VERSION = 9fc713a95b1ff150dd0b0647e465d3c624056bb1
endif

ifndef ESP_HAL_3RDPARTY_URL
	ESP_HAL_3RDPARTY_URL = https://github.com/espressif/esp-hal-3rdparty.git
endif

# When set USE_NXTMPDIR_ESP_REPO_DIRECTLY=y, will directly use esp-hal-3rdparty
# under nxtmpdir without CHECK_COMMITSHA, reset, checkout and update.

USE_NXTMPDIR_ESP_REPO_DIRECTLY ?= n

ifeq ($(STORAGETMP),y)
ifeq ($(USE_NXTMPDIR_ESP_REPO_DIRECTLY),y)
define CLONE_ESP_HAL_3RDPARTY_REPO
	$(call COPYDIR, $(NXTMPDIR)/$(ESP_HAL_3RDPARTY_REPO),chip/$(ESP_HAL_3RDPARTY_REPO))
endef
else
define CLONE_ESP_HAL_3RDPARTY_REPO
	$(call CHECK_COMMITSHA, $(NXTMPDIR)/$(ESP_HAL_3RDPARTY_REPO),$(ESP_HAL_3RDPARTY_VERSION))
	$(call CLONE, $(ESP_HAL_3RDPARTY_URL),chip/$(ESP_HAL_3RDPARTY_REPO),$(NXTMPDIR)/$(ESP_HAL_3RDPARTY_REPO))
endef
endif
else
define CLONE_ESP_HAL_3RDPARTY_REPO
	$(call CLONE, $(ESP_HAL_3RDPARTY_URL),chip/$(ESP_HAL_3RDPARTY_REPO))
endef
endif

chip/$(ESP_HAL_3RDPARTY_REPO):
	$(Q) echo "Cloning Espressif HAL for 3rd Party Platforms"
	$(Q) $(call CLONE_ESP_HAL_3RDPARTY_REPO)
ifneq ($(USE_NXTMPDIR_ESP_REPO_DIRECTLY),y)
	$(Q) echo "Espressif HAL for 3rd Party Platforms: ${ESP_HAL_3RDPARTY_VERSION}"
	$(Q) git -C chip/$(ESP_HAL_3RDPARTY_REPO) checkout --quiet $(ESP_HAL_3RDPARTY_VERSION)
endif

# Silent preprocessor warnings

CFLAGS += -Wno-undef -Wno-unused-variable -fno-jump-tables -fno-tree-switch-conversion

# Enable strict volatile bitfield access

CFLAGS += -fstrict-volatile-bitfields

CHIP_SERIES = $(patsubst "%",%,$(CONFIG_ESPRESSIF_CHIP_SERIES))

include chip/hal.mk

include common/espressif/Make.defs

include chip/Bootloader.mk

context:: chip/$(ESP_HAL_3RDPARTY_REPO)
ifeq ($(CONFIG_ESPRESSIF_WIRELESS),y)
ifneq ($(USE_NXTMPDIR_ESP_REPO_DIRECTLY),y)
	$(Q) echo "Espressif HAL for 3rd Party Platforms: initializing submodules..."
	$(Q) git -C chip/$(ESP_HAL_3RDPARTY_REPO) submodule --quiet update --init $(GIT_DEPTH_PARAMETER) components/mbedtls/mbedtls components/esp_phy/lib components/esp_wifi/lib
	$(Q) git -C chip/$(ESP_HAL_3RDPARTY_REPO)/components/mbedtls/mbedtls reset --quiet --hard
endif
	$(Q) echo "Applying patches..."
	$(Q) cd chip/$(ESP_HAL_3RDPARTY_REPO)/components/mbedtls/mbedtls && git apply ../../../nuttx/patches/components/mbedtls/mbedtls/*.patch
endif

distclean::
	$(call DELDIR,chip/$(ESP_HAL_3RDPARTY_REPO))