/*
 * @brief Ethernet MAC1 interface
 */


#ifndef ENET_MAC_IF1_H_INCLUDED
#define ENET_MAC_IF1_H_INCLUDED

#include "event_groups.h"

#include "lwip/err.h"
#include "fsl_enet.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
/* MAC address configuration. */
#define configMAC1_ADDR0 0x02
#define configMAC1_ADDR1 0x12
#define configMAC1_ADDR2 0x13
#define configMAC1_ADDR3 0x10
#define configMAC1_ADDR4 0x16
#if PTP_APP_MASTER_TEST
#define configMAC1_ADDR5 0x11
#else
#if VOLTAGE_SAMPLE
#define configMAC1_ADDR5 0x12
#else
#define configMAC1_ADDR5 0x13
#endif
#endif

/* 1588 timer clock frequence */
#define PTP_CLOCK_FRE_RT        	25000000U

/* Enable test */
#define PTP_TEST_APP_ENABLE			true

/* Default 1588 timer channel used for output compare */
#define PTP_TEST_APP_CHANNEL		kENET_PtpTimerChannel3


#ifndef ENET_RXBD_NUM
    #define ENET_RXBD_NUM (5)
#endif
#ifndef ENET_TXBD_NUM
#if defined(FSL_FEATURE_SOC_LPC_ENET_COUNT) && (FSL_FEATURE_SOC_LPC_ENET_COUNT > 0)
    #define ENET_TXBD_NUM (5)
#else
    #define ENET_TXBD_NUM (3)
#endif
#endif
#ifndef ENET_RXBUFF_SIZE
    #define ENET_RXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
#endif
#ifndef ENET_TXBUFF_SIZE
    #define ENET_TXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
#endif

#define ENET_OK             (0U)
#define ENET_ERROR          (0xFFU)
#define ENET_TIMEOUT        (0xFFFU)

/* ENET IRQ priority. Used in FreeRTOS. */
/* Interrupt priorities. */
#ifdef __CA7_REV
#ifndef ENET_PRIORITY
    #define ENET_PRIORITY       (21U)
#endif
#ifndef ENET_1588_PRIORITY
    #define ENET_1588_PRIORITY  (20U)
#endif
#else
#ifndef ENET_PRIORITY
    #define ENET_PRIORITY       (7U)
#endif
#ifndef ENET_1588_PRIORITY
    #define ENET_1588_PRIORITY  (5U)
#endif
#endif

/*  Defines Ethernet Autonegotiation Timeout during initialization.
 *  Set it to 0 to disable the waiting. */
#ifndef ENET_ATONEGOTIATION_TIMEOUT
    #define ENET_ATONEGOTIATION_TIMEOUT     (0xFFFU)
#endif


 // Helper struct to hold data for configuration of ethernet interface.
 //
/*
typedef struct ethernetif_config
{
    clock_name_t clockName;
    uint8_t macAddress[6];
} ethernetif_config_t;
*/
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */

typedef void (*process_rx_callback_fn) (void *ctx);
typedef bool (*sav_rx_callback_fn) (uint8_t *sav_mac_addr);


struct ethernet_savif
{
    process_rx_callback_fn process_rx_buffer;
    sav_rx_callback_fn sav_rx_enabled;
};


/**
 * This function should be passed as a parameter to netif_add()
 * if you initialize the second ENET interface.
 */
void set_sav_interface(struct ethernet_savif *sav_intf);

const uint8_t* netGetHwAddr(void);

// void enetif1_init(void);
void enetif0_init(void);

/**
 * This function should be called when a packet is ready to be read
 * from the interface.
 * It is used by bare-metal applications.
 *
 * @param netif the lwip network interface structure for this ethernetif
 */


#if PTP_ENABLED
#include "lwip/pbuf.h"
#include "lwip/prot/ethernet.h"

#define ENET_NANOSECOND_ONE_SECOND	1000000000U
#define PTP_AT_INC              (ENET_NANOSECOND_ONE_SECOND/PTP_CLOCK_FRE_RT)


void ethernet_ptptime_settime(enet_ptp_time_t *timestamp);
void ethernet_ptptime_gettime(enet_ptp_time_t *timestamp);
void ethernet_ptptime_adjfreq(int32_t incps);
void ethernet_ptptime_adjtime(int32_t second, int32_t nano_second);
err_t enet_get_rxframe_time(enet_ptp_time_data_t *ptpTimeData);
err_t enet_get_txframe_time(enet_ptp_time_data_t *ptpTimeData);
struct ethernetif *enet_get_ethenetif(void);
err_t enet1_send_frame(unsigned char *data, const uint16_t length);

void enet_mac1_input(void);

// For PTPD and process bus based on ethernet
// err_t mac1_linkoutput(struct pbuf *p);

/*
err_t enet_mac1_output(struct pbuf* p,
                const struct eth_addr* src, const struct eth_addr* dst,
                u16_t eth_type);
*/

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

#endif   /* ENET_MAC_IF1_H_INCLUDED */
