/* * Copyright (c) 2021-2024 HPMicro * * SPDX-License-Identifier: BSD-3-Clause * */ /*---------------------------------------------------------------------* * Includes *---------------------------------------------------------------------*/ #include "common.h" #include "netconf.h" #include "sys_arch.h" #include "lwip.h" #include "lwip/init.h" #include "httpd.h" ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT) __RW enet_rx_desc_t dma_rx_desc_tab[ENET_RX_BUFF_COUNT] ; /* Ethernet Rx DMA Descriptor */ ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT) __RW enet_tx_desc_t dma_tx_desc_tab[ENET_TX_BUFF_COUNT] ; /* Ethernet Tx DMA Descriptor */ ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT) __RW uint8_t rx_buff[ENET_RX_BUFF_COUNT][ENET_RX_BUFF_SIZE]; /* Ethernet Receive Buffer */ ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT) __RW uint8_t tx_buff[ENET_TX_BUFF_COUNT][ENET_TX_BUFF_SIZE]; /* Ethernet Transmit Buffer */ enet_desc_t desc; uint8_t mac[ENET_MAC]; #if defined(__ENABLE_ENET_RECEIVE_INTERRUPT) && __ENABLE_ENET_RECEIVE_INTERRUPT volatile bool rx_flag; #endif struct netif gnetif; /*---------------------------------------------------------------------* * Initialization *---------------------------------------------------------------------*/ hpm_stat_t enet_init(ENET_Type *ptr) { enet_int_config_t int_config = {.int_enable = 0, .int_mask = 0}; enet_mac_config_t enet_config; enet_tx_control_config_t enet_tx_control_config; #if defined(RGMII) && RGMII #if defined(__USE_DP83867) && __USE_DP83867 dp83867_config_t phy_config; #else rtl8211_config_t phy_config; #endif #else #if defined(__USE_DP83848) && __USE_DP83848 dp83848_config_t phy_config; #else rtl8201_config_t phy_config; #endif #endif /* Initialize td, rd and the corresponding buffers */ memset((uint8_t *)dma_tx_desc_tab, 0x00, sizeof(dma_tx_desc_tab)); memset((uint8_t *)dma_rx_desc_tab, 0x00, sizeof(dma_rx_desc_tab)); memset((uint8_t *)rx_buff, 0x00, sizeof(rx_buff)); memset((uint8_t *)tx_buff, 0x00, sizeof(tx_buff)); desc.tx_desc_list_head = (enet_tx_desc_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)dma_tx_desc_tab); desc.rx_desc_list_head = (enet_rx_desc_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)dma_rx_desc_tab); desc.tx_buff_cfg.buffer = core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)tx_buff); desc.tx_buff_cfg.count = ENET_TX_BUFF_COUNT; desc.tx_buff_cfg.size = ENET_TX_BUFF_SIZE; desc.rx_buff_cfg.buffer = core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)rx_buff); desc.rx_buff_cfg.count = ENET_RX_BUFF_COUNT; desc.rx_buff_cfg.size = ENET_RX_BUFF_SIZE; /*Get a default control config for tx descriptor */ enet_get_default_tx_control_config(ENET, &enet_tx_control_config); /* Set the control config for tx descriptor */ memcpy(&desc.tx_control_config, &enet_tx_control_config, sizeof(enet_tx_control_config_t)); /* Get MAC address */ enet_get_mac_address(mac); /* Set MAC0 address */ enet_config.mac_addr_high[0] = mac[5] << 8 | mac[4]; enet_config.mac_addr_low[0] = mac[3] << 24 | mac[2] << 16 | mac[1] << 8 | mac[0]; enet_config.valid_max_count = 1; /* Set DMA PBL */ enet_config.dma_pbl = board_get_enet_dma_pbl(ENET); /* Set SARC */ enet_config.sarc = enet_sarc_replace_mac0; #if defined(__ENABLE_ENET_RECEIVE_INTERRUPT) && __ENABLE_ENET_RECEIVE_INTERRUPT /* Enable Enet IRQ */ board_enable_enet_irq(ENET); /* Set the interrupt enable mask */ int_config.int_enable = enet_normal_int_sum_en /* Enable normal interrupt summary */ | enet_receive_int_en; /* Enable receive interrupt */ int_config.int_mask = enet_rgsmii_int_mask; /* Disable RGSMII interrupt */ #endif int_config.mmc_intr_mask_rx = 0x03ffffff; /* Disable all mmc rx interrupt events */ int_config.mmc_intr_mask_tx = 0x03ffffff; /* Disable all mmc tx interrupt events */ /* Initialize enet controller */ enet_controller_init(ptr, ENET_INF_TYPE, &desc, &enet_config, &int_config); #if defined(__ENABLE_ENET_RECEIVE_INTERRUPT) && __ENABLE_ENET_RECEIVE_INTERRUPT /* Disable LPI interrupt */ enet_disable_lpi_interrupt(ENET); #endif /* Initialize phy */ #if defined(RGMII) && RGMII #if defined(__USE_DP83867) && __USE_DP83867 dp83867_reset(ptr); #if defined(__DISABLE_AUTO_NEGO) && __DISABLE_AUTO_NEGO dp83867_set_mdi_crossover_mode(ENET, enet_phy_mdi_crossover_manual_mdix); #endif dp83867_basic_mode_default_config(ptr, &phy_config); if (dp83867_basic_mode_init(ptr, &phy_config) == true) { #else rtl8211_reset(ptr); rtl8211_basic_mode_default_config(ptr, &phy_config); if (rtl8211_basic_mode_init(ptr, &phy_config) == true) { #endif #else #if defined(__USE_DP83848) && __USE_DP83848 dp83848_reset(ptr); dp83848_basic_mode_default_config(ptr, &phy_config); if (dp83848_basic_mode_init(ptr, &phy_config) == true) { #else rtl8201_reset(ptr); rtl8201_basic_mode_default_config(ptr, &phy_config); if (rtl8201_basic_mode_init(ptr, &phy_config) == true) { #endif #endif printf("Enet phy init passed !\n"); return status_success; } else { printf("Enet phy init failed !\n"); return status_fail; } } /*---------------------------------------------------------------------* * Main / *---------------------------------------------------------------------*/ int main(void) { /* Initialize BSP */ board_init(); /* Initialize GPIOs */ board_init_enet_pins(ENET); /* Reset an enet PHY */ board_reset_enet_phy(ENET); #if defined(__ENABLE_ENET_RECEIVE_INTERRUPT) && __ENABLE_ENET_RECEIVE_INTERRUPT printf("This is an ethernet demo: HTTP Server (Interrupt Usage)\n"); #else printf("This is an ethernet demo: HTTP Server (Polling Usage)\n"); #endif printf("LwIP Version: %s\n", LWIP_VERSION_STRING); /* Set RGMII clock delay */ #if defined(RGMII) && RGMII board_init_enet_rgmii_clock_delay(ENET); #else /* Set RMII reference clock */ board_init_enet_rmii_reference_clock(ENET, BOARD_ENET_RMII_INT_REF_CLK); printf("Reference Clock: %s\n", BOARD_ENET_RMII_INT_REF_CLK ? "Internal Clock" : "External Clock"); #endif /* Start a board timer */ board_timer_create(LWIP_APP_TIMER_INTERVAL, sys_timer_callback); /* Initialize MAC and DMA */ if (enet_init(ENET) == 0) { /* Initialize the Lwip stack */ lwip_init(); netif_config(&gnetif); /* Start services */ enet_services(&gnetif); /* Initialize the httpd */ httpd_init(); while (1) { enet_common_handler(&gnetif); } } else { printf("Enet initialization fails !!!\n"); while (1) { } } return 0; }