/*
 * Copyright 2020 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef _BOARD_H_
#define _BOARD_H_

#include "board_define.h"
#include "clock_config.h"
#include "fsl_common.h"
#include "fsl_gpio.h"
#include "fsl_clock.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
/*! @brief The board name */
#if TARGET_BOARD == BOARD_1170EVK
#define BOARD_NAME "MIMXRT1170-EVK"
#ifndef DEBUG_CONSOLE_UART_INDEX
#define DEBUG_CONSOLE_UART_INDEX 1
#endif
#elif TARGET_BOARD == BOARD_SMART_TLHMI
#define BOARD_NAME               "SMART_TLHMI"
#define DEBUG_CONSOLE_UART_INDEX 12
#elif
#error "No Target Board Define."
#endif

/* The UART to use for debug messages. */
#define BOARD_DEBUG_UART_TYPE     kSerialPort_Uart
#define BOARD_DEBUG_UART_CLK_FREQ 24000000

#if DEBUG_CONSOLE_UART_INDEX == 1
#define BOARD_DEBUG_UART_BASEADDR (uint32_t) LPUART1
#define BOARD_DEBUG_UART_INSTANCE 1U
#define BOARD_UART_IRQ            LPUART1_IRQn
#define BOARD_UART_IRQ_HANDLER    LPUART1_IRQHandler
#elif DEBUG_CONSOLE_UART_INDEX == 2
#define BOARD_DEBUG_UART_BASEADDR (uint32_t) LPUART2
#define BOARD_DEBUG_UART_INSTANCE 2U
#define BOARD_UART_IRQ            LPUART2_IRQn
#define BOARD_UART_IRQ_HANDLER    LPUART2_IRQHandler
#elif DEBUG_CONSOLE_UART_INDEX == 12
#define BOARD_DEBUG_UART_BASEADDR (uint32_t) LPUART12
#define BOARD_DEBUG_UART_INSTANCE 12U
#define BOARD_UART_IRQ            LPUART12_IRQn
#define BOARD_UART_IRQ_HANDLER    LPUART12_IRQHandler
#else
#error "Unsupported UART"
#endif

/* Hardware semaphore defines. */
#define BOARD_SEM4_BASE      SEMA4
#define BOARD_SEM4_PROC_NUM  1
#define BOARD_SEM4_UART_GATE 1

#ifndef BOARD_DEBUG_UART_BAUDRATE
#define BOARD_DEBUG_UART_BAUDRATE (115200U)
#endif /* BOARD_DEBUG_UART_BAUDRATE */

/* Definitions for eRPC MU transport layer */
#if defined(FSL_FEATURE_MU_SIDE_A)
#define MU_BASE        MUA
#define MU_IRQ         MUA_IRQn
#define MU_IRQ_HANDLER MUA_IRQHandler
#endif
#if defined(FSL_FEATURE_MU_SIDE_B)
#define MU_BASE        MUB
#define MU_IRQ         MUB_IRQn
#define MU_IRQ_HANDLER MUB_IRQHandler
#endif
#define MU_IRQ_PRIORITY (2)

/*! @brief The USER_LED used for board */
#if TARGET_BOARD == BOARD_1170EVK
#define LOGIC_LED_ON  (0U)
#define LOGIC_LED_OFF (1U)
#ifndef BOARD_USER_LED_GPIO
#define BOARD_USER_LED_GPIO GPIO9
#endif
#ifndef BOARD_USER_LED_GPIO_PIN
#define BOARD_USER_LED_GPIO_PIN (3U)
#endif
#elif TARGET_BOARD == BOARD_SMART_TLHMI
#define BOARD_LED_RED_GPIO   GPIO2
#define BOARD_LED_RED_PIN    (10U)
#define BOARD_LED_GREEN_GPIO GPIO2
#define BOARD_LED_GREEN_PIN  (11U)
#define BOARD_LED_BLUE_GPIO  GPIO2
#define BOARD_LED_BLUE_PIN   (12U)
#endif

