261 lines
7.2 KiB
C
261 lines
7.2 KiB
C
/*
|
|
* Copyright (c) 2016, Freescale Semiconductor, Inc.
|
|
* Copyright 2016-2020,2022-2023 NXP
|
|
* All rights reserved.
|
|
*
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
/*******************************************************************************
|
|
* Includes
|
|
******************************************************************************/
|
|
#include "lwip/opt.h"
|
|
|
|
#if LWIP_TCP
|
|
|
|
#include "lwip/apps/httpd.h"
|
|
#include "lwip/timeouts.h"
|
|
#include "lwip/init.h"
|
|
#include "netif/ethernet.h"
|
|
#include "ethernetif.h"
|
|
|
|
#include "pin_mux.h"
|
|
#include "board.h"
|
|
#ifndef configMAC_ADDR
|
|
#include "fsl_silicon_id.h"
|
|
#endif
|
|
#include "fsl_phy.h"
|
|
|
|
#include "fsl_iomuxc.h"
|
|
#include "fsl_enet.h"
|
|
#include "fsl_phyksz8081.h"
|
|
/*******************************************************************************
|
|
* Definitions
|
|
******************************************************************************/
|
|
|
|
/* @TEST_ANCHOR */
|
|
|
|
/* IP address configuration. */
|
|
#ifndef configIP_ADDR0
|
|
#define configIP_ADDR0 192
|
|
#endif
|
|
#ifndef configIP_ADDR1
|
|
#define configIP_ADDR1 168
|
|
#endif
|
|
#ifndef configIP_ADDR2
|
|
#define configIP_ADDR2 0
|
|
#endif
|
|
#ifndef configIP_ADDR3
|
|
#define configIP_ADDR3 102
|
|
#endif
|
|
|
|
/* Netmask configuration. */
|
|
#ifndef configNET_MASK0
|
|
#define configNET_MASK0 255
|
|
#endif
|
|
#ifndef configNET_MASK1
|
|
#define configNET_MASK1 255
|
|
#endif
|
|
#ifndef configNET_MASK2
|
|
#define configNET_MASK2 255
|
|
#endif
|
|
#ifndef configNET_MASK3
|
|
#define configNET_MASK3 0
|
|
#endif
|
|
|
|
/* Gateway address configuration. */
|
|
#ifndef configGW_ADDR0
|
|
#define configGW_ADDR0 192
|
|
#endif
|
|
#ifndef configGW_ADDR1
|
|
#define configGW_ADDR1 168
|
|
#endif
|
|
#ifndef configGW_ADDR2
|
|
#define configGW_ADDR2 0
|
|
#endif
|
|
#ifndef configGW_ADDR3
|
|
#define configGW_ADDR3 100
|
|
#endif
|
|
|
|
/* Ethernet configuration. */
|
|
extern phy_ksz8081_resource_t g_phy_resource;
|
|
#define EXAMPLE_ENET ENET
|
|
#define EXAMPLE_PHY_ADDRESS BOARD_ENET0_PHY_ADDRESS
|
|
#define EXAMPLE_PHY_OPS &phyksz8081_ops
|
|
#define EXAMPLE_PHY_RESOURCE &g_phy_resource
|
|
#define EXAMPLE_CLOCK_FREQ CLOCK_GetFreq(kCLOCK_IpgClk)
|
|
|
|
#ifndef EXAMPLE_NETIF_INIT_FN
|
|
/*! @brief Network interface initialization function. */
|
|
#define EXAMPLE_NETIF_INIT_FN ethernetif0_init
|
|
#endif /* EXAMPLE_NETIF_INIT_FN */
|
|
|
|
/*******************************************************************************
|
|
* Prototypes
|
|
******************************************************************************/
|
|
|
|
/*******************************************************************************
|
|
* Variables
|
|
******************************************************************************/
|
|
phy_ksz8081_resource_t g_phy_resource;
|
|
|
|
static phy_handle_t phyHandle;
|
|
|
|
/*******************************************************************************
|
|
* Code
|
|
******************************************************************************/
|
|
void BOARD_InitModuleClock(void)
|
|
{
|
|
const clock_enet_pll_config_t config = {.enableClkOutput = true, .enableClkOutput25M = false, .loopDivider = 1};
|
|
CLOCK_InitEnetPll(&config);
|
|
}
|
|
|
|
static void MDIO_Init(void)
|
|
{
|
|
(void)CLOCK_EnableClock(s_enetClock[ENET_GetInstance(EXAMPLE_ENET)]);
|
|
ENET_SetSMI(EXAMPLE_ENET, EXAMPLE_CLOCK_FREQ, false);
|
|
}
|
|
|
|
status_t MDIO_Write(uint8_t phyAddr, uint8_t regAddr, uint16_t data)
|
|
{
|
|
return ENET_MDIOWrite(EXAMPLE_ENET, phyAddr, regAddr, data);
|
|
}
|
|
|
|
status_t MDIO_Read(uint8_t phyAddr, uint8_t regAddr, uint16_t *pData)
|
|
{
|
|
return ENET_MDIORead(EXAMPLE_ENET, phyAddr, regAddr, pData);
|
|
}
|
|
|
|
|
|
|
|
#if LWIP_IPV6
|
|
static void print_ipv6_addresses(struct netif *netif)
|
|
{
|
|
for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
|
|
{
|
|
const char *str_ip = "-";
|
|
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)))
|
|
{
|
|
str_ip = ip6addr_ntoa(netif_ip6_addr(netif, i));
|
|
}
|
|
PRINTF(" IPv6 Address%d : %s\r\n", i, str_ip);
|
|
}
|
|
}
|
|
|
|
static void netif_ipv6_callback(struct netif *cb_netif)
|
|
{
|
|
PRINTF("IPv6 address update, valid addresses:\r\n");
|
|
print_ipv6_addresses(cb_netif);
|
|
PRINTF("\r\n");
|
|
}
|
|
#endif /* LWIP_IPV6 */
|
|
|
|
/*!
|
|
* @brief Interrupt service for SysTick timer.
|
|
*/
|
|
void SysTick_Handler(void)
|
|
{
|
|
time_isr();
|
|
}
|
|
|
|
/*!
|
|
* @brief Main function
|
|
*/
|
|
int main(void)
|
|
{
|
|
struct netif netif;
|
|
#if LWIP_IPV4
|
|
ip4_addr_t netif_ipaddr, netif_netmask, netif_gw;
|
|
#endif /* LWIP_IPV4 */
|
|
ethernetif_config_t enet_config = {.phyHandle = &phyHandle,
|
|
.phyAddr = EXAMPLE_PHY_ADDRESS,
|
|
.phyOps = EXAMPLE_PHY_OPS,
|
|
.phyResource = EXAMPLE_PHY_RESOURCE,
|
|
#ifdef configMAC_ADDR
|
|
.macAddress = configMAC_ADDR
|
|
#endif
|
|
};
|
|
|
|
BOARD_ConfigMPU();
|
|
BOARD_InitBootPins();
|
|
BOARD_InitBootClocks();
|
|
BOARD_InitDebugConsole();
|
|
BOARD_InitModuleClock();
|
|
|
|
IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);
|
|
|
|
/* Hardware reset PHY. */
|
|
BOARD_ENET_PHY_RESET;
|
|
|
|
MDIO_Init();
|
|
g_phy_resource.read = MDIO_Read;
|
|
g_phy_resource.write = MDIO_Write;
|
|
|
|
time_init();
|
|
|
|
/* Set MAC address. */
|
|
#ifndef configMAC_ADDR
|
|
(void)SILICONID_ConvertToMacAddr(&enet_config.macAddress);
|
|
#endif
|
|
|
|
/* Get clock after hardware init. */
|
|
enet_config.srcClockHz = EXAMPLE_CLOCK_FREQ;
|
|
|
|
#if LWIP_IPV4
|
|
IP4_ADDR(&netif_ipaddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3);
|
|
IP4_ADDR(&netif_netmask, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3);
|
|
IP4_ADDR(&netif_gw, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3);
|
|
#endif /* LWIP_IPV4 */
|
|
|
|
lwip_init();
|
|
|
|
#if LWIP_IPV4
|
|
netif_add(&netif, &netif_ipaddr, &netif_netmask, &netif_gw, &enet_config, EXAMPLE_NETIF_INIT_FN, ethernet_input);
|
|
#else
|
|
netif_add(&netif, &enet_config, EXAMPLE_NETIF_INIT_FN, ethernet_input);
|
|
#endif /* LWIP_IPV4 */
|
|
netif_set_default(&netif);
|
|
netif_set_up(&netif);
|
|
|
|
#if LWIP_IPV6
|
|
netif_create_ip6_linklocal_address(&netif, 1);
|
|
#endif /* LWIP_IPV6 */
|
|
|
|
while (ethernetif_wait_linkup(&netif, 5000) != ERR_OK)
|
|
{
|
|
PRINTF("PHY Auto-negotiation failed. Please check the cable connection and link partner setting.\r\n");
|
|
}
|
|
|
|
httpd_init();
|
|
|
|
#if LWIP_IPV6
|
|
set_ipv6_valid_state_cb(netif_ipv6_callback);
|
|
#endif /* LWIP_IPV6 */
|
|
|
|
PRINTF("\r\n***********************************************************\r\n");
|
|
PRINTF(" HTTP Server example\r\n");
|
|
PRINTF("***********************************************************\r\n");
|
|
#if LWIP_IPV4
|
|
PRINTF(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&netif_ipaddr)[0], ((u8_t *)&netif_ipaddr)[1],
|
|
((u8_t *)&netif_ipaddr)[2], ((u8_t *)&netif_ipaddr)[3]);
|
|
PRINTF(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&netif_netmask)[0], ((u8_t *)&netif_netmask)[1],
|
|
((u8_t *)&netif_netmask)[2], ((u8_t *)&netif_netmask)[3]);
|
|
PRINTF(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&netif_gw)[0], ((u8_t *)&netif_gw)[1],
|
|
((u8_t *)&netif_gw)[2], ((u8_t *)&netif_gw)[3]);
|
|
#endif /* LWIP_IPV4 */
|
|
#if LWIP_IPV6
|
|
print_ipv6_addresses(&netif);
|
|
#endif /* LWIP_IPV6 */
|
|
PRINTF("***********************************************************\r\n");
|
|
|
|
while (1)
|
|
{
|
|
/* Poll the driver, get any outstanding frames */
|
|
ethernetif_input(&netif);
|
|
|
|
sys_check_timeouts(); /* Handle all system timeouts for all core protocols */
|
|
}
|
|
}
|
|
#endif /* LWIP_TCP */
|