############################################################################
# arch/arm/src/nrf91/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 armv8-m/Make.defs

CHIP_CSRCS += nrf91_start.c nrf91_clockconfig.c nrf91_irq.c nrf91_utils.c
CHIP_CSRCS += nrf91_allocateheap.c nrf91_lowputc.c nrf91_gpio.c
CHIP_CSRCS += nrf91_uid.c nrf91_spu.c nrf91_errata.c

ifeq ($(CONFIG_NRF91_SYSTIMER_SYSTICK),y)
CHIP_CSRCS += nrf91_systick.c
endif

ifeq ($(CONFIG_NRF91_SYSTIMER_RTC),y)
CHIP_CSRCS += nrf91_tickless_rtc.c
endif

ifeq ($(CONFIG_NRF91_PROGMEM),y)
CHIP_CSRCS += nrf91_flash.c
endif

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

ifeq ($(CONFIG_NRF91_GPIOTE),y)
CHIP_CSRCS += nrf91_gpiote.c
endif

CHIP_CSRCS += nrf91_serial.c

ifeq ($(CONFIG_NRF91_SPI_MASTER),y)
CHIP_CSRCS += nrf91_spi.c
endif

ifeq ($(CONFIG_NRF91_I2C_MASTER),y)
CHIP_CSRCS += nrf91_i2c.c
endif

ifeq ($(CONFIG_NRF91_TIMER),y)
CHIP_CSRCS += nrf91_tim.c
ifeq ($(CONFIG_TIMER),y)
CHIP_CSRCS += nrf91_tim_lowerhalf.c
endif
endif

ifeq ($(CONFIG_NRF91_RTC),y)
CHIP_CSRCS += nrf91_rtc.c
endif

ifeq ($(CONFIG_NRF91_PWM),y)
CHIP_CSRCS += nrf91_pwm.c
endif

ifeq ($(CONFIG_NRF91_SAADC),y)
CHIP_CSRCS += nrf91_adc.c
endif

ifeq ($(CONFIG_PM),y)
CHIP_CSRCS += nrf91_pminitialize.c
endif

ifeq ($(CONFIG_NRF91_MODEM),y)

NRFXLIB_UNPACK  := sdk-nrfxlib
NRFXLIB_VER     := 2.4.0
NRFXLIB_REF     := v$(NRFXLIB_VER)
NRFXLIB_TGZ     := $(NRFXLIB_REF).tar.gz
NRFXLIB_URL     := https://github.com/nrfconnect/sdk-nrfxlib/archive

$(NRFXLIB_TGZ):
	$(call DOWNLOAD,$(NRFXLIB_URL),$(NRFXLIB_TGZ),chip/$(NRFXLIB_TGZ))

chip/$(NRFXLIB_UNPACK): $(NRFXLIB_TGZ)
	$(Q) echo "Unpacking: NRXFLIB"
	$(Q) cd chip && tar zxf $(NRFXLIB_TGZ)
	$(Q) mv chip/$(NRFXLIB_UNPACK)-$(NRFXLIB_VER)* chip/$(NRFXLIB_UNPACK)
	$(Q) touch chip/$(NRFXLIB_UNPACK)

ifeq ($(wildcard chip/$(NRFXLIB_UNPACK)/.git),)
context:: chip/$(NRFXLIB_UNPACK)

distclean::
	$(call DELFILE, chip/$(NRFXLIB_TGZ))
	$(call DELDIR, chip/$(NRFXLIB_UNPACK))
endif

CHIP_CSRCS += nrf91_modem.c nrf91_modem_os.c nrf91_nrfx_ipc.c nrf91_modem_sock.c

ifeq ($(CONFIG_NRF91_MODEM_AT),y)
CHIP_CSRCS += nrf91_modem_at.c
endif

ifeq ($(CONFIG_NRF91_MODEM_GNSS),y)
CHIP_CSRCS += nrf91_modem_gnss.c
endif

NRFXLIB_DIR = $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)sdk-nrfxlib

INCLUDES += \
	${INCDIR_PREFIX}$(NRFXLIB_DIR)$(DELIM)nrf_modem$(DELIM)include

ifeq ($(CONFIG_ARCH_FPU),y)
  LIB_VARIANT=hard-float
else
  LIB_VARIANT=soft-float
endif

EXTRA_LIBPATHS += \
	-L $(NRFXLIB_DIR)$(DELIM)nrf_modem$(DELIM)lib$(DELIM)cortex-m33$(DELIM)$(LIB_VARIANT)

ifeq ($(CONFIG_NRF91_MODEM_LOG),y)
  EXTRA_LIBS += -lmodem_log
else
  EXTRA_LIBS += -lmodem
endif

endif