#define USER_LED_INIT(output)                                            \
    GPIO_PinWrite(BOARD_USER_LED_GPIO, BOARD_USER_LED_GPIO_PIN, output); \
    BOARD_USER_LED_GPIO->GDIR |= (1U << BOARD_USER_LED_GPIO_PIN) /*!< Enable target USER_LED */
#define USER_LED_OFF() \
    GPIO_PortClear(BOARD_USER_LED_GPIO, 1U << BOARD_USER_LED_GPIO_PIN)                 /*!< Turn off target USER_LED */
#define USER_LED_ON() GPIO_PortSet(BOARD_USER_LED_GPIO, 1U << BOARD_USER_LED_GPIO_PIN) /*!<Turn on target USER_LED*/
#define USER_LED_TOGGLE()                                       \
    GPIO_PinWrite(BOARD_USER_LED_GPIO, BOARD_USER_LED_GPIO_PIN, \
                  0x1 ^ GPIO_PinRead(BOARD_USER_LED_GPIO, BOARD_USER_LED_GPIO_PIN)) /*!< Toggle target USER_LED */

/*! @brief The board switch buttons */
#define BOARD_BUTTON_SW1_GPIO GPIO2
#define BOARD_BUTTON_SW1_PIN  (15U)
#define BOARD_BUTTON_SW2_GPIO GPIO2
#define BOARD_BUTTON_SW2_PIN  (16U)
#define BOARD_BUTTON_SW3_GPIO GPIO2
#define BOARD_BUTTON_SW3_PIN  (17U)
#define BOARD_BUTTON_SW1_IRQ  GPIO2_Combined_0_15_IRQn
#define BOARD_BUTTON_SW2_IRQ  GPIO2_Combined_16_31_IRQn
#define BOARD_BUTTON_SW3_IRQ  GPIO2_Combined_16_31_IRQn

/*! @brief Define the port interrupt number for the board switches */
#ifndef BOARD_USER_BUTTON_GPIO
#define BOARD_USER_BUTTON_GPIO GPIO13
#endif
#ifndef BOARD_USER_BUTTON_GPIO_PIN
#define BOARD_USER_BUTTON_GPIO_PIN (0U)
#endif
#define BOARD_USER_BUTTON_IRQ         GPIO13_Combined_0_31_IRQn
#define BOARD_USER_BUTTON_IRQ_HANDLER GPIO13_Combined_0_31_IRQHandler
#define BOARD_USER_BUTTON_NAME        "SW7"

/*! @brief The board flash size */
#define BOARD_FLASH_SIZE (0x1000000U)

/* SKIP_SEMC_INIT can also be defined independently */
#ifdef USE_SDRAM
#define SKIP_SEMC_INIT
#endif

/*! @brief The ENET0 PHY address. */
#define BOARD_ENET0_PHY_ADDRESS (0x02U) /* Phy address of enet port 0. */

/*! @brief The ENET1 PHY address. */
#define BOARD_ENET1_PHY_ADDRESS (0x01U) /* Phy address of enet port 1. */

/*! @brief The EMVSIM SMARTCARD PHY configuration. */
#define BOARD_SMARTCARD_MODULE                (EMVSIM1)      /*!< SMARTCARD communicational module instance */
#define BOARD_SMARTCARD_MODULE_IRQ            (EMVSIM1_IRQn) /*!< SMARTCARD communicational module IRQ handler */
#define BOARD_SMARTCARD_CLOCK_MODULE_CLK_FREQ (CLOCK_GetRootClockFreq(kCLOCK_Root_Emv1))
#define BOARD_SMARTCARD_CLOCK_VALUE           (4000000U) /*!< SMARTCARD clock frequency */

/* USB PHY condfiguration */
#define BOARD_USB_PHY_D_CAL     (0x07U)
#define BOARD_USB_PHY_TXCAL45DP (0x06U)
#define BOARD_USB_PHY_TXCAL45DM (0x06U)

#define BOARD_ARDUINO_INT_IRQ   (GPIO1_INT3_IRQn)
#define BOARD_ARDUINO_I2C_IRQ   (LPI2C1_IRQn)
#define BOARD_ARDUINO_I2C_INDEX (1)

#define BOARD_HAS_SDCARD (1U)

/* @Brief Board accelerator sensor configuration */
#define BOARD_ACCEL_I2C_BASEADDR LPI2C5
/* Clock divider for LPI2C clock source */
#define BOARD_ACCEL_I2C_CLOCK_FREQ (CLOCK_GetRootClockFreq(kCLOCK_Root_Lpi2c5))

#define BOARD_CODEC_I2C_BASEADDR             LPI2C5
#define BOARD_CODEC_I2C_INSTANCE             5U
#define BOARD_CODEC_I2C_CLOCK_SOURCE_SELECT  (0U)
#define BOARD_CODEC_I2C_CLOCK_SOURCE_DIVIDER (6U)
#define BOARD_CODEC_I2C_CLOCK_FREQ           (24000000U)

/* @Brief Board CAMERA configuration */
/* MIPI Camera configurations */
#define BOARD_MIPI_CAMERA_I2C_BASE          LPI2C6
#define BOARD_MIPI_CAMERA_I2C_CLOCK_ROOT    kCLOCK_Root_Lpi2c6
#define BOARD_MIPI_CAMERA_I2C_CLOCK_SOURCE  (1U)  /* OSC24M. */
#define BOARD_MIPI_CAMERA_I2C_CLOCK_DIVIDER (4U) /* Divider = 4, LPI2C clock frequency 6M. */
#if TARGET_BOARD == BOARD_1170EVK
#define BOARD_MIPI_CAMERA_PWDN_GPIO  GPIO9
#define BOARD_MIPI_CAMERA_PWDN_PIN   25
#define BOARD_MIPI_CAMERA_RESET_GPIO GPIO11
#define BOARD_MIPI_CAMERA_RESET_PIN  15
#elif TARGET_BOARD == BOARD_SMART_TLHMI
#define BOARD_MIPI_CAMERA_PWDN_GPIO  GPIO13
#define BOARD_MIPI_CAMERA_PWDN_PIN   9
#define BOARD_MIPI_CAMERA_RESET_GPIO GPIO13
#define BOARD_MIPI_CAMERA_RESET_PIN  5
#endif

/* CSI Camera configurations */
#define BOARD_CSI_CAMERA_I2C_BASE          LPI2C6
#define BOARD_CSI_CAMERA_I2C_CLOCK_ROOT    kCLOCK_Root_Lpi2c6
#define BOARD_CSI_CAMERA_I2C_CLOCK_SOURCE  (kCLOCK_LPI2C6_ClockRoot_MuxOsc24MOut) /* OSC24M. */
#define BOARD_CSI_CAMERA_I2C_CLOCK_DIVIDER (4U) /* Divider = 4, LPI2C clock frequency 6M. */

#define BOARD_CSI_CAMERA_PWDN_GPIO  GPIO13
#define BOARD_CSI_CAMERA_PWDN_PIN   9
#define BOARD_CSI_CAMERA_RESET_GPIO GPIO2
#define BOARD_CSI_CAMERA_RESET_PIN  24

/* FlexIO Camera configurations */
#define BOARD_FLEXIO_CAMERA_I2C_BASE LPI2C5
#define BOARD_FLEXIO_CAMERA_I2C_CLOCK_ROOT kCLOCK_Root_Lpi2c5
#define BOARD_FLEXIO_CAMERA_I2C_CLOCK_SOURCE (1U)   /* OSC24M. */
#define BOARD_FLEXIO_CAMERA_I2C_CLOCK_DIVIDER (4U) /* Divider = 4, LPI2C clock frequency 6M. */

#define BOARD_FLEXIO_CAMERA_INST FLEXIO2
#define BOARD_FLEXIO_CAMERA_DATA_PIN_START_INDEX 24
#define BOARD_FLEXIO_CAMERA_DATA_PIN_END_INDEX 31
#define BOARD_FLEXIO_CAMERA_XCLK_PIN_INDEX 0
#define BOARD_FLEXIO_CAMERA_PCLK_PIN_INDEX 1
#define BOARD_FLEXIO_CAMERA_HREF_PIN_INDEX 2

#define BOARD_FLEXIO_CAMERA_VSYNC_GPIO_BASE GPIO3
#define BOARD_FLEXIO_CAMERA_VSYNC_PIN_INDEX 31u
#define BOARD_FLEXIO_CAMERA_VSYNC_IRQn GPIO3_Combined_16_31_IRQn
#define BOARD_FLEXIO_CAMERA_VSYNC_IRQHandler GPIO3_Combined_16_31_IRQHandler

#define BOARD_FLEXIO_CAMERA_PWDN_GPIO GPIO10
#define BOARD_FLEXIO_CAMERA_PWDN_PIN 13U
#define BOARD_FLEXIO_CAMERA_RESET_GPIO GPIO2
#define BOARD_FLEXIO_CAMERA_RESET_PIN 25U

/*! @brief The MIPI panel pins. */
#if TARGET_BOARD == BOARD_1170EVK
/* LCD panel. */
#define BOARD_MIPI_PANEL_RST_GPIO   GPIO9
#define BOARD_MIPI_PANEL_RST_PIN    1
#define BOARD_MIPI_PANEL_POWER_GPIO GPIO11
#define BOARD_MIPI_PANEL_POWER_PIN  16
#define BOARD_MIPI_PANEL_BL_GPIO    GPIO9
#define BOARD_MIPI_PANEL_BL_PIN     29

/* Touch panel. */
#define BOARD_MIPI_PANEL_TOUCH_I2C_BASEADDR      LPI2C5
#define BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_ROOT    kCLOCK_Root_Lpi2c5
#define BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_SOURCE  (1U)  /* OSC24M. */
#define BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_DIVIDER (4U) /* Divider = 4, LPI2C clock frequency 6M. */
#define BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_FREQ    CLOCK_GetRootClockFreq(BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_ROOT)
#define BOARD_MIPI_PANEL_TOUCH_RST_GPIO          GPIO9
#define BOARD_MIPI_PANEL_TOUCH_RST_PIN           0
#define BOARD_MIPI_PANEL_TOUCH_INT_GPIO          GPIO8
#define BOARD_MIPI_PANEL_TOUCH_INT_PIN           31
#elif TARGET_BOARD == BOARD_SMART_TLHMI
/* LCD panel. */
#define BOARD_MIPI_PANEL_RST_GPIO                GPIO8
#define BOARD_MIPI_PANEL_RST_PIN                 22
#define BOARD_MIPI_PANEL_POWER_GPIO              GPIO11
#define BOARD_MIPI_PANEL_POWER_PIN               16
#define BOARD_MIPI_PANEL_BL_GPIO                 GPIO9
#define BOARD_MIPI_PANEL_BL_PIN                  5

/* Touch panel. */
#define BOARD_MIPI_PANEL_TOUCH_I2C_BASEADDR      LPI2C5
#define BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_ROOT    kCLOCK_Root_Lpi2c5
#define BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_SOURCE  (1U)  /* OSC24M. */
#define BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_DIVIDER (4U) /* Divider = 4, LPI2C clock frequency 6M. */
#define BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_FREQ    CLOCK_GetRootClockFreq(BOARD_MIPI_PANEL_TOUCH_I2C_CLOCK_ROOT)
#define BOARD_MIPI_PANEL_TOUCH_RST_GPIO          GPIO8
#define BOARD_MIPI_PANEL_TOUCH_RST_PIN           21
#define BOARD_MIPI_PANEL_TOUCH_INT_GPIO          GPIO8
#define BOARD_MIPI_PANEL_TOUCH_INT_PIN           20
#endif

/* SD card detection method when using wifi module. */
#define BOARD_WIFI_SD_DETECT_TYPE kSDMMCHOST_DetectCardByHostDATA3

#define BOARD_BT_UART_INSTANCE    7
#define BOARD_BT_UART_BAUDRATE    3000000
#define BOARD_BT_UART_CLK_FREQ    CLOCK_GetRootClockFreq(kCLOCK_Root_Lpuart7);
#define BOARD_BT_UART_IRQ         LPUART7_IRQn
#define BOARD_BT_UART_IRQ_HANDLER LPUART7_IRQHandler

/*! @brief The Ethernet port used by network examples, default use 1G port. */
/* Below comment is for test script to easily define which port to be used, please don't delete. */
/* @TEST_ANCHOR */
#ifndef BOARD_NETWORK_USE_100M_ENET_PORT
#define BOARD_NETWORK_USE_100M_ENET_PORT (0U)
#endif

#define BOARD_FLEXSPI           FLEXSPI1
#define BOARD_FLEXSPI_CLOCK     kCLOCK_FlexSpi1
#define BOARD_FLEXSPI_AMBA_BASE FlexSPI1_AMBA_BASE

#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */

/*******************************************************************************
 * API
 ******************************************************************************/
uint32_t BOARD_DebugConsoleSrcFreq(void);

void BOARD_InitDebugConsole(void);

void BOARD_ConfigMPU(void);
void BOARD_LPI2C_Init(LPI2C_Type *base, uint32_t clkSrc_Hz);
status_t BOARD_LPI2C_Send(LPI2C_Type *base,
                          uint8_t deviceAddress,
                          uint32_t subAddress,
                          uint8_t subaddressSize,
                          uint8_t *txBuff,
                          uint8_t txBuffSize);
status_t BOARD_LPI2C_Receive(LPI2C_Type *base,
                             uint8_t deviceAddress,
                             uint32_t subAddress,
                             uint8_t subaddressSize,
                             uint8_t *rxBuff,
                             uint8_t rxBuffSize);
status_t BOARD_LPI2C_SendSCCB(LPI2C_Type *base,
                              uint8_t deviceAddress,
                              uint32_t subAddress,
                              uint8_t subaddressSize,
                              uint8_t *txBuff,
                              uint8_t txBuffSize);
status_t BOARD_LPI2C_ReceiveSCCB(LPI2C_Type *base,
                                 uint8_t deviceAddress,
                                 uint32_t subAddress,
                                 uint8_t subaddressSize,
                                 uint8_t *rxBuff,
                                 uint8_t rxBuffSize);
void BOARD_Accel_I2C_Init(void);
status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff);
status_t BOARD_Accel_I2C_Receive(
    uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize);
void BOARD_Codec_I2C_Init(void);
status_t BOARD_Codec_I2C_Send(
    uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize);
status_t BOARD_Codec_I2C_Receive(
    uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize);
status_t BOARD_MIPICameraI2CSend(
    uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize);
status_t BOARD_MIPICameraI2CReceive(
    uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize);
void BOARD_MIPICameraI2CInit(void);
void BOARD_MIPIPanelTouch_I2C_Init(void);
status_t BOARD_MIPIPanelTouch_I2C_Send(
    uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize);
status_t BOARD_MIPIPanelTouch_I2C_Receive(
    uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize);

void BOARD_InitEDMA();
void BOARD_SD_Pin_Config(uint32_t speed, uint32_t strength);
void BOARD_MMC_Pin_Config(uint32_t speed, uint32_t strength);

#if defined(__cplusplus)
}
#endif /* __cplusplus */

#endif /* _BOARD_H_ */